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