blob: 0f8a0d0331b91715238f01e8f54ce74525fb0cc4 [file] [log] [blame]
Fenghua Yu9599ec02011-01-17 17:39:15 -08001/*
2 * Normally compiler builtins are used, but sometimes the compiler calls out
3 * of line code. Based on asm-i386/string.h.
4 *
5 * This assembly file is re-written from memmove_64.c file.
6 * - Copyright 2011 Fenghua Yu <fenghua.yu@intel.com>
7 */
Fenghua Yu9599ec02011-01-17 17:39:15 -08008#include <linux/linkage.h>
9#include <asm/dwarf2.h>
Fenghua Yu057e05c2011-05-17 15:29:17 -070010#include <asm/cpufeature.h>
Andy Lutomirski59e97e42011-07-13 09:24:10 -040011#include <asm/alternative-asm.h>
Fenghua Yu9599ec02011-01-17 17:39:15 -080012
13#undef memmove
14
15/*
16 * Implement memmove(). This can handle overlap between src and dst.
17 *
18 * Input:
19 * rdi: dest
20 * rsi: src
21 * rdx: count
22 *
23 * Output:
24 * rax: dest
25 */
Andrey Ryabinin393f2032015-02-13 14:39:56 -080026.weak memmove
27
Fenghua Yu9599ec02011-01-17 17:39:15 -080028ENTRY(memmove)
Andrey Ryabinin393f2032015-02-13 14:39:56 -080029ENTRY(__memmove)
Fenghua Yu9599ec02011-01-17 17:39:15 -080030 CFI_STARTPROC
Fenghua Yu057e05c2011-05-17 15:29:17 -070031
Andy Shevchenkod50ba362013-04-15 12:06:10 +030032 /* Handle more 32 bytes in loop */
Fenghua Yu9599ec02011-01-17 17:39:15 -080033 mov %rdi, %rax
34 cmp $0x20, %rdx
35 jb 1f
36
37 /* Decide forward/backward copy mode */
38 cmp %rdi, %rsi
Fenghua Yu057e05c2011-05-17 15:29:17 -070039 jge .Lmemmove_begin_forward
40 mov %rsi, %r8
41 add %rdx, %r8
42 cmp %rdi, %r8
43 jg 2f
Fenghua Yu9599ec02011-01-17 17:39:15 -080044
Fenghua Yu057e05c2011-05-17 15:29:17 -070045.Lmemmove_begin_forward:
Borislav Petkova77600c2015-02-04 09:11:17 +010046 ALTERNATIVE "", "movq %rdx, %rcx; rep movsb; retq", X86_FEATURE_ERMS
47
Fenghua Yu9599ec02011-01-17 17:39:15 -080048 /*
49 * movsq instruction have many startup latency
50 * so we handle small size by general register.
51 */
52 cmp $680, %rdx
53 jb 3f
54 /*
55 * movsq instruction is only good for aligned case.
56 */
57
58 cmpb %dil, %sil
59 je 4f
603:
61 sub $0x20, %rdx
62 /*
Andy Shevchenkobb916ff2013-04-15 12:06:09 +030063 * We gobble 32 bytes forward in each loop.
Fenghua Yu9599ec02011-01-17 17:39:15 -080064 */
655:
66 sub $0x20, %rdx
67 movq 0*8(%rsi), %r11
68 movq 1*8(%rsi), %r10
69 movq 2*8(%rsi), %r9
70 movq 3*8(%rsi), %r8
71 leaq 4*8(%rsi), %rsi
72
73 movq %r11, 0*8(%rdi)
74 movq %r10, 1*8(%rdi)
75 movq %r9, 2*8(%rdi)
76 movq %r8, 3*8(%rdi)
77 leaq 4*8(%rdi), %rdi
78 jae 5b
79 addq $0x20, %rdx
80 jmp 1f
81 /*
82 * Handle data forward by movsq.
83 */
84 .p2align 4
854:
86 movq %rdx, %rcx
87 movq -8(%rsi, %rdx), %r11
88 lea -8(%rdi, %rdx), %r10
89 shrq $3, %rcx
90 rep movsq
91 movq %r11, (%r10)
92 jmp 13f
Fenghua Yu057e05c2011-05-17 15:29:17 -070093.Lmemmove_end_forward:
94
Fenghua Yu9599ec02011-01-17 17:39:15 -080095 /*
96 * Handle data backward by movsq.
97 */
98 .p2align 4
997:
100 movq %rdx, %rcx
101 movq (%rsi), %r11
102 movq %rdi, %r10
103 leaq -8(%rsi, %rdx), %rsi
104 leaq -8(%rdi, %rdx), %rdi
105 shrq $3, %rcx
106 std
107 rep movsq
108 cld
109 movq %r11, (%r10)
110 jmp 13f
111
112 /*
113 * Start to prepare for backward copy.
114 */
115 .p2align 4
1162:
117 cmp $680, %rdx
118 jb 6f
119 cmp %dil, %sil
120 je 7b
1216:
122 /*
123 * Calculate copy position to tail.
124 */
125 addq %rdx, %rsi
126 addq %rdx, %rdi
127 subq $0x20, %rdx
128 /*
Andy Shevchenkobb916ff2013-04-15 12:06:09 +0300129 * We gobble 32 bytes backward in each loop.
Fenghua Yu9599ec02011-01-17 17:39:15 -0800130 */
1318:
132 subq $0x20, %rdx
133 movq -1*8(%rsi), %r11
134 movq -2*8(%rsi), %r10
135 movq -3*8(%rsi), %r9
136 movq -4*8(%rsi), %r8
137 leaq -4*8(%rsi), %rsi
138
139 movq %r11, -1*8(%rdi)
140 movq %r10, -2*8(%rdi)
141 movq %r9, -3*8(%rdi)
142 movq %r8, -4*8(%rdi)
143 leaq -4*8(%rdi), %rdi
144 jae 8b
145 /*
146 * Calculate copy position to head.
147 */
148 addq $0x20, %rdx
149 subq %rdx, %rsi
150 subq %rdx, %rdi
1511:
152 cmpq $16, %rdx
153 jb 9f
154 /*
155 * Move data from 16 bytes to 31 bytes.
156 */
157 movq 0*8(%rsi), %r11
158 movq 1*8(%rsi), %r10
159 movq -2*8(%rsi, %rdx), %r9
160 movq -1*8(%rsi, %rdx), %r8
161 movq %r11, 0*8(%rdi)
162 movq %r10, 1*8(%rdi)
163 movq %r9, -2*8(%rdi, %rdx)
164 movq %r8, -1*8(%rdi, %rdx)
165 jmp 13f
166 .p2align 4
1679:
168 cmpq $8, %rdx
169 jb 10f
170 /*
171 * Move data from 8 bytes to 15 bytes.
172 */
173 movq 0*8(%rsi), %r11
174 movq -1*8(%rsi, %rdx), %r10
175 movq %r11, 0*8(%rdi)
176 movq %r10, -1*8(%rdi, %rdx)
177 jmp 13f
17810:
179 cmpq $4, %rdx
180 jb 11f
181 /*
182 * Move data from 4 bytes to 7 bytes.
183 */
184 movl (%rsi), %r11d
185 movl -4(%rsi, %rdx), %r10d
186 movl %r11d, (%rdi)
187 movl %r10d, -4(%rdi, %rdx)
188 jmp 13f
18911:
190 cmp $2, %rdx
191 jb 12f
192 /*
193 * Move data from 2 bytes to 3 bytes.
194 */
195 movw (%rsi), %r11w
196 movw -2(%rsi, %rdx), %r10w
197 movw %r11w, (%rdi)
198 movw %r10w, -2(%rdi, %rdx)
199 jmp 13f
20012:
201 cmp $1, %rdx
202 jb 13f
203 /*
204 * Move data for 1 byte.
205 */
206 movb (%rsi), %r11b
207 movb %r11b, (%rdi)
20813:
209 retq
210 CFI_ENDPROC
Andrey Ryabinin393f2032015-02-13 14:39:56 -0800211ENDPROC(__memmove)
Fenghua Yu9599ec02011-01-17 17:39:15 -0800212ENDPROC(memmove)