1*fdc4107cSJohn Marino /* DWARF2 EH unwinding support for DragonFly BSD: AMD x86-64 and x86.
2*fdc4107cSJohn Marino    Copyright (C) 2010 John Marino <draco@marino.st> */
3*fdc4107cSJohn Marino 
4*fdc4107cSJohn Marino /* Do code reading to identify a signal frame, and set the frame
5*fdc4107cSJohn Marino    state data appropriately.  See unwind-dw2.c for the structs. */
6*fdc4107cSJohn Marino 
7*fdc4107cSJohn Marino #include <sys/types.h>
8*fdc4107cSJohn Marino #include <sys/sysctl.h>
9*fdc4107cSJohn Marino #include <signal.h>
10*fdc4107cSJohn Marino #include <sys/ucontext.h>
11*fdc4107cSJohn Marino #include <machine/sigframe.h>
12*fdc4107cSJohn Marino 
13*fdc4107cSJohn Marino 
14*fdc4107cSJohn Marino #define REG_NAME(reg)	sf_uc.uc_mcontext.mc_## reg
15*fdc4107cSJohn Marino 
16*fdc4107cSJohn Marino #ifdef __x86_64__
17*fdc4107cSJohn Marino #define MD_FALLBACK_FRAME_STATE_FOR x86_64_dragonfly_fallback_frame_state
18*fdc4107cSJohn Marino 
19*fdc4107cSJohn Marino 
20*fdc4107cSJohn Marino static void
x86_64_sigtramp_range(unsigned char ** start,unsigned char ** end)21*fdc4107cSJohn Marino x86_64_sigtramp_range (unsigned char **start, unsigned char **end)
22*fdc4107cSJohn Marino {
23*fdc4107cSJohn Marino   unsigned long ps_strings;
24*fdc4107cSJohn Marino   int mib[2];
25*fdc4107cSJohn Marino   size_t len;
26*fdc4107cSJohn Marino 
27*fdc4107cSJohn Marino   mib[0] = CTL_KERN;
28*fdc4107cSJohn Marino   mib[1] = KERN_PS_STRINGS;
29*fdc4107cSJohn Marino   len = sizeof (ps_strings);
30*fdc4107cSJohn Marino   sysctl (mib, 2, &ps_strings, &len, NULL, 0);
31*fdc4107cSJohn Marino 
32*fdc4107cSJohn Marino   *start = (unsigned char *)ps_strings - 32;
33*fdc4107cSJohn Marino   *end   = (unsigned char *)ps_strings;
34*fdc4107cSJohn Marino }
35*fdc4107cSJohn Marino 
36*fdc4107cSJohn Marino 
37*fdc4107cSJohn Marino static _Unwind_Reason_Code
x86_64_dragonfly_fallback_frame_state(struct _Unwind_Context * context,_Unwind_FrameState * fs)38*fdc4107cSJohn Marino x86_64_dragonfly_fallback_frame_state
39*fdc4107cSJohn Marino (struct _Unwind_Context *context, _Unwind_FrameState *fs)
40*fdc4107cSJohn Marino {
41*fdc4107cSJohn Marino   unsigned char *pc = context->ra;
42*fdc4107cSJohn Marino   unsigned char *sigtramp_start, *sigtramp_end;
43*fdc4107cSJohn Marino   struct sigframe *sf;
44*fdc4107cSJohn Marino   long new_cfa;
45*fdc4107cSJohn Marino 
46*fdc4107cSJohn Marino   x86_64_sigtramp_range(&sigtramp_start, &sigtramp_end);
47*fdc4107cSJohn Marino   if (pc >= sigtramp_end || pc < sigtramp_start)
48*fdc4107cSJohn Marino     return _URC_END_OF_STACK;
49*fdc4107cSJohn Marino 
50*fdc4107cSJohn Marino   sf = (struct sigframe *) context->cfa;
51*fdc4107cSJohn Marino   new_cfa = sf->REG_NAME(rsp);
52*fdc4107cSJohn Marino   fs->regs.cfa_how = CFA_REG_OFFSET;
53*fdc4107cSJohn Marino   /* Register 7 is rsp  */
54*fdc4107cSJohn Marino   fs->regs.cfa_reg = 7;
55*fdc4107cSJohn Marino   fs->regs.cfa_offset = new_cfa - (long) context->cfa;
56*fdc4107cSJohn Marino 
57*fdc4107cSJohn Marino   /* The SVR4 register numbering macros aren't usable in libgcc.  */
58*fdc4107cSJohn Marino   fs->regs.reg[0].how = REG_SAVED_OFFSET;
59*fdc4107cSJohn Marino   fs->regs.reg[0].loc.offset = (long)&sf->REG_NAME(rax) - new_cfa;
60*fdc4107cSJohn Marino   fs->regs.reg[1].how = REG_SAVED_OFFSET;
61*fdc4107cSJohn Marino   fs->regs.reg[1].loc.offset = (long)&sf->REG_NAME(rdx) - new_cfa;
62*fdc4107cSJohn Marino   fs->regs.reg[2].how = REG_SAVED_OFFSET;
63*fdc4107cSJohn Marino   fs->regs.reg[2].loc.offset = (long)&sf->REG_NAME(rcx) - new_cfa;
64*fdc4107cSJohn Marino   fs->regs.reg[3].how = REG_SAVED_OFFSET;
65*fdc4107cSJohn Marino   fs->regs.reg[3].loc.offset = (long)&sf->REG_NAME(rbx) - new_cfa;
66*fdc4107cSJohn Marino   fs->regs.reg[4].how = REG_SAVED_OFFSET;
67*fdc4107cSJohn Marino   fs->regs.reg[4].loc.offset = (long)&sf->REG_NAME(rsi) - new_cfa;
68*fdc4107cSJohn Marino   fs->regs.reg[5].how = REG_SAVED_OFFSET;
69*fdc4107cSJohn Marino   fs->regs.reg[5].loc.offset = (long)&sf->REG_NAME(rdi) - new_cfa;
70*fdc4107cSJohn Marino   fs->regs.reg[6].how = REG_SAVED_OFFSET;
71*fdc4107cSJohn Marino   fs->regs.reg[6].loc.offset = (long)&sf->REG_NAME(rbp) - new_cfa;
72*fdc4107cSJohn Marino   fs->regs.reg[8].how = REG_SAVED_OFFSET;
73*fdc4107cSJohn Marino   fs->regs.reg[8].loc.offset = (long)&sf->REG_NAME(r8) - new_cfa;
74*fdc4107cSJohn Marino   fs->regs.reg[9].how = REG_SAVED_OFFSET;
75*fdc4107cSJohn Marino   fs->regs.reg[9].loc.offset = (long)&sf->REG_NAME(r9) - new_cfa;
76*fdc4107cSJohn Marino   fs->regs.reg[10].how = REG_SAVED_OFFSET;
77*fdc4107cSJohn Marino   fs->regs.reg[10].loc.offset = (long)&sf->REG_NAME(r10) - new_cfa;
78*fdc4107cSJohn Marino   fs->regs.reg[11].how = REG_SAVED_OFFSET;
79*fdc4107cSJohn Marino   fs->regs.reg[11].loc.offset = (long)&sf->REG_NAME(r11) - new_cfa;
80*fdc4107cSJohn Marino   fs->regs.reg[12].how = REG_SAVED_OFFSET;
81*fdc4107cSJohn Marino   fs->regs.reg[12].loc.offset = (long)&sf->REG_NAME(r12) - new_cfa;
82*fdc4107cSJohn Marino   fs->regs.reg[13].how = REG_SAVED_OFFSET;
83*fdc4107cSJohn Marino   fs->regs.reg[13].loc.offset = (long)&sf->REG_NAME(r13) - new_cfa;
84*fdc4107cSJohn Marino   fs->regs.reg[14].how = REG_SAVED_OFFSET;
85*fdc4107cSJohn Marino   fs->regs.reg[14].loc.offset = (long)&sf->REG_NAME(r14) - new_cfa;
86*fdc4107cSJohn Marino   fs->regs.reg[15].how = REG_SAVED_OFFSET;
87*fdc4107cSJohn Marino   fs->regs.reg[15].loc.offset = (long)&sf->REG_NAME(r15) - new_cfa;
88*fdc4107cSJohn Marino   fs->regs.reg[16].how = REG_SAVED_OFFSET;
89*fdc4107cSJohn Marino   fs->regs.reg[16].loc.offset = (long)&sf->REG_NAME(rip) - new_cfa;
90*fdc4107cSJohn Marino   fs->retaddr_column = 16;
91*fdc4107cSJohn Marino   fs->signal_frame = 1;
92*fdc4107cSJohn Marino   return _URC_NO_REASON;
93*fdc4107cSJohn Marino }
94*fdc4107cSJohn Marino 
95*fdc4107cSJohn Marino #else /* Next section is for i386  */
96*fdc4107cSJohn Marino 
97*fdc4107cSJohn Marino #define MD_FALLBACK_FRAME_STATE_FOR x86_dragonfly_fallback_frame_state
98*fdc4107cSJohn Marino 
99*fdc4107cSJohn Marino 
100*fdc4107cSJohn Marino static void
x86_sigtramp_range(unsigned char ** start,unsigned char ** end)101*fdc4107cSJohn Marino x86_sigtramp_range (unsigned char **start, unsigned char **end)
102*fdc4107cSJohn Marino {
103*fdc4107cSJohn Marino   unsigned long ps_strings;
104*fdc4107cSJohn Marino   int mib[2];
105*fdc4107cSJohn Marino   size_t len;
106*fdc4107cSJohn Marino 
107*fdc4107cSJohn Marino   mib[0] = CTL_KERN;
108*fdc4107cSJohn Marino   mib[1] = KERN_PS_STRINGS;
109*fdc4107cSJohn Marino   len = sizeof (ps_strings);
110*fdc4107cSJohn Marino   sysctl (mib, 2, &ps_strings, &len, NULL, 0);
111*fdc4107cSJohn Marino 
112*fdc4107cSJohn Marino   *start = (unsigned char *)ps_strings - 128;
113*fdc4107cSJohn Marino   *end   = (unsigned char *)ps_strings;
114*fdc4107cSJohn Marino }
115*fdc4107cSJohn Marino 
116*fdc4107cSJohn Marino 
117*fdc4107cSJohn Marino static _Unwind_Reason_Code
x86_dragonfly_fallback_frame_state(struct _Unwind_Context * context,_Unwind_FrameState * fs)118*fdc4107cSJohn Marino x86_dragonfly_fallback_frame_state
119*fdc4107cSJohn Marino (struct _Unwind_Context *context, _Unwind_FrameState *fs)
120*fdc4107cSJohn Marino {
121*fdc4107cSJohn Marino   unsigned char *pc = context->ra;
122*fdc4107cSJohn Marino   unsigned char *sigtramp_start, *sigtramp_end;
123*fdc4107cSJohn Marino   struct sigframe *sf;
124*fdc4107cSJohn Marino   long new_cfa;
125*fdc4107cSJohn Marino 
126*fdc4107cSJohn Marino   x86_sigtramp_range(&sigtramp_start, &sigtramp_end);
127*fdc4107cSJohn Marino 
128*fdc4107cSJohn Marino   if (pc >= sigtramp_end || pc < sigtramp_start)
129*fdc4107cSJohn Marino     return _URC_END_OF_STACK;
130*fdc4107cSJohn Marino 
131*fdc4107cSJohn Marino   sf = (struct sigframe *) context->cfa;
132*fdc4107cSJohn Marino   new_cfa = sf->REG_NAME(esp);
133*fdc4107cSJohn Marino   fs->regs.cfa_how = CFA_REG_OFFSET;
134*fdc4107cSJohn Marino   fs->regs.cfa_reg = 4;
135*fdc4107cSJohn Marino   fs->regs.cfa_offset = new_cfa - (long) context->cfa;
136*fdc4107cSJohn Marino 
137*fdc4107cSJohn Marino   /* The SVR4 register numbering macros aren't usable in libgcc.  */
138*fdc4107cSJohn Marino   fs->regs.reg[0].how = REG_SAVED_OFFSET;
139*fdc4107cSJohn Marino   fs->regs.reg[0].loc.offset = (long)&sf->REG_NAME(eax) - new_cfa;
140*fdc4107cSJohn Marino   fs->regs.reg[3].how = REG_SAVED_OFFSET;
141*fdc4107cSJohn Marino   fs->regs.reg[3].loc.offset = (long)&sf->REG_NAME(ebx) - new_cfa;
142*fdc4107cSJohn Marino   fs->regs.reg[1].how = REG_SAVED_OFFSET;
143*fdc4107cSJohn Marino   fs->regs.reg[1].loc.offset = (long)&sf->REG_NAME(ecx) - new_cfa;
144*fdc4107cSJohn Marino   fs->regs.reg[2].how = REG_SAVED_OFFSET;
145*fdc4107cSJohn Marino   fs->regs.reg[2].loc.offset = (long)&sf->REG_NAME(edx) - new_cfa;
146*fdc4107cSJohn Marino   fs->regs.reg[6].how = REG_SAVED_OFFSET;
147*fdc4107cSJohn Marino   fs->regs.reg[6].loc.offset = (long)&sf->REG_NAME(esi) - new_cfa;
148*fdc4107cSJohn Marino   fs->regs.reg[7].how = REG_SAVED_OFFSET;
149*fdc4107cSJohn Marino   fs->regs.reg[7].loc.offset = (long)&sf->REG_NAME(edi) - new_cfa;
150*fdc4107cSJohn Marino   fs->regs.reg[5].how = REG_SAVED_OFFSET;
151*fdc4107cSJohn Marino   fs->regs.reg[5].loc.offset = (long)&sf->REG_NAME(ebp) - new_cfa;
152*fdc4107cSJohn Marino   fs->regs.reg[8].how = REG_SAVED_OFFSET;
153*fdc4107cSJohn Marino   fs->regs.reg[8].loc.offset = (long)&sf->REG_NAME(eip) - new_cfa;
154*fdc4107cSJohn Marino   fs->retaddr_column = 8;
155*fdc4107cSJohn Marino   fs->signal_frame = 1;
156*fdc4107cSJohn Marino   return _URC_NO_REASON;
157*fdc4107cSJohn Marino }
158*fdc4107cSJohn Marino #endif /* ifdef __x86_64__  */
159