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