1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2001-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 "offsets.h"
27#include "unwind_i.h"
28
29static inline int
30linux_sigtramp (struct cursor *c, ia64_loc_t prev_cfm_loc,
31                unw_word_t *num_regsp)
32{
33#if defined(UNW_LOCAL_ONLY) && !defined(__linux__)
34  return -UNW_EINVAL;
35#else
36  unw_word_t sc_addr;
37  int ret;
38
39  if ((ret = ia64_get (c, IA64_LOC_ADDR (c->sp + 0x10
40                                         + LINUX_SIGFRAME_ARG2_OFF, 0),
41                       &sc_addr)) < 0)
42    return ret;
43
44  c->sigcontext_addr = sc_addr;
45
46  if (!IA64_IS_REG_LOC (c->loc[IA64_REG_IP])
47      && IA64_GET_ADDR (c->loc[IA64_REG_IP]) == sc_addr + LINUX_SC_BR_OFF + 8)
48    {
49      /* Linux kernels before 2.4.19 and 2.5.10 had buggy
50         unwind info for sigtramp.  Fix it up here.  */
51      c->loc[IA64_REG_IP]  = IA64_LOC_ADDR (sc_addr + LINUX_SC_IP_OFF, 0);
52      c->cfm_loc = IA64_LOC_ADDR (sc_addr + LINUX_SC_CFM_OFF, 0);
53    }
54
55  /* do what can't be described by unwind directives: */
56  c->loc[IA64_REG_PFS] = IA64_LOC_ADDR (sc_addr + LINUX_SC_AR_PFS_OFF, 0);
57  c->ec_loc = prev_cfm_loc;
58  *num_regsp = c->cfm & 0x7f;           /* size of frame */
59  return 0;
60#endif
61}
62
63static inline int
64linux_interrupt (struct cursor *c, ia64_loc_t prev_cfm_loc,
65                 unw_word_t *num_regsp, int marker)
66{
67#if defined(UNW_LOCAL_ONLY) && !(defined(__linux__) && defined(__KERNEL__))
68  return -UNW_EINVAL;
69#else
70  unw_word_t sc_addr, num_regs;
71  ia64_loc_t pfs_loc;
72
73  sc_addr = c->sigcontext_addr = c->sp + 0x10;
74
75  if ((c->pr & (1UL << LINUX_PT_P_NONSYS)) != 0)
76    num_regs = c->cfm & 0x7f;
77  else
78    num_regs = 0;
79
80  /* do what can't be described by unwind directives: */
81  if (marker == ABI_MARKER_OLD_LINUX_INTERRUPT)
82          pfs_loc = IA64_LOC_ADDR (sc_addr + LINUX_OLD_PT_PFS_OFF, 0);
83  else
84          pfs_loc = IA64_LOC_ADDR (sc_addr + LINUX_PT_PFS_OFF, 0);
85  c->loc[IA64_REG_PFS] = pfs_loc;
86  c->ec_loc = prev_cfm_loc;
87  *num_regsp = num_regs;                /* size of frame */
88  return 0;
89#endif
90}
91
92static inline int
93hpux_sigtramp (struct cursor *c, ia64_loc_t prev_cfm_loc,
94               unw_word_t *num_regsp)
95{
96#if defined(UNW_LOCAL_ONLY) && !defined(__hpux)
97  return -UNW_EINVAL;
98#else
99  unw_word_t sc_addr, bsp, bspstore;
100  ia64_loc_t sc_loc;
101  int ret, i;
102
103  /* HP-UX passes the address of ucontext_t in r32: */
104  if ((ret = ia64_get_stacked (c, 32, &sc_loc, NULL)) < 0)
105    return ret;
106  if ((ret = ia64_get (c, sc_loc, &sc_addr)) < 0)
107    return ret;
108
109  c->sigcontext_addr = sc_addr;
110
111  /* Now mark all (preserved) registers as coming from the
112     signal context: */
113  c->cfm_loc = IA64_LOC_UC_REG (UNW_IA64_CFM, sc_addr);
114  c->loc[IA64_REG_PRI_UNAT_MEM] = IA64_NULL_LOC;
115  c->loc[IA64_REG_PSP] = IA64_LOC_UC_REG (UNW_IA64_GR + 12, sc_addr);
116  c->loc[IA64_REG_BSP] = IA64_LOC_UC_REG (UNW_IA64_AR_BSP, sc_addr);
117  c->loc[IA64_REG_BSPSTORE] = IA64_LOC_UC_REG (UNW_IA64_AR_BSPSTORE, sc_addr);
118  c->loc[IA64_REG_PFS] = IA64_LOC_UC_REG (UNW_IA64_AR_PFS, sc_addr);
119  c->loc[IA64_REG_RNAT] = IA64_LOC_UC_REG (UNW_IA64_AR_RNAT, sc_addr);
120  c->loc[IA64_REG_IP] = IA64_LOC_UC_REG (UNW_IA64_IP, sc_addr);
121  c->loc[IA64_REG_R4] = IA64_LOC_UC_REG (UNW_IA64_GR + 4, sc_addr);
122  c->loc[IA64_REG_R5] = IA64_LOC_UC_REG (UNW_IA64_GR + 5, sc_addr);
123  c->loc[IA64_REG_R6] = IA64_LOC_UC_REG (UNW_IA64_GR + 6, sc_addr);
124  c->loc[IA64_REG_R7] = IA64_LOC_UC_REG (UNW_IA64_GR + 7, sc_addr);
125  c->loc[IA64_REG_NAT4] = IA64_LOC_UC_REG (UNW_IA64_NAT + 4, sc_addr);
126  c->loc[IA64_REG_NAT5] = IA64_LOC_UC_REG (UNW_IA64_NAT + 5, sc_addr);
127  c->loc[IA64_REG_NAT6] = IA64_LOC_UC_REG (UNW_IA64_NAT + 6, sc_addr);
128  c->loc[IA64_REG_NAT7] = IA64_LOC_UC_REG (UNW_IA64_NAT + 7, sc_addr);
129  c->loc[IA64_REG_UNAT] = IA64_LOC_UC_REG (UNW_IA64_AR_UNAT, sc_addr);
130  c->loc[IA64_REG_PR] = IA64_LOC_UC_REG (UNW_IA64_PR, sc_addr);
131  c->loc[IA64_REG_LC] = IA64_LOC_UC_REG (UNW_IA64_AR_LC, sc_addr);
132  c->loc[IA64_REG_FPSR] = IA64_LOC_UC_REG (UNW_IA64_AR_FPSR, sc_addr);
133  c->loc[IA64_REG_B1] = IA64_LOC_UC_REG (UNW_IA64_BR + 1, sc_addr);
134  c->loc[IA64_REG_B2] = IA64_LOC_UC_REG (UNW_IA64_BR + 2, sc_addr);
135  c->loc[IA64_REG_B3] = IA64_LOC_UC_REG (UNW_IA64_BR + 3, sc_addr);
136  c->loc[IA64_REG_B4] = IA64_LOC_UC_REG (UNW_IA64_BR + 4, sc_addr);
137  c->loc[IA64_REG_B5] = IA64_LOC_UC_REG (UNW_IA64_BR + 5, sc_addr);
138  c->loc[IA64_REG_F2] = IA64_LOC_UC_REG (UNW_IA64_FR + 2, sc_addr);
139  c->loc[IA64_REG_F3] = IA64_LOC_UC_REG (UNW_IA64_FR + 3, sc_addr);
140  c->loc[IA64_REG_F4] = IA64_LOC_UC_REG (UNW_IA64_FR + 4, sc_addr);
141  c->loc[IA64_REG_F5] = IA64_LOC_UC_REG (UNW_IA64_FR + 5, sc_addr);
142  for (i = 0; i < 16; ++i)
143    c->loc[IA64_REG_F16 + i] = IA64_LOC_UC_REG (UNW_IA64_FR + 16 + i, sc_addr);
144
145  c->pi.flags |= UNW_PI_FLAG_IA64_RBS_SWITCH;
146
147  /* update the CFM cache: */
148  if ((ret = ia64_get (c, c->cfm_loc, &c->cfm)) < 0)
149    return ret;
150  /* update the PSP cache: */
151  if ((ret = ia64_get (c, c->loc[IA64_REG_PSP], &c->psp)) < 0)
152    return ret;
153
154  if ((ret = ia64_get (c, c->loc[IA64_REG_BSP], &bsp)) < 0
155      || (ret = ia64_get (c, c->loc[IA64_REG_BSPSTORE], &bspstore)) < 0)
156    return ret;
157  if (bspstore < bsp)
158    /* Dirty partition got spilled into the ucontext_t structure
159       itself.  We'll need to access it via uc_access(3).  */
160    rbs_switch (c, bsp, bspstore, IA64_LOC_UC_ADDR (bsp | 0x1f8, 0));
161
162  c->ec_loc = prev_cfm_loc;
163
164  *num_regsp = 0;
165  return 0;
166#endif
167}
168
169
170static inline int
171check_rbs_switch (struct cursor *c)
172{
173  unw_word_t saved_bsp, saved_bspstore, loadrs, ndirty;
174  int ret = 0;
175
176  saved_bsp = c->bsp;
177  if (c->pi.flags & UNW_PI_FLAG_IA64_RBS_SWITCH)
178    {
179      /* Got ourselves a frame that has saved ar.bspstore, ar.bsp,
180         and ar.rnat, so we're all set for rbs-switching:  */
181      if ((ret = ia64_get (c, c->loc[IA64_REG_BSP], &saved_bsp)) < 0
182          || (ret = ia64_get (c, c->loc[IA64_REG_BSPSTORE], &saved_bspstore)))
183        return ret;
184    }
185  else if ((c->abi_marker == ABI_MARKER_LINUX_SIGTRAMP
186            || c->abi_marker == ABI_MARKER_OLD_LINUX_SIGTRAMP)
187           && !IA64_IS_REG_LOC (c->loc[IA64_REG_BSP])
188           && (IA64_GET_ADDR (c->loc[IA64_REG_BSP])
189               == c->sigcontext_addr + LINUX_SC_AR_BSP_OFF))
190    {
191      /* When Linux delivers a signal on an alternate stack, it
192         does things a bit differently from what the unwind
193         conventions allow us to describe: instead of saving
194         ar.rnat, ar.bsp, and ar.bspstore, it saves the former two
195         plus the "loadrs" value.  Because of this, we need to
196         detect & record a potential rbs-area switch
197         manually... */
198
199      /* If ar.bsp has been saved already AND the current bsp is
200         not equal to the saved value, then we know for sure that
201         we're past the point where the backing store has been
202         switched (and before the point where it's restored).  */
203      if ((ret = ia64_get (c, IA64_LOC_ADDR (c->sigcontext_addr
204                                             + LINUX_SC_AR_BSP_OFF, 0),
205                           &saved_bsp) < 0)
206          || (ret = ia64_get (c, IA64_LOC_ADDR (c->sigcontext_addr
207                                                + LINUX_SC_LOADRS_OFF, 0),
208                              &loadrs) < 0))
209        return ret;
210      loadrs >>= 16;
211      ndirty = rse_num_regs (c->bsp - loadrs, c->bsp);
212      saved_bspstore = rse_skip_regs (saved_bsp, -ndirty);
213    }
214
215  if (saved_bsp == c->bsp)
216    return 0;
217
218  return rbs_switch (c, saved_bsp, saved_bspstore, c->loc[IA64_REG_RNAT]);
219}
220
221static inline int
222update_frame_state (struct cursor *c)
223{
224  unw_word_t prev_ip, prev_sp, prev_bsp, ip, num_regs;
225  ia64_loc_t prev_cfm_loc;
226  int ret;
227
228  prev_cfm_loc = c->cfm_loc;
229  prev_ip = c->ip;
230  prev_sp = c->sp;
231  prev_bsp = c->bsp;
232
233  /* Update the IP cache (do this first: if we reach the end of the
234     frame-chain, the rest of the info may not be valid/useful
235     anymore. */
236  ret = ia64_get (c, c->loc[IA64_REG_IP], &ip);
237  if (ret < 0)
238    return ret;
239  c->ip = ip;
240
241  if ((ip & 0xc) != 0)
242    {
243      /* don't let obviously bad addresses pollute the cache */
244      Debug (1, "rejecting bad ip=0x%lx\n", (long) c->ip);
245      return -UNW_EINVALIDIP;
246    }
247
248  c->cfm_loc = c->loc[IA64_REG_PFS];
249  /* update the CFM cache: */
250  ret = ia64_get (c, c->cfm_loc, &c->cfm);
251  if (ret < 0)
252    return ret;
253
254  /* Normally, AR.EC is stored in the CFM save-location.  That
255     save-location contains the full function-state as defined by
256     AR.PFS.  However, interruptions only save the frame-marker, not
257     any other info in CFM.  Instead, AR.EC gets saved on the first
258     call by the interruption-handler.  Thus, interruption-related
259     frames need to track the _previous_ CFM save-location since
260     that's were AR.EC is saved.  We support this by setting ec_loc to
261     cfm_loc by default and giving frames marked with an ABI-marker
262     the chance to override this value with prev_cfm_loc.  */
263  c->ec_loc = c->cfm_loc;
264
265  num_regs = 0;
266  if (unlikely (c->abi_marker))
267    {
268      c->last_abi_marker = c->abi_marker;
269      switch (ia64_get_abi_marker (c))
270        {
271        case ABI_MARKER_LINUX_SIGTRAMP:
272        case ABI_MARKER_OLD_LINUX_SIGTRAMP:
273          ia64_set_abi (c, ABI_LINUX);
274          if ((ret = linux_sigtramp (c, prev_cfm_loc, &num_regs)) < 0)
275            return ret;
276          break;
277
278        case ABI_MARKER_OLD_LINUX_INTERRUPT:
279        case ABI_MARKER_LINUX_INTERRUPT:
280          ia64_set_abi (c, ABI_LINUX);
281          if ((ret = linux_interrupt (c, prev_cfm_loc, &num_regs,
282                                      c->abi_marker)) < 0)
283            return ret;
284          break;
285
286        case ABI_MARKER_HP_UX_SIGTRAMP:
287          ia64_set_abi (c, ABI_HPUX);
288          if ((ret = hpux_sigtramp (c, prev_cfm_loc, &num_regs)) < 0)
289            return ret;
290          break;
291
292        default:
293          Debug (1, "unknown ABI marker: ABI=%u, context=%u\n",
294                 c->abi_marker >> 8, c->abi_marker & 0xff);
295          return -UNW_EINVAL;
296        }
297      Debug (12, "sigcontext_addr=%lx (ret=%d)\n",
298             (unsigned long) c->sigcontext_addr, ret);
299
300      c->sigcontext_off = c->sigcontext_addr - c->sp;
301
302      /* update the IP cache: */
303      if ((ret = ia64_get (c, c->loc[IA64_REG_IP], &ip)) < 0)
304        return ret;
305      c->ip = ip;
306      if (ip == 0)
307        /* end of frame-chain reached */
308        return 0;
309    }
310  else
311    num_regs = (c->cfm >> 7) & 0x7f;    /* size of locals */
312
313  if (!IA64_IS_NULL_LOC (c->loc[IA64_REG_BSP]))
314    {
315      ret = check_rbs_switch (c);
316      if (ret < 0)
317        return ret;
318    }
319
320  c->bsp = rse_skip_regs (c->bsp, -num_regs);
321
322  c->sp = c->psp;
323  c->abi_marker = 0;
324
325  if (c->ip == prev_ip && c->sp == prev_sp && c->bsp == prev_bsp)
326    {
327      Dprintf ("%s: ip, sp, and bsp unchanged; stopping here (ip=0x%lx)\n",
328               __FUNCTION__, (long) ip);
329      return -UNW_EBADFRAME;
330    }
331
332  /* as we unwind, the saved ar.unat becomes the primary unat: */
333  c->loc[IA64_REG_PRI_UNAT_MEM] = c->loc[IA64_REG_UNAT];
334
335  /* restore the predicates: */
336  ret = ia64_get (c, c->loc[IA64_REG_PR], &c->pr);
337  if (ret < 0)
338    return ret;
339
340  c->pi_valid = 0;
341  return 0;
342}
343
344
345int
346unw_step (unw_cursor_t *cursor)
347{
348  struct cursor *c = (struct cursor *) cursor;
349  int ret;
350
351  Debug (1, "(cursor=%p, ip=0x%016lx)\n", c, (unsigned long) c->ip);
352
353  if ((ret = ia64_find_save_locs (c)) >= 0
354      && (ret = update_frame_state (c)) >= 0)
355    ret = (c->ip == 0) ? 0 : 1;
356
357  Debug (2, "returning %d (ip=0x%016lx)\n", ret, (unsigned long) c->ip);
358  return ret;
359}
360