blob: b8eabbf8335eaabafcbad82990e2647f6897fde8 [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
Elliott Hughes851e68a2014-02-19 16:53:20 -080029#include <private/bionic_asm.h>
30#include <private/libc_events.h>
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080031
32 /*
33 * Optimized memset() for ARM.
34 *
35 * memset() returns its first argument.
36 */
37
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -070038 .syntax unified
39
Christopher Ferris59a13c12013-08-01 13:13:33 -070040ENTRY(__memset_chk)
41 cmp r2, r3
42 bls done
43
44 ldr r0, error_message
45 ldr r1, error_code
461:
47 add r0, pc
48 bl __fortify_chk_fail
49error_code:
50 .word BIONIC_EVENT_MEMSET_BUFFER_OVERFLOW
51error_message:
52 .word error_string-(1b+8)
53
54END(__memset_chk)
55
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080056ENTRY(bzero)
57 mov r2, r1
58 mov r1, #0
Christopher Ferris59a13c12013-08-01 13:13:33 -070059
60done:
61 // Fall through to memset...
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080062END(bzero)
63
64ENTRY(memset)
65 /* compute the offset to align the destination
66 * offset = (4-(src&3))&3 = -src & 3
67 */
68 .save {r0, r4-r7, lr}
69 stmfd sp!, {r0, r4-r7, lr}
70 rsb r3, r0, #0
71 ands r3, r3, #3
72 cmp r3, r2
73 movhi r3, r2
74
75 /* splat r1 */
76 mov r1, r1, lsl #24
77 orr r1, r1, r1, lsr #8
78 orr r1, r1, r1, lsr #16
79
80 movs r12, r3, lsl #31
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -070081 strbcs r1, [r0], #1 /* can't use strh (alignment unknown) */
82 strbcs r1, [r0], #1
83 strbmi r1, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080084 subs r2, r2, r3
Christopher Ferrise1e434a2015-07-06 12:03:40 -070085 popls {r0, r4-r7, pc} /* return */
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080086
87 /* align the destination to a cache-line */
88 mov r12, r1
89 mov lr, r1
90 mov r4, r1
91 mov r5, r1
92 mov r6, r1
93 mov r7, r1
94
95 rsb r3, r0, #0
96 ands r3, r3, #0x1C
97 beq 3f
98 cmp r3, r2
99 andhi r3, r2, #0x1C
100 sub r2, r2, r3
101
102 /* conditionally writes 0 to 7 words (length in r3) */
103 movs r3, r3, lsl #28
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700104 stmcs r0!, {r1, lr}
105 stmcs r0!, {r1, lr}
106 stmmi r0!, {r1, lr}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800107 movs r3, r3, lsl #2
108 strcs r1, [r0], #4
109
1103:
111 subs r2, r2, #32
112 mov r3, r1
113 bmi 2f
1141: subs r2, r2, #32
115 stmia r0!, {r1,r3,r4,r5,r6,r7,r12,lr}
116 bhs 1b
1172: add r2, r2, #32
118
119 /* conditionally stores 0 to 31 bytes */
120 movs r2, r2, lsl #28
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700121 stmcs r0!, {r1,r3,r12,lr}
122 stmmi r0!, {r1, lr}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800123 movs r2, r2, lsl #2
124 strcs r1, [r0], #4
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700125 strhmi r1, [r0], #2
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800126 movs r2, r2, lsl #2
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700127 strbcs r1, [r0]
Christopher Ferrise1e434a2015-07-06 12:03:40 -0700128 ldmfd sp!, {r0, r4-r7, pc}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800129END(memset)
Christopher Ferris59a13c12013-08-01 13:13:33 -0700130
131 .data
132error_string:
Elliott Hughes68b67112013-10-15 17:17:05 -0700133 .string "memset: prevented write past end of buffer"