Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
Elliott Hughes | 54a7494 | 2014-01-03 16:40:37 -0800 | [diff] [blame] | 3 | * All rights reserved. |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 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 | |
Elliott Hughes | 851e68a | 2014-02-19 16:53:20 -0800 | [diff] [blame] | 29 | #include <private/bionic_asm.h> |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 30 | |
Elliott Hughes | 9fb536d | 2014-12-05 12:17:25 -0800 | [diff] [blame^] | 31 | // _JBLEN is the size of a jmp_buf in longs(64bit on AArch64). |
| 32 | #define _JBLEN 32 |
| 33 | |
| 34 | // According to AARCH64 PCS document we need to save the following |
| 35 | // registers: |
| 36 | // |
| 37 | // Core x19 - x30, sp (see section 5.1.1) |
| 38 | // VFP d8 - d15 (see section 5.1.2) |
| 39 | // |
| 40 | // NOTE: All the registers saved here will have 64bit vales (except FPSR). |
| 41 | // AAPCS mandates that the higher part of q registers do not need to |
| 42 | // be saved by the callee. |
| 43 | // |
| 44 | |
| 45 | // The structure of jmp_buf for AArch64: |
| 46 | // |
| 47 | // NOTE: _JBLEN is the size of jmp_buf in longs(64bit on AArch64)! The table |
| 48 | // below computes the offsets in words(32bit). |
| 49 | // |
| 50 | // word name description |
| 51 | // 0 magic magic number |
| 52 | // 1 sigmask signal mask (not used with _setjmp / _longjmp) |
| 53 | // 2 core_base base of core registers (x19-x30, sp) |
| 54 | // 28 float_base base of float registers (d8-d15) |
| 55 | // 44 reserved reserved entries (room to grow) |
| 56 | // 64 |
| 57 | // |
| 58 | // |
| 59 | // NOTE: The instructions that load/store core/vfp registers expect 8-byte |
| 60 | // alignment. Contrary to the previous setjmp header for ARM we do not |
| 61 | // need to save status/control registers for VFP (it is not a |
| 62 | // requirement for setjmp). |
| 63 | // |
| 64 | |
| 65 | #define _JB_MAGIC 0 |
| 66 | #define _JB_SIGMASK (_JB_MAGIC+1) |
| 67 | #define _JB_CORE_BASE (_JB_SIGMASK+1) |
| 68 | #define _JB_FLOAT_BASE (_JB_CORE_BASE + (31-19+1)*2) |
| 69 | |
| 70 | .L_setjmp_magic_signal_mask_n: .word 0x53657200 |
| 71 | .L_setjmp_magic_signal_mask_y: .word 0x53657201 |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 72 | |
| 73 | ENTRY(setjmp) |
Elliott Hughes | 9fb536d | 2014-12-05 12:17:25 -0800 | [diff] [blame^] | 74 | mov w1, #1 |
| 75 | b sigsetjmp |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 76 | END(setjmp) |
| 77 | |
Elliott Hughes | 9fb536d | 2014-12-05 12:17:25 -0800 | [diff] [blame^] | 78 | ENTRY(_setjmp) |
| 79 | mov w1, #0 |
| 80 | b sigsetjmp |
| 81 | END(_setjmp) |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 82 | |
Elliott Hughes | 9fb536d | 2014-12-05 12:17:25 -0800 | [diff] [blame^] | 83 | // int sigsetjmp(sigjmp_buf env, int save_signal_mask); |
| 84 | ENTRY(sigsetjmp) |
| 85 | // Do we need to save the signal mask? |
| 86 | ldr w9, .L_setjmp_magic_signal_mask_n |
| 87 | cbz w1, 1f |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 88 | |
Elliott Hughes | 9fb536d | 2014-12-05 12:17:25 -0800 | [diff] [blame^] | 89 | // Get current signal mask. |
| 90 | stp x0, x30, [sp, #-16]! |
| 91 | mov x0, xzr |
| 92 | bl sigblock |
| 93 | mov w1, w0 |
| 94 | ldp x0, x30, [sp], #16 |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 95 | |
Elliott Hughes | 9fb536d | 2014-12-05 12:17:25 -0800 | [diff] [blame^] | 96 | // Save signal mask. |
| 97 | str w1, [x0, #(_JB_SIGMASK * 4)] |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 98 | |
Elliott Hughes | 9fb536d | 2014-12-05 12:17:25 -0800 | [diff] [blame^] | 99 | ldr w9, .L_setjmp_magic_signal_mask_y |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 100 | |
Elliott Hughes | 9fb536d | 2014-12-05 12:17:25 -0800 | [diff] [blame^] | 101 | 1: |
| 102 | // Save magic number. |
| 103 | str w9, [x0, #(_JB_MAGIC * 4)] |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 104 | |
Elliott Hughes | 9fb536d | 2014-12-05 12:17:25 -0800 | [diff] [blame^] | 105 | // Save core registers. |
| 106 | mov x10, sp |
| 107 | stp x30, x10, [x0, #(_JB_CORE_BASE * 4 + 16 * 0)] |
| 108 | stp x28, x29, [x0, #(_JB_CORE_BASE * 4 + 16 * 1)] |
| 109 | stp x26, x27, [x0, #(_JB_CORE_BASE * 4 + 16 * 2)] |
| 110 | stp x24, x25, [x0, #(_JB_CORE_BASE * 4 + 16 * 3)] |
| 111 | stp x22, x23, [x0, #(_JB_CORE_BASE * 4 + 16 * 4)] |
| 112 | stp x20, x21, [x0, #(_JB_CORE_BASE * 4 + 16 * 5)] |
| 113 | str x19, [x0, #(_JB_CORE_BASE * 4 + 16 * 6)] |
| 114 | |
| 115 | // Save floating point registers. |
| 116 | stp d14, d15, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 0)] |
| 117 | stp d12, d13, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 1)] |
| 118 | stp d10, d11, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 2)] |
| 119 | stp d8, d9, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 3)] |
| 120 | |
| 121 | mov w0, wzr |
| 122 | ret |
| 123 | END(sigsetjmp) |
| 124 | |
| 125 | // void siglongjmp(sigjmp_buf env, int value); |
| 126 | ENTRY(siglongjmp) |
| 127 | // Check magic. |
| 128 | ldr w10, [x0, #(_JB_MAGIC * 4)] |
| 129 | ldr w9, .L_setjmp_magic_signal_mask_n |
| 130 | cmp w9, w10 |
| 131 | b.eq 1f |
| 132 | ldr w9, .L_setjmp_magic_signal_mask_y |
| 133 | cmp w9, w10 |
| 134 | b.ne longjmperror |
| 135 | |
| 136 | // Restore signal mask. |
| 137 | stp x0, x30, [sp, #-16]! |
| 138 | mov x19, x1 |
| 139 | ldr w0, [x0, #(_JB_SIGMASK * 4)] |
| 140 | bl sigsetmask |
| 141 | ldp x0, x30, [sp], #16 |
| 142 | mov x1, x19 |
| 143 | |
| 144 | 1: |
| 145 | // Restore core registers. |
| 146 | ldp x30, x10, [x0, #(_JB_CORE_BASE * 4 + 16 * 0)] |
| 147 | mov sp, x10 |
| 148 | ldp x28, x29, [x0, #(_JB_CORE_BASE * 4 + 16 * 1)] |
| 149 | ldp x26, x27, [x0, #(_JB_CORE_BASE * 4 + 16 * 2)] |
| 150 | ldp x24, x25, [x0, #(_JB_CORE_BASE * 4 + 16 * 3)] |
| 151 | ldp x22, x23, [x0, #(_JB_CORE_BASE * 4 + 16 * 4)] |
| 152 | ldp x20, x21, [x0, #(_JB_CORE_BASE * 4 + 16 * 5)] |
| 153 | ldr x19, [x0, #(_JB_CORE_BASE * 4 + 16 * 6)] |
| 154 | |
| 155 | // Restore floating point registers. |
| 156 | ldp d14, d15, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 0)] |
| 157 | ldp d12, d13, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 1)] |
| 158 | ldp d10, d11, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 2)] |
| 159 | ldp d8, d9, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 3)] |
| 160 | |
| 161 | // Validate sp (sp mod 16 = 0) and lr (lr mod 4 = 0). |
| 162 | tst x30, #3 |
| 163 | b.ne longjmperror |
| 164 | mov x10, sp |
| 165 | tst x10, #15 |
| 166 | b.ne longjmperror |
| 167 | |
| 168 | // Set return value. |
| 169 | cmp w1, wzr |
| 170 | csinc w0, w1, wzr, ne |
| 171 | ret |
| 172 | END(siglongjmp) |
| 173 | |
| 174 | .globl longjmp |
| 175 | .equ longjmp, siglongjmp |
| 176 | .globl _longjmp |
| 177 | .equ _longjmp, siglongjmp |