1/* libunwind - a platform-independent unwind library
2   Copyright (C) 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#if UNW_TARGET_IA64
27
28#include "libunwind_i.h"
29#include "tdep-ia64/rse.h"
30
31static inline int
32bsp_match (unw_cursor_t *c, unw_word_t *wp)
33{
34  unw_word_t bsp, pfs, sol;
35
36  if (unw_get_reg (c, UNW_IA64_BSP, &bsp) < 0
37      || unw_get_reg (c, UNW_IA64_AR_PFS, &pfs) < 0)
38    abort ();
39
40  /* simulate the effect of "br.call sigsetjmp" on ar.bsp: */
41  sol = (pfs >> 7) & 0x7f;
42  bsp = rse_skip_regs (bsp, sol);
43
44  if (bsp != wp[JB_BSP])
45    return 0;
46
47  if (unlikely (sol == 0))
48    {
49      unw_word_t sp, prev_sp;
50      unw_cursor_t tmp = *c;
51
52      /* The caller of {sig,}setjmp() cannot have a NULL-frame.  If we
53	 see a NULL-frame, we haven't reached the right target yet.
54	 To have a NULL-frame, the number of locals must be zero and
55	 the stack-frame must also be empty.  */
56
57      if (unw_step (&tmp) < 0)
58	abort ();
59
60      if (unw_get_reg (&tmp, UNW_REG_SP, &sp) < 0
61	  || unw_get_reg (&tmp, UNW_REG_SP, &prev_sp) < 0)
62	abort ();
63
64      if (sp == prev_sp)
65	/* got a NULL-frame; keep looking... */
66	return 0;
67    }
68  return 1;
69}
70
71/* On ia64 we cannot always call sigprocmask() at
72   _UI_siglongjmp_cont() because the signal may have switched stacks
73   and the old stack's register-backing store may have overflown,
74   leaving us no space to allocate the stacked registers needed to
75   call sigprocmask().  Fortunately, we can just let unw_resume() (via
76   sigreturn) take care of restoring the signal-mask.  That's faster
77   anyhow.  */
78static inline int
79resume_restores_sigmask (unw_cursor_t *c, unw_word_t *wp)
80{
81  unw_word_t sc_addr = ((struct cursor *) c)->sigcontext_addr;
82  struct sigcontext *sc = (struct sigcontext *) sc_addr;
83  sigset_t current_mask;
84  void *mp;
85
86  if (!sc_addr)
87    return 0;
88
89  /* let unw_resume() install the desired signal mask */
90
91  if (wp[JB_MASK_SAVED])
92    mp = &wp[JB_MASK];
93  else
94    {
95      if (sigprocmask (SIG_BLOCK, NULL, &current_mask) < 0)
96	abort ();
97      mp = &current_mask;
98    }
99  memcpy (&sc->sc_mask, mp, sizeof (sc->sc_mask));
100  return 1;
101}
102
103#else /* !UNW_TARGET_IA64 */
104
105static inline int
106bsp_match (unw_cursor_t *c, unw_word_t *wp)
107{
108  return 1;
109}
110
111static inline int
112resume_restores_sigmask (unw_cursor_t *c, unw_word_t *wp)
113{
114  /* We may want to do this analogously as for ia64... */
115  return 0;
116}
117
118#endif /* !UNW_TARGET_IA64 */
119