The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1997 Mark Brinicombe |
Elliott Hughes | dfb74c5 | 2016-10-24 12:53:17 -0700 | [diff] [blame] | 3 | * Copyright (C) 2010 The Android Open Source Project |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. All advertising materials mentioning features or use of this software |
| 15 | * must display the following acknowledgement: |
| 16 | * This product includes software developed by Mark Brinicombe |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
Elliott Hughes | 851e68a | 2014-02-19 16:53:20 -0800 | [diff] [blame] | 34 | #include <private/bionic_asm.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 36 | // According to the ARM AAPCS document, we only need to save |
| 37 | // the following registers: |
| 38 | // |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 39 | // Core r4-r11, sp, lr |
| 40 | // AAPCS 5.1.1: |
| 41 | // A subroutine must preserve the contents of the registers r4-r8, r10, r11 |
| 42 | // and SP (and r9 in PCS variants that designate r9 as v6). |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 43 | // |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 44 | // VFP d8-d15 |
| 45 | // AAPCS 5.1.2.1: |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 46 | // Registers s16-s31 (d8-d15, q4-q7) must be preserved across subroutine |
| 47 | // calls; registers s0-s15 (d0-d7, q0-q3) do not need to be preserved |
| 48 | // (and can be used for passing arguments or returning results in standard |
| 49 | // procedure-call variants). Registers d16-d31 (q8-q15), if present, do |
| 50 | // not need to be preserved. |
| 51 | // |
| 52 | // FPSCR saved because glibc does. |
| 53 | |
| 54 | // The internal structure of a jmp_buf is totally private. |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 55 | // Current layout (changes from release to release): |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 56 | // |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 57 | // word name description |
| 58 | // 0 sigflag/cookie setjmp cookie in top 31 bits, signal mask flag in low bit |
Elliott Hughes | 460130b | 2018-01-31 09:05:26 -0800 | [diff] [blame] | 59 | // 1 sigmask 64-bit signal mask (not used with _setjmp / _longjmp) |
| 60 | // 2 " " |
Elliott Hughes | e58d49e | 2018-02-05 13:36:02 -0800 | [diff] [blame^] | 61 | // 3 reserved (unused to allow float_base to be maximally aligned; |
| 62 | // this avoids software emulation of unaligned loads/stores) |
Elliott Hughes | 460130b | 2018-01-31 09:05:26 -0800 | [diff] [blame] | 63 | // 4 float_base base of float registers (d8 to d15) |
| 64 | // 20 float_state floating-point status and control register |
| 65 | // 21 core_base base of core registers (r4-r11, r13-r14) |
Elliott Hughes | e58d49e | 2018-02-05 13:36:02 -0800 | [diff] [blame^] | 66 | // 31 checksum checksum of all of the core registers, to give better error messages |
Elliott Hughes | 460130b | 2018-01-31 09:05:26 -0800 | [diff] [blame] | 67 | // 32 reserved reserved entries (room to grow) |
Elliott Hughes | e58d49e | 2018-02-05 13:36:02 -0800 | [diff] [blame^] | 68 | // ... |
| 69 | // 63 " " |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 70 | |
Elliott Hughes | 7509622 | 2014-12-08 16:01:20 -0800 | [diff] [blame] | 71 | #define _JB_SIGFLAG 0 |
Elliott Hughes | 460130b | 2018-01-31 09:05:26 -0800 | [diff] [blame] | 72 | #define _JB_SIGMASK (_JB_SIGFLAG + 1) |
| 73 | #define _JB_FLOAT_BASE (_JB_SIGMASK + 3) |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 74 | #define _JB_FLOAT_STATE (_JB_FLOAT_BASE + (15-8+1)*2) |
| 75 | #define _JB_CORE_BASE (_JB_FLOAT_STATE+1) |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 76 | #define _JB_CHECKSUM (_JB_CORE_BASE+10) |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 77 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 78 | ENTRY(setjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 79 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(setjmp) |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 80 | mov r1, #1 |
| 81 | b sigsetjmp |
Kenny Root | 420878c | 2011-02-16 11:55:58 -0800 | [diff] [blame] | 82 | END(setjmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 83 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 84 | ENTRY(_setjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 85 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(_setjmp) |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 86 | mov r1, #0 |
| 87 | b sigsetjmp |
| 88 | END(_setjmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 89 | |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 90 | #define MANGLE_REGISTERS 1 |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 91 | #define USE_CHECKSUM 1 |
| 92 | |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 93 | .macro m_mangle_registers reg |
| 94 | #if MANGLE_REGISTERS |
| 95 | eor r4, r4, \reg |
| 96 | eor r5, r5, \reg |
| 97 | eor r6, r6, \reg |
| 98 | eor r7, r7, \reg |
| 99 | eor r8, r8, \reg |
| 100 | eor r9, r9, \reg |
| 101 | eor r10, r10, \reg |
| 102 | eor r11, r11, \reg |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 103 | eor r13, r13, \reg |
| 104 | eor r14, r14, \reg |
| 105 | #endif |
| 106 | .endm |
| 107 | |
| 108 | .macro m_unmangle_registers reg |
| 109 | m_mangle_registers \reg |
| 110 | .endm |
| 111 | |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 112 | .macro m_calculate_checksum dst, src, scratch |
| 113 | mov \dst, #0 |
| 114 | .irp i,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28 |
| 115 | ldr \scratch, [\src, #(\i * 4)] |
| 116 | eor \dst, \dst, \scratch |
| 117 | .endr |
| 118 | .endm |
| 119 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 120 | // int sigsetjmp(sigjmp_buf env, int save_signal_mask); |
| 121 | ENTRY(sigsetjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 122 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(sigsetjmp) |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 123 | stmfd sp!, {r0, lr} |
| 124 | .cfi_def_cfa_offset 8 |
| 125 | .cfi_rel_offset r0, 0 |
| 126 | .cfi_rel_offset lr, 4 |
| 127 | |
| 128 | mov r0, r1 |
| 129 | bl __bionic_setjmp_cookie_get |
| 130 | mov r1, r0 |
| 131 | |
| 132 | ldmfd sp, {r0} |
| 133 | |
| 134 | // Save the setjmp cookie for later. |
| 135 | bic r2, r1, #1 |
| 136 | stmfd sp!, {r2} |
| 137 | .cfi_adjust_cfa_offset 4 |
| 138 | |
| 139 | // Record the setjmp cookie and whether or not we're saving the signal mask. |
Elliott Hughes | 7509622 | 2014-12-08 16:01:20 -0800 | [diff] [blame] | 140 | str r1, [r0, #(_JB_SIGFLAG * 4)] |
| 141 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 142 | // Do we need to save the signal mask? |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 143 | tst r1, #1 |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 144 | beq 1f |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 145 | |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 146 | // Align the stack. |
| 147 | sub sp, #4 |
| 148 | .cfi_adjust_cfa_offset 4 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 149 | |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 150 | // Save the current signal mask. |
| 151 | add r2, r0, #(_JB_SIGMASK * 4) |
| 152 | mov r0, #2 // SIG_SETMASK |
| 153 | mov r1, #0 |
Elliott Hughes | 460130b | 2018-01-31 09:05:26 -0800 | [diff] [blame] | 154 | bl sigprocmask64 |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 155 | |
| 156 | // Unalign the stack. |
| 157 | add sp, #4 |
| 158 | .cfi_adjust_cfa_offset -4 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 159 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 160 | 1: |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 161 | ldmfd sp!, {r2} |
| 162 | .cfi_adjust_cfa_offset -4 |
| 163 | ldmfd sp!, {r0, lr} |
| 164 | .cfi_adjust_cfa_offset -8 |
| 165 | .cfi_restore r0 |
| 166 | .cfi_restore lr |
| 167 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 168 | // Save core registers. |
| 169 | add r1, r0, #(_JB_CORE_BASE * 4) |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 170 | m_mangle_registers r2 |
Josh Gao | b24d743 | 2015-10-19 12:56:50 -0700 | [diff] [blame] | 171 | |
| 172 | // ARM deprecates using sp in the register list for stmia. |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 173 | stmia r1, {r4-r11, lr} |
| 174 | str sp, [r1, #(9 * 4)] |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 175 | m_unmangle_registers r2 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 176 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 177 | // Save floating-point registers. |
| 178 | add r1, r0, #(_JB_FLOAT_BASE * 4) |
| 179 | vstmia r1, {d8-d15} |
David 'Digit' Turner | 68b5f55 | 2010-03-25 09:54:33 -0700 | [diff] [blame] | 180 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 181 | // Save floating-point state. |
| 182 | fmrx r1, fpscr |
| 183 | str r1, [r0, #(_JB_FLOAT_STATE * 4)] |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 184 | |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 185 | #if USE_CHECKSUM |
| 186 | // Calculate the checksum. |
| 187 | m_calculate_checksum r12, r0, r2 |
| 188 | str r12, [r0, #(_JB_CHECKSUM * 4)] |
| 189 | #endif |
| 190 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 191 | mov r0, #0 |
| 192 | bx lr |
| 193 | END(sigsetjmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 194 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 195 | // void siglongjmp(sigjmp_buf env, int value); |
| 196 | ENTRY(siglongjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 197 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(siglongjmp) |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 198 | stmfd sp!, {r0, r1, lr} |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 199 | .cfi_def_cfa_offset 12 |
| 200 | .cfi_rel_offset r0, 0 |
| 201 | .cfi_rel_offset r1, 4 |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 202 | .cfi_rel_offset lr, 8 |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 203 | |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 204 | #if USE_CHECKSUM |
| 205 | // Check the checksum before doing anything. |
| 206 | m_calculate_checksum r12, r0, r3 |
| 207 | ldr r2, [r0, #(_JB_CHECKSUM * 4)] |
| 208 | |
| 209 | teq r2, r12 |
| 210 | bne __bionic_setjmp_checksum_mismatch |
| 211 | #endif |
| 212 | |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 213 | // Fetch the signal flag. |
| 214 | ldr r1, [r0, #(_JB_SIGFLAG * 4)] |
| 215 | |
| 216 | // Do we need to restore the signal mask? |
| 217 | ands r1, r1, #1 |
| 218 | beq 1f |
| 219 | |
| 220 | // Restore the signal mask. |
Elliott Hughes | 7ebafb3 | 2018-01-29 10:23:01 -0800 | [diff] [blame] | 221 | mov r2, #0 |
| 222 | add r1, r0, #(_JB_SIGMASK * 4) |
| 223 | mov r0, #2 // SIG_SETMASK |
Elliott Hughes | 460130b | 2018-01-31 09:05:26 -0800 | [diff] [blame] | 224 | bl sigprocmask64 |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 225 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 226 | 1: |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 227 | ldmfd sp!, {r0, r1, lr} |
| 228 | .cfi_adjust_cfa_offset -12 |
| 229 | .cfi_restore r0 |
| 230 | .cfi_restore r1 |
| 231 | .cfi_restore lr |
| 232 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 233 | // Restore floating-point registers. |
| 234 | add r2, r0, #(_JB_FLOAT_BASE * 4) |
| 235 | vldmia r2, {d8-d15} |
| 236 | |
| 237 | // Restore floating-point state. |
| 238 | ldr r2, [r0, #(_JB_FLOAT_STATE * 4)] |
| 239 | fmxr fpscr, r2 |
| 240 | |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 241 | // Load the cookie. |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 242 | ldr r3, [r0, #(_JB_SIGFLAG * 4)] |
| 243 | bic r3, r3, #1 |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 244 | |
| 245 | // Restore core registers. |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 246 | add r2, r0, #(_JB_CORE_BASE * 4) |
Josh Gao | b24d743 | 2015-10-19 12:56:50 -0700 | [diff] [blame] | 247 | |
| 248 | // ARM deprecates using sp in the register list for ldmia. |
Josh Gao | a4c6913 | 2016-03-02 19:03:17 -0800 | [diff] [blame] | 249 | ldmia r2, {r4-r11, lr} |
| 250 | ldr sp, [r2, #(9 * 4)] |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 251 | m_unmangle_registers r3 |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 252 | |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 253 | // Save the return value/address and check the setjmp cookie. |
| 254 | stmfd sp!, {r1, lr} |
| 255 | .cfi_adjust_cfa_offset 8 |
| 256 | .cfi_rel_offset lr, 4 |
| 257 | mov r0, r3 |
| 258 | bl __bionic_setjmp_cookie_check |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 259 | |
Josh Gao | 7fda8d2 | 2015-09-10 15:40:24 -0700 | [diff] [blame] | 260 | // Restore return value/address. |
| 261 | ldmfd sp!, {r0, lr} |
| 262 | .cfi_adjust_cfa_offset -8 |
| 263 | .cfi_restore lr |
| 264 | |
Elliott Hughes | b393299 | 2014-12-05 15:39:51 -0800 | [diff] [blame] | 265 | teq r0, #0 |
| 266 | moveq r0, #1 |
| 267 | bx lr |
| 268 | END(siglongjmp) |
| 269 | |
Christopher Ferris | 2495851 | 2015-03-25 09:12:00 -0700 | [diff] [blame] | 270 | ALIAS_SYMBOL(longjmp, siglongjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 271 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(longjmp) |
Christopher Ferris | 2495851 | 2015-03-25 09:12:00 -0700 | [diff] [blame] | 272 | ALIAS_SYMBOL(_longjmp, siglongjmp) |
dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 273 | __BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(_longjmp) |