| 1 |
/* DWARF2 EH unwinding support for MicroBlaze Linux. |
| 2 |
Copyright (C) 2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
Under Section 7 of GPL version 3, you are granted additional |
| 17 |
permissions described in the GCC Runtime Library Exception, version |
| 18 |
3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License and |
| 21 |
a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
<http://www.gnu.org/licenses/>. */ |
| 24 |
|
| 25 |
#ifndef inhibit_libc |
| 26 |
|
| 27 |
/* Do code reading to identify a signal frame, and set the frame state |
| 28 |
data appropriately. See unwind-dw2.c for the structs. */ |
| 29 |
|
| 30 |
#include <signal.h> |
| 31 |
#include <sys/ucontext.h> |
| 32 |
#include <asm/unistd.h> |
| 33 |
|
| 34 |
#define MD_FALLBACK_FRAME_STATE_FOR microblaze_fallback_frame_state |
| 35 |
|
| 36 |
static _Unwind_Reason_Code |
| 37 |
microblaze_fallback_frame_state (struct _Unwind_Context *context, |
| 38 |
_Unwind_FrameState *fs) |
| 39 |
{ |
| 40 |
const unsigned int *pc = (const unsigned int *) context->ra; |
| 41 |
struct sigcontext *sc; |
| 42 |
_Unwind_Ptr new_cfa; |
| 43 |
int i; |
| 44 |
|
| 45 |
/* The outermost frame of a thread may leave a null or near-null |
| 46 |
return address; do not dereference it looking for the |
| 47 |
trampoline. */ |
| 48 |
if ((unsigned long) pc < 4096) |
| 49 |
return _URC_END_OF_STACK; |
| 50 |
|
| 51 |
/* The kernel writes the signal trampoline onto the stack |
| 52 |
(struct rt_sigframe.tramp): |
| 53 |
|
| 54 |
addik r12, r0, __NR_rt_sigreturn |
| 55 |
brki r14, 0x8 |
| 56 |
|
| 57 |
and sets the saved r15 to the trampoline address minus 8 (the |
| 58 |
handler returns with "rtsd r15, 8"), so the unwound return |
| 59 |
address points 8 bytes before the trampoline. */ |
| 60 |
if (pc[0] == (0x31800000 | __NR_rt_sigreturn) && pc[1] == 0xb9cc0008) |
| 61 |
; |
| 62 |
else if (pc[2] == (0x31800000 | __NR_rt_sigreturn) && pc[3] == 0xb9cc0008) |
| 63 |
pc += 2; |
| 64 |
else |
| 65 |
return _URC_END_OF_STACK; |
| 66 |
|
| 67 |
/* The trampoline is the last member of the kernel's rt_sigframe and |
| 68 |
the ucontext sits directly in front of it. Anchor there rather |
| 69 |
than at the CFA so the layout of the frame head does not matter |
| 70 |
(the kernel may insert an ABI argument-home gap at the front). |
| 71 |
uClibc's ucontext_t matches the kernel's struct ucontext. */ |
| 72 |
ucontext_t *uc = (ucontext_t *) ((_Unwind_Ptr) pc - sizeof (ucontext_t)); |
| 73 |
|
| 74 |
sc = (struct sigcontext *) &uc->uc_mcontext; |
| 75 |
|
| 76 |
new_cfa = sc->regs.r1; |
| 77 |
fs->regs.cfa_how = CFA_REG_OFFSET; |
| 78 |
fs->regs.cfa_reg = 1; /* r1, the stack pointer. */ |
| 79 |
fs->regs.cfa_offset = new_cfa - (_Unwind_Ptr) context->cfa; |
| 80 |
|
| 81 |
/* pt_regs holds r0..r31 consecutively. */ |
| 82 |
for (i = 0; i < 32; i++) |
| 83 |
{ |
| 84 |
fs->regs.how[i] = REG_SAVED_OFFSET; |
| 85 |
fs->regs.reg[i].loc.offset |
| 86 |
= (_Unwind_Ptr) &sc->regs.r0 + i * sizeof (unsigned long) - new_cfa; |
| 87 |
} |
| 88 |
|
| 89 |
/* The interrupted PC goes into the alternate return column |
| 90 |
(DWARF_ALT_FRAME_RETURN_COLUMN, defined as 36 in |
| 91 |
gcc/config/microblaze/microblaze.h), one past the hard registers; |
| 92 |
column 15 above keeps the interrupted r15. */ |
| 93 |
fs->regs.how[__LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__] = REG_SAVED_OFFSET; |
| 94 |
fs->regs.reg[__LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__].loc.offset |
| 95 |
= (_Unwind_Ptr) &sc->regs.pc - new_cfa; |
| 96 |
fs->retaddr_column = __LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__; |
| 97 |
fs->signal_frame = 1; |
| 98 |
|
| 99 |
return _URC_NO_REASON; |
| 100 |
} |
| 101 |
|
| 102 |
#endif |