1 /* Copyright 2002 Andi Kleen */
3 #include <linux/linkage.h>
5 #include <asm/cpufeature.h>
6 #include <asm/dwarf2.h>
7 #include <asm/alternative-asm.h>
10 * memcpy - Copy a memory block.
18 * rax original destination
22 * memcpy_c() - fast string ops (REP MOVSQ) based variant.
24 * This gets patched over the unrolled variant (below) via the
25 * alternative instructions framework:
27 .section .altinstr_replacement, "ax", @progbits
41 * memcpy_c_e() - enhanced fast string memcpy. This is faster and simpler than
42 * memcpy_c. Use memcpy_c_e when possible.
44 * This gets patched over the unrolled variant (below) via the
45 * alternative instructions framework:
47 .section .altinstr_replacement, "ax", @progbits
65 * We check whether memory false dependence could occur,
66 * then jump to corresponding copy mode.
75 * Move in blocks of 4x8 bytes:
88 jae .Lcopy_forward_loop
94 * Calculate copy position to tail.
100 * At most 3 ALU operations in one cycle,
101 * so append NOPS in the same 16 bytes trunk.
104 .Lcopy_backward_loop:
108 movq -3*8(%rsi), %r10
109 movq -4*8(%rsi), %r11
110 leaq -4*8(%rsi), %rsi
113 movq %r10, -3*8(%rdi)
114 movq %r11, -4*8(%rdi)
115 leaq -4*8(%rdi), %rdi
116 jae .Lcopy_backward_loop
119 * Calculate copy position to head.
129 * Move data from 16 bytes to 31 bytes.
133 movq -2*8(%rsi, %rdx), %r10
134 movq -1*8(%rsi, %rdx), %r11
137 movq %r10, -2*8(%rdi, %rdx)
138 movq %r11, -1*8(%rdi, %rdx)
145 * Move data from 8 bytes to 15 bytes.
148 movq -1*8(%rsi, %rdx), %r9
150 movq %r9, -1*8(%rdi, %rdx)
158 * Move data from 4 bytes to 7 bytes.
161 movl -4(%rsi, %rdx), %r8d
163 movl %r8d, -4(%rdi, %rdx)
170 * Move data from 1 bytes to 3 bytes.
175 movzbq (%rsi, %rdx), %r9
177 movb %r9b, (%rdi, %rdx)
188 * Some CPUs are adding enhanced REP MOVSB/STOSB feature
189 * If the feature is supported, memcpy_c_e() is the first choice.
190 * If enhanced rep movsb copy is not available, use fast string copy
191 * memcpy_c() when possible. This is faster and code is simpler than
193 * Otherwise, original memcpy() is used.
194 * In .altinstructions section, ERMS feature is placed after REG_GOOD
195 * feature to implement the right patch order.
197 * Replace only beginning, memcpy is used to apply alternatives,
198 * so it is silly to overwrite itself with nops - reboot is the
201 .section .altinstructions, "a"
202 altinstruction_entry memcpy,.Lmemcpy_c,X86_FEATURE_REP_GOOD,\
203 .Lmemcpy_e-.Lmemcpy_c,.Lmemcpy_e-.Lmemcpy_c
204 altinstruction_entry memcpy,.Lmemcpy_c_e,X86_FEATURE_ERMS, \
205 .Lmemcpy_e_e-.Lmemcpy_c_e,.Lmemcpy_e_e-.Lmemcpy_c_e