1a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock/* libunwind - a platform-independent unwind library
2a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock   Copyright (c) 2002-2004 Hewlett-Packard Development Company, L.P.
3a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
5a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock   Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
6a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
7a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockThis file is part of libunwind.
8a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
9a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockPermission is hereby granted, free of charge, to any person obtaining
10a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbocka copy of this software and associated documentation files (the
11a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock"Software"), to deal in the Software without restriction, including
12a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockwithout limitation the rights to use, copy, modify, merge, publish,
13a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockdistribute, sublicense, and/or sell copies of the Software, and to
14a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockpermit persons to whom the Software is furnished to do so, subject to
15a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockthe following conditions:
16a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
17a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockThe above copyright notice and this permission notice shall be
18a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockincluded in all copies or substantial portions of the Software.
19a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
20a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
27a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
28a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock#include <stdlib.h>
29a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
3053095e6b3e4a6738e814c8f40cf546dca96a061fKonstantin Belousov#include "offsets.h"
31a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock#include "unwind_i.h"
32a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
33a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock#ifndef UNW_REMOTE_ONLY
34a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
35a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockHIDDEN inline int
36a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockx86_64_local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
37a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock{
38a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm  struct cursor *c = (struct cursor *) cursor;
39649f1fb3449a65dd0626a709432d8b02a7c56bbcArun Sharma  ucontext_t *uc = c->uc;
40a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm
41a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm  /* Ensure c->pi is up-to-date.  On x86-64, it's relatively common to
42a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm     be missing DWARF unwind info.  We don't want to fail in that
43a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm     case, because the frame-chain still would let us do a backtrace
44a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm     at least.  */
45a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm  dwarf_make_proc_info (&c->dwarf);
46a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm
47a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm  if (unlikely (c->sigcontext_format != X86_64_SCF_NONE))
48a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm    {
49979af4502fe19b0d98459633731d004a4a010a0eKonstantin Belousov      x86_64_sigreturn(cursor);
500f1c0f5f664004f0e812e7c2d939bc21fc5e89dfKonstantin Belousov      abort();
51a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm    }
52a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm  else
53a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm    {
54a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm      Debug (8, "resuming at ip=%llx via setcontext()\n",
55a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm	     (unsigned long long) c->dwarf.ip);
56491d576529a3b01208accb37627a075d7ce07093Arun Sharma      setcontext (uc);
57a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm    }
58a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm  return -UNW_EINVAL;
59a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock}
60a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
61a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock#endif /* !UNW_REMOTE_ONLY */
62a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
63a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock/* This routine is responsible for copying the register values in
64a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock   cursor C and establishing them as the current machine state. */
65a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
66a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockstatic inline int
67a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockestablish_machine_state (struct cursor *c)
68a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock{
69a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  int (*access_reg) (unw_addr_space_t, unw_regnum_t, unw_word_t *,
70a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock		     int write, void *);
71a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  int (*access_fpreg) (unw_addr_space_t, unw_regnum_t, unw_fpreg_t *,
72a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock		       int write, void *);
73a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  unw_addr_space_t as = c->dwarf.as;
74a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  void *arg = c->dwarf.as_arg;
75a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  unw_fpreg_t fpval;
76a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  unw_word_t val;
77a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  int reg;
78a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
79a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  access_reg = as->acc.access_reg;
80a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  access_fpreg = as->acc.access_fpreg;
81a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
82a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  Debug (8, "copying out cursor state\n");
83a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
84a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm  for (reg = 0; reg <= UNW_REG_LAST; ++reg)
85a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock    {
86a9cad72ed4626c80843b5f4ac075b93b4a873155hp.com!davidm      Debug (16, "copying %s %d\n", unw_regname (reg), reg);
87a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock      if (unw_is_fpreg (reg))
88a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock	{
89a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock	  if (tdep_access_fpreg (c, reg, &fpval, 0) >= 0)
90a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock	    (*access_fpreg) (as, reg, &fpval, 1, arg);
91a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock	}
92a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock      else
93a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock	{
94a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock	  if (tdep_access_reg (c, reg, &val, 0) >= 0)
95a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock	    (*access_reg) (as, reg, &val, 1, arg);
96a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock	}
97a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock    }
98a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  return 0;
99a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock}
100a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
101a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockPROTECTED int
102a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbockunw_resume (unw_cursor_t *cursor)
103a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock{
104a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  struct cursor *c = (struct cursor *) cursor;
105a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  int ret;
106a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
107f576cce7480d6acee537cecfeb5530187e50f785hp.com!davidm  Debug (1, "(cursor=%p)\n", c);
108a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
109a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  if ((ret = establish_machine_state (c)) < 0)
110a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock    return ret;
111a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock
112a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock  return (*c->dwarf.as->acc.resume) (c->dwarf.as, (unw_cursor_t *) c,
113a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock				     c->dwarf.as_arg);
114a766efd844260866e0d216f6eeef87f4593f60b2ibm.com!masbock}
115