blob: f5e97f2cc609cd8b8e7a212f87c4f3f30f8e467e [file] [log] [blame]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <machine/cpu-features.h>
Elliott Hughes851e68a2014-02-19 16:53:20 -080030#include <private/bionic_asm.h>
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080031
32 /*
33 * Optimized memcpy() for ARM.
34 *
35 * note that memcpy() always returns the destination pointer,
36 * so we have to preserve R0.
37 */
38
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -070039 .syntax unified
40
Christopher Ferris59a13c12013-08-01 13:13:33 -070041ENTRY(__memcpy_chk)
42 cmp r2, r3
Elliott Hughes3c6016f2016-03-01 14:45:58 -080043 bls memcpy
Christopher Ferris59a13c12013-08-01 13:13:33 -070044
Elliott Hughes3c6016f2016-03-01 14:45:58 -080045 // Preserve lr for backtrace.
46 push {lr}
47 .cfi_def_cfa_offset 4
48 .cfi_rel_offset lr, 0
49
50 bl __memcpy_chk_fail
Christopher Ferris59a13c12013-08-01 13:13:33 -070051END(__memcpy_chk)
52
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080053ENTRY(memcpy)
54 /* The stack must always be 64-bits aligned to be compliant with the
55 * ARM ABI. Since we have to save R0, we might as well save R4
56 * which we can use for better pipelining of the reads below
57 */
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080058 stmfd sp!, {r0, r4, lr}
Christopher Ferris7123d432014-10-17 14:08:54 -070059 .cfi_def_cfa_offset 12
60 .cfi_rel_offset r0, 0
61 .cfi_rel_offset r4, 4
62 .cfi_rel_offset lr, 8
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080063 /* Making room for r5-r11 which will be spilled later */
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080064 sub sp, sp, #28
Christopher Ferris7123d432014-10-17 14:08:54 -070065 .cfi_adjust_cfa_offset 28
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080066
67 // preload the destination because we'll align it to a cache line
68 // with small writes. Also start the source "pump".
Elliott Hughesc54ca402013-12-13 12:17:13 -080069 pld [r0, #0]
70 pld [r1, #0]
71 pld [r1, #32]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080072
73 /* it simplifies things to take care of len<4 early */
74 cmp r2, #4
Christopher Ferris7123d432014-10-17 14:08:54 -070075 blo .Lcopy_last_3_and_return
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080076
77 /* compute the offset to align the source
78 * offset = (4-(src&3))&3 = -src & 3
79 */
80 rsb r3, r1, #0
81 ands r3, r3, #3
Christopher Ferris7123d432014-10-17 14:08:54 -070082 beq .Lsrc_aligned
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080083
84 /* align source to 32 bits. We need to insert 2 instructions between
85 * a ldr[b|h] and str[b|h] because byte and half-word instructions
86 * stall 2 cycles.
87 */
88 movs r12, r3, lsl #31
89 sub r2, r2, r3 /* we know that r3 <= r2 because r2 >= 4 */
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -070090 ldrbmi r3, [r1], #1
91 ldrbcs r4, [r1], #1
92 ldrbcs r12,[r1], #1
93 strbmi r3, [r0], #1
94 strbcs r4, [r0], #1
95 strbcs r12,[r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080096
Christopher Ferris7123d432014-10-17 14:08:54 -070097.Lsrc_aligned:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080098
99 /* see if src and dst are aligned together (congruent) */
100 eor r12, r0, r1
101 tst r12, #3
Christopher Ferris7123d432014-10-17 14:08:54 -0700102 bne .Lnon_congruent
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800103
104 /* Use post-incriment mode for stm to spill r5-r11 to reserved stack
105 * frame. Don't update sp.
106 */
107 stmea sp, {r5-r11}
108
109 /* align the destination to a cache-line */
110 rsb r3, r0, #0
111 ands r3, r3, #0x1C
Christopher Ferris7123d432014-10-17 14:08:54 -0700112 beq .Lcongruent_aligned32
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800113 cmp r3, r2
114 andhi r3, r2, #0x1C
115
116 /* conditionally copies 0 to 7 words (length in r3) */
117 movs r12, r3, lsl #28
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700118 ldmcs r1!, {r4, r5, r6, r7} /* 16 bytes */
119 ldmmi r1!, {r8, r9} /* 8 bytes */
120 stmcs r0!, {r4, r5, r6, r7}
121 stmmi r0!, {r8, r9}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800122 tst r3, #0x4
123 ldrne r10,[r1], #4 /* 4 bytes */
124 strne r10,[r0], #4
125 sub r2, r2, r3
126
Christopher Ferris7123d432014-10-17 14:08:54 -0700127.Lcongruent_aligned32:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800128 /*
129 * here source is aligned to 32 bytes.
130 */
131
Christopher Ferris7123d432014-10-17 14:08:54 -0700132.Lcached_aligned32:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800133 subs r2, r2, #32
Christopher Ferris7123d432014-10-17 14:08:54 -0700134 blo .Lless_than_32_left
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800135
136 /*
137 * We preload a cache-line up to 64 bytes ahead. On the 926, this will
138 * stall only until the requested world is fetched, but the linefill
139 * continues in the the background.
140 * While the linefill is going, we write our previous cache-line
141 * into the write-buffer (which should have some free space).
142 * When the linefill is done, the writebuffer will
143 * start dumping its content into memory
144 *
145 * While all this is going, we then load a full cache line into
146 * 8 registers, this cache line should be in the cache by now
147 * (or partly in the cache).
148 *
149 * This code should work well regardless of the source/dest alignment.
150 *
151 */
152
153 // Align the preload register to a cache-line because the cpu does
154 // "critical word first" (the first word requested is loaded first).
155 bic r12, r1, #0x1F
156 add r12, r12, #64
157
1581: ldmia r1!, { r4-r11 }
Elliott Hughesc54ca402013-12-13 12:17:13 -0800159 pld [r12, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800160 subs r2, r2, #32
161
162 // NOTE: if r12 is more than 64 ahead of r1, the following ldrhi
163 // for ARM9 preload will not be safely guarded by the preceding subs.
164 // When it is safely guarded the only possibility to have SIGSEGV here
165 // is because the caller overstates the length.
166 ldrhi r3, [r12], #32 /* cheap ARM9 preload */
167 stmia r0!, { r4-r11 }
168 bhs 1b
169
170 add r2, r2, #32
171
Christopher Ferris7123d432014-10-17 14:08:54 -0700172.Lless_than_32_left:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800173 /*
174 * less than 32 bytes left at this point (length in r2)
175 */
176
177 /* skip all this if there is nothing to do, which should
178 * be a common case (if not executed the code below takes
179 * about 16 cycles)
180 */
181 tst r2, #0x1F
182 beq 1f
183
184 /* conditionnaly copies 0 to 31 bytes */
185 movs r12, r2, lsl #28
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700186 ldmcs r1!, {r4, r5, r6, r7} /* 16 bytes */
187 ldmmi r1!, {r8, r9} /* 8 bytes */
188 stmcs r0!, {r4, r5, r6, r7}
189 stmmi r0!, {r8, r9}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800190 movs r12, r2, lsl #30
191 ldrcs r3, [r1], #4 /* 4 bytes */
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700192 ldrhmi r4, [r1], #2 /* 2 bytes */
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800193 strcs r3, [r0], #4
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700194 strhmi r4, [r0], #2
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800195 tst r2, #0x1
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700196 ldrbne r3, [r1] /* last byte */
197 strbne r3, [r0]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800198
199 /* we're done! restore everything and return */
2001: ldmfd sp!, {r5-r11}
Christopher Ferrise1e434a2015-07-06 12:03:40 -0700201 ldmfd sp!, {r0, r4, pc}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800202
203 /********************************************************************/
204
Christopher Ferris7123d432014-10-17 14:08:54 -0700205.Lnon_congruent:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800206 /*
207 * here source is aligned to 4 bytes
208 * but destination is not.
209 *
210 * in the code below r2 is the number of bytes read
211 * (the number of bytes written is always smaller, because we have
212 * partial words in the shift queue)
213 */
214 cmp r2, #4
Christopher Ferris7123d432014-10-17 14:08:54 -0700215 blo .Lcopy_last_3_and_return
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800216
Christopher Ferris7123d432014-10-17 14:08:54 -0700217 /* Use post-increment mode for stm to spill r5-r11 to reserved stack
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800218 * frame. Don't update sp.
219 */
220 stmea sp, {r5-r11}
221
222 /* compute shifts needed to align src to dest */
223 rsb r5, r0, #0
224 and r5, r5, #3 /* r5 = # bytes in partial words */
225 mov r12, r5, lsl #3 /* r12 = right */
226 rsb lr, r12, #32 /* lr = left */
227
228 /* read the first word */
229 ldr r3, [r1], #4
230 sub r2, r2, #4
231
232 /* write a partial word (0 to 3 bytes), such that destination
233 * becomes aligned to 32 bits (r5 = nb of words to copy for alignment)
234 */
235 movs r5, r5, lsl #31
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700236 strbmi r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800237 movmi r3, r3, lsr #8
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700238 strbcs r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800239 movcs r3, r3, lsr #8
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700240 strbcs r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800241 movcs r3, r3, lsr #8
242
243 cmp r2, #4
Christopher Ferris7123d432014-10-17 14:08:54 -0700244 blo .Lpartial_word_tail
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800245
246 /* Align destination to 32 bytes (cache line boundary) */
2471: tst r0, #0x1c
248 beq 2f
249 ldr r5, [r1], #4
250 sub r2, r2, #4
251 orr r4, r3, r5, lsl lr
252 mov r3, r5, lsr r12
253 str r4, [r0], #4
254 cmp r2, #4
255 bhs 1b
Christopher Ferris7123d432014-10-17 14:08:54 -0700256 blo .Lpartial_word_tail
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800257
258 /* copy 32 bytes at a time */
2592: subs r2, r2, #32
Christopher Ferris7123d432014-10-17 14:08:54 -0700260 blo .Lless_than_thirtytwo
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800261
262 /* Use immediate mode for the shifts, because there is an extra cycle
263 * for register shifts, which could account for up to 50% of
264 * performance hit.
265 */
266
267 cmp r12, #24
Christopher Ferris7123d432014-10-17 14:08:54 -0700268 beq .Lloop24
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800269 cmp r12, #8
Christopher Ferris7123d432014-10-17 14:08:54 -0700270 beq .Lloop8
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800271
Christopher Ferris7123d432014-10-17 14:08:54 -0700272.Lloop16:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800273 ldr r12, [r1], #4
2741: mov r4, r12
275 ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
Elliott Hughesc54ca402013-12-13 12:17:13 -0800276 pld [r1, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800277 subs r2, r2, #32
278 ldrhs r12, [r1], #4
279 orr r3, r3, r4, lsl #16
280 mov r4, r4, lsr #16
281 orr r4, r4, r5, lsl #16
282 mov r5, r5, lsr #16
283 orr r5, r5, r6, lsl #16
284 mov r6, r6, lsr #16
285 orr r6, r6, r7, lsl #16
286 mov r7, r7, lsr #16
287 orr r7, r7, r8, lsl #16
288 mov r8, r8, lsr #16
289 orr r8, r8, r9, lsl #16
290 mov r9, r9, lsr #16
291 orr r9, r9, r10, lsl #16
292 mov r10, r10, lsr #16
293 orr r10, r10, r11, lsl #16
294 stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
295 mov r3, r11, lsr #16
296 bhs 1b
Christopher Ferris7123d432014-10-17 14:08:54 -0700297 b .Lless_than_thirtytwo
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800298
Christopher Ferris7123d432014-10-17 14:08:54 -0700299.Lloop8:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800300 ldr r12, [r1], #4
3011: mov r4, r12
302 ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
Elliott Hughesc54ca402013-12-13 12:17:13 -0800303 pld [r1, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800304 subs r2, r2, #32
305 ldrhs r12, [r1], #4
306 orr r3, r3, r4, lsl #24
307 mov r4, r4, lsr #8
308 orr r4, r4, r5, lsl #24
309 mov r5, r5, lsr #8
310 orr r5, r5, r6, lsl #24
311 mov r6, r6, lsr #8
312 orr r6, r6, r7, lsl #24
313 mov r7, r7, lsr #8
314 orr r7, r7, r8, lsl #24
315 mov r8, r8, lsr #8
316 orr r8, r8, r9, lsl #24
317 mov r9, r9, lsr #8
318 orr r9, r9, r10, lsl #24
319 mov r10, r10, lsr #8
320 orr r10, r10, r11, lsl #24
321 stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
322 mov r3, r11, lsr #8
323 bhs 1b
Christopher Ferris7123d432014-10-17 14:08:54 -0700324 b .Lless_than_thirtytwo
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800325
Christopher Ferris7123d432014-10-17 14:08:54 -0700326.Lloop24:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800327 ldr r12, [r1], #4
3281: mov r4, r12
329 ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
Elliott Hughesc54ca402013-12-13 12:17:13 -0800330 pld [r1, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800331 subs r2, r2, #32
332 ldrhs r12, [r1], #4
333 orr r3, r3, r4, lsl #8
334 mov r4, r4, lsr #24
335 orr r4, r4, r5, lsl #8
336 mov r5, r5, lsr #24
337 orr r5, r5, r6, lsl #8
338 mov r6, r6, lsr #24
339 orr r6, r6, r7, lsl #8
340 mov r7, r7, lsr #24
341 orr r7, r7, r8, lsl #8
342 mov r8, r8, lsr #24
343 orr r8, r8, r9, lsl #8
344 mov r9, r9, lsr #24
345 orr r9, r9, r10, lsl #8
346 mov r10, r10, lsr #24
347 orr r10, r10, r11, lsl #8
348 stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
349 mov r3, r11, lsr #24
350 bhs 1b
351
352
Christopher Ferris7123d432014-10-17 14:08:54 -0700353.Lless_than_thirtytwo:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800354 /* copy the last 0 to 31 bytes of the source */
355 rsb r12, lr, #32 /* we corrupted r12, recompute it */
356 add r2, r2, #32
357 cmp r2, #4
Christopher Ferris7123d432014-10-17 14:08:54 -0700358 blo .Lpartial_word_tail
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800359
3601: ldr r5, [r1], #4
361 sub r2, r2, #4
362 orr r4, r3, r5, lsl lr
363 mov r3, r5, lsr r12
364 str r4, [r0], #4
365 cmp r2, #4
366 bhs 1b
367
Christopher Ferris7123d432014-10-17 14:08:54 -0700368.Lpartial_word_tail:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800369 /* we have a partial word in the input buffer */
370 movs r5, lr, lsl #(31-3)
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700371 strbmi r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800372 movmi r3, r3, lsr #8
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700373 strbcs r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800374 movcs r3, r3, lsr #8
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700375 strbcs r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800376
377 /* Refill spilled registers from the stack. Don't update sp. */
378 ldmfd sp, {r5-r11}
379
Christopher Ferris7123d432014-10-17 14:08:54 -0700380.Lcopy_last_3_and_return:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800381 movs r2, r2, lsl #31 /* copy remaining 0, 1, 2 or 3 bytes */
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700382 ldrbmi r2, [r1], #1
383 ldrbcs r3, [r1], #1
384 ldrbcs r12,[r1]
385 strbmi r2, [r0], #1
386 strbcs r3, [r0], #1
387 strbcs r12,[r0]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800388
389 /* we're done! restore sp and spilled registers and return */
390 add sp, sp, #28
Christopher Ferrise1e434a2015-07-06 12:03:40 -0700391 ldmfd sp!, {r0, r4, pc}
Christopher Ferris7123d432014-10-17 14:08:54 -0700392END(memcpy)