1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2002-2003 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
6
7 This file is part of libunwind.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
27
28 #include "libunwind_i.h"
29 #include "unwind_i.h"
30 #include "ucontext_i.h"
31
32 #include <sys/syscall.h>
33
34 HIDDEN void
tdep_fetch_frame(struct dwarf_cursor * dw,unw_word_t ip,int need_unwind_info)35 tdep_fetch_frame (struct dwarf_cursor *dw, unw_word_t ip, int need_unwind_info)
36 {
37 struct cursor *c = (struct cursor *) dw;
38 assert(! need_unwind_info || dw->pi_valid);
39 assert(! need_unwind_info || dw->pi.unwind_info);
40 if (dw->pi_valid
41 && dw->pi.unwind_info
42 && ((struct dwarf_cie_info *) dw->pi.unwind_info)->signal_frame)
43 c->sigcontext_format = X86_64_SCF_LINUX_RT_SIGFRAME;
44 else
45 c->sigcontext_format = X86_64_SCF_NONE;
46
47 Debug(5, "fetch frame ip=0x%lx cfa=0x%lx format=%d\n",
48 dw->ip, dw->cfa, c->sigcontext_format);
49 }
50
51 HIDDEN int
tdep_cache_frame(struct dwarf_cursor * dw)52 tdep_cache_frame (struct dwarf_cursor *dw)
53 {
54 struct cursor *c = (struct cursor *) dw;
55
56 Debug(5, "cache frame ip=0x%lx cfa=0x%lx format=%d\n",
57 dw->ip, dw->cfa, c->sigcontext_format);
58 return c->sigcontext_format;
59 }
60
61 HIDDEN void
tdep_reuse_frame(struct dwarf_cursor * dw,int frame)62 tdep_reuse_frame (struct dwarf_cursor *dw, int frame)
63 {
64 struct cursor *c = (struct cursor *) dw;
65 c->sigcontext_format = frame;
66 if (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME)
67 {
68 c->frame_info.frame_type = UNW_X86_64_FRAME_SIGRETURN;
69 /* Offset from cfa to ucontext_t in signal frame. */
70 c->frame_info.cfa_reg_offset = 0;
71 c->sigcontext_addr = dw->cfa;
72 }
73
74 Debug(5, "reuse frame ip=0x%lx cfa=0x%lx format=%d addr=0x%lx offset=%+d\n",
75 dw->ip, dw->cfa, c->sigcontext_format, c->sigcontext_addr,
76 (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME
77 ? c->frame_info.cfa_reg_offset : 0));
78 }
79
80 int
unw_is_signal_frame(unw_cursor_t * cursor)81 unw_is_signal_frame (unw_cursor_t *cursor)
82 {
83 struct cursor *c = (struct cursor *) cursor;
84 return c->sigcontext_format != X86_64_SCF_NONE;
85 }
86
87 HIDDEN int
x86_64_handle_signal_frame(unw_cursor_t * cursor)88 x86_64_handle_signal_frame (unw_cursor_t *cursor)
89 {
90 #if UNW_DEBUG /* To silence compiler warnings */
91 /* Should not get here because we now use kernel-provided dwarf
92 information for the signal trampoline and dwarf_step() works.
93 Hence unw_step() should never call this function. Maybe
94 restore old non-dwarf signal handling here, but then the
95 gating on unw_is_signal_frame() needs to be removed. */
96 struct cursor *c = (struct cursor *) cursor;
97 Debug(1, "old format signal frame? format=%d addr=0x%lx cfa=0x%lx\n",
98 c->sigcontext_format, c->sigcontext_addr, c->dwarf.cfa);
99 #endif
100 return -UNW_EBADFRAME;
101 }
102
103 #ifndef UNW_REMOTE_ONLY
104 HIDDEN void *
x86_64_r_uc_addr(ucontext_t * uc,int reg)105 x86_64_r_uc_addr (ucontext_t *uc, int reg)
106 {
107 /* NOTE: common_init() in init.h inlines these for fast path access. */
108 void *addr;
109
110 switch (reg)
111 {
112 case UNW_X86_64_R8: addr = &uc->uc_mcontext.gregs[REG_R8]; break;
113 case UNW_X86_64_R9: addr = &uc->uc_mcontext.gregs[REG_R9]; break;
114 case UNW_X86_64_R10: addr = &uc->uc_mcontext.gregs[REG_R10]; break;
115 case UNW_X86_64_R11: addr = &uc->uc_mcontext.gregs[REG_R11]; break;
116 case UNW_X86_64_R12: addr = &uc->uc_mcontext.gregs[REG_R12]; break;
117 case UNW_X86_64_R13: addr = &uc->uc_mcontext.gregs[REG_R13]; break;
118 case UNW_X86_64_R14: addr = &uc->uc_mcontext.gregs[REG_R14]; break;
119 case UNW_X86_64_R15: addr = &uc->uc_mcontext.gregs[REG_R15]; break;
120 case UNW_X86_64_RDI: addr = &uc->uc_mcontext.gregs[REG_RDI]; break;
121 case UNW_X86_64_RSI: addr = &uc->uc_mcontext.gregs[REG_RSI]; break;
122 case UNW_X86_64_RBP: addr = &uc->uc_mcontext.gregs[REG_RBP]; break;
123 case UNW_X86_64_RBX: addr = &uc->uc_mcontext.gregs[REG_RBX]; break;
124 case UNW_X86_64_RDX: addr = &uc->uc_mcontext.gregs[REG_RDX]; break;
125 case UNW_X86_64_RAX: addr = &uc->uc_mcontext.gregs[REG_RAX]; break;
126 case UNW_X86_64_RCX: addr = &uc->uc_mcontext.gregs[REG_RCX]; break;
127 case UNW_X86_64_RSP: addr = &uc->uc_mcontext.gregs[REG_RSP]; break;
128 case UNW_X86_64_RIP: addr = &uc->uc_mcontext.gregs[REG_RIP]; break;
129
130 default:
131 addr = NULL;
132 }
133 return addr;
134 }
135
136 /* sigreturn() is a no-op on x86_64 glibc. */
137 HIDDEN NORETURN void
x86_64_sigreturn(unw_cursor_t * cursor)138 x86_64_sigreturn (unw_cursor_t *cursor)
139 {
140 struct cursor *c = (struct cursor *) cursor;
141 struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
142 mcontext_t *sc_mcontext = &((ucontext_t*)sc)->uc_mcontext;
143 /* Copy in saved uc - all preserved regs are at the start of sigcontext */
144 memcpy(sc_mcontext, &dwarf_get_uc(&c->dwarf)->uc_mcontext,
145 DWARF_NUM_PRESERVED_REGS * sizeof(unw_word_t));
146
147 Debug (8, "resuming at ip=%llx via sigreturn(%p)\n",
148 (unsigned long long) c->dwarf.ip, sc);
149 __asm__ __volatile__ ("mov %0, %%rsp;"
150 "mov %1, %%rax;"
151 "syscall"
152 :: "r"((uint64_t)sc), "i"(SYS_rt_sigreturn)
153 : "memory");
154 abort();
155 }
156
157 #endif
158