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