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