1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Normally compiler builtins are used, but sometimes the compiler calls out
4 * of line code. Based on asm-i386/string.h.
6 * This assembly file is re-written from memmove_64.c file.
7 * - Copyright 2011 Fenghua Yu <fenghua.yu@intel.com>
9 #include <linux/export.h>
10 #include <linux/linkage.h>
11 #include <asm/cpufeatures.h>
12 #include <asm/alternative.h>
16 .section .noinstr.text, "ax"
19 * Implement memmove(). This can handle overlap between src and dst.
29 SYM_FUNC_START(__memmove)
33 /* Decide forward/backward copy mode */
35 jge .Lmemmove_begin_forward
41 #define CHECK_LEN cmp $0x20, %rdx; jb 1f
42 #define MEMMOVE_BYTES movq %rdx, %rcx; rep movsb; RET
43 .Lmemmove_begin_forward:
44 ALTERNATIVE_2 __stringify(CHECK_LEN), \
45 __stringify(CHECK_LEN; MEMMOVE_BYTES), X86_FEATURE_ERMS, \
46 __stringify(MEMMOVE_BYTES), X86_FEATURE_FSRM
49 * movsq instruction have many startup latency
50 * so we handle small size by general register.
55 * movsq instruction is only good for aligned case.
63 * We gobble 32 bytes forward in each loop.
82 * Handle data forward by movsq.
87 movq -8(%rsi, %rdx), %r11
88 lea -8(%rdi, %rdx), %r10
93 .Lmemmove_end_forward:
96 * Handle data backward by movsq.
103 leaq -8(%rsi, %rdx), %rsi
104 leaq -8(%rdi, %rdx), %rdi
113 * Start to prepare for backward copy.
125 * Calculate copy position to tail.
131 * We gobble 32 bytes backward in each loop.
135 movq -1*8(%rsi), %r11
136 movq -2*8(%rsi), %r10
139 leaq -4*8(%rsi), %rsi
141 movq %r11, -1*8(%rdi)
142 movq %r10, -2*8(%rdi)
145 leaq -4*8(%rdi), %rdi
148 * Calculate copy position to head.
157 * Move data from 16 bytes to 31 bytes.
161 movq -2*8(%rsi, %rdx), %r9
162 movq -1*8(%rsi, %rdx), %r8
165 movq %r9, -2*8(%rdi, %rdx)
166 movq %r8, -1*8(%rdi, %rdx)
173 * Move data from 8 bytes to 15 bytes.
176 movq -1*8(%rsi, %rdx), %r10
178 movq %r10, -1*8(%rdi, %rdx)
184 * Move data from 4 bytes to 7 bytes.
187 movl -4(%rsi, %rdx), %r10d
189 movl %r10d, -4(%rdi, %rdx)
195 * Move data from 2 bytes to 3 bytes.
198 movw -2(%rsi, %rdx), %r10w
200 movw %r10w, -2(%rdi, %rdx)
206 * Move data for 1 byte.
212 SYM_FUNC_END(__memmove)
213 EXPORT_SYMBOL(__memmove)
215 SYM_FUNC_ALIAS_MEMFUNC(memmove, __memmove)
216 EXPORT_SYMBOL(memmove)