Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 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 <machine/setjmp.h> |
| 31 | |
| 32 | /* |
| 33 | * C library - _setjmp, _longjmp |
| 34 | * |
| 35 | * _longjmp(jmp_buf state, int value) |
| 36 | * will generate a "return(v)" from the last call to _setjmp(state) by restoring |
| 37 | * registers from the stack. The previous signal state is NOT restored. |
| 38 | * |
| 39 | * NOTE: x0 return value |
| 40 | * x9-x15 temporary registers |
| 41 | */ |
| 42 | |
| 43 | ENTRY(setjmp) |
| 44 | /* block all signals an retrieve signal mask */ |
| 45 | stp x0, x30, [sp, #-16]! |
| 46 | |
| 47 | mov x0, xzr |
| 48 | bl PIC_SYM(_C_LABEL(sigblock), PLT) |
| 49 | mov w1, w0 |
| 50 | |
| 51 | ldp x0, x30, [sp], #16 |
| 52 | |
| 53 | /* store signal mask */ |
| 54 | str w1, [x0, #(_JB_SIGMASK *4)] |
| 55 | |
| 56 | /* store magic number */ |
| 57 | ldr w9, .L_setjmp_magic |
| 58 | str w9, [x0, #(_JB_MAGIC * 4)] |
| 59 | |
| 60 | /* store core registers */ |
| 61 | mov x10, sp |
| 62 | stp x30, x10, [x0, #(_JB_CORE_BASE * 4 + 16 * 0)] |
| 63 | stp x28, x29, [x0, #(_JB_CORE_BASE * 4 + 16 * 1)] |
| 64 | stp x26, x27, [x0, #(_JB_CORE_BASE * 4 + 16 * 2)] |
| 65 | stp x24, x25, [x0, #(_JB_CORE_BASE * 4 + 16 * 3)] |
| 66 | stp x22, x23, [x0, #(_JB_CORE_BASE * 4 + 16 * 4)] |
| 67 | stp x20, x21, [x0, #(_JB_CORE_BASE * 4 + 16 * 5)] |
| 68 | str x19, [x0, #(_JB_CORE_BASE * 4 + 16 * 6)] |
| 69 | |
| 70 | /* store floating point registers */ |
| 71 | stp d14, d15, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 0)] |
| 72 | stp d12, d13, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 1)] |
| 73 | stp d10, d11, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 2)] |
| 74 | stp d8, d9, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 3)] |
| 75 | |
| 76 | mov w0, wzr |
| 77 | ret |
| 78 | END(setjmp) |
| 79 | |
| 80 | .L_setjmp_magic: |
| 81 | .word _JB_MAGIC__SETJMP |
| 82 | |
| 83 | ENTRY(longjmp) |
| 84 | /* check magic */ |
| 85 | ldr w9, .L_setjmp_magic |
| 86 | ldr w10, [x0, #(_JB_MAGIC * 4)] |
| 87 | cmp w9, w10 |
| 88 | b.ne botch |
| 89 | |
| 90 | /* restore core registers */ |
| 91 | ldp x30, x10, [x0, #(_JB_CORE_BASE * 4 + 16 * 0)] |
| 92 | mov sp, x10 |
| 93 | ldp x28, x29, [x0, #(_JB_CORE_BASE * 4 + 16 * 1)] |
| 94 | ldp x26, x27, [x0, #(_JB_CORE_BASE * 4 + 16 * 2)] |
| 95 | ldp x24, x25, [x0, #(_JB_CORE_BASE * 4 + 16 * 3)] |
| 96 | ldp x22, x23, [x0, #(_JB_CORE_BASE * 4 + 16 * 4)] |
| 97 | ldp x20, x21, [x0, #(_JB_CORE_BASE * 4 + 16 * 5)] |
| 98 | ldr x19, [x0, #(_JB_CORE_BASE * 4 + 16 * 6)] |
| 99 | |
| 100 | /* restore floating point registers */ |
| 101 | ldp d14, d15, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 0)] |
| 102 | ldp d12, d13, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 1)] |
| 103 | ldp d10, d11, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 2)] |
| 104 | ldp d8, d9, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 3)] |
| 105 | |
| 106 | /* validate sp (sp mod 16 = 0) and lr (lr mod 4 = 0) */ |
| 107 | tst x30, #3 |
| 108 | b.ne botch |
| 109 | mov x10, sp |
| 110 | tst x10, #15 |
| 111 | b.ne botch |
| 112 | |
| 113 | /* set return value */ |
| 114 | cmp w1, wzr |
| 115 | csinc w0, w1, wzr, ne |
| 116 | ret |
| 117 | |
| 118 | /* validation failed, die die die */ |
| 119 | botch: |
| 120 | bl PIC_SYM(_C_LABEL(longjmperror), PLT) |
| 121 | bl PIC_SYM(_C_LABEL(abort), PLT) |
| 122 | b . - 8 /* Cannot get here */ |
| 123 | END(longjmp) |