1/* libunwind - a platform-independent unwind library
2   Copyright (c) 2004 Hewlett-Packard Development Company, L.P.
3	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25
26#include <stdlib.h>
27
28#include "unwind_i.h"
29
30#ifndef UNW_REMOTE_ONLY
31
32#if defined(__linux)
33
34# include <sys/syscall.h>
35
36static NORETURN inline long
37my_rt_sigreturn (void *new_sp, int in_syscall)
38{
39  register unsigned long r25 __asm__ ("r25") = (in_syscall != 0);
40  register unsigned long r20 __asm__ ("r20") = SYS_rt_sigreturn;
41
42  __asm__ __volatile__ ("copy %0, %%sp\n"
43			"be,l 0x100(%%sr2,%%r0),%%sr0,%%r31\n"
44			"nop"
45			:
46			: "r"(new_sp), "r"(r20), "r"(r25)
47			: "memory");
48  abort ();
49}
50
51#endif /* __linux */
52
53HIDDEN inline int
54hppa_local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
55{
56#if defined(__linux)
57  struct cursor *c = (struct cursor *) cursor;
58  ucontext_t *uc = c->dwarf.as_arg;
59
60  /* Ensure c->pi is up-to-date.  On PA-RISC, it's relatively common to be
61     missing DWARF unwind info.  We don't want to fail in that case,
62     because the frame-chain still would let us do a backtrace at
63     least.  */
64  dwarf_make_proc_info (&c->dwarf);
65
66  if (unlikely (c->sigcontext_format != HPPA_SCF_NONE))
67    {
68      struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
69
70      Debug (8, "resuming at ip=%x via sigreturn(%p)\n", c->dwarf.ip, sc);
71      my_rt_sigreturn (sc, (sc->sc_flags & PARISC_SC_FLAG_IN_SYSCALL) != 0);
72    }
73  else
74    {
75      Debug (8, "resuming at ip=%x via setcontext()\n", c->dwarf.ip);
76      setcontext (uc);
77    }
78#else
79# warning Implement me!
80#endif
81  return -UNW_EINVAL;
82}
83
84#endif /* !UNW_REMOTE_ONLY */
85
86/* This routine is responsible for copying the register values in
87   cursor C and establishing them as the current machine state. */
88
89static inline int
90establish_machine_state (struct cursor *c)
91{
92  int (*access_reg) (unw_addr_space_t, unw_regnum_t, unw_word_t *,
93		     int write, void *);
94  int (*access_fpreg) (unw_addr_space_t, unw_regnum_t, unw_fpreg_t *,
95		       int write, void *);
96  unw_addr_space_t as = c->dwarf.as;
97  void *arg = c->dwarf.as_arg;
98  unw_fpreg_t fpval;
99  unw_word_t val;
100  int reg;
101
102  access_reg = as->acc.access_reg;
103  access_fpreg = as->acc.access_fpreg;
104
105  Debug (8, "copying out cursor state\n");
106
107  for (reg = 0; reg <= UNW_REG_LAST; ++reg)
108    {
109      Debug (16, "copying %s %d\n", unw_regname (reg), reg);
110      if (unw_is_fpreg (reg))
111	{
112	  if (tdep_access_fpreg (c, reg, &fpval, 0) >= 0)
113	    (*access_fpreg) (as, reg, &fpval, 1, arg);
114	}
115      else
116	{
117	  if (tdep_access_reg (c, reg, &val, 0) >= 0)
118	    (*access_reg) (as, reg, &val, 1, arg);
119	}
120    }
121  return 0;
122}
123
124PROTECTED int
125unw_resume (unw_cursor_t *cursor)
126{
127  struct cursor *c = (struct cursor *) cursor;
128  int ret;
129
130  Debug (1, "(cursor=%p)\n", c);
131
132  if (!c->dwarf.ip)
133    {
134      /* This can happen easily when the frame-chain gets truncated
135         due to bad or missing unwind-info.  */
136      Debug (1, "refusing to resume execution at address 0\n");
137      return -UNW_EINVAL;
138    }
139
140  if ((ret = establish_machine_state (c)) < 0)
141    return ret;
142
143  return (*c->dwarf.as->acc.resume) (c->dwarf.as, (unw_cursor_t *) c,
144				     c->dwarf.as_arg);
145}
146