blob: 37a8b8f9b04b83f304ad8f87315026e2cc33feb9 [file] [log] [blame]
mostang.com!davidm997f0252002-12-19 07:16:50 +00001/* libunwind - a platform-independent unwind library
mostang.com!davidmea0a71a2003-01-21 17:41:20 +00002 Copyright (C) 2001-2003 Hewlett-Packard Co
mostang.com!davidm997f0252002-12-19 07:16:50 +00003 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 <stdlib.h>
27
28#include "rse.h"
29#include "unwind_i.h"
mostang.com!davidmea0a71a2003-01-21 17:41:20 +000030#include "offsets.h"
mostang.com!davidm997f0252002-12-19 07:16:50 +000031
32#ifndef UNW_REMOTE_ONLY
33
mostang.com!davidme4f251b2003-03-28 07:43:22 +000034static inline int
35local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
mostang.com!davidm997f0252002-12-19 07:16:50 +000036{
mostang.com!davidm3d3510c2003-04-24 20:45:07 +000037#if defined(__linux)
mostang.com!davidmb8c17812003-02-15 08:49:55 +000038 unw_word_t val, sol, sof, pri_unat, n, pfs;
mostang.com!davidm997f0252002-12-19 07:16:50 +000039 struct cursor *c = (struct cursor *) cursor;
mostang.com!davidm678d3202003-02-08 10:10:59 +000040 struct
41 {
42 unw_word_t r1;
43 unw_word_t r4;
44 unw_word_t r5;
45 unw_word_t r6;
46 unw_word_t r7;
47 unw_word_t r15;
48 unw_word_t r16;
49 unw_word_t r17;
50 unw_word_t r18;
51 }
52 extra;
53 int ret;
54# define GET_NAT(n) \
mostang.com!davidm997f0252002-12-19 07:16:50 +000055 do \
56 { \
57 ret = ia64_access_reg (c, UNW_IA64_NAT + (n), &val, 0); \
58 if (ret < 0) \
59 return ret; \
60 if (val) \
mostang.com!davidm678d3202003-02-08 10:10:59 +000061 pri_unat |= (unw_word_t) 1 << n; \
mostang.com!davidm997f0252002-12-19 07:16:50 +000062 } \
63 while (0)
mostang.com!davidm997f0252002-12-19 07:16:50 +000064
65 /* ensure c->pi is up-to-date: */
66 if ((ret = ia64_make_proc_info (c)) < 0)
67 return ret;
68
mostang.com!davidm678d3202003-02-08 10:10:59 +000069 /* Copy contents of r4-r7 into "extra", so that their values end up
70 contiguous, so we can use a single (primary-) UNaT value. */
mostang.com!davidmc1d25572003-04-23 05:56:59 +000071 if ((ret = ia64_get (c, c->loc[IA64_REG_R4], &extra.r4)) < 0
72 || (ret = ia64_get (c, c->loc[IA64_REG_R5], &extra.r5)) < 0
73 || (ret = ia64_get (c, c->loc[IA64_REG_R6], &extra.r6)) < 0
74 || (ret = ia64_get (c, c->loc[IA64_REG_R7], &extra.r7)) < 0)
mostang.com!davidm678d3202003-02-08 10:10:59 +000075 return ret;
mostang.com!davidm997f0252002-12-19 07:16:50 +000076
mostang.com!davidm678d3202003-02-08 10:10:59 +000077 /* Form the primary UNaT value: */
78 pri_unat = 0;
79 GET_NAT (4); GET_NAT(5);
80 GET_NAT (6); GET_NAT(7);
81 n = (((uintptr_t) &extra.r4) / 8 - 4) % 64;
82 pri_unat = (pri_unat << n) | (pri_unat >> (64 - n));
mostang.com!davidm997f0252002-12-19 07:16:50 +000083
mostang.com!davidmc1d25572003-04-23 05:56:59 +000084 if (unlikely (c->sigcontext_addr))
mostang.com!davidmea0a71a2003-01-21 17:41:20 +000085 {
mostang.com!davidmc1d25572003-04-23 05:56:59 +000086 struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
mostang.com!davidm678d3202003-02-08 10:10:59 +000087# define PR_SCRATCH 0xffc0 /* p6-p15 are scratch */
88# define PR_PRESERVED (~(PR_SCRATCH | 1))
mostang.com!davidmea0a71a2003-01-21 17:41:20 +000089
90 /* We're returning to a frame that was (either directly or
91 indirectly) interrupted by a signal. We have to restore
92 _both_ "preserved" and "scratch" registers. That doesn't
93 leave us any registers to work with, and the only way we can
94 achieve this is by doing a sigreturn().
95
96 Note: it might be tempting to think that we don't have to
97 restore the scratch registers when returning to a frame that
98 was indirectly interrupted by a signal. However, that is not
99 safe because that frame and its descendants could have been
100 using a special convention that stores "preserved" state in
mostang.com!davidm678d3202003-02-08 10:10:59 +0000101 scratch registers. For example, the Linux fsyscall
102 convention does this with r11 (to save ar.pfs) and b6 (to
103 save "rp"). */
mostang.com!davidmea0a71a2003-01-21 17:41:20 +0000104
mostang.com!davidm678d3202003-02-08 10:10:59 +0000105 sc->sc_gr[12] = c->psp;
mostang.com!davidmc1d25572003-04-23 05:56:59 +0000106 c->psp = c->sigcontext_addr - c->sigcontext_off;
mostang.com!davidm678d3202003-02-08 10:10:59 +0000107
108 sof = (c->cfm & 0x7f);
mostang.com!davidm02c8c0c2003-02-14 06:25:36 +0000109 rbs_cover_and_flush (c, sof);
mostang.com!davidm678d3202003-02-08 10:10:59 +0000110
111 sc->sc_ip = c->ip;
112 sc->sc_cfm = c->cfm & (((unw_word_t) 1 << 38) - 1);
113 sc->sc_pr = (c->pr & ~PR_SCRATCH) | (sc->sc_pr & ~PR_PRESERVED);
mostang.com!davidmc1d25572003-04-23 05:56:59 +0000114 if ((ret = ia64_get (c, c->loc[IA64_REG_PFS], &sc->sc_ar_pfs)) < 0
115 || (ret = ia64_get (c, c->loc[IA64_REG_FPSR], &sc->sc_ar_fpsr)) < 0
116 || (ret = ia64_get (c, c->loc[IA64_REG_UNAT], &sc->sc_ar_unat)) < 0)
mostang.com!davidm678d3202003-02-08 10:10:59 +0000117 return ret;
118
119 sc->sc_gr[1] = c->pi.gp;
120 if (c->eh_valid_mask & 0x1) sc->sc_gr[15] = c->eh_args[0];
121 if (c->eh_valid_mask & 0x2) sc->sc_gr[16] = c->eh_args[1];
122 if (c->eh_valid_mask & 0x4) sc->sc_gr[17] = c->eh_args[2];
123 if (c->eh_valid_mask & 0x8) sc->sc_gr[18] = c->eh_args[3];
mostang.com!davidmea0a71a2003-01-21 17:41:20 +0000124 }
125 else
126 {
mostang.com!davidm678d3202003-02-08 10:10:59 +0000127 /* Account for the fact that _Uia64_install_context() will
128 return via br.ret, which will decrement bsp by size-of-locals. */
mostang.com!davidmc1d25572003-04-23 05:56:59 +0000129 if ((ret = ia64_get (c, c->loc[IA64_REG_PFS], &pfs)) < 0)
mostang.com!davidm678d3202003-02-08 10:10:59 +0000130 return ret;
131 sol = (pfs >> 7) & 0x7f;
mostang.com!davidm02c8c0c2003-02-14 06:25:36 +0000132 rbs_cover_and_flush (c, sol);
mostang.com!davidm678d3202003-02-08 10:10:59 +0000133
134 extra.r1 = c->pi.gp;
135 extra.r15 = c->eh_args[0];
136 extra.r16 = c->eh_args[1];
137 extra.r17 = c->eh_args[2];
138 extra.r18 = c->eh_args[3];
mostang.com!davidmea0a71a2003-01-21 17:41:20 +0000139 }
mostang.com!davidm6c584b72003-04-23 19:22:42 +0000140 ia64_install_cursor (c, pri_unat, (unw_word_t *) &extra);
mostang.com!davidm3d3510c2003-04-24 20:45:07 +0000141#elif defined(__hpux)
142 struct cursor *c = (struct cursor *) cursor;
143
144 setcontext (c->as_arg); /* should not return */
145#endif
146 return -UNW_EINVAL;
mostang.com!davidm997f0252002-12-19 07:16:50 +0000147}
148
mostang.com!davidme4f251b2003-03-28 07:43:22 +0000149HIDDEN int
150ia64_local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
151{
mostang.com!davidm6c584b72003-04-23 19:22:42 +0000152 return local_resume (as, cursor, arg);
mostang.com!davidme4f251b2003-03-28 07:43:22 +0000153}
154
mostang.com!davidm997f0252002-12-19 07:16:50 +0000155#endif /* !UNW_REMOTE_ONLY */
156
mostang.com!davidm678d3202003-02-08 10:10:59 +0000157#ifndef UNW_LOCAL_ONLY
158
159static inline int
160remote_install_cursor (struct cursor *c)
161{
hp.com!davidm2b0b48f2003-02-22 03:08:22 +0000162 int (*access_reg) (unw_addr_space_t, unw_regnum_t, unw_word_t *,
163 int write, void *);
164 int (*access_fpreg) (unw_addr_space_t, unw_regnum_t, unw_fpreg_t *,
165 int write, void *);
166 unw_fpreg_t fpval;
167 unw_word_t val;
168 int reg;
169
mostang.com!davidm3d3510c2003-04-24 20:45:07 +0000170#ifdef __linux
hp.com!davidm2b0b48f2003-02-22 03:08:22 +0000171 if (c->as == unw_local_addr_space)
172 {
173 /* Take a short-cut: we directly resume out of the cursor and
174 all we need to do is make sure that all locations point to
175 memory, not registers. Furthermore, R4-R7 and NAT4-NAT7 are
176 taken care of by ia64_local_resume() so they don't need to be
177 handled here. */
mostang.com!davidm6c584b72003-04-23 19:22:42 +0000178# define MEMIFY(preg, reg) \
179 do { \
180 if (IA64_IS_REG_LOC (c->loc[(preg)])) \
181 c->loc[(preg)] = IA64_LOC_ADDR((unw_word_t) \
182 tdep_uc_addr(c->as_arg, (reg)), 0); \
hp.com!davidm2b0b48f2003-02-22 03:08:22 +0000183 } while (0)
184 MEMIFY (IA64_REG_PR, UNW_IA64_PR);
185 MEMIFY (IA64_REG_PFS, UNW_IA64_AR_PFS);
186 MEMIFY (IA64_REG_RNAT, UNW_IA64_AR_RNAT);
187 MEMIFY (IA64_REG_UNAT, UNW_IA64_AR_UNAT);
188 MEMIFY (IA64_REG_LC, UNW_IA64_AR_LC);
189 MEMIFY (IA64_REG_FPSR, UNW_IA64_AR_FPSR);
mostang.com!davidmc1d25572003-04-23 05:56:59 +0000190 MEMIFY (IA64_REG_IP, UNW_IA64_BR + 0);
hp.com!davidm2b0b48f2003-02-22 03:08:22 +0000191 MEMIFY (IA64_REG_B1, UNW_IA64_BR + 1);
192 MEMIFY (IA64_REG_B2, UNW_IA64_BR + 2);
193 MEMIFY (IA64_REG_B3, UNW_IA64_BR + 3);
194 MEMIFY (IA64_REG_B4, UNW_IA64_BR + 4);
195 MEMIFY (IA64_REG_B5, UNW_IA64_BR + 5);
196 MEMIFY (IA64_REG_F2, UNW_IA64_FR + 2);
197 MEMIFY (IA64_REG_F3, UNW_IA64_FR + 3);
198 MEMIFY (IA64_REG_F4, UNW_IA64_FR + 4);
199 MEMIFY (IA64_REG_F5, UNW_IA64_FR + 5);
200 MEMIFY (IA64_REG_F16, UNW_IA64_FR + 16);
201 MEMIFY (IA64_REG_F17, UNW_IA64_FR + 17);
202 MEMIFY (IA64_REG_F18, UNW_IA64_FR + 18);
203 MEMIFY (IA64_REG_F19, UNW_IA64_FR + 19);
204 MEMIFY (IA64_REG_F20, UNW_IA64_FR + 20);
205 MEMIFY (IA64_REG_F21, UNW_IA64_FR + 21);
206 MEMIFY (IA64_REG_F22, UNW_IA64_FR + 22);
207 MEMIFY (IA64_REG_F23, UNW_IA64_FR + 23);
208 MEMIFY (IA64_REG_F24, UNW_IA64_FR + 24);
209 MEMIFY (IA64_REG_F25, UNW_IA64_FR + 25);
210 MEMIFY (IA64_REG_F26, UNW_IA64_FR + 26);
211 MEMIFY (IA64_REG_F27, UNW_IA64_FR + 27);
212 MEMIFY (IA64_REG_F28, UNW_IA64_FR + 28);
213 MEMIFY (IA64_REG_F29, UNW_IA64_FR + 29);
214 MEMIFY (IA64_REG_F30, UNW_IA64_FR + 30);
215 MEMIFY (IA64_REG_F31, UNW_IA64_FR + 31);
216 }
217 else
mostang.com!davidm3d3510c2003-04-24 20:45:07 +0000218#endif /* __linux */
hp.com!davidm2b0b48f2003-02-22 03:08:22 +0000219 {
220 access_reg = c->as->acc.access_reg;
221 access_fpreg = c->as->acc.access_fpreg;
222
mostang.com!davidm3d3510c2003-04-24 20:45:07 +0000223 debug (1, "%s: copying out cursor state\n", __FUNCTION__);
224
hp.com!davidm2b0b48f2003-02-22 03:08:22 +0000225 for (reg = 0; reg < UNW_REG_LAST; ++reg)
226 {
227 if (unw_is_fpreg (reg))
228 {
mostang.com!davidm3d3510c2003-04-24 20:45:07 +0000229 if (ia64_access_fpreg (c, reg, &fpval, 0) >= 0)
hp.com!davidm2b0b48f2003-02-22 03:08:22 +0000230 (*access_fpreg) (c->as, reg, &fpval, 1, c->as_arg);
231 }
232 else
233 {
mostang.com!davidm3d3510c2003-04-24 20:45:07 +0000234 if (ia64_access_reg (c, reg, &val, 0) >= 0)
hp.com!davidm2b0b48f2003-02-22 03:08:22 +0000235 (*access_reg) (c->as, reg, &val, 1, c->as_arg);
236 }
237 }
238 }
239 return (*c->as->acc.resume) (c->as, (unw_cursor_t *) c, c->as_arg);
mostang.com!davidm678d3202003-02-08 10:10:59 +0000240}
241
mostang.com!davidm6c584b72003-04-23 19:22:42 +0000242#endif /* !UNW_LOCAL_ONLY */
mostang.com!davidm678d3202003-02-08 10:10:59 +0000243
mostang.com!davidm997f0252002-12-19 07:16:50 +0000244int
245unw_resume (unw_cursor_t *cursor)
246{
247 struct cursor *c = (struct cursor *) cursor;
248
249#ifdef UNW_LOCAL_ONLY
mostang.com!davidme4f251b2003-03-28 07:43:22 +0000250 return local_resume (c->as, cursor, c->as_arg);
mostang.com!davidm997f0252002-12-19 07:16:50 +0000251#else
mostang.com!davidm3d3510c2003-04-24 20:45:07 +0000252 return remote_install_cursor (c);
mostang.com!davidm997f0252002-12-19 07:16:50 +0000253#endif
254}