1 /* Copyright 2002 Andi Kleen */
3 #include <linux/linkage.h>
5 #include <asm/cpufeatures.h>
6 #include <asm/alternative-asm.h>
9 * We build a jump to memcpy_orig by default which gets NOPped out on
10 * the majority of x86 CPUs which set REP_GOOD. In addition, CPUs which
11 * have the enhanced REP MOVSB/STOSB feature (ERMS), change those NOPs
12 * to a jmp to memcpy_erms which does the REP; MOVSB mem copy.
18 * memcpy - Copy a memory block.
26 * rax original destination
30 ALTERNATIVE_2 "jmp memcpy_orig", "", X86_FEATURE_REP_GOOD, \
31 "jmp memcpy_erms", X86_FEATURE_ERMS
45 * memcpy_erms() - enhanced fast string memcpy. This is faster and
46 * simpler than memcpy. Use memcpy_erms when possible.
62 * We check whether memory false dependence could occur,
63 * then jump to corresponding copy mode.
72 * Move in blocks of 4x8 bytes:
85 jae .Lcopy_forward_loop
91 * Calculate copy position to tail.
97 * At most 3 ALU operations in one cycle,
98 * so append NOPS in the same 16 bytes trunk.
101 .Lcopy_backward_loop:
105 movq -3*8(%rsi), %r10
106 movq -4*8(%rsi), %r11
107 leaq -4*8(%rsi), %rsi
110 movq %r10, -3*8(%rdi)
111 movq %r11, -4*8(%rdi)
112 leaq -4*8(%rdi), %rdi
113 jae .Lcopy_backward_loop
116 * Calculate copy position to head.
126 * Move data from 16 bytes to 31 bytes.
130 movq -2*8(%rsi, %rdx), %r10
131 movq -1*8(%rsi, %rdx), %r11
134 movq %r10, -2*8(%rdi, %rdx)
135 movq %r11, -1*8(%rdi, %rdx)
142 * Move data from 8 bytes to 15 bytes.
145 movq -1*8(%rsi, %rdx), %r9
147 movq %r9, -1*8(%rdi, %rdx)
155 * Move data from 4 bytes to 7 bytes.
158 movl -4(%rsi, %rdx), %r8d
160 movl %r8d, -4(%rdi, %rdx)
167 * Move data from 1 bytes to 3 bytes.
172 movzbq (%rsi, %rdx), %r9
174 movb %r9b, (%rdi, %rdx)
184 * memcpy_mcsafe - memory copy with machine check exception handling
185 * Note that we only catch machine checks when reading the source addresses.
186 * Writes to target are posted and don't generate machine checks.
190 /* Less than 8 bytes? Go to byte copy loop */
193 /* Check for bad alignment of source */
195 /* Already aligned */
198 /* Copy one byte at a time until source is 8-byte aligned */
204 .L_copy_leading_bytes:
210 jnz .L_copy_leading_bytes
213 /* Figure out how many whole cache lines (64-bytes) to copy */
217 jz .L_no_whole_cache_lines
219 /* Loop copying whole cache lines */
220 .L_cache_w0: movq (%rsi), %r8
221 .L_cache_w1: movq 1*8(%rsi), %r9
222 .L_cache_w2: movq 2*8(%rsi), %r10
223 .L_cache_w3: movq 3*8(%rsi), %r11
228 .L_cache_w4: movq 4*8(%rsi), %r8
229 .L_cache_w5: movq 5*8(%rsi), %r9
230 .L_cache_w6: movq 6*8(%rsi), %r10
231 .L_cache_w7: movq 7*8(%rsi), %r11
241 /* Are there any trailing 8-byte words? */
242 .L_no_whole_cache_lines:
248 /* Copy trailing words */
249 .L_copy_trailing_words:
255 jnz .L_copy_trailing_words
257 /* Any trailing bytes? */
260 jz .L_done_memcpy_trap
262 /* Copy trailing bytes */
264 .L_copy_trailing_bytes:
270 jnz .L_copy_trailing_bytes
272 /* Copy successful. Return zero */
276 ENDPROC(memcpy_mcsafe)
278 .section .fixup, "ax"
279 /* Return -EFAULT for any failure */
280 .L_memcpy_mcsafe_fail:
286 _ASM_EXTABLE_FAULT(.L_copy_leading_bytes, .L_memcpy_mcsafe_fail)
287 _ASM_EXTABLE_FAULT(.L_cache_w0, .L_memcpy_mcsafe_fail)
288 _ASM_EXTABLE_FAULT(.L_cache_w1, .L_memcpy_mcsafe_fail)
289 _ASM_EXTABLE_FAULT(.L_cache_w3, .L_memcpy_mcsafe_fail)
290 _ASM_EXTABLE_FAULT(.L_cache_w3, .L_memcpy_mcsafe_fail)
291 _ASM_EXTABLE_FAULT(.L_cache_w4, .L_memcpy_mcsafe_fail)
292 _ASM_EXTABLE_FAULT(.L_cache_w5, .L_memcpy_mcsafe_fail)
293 _ASM_EXTABLE_FAULT(.L_cache_w6, .L_memcpy_mcsafe_fail)
294 _ASM_EXTABLE_FAULT(.L_cache_w7, .L_memcpy_mcsafe_fail)
295 _ASM_EXTABLE_FAULT(.L_copy_trailing_words, .L_memcpy_mcsafe_fail)
296 _ASM_EXTABLE_FAULT(.L_copy_trailing_bytes, .L_memcpy_mcsafe_fail)