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
42 * memcpy_c_e() - enhanced fast string memcpy. This is faster and simpler than
43 * memcpy_c. Use memcpy_c_e when possible.
45 * This gets patched over the unrolled variant (below) via the
46 * alternative instructions framework:
48 .section .altinstr_replacement, "ax", @progbits
64 * Use 32bit CMP here to avoid long NOP padding.
70 * We check whether memory false dependence could occur,
71 * then jump to corresponding copy mode.
80 * Move in blocks of 4x8 bytes:
93 jae .Lcopy_forward_loop
99 * Calculate copy position to tail.
105 * At most 3 ALU operations in one cycle,
106 * so append NOPS in the same 16bytes trunk.
109 .Lcopy_backward_loop:
113 movq -3*8(%rsi), %r10
114 movq -4*8(%rsi), %r11
115 leaq -4*8(%rsi), %rsi
118 movq %r10, -3*8(%rdi)
119 movq %r11, -4*8(%rdi)
120 leaq -4*8(%rdi), %rdi
121 jae .Lcopy_backward_loop
124 * Calculate copy position to head.
134 * Move data from 16 bytes to 31 bytes.
138 movq -2*8(%rsi, %rdx), %r10
139 movq -1*8(%rsi, %rdx), %r11
142 movq %r10, -2*8(%rdi, %rdx)
143 movq %r11, -1*8(%rdi, %rdx)
150 * Move data from 8 bytes to 15 bytes.
153 movq -1*8(%rsi, %rdx), %r9
155 movq %r9, -1*8(%rdi, %rdx)
163 * Move data from 4 bytes to 7 bytes.
166 movl -4(%rsi, %rdx), %r8d
168 movl %r8d, -4(%rdi, %rdx)
175 * Move data from 1 bytes to 3 bytes.
192 * Some CPUs are adding enhanced REP MOVSB/STOSB feature
193 * If the feature is supported, memcpy_c_e() is the first choice.
194 * If enhanced rep movsb copy is not available, use fast string copy
195 * memcpy_c() when possible. This is faster and code is simpler than
197 * Otherwise, original memcpy() is used.
198 * In .altinstructions section, ERMS feature is placed after REG_GOOD
199 * feature to implement the right patch order.
201 * Replace only beginning, memcpy is used to apply alternatives,
202 * so it is silly to overwrite itself with nops - reboot is the
205 .section .altinstructions, "a"
206 altinstruction_entry memcpy,.Lmemcpy_c,X86_FEATURE_REP_GOOD,\
207 .Lmemcpy_e-.Lmemcpy_c,.Lmemcpy_e-.Lmemcpy_c
208 altinstruction_entry memcpy,.Lmemcpy_c_e,X86_FEATURE_ERMS, \
209 .Lmemcpy_e_e-.Lmemcpy_c_e,.Lmemcpy_e_e-.Lmemcpy_c_e