Initial import
[external/libunwind.git] / src / x86_64 / Gos-linux.c
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 HIDDEN void
34 tdep_fetch_frame (struct dwarf_cursor *dw, unw_word_t ip, int need_unwind_info)
35 {
36   struct cursor *c = (struct cursor *) dw;
37   assert(! need_unwind_info || dw->pi_valid);
38   assert(! need_unwind_info || dw->pi.unwind_info);
39   if (dw->pi_valid
40       && dw->pi.unwind_info
41       && ((struct dwarf_cie_info *) dw->pi.unwind_info)->signal_frame)
42     c->sigcontext_format = X86_64_SCF_LINUX_RT_SIGFRAME;
43   else
44     c->sigcontext_format = X86_64_SCF_NONE;
45
46   Debug(5, "fetch frame ip=0x%lx cfa=0x%lx format=%d\n",
47         dw->ip, dw->cfa, c->sigcontext_format);
48 }
49
50 HIDDEN void
51 tdep_cache_frame (struct dwarf_cursor *dw, struct dwarf_reg_state *rs)
52 {
53   struct cursor *c = (struct cursor *) dw;
54   rs->signal_frame = c->sigcontext_format;
55
56   Debug(5, "cache frame ip=0x%lx cfa=0x%lx format=%d\n",
57         dw->ip, dw->cfa, c->sigcontext_format);
58 }
59
60 HIDDEN void
61 tdep_reuse_frame (struct dwarf_cursor *dw, struct dwarf_reg_state *rs)
62 {
63   struct cursor *c = (struct cursor *) dw;
64   c->sigcontext_format = rs->signal_frame;
65   if (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME)
66   {
67     c->frame_info.frame_type = UNW_X86_64_FRAME_SIGRETURN;
68     /* Offset from cfa to ucontext_t in signal frame.  */
69     c->frame_info.cfa_reg_offset = 0;
70     c->sigcontext_addr = dw->cfa;
71   }
72   else
73     c->sigcontext_addr = 0;
74
75   Debug(5, "reuse frame ip=0x%lx cfa=0x%lx format=%d addr=0x%lx offset=%+d\n",
76         dw->ip, dw->cfa, c->sigcontext_format, c->sigcontext_addr,
77         (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME
78          ? c->frame_info.cfa_reg_offset : 0));
79 }
80
81 PROTECTED int
82 unw_is_signal_frame (unw_cursor_t *cursor)
83 {
84   struct cursor *c = (struct cursor *) cursor;
85   return c->sigcontext_format != X86_64_SCF_NONE;
86 }
87
88 PROTECTED int
89 unw_handle_signal_frame (unw_cursor_t *cursor)
90 {
91 #if UNW_DEBUG /* To silence compiler warnings */
92   /* Should not get here because we now use kernel-provided dwarf
93      information for the signal trampoline and dwarf_step() works.
94      Hence unw_step() should never call this function. Maybe
95      restore old non-dwarf signal handling here, but then the
96      gating on unw_is_signal_frame() needs to be removed. */
97   struct cursor *c = (struct cursor *) cursor;
98   Debug(1, "old format signal frame? format=%d addr=0x%lx cfa=0x%lx\n",
99         c->sigcontext_format, c->sigcontext_addr, c->dwarf.cfa);
100 #endif
101   return -UNW_EBADFRAME;
102 }
103
104 #ifndef UNW_REMOTE_ONLY
105 HIDDEN void *
106 x86_64_r_uc_addr (ucontext_t *uc, int reg)
107 {
108   /* NOTE: common_init() in init.h inlines these for fast path access. */
109   void *addr;
110
111   switch (reg)
112     {
113     case UNW_X86_64_R8: addr = &uc->uc_mcontext.gregs[REG_R8]; break;
114     case UNW_X86_64_R9: addr = &uc->uc_mcontext.gregs[REG_R9]; break;
115     case UNW_X86_64_R10: addr = &uc->uc_mcontext.gregs[REG_R10]; break;
116     case UNW_X86_64_R11: addr = &uc->uc_mcontext.gregs[REG_R11]; break;
117     case UNW_X86_64_R12: addr = &uc->uc_mcontext.gregs[REG_R12]; break;
118     case UNW_X86_64_R13: addr = &uc->uc_mcontext.gregs[REG_R13]; break;
119     case UNW_X86_64_R14: addr = &uc->uc_mcontext.gregs[REG_R14]; break;
120     case UNW_X86_64_R15: addr = &uc->uc_mcontext.gregs[REG_R15]; break;
121     case UNW_X86_64_RDI: addr = &uc->uc_mcontext.gregs[REG_RDI]; break;
122     case UNW_X86_64_RSI: addr = &uc->uc_mcontext.gregs[REG_RSI]; break;
123     case UNW_X86_64_RBP: addr = &uc->uc_mcontext.gregs[REG_RBP]; break;
124     case UNW_X86_64_RBX: addr = &uc->uc_mcontext.gregs[REG_RBX]; break;
125     case UNW_X86_64_RDX: addr = &uc->uc_mcontext.gregs[REG_RDX]; break;
126     case UNW_X86_64_RAX: addr = &uc->uc_mcontext.gregs[REG_RAX]; break;
127     case UNW_X86_64_RCX: addr = &uc->uc_mcontext.gregs[REG_RCX]; break;
128     case UNW_X86_64_RSP: addr = &uc->uc_mcontext.gregs[REG_RSP]; break;
129     case UNW_X86_64_RIP: addr = &uc->uc_mcontext.gregs[REG_RIP]; break;
130
131     default:
132       addr = NULL;
133     }
134   return addr;
135 }
136
137 /* sigreturn() is a no-op on x86_64 glibc.  */
138 HIDDEN NORETURN void
139 x86_64_sigreturn (unw_cursor_t *cursor)
140 {
141   struct cursor *c = (struct cursor *) cursor;
142   struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
143
144   Debug (8, "resuming at ip=%llx via sigreturn(%p)\n",
145              (unsigned long long) c->dwarf.ip, sc);
146   __asm__ __volatile__ ("mov %0, %%rsp;"
147                         "mov %1, %%rax;"
148                         "syscall"
149                         :: "r"(sc), "i"(SYS_rt_sigreturn)
150                         : "memory");
151   abort();
152 }
153
154 #endif