1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
3
4 This file is part of libunwind.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <ucontext.h>
30 #include <machine/sigframe.h>
31 #include <signal.h>
32 #include <stddef.h>
33 #include "unwind_i.h"
34 #include "ucontext_i.h"
35
36 int
unw_is_signal_frame(unw_cursor_t * cursor)37 unw_is_signal_frame (unw_cursor_t *cursor)
38 {
39 /* XXXKIB */
40 struct cursor *c = (struct cursor *) cursor;
41 unw_word_t w0, w1, w2, b0, ip;
42 unw_addr_space_t as;
43 unw_accessors_t *a;
44 void *arg;
45 int ret;
46
47 as = c->dwarf.as;
48 a = unw_get_accessors_int (as);
49 arg = c->dwarf.as_arg;
50
51 /* Check if RIP points at sigreturn sequence.
52 48 8d 7c 24 10 lea SIGF_UC(%rsp),%rdi
53 6a 00 pushq $0
54 48 c7 c0 a1 01 00 00 movq $SYS_sigreturn,%rax
55 0f 05 syscall
56 f4 0: hlt
57 eb fd jmp 0b
58 */
59
60 ip = c->dwarf.ip;
61 c->sigcontext_format = X86_64_SCF_NONE;
62 if ((ret = (*a->access_mem) (as, ip, &w0, 0, arg)) < 0
63 || (ret = (*a->access_mem) (as, ip + 8, &w1, 0, arg)) < 0
64 || (ret = (*a->access_mem) (as, ip + 16, &w2, 0, arg)) < 0)
65 return 0;
66 w2 &= 0xffffff;
67 if (w0 == 0x48006a10247c8d48 &&
68 w1 == 0x050f000001a1c0c7 &&
69 w2 == 0x0000000000fdebf4)
70 {
71 c->sigcontext_format = X86_64_SCF_FREEBSD_SIGFRAME;
72 return (c->sigcontext_format);
73 }
74 /* Check if RIP points at standard syscall sequence.
75 49 89 ca mov %rcx,%r10
76 0f 05 syscall
77 */
78 if ((ret = (*a->access_mem) (as, ip - 5, &b0, 0, arg)) < 0)
79 return (0);
80 Debug (12, "b0 0x%lx\n", b0);
81 if ((b0 & 0xffffffffffffff) == 0x050fca89490000 ||
82 (b0 & 0xffffffffff) == 0x050fca8949)
83 {
84 c->sigcontext_format = X86_64_SCF_FREEBSD_SYSCALL;
85 return (c->sigcontext_format);
86 }
87 return (X86_64_SCF_NONE);
88 }
89
90 HIDDEN int
x86_64_handle_signal_frame(unw_cursor_t * cursor)91 x86_64_handle_signal_frame (unw_cursor_t *cursor)
92 {
93 struct cursor *c = (struct cursor *) cursor;
94 unw_word_t ucontext;
95 int i, ret;
96
97 if (c->sigcontext_format == X86_64_SCF_FREEBSD_SIGFRAME)
98 {
99 ucontext = c->dwarf.cfa + offsetof(struct sigframe, sf_uc);
100 c->sigcontext_addr = c->dwarf.cfa;
101 Debug(1, "signal frame, skip over trampoline\n");
102
103 struct dwarf_loc rsp_loc = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSP, 0);
104 ret = dwarf_get (&c->dwarf, rsp_loc, &c->dwarf.cfa);
105 if (ret < 0)
106 {
107 Debug (2, "returning %d\n", ret);
108 return ret;
109 }
110
111 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
112 c->dwarf.loc[i] = DWARF_NULL_LOC;
113
114 c->dwarf.loc[RAX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RAX, 0);
115 c->dwarf.loc[RDX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RDX, 0);
116 c->dwarf.loc[RCX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RCX, 0);
117 c->dwarf.loc[RBX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RBX, 0);
118 c->dwarf.loc[RSI] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSI, 0);
119 c->dwarf.loc[RDI] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RDI, 0);
120 c->dwarf.loc[RBP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RBP, 0);
121 c->dwarf.loc[RSP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSP, 0);
122 c->dwarf.loc[ R8] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R8, 0);
123 c->dwarf.loc[ R9] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R9, 0);
124 c->dwarf.loc[R10] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R10, 0);
125 c->dwarf.loc[R11] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R11, 0);
126 c->dwarf.loc[R12] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R12, 0);
127 c->dwarf.loc[R13] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R13, 0);
128 c->dwarf.loc[R14] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R14, 0);
129 c->dwarf.loc[R15] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R15, 0);
130 c->dwarf.loc[RIP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RIP, 0);
131
132 return 0;
133 }
134 else if (c->sigcontext_format == X86_64_SCF_FREEBSD_SYSCALL)
135 {
136 c->dwarf.loc[RCX] = c->dwarf.loc[R10];
137 /* rsp_loc = DWARF_LOC(c->dwarf.cfa - 8, 0); */
138 /* rbp_loc = c->dwarf.loc[RBP]; */
139 c->dwarf.loc[RSP] = DWARF_VAL_LOC (c, c->dwarf.cfa + 8);
140 c->dwarf.loc[RIP] = DWARF_LOC (c->dwarf.cfa, 0);
141 ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
142 Debug (1, "Frame Chain [RIP=0x%Lx] = 0x%Lx\n",
143 (unsigned long long) DWARF_GET_LOC (c->dwarf.loc[RIP]),
144 (unsigned long long) c->dwarf.ip);
145 if (ret < 0)
146 {
147 Debug (2, "returning %d\n", ret);
148 return ret;
149 }
150 c->dwarf.cfa += 8;
151 c->dwarf.use_prev_instr = 1;
152 return 1;
153 }
154 else
155 return -UNW_EBADFRAME;
156
157 }
158
159 #ifndef UNW_REMOTE_ONLY
160 HIDDEN void *
x86_64_r_uc_addr(ucontext_t * uc,int reg)161 x86_64_r_uc_addr (ucontext_t *uc, int reg)
162 {
163 /* NOTE: common_init() in init.h inlines these for fast path access. */
164 void *addr;
165
166 switch (reg)
167 {
168 case UNW_X86_64_R8: addr = &uc->uc_mcontext.mc_r8; break;
169 case UNW_X86_64_R9: addr = &uc->uc_mcontext.mc_r9; break;
170 case UNW_X86_64_R10: addr = &uc->uc_mcontext.mc_r10; break;
171 case UNW_X86_64_R11: addr = &uc->uc_mcontext.mc_r11; break;
172 case UNW_X86_64_R12: addr = &uc->uc_mcontext.mc_r12; break;
173 case UNW_X86_64_R13: addr = &uc->uc_mcontext.mc_r13; break;
174 case UNW_X86_64_R14: addr = &uc->uc_mcontext.mc_r14; break;
175 case UNW_X86_64_R15: addr = &uc->uc_mcontext.mc_r15; break;
176 case UNW_X86_64_RDI: addr = &uc->uc_mcontext.mc_rdi; break;
177 case UNW_X86_64_RSI: addr = &uc->uc_mcontext.mc_rsi; break;
178 case UNW_X86_64_RBP: addr = &uc->uc_mcontext.mc_rbp; break;
179 case UNW_X86_64_RBX: addr = &uc->uc_mcontext.mc_rbx; break;
180 case UNW_X86_64_RDX: addr = &uc->uc_mcontext.mc_rdx; break;
181 case UNW_X86_64_RAX: addr = &uc->uc_mcontext.mc_rax; break;
182 case UNW_X86_64_RCX: addr = &uc->uc_mcontext.mc_rcx; break;
183 case UNW_X86_64_RSP: addr = &uc->uc_mcontext.mc_rsp; break;
184 case UNW_X86_64_RIP: addr = &uc->uc_mcontext.mc_rip; break;
185
186 default:
187 addr = NULL;
188 }
189 return addr;
190 }
191
192 HIDDEN NORETURN void
x86_64_sigreturn(unw_cursor_t * cursor)193 x86_64_sigreturn (unw_cursor_t *cursor)
194 {
195 struct cursor *c = (struct cursor *) cursor;
196 ucontext_t *uc = (ucontext_t *)(c->sigcontext_addr +
197 offsetof(struct sigframe, sf_uc));
198
199 uc->uc_mcontext.mc_r8 = c->uc->uc_mcontext.mc_r8;
200 uc->uc_mcontext.mc_r9 = c->uc->uc_mcontext.mc_r9;
201 uc->uc_mcontext.mc_r10 = c->uc->uc_mcontext.mc_r10;
202 uc->uc_mcontext.mc_r11 = c->uc->uc_mcontext.mc_r11;
203 uc->uc_mcontext.mc_r12 = c->uc->uc_mcontext.mc_r12;
204 uc->uc_mcontext.mc_r13 = c->uc->uc_mcontext.mc_r13;
205 uc->uc_mcontext.mc_r14 = c->uc->uc_mcontext.mc_r14;
206 uc->uc_mcontext.mc_r15 = c->uc->uc_mcontext.mc_r15;
207 uc->uc_mcontext.mc_rdi = c->uc->uc_mcontext.mc_rdi;
208 uc->uc_mcontext.mc_rsi = c->uc->uc_mcontext.mc_rsi;
209 uc->uc_mcontext.mc_rbp = c->uc->uc_mcontext.mc_rbp;
210 uc->uc_mcontext.mc_rbx = c->uc->uc_mcontext.mc_rbx;
211 uc->uc_mcontext.mc_rdx = c->uc->uc_mcontext.mc_rdx;
212 uc->uc_mcontext.mc_rax = c->uc->uc_mcontext.mc_rax;
213 uc->uc_mcontext.mc_rcx = c->uc->uc_mcontext.mc_rcx;
214 uc->uc_mcontext.mc_rsp = c->uc->uc_mcontext.mc_rsp;
215 uc->uc_mcontext.mc_rip = c->uc->uc_mcontext.mc_rip;
216
217 Debug (8, "resuming at ip=%llx via sigreturn(%p)\n",
218 (unsigned long long) c->dwarf.ip, uc);
219 sigreturn(uc);
220 abort();
221 }
222 #endif
223