1 /* Copyright (C) 2016-2018 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 #ifndef inhibit_libc
23 
24 #include <signal.h>
25 #include <stdint.h>
26 #include <sys/ucontext.h>
27 
28 #define LI_A7_8B 0x08b00893
29 #define ECALL    0x00000073
30 
31 #define MD_FALLBACK_FRAME_STATE_FOR riscv_fallback_frame_state
32 
33 static _Unwind_Reason_Code
riscv_fallback_frame_state(struct _Unwind_Context * context,_Unwind_FrameState * fs)34 riscv_fallback_frame_state (struct _Unwind_Context *context,
35 			    _Unwind_FrameState * fs)
36 {
37   /* The kernel creates an rt_sigframe on the stack immediately prior
38      to delivering a signal.
39 
40      This structure must have the same shape as the linux kernel
41      equivalent.  */
42   struct rt_sigframe
43   {
44     siginfo_t info;
45     ucontext_t uc;
46   };
47 
48   struct rt_sigframe *rt_;
49   _Unwind_Ptr new_cfa;
50   uint16_t *pc = context->ra;
51   struct sigcontext *sc;
52   int i;
53 
54   /* A signal frame will have a return address pointing to
55      __default_sa_restorer. This code is hardwired as:
56 
57      0x08b00893		li	a7,0x8b
58      0x00000073		ecall
59 
60      Note, the PC might only have 2-byte alignment.
61    */
62   if (pc[0] != (uint16_t)LI_A7_8B || pc[1] != (uint16_t)(LI_A7_8B >> 16)
63       || pc[2] != (uint16_t)ECALL || pc[3] != (uint16_t)(ECALL >> 16))
64     return _URC_END_OF_STACK;
65 
66   rt_ = context->cfa;
67   sc = &rt_->uc.uc_mcontext;
68 
69   new_cfa = (_Unwind_Ptr) sc;
70   fs->regs.cfa_how = CFA_REG_OFFSET;
71   fs->regs.cfa_reg = __LIBGCC_STACK_POINTER_REGNUM__;
72   fs->regs.cfa_offset = new_cfa - (_Unwind_Ptr) context->cfa;
73 
74   for (i = 0; i < 32; i++)
75     {
76       fs->regs.reg[i].how = REG_SAVED_OFFSET;
77       fs->regs.reg[i].loc.offset = (_Unwind_Ptr) &sc->gregs[i] - new_cfa;
78     }
79 
80   fs->signal_frame = 1;
81   fs->retaddr_column = __LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__;
82   fs->regs.reg[fs->retaddr_column].how = REG_SAVED_VAL_OFFSET;
83   fs->regs.reg[fs->retaddr_column].loc.offset =
84     (_Unwind_Ptr) sc->gregs[0] - new_cfa;
85 
86   return _URC_NO_REASON;
87 }
88 
89 #endif
90