1/* DWARF2 EH unwinding support for IA64 Linux.
2   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3
4   This file is part of GCC.
5
6   GCC is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published
8   by the Free Software Foundation; either version 2, or (at your
9   option) any later version.
10
11   In addition to the permissions in the GNU General Public License,
12   the Free Software Foundation gives you unlimited permission to link
13   the compiled version of this file with other programs, and to
14   distribute those programs without any restriction coming from the
15   use of this file.  (The General Public License restrictions do
16   apply in other respects; for example, they cover modification of
17   the file, and distribution when not linked into another program.)
18
19   GCC is distributed in the hope that it will be useful, but WITHOUT
20   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
22   License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with GCC; see the file COPYING.  If not, write to the
26   Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
27   MA 02110-1301, USA.  */
28
29/* Do code reading to identify a signal frame, and set the frame
30   state data appropriately.  See unwind-dw2.c for the structs.  */
31
32/* This works only for glibc-2.3 and later, because sigcontext is different
33   in glibc-2.2.4.  */
34
35#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
36#include <signal.h>
37#include <sys/ucontext.h>
38
39#define IA64_GATE_AREA_START 0xa000000000000100LL
40#define IA64_GATE_AREA_END   0xa000000000030000LL
41
42#define MD_FALLBACK_FRAME_STATE_FOR ia64_fallback_frame_state
43
44static _Unwind_Reason_Code
45ia64_fallback_frame_state (struct _Unwind_Context *context,
46			   _Unwind_FrameState *fs)
47{
48  if (context->rp >= IA64_GATE_AREA_START
49      && context->rp < IA64_GATE_AREA_END)
50    {
51      struct sigframe {
52	char scratch[16];
53	unsigned long sig_number;
54	struct siginfo *info;
55	struct sigcontext *sc;
56      } *frame_ = (struct sigframe *)context->psp;
57      struct sigcontext *sc = frame_->sc;
58
59      /* Restore scratch registers in case the unwinder needs to
60	 refer to a value stored in one of them.  */
61      {
62	int i;
63
64	for (i = 2; i < 4; i++)
65	  context->ireg[i - 2].loc = &sc->sc_gr[i];
66	for (i = 8; i < 12; i++)
67	  context->ireg[i - 2].loc = &sc->sc_gr[i];
68	for (i = 14; i < 32; i++)
69	  context->ireg[i - 2].loc = &sc->sc_gr[i];
70      }
71
72      context->fpsr_loc = &(sc->sc_ar_fpsr);
73      context->pfs_loc = &(sc->sc_ar_pfs);
74      context->lc_loc = &(sc->sc_ar_lc);
75      context->unat_loc = &(sc->sc_ar_unat);
76      context->br_loc[0] = &(sc->sc_br[0]);
77      context->br_loc[6] = &(sc->sc_br[6]);
78      context->br_loc[7] = &(sc->sc_br[7]);
79      context->pr = sc->sc_pr;
80      context->psp = sc->sc_gr[12];
81      context->gp = sc->sc_gr[1];
82      /* Signal frame doesn't have an associated reg. stack frame
83         other than what we adjust for below.	  */
84      fs -> no_reg_stack_frame = 1;
85
86      if (sc->sc_rbs_base)
87	{
88	  /* Need to switch from alternate register backing store.  */
89	  long ndirty, loadrs = sc->sc_loadrs >> 16;
90	  unsigned long alt_bspstore = context->bsp - loadrs;
91	  unsigned long bspstore;
92	  unsigned long *ar_bsp = (unsigned long *)(sc->sc_ar_bsp);
93
94	  ndirty = ia64_rse_num_regs ((unsigned long *) alt_bspstore,
95				      (unsigned long *) context->bsp);
96	  bspstore = (unsigned long)
97	    ia64_rse_skip_regs (ar_bsp, -ndirty);
98	  ia64_copy_rbs (context, bspstore, alt_bspstore, loadrs,
99			 sc->sc_ar_rnat);
100	}
101
102      /* Don't touch the branch registers o.t. b0, b6 and b7.
103	 The kernel doesn't pass the preserved branch registers
104	 in the sigcontext but leaves them intact, so there's no
105	 need to do anything with them here.  */
106      {
107	unsigned long sof = sc->sc_cfm & 0x7f;
108	context->bsp = (unsigned long)
109	  ia64_rse_skip_regs ((unsigned long *)(sc->sc_ar_bsp), -sof);
110      }
111
112      fs->curr.reg[UNW_REG_RP].where = UNW_WHERE_SPREL;
113      fs->curr.reg[UNW_REG_RP].val
114	= (unsigned long)&(sc->sc_ip) - context->psp;
115      fs->curr.reg[UNW_REG_RP].when = -1;
116
117      return _URC_NO_REASON;
118    }
119  return _URC_END_OF_STACK;
120}
121
122#define MD_HANDLE_UNWABI ia64_handle_unwabi
123
124static void
125ia64_handle_unwabi (struct _Unwind_Context *context, _Unwind_FrameState *fs)
126{
127  if (fs->unwabi == ((3 << 8) | 's')
128      || fs->unwabi == ((0 << 8) | 's'))
129    {
130      struct sigframe {
131	char scratch[16];
132	unsigned long sig_number;
133	struct siginfo *info;
134	struct sigcontext *sc;
135      } *frame = (struct sigframe *)context->psp;
136      struct sigcontext *sc = frame->sc;
137
138      /* Restore scratch registers in case the unwinder needs to
139	 refer to a value stored in one of them.  */
140      {
141	int i;
142
143	for (i = 2; i < 4; i++)
144	  context->ireg[i - 2].loc = &sc->sc_gr[i];
145	for (i = 8; i < 12; i++)
146	  context->ireg[i - 2].loc = &sc->sc_gr[i];
147	for (i = 14; i < 32; i++)
148	  context->ireg[i - 2].loc = &sc->sc_gr[i];
149      }
150
151      context->pfs_loc = &(sc->sc_ar_pfs);
152      context->lc_loc = &(sc->sc_ar_lc);
153      context->unat_loc = &(sc->sc_ar_unat);
154      context->br_loc[0] = &(sc->sc_br[0]);
155      context->br_loc[6] = &(sc->sc_br[6]);
156      context->br_loc[7] = &(sc->sc_br[7]);
157      context->pr = sc->sc_pr;
158      context->gp = sc->sc_gr[1];
159      /* Signal frame doesn't have an associated reg. stack frame
160         other than what we adjust for below.	  */
161      fs -> no_reg_stack_frame = 1;
162
163      if (sc->sc_rbs_base)
164	{
165	  /* Need to switch from alternate register backing store.  */
166	  long ndirty, loadrs = sc->sc_loadrs >> 16;
167	  unsigned long alt_bspstore = context->bsp - loadrs;
168	  unsigned long bspstore;
169	  unsigned long *ar_bsp = (unsigned long *)(sc->sc_ar_bsp);
170
171	  ndirty = ia64_rse_num_regs ((unsigned long *) alt_bspstore,
172				      (unsigned long *) context->bsp);
173	  bspstore = (unsigned long) ia64_rse_skip_regs (ar_bsp, -ndirty);
174	  ia64_copy_rbs (context, bspstore, alt_bspstore, loadrs,
175			 sc->sc_ar_rnat);
176	}
177
178      /* Don't touch the branch registers o.t. b0, b6 and b7.
179	 The kernel doesn't pass the preserved branch registers
180	 in the sigcontext but leaves them intact, so there's no
181	 need to do anything with them here.  */
182      {
183	unsigned long sof = sc->sc_cfm & 0x7f;
184	context->bsp = (unsigned long)
185	  ia64_rse_skip_regs ((unsigned long *)(sc->sc_ar_bsp), -sof);
186      }
187
188      /* pfs_loc already set above.  Without this pfs_loc would point
189	 incorrectly to sc_cfm instead of sc_ar_pfs.  */
190      fs->curr.reg[UNW_REG_PFS].where = UNW_WHERE_NONE;
191    }
192}
193#endif /* glibc-2.3 or better */
194