|
| 1 | +/* Return stack-safe version of longjmp for ARM64. |
| 2 | + |
| 3 | + Copyright (C) 1997-2018 Free Software Foundation, Inc. |
| 4 | + |
| 5 | + Return stack support added by |
| 6 | + Philipp Zieris <[email protected]> |
| 7 | + Copyright (C) 2018 Fraunhofer AISEC. |
| 8 | + |
| 9 | + This program is free software; you can redistribute it and/or |
| 10 | + modify it under the terms of the GNU Lesser General Public License as |
| 11 | + published by the Free Software Foundation; either version 2.1 of the |
| 12 | + License, or (at your option) any later version. |
| 13 | + |
| 14 | + This program is distributed in the hope that it will be useful, |
| 15 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + Lesser General Public License for more details. |
| 18 | + |
| 19 | + You should have received a copy of the GNU Lesser General Public |
| 20 | + License along with the GNU C Library. If not, see |
| 21 | + <http://www.gnu.org/licenses/>. */ |
| 22 | + |
| 23 | +#include <sysdep.h> |
| 24 | +#include <jmpbuf-offsets.h> |
| 25 | +#include <stap-probe.h> |
| 26 | + |
| 27 | +/* Safe version of __longjmp. Restores the return stack pointer |
| 28 | + by unwinding the return stack until the marker is reached. |
| 29 | + __safe_longjmp(jmpbuf, val) */ |
| 30 | + |
| 31 | +ENTRY (__safe_longjmp) |
| 32 | + cfi_def_cfa(x0, 0) |
| 33 | + cfi_offset(x19, JB_X19<<3) |
| 34 | + cfi_offset(x20, JB_X20<<3) |
| 35 | + cfi_offset(x21, JB_X21<<3) |
| 36 | + cfi_offset(x22, JB_X22<<3) |
| 37 | + cfi_offset(x23, JB_X23<<3) |
| 38 | + cfi_offset(x24, JB_X24<<3) |
| 39 | + cfi_offset(x25, JB_X25<<3) |
| 40 | + cfi_offset(x26, JB_X26<<3) |
| 41 | + cfi_offset(x27, JB_X27<<3) |
| 42 | + cfi_offset(x29, JB_X29<<3) |
| 43 | + cfi_offset(x30, JB_LR<<3) |
| 44 | + |
| 45 | + cfi_offset( d8, JB_D8<<3) |
| 46 | + cfi_offset( d9, JB_D9<<3) |
| 47 | + cfi_offset(d10, JB_D10<<3) |
| 48 | + cfi_offset(d11, JB_D11<<3) |
| 49 | + cfi_offset(d12, JB_D12<<3) |
| 50 | + cfi_offset(d13, JB_D13<<3) |
| 51 | + cfi_offset(d14, JB_D14<<3) |
| 52 | + cfi_offset(d15, JB_D15<<3) |
| 53 | + |
| 54 | + DELOUSE (0) |
| 55 | + |
| 56 | + ldp x19, x20, [x0, #JB_X19<<3] |
| 57 | + ldp x21, x22, [x0, #JB_X21<<3] |
| 58 | + ldp x23, x24, [x0, #JB_X23<<3] |
| 59 | + ldp x25, x26, [x0, #JB_X25<<3] |
| 60 | + ldr x27, [x0, #JB_X27<<3] |
| 61 | +#ifdef PTR_DEMANGLE |
| 62 | + ldp x29, x4, [x0, #JB_X29<<3] |
| 63 | + PTR_DEMANGLE (30, 4, 3, 2) |
| 64 | +#else |
| 65 | + ldp x29, x30, [x0, #JB_X29<<3] |
| 66 | +#endif |
| 67 | + /* longjmp probe takes 3 arguments, address of jump buffer as |
| 68 | + first argument (8@x0), return value as second argument (-4@x1), |
| 69 | + and target address (8@x30), respectively. */ |
| 70 | + LIBC_PROBE (longjmp, 3, 8@x0, -4@x1, 8@x30) |
| 71 | + ldp d8, d9, [x0, #JB_D8<<3] |
| 72 | + ldp d10, d11, [x0, #JB_D10<<3] |
| 73 | + ldp d12, d13, [x0, #JB_D12<<3] |
| 74 | + ldp d14, d15, [x0, #JB_D14<<3] |
| 75 | + |
| 76 | + /* Originally this was implemented with a series of |
| 77 | + .cfi_restore() directives. |
| 78 | + |
| 79 | + The theory was that cfi_restore should revert to previous |
| 80 | + frame value is the same as the current value. In practice |
| 81 | + this doesn't work, even after cfi_restore() gdb continues |
| 82 | + to try to recover a previous frame value offset from x0, |
| 83 | + which gets stuffed after a few more instructions. The |
| 84 | + cfi_same_value() mechanism appears to work fine. */ |
| 85 | + |
| 86 | + cfi_same_value(x19) |
| 87 | + cfi_same_value(x20) |
| 88 | + cfi_same_value(x21) |
| 89 | + cfi_same_value(x22) |
| 90 | + cfi_same_value(x23) |
| 91 | + cfi_same_value(x24) |
| 92 | + cfi_same_value(x25) |
| 93 | + cfi_same_value(x26) |
| 94 | + cfi_same_value(x27) |
| 95 | + cfi_same_value(x29) |
| 96 | + cfi_same_value(x30) |
| 97 | + cfi_same_value(d8) |
| 98 | + cfi_same_value(d9) |
| 99 | + cfi_same_value(d10) |
| 100 | + cfi_same_value(d11) |
| 101 | + cfi_same_value(d12) |
| 102 | + cfi_same_value(d13) |
| 103 | + cfi_same_value(d14) |
| 104 | + cfi_same_value(d15) |
| 105 | +#ifdef PTR_DEMANGLE |
| 106 | + ldr x4, [x0, #JB_SP<<3] |
| 107 | + PTR_DEMANGLE (5, 4, 3, 2) |
| 108 | +#else |
| 109 | + ldr x5, [x0, #JB_SP<<3] |
| 110 | +#endif |
| 111 | + mov sp, x5 |
| 112 | + /* Unwind return stack. */ |
| 113 | + ldr x4, [x0, #JB_MARKER<<3] |
| 114 | +.Lrs_unwind: |
| 115 | + ldr x5, [x28, #-8]! |
| 116 | + cmp x4, x5 |
| 117 | + bne .Lrs_unwind |
| 118 | + add x28, x28, #8 |
| 119 | + |
| 120 | + /* longjmp_target probe takes 3 arguments, address of jump buffer |
| 121 | + as first argument (8@x0), return value as second argument (-4@x1), |
| 122 | + and target address (8@x30), respectively. */ |
| 123 | + LIBC_PROBE (longjmp_target, 3, 8@x0, -4@x1, 8@x30) |
| 124 | + cmp x1, #0 |
| 125 | + mov x0, #1 |
| 126 | + csel x0, x1, x0, ne |
| 127 | + /* Use br instead of ret because ret is guaranteed to mispredict */ |
| 128 | + br x30 |
| 129 | +END (__safe_longjmp) |
0 commit comments