[NDS32] Sync glibc and kernel structure, all use _rt_sigframe.
[official-gcc.git] / libgcc / config / nds32 / linux-unwind.h
blobc8f5983c916cec4d1a86b30c0f26b738f0d4f5fc
1 /* DWARF2 EH unwinding support for NDS32 Linux signal frame.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3 Contributed by Andes Technology Corporation.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
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.
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/>. */
26 #ifndef inhibit_libc
28 /* Do code reading to identify a signal frame, and set the frame
29 state data appropriately. See unwind-dw2.c for the structs.
30 The corresponding bits in the Linux kernel are in
31 arch/nds32/kernel/signal.c. */
33 #include <signal.h>
34 #include <asm/unistd.h>
35 #include <sys/ucontext.h>
37 /* Exactly the same layout as the kernel structures, unique names. */
39 /* arch/nds32/kernel/signal.c */
40 struct _rt_sigframe {
41 siginfo_t info;
42 struct ucontext_t uc;
45 #define SIGRETURN 0xeb0e0a64
46 #define RT_SIGRETURN 0xab150a64
48 #define MD_FALLBACK_FRAME_STATE_FOR nds32_fallback_frame_state
50 /* This function is supposed to be invoked by uw_frame_state_for()
51 when there is no unwind data available.
53 Generally, given the _Unwind_Context CONTEXT for a stack frame,
54 we need to look up its caller and decode information into FS.
55 However, if the exception handling happens within a signal handler,
56 the return address of signal handler is a special module, which
57 contains signal return syscall and has no FDE in the .eh_frame section.
58 We need to implement MD_FALLBACK_FRAME_STATE_FOR so that we can
59 unwind through signal frames. */
60 static _Unwind_Reason_Code
61 nds32_fallback_frame_state (struct _Unwind_Context *context,
62 _Unwind_FrameState *fs)
64 u_int32_t *pc = (u_int32_t *) context->ra;
65 struct sigcontext *sc_;
66 _Unwind_Ptr new_cfa;
68 #ifdef __NDS32_EB__
69 #error "Signal handler is not supported for force unwind."
70 #endif
72 if ((_Unwind_Ptr) pc & 3)
73 return _URC_END_OF_STACK;
75 /* Check if we are going through a signal handler.
76 See arch/nds32/kernel/signal.c implementation.
77 SWI_SYS_SIGRETURN -> (0xeb0e0a64)
78 SWI_SYS_RT_SIGRETURN -> (0xab150a64)
79 FIXME: Currently we only handle little endian (EL) case. */
80 if (pc[0] == SIGRETURN || pc[0] == RT_SIGRETURN)
82 /* Using '_sigfame' memory address to locate kernal's sigcontext.
83 The sigcontext structures in arch/nds32/include/asm/sigcontext.h. */
84 struct _rt_sigframe *rt_;
85 rt_ = context->cfa;
86 sc_ = &rt_->sig.uc.uc_mcontext;
88 else
89 return _URC_END_OF_STACK;
91 /* Update cfa from sigcontext. */
92 new_cfa = (_Unwind_Ptr) sc_;
93 fs->regs.cfa_how = CFA_REG_OFFSET;
94 fs->regs.cfa_reg = STACK_POINTER_REGNUM;
95 fs->regs.cfa_offset = new_cfa - (_Unwind_Ptr) context->cfa;
97 #define NDS32_PUT_FS_REG(NUM, NAME) \
98 (fs->regs.reg[NUM].how = REG_SAVED_OFFSET, \
99 fs->regs.reg[NUM].loc.offset = (_Unwind_Ptr) &(sc_->NAME) - new_cfa)
101 /* Restore all registers value. */
102 NDS32_PUT_FS_REG (0, nds32_r0);
103 NDS32_PUT_FS_REG (1, nds32_r1);
104 NDS32_PUT_FS_REG (2, nds32_r2);
105 NDS32_PUT_FS_REG (3, nds32_r3);
106 NDS32_PUT_FS_REG (4, nds32_r4);
107 NDS32_PUT_FS_REG (5, nds32_r5);
108 NDS32_PUT_FS_REG (6, nds32_r6);
109 NDS32_PUT_FS_REG (7, nds32_r7);
110 NDS32_PUT_FS_REG (8, nds32_r8);
111 NDS32_PUT_FS_REG (9, nds32_r9);
112 NDS32_PUT_FS_REG (10, nds32_r10);
113 NDS32_PUT_FS_REG (11, nds32_r11);
114 NDS32_PUT_FS_REG (12, nds32_r12);
115 NDS32_PUT_FS_REG (13, nds32_r13);
116 NDS32_PUT_FS_REG (14, nds32_r14);
117 NDS32_PUT_FS_REG (15, nds32_r15);
118 NDS32_PUT_FS_REG (16, nds32_r16);
119 NDS32_PUT_FS_REG (17, nds32_r17);
120 NDS32_PUT_FS_REG (18, nds32_r18);
121 NDS32_PUT_FS_REG (19, nds32_r19);
122 NDS32_PUT_FS_REG (20, nds32_r20);
123 NDS32_PUT_FS_REG (21, nds32_r21);
124 NDS32_PUT_FS_REG (22, nds32_r22);
125 NDS32_PUT_FS_REG (23, nds32_r23);
126 NDS32_PUT_FS_REG (24, nds32_r24);
127 NDS32_PUT_FS_REG (25, nds32_r25);
129 NDS32_PUT_FS_REG (28, nds32_fp);
130 NDS32_PUT_FS_REG (29, nds32_gp);
131 NDS32_PUT_FS_REG (30, nds32_lp);
132 NDS32_PUT_FS_REG (31, nds32_sp);
134 /* Restore PC, point to trigger signal instruction. */
135 NDS32_PUT_FS_REG (32, nds32_ipc);
137 #undef NDS32_PUT_FS_REG
139 /* The retaddr is PC, use PC to find FDE. */
140 fs->retaddr_column = 32;
141 fs->signal_frame = 1;
143 return _URC_NO_REASON;
146 #endif