1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright 2002 Andi Kleen */
4 #include <linux/linkage.h>
6 #include <asm/cpufeatures.h>
7 #include <asm/alternative-asm.h>
10 * We build a jump to memcpy_orig by default which gets NOPped out on
11 * the majority of x86 CPUs which set REP_GOOD. In addition, CPUs which
12 * have the enhanced REP MOVSB/STOSB feature (ERMS), change those NOPs
13 * to a jmp to memcpy_erms which does the REP; MOVSB mem copy.
19 * memcpy - Copy a memory block.
27 * rax original destination
31 ALTERNATIVE_2 "jmp memcpy_orig", "", X86_FEATURE_REP_GOOD, \
32 "jmp memcpy_erms", X86_FEATURE_ERMS
46 * memcpy_erms() - enhanced fast string memcpy. This is faster and
47 * simpler than memcpy. Use memcpy_erms when possible.
63 * We check whether memory false dependence could occur,
64 * then jump to corresponding copy mode.
73 * Move in blocks of 4x8 bytes:
86 jae .Lcopy_forward_loop
92 * Calculate copy position to tail.
98 * At most 3 ALU operations in one cycle,
99 * so append NOPS in the same 16 bytes trunk.
102 .Lcopy_backward_loop:
106 movq -3*8(%rsi), %r10
107 movq -4*8(%rsi), %r11
108 leaq -4*8(%rsi), %rsi
111 movq %r10, -3*8(%rdi)
112 movq %r11, -4*8(%rdi)
113 leaq -4*8(%rdi), %rdi
114 jae .Lcopy_backward_loop
117 * Calculate copy position to head.
127 * Move data from 16 bytes to 31 bytes.
131 movq -2*8(%rsi, %rdx), %r10
132 movq -1*8(%rsi, %rdx), %r11
135 movq %r10, -2*8(%rdi, %rdx)
136 movq %r11, -1*8(%rdi, %rdx)
143 * Move data from 8 bytes to 15 bytes.
146 movq -1*8(%rsi, %rdx), %r9
148 movq %r9, -1*8(%rdi, %rdx)
156 * Move data from 4 bytes to 7 bytes.
159 movl -4(%rsi, %rdx), %r8d
161 movl %r8d, -4(%rdi, %rdx)
168 * Move data from 1 bytes to 3 bytes.
173 movzbq (%rsi, %rdx), %r9
175 movb %r9b, (%rdi, %rdx)
185 * memcpy_mcsafe_unrolled - memory copy with machine check exception handling
186 * Note that we only catch machine checks when reading the source addresses.
187 * Writes to target are posted and don't generate machine checks.
189 ENTRY(memcpy_mcsafe_unrolled)
191 /* Less than 8 bytes? Go to byte copy loop */
194 /* Check for bad alignment of source */
196 /* Already aligned */
199 /* Copy one byte at a time until source is 8-byte aligned */
205 .L_copy_leading_bytes:
211 jnz .L_copy_leading_bytes
214 /* Figure out how many whole cache lines (64-bytes) to copy */
218 jz .L_no_whole_cache_lines
220 /* Loop copying whole cache lines */
221 .L_cache_w0: movq (%rsi), %r8
222 .L_cache_w1: movq 1*8(%rsi), %r9
223 .L_cache_w2: movq 2*8(%rsi), %r10
224 .L_cache_w3: movq 3*8(%rsi), %r11
229 .L_cache_w4: movq 4*8(%rsi), %r8
230 .L_cache_w5: movq 5*8(%rsi), %r9
231 .L_cache_w6: movq 6*8(%rsi), %r10
232 .L_cache_w7: movq 7*8(%rsi), %r11
242 /* Are there any trailing 8-byte words? */
243 .L_no_whole_cache_lines:
249 /* Copy trailing words */
250 .L_copy_trailing_words:
256 jnz .L_copy_trailing_words
258 /* Any trailing bytes? */
261 jz .L_done_memcpy_trap
263 /* Copy trailing bytes */
265 .L_copy_trailing_bytes:
271 jnz .L_copy_trailing_bytes
273 /* Copy successful. Return zero */
277 ENDPROC(memcpy_mcsafe_unrolled)
279 .section .fixup, "ax"
280 /* Return -EFAULT for any failure */
281 .L_memcpy_mcsafe_fail:
287 _ASM_EXTABLE_FAULT(.L_copy_leading_bytes, .L_memcpy_mcsafe_fail)
288 _ASM_EXTABLE_FAULT(.L_cache_w0, .L_memcpy_mcsafe_fail)
289 _ASM_EXTABLE_FAULT(.L_cache_w1, .L_memcpy_mcsafe_fail)
290 _ASM_EXTABLE_FAULT(.L_cache_w2, .L_memcpy_mcsafe_fail)
291 _ASM_EXTABLE_FAULT(.L_cache_w3, .L_memcpy_mcsafe_fail)
292 _ASM_EXTABLE_FAULT(.L_cache_w4, .L_memcpy_mcsafe_fail)
293 _ASM_EXTABLE_FAULT(.L_cache_w5, .L_memcpy_mcsafe_fail)
294 _ASM_EXTABLE_FAULT(.L_cache_w6, .L_memcpy_mcsafe_fail)
295 _ASM_EXTABLE_FAULT(.L_cache_w7, .L_memcpy_mcsafe_fail)
296 _ASM_EXTABLE_FAULT(.L_copy_trailing_words, .L_memcpy_mcsafe_fail)
297 _ASM_EXTABLE_FAULT(.L_copy_trailing_bytes, .L_memcpy_mcsafe_fail)