5f82c013c6b4df05413b234c9ebe756d7d1f9f59
[platform/upstream/libunwind.git] / src / ia64 / Ginit_local.c
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
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH 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
31 PROTECTED int
32 unw_init_local (unw_cursor_t *cursor, unw_context_t *uc)
33 {
34   return -UNW_EINVAL;
35 }
36
37 #else /* !UNW_REMOTE_ONLY */
38
39 static inline void
40 set_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
50 static inline int
51 get_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
79 PROTECTED int
80 unw_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 */