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
67 * We check whether memory false dependence could occur,
68 * then jump to corresponding copy mode.
77 * Move in blocks of 4x8 bytes:
90 jae .Lcopy_forward_loop
96 * Calculate copy position to tail.
102 * At most 3 ALU operations in one cycle,
103 * so append NOPS in the same 16 bytes trunk.
106 .Lcopy_backward_loop:
110 movq -3*8(%rsi), %r10
111 movq -4*8(%rsi), %r11
112 leaq -4*8(%rsi), %rsi
115 movq %r10, -3*8(%rdi)
116 movq %r11, -4*8(%rdi)
117 leaq -4*8(%rdi), %rdi
118 jae .Lcopy_backward_loop
121 * Calculate copy position to head.
131 * Move data from 16 bytes to 31 bytes.
135 movq -2*8(%rsi, %rdx), %r10
136 movq -1*8(%rsi, %rdx), %r11
139 movq %r10, -2*8(%rdi, %rdx)
140 movq %r11, -1*8(%rdi, %rdx)
147 * Move data from 8 bytes to 15 bytes.
150 movq -1*8(%rsi, %rdx), %r9
152 movq %r9, -1*8(%rdi, %rdx)
160 * Move data from 4 bytes to 7 bytes.
163 movl -4(%rsi, %rdx), %r8d
165 movl %r8d, -4(%rdi, %rdx)
172 * Move data from 1 bytes to 3 bytes.
177 movzbq (%rsi, %rdx), %r9
179 movb %r9b, (%rdi, %rdx)
190 * Some CPUs are adding enhanced REP MOVSB/STOSB feature
191 * If the feature is supported, memcpy_c_e() is the first choice.
192 * If enhanced rep movsb copy is not available, use fast string copy
193 * memcpy_c() when possible. This is faster and code is simpler than
195 * Otherwise, original memcpy() is used.
196 * In .altinstructions section, ERMS feature is placed after REG_GOOD
197 * feature to implement the right patch order.
199 * Replace only beginning, memcpy is used to apply alternatives,
200 * so it is silly to overwrite itself with nops - reboot is the
203 .section .altinstructions, "a"
204 altinstruction_entry __memcpy,.Lmemcpy_c,X86_FEATURE_REP_GOOD,\
205 .Lmemcpy_e-.Lmemcpy_c,.Lmemcpy_e-.Lmemcpy_c
206 altinstruction_entry __memcpy,.Lmemcpy_c_e,X86_FEATURE_ERMS, \
207 .Lmemcpy_e_e-.Lmemcpy_c_e,.Lmemcpy_e_e-.Lmemcpy_c_e