1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright 2002 Andi Kleen */
4 #include <linux/export.h>
5 #include <linux/linkage.h>
7 #include <asm/cpufeatures.h>
8 #include <asm/alternative.h>
10 .section .noinstr.text, "ax"
13 * memcpy - Copy a memory block.
21 * rax original destination
23 * The FSRM alternative should be done inline (avoiding the call and
24 * the disgusting return handling), but that would require some help
25 * from the compiler for better calling conventions.
27 * The 'rep movsb' itself is small enough to replace the call, but the
28 * two register moves blow up the code. And one of them is "needed"
29 * only for the return value that is the same as the source input,
30 * which the compiler could/should do much better anyway.
32 SYM_TYPED_FUNC_START(__memcpy)
33 ALTERNATIVE "jmp memcpy_orig", "", X86_FEATURE_FSRM
39 SYM_FUNC_END(__memcpy)
40 EXPORT_SYMBOL(__memcpy)
42 SYM_FUNC_ALIAS_MEMFUNC(memcpy, __memcpy)
45 SYM_FUNC_START_LOCAL(memcpy_orig)
52 * We check whether memory false dependence could occur,
53 * then jump to corresponding copy mode.
62 * Move in blocks of 4x8 bytes:
75 jae .Lcopy_forward_loop
81 * Calculate copy position to tail.
87 * At most 3 ALU operations in one cycle,
88 * so append NOPS in the same 16 bytes trunk.
100 movq %r10, -3*8(%rdi)
101 movq %r11, -4*8(%rdi)
102 leaq -4*8(%rdi), %rdi
103 jae .Lcopy_backward_loop
106 * Calculate copy position to head.
116 * Move data from 16 bytes to 31 bytes.
120 movq -2*8(%rsi, %rdx), %r10
121 movq -1*8(%rsi, %rdx), %r11
124 movq %r10, -2*8(%rdi, %rdx)
125 movq %r11, -1*8(%rdi, %rdx)
132 * Move data from 8 bytes to 15 bytes.
135 movq -1*8(%rsi, %rdx), %r9
137 movq %r9, -1*8(%rdi, %rdx)
145 * Move data from 4 bytes to 7 bytes.
148 movl -4(%rsi, %rdx), %r8d
150 movl %r8d, -4(%rdi, %rdx)
157 * Move data from 1 bytes to 3 bytes.
162 movzbq (%rsi, %rdx), %r9
164 movb %r9b, (%rdi, %rdx)
170 SYM_FUNC_END(memcpy_orig)