Christopher Ferris | 7c83a1e | 2013-02-26 01:30:00 -0800 | [diff] [blame] | 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> |
Christopher Ferris | 59a13c1 | 2013-08-01 13:13:33 -0700 | [diff] [blame^] | 31 | #include "libc_events.h" |
Christopher Ferris | 7c83a1e | 2013-02-26 01:30:00 -0800 | [diff] [blame] | 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 | |
Christopher Ferris | 59a13c1 | 2013-08-01 13:13:33 -0700 | [diff] [blame^] | 40 | ENTRY(__memset_chk) |
| 41 | cmp r2, r3 |
| 42 | bls done |
| 43 | |
| 44 | ldr r0, error_message |
| 45 | ldr r1, error_code |
| 46 | 1: |
| 47 | add r0, pc |
| 48 | bl __fortify_chk_fail |
| 49 | error_code: |
| 50 | .word BIONIC_EVENT_MEMSET_BUFFER_OVERFLOW |
| 51 | error_message: |
| 52 | .word error_string-(1b+8) |
| 53 | |
| 54 | END(__memset_chk) |
| 55 | |
Christopher Ferris | 7c83a1e | 2013-02-26 01:30:00 -0800 | [diff] [blame] | 56 | ENTRY(bzero) |
| 57 | mov r2, r1 |
| 58 | mov r1, #0 |
Christopher Ferris | 59a13c1 | 2013-08-01 13:13:33 -0700 | [diff] [blame^] | 59 | |
| 60 | done: |
| 61 | // Fall through to memset... |
Christopher Ferris | 7c83a1e | 2013-02-26 01:30:00 -0800 | [diff] [blame] | 62 | END(bzero) |
| 63 | |
| 64 | /* memset() returns its first argument. */ |
| 65 | ENTRY(memset) |
| 66 | # The neon memset only wins for less than 132. |
| 67 | cmp r2, #132 |
| 68 | bhi 11f |
| 69 | |
| 70 | .save {r0} |
| 71 | stmfd sp!, {r0} |
| 72 | |
| 73 | vdup.8 q0, r1 |
| 74 | |
| 75 | /* make sure we have at least 32 bytes to write */ |
| 76 | subs r2, r2, #32 |
| 77 | blo 2f |
| 78 | vmov q1, q0 |
| 79 | |
| 80 | 1: /* The main loop writes 32 bytes at a time */ |
| 81 | subs r2, r2, #32 |
| 82 | vst1.8 {d0 - d3}, [r0]! |
| 83 | bhs 1b |
| 84 | |
| 85 | 2: /* less than 32 left */ |
| 86 | add r2, r2, #32 |
| 87 | tst r2, #0x10 |
| 88 | beq 3f |
| 89 | |
| 90 | // writes 16 bytes, 128-bits aligned |
| 91 | vst1.8 {d0, d1}, [r0]! |
| 92 | 3: /* write up to 15-bytes (count in r2) */ |
| 93 | movs ip, r2, lsl #29 |
| 94 | bcc 1f |
| 95 | vst1.8 {d0}, [r0]! |
| 96 | 1: bge 2f |
| 97 | vst1.32 {d0[0]}, [r0]! |
| 98 | 2: movs ip, r2, lsl #31 |
| 99 | strmib r1, [r0], #1 |
| 100 | strcsb r1, [r0], #1 |
| 101 | strcsb r1, [r0], #1 |
| 102 | ldmfd sp!, {r0} |
| 103 | bx lr |
| 104 | 11: |
| 105 | /* compute the offset to align the destination |
| 106 | * offset = (4-(src&3))&3 = -src & 3 |
| 107 | */ |
| 108 | |
| 109 | .save {r0, r4-r7, lr} |
| 110 | stmfd sp!, {r0, r4-r7, lr} |
| 111 | rsb r3, r0, #0 |
| 112 | ands r3, r3, #3 |
| 113 | cmp r3, r2 |
| 114 | movhi r3, r2 |
| 115 | |
| 116 | /* splat r1 */ |
| 117 | mov r1, r1, lsl #24 |
| 118 | orr r1, r1, r1, lsr #8 |
| 119 | orr r1, r1, r1, lsr #16 |
| 120 | |
| 121 | movs r12, r3, lsl #31 |
| 122 | strcsb r1, [r0], #1 /* can't use strh (alignment unknown) */ |
| 123 | strcsb r1, [r0], #1 |
| 124 | strmib r1, [r0], #1 |
| 125 | subs r2, r2, r3 |
| 126 | ldmlsfd sp!, {r0, r4-r7, lr} /* return */ |
| 127 | bxls lr |
| 128 | |
| 129 | /* align the destination to a cache-line */ |
| 130 | mov r12, r1 |
| 131 | mov lr, r1 |
| 132 | mov r4, r1 |
| 133 | mov r5, r1 |
| 134 | mov r6, r1 |
| 135 | mov r7, r1 |
| 136 | |
| 137 | rsb r3, r0, #0 |
| 138 | ands r3, r3, #0x1C |
| 139 | beq 3f |
| 140 | cmp r3, r2 |
| 141 | andhi r3, r2, #0x1C |
| 142 | sub r2, r2, r3 |
| 143 | |
| 144 | /* conditionally writes 0 to 7 words (length in r3) */ |
| 145 | movs r3, r3, lsl #28 |
| 146 | stmcsia r0!, {r1, lr} |
| 147 | stmcsia r0!, {r1, lr} |
| 148 | stmmiia r0!, {r1, lr} |
| 149 | movs r3, r3, lsl #2 |
| 150 | strcs r1, [r0], #4 |
| 151 | |
| 152 | 3: |
| 153 | subs r2, r2, #32 |
| 154 | mov r3, r1 |
| 155 | bmi 2f |
| 156 | 1: subs r2, r2, #32 |
| 157 | stmia r0!, {r1,r3,r4,r5,r6,r7,r12,lr} |
| 158 | bhs 1b |
| 159 | 2: add r2, r2, #32 |
| 160 | |
| 161 | /* conditionally stores 0 to 31 bytes */ |
| 162 | movs r2, r2, lsl #28 |
| 163 | stmcsia r0!, {r1,r3,r12,lr} |
| 164 | stmmiia r0!, {r1, lr} |
| 165 | movs r2, r2, lsl #2 |
| 166 | strcs r1, [r0], #4 |
| 167 | strmih r1, [r0], #2 |
| 168 | movs r2, r2, lsl #2 |
| 169 | strcsb r1, [r0] |
| 170 | ldmfd sp!, {r0, r4-r7, lr} |
| 171 | bx lr |
| 172 | END(memset) |
Christopher Ferris | 59a13c1 | 2013-08-01 13:13:33 -0700 | [diff] [blame^] | 173 | |
| 174 | .data |
| 175 | error_string: |
| 176 | .string "memset buffer overflow" |