1 /* DWARF2 EH unwinding support for C-SKY Linux.
2    Copyright (C) 2018-2019 Free Software Foundation, Inc.
3    Contributed by C-SKY Microsystems and Mentor Graphics.
4 
5    This file is part of GCC.
6 
7    GCC is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11 
12    GCC is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    Under Section 7 of GPL version 3, you are granted additional
18    permissions described in the GCC Runtime Library Exception, version
19    3.1, as published by the Free Software Foundation.
20 
21    You should have received a copy of the GNU General Public License and
22    a copy of the GCC Runtime Library Exception along with this program;
23    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24    <http://www.gnu.org/licenses/>.  */
25 
26 #ifndef inhibit_libc
27 
28 /* Do code reading to identify a signal frame, and set the frame state data
29    appropriately.  See unwind-dw2.c for the structs.  */
30 
31 #include <signal.h>
32 #include <asm/unistd.h>
33 
34 #define TRAP0_V1 0x0008
35 #define MOVI_R1_119_V1 (0x6000 + (119 << 4) + 1)
36 #define MOVI_R1_127_V1 (0x6000 + (127 << 4) + 1)
37 #define ADDI_R1_32_V1 (0x2000 + (31 << 4) + 1)
38 #define ADDI_R1_14_V1 (0x2000 + (13 << 4) + 1)
39 #define ADDI_R1_12_V1 (0x2000 + (11 << 4) + 1)
40 
41 #define TRAP0_V2_PART0 0xc000
42 #define TRAP0_V2_PART1 0x2020
43 #define MOVI_R7_119_V2_PART0 0xea07
44 #define MOVI_R7_119_V2_PART1 119
45 #define MOVI_R7_173_V2_PART0 0xea07
46 #define MOVI_R7_173_V2_PART1 173
47 #define MOVI_R7_139_V2_PART0 0xea07
48 #define MOVI_R7_139_V2_PART1 139
49 
50 #define sc_pt_regs(x) (sc->sc_pt_regs.x)
51 #define sc_pt_regs_lr (sc->sc_pt_regs.lr)
52 #define sc_pt_regs_tls(x) sc_pt_regs(x)
53 
54 #define MD_FALLBACK_FRAME_STATE_FOR csky_fallback_frame_state
55 
56 static _Unwind_Reason_Code
csky_fallback_frame_state(struct _Unwind_Context * context,_Unwind_FrameState * fs)57 csky_fallback_frame_state (struct _Unwind_Context *context,
58 			   _Unwind_FrameState *fs)
59 {
60   u_int16_t *pc = (u_int16_t *) context->ra;
61   struct sigcontext *sc;
62   _Unwind_Ptr new_cfa;
63   int i;
64 
65   /* movi r7, __NR_rt_sigreturn; trap 0  */
66   if ((*(pc + 0) == MOVI_R7_139_V2_PART0)
67       && (*(pc + 1) == MOVI_R7_139_V2_PART1) && (*(pc + 2) == TRAP0_V2_PART0)
68       && (*(pc + 3) == TRAP0_V2_PART1))
69     {
70       struct rt_sigframe
71       {
72 	int sig;
73 	struct siginfo *pinfo;
74 	void *puc;
75 	siginfo_t info;
76 	ucontext_t uc;
77       } *_rt = context->cfa;
78       sc = &(_rt->uc.uc_mcontext);
79     }
80   else
81     return _URC_END_OF_STACK;
82 
83   new_cfa = (_Unwind_Ptr) sc_pt_regs (usp);
84   fs->regs.cfa_how = CFA_REG_OFFSET;
85   fs->regs.cfa_reg = STACK_POINTER_REGNUM;
86   fs->regs.cfa_offset = new_cfa - (_Unwind_Ptr) context->cfa;
87 
88   fs->regs.reg[0].how = REG_SAVED_OFFSET;
89   fs->regs.reg[0].loc.offset = (_Unwind_Ptr) & sc_pt_regs (a0) - new_cfa;
90 
91   fs->regs.reg[1].how = REG_SAVED_OFFSET;
92   fs->regs.reg[1].loc.offset = (_Unwind_Ptr) & sc_pt_regs (a1) - new_cfa;
93 
94   fs->regs.reg[2].how = REG_SAVED_OFFSET;
95   fs->regs.reg[2].loc.offset = (_Unwind_Ptr) & sc_pt_regs (a2) - new_cfa;
96 
97   fs->regs.reg[3].how = REG_SAVED_OFFSET;
98   fs->regs.reg[3].loc.offset = (_Unwind_Ptr) & sc_pt_regs (a3) - new_cfa;
99 
100   for (i = 4; i < 14; i++)
101     {
102       fs->regs.reg[i].how = REG_SAVED_OFFSET;
103       fs->regs.reg[i].loc.offset =
104 	(_Unwind_Ptr) & sc_pt_regs (regs[i - 4]) - new_cfa;
105     }
106 
107   for (i = 16; i < 31; i++)
108     {
109       fs->regs.reg[i].how = REG_SAVED_OFFSET;
110       fs->regs.reg[i].loc.offset =
111 	(_Unwind_Ptr) & sc_pt_regs (exregs[i - 16]) - new_cfa;
112     }
113 
114   fs->regs.reg[31].loc.offset =
115     (_Unwind_Ptr) & sc_pt_regs_tls (tls) - new_cfa;
116   /* FIXME : hi lo ? */
117   fs->regs.reg[15].how = REG_SAVED_OFFSET;
118   fs->regs.reg[15].loc.offset = (_Unwind_Ptr) & sc_pt_regs_lr - new_cfa;
119 
120   fs->regs.reg[32].how = REG_SAVED_OFFSET;
121   fs->regs.reg[32].loc.offset = (_Unwind_Ptr) & sc_pt_regs (pc) - new_cfa;
122   fs->retaddr_column = 32;
123   fs->signal_frame = 1;
124 
125   return _URC_NO_REASON;
126 }
127 
128 #endif
129