1 /*
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 */
29 #include <machine/cpu-features.h>
30 #include <machine/asm.h>
31 #include "private/libc_events.h"
33 /*
34 * This code assumes it is running on a processor that supports all arm v7
35 * instructions and that supports neon instructions.
36 */
38 .fpu neon
40 ENTRY(__memset_chk)
41 cmp r2, r3
42 bls .L_done
44 // Preserve lr for backtrace.
45 push {lr}
46 .save {lr}
47 .cfi_def_cfa_offset 4
48 .cfi_rel_offset lr, 0
50 ldr r0, error_message
51 ldr r1, error_code
52 1:
53 add r0, pc
54 bl __fortify_chk_fail
55 error_code:
56 .word BIONIC_EVENT_MEMSET_BUFFER_OVERFLOW
57 error_message:
58 .word error_string-(1b+8)
59 END(__memset_chk)
61 ENTRY(bzero)
62 mov r2, r1
63 mov r1, #0
65 .L_done:
66 // Fall through to memset...
67 END(bzero)
69 /* memset() returns its first argument. */
70 ENTRY(memset)
71 // The neon memset only wins for less than 132.
72 cmp r2, #132
73 bhi __memset_large_copy
75 stmfd sp!, {r0}
76 .save {r0}
77 .cfi_def_cfa_offset 4
78 .cfi_rel_offset r0, 0
80 vdup.8 q0, r1
82 /* make sure we have at least 32 bytes to write */
83 subs r2, r2, #32
84 blo 2f
85 vmov q1, q0
87 1: /* The main loop writes 32 bytes at a time */
88 subs r2, r2, #32
89 vst1.8 {d0 - d3}, [r0]!
90 bhs 1b
92 2: /* less than 32 left */
93 add r2, r2, #32
94 tst r2, #0x10
95 beq 3f
97 // writes 16 bytes, 128-bits aligned
98 vst1.8 {d0, d1}, [r0]!
99 3: /* write up to 15-bytes (count in r2) */
100 movs ip, r2, lsl #29
101 bcc 1f
102 vst1.8 {d0}, [r0]!
103 1: bge 2f
104 vst1.32 {d0[0]}, [r0]!
105 2: movs ip, r2, lsl #31
106 strmib r1, [r0], #1
107 strcsb r1, [r0], #1
108 strcsb r1, [r0], #1
109 ldmfd sp!, {r0}
110 bx lr
111 END(memset)
113 ENTRY_PRIVATE(__memset_large_copy)
114 /* compute the offset to align the destination
115 * offset = (4-(src&3))&3 = -src & 3
116 */
117 stmfd sp!, {r0, r4-r7, lr}
118 .save {r0, r4-r7, lr}
119 .cfi_def_cfa_offset 24
120 .cfi_rel_offset r0, 0
121 .cfi_rel_offset r4, 4
122 .cfi_rel_offset r5, 8
123 .cfi_rel_offset r6, 12
124 .cfi_rel_offset r7, 16
125 .cfi_rel_offset lr, 20
127 rsb r3, r0, #0
128 ands r3, r3, #3
129 cmp r3, r2
130 movhi r3, r2
132 /* splat r1 */
133 mov r1, r1, lsl #24
134 orr r1, r1, r1, lsr #8
135 orr r1, r1, r1, lsr #16
137 movs r12, r3, lsl #31
138 strcsb r1, [r0], #1 /* can't use strh (alignment unknown) */
139 strcsb r1, [r0], #1
140 strmib r1, [r0], #1
141 subs r2, r2, r3
142 ldmlsfd sp!, {r0, r4-r7, lr} /* return */
143 bxls lr
145 /* align the destination to a cache-line */
146 mov r12, r1
147 mov lr, r1
148 mov r4, r1
149 mov r5, r1
150 mov r6, r1
151 mov r7, r1
153 rsb r3, r0, #0
154 ands r3, r3, #0x1C
155 beq 3f
156 cmp r3, r2
157 andhi r3, r2, #0x1C
158 sub r2, r2, r3
160 /* conditionally writes 0 to 7 words (length in r3) */
161 movs r3, r3, lsl #28
162 stmcsia r0!, {r1, lr}
163 stmcsia r0!, {r1, lr}
164 stmmiia r0!, {r1, lr}
165 movs r3, r3, lsl #2
166 strcs r1, [r0], #4
168 3:
169 subs r2, r2, #32
170 mov r3, r1
171 bmi 2f
172 1: subs r2, r2, #32
173 stmia r0!, {r1,r3,r4,r5,r6,r7,r12,lr}
174 bhs 1b
175 2: add r2, r2, #32
177 /* conditionally stores 0 to 31 bytes */
178 movs r2, r2, lsl #28
179 stmcsia r0!, {r1,r3,r12,lr}
180 stmmiia r0!, {r1, lr}
181 movs r2, r2, lsl #2
182 strcs r1, [r0], #4
183 strmih r1, [r0], #2
184 movs r2, r2, lsl #2
185 strcsb r1, [r0]
186 ldmfd sp!, {r0, r4-r7, lr}
187 bx lr
188 END(__memset_large_copy)
190 .data
191 error_string:
192 .string "memset: prevented write past end of buffer"