1 /* DWARF2 EH unwinding support for GNU Hurd: x86.
2    Copyright (C) 2020 Free Software Foundation, Inc.
3    Contributed by Samuel Thibault <samuel.thibault@gnu.org>
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 /* Do code reading to identify a signal frame, and set the frame
27    state data appropriately.  See unwind-dw2.c for the structs. */
28 
29 #ifndef inhibit_libc
30 
31 #include <signal.h>
32 
33 #define MD_FALLBACK_FRAME_STATE_FOR x86_gnu_fallback_frame_state
34 
35 static _Unwind_Reason_Code
x86_gnu_fallback_frame_state(struct _Unwind_Context * context,_Unwind_FrameState * fs)36 x86_gnu_fallback_frame_state
37 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
38 {
39   struct handler_args {
40     int signo;
41     union
42       {
43 	struct
44 	  {
45 	    long int sigcode;
46 	    struct sigcontext *scp;
47 	  } legacy;
48 	struct
49 	  {
50 	    siginfo_t *siginfop;
51 	    ucontext_t *uctxp;
52 	  } posix;
53       };
54   } *handler_args;
55   long int sigcode;
56   unsigned long usp;
57 
58 /*
59  * i386 sigtramp frame we are looking for follows.
60  * (see glibc/sysdeps/mach/hurd/i386/trampoline.c assembly)
61  *
62  * rpc_wait_trampoline:
63  *   0:	b8 e7 ff ff ff       	mov    $-25,%eax       mach_msg_trap
64  *   5:	9a 00 00 00 00 07 00 	lcall  $7,$0
65  *  12:	89 01                	movl   %eax, (%ecx)
66  *  14:	89 dc                	movl   %ebx, %esp      switch to signal stack
67  *
68  * trampoline:
69  *  16:	ff d2                	call   *%edx           call the handler function
70  * RA HERE
71  *  18:	83 c4 0c             	addl   $12, %esp       pop its args
72  *  21:	c3                   	ret                    return to sigreturn
73  *
74  * firewall:
75  *  22:	f4                   	hlt
76  */
77 
78   if (!(   *(unsigned int   *)(context->ra     ) == 0xc30cc483
79         && *(unsigned char  *)(context->ra +  4) ==       0xf4
80 
81         && *(unsigned int   *)(context->ra -  4) == 0xd2ffdc89
82         && *(unsigned int   *)(context->ra -  8) == 0x01890007
83         && *(unsigned int   *)(context->ra - 12) == 0x00000000
84         && *(unsigned int   *)(context->ra - 16) == 0x9affffff
85         && *(unsigned short *)(context->ra - 18) ==     0xe7b8))
86     return _URC_END_OF_STACK;
87 
88   handler_args = context->cfa;
89   sigcode = handler_args->legacy.sigcode;
90   if (sigcode >= -16 && sigcode < 4096)
91     {
92       /* This cannot be a SIGINFO pointer, assume legacy.  */
93       struct sigcontext *scp = handler_args->legacy.scp;
94       usp = scp->sc_uesp;
95 
96       fs->regs.reg[0].loc.offset = (unsigned long)&scp->sc_eax - usp;
97       fs->regs.reg[1].loc.offset = (unsigned long)&scp->sc_ecx - usp;
98       fs->regs.reg[2].loc.offset = (unsigned long)&scp->sc_edx - usp;
99       fs->regs.reg[3].loc.offset = (unsigned long)&scp->sc_ebx - usp;
100       fs->regs.reg[5].loc.offset = (unsigned long)&scp->sc_ebp - usp;
101       fs->regs.reg[6].loc.offset = (unsigned long)&scp->sc_esi - usp;
102       fs->regs.reg[7].loc.offset = (unsigned long)&scp->sc_edi - usp;
103       fs->regs.reg[8].loc.offset = (unsigned long)&scp->sc_eip - usp;
104     }
105   else
106     {
107       /* This is not a valid sigcode, assume SIGINFO.  */
108       ucontext_t *uctxp = handler_args->posix.uctxp;
109       gregset_t *gregset = &uctxp->uc_mcontext.gregs;
110       usp = (*gregset)[REG_UESP];
111 
112       fs->regs.reg[0].loc.offset = (unsigned long)&(*gregset)[REG_EAX] - usp;
113       fs->regs.reg[1].loc.offset = (unsigned long)&(*gregset)[REG_ECX] - usp;
114       fs->regs.reg[2].loc.offset = (unsigned long)&(*gregset)[REG_EDX] - usp;
115       fs->regs.reg[3].loc.offset = (unsigned long)&(*gregset)[REG_EBX] - usp;
116       fs->regs.reg[5].loc.offset = (unsigned long)&(*gregset)[REG_EBP] - usp;
117       fs->regs.reg[6].loc.offset = (unsigned long)&(*gregset)[REG_ESI] - usp;
118       fs->regs.reg[7].loc.offset = (unsigned long)&(*gregset)[REG_EDI] - usp;
119       fs->regs.reg[8].loc.offset = (unsigned long)&(*gregset)[REG_EIP] - usp;
120     }
121 
122   fs->regs.cfa_how = CFA_REG_OFFSET;
123   fs->regs.cfa_reg = 4;
124   fs->regs.cfa_offset = usp - (unsigned long) context->cfa;
125 
126   fs->regs.reg[0].how = REG_SAVED_OFFSET;
127   fs->regs.reg[1].how = REG_SAVED_OFFSET;
128   fs->regs.reg[2].how = REG_SAVED_OFFSET;
129   fs->regs.reg[3].how = REG_SAVED_OFFSET;
130   fs->regs.reg[5].how = REG_SAVED_OFFSET;
131   fs->regs.reg[6].how = REG_SAVED_OFFSET;
132   fs->regs.reg[7].how = REG_SAVED_OFFSET;
133   fs->regs.reg[8].how = REG_SAVED_OFFSET;
134 
135   fs->retaddr_column = 8;
136   fs->signal_frame = 1;
137 
138   return _URC_NO_REASON;
139 }
140 
141 #endif /* ifndef inhibit_libc */
142