1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright 2002 Andi Kleen */
4 #include <linux/linkage.h>
6 #include <asm/cpufeatures.h>
7 #include <asm/alternative-asm.h>
8 #include <asm/export.h>
10 .pushsection .noinstr.text, "ax"
13 * We build a jump to memcpy_orig by default which gets NOPped out on
14 * the majority of x86 CPUs which set REP_GOOD. In addition, CPUs which
15 * have the enhanced REP MOVSB/STOSB feature (ERMS), change those NOPs
16 * to a jmp to memcpy_erms which does the REP; MOVSB mem copy.
20 * memcpy - Copy a memory block.
28 * rax original destination
30 SYM_FUNC_START_ALIAS(__memcpy)
31 SYM_FUNC_START_WEAK(memcpy)
32 ALTERNATIVE_2 "jmp memcpy_orig", "", X86_FEATURE_REP_GOOD, \
33 "jmp memcpy_erms", X86_FEATURE_ERMS
44 SYM_FUNC_END_ALIAS(__memcpy)
46 EXPORT_SYMBOL(__memcpy)
49 * memcpy_erms() - enhanced fast string memcpy. This is faster and
50 * simpler than memcpy. Use memcpy_erms when possible.
52 SYM_FUNC_START_LOCAL(memcpy_erms)
57 SYM_FUNC_END(memcpy_erms)
59 SYM_FUNC_START_LOCAL(memcpy_orig)
66 * We check whether memory false dependence could occur,
67 * then jump to corresponding copy mode.
76 * Move in blocks of 4x8 bytes:
89 jae .Lcopy_forward_loop
95 * Calculate copy position to tail.
101 * At most 3 ALU operations in one cycle,
102 * so append NOPS in the same 16 bytes trunk.
105 .Lcopy_backward_loop:
109 movq -3*8(%rsi), %r10
110 movq -4*8(%rsi), %r11
111 leaq -4*8(%rsi), %rsi
114 movq %r10, -3*8(%rdi)
115 movq %r11, -4*8(%rdi)
116 leaq -4*8(%rdi), %rdi
117 jae .Lcopy_backward_loop
120 * Calculate copy position to head.
130 * Move data from 16 bytes to 31 bytes.
134 movq -2*8(%rsi, %rdx), %r10
135 movq -1*8(%rsi, %rdx), %r11
138 movq %r10, -2*8(%rdi, %rdx)
139 movq %r11, -1*8(%rdi, %rdx)
146 * Move data from 8 bytes to 15 bytes.
149 movq -1*8(%rsi, %rdx), %r9
151 movq %r9, -1*8(%rdi, %rdx)
159 * Move data from 4 bytes to 7 bytes.
162 movl -4(%rsi, %rdx), %r8d
164 movl %r8d, -4(%rdi, %rdx)
171 * Move data from 1 bytes to 3 bytes.
176 movzbq (%rsi, %rdx), %r9
178 movb %r9b, (%rdi, %rdx)
184 SYM_FUNC_END(memcpy_orig)