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
7This file is part of libunwind.
8
9Permission is hereby granted, free of charge, to any person obtaining
10a copy of this software and associated documentation files (the
11"Software"), to deal in the Software without restriction, including
12without limitation the rights to use, copy, modify, merge, publish,
13distribute, sublicense, and/or sell copies of the Software, and to
14permit persons to whom the Software is furnished to do so, subject to
15the following conditions:
16
17The above copyright notice and this permission notice shall be
18included in all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26WITH 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
33HIDDEN void
34tdep_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
50HIDDEN void
51tdep_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
60HIDDEN void
61tdep_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
81PROTECTED int
82unw_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
88PROTECTED int
89unw_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
105HIDDEN void *
106x86_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.  */
138HIDDEN NORETURN void
139x86_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
155