| 1 |
/* Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
This file is free software; you can redistribute it and/or modify it |
| 4 |
under the terms of the GNU General Public License as published by the |
| 5 |
Free Software Foundation; either version 3, or (at your option) any |
| 6 |
later version. |
| 7 |
|
| 8 |
This file is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
General Public License for more details. |
| 12 |
|
| 13 |
Under Section 7 of GPL version 3, you are granted additional |
| 14 |
permissions described in the GCC Runtime Library Exception, version |
| 15 |
3.1, as published by the Free Software Foundation. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License and |
| 18 |
a copy of the GCC Runtime Library Exception along with this program; |
| 19 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 20 |
<http://www.gnu.org/licenses/>. */ |
| 21 |
|
| 22 |
/* Unwind shadow stack. */ |
| 23 |
#if defined(__riscv_zicfiss) |
| 24 |
# include "config/riscv/shadow-stack-unwind.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
#ifndef inhibit_libc |
| 28 |
|
| 29 |
#include <signal.h> |
| 30 |
#include <stdint.h> |
| 31 |
#include <sys/ucontext.h> |
| 32 |
|
| 33 |
#define LI_A7_8B 0x08b00893 |
| 34 |
#define ECALL 0x00000073 |
| 35 |
|
| 36 |
#define MD_FALLBACK_FRAME_STATE_FOR riscv_fallback_frame_state |
| 37 |
|
| 38 |
static _Unwind_Reason_Code |
| 39 |
riscv_fallback_frame_state (struct _Unwind_Context *context, |
| 40 |
_Unwind_FrameState * fs) |
| 41 |
{ |
| 42 |
/* The kernel creates an rt_sigframe on the stack immediately prior |
| 43 |
to delivering a signal. |
| 44 |
|
| 45 |
This structure must have the same shape as the linux kernel |
| 46 |
equivalent. */ |
| 47 |
struct rt_sigframe |
| 48 |
{ |
| 49 |
siginfo_t info; |
| 50 |
ucontext_t uc; |
| 51 |
}; |
| 52 |
|
| 53 |
struct rt_sigframe *rt_; |
| 54 |
_Unwind_Ptr new_cfa; |
| 55 |
uint16_t *pc = context->ra; |
| 56 |
struct sigcontext *sc; |
| 57 |
int i; |
| 58 |
|
| 59 |
/* A signal frame will have a return address pointing to |
| 60 |
__default_sa_restorer. This code is hardwired as: |
| 61 |
|
| 62 |
0x08b00893 li a7,0x8b |
| 63 |
0x00000073 ecall |
| 64 |
|
| 65 |
Note, the PC might only have 2-byte alignment. |
| 66 |
*/ |
| 67 |
if (pc[0] != (uint16_t)LI_A7_8B || pc[1] != (uint16_t)(LI_A7_8B >> 16) |
| 68 |
|| pc[2] != (uint16_t)ECALL || pc[3] != (uint16_t)(ECALL >> 16)) |
| 69 |
return _URC_END_OF_STACK; |
| 70 |
|
| 71 |
rt_ = context->cfa; |
| 72 |
sc = (struct sigcontext *) &rt_->uc.uc_mcontext; |
| 73 |
|
| 74 |
new_cfa = (_Unwind_Ptr) sc; |
| 75 |
fs->regs.cfa_how = CFA_REG_OFFSET; |
| 76 |
fs->regs.cfa_reg = __LIBGCC_STACK_POINTER_REGNUM__; |
| 77 |
fs->regs.cfa_offset = new_cfa - (_Unwind_Ptr) context->cfa; |
| 78 |
|
| 79 |
for (i = 0; i < 32; i++) |
| 80 |
{ |
| 81 |
fs->regs.how[i] = REG_SAVED_OFFSET; |
| 82 |
fs->regs.reg[i].loc.offset = (_Unwind_Ptr) &sc->gregs[i] - new_cfa; |
| 83 |
} |
| 84 |
|
| 85 |
fs->signal_frame = 1; |
| 86 |
fs->retaddr_column = __LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__; |
| 87 |
fs->regs.how[fs->retaddr_column] = REG_SAVED_VAL_OFFSET; |
| 88 |
fs->regs.reg[fs->retaddr_column].loc.offset = |
| 89 |
(_Unwind_Ptr) sc->gregs[0] - new_cfa; |
| 90 |
|
| 91 |
return _URC_NO_REASON; |
| 92 |
} |
| 93 |
|
| 94 |
#endif |