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 "unwind_i.h"
29 #include "ucontext_i.h"
30
31 #include <sys/syscall.h>
32
33 struct sigframe {
34 uint64_t signo;
35 uint64_t sip;
36 };
37
38 int
unw_is_signal_frame(unw_cursor_t * cursor)39 unw_is_signal_frame (unw_cursor_t *cursor)
40 {
41 struct cursor *c = (struct cursor *) cursor;
42
43 c->sigcontext_format = (c->dwarf.ip == (unw_word_t)-1) ?
44 X86_64_SCF_SOLARIS_SIGFRAME : X86_64_SCF_NONE;
45
46 return (c->sigcontext_format);
47 }
48
49 HIDDEN int
x86_64_handle_signal_frame(unw_cursor_t * cursor)50 x86_64_handle_signal_frame (unw_cursor_t *cursor)
51 {
52 struct cursor *c = (struct cursor *) cursor;
53 unw_word_t ucontext = c->dwarf.cfa + sizeof (struct sigframe);
54
55 if (c->sigcontext_format != X86_64_SCF_SOLARIS_SIGFRAME)
56 return -UNW_EBADFRAME;
57
58 c->sigcontext_addr = c->dwarf.cfa;
59
60 Debug(1, "signal frame cfa = %lx ucontext = %lx\n",
61 (uint64_t)c->dwarf.cfa, (uint64_t)ucontext);
62
63 struct dwarf_loc rsp_loc = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSP, 0);
64 int ret = dwarf_get (&c->dwarf, rsp_loc, &c->dwarf.cfa);
65
66 if (ret < 0)
67 {
68 Debug (2, "return %d\n", ret);
69 return ret;
70 }
71
72 c->dwarf.loc[RAX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RAX, 0);
73 c->dwarf.loc[RDX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RDX, 0);
74 c->dwarf.loc[RCX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RCX, 0);
75 c->dwarf.loc[RBX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RBX, 0);
76 c->dwarf.loc[RSI] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSI, 0);
77 c->dwarf.loc[RDI] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RDI, 0);
78 c->dwarf.loc[RBP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RBP, 0);
79 c->dwarf.loc[RSP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSP, 0);
80 c->dwarf.loc[ R8] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R8, 0);
81 c->dwarf.loc[ R9] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R9, 0);
82 c->dwarf.loc[R10] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R10, 0);
83 c->dwarf.loc[R11] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R11, 0);
84 c->dwarf.loc[R12] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R12, 0);
85 c->dwarf.loc[R13] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R13, 0);
86 c->dwarf.loc[R14] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R14, 0);
87 c->dwarf.loc[R15] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R15, 0);
88 c->dwarf.loc[RIP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RIP, 0);
89
90 c->dwarf.use_prev_instr = 1;
91 return 0;
92 }
93
94 #ifndef UNW_REMOTE_ONLY
95 HIDDEN void *
x86_64_r_uc_addr(ucontext_t * uc,int reg)96 x86_64_r_uc_addr (ucontext_t *uc, int reg)
97 {
98 /* NOTE: common_init() in init.h inlines these for fast path access. */
99 void *addr;
100
101 switch (reg)
102 {
103 case UNW_X86_64_R8: addr = &uc->uc_mcontext.gregs[REG_R8]; break;
104 case UNW_X86_64_R9: addr = &uc->uc_mcontext.gregs[REG_R9]; break;
105 case UNW_X86_64_R10: addr = &uc->uc_mcontext.gregs[REG_R10]; break;
106 case UNW_X86_64_R11: addr = &uc->uc_mcontext.gregs[REG_R11]; break;
107 case UNW_X86_64_R12: addr = &uc->uc_mcontext.gregs[REG_R12]; break;
108 case UNW_X86_64_R13: addr = &uc->uc_mcontext.gregs[REG_R13]; break;
109 case UNW_X86_64_R14: addr = &uc->uc_mcontext.gregs[REG_R14]; break;
110 case UNW_X86_64_R15: addr = &uc->uc_mcontext.gregs[REG_R15]; break;
111 case UNW_X86_64_RDI: addr = &uc->uc_mcontext.gregs[REG_RDI]; break;
112 case UNW_X86_64_RSI: addr = &uc->uc_mcontext.gregs[REG_RSI]; break;
113 case UNW_X86_64_RBP: addr = &uc->uc_mcontext.gregs[REG_RBP]; break;
114 case UNW_X86_64_RBX: addr = &uc->uc_mcontext.gregs[REG_RBX]; break;
115 case UNW_X86_64_RDX: addr = &uc->uc_mcontext.gregs[REG_RDX]; break;
116 case UNW_X86_64_RAX: addr = &uc->uc_mcontext.gregs[REG_RAX]; break;
117 case UNW_X86_64_RCX: addr = &uc->uc_mcontext.gregs[REG_RCX]; break;
118 case UNW_X86_64_RSP: addr = &uc->uc_mcontext.gregs[REG_RSP]; break;
119 case UNW_X86_64_RIP: addr = &uc->uc_mcontext.gregs[REG_RIP]; break;
120
121 default:
122 addr = NULL;
123 }
124 return addr;
125 }
126
127 HIDDEN NORETURN void
x86_64_sigreturn(unw_cursor_t * cursor)128 x86_64_sigreturn (unw_cursor_t *cursor)
129 {
130 abort();
131 }
132
133 #endif
134