1 /* DWARF2 EH unwinding support for ARC Linux.
2    Copyright (C) 2017-2021 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 it
7    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, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14    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
21    and a copy of the GCC Runtime Library Exception along with this
22    program; see the files COPYING3 and COPYING.RUNTIME respectively.
23    If not, see <http://www.gnu.org/licenses/>.  */
24 
25 /* This order is defined by a structure in the kernel, in file
26    arch/arc/kernel/signal.c.  */
27 
28 #define REGISTER_IN_STACK(REG_NAME, ID) \
29   REG_NAME,
30 enum registers_stack_order {
31   REGISTER_STACK_ORDER_START = -1,
32   #include "config/arc/linux-unwind-reg.def"
33   REGISTER_STACK_ORDER_SIZE,
34 };
35 
36 struct register_position {
37   int reg_id;
38   int offset_in_stack;
39 };
40 #undef REGISTER_IN_STACK
41 
42 #define REGISTER_SIZE_IN_WORDS 4
43 #define REGISTER_IN_STACK(REG_NAME, ID) (int) ID,
44 int
45 register_id_for_index[REGISTER_STACK_ORDER_SIZE] = {
46   #include "config/arc/linux-unwind-reg.def"
47 };
48 #undef REGISTER_IN_STACK
49 
50 #ifndef inhibit_libc
51 /* Do code reading to identify a signal frame, and set the frame
52    state data appropriately.  See unwind-dw2.c for the structs.  */
53 
54 #include <signal.h>
55 #include <asm/unistd.h>
56 
57 /*
58 00010edc <__default_rt_sa_restorer>:
59    10edc:	208a 12c2		mov     r8,139
60    10ee0:	781e			trap_s  0
61    10ee2:	7ee0			j_s     [blink]
62 */
63 
64 #if __BIG_ENDIAN__
65 #define MOV_R8_139	  0x8a20c212
66 #define TRAP_S_J_S_BLINK  0x1e78e07e
67 #define SWI		  0x6f223f00
68 #elif __LITTLE_ENDIAN__
69 #define MOV_R8_139	  0x12c2208a
70 #define TRAP_S_J_S_BLINK  0x7ee0781e
71 #define SWI		  0x003f226f
72 #endif
73 
74 #define MD_FALLBACK_FRAME_STATE_FOR arc_fallback_frame_state
75 
76 static __attribute__((noinline)) _Unwind_Reason_Code
arc_fallback_frame_state(struct _Unwind_Context * context,_Unwind_FrameState * fs)77 arc_fallback_frame_state (struct _Unwind_Context *context,
78 			   _Unwind_FrameState *fs)
79 {
80   struct rt_sigframe {
81     siginfo_t info;
82     ucontext_t uc;
83     unsigned int sigret_magic;
84   };
85 
86   struct rt_sigframe *rt_;
87   u_int32_t *pc = (u_int32_t *) context->ra;
88   struct sigcontext *sc;
89   _Unwind_Ptr new_cfa;
90   int i;
91 
92 #ifdef __ARC700__
93   if (pc[1] != SWI)
94     return _URC_END_OF_STACK;
95 #else
96   if (pc[1] != TRAP_S_J_S_BLINK)
97     return _URC_END_OF_STACK;
98 #endif
99 
100   if (pc[0] == MOV_R8_139)
101     {
102       rt_ = context->cfa;
103       sc = &rt_->uc.uc_mcontext;
104     }
105   else
106     return _URC_END_OF_STACK;
107 
108   new_cfa = (_Unwind_Ptr) sc;
109   fs->regs.cfa_how = CFA_REG_OFFSET;
110   fs->regs.cfa_reg = __LIBGCC_STACK_POINTER_REGNUM__;
111   fs->regs.cfa_offset = new_cfa - (_Unwind_Ptr) context->cfa;
112 
113   unsigned long *regs = &sc->regs.scratch.bta;
114   for (i = 0; i < REGISTER_STACK_ORDER_SIZE; i++)
115     {
116       if (register_id_for_index[i] == -1)
117 	continue;
118       fs->regs.reg[register_id_for_index[i]].how = REG_SAVED_OFFSET;
119       fs->regs.reg[register_id_for_index[i]].loc.offset
120 	= ((_Unwind_Ptr) &(regs[i])) - new_cfa;
121     }
122 
123   fs->regs.reg[31].how = REG_SAVED_VAL_OFFSET;
124   fs->regs.reg[31].loc.offset = ((_Unwind_Ptr) (regs[ret])) - new_cfa;
125 
126   fs->retaddr_column = 31;
127 
128   return _URC_NO_REASON;
129 }
130 #endif
131 
132 #define MD_FROB_UPDATE_CONTEXT arc_frob_update_context
133 /* Save fp register for unwinding to work.  */
134 
135 static void
arc_frob_update_context(struct _Unwind_Context * context,_Unwind_FrameState * fs)136 arc_frob_update_context (struct _Unwind_Context *context,
137 			 _Unwind_FrameState *fs)
138 {
139   _Unwind_Word fp_val;
140   asm ("mov %0,fp" : "=r" (fp_val));
141 
142   switch (fs->regs.reg[27].how)
143     {
144     case REG_UNSAVED:
145     case REG_UNDEFINED:
146       if (context->reg[27] == NULL)
147 	_Unwind_SetGRValue (context, 27, fp_val);
148       break;
149 
150     default:
151       break;
152     }
153 }
154