1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2001-2003, 2005 Hewlett-Packard Co
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 "init.h"
27#include "unwind_i.h"
28
29#ifdef UNW_REMOTE_ONLY
30
31PROTECTED int
32unw_init_local (unw_cursor_t *cursor, unw_context_t *uc)
33{
34  return -UNW_EINVAL;
35}
36
37#else /* !UNW_REMOTE_ONLY */
38
39static inline void
40set_as_arg (struct cursor *c, unw_context_t *uc)
41{
42#if defined(__linux) && defined(__KERNEL__)
43  c->task = current;
44  c->as_arg = &uc->sw;
45#else
46  c->as_arg = uc;
47#endif
48}
49
50static inline int
51get_initial_stack_pointers (struct cursor *c, unw_context_t *uc,
52			    unw_word_t *sp, unw_word_t *bsp)
53{
54#if defined(__linux)
55  unw_word_t sol, bspstore;
56
57#ifdef __KERNEL__
58  sol = (uc->sw.ar_pfs >> 7) & 0x7f;
59  bspstore = uc->sw.ar_bspstore;
60  *sp = uc->ksp;
61# else
62  sol = (uc->uc_mcontext.sc_ar_pfs >> 7) & 0x7f;
63  bspstore = uc->uc_mcontext.sc_ar_bsp;
64  *sp = uc->uc_mcontext.sc_gr[12];
65# endif
66  *bsp = rse_skip_regs (bspstore, -sol);
67#elif defined(__hpux)
68  int ret;
69
70  if ((ret = ia64_get (c, IA64_REG_LOC (c, UNW_IA64_GR + 12), sp)) < 0
71      || (ret = ia64_get (c, IA64_REG_LOC (c, UNW_IA64_AR_BSP), bsp)) < 0)
72    return ret;
73#else
74# error Fix me.
75#endif
76  return 0;
77}
78
79PROTECTED int
80unw_init_local (unw_cursor_t *cursor, unw_context_t *uc)
81{
82  struct cursor *c = (struct cursor *) cursor;
83  unw_word_t sp, bsp;
84  int ret;
85
86  if (!tdep_init_done)
87    tdep_init ();
88
89  Debug (1, "(cursor=%p)\n", c);
90
91  c->as = unw_local_addr_space;
92  set_as_arg (c, uc);
93
94  if ((ret = get_initial_stack_pointers (c, uc, &sp, &bsp)) < 0)
95    return ret;
96
97  Debug (4, "initial bsp=%lx, sp=%lx\n", bsp, sp);
98
99  if ((ret = common_init (c, sp, bsp)) < 0)
100    return ret;
101
102#ifdef __hpux
103  /* On HP-UX, the context created by getcontext() points to the
104     getcontext() system call stub.  Step over it: */
105  ret = unw_step (cursor);
106#endif
107  return ret;
108}
109
110#endif /* !UNW_REMOTE_ONLY */
111