blob: b0c79abf7542f0d7fdf9d57840e64eca53c33a4d [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>
31#include <private/libc_events.h>
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080032
33 /*
34 * Optimized memcpy() for ARM.
35 *
36 * note that memcpy() always returns the destination pointer,
37 * so we have to preserve R0.
38 */
39
Christopher Ferris59a13c12013-08-01 13:13:33 -070040ENTRY(__memcpy_chk)
41 cmp r2, r3
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -070042 bhi __memcpy_chk_fail
Christopher Ferris59a13c12013-08-01 13:13:33 -070043
44 // Fall through to memcpy...
45END(__memcpy_chk)
46
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080047ENTRY(memcpy)
48 /* The stack must always be 64-bits aligned to be compliant with the
49 * ARM ABI. Since we have to save R0, we might as well save R4
50 * which we can use for better pipelining of the reads below
51 */
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080052 stmfd sp!, {r0, r4, lr}
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -070053 .cfi_def_cfa_offset 12
54 .cfi_rel_offset r0, 0
55 .cfi_rel_offset r4, 4
56 .cfi_rel_offset lr, 8
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080057 /* Making room for r5-r11 which will be spilled later */
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080058 sub sp, sp, #28
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -070059 .cfi_adjust_cfa_offset 28
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080060
61 // preload the destination because we'll align it to a cache line
62 // with small writes. Also start the source "pump".
Elliott Hughesc54ca402013-12-13 12:17:13 -080063 pld [r0, #0]
64 pld [r1, #0]
65 pld [r1, #32]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080066
67 /* it simplifies things to take care of len<4 early */
68 cmp r2, #4
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -070069 blo .Lcopy_last_3_and_return
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080070
71 /* compute the offset to align the source
72 * offset = (4-(src&3))&3 = -src & 3
73 */
74 rsb r3, r1, #0
75 ands r3, r3, #3
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -070076 beq .Lsrc_aligned
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080077
78 /* align source to 32 bits. We need to insert 2 instructions between
79 * a ldr[b|h] and str[b|h] because byte and half-word instructions
80 * stall 2 cycles.
81 */
82 movs r12, r3, lsl #31
83 sub r2, r2, r3 /* we know that r3 <= r2 because r2 >= 4 */
84 ldrmib r3, [r1], #1
85 ldrcsb r4, [r1], #1
86 ldrcsb r12,[r1], #1
87 strmib r3, [r0], #1
88 strcsb r4, [r0], #1
89 strcsb r12,[r0], #1
90
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -070091.Lsrc_aligned:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080092
93 /* see if src and dst are aligned together (congruent) */
94 eor r12, r0, r1
95 tst r12, #3
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -070096 bne .Lnon_congruent
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080097
98 /* Use post-incriment mode for stm to spill r5-r11 to reserved stack
99 * frame. Don't update sp.
100 */
101 stmea sp, {r5-r11}
102
103 /* align the destination to a cache-line */
104 rsb r3, r0, #0
105 ands r3, r3, #0x1C
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700106 beq .Lcongruent_aligned32
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800107 cmp r3, r2
108 andhi r3, r2, #0x1C
109
110 /* conditionally copies 0 to 7 words (length in r3) */
111 movs r12, r3, lsl #28
112 ldmcsia r1!, {r4, r5, r6, r7} /* 16 bytes */
113 ldmmiia r1!, {r8, r9} /* 8 bytes */
114 stmcsia r0!, {r4, r5, r6, r7}
115 stmmiia r0!, {r8, r9}
116 tst r3, #0x4
117 ldrne r10,[r1], #4 /* 4 bytes */
118 strne r10,[r0], #4
119 sub r2, r2, r3
120
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700121.Lcongruent_aligned32:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800122 /*
123 * here source is aligned to 32 bytes.
124 */
125
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700126.Lcached_aligned32:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800127 subs r2, r2, #32
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700128 blo .Lless_than_32_left
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800129
130 /*
131 * We preload a cache-line up to 64 bytes ahead. On the 926, this will
132 * stall only until the requested world is fetched, but the linefill
133 * continues in the the background.
134 * While the linefill is going, we write our previous cache-line
135 * into the write-buffer (which should have some free space).
136 * When the linefill is done, the writebuffer will
137 * start dumping its content into memory
138 *
139 * While all this is going, we then load a full cache line into
140 * 8 registers, this cache line should be in the cache by now
141 * (or partly in the cache).
142 *
143 * This code should work well regardless of the source/dest alignment.
144 *
145 */
146
147 // Align the preload register to a cache-line because the cpu does
148 // "critical word first" (the first word requested is loaded first).
149 bic r12, r1, #0x1F
150 add r12, r12, #64
151
1521: ldmia r1!, { r4-r11 }
Elliott Hughesc54ca402013-12-13 12:17:13 -0800153 pld [r12, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800154 subs r2, r2, #32
155
156 // NOTE: if r12 is more than 64 ahead of r1, the following ldrhi
157 // for ARM9 preload will not be safely guarded by the preceding subs.
158 // When it is safely guarded the only possibility to have SIGSEGV here
159 // is because the caller overstates the length.
160 ldrhi r3, [r12], #32 /* cheap ARM9 preload */
161 stmia r0!, { r4-r11 }
162 bhs 1b
163
164 add r2, r2, #32
165
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700166.Lless_than_32_left:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800167 /*
168 * less than 32 bytes left at this point (length in r2)
169 */
170
171 /* skip all this if there is nothing to do, which should
172 * be a common case (if not executed the code below takes
173 * about 16 cycles)
174 */
175 tst r2, #0x1F
176 beq 1f
177
178 /* conditionnaly copies 0 to 31 bytes */
179 movs r12, r2, lsl #28
180 ldmcsia r1!, {r4, r5, r6, r7} /* 16 bytes */
181 ldmmiia r1!, {r8, r9} /* 8 bytes */
182 stmcsia r0!, {r4, r5, r6, r7}
183 stmmiia r0!, {r8, r9}
184 movs r12, r2, lsl #30
185 ldrcs r3, [r1], #4 /* 4 bytes */
186 ldrmih r4, [r1], #2 /* 2 bytes */
187 strcs r3, [r0], #4
188 strmih r4, [r0], #2
189 tst r2, #0x1
190 ldrneb r3, [r1] /* last byte */
191 strneb r3, [r0]
192
193 /* we're done! restore everything and return */
1941: ldmfd sp!, {r5-r11}
195 ldmfd sp!, {r0, r4, lr}
196 bx lr
197
198 /********************************************************************/
199
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700200.Lnon_congruent:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800201 /*
202 * here source is aligned to 4 bytes
203 * but destination is not.
204 *
205 * in the code below r2 is the number of bytes read
206 * (the number of bytes written is always smaller, because we have
207 * partial words in the shift queue)
208 */
209 cmp r2, #4
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700210 blo .Lcopy_last_3_and_return
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800211
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700212 /* Use post-increment mode for stm to spill r5-r11 to reserved stack
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800213 * frame. Don't update sp.
214 */
215 stmea sp, {r5-r11}
216
217 /* compute shifts needed to align src to dest */
218 rsb r5, r0, #0
219 and r5, r5, #3 /* r5 = # bytes in partial words */
220 mov r12, r5, lsl #3 /* r12 = right */
221 rsb lr, r12, #32 /* lr = left */
222
223 /* read the first word */
224 ldr r3, [r1], #4
225 sub r2, r2, #4
226
227 /* write a partial word (0 to 3 bytes), such that destination
228 * becomes aligned to 32 bits (r5 = nb of words to copy for alignment)
229 */
230 movs r5, r5, lsl #31
231 strmib r3, [r0], #1
232 movmi r3, r3, lsr #8
233 strcsb r3, [r0], #1
234 movcs r3, r3, lsr #8
235 strcsb r3, [r0], #1
236 movcs r3, r3, lsr #8
237
238 cmp r2, #4
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700239 blo .Lpartial_word_tail
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800240
241 /* Align destination to 32 bytes (cache line boundary) */
2421: tst r0, #0x1c
243 beq 2f
244 ldr r5, [r1], #4
245 sub r2, r2, #4
246 orr r4, r3, r5, lsl lr
247 mov r3, r5, lsr r12
248 str r4, [r0], #4
249 cmp r2, #4
250 bhs 1b
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700251 blo .Lpartial_word_tail
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800252
253 /* copy 32 bytes at a time */
2542: subs r2, r2, #32
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700255 blo .Lless_than_thirtytwo
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800256
257 /* Use immediate mode for the shifts, because there is an extra cycle
258 * for register shifts, which could account for up to 50% of
259 * performance hit.
260 */
261
262 cmp r12, #24
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700263 beq .Lloop24
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800264 cmp r12, #8
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700265 beq .Lloop8
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800266
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700267.Lloop16:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800268 ldr r12, [r1], #4
2691: mov r4, r12
270 ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
Elliott Hughesc54ca402013-12-13 12:17:13 -0800271 pld [r1, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800272 subs r2, r2, #32
273 ldrhs r12, [r1], #4
274 orr r3, r3, r4, lsl #16
275 mov r4, r4, lsr #16
276 orr r4, r4, r5, lsl #16
277 mov r5, r5, lsr #16
278 orr r5, r5, r6, lsl #16
279 mov r6, r6, lsr #16
280 orr r6, r6, r7, lsl #16
281 mov r7, r7, lsr #16
282 orr r7, r7, r8, lsl #16
283 mov r8, r8, lsr #16
284 orr r8, r8, r9, lsl #16
285 mov r9, r9, lsr #16
286 orr r9, r9, r10, lsl #16
287 mov r10, r10, lsr #16
288 orr r10, r10, r11, lsl #16
289 stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
290 mov r3, r11, lsr #16
291 bhs 1b
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700292 b .Lless_than_thirtytwo
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800293
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700294.Lloop8:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800295 ldr r12, [r1], #4
2961: mov r4, r12
297 ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
Elliott Hughesc54ca402013-12-13 12:17:13 -0800298 pld [r1, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800299 subs r2, r2, #32
300 ldrhs r12, [r1], #4
301 orr r3, r3, r4, lsl #24
302 mov r4, r4, lsr #8
303 orr r4, r4, r5, lsl #24
304 mov r5, r5, lsr #8
305 orr r5, r5, r6, lsl #24
306 mov r6, r6, lsr #8
307 orr r6, r6, r7, lsl #24
308 mov r7, r7, lsr #8
309 orr r7, r7, r8, lsl #24
310 mov r8, r8, lsr #8
311 orr r8, r8, r9, lsl #24
312 mov r9, r9, lsr #8
313 orr r9, r9, r10, lsl #24
314 mov r10, r10, lsr #8
315 orr r10, r10, r11, lsl #24
316 stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
317 mov r3, r11, lsr #8
318 bhs 1b
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700319 b .Lless_than_thirtytwo
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800320
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700321.Lloop24:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800322 ldr r12, [r1], #4
3231: mov r4, r12
324 ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
Elliott Hughesc54ca402013-12-13 12:17:13 -0800325 pld [r1, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800326 subs r2, r2, #32
327 ldrhs r12, [r1], #4
328 orr r3, r3, r4, lsl #8
329 mov r4, r4, lsr #24
330 orr r4, r4, r5, lsl #8
331 mov r5, r5, lsr #24
332 orr r5, r5, r6, lsl #8
333 mov r6, r6, lsr #24
334 orr r6, r6, r7, lsl #8
335 mov r7, r7, lsr #24
336 orr r7, r7, r8, lsl #8
337 mov r8, r8, lsr #24
338 orr r8, r8, r9, lsl #8
339 mov r9, r9, lsr #24
340 orr r9, r9, r10, lsl #8
341 mov r10, r10, lsr #24
342 orr r10, r10, r11, lsl #8
343 stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
344 mov r3, r11, lsr #24
345 bhs 1b
346
347
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700348.Lless_than_thirtytwo:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800349 /* copy the last 0 to 31 bytes of the source */
350 rsb r12, lr, #32 /* we corrupted r12, recompute it */
351 add r2, r2, #32
352 cmp r2, #4
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700353 blo .Lpartial_word_tail
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800354
3551: ldr r5, [r1], #4
356 sub r2, r2, #4
357 orr r4, r3, r5, lsl lr
358 mov r3, r5, lsr r12
359 str r4, [r0], #4
360 cmp r2, #4
361 bhs 1b
362
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700363.Lpartial_word_tail:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800364 /* we have a partial word in the input buffer */
365 movs r5, lr, lsl #(31-3)
366 strmib r3, [r0], #1
367 movmi r3, r3, lsr #8
368 strcsb r3, [r0], #1
369 movcs r3, r3, lsr #8
370 strcsb r3, [r0], #1
371
372 /* Refill spilled registers from the stack. Don't update sp. */
373 ldmfd sp, {r5-r11}
374
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700375.Lcopy_last_3_and_return:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800376 movs r2, r2, lsl #31 /* copy remaining 0, 1, 2 or 3 bytes */
377 ldrmib r2, [r1], #1
378 ldrcsb r3, [r1], #1
379 ldrcsb r12,[r1]
380 strmib r2, [r0], #1
381 strcsb r3, [r0], #1
382 strcsb r12,[r0]
383
384 /* we're done! restore sp and spilled registers and return */
385 add sp, sp, #28
386 ldmfd sp!, {r0, r4, lr}
387 bx lr
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700388END(memcpy)
Christopher Ferris59a13c12013-08-01 13:13:33 -0700389
390 // Only reached when the __memcpy_chk check fails.
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700391ENTRY_PRIVATE(__memcpy_chk_fail)
392 // Preserve lr for backtrace.
393 push {lr}
394 .cfi_def_cfa_offset 4
395 .cfi_rel_offset lr, 0
396
Christopher Ferris59a13c12013-08-01 13:13:33 -0700397 ldr r0, error_message
398 ldr r1, error_code
3991:
400 add r0, pc
401 bl __fortify_chk_fail
402error_code:
403 .word BIONIC_EVENT_MEMCPY_BUFFER_OVERFLOW
404error_message:
405 .word error_string-(1b+8)
Christopher Ferrisf13e1eb2014-10-17 14:08:54 -0700406END(__memcpy_chk_fail)
Christopher Ferris59a13c12013-08-01 13:13:33 -0700407
408 .data
409error_string:
Elliott Hughes68b67112013-10-15 17:17:05 -0700410 .string "memcpy: prevented write past end of buffer"