blob: 452b9d4ad2d9f82ff2f609922a2bd6e7a2c3db53 [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>
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080030
31 /*
32 * Optimized memset() for ARM.
33 *
34 * memset() returns its first argument.
35 */
36
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -070037 .syntax unified
38
Christopher Ferris59a13c12013-08-01 13:13:33 -070039ENTRY(__memset_chk)
40 cmp r2, r3
41 bls done
42
43 ldr r0, error_message
Christopher Ferris59a13c12013-08-01 13:13:33 -0700441:
45 add r0, pc
Elliott Hughesb83d6742016-02-25 20:33:47 -080046 bl __fortify_fatal
Christopher Ferris59a13c12013-08-01 13:13:33 -070047error_message:
48 .word error_string-(1b+8)
49
50END(__memset_chk)
51
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080052ENTRY(bzero)
53 mov r2, r1
54 mov r1, #0
Christopher Ferris59a13c12013-08-01 13:13:33 -070055
56done:
57 // Fall through to memset...
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080058END(bzero)
59
60ENTRY(memset)
61 /* compute the offset to align the destination
62 * offset = (4-(src&3))&3 = -src & 3
63 */
64 .save {r0, r4-r7, lr}
65 stmfd sp!, {r0, r4-r7, lr}
66 rsb r3, r0, #0
67 ands r3, r3, #3
68 cmp r3, r2
69 movhi r3, r2
70
71 /* splat r1 */
72 mov r1, r1, lsl #24
73 orr r1, r1, r1, lsr #8
74 orr r1, r1, r1, lsr #16
75
76 movs r12, r3, lsl #31
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -070077 strbcs r1, [r0], #1 /* can't use strh (alignment unknown) */
78 strbcs r1, [r0], #1
79 strbmi r1, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080080 subs r2, r2, r3
Christopher Ferrise1e434a2015-07-06 12:03:40 -070081 popls {r0, r4-r7, pc} /* return */
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080082
83 /* align the destination to a cache-line */
84 mov r12, r1
85 mov lr, r1
86 mov r4, r1
87 mov r5, r1
88 mov r6, r1
89 mov r7, r1
90
91 rsb r3, r0, #0
92 ands r3, r3, #0x1C
93 beq 3f
94 cmp r3, r2
95 andhi r3, r2, #0x1C
96 sub r2, r2, r3
97
98 /* conditionally writes 0 to 7 words (length in r3) */
99 movs r3, r3, lsl #28
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700100 stmcs r0!, {r1, lr}
101 stmcs r0!, {r1, lr}
102 stmmi r0!, {r1, lr}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800103 movs r3, r3, lsl #2
104 strcs r1, [r0], #4
105
1063:
107 subs r2, r2, #32
108 mov r3, r1
109 bmi 2f
1101: subs r2, r2, #32
111 stmia r0!, {r1,r3,r4,r5,r6,r7,r12,lr}
112 bhs 1b
1132: add r2, r2, #32
114
115 /* conditionally stores 0 to 31 bytes */
116 movs r2, r2, lsl #28
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700117 stmcs r0!, {r1,r3,r12,lr}
118 stmmi r0!, {r1, lr}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800119 movs r2, r2, lsl #2
120 strcs r1, [r0], #4
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700121 strhmi r1, [r0], #2
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800122 movs r2, r2, lsl #2
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700123 strbcs r1, [r0]
Christopher Ferrise1e434a2015-07-06 12:03:40 -0700124 ldmfd sp!, {r0, r4-r7, pc}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800125END(memset)
Christopher Ferris59a13c12013-08-01 13:13:33 -0700126
127 .data
128error_string:
Elliott Hughes68b67112013-10-15 17:17:05 -0700129 .string "memset: prevented write past end of buffer"