memset.S revision 59a13c122ebc4191583b67c846a95d690dcda5cf
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/asm.h>
30#include "libc_events.h"
31
32        /*
33         * Optimized memset() for ARM.
34         *
35         * memset() returns its first argument.
36         */
37
38ENTRY(__memset_chk)
39        cmp         r2, r3
40        bls         done
41
42        ldr         r0, error_message
43        ldr         r1, error_code
441:
45        add         r0, pc
46        bl          __fortify_chk_fail
47error_code:
48        .word       BIONIC_EVENT_MEMSET_BUFFER_OVERFLOW
49error_message:
50        .word       error_string-(1b+8)
51
52END(__memset_chk)
53
54ENTRY(bzero)
55        mov     r2, r1
56        mov     r1, #0
57
58done:
59        // Fall through to memset...
60END(bzero)
61
62ENTRY(memset)
63        /* compute the offset to align the destination
64         * offset = (4-(src&3))&3 = -src & 3
65         */
66        .save       {r0, r4-r7, lr}
67        stmfd       sp!, {r0, r4-r7, lr}
68        rsb         r3, r0, #0
69        ands        r3, r3, #3
70        cmp         r3, r2
71        movhi       r3, r2
72
73        /* splat r1 */
74        mov         r1, r1, lsl #24
75        orr         r1, r1, r1, lsr #8
76        orr         r1, r1, r1, lsr #16
77
78        movs        r12, r3, lsl #31
79        strcsb      r1, [r0], #1    /* can't use strh (alignment unknown) */
80        strcsb      r1, [r0], #1
81        strmib      r1, [r0], #1
82        subs        r2, r2, r3
83        ldmlsfd     sp!, {r0, r4-r7, lr}    /* return */
84        bxls        lr
85
86        /* align the destination to a cache-line */
87        mov         r12, r1
88        mov         lr, r1
89        mov         r4, r1
90        mov         r5, r1
91        mov         r6, r1
92        mov         r7, r1
93
94        rsb         r3, r0, #0
95        ands        r3, r3, #0x1C
96        beq         3f
97        cmp         r3, r2
98        andhi       r3, r2, #0x1C
99        sub         r2, r2, r3
100
101        /* conditionally writes 0 to 7 words (length in r3) */
102        movs        r3, r3, lsl #28
103        stmcsia     r0!, {r1, lr}
104        stmcsia     r0!, {r1, lr}
105        stmmiia     r0!, {r1, lr}
106        movs        r3, r3, lsl #2
107        strcs       r1, [r0], #4
108
1093:
110        subs        r2, r2, #32
111        mov         r3, r1
112        bmi         2f
1131:      subs        r2, r2, #32
114        stmia       r0!, {r1,r3,r4,r5,r6,r7,r12,lr}
115        bhs         1b
1162:      add         r2, r2, #32
117
118        /* conditionally stores 0 to 31 bytes */
119        movs        r2, r2, lsl #28
120        stmcsia     r0!, {r1,r3,r12,lr}
121        stmmiia     r0!, {r1, lr}
122        movs        r2, r2, lsl #2
123        strcs       r1, [r0], #4
124        strmih      r1, [r0], #2
125        movs        r2, r2, lsl #2
126        strcsb      r1, [r0]
127        ldmfd       sp!, {r0, r4-r7, lr}
128        bx          lr
129END(memset)
130
131        .data
132error_string:
133        .string     "memset buffer overflow"
134