1627f7eb2Smrg /* DWARF2 EH unwinding support for C-SKY Linux.
2*4c3eb207Smrg Copyright (C) 2018-2020 Free Software Foundation, Inc.
3627f7eb2Smrg Contributed by C-SKY Microsystems and Mentor Graphics.
4627f7eb2Smrg
5627f7eb2Smrg This file is part of GCC.
6627f7eb2Smrg
7627f7eb2Smrg GCC is free software; you can redistribute it and/or modify
8627f7eb2Smrg it under the terms of the GNU General Public License as published by
9627f7eb2Smrg the Free Software Foundation; either version 3, or (at your option)
10627f7eb2Smrg any later version.
11627f7eb2Smrg
12627f7eb2Smrg GCC is distributed in the hope that it will be useful,
13627f7eb2Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
14627f7eb2Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15627f7eb2Smrg GNU General Public License for more details.
16627f7eb2Smrg
17627f7eb2Smrg Under Section 7 of GPL version 3, you are granted additional
18627f7eb2Smrg permissions described in the GCC Runtime Library Exception, version
19627f7eb2Smrg 3.1, as published by the Free Software Foundation.
20627f7eb2Smrg
21627f7eb2Smrg You should have received a copy of the GNU General Public License and
22627f7eb2Smrg a copy of the GCC Runtime Library Exception along with this program;
23627f7eb2Smrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24627f7eb2Smrg <http://www.gnu.org/licenses/>. */
25627f7eb2Smrg
26627f7eb2Smrg #ifndef inhibit_libc
27627f7eb2Smrg
28627f7eb2Smrg /* Do code reading to identify a signal frame, and set the frame state data
29627f7eb2Smrg appropriately. See unwind-dw2.c for the structs. */
30627f7eb2Smrg
31627f7eb2Smrg #include <signal.h>
32627f7eb2Smrg #include <asm/unistd.h>
33627f7eb2Smrg
34627f7eb2Smrg #define TRAP0_V1 0x0008
35627f7eb2Smrg #define MOVI_R1_119_V1 (0x6000 + (119 << 4) + 1)
36627f7eb2Smrg #define MOVI_R1_127_V1 (0x6000 + (127 << 4) + 1)
37627f7eb2Smrg #define ADDI_R1_32_V1 (0x2000 + (31 << 4) + 1)
38627f7eb2Smrg #define ADDI_R1_14_V1 (0x2000 + (13 << 4) + 1)
39627f7eb2Smrg #define ADDI_R1_12_V1 (0x2000 + (11 << 4) + 1)
40627f7eb2Smrg
41627f7eb2Smrg #define TRAP0_V2_PART0 0xc000
42627f7eb2Smrg #define TRAP0_V2_PART1 0x2020
43627f7eb2Smrg #define MOVI_R7_119_V2_PART0 0xea07
44627f7eb2Smrg #define MOVI_R7_119_V2_PART1 119
45627f7eb2Smrg #define MOVI_R7_173_V2_PART0 0xea07
46627f7eb2Smrg #define MOVI_R7_173_V2_PART1 173
47627f7eb2Smrg #define MOVI_R7_139_V2_PART0 0xea07
48627f7eb2Smrg #define MOVI_R7_139_V2_PART1 139
49627f7eb2Smrg
50627f7eb2Smrg #define sc_pt_regs(x) (sc->sc_pt_regs.x)
51627f7eb2Smrg #define sc_pt_regs_lr (sc->sc_pt_regs.lr)
52627f7eb2Smrg #define sc_pt_regs_tls(x) sc_pt_regs(x)
53627f7eb2Smrg
54627f7eb2Smrg #define MD_FALLBACK_FRAME_STATE_FOR csky_fallback_frame_state
55627f7eb2Smrg
56627f7eb2Smrg static _Unwind_Reason_Code
csky_fallback_frame_state(struct _Unwind_Context * context,_Unwind_FrameState * fs)57627f7eb2Smrg csky_fallback_frame_state (struct _Unwind_Context *context,
58627f7eb2Smrg _Unwind_FrameState *fs)
59627f7eb2Smrg {
60627f7eb2Smrg u_int16_t *pc = (u_int16_t *) context->ra;
61627f7eb2Smrg struct sigcontext *sc;
62627f7eb2Smrg _Unwind_Ptr new_cfa;
63627f7eb2Smrg int i;
64627f7eb2Smrg
65627f7eb2Smrg /* movi r7, __NR_rt_sigreturn; trap 0 */
66627f7eb2Smrg if ((*(pc + 0) == MOVI_R7_139_V2_PART0)
67627f7eb2Smrg && (*(pc + 1) == MOVI_R7_139_V2_PART1) && (*(pc + 2) == TRAP0_V2_PART0)
68627f7eb2Smrg && (*(pc + 3) == TRAP0_V2_PART1))
69627f7eb2Smrg {
70627f7eb2Smrg struct rt_sigframe
71627f7eb2Smrg {
72627f7eb2Smrg int sig;
73627f7eb2Smrg struct siginfo *pinfo;
74627f7eb2Smrg void *puc;
75627f7eb2Smrg siginfo_t info;
76627f7eb2Smrg ucontext_t uc;
77627f7eb2Smrg } *_rt = context->cfa;
78627f7eb2Smrg sc = &(_rt->uc.uc_mcontext);
79627f7eb2Smrg }
80627f7eb2Smrg else
81627f7eb2Smrg return _URC_END_OF_STACK;
82627f7eb2Smrg
83627f7eb2Smrg new_cfa = (_Unwind_Ptr) sc_pt_regs (usp);
84627f7eb2Smrg fs->regs.cfa_how = CFA_REG_OFFSET;
85627f7eb2Smrg fs->regs.cfa_reg = STACK_POINTER_REGNUM;
86627f7eb2Smrg fs->regs.cfa_offset = new_cfa - (_Unwind_Ptr) context->cfa;
87627f7eb2Smrg
88627f7eb2Smrg fs->regs.reg[0].how = REG_SAVED_OFFSET;
89627f7eb2Smrg fs->regs.reg[0].loc.offset = (_Unwind_Ptr) & sc_pt_regs (a0) - new_cfa;
90627f7eb2Smrg
91627f7eb2Smrg fs->regs.reg[1].how = REG_SAVED_OFFSET;
92627f7eb2Smrg fs->regs.reg[1].loc.offset = (_Unwind_Ptr) & sc_pt_regs (a1) - new_cfa;
93627f7eb2Smrg
94627f7eb2Smrg fs->regs.reg[2].how = REG_SAVED_OFFSET;
95627f7eb2Smrg fs->regs.reg[2].loc.offset = (_Unwind_Ptr) & sc_pt_regs (a2) - new_cfa;
96627f7eb2Smrg
97627f7eb2Smrg fs->regs.reg[3].how = REG_SAVED_OFFSET;
98627f7eb2Smrg fs->regs.reg[3].loc.offset = (_Unwind_Ptr) & sc_pt_regs (a3) - new_cfa;
99627f7eb2Smrg
100627f7eb2Smrg for (i = 4; i < 14; i++)
101627f7eb2Smrg {
102627f7eb2Smrg fs->regs.reg[i].how = REG_SAVED_OFFSET;
103627f7eb2Smrg fs->regs.reg[i].loc.offset =
104627f7eb2Smrg (_Unwind_Ptr) & sc_pt_regs (regs[i - 4]) - new_cfa;
105627f7eb2Smrg }
106627f7eb2Smrg
107627f7eb2Smrg for (i = 16; i < 31; i++)
108627f7eb2Smrg {
109627f7eb2Smrg fs->regs.reg[i].how = REG_SAVED_OFFSET;
110627f7eb2Smrg fs->regs.reg[i].loc.offset =
111627f7eb2Smrg (_Unwind_Ptr) & sc_pt_regs (exregs[i - 16]) - new_cfa;
112627f7eb2Smrg }
113627f7eb2Smrg
114627f7eb2Smrg fs->regs.reg[31].loc.offset =
115627f7eb2Smrg (_Unwind_Ptr) & sc_pt_regs_tls (tls) - new_cfa;
116627f7eb2Smrg /* FIXME : hi lo ? */
117627f7eb2Smrg fs->regs.reg[15].how = REG_SAVED_OFFSET;
118627f7eb2Smrg fs->regs.reg[15].loc.offset = (_Unwind_Ptr) & sc_pt_regs_lr - new_cfa;
119627f7eb2Smrg
120627f7eb2Smrg fs->regs.reg[32].how = REG_SAVED_OFFSET;
121627f7eb2Smrg fs->regs.reg[32].loc.offset = (_Unwind_Ptr) & sc_pt_regs (pc) - new_cfa;
122627f7eb2Smrg fs->retaddr_column = 32;
123627f7eb2Smrg fs->signal_frame = 1;
124627f7eb2Smrg
125627f7eb2Smrg return _URC_NO_REASON;
126627f7eb2Smrg }
127627f7eb2Smrg
128627f7eb2Smrg #endif
129