blob: eced09abf8f6a356dbd5ea7b068605ffee05d92d [file] [log] [blame]
Jose Flavio Aguilar Paulinob33021e2007-08-02 09:59:43 -06001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2006-2007 IBM
3 Contributed by
4 Corey Ashford <cjashfor@us.ibm.com>
5 Jose Flavio Aguilar Paulino <jflavio@br.ibm.com> <joseflavio@gmail.com>
6
7This file is part of libunwind.
8
9Permission is hereby granted, free of charge, to any person obtaining
10a copy of this software and associated documentation files (the
11"Software"), to deal in the Software without restriction, including
12without limitation the rights to use, copy, modify, merge, publish,
13distribute, sublicense, and/or sell copies of the Software, and to
14permit persons to whom the Software is furnished to do so, subject to
15the following conditions:
16
17The above copyright notice and this permission notice shall be
18included in all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
27
28#include <stdlib.h>
29#include <string.h>
30
31#include "ucontext_i.h"
32#include "unwind_i.h"
33
34#ifdef UNW_REMOTE_ONLY
35
36/* unw_local_addr_space is a NULL pointer in this case. */
37PROTECTED unw_addr_space_t unw_local_addr_space;
38
39#else /* !UNW_REMOTE_ONLY */
40
41static struct unw_addr_space local_addr_space;
42
43PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
44
Jose Flavio Aguilar Paulinob33021e2007-08-02 09:59:43 -060045static void *
46uc_addr (ucontext_t *uc, int reg)
47{
48 void *addr;
49
50 if ((unsigned) (reg - UNW_PPC64_R0) < 32)
51 addr = &uc->uc_mcontext.gp_regs[reg - UNW_PPC64_R0];
52
53 else if ((unsigned) (reg - UNW_PPC64_F0) < 32)
54 addr = &uc->uc_mcontext.fp_regs[reg - UNW_PPC64_F0];
55
56 else if ((unsigned) (reg - UNW_PPC64_V0) < 32)
57 addr = (uc->uc_mcontext.v_regs == 0) ? NULL : &uc->uc_mcontext.v_regs->vrregs[reg - UNW_PPC64_V0][0];
58
59 else
60 {
61 unsigned gregs_idx;
62
63 switch (reg)
64 {
65 case UNW_PPC64_NIP:
66 gregs_idx = NIP_IDX;
67 break;
68 case UNW_PPC64_CTR:
69 gregs_idx = CTR_IDX;
70 break;
71 case UNW_PPC64_LR:
72 gregs_idx = LINK_IDX;
73 break;
74 case UNW_PPC64_XER:
75 gregs_idx = XER_IDX;
76 break;
77 case UNW_PPC64_CR0:
78 gregs_idx = CCR_IDX;
79 break;
80 default:
81 return NULL;
82 }
83 addr = &uc->uc_mcontext.gp_regs[gregs_idx];
84 }
85 return addr;
86}
87
88# ifdef UNW_LOCAL_ONLY
89
90HIDDEN void *
91tdep_uc_addr (ucontext_t *uc, int reg)
92{
93 return uc_addr (uc, reg);
94}
95
96# endif /* UNW_LOCAL_ONLY */
97
98HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
99
100
101static void
102put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
103{
104 /* it's a no-op */
105}
106
107static int
108get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
109 void *arg)
110{
111 *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
112 return 0;
113}
114
115static int
116access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
117 void *arg)
118{
119 if (write)
120 {
Christopher Ferris7d46a212013-11-14 14:03:33 -0800121 /* ANDROID support update. */
Christopher Ferriscdf9ee52013-11-22 21:09:24 -0800122#ifdef UNW_LOCAL_ONLY
Christopher Ferris7d46a212013-11-14 14:03:33 -0800123 if (maps_is_writable(as->map_list, addr))
124 {
Christopher Ferriscdf9ee52013-11-22 21:09:24 -0800125#endif
Christopher Ferris7d46a212013-11-14 14:03:33 -0800126 Debug (12, "mem[%lx] <- %lx\n", addr, *val);
127 *(unw_word_t *) addr = *val;
Christopher Ferriscdf9ee52013-11-22 21:09:24 -0800128#ifdef UNW_LOCAL_ONLY
Christopher Ferris7d46a212013-11-14 14:03:33 -0800129 }
130 else
131 {
132 Debug (12, "Unwritable memory mem[%lx] <- %lx\n", addr, *val);
133 return -1;
134 }
Christopher Ferriscdf9ee52013-11-22 21:09:24 -0800135#endif
Christopher Ferris7d46a212013-11-14 14:03:33 -0800136 /* End of ANDROID update. */
Jose Flavio Aguilar Paulinob33021e2007-08-02 09:59:43 -0600137 }
138 else
139 {
Christopher Ferris7d46a212013-11-14 14:03:33 -0800140 /* ANDROID support update. */
Christopher Ferriscdf9ee52013-11-22 21:09:24 -0800141#ifdef UNW_LOCAL_ONLY
Christopher Ferris7d46a212013-11-14 14:03:33 -0800142 if (maps_is_readable(as->map_list, addr))
143 {
Christopher Ferriscdf9ee52013-11-22 21:09:24 -0800144#endif
Christopher Ferris7d46a212013-11-14 14:03:33 -0800145 *val = *(unw_word_t *) addr;
146 Debug (12, "mem[%lx] -> %lx\n", addr, *val);
Christopher Ferriscdf9ee52013-11-22 21:09:24 -0800147#ifdef UNW_LOCAL_ONLY
Christopher Ferris7d46a212013-11-14 14:03:33 -0800148 }
149 else
150 {
151 Debug (12, "Unreadable memory mem[%lx] -> XXX\n", addr);
152 return -1;
153 }
Christopher Ferriscdf9ee52013-11-22 21:09:24 -0800154#endif
Christopher Ferris7d46a212013-11-14 14:03:33 -0800155 /* End of ANDROID update. */
Jose Flavio Aguilar Paulinob33021e2007-08-02 09:59:43 -0600156 }
157 return 0;
158}
159
160static int
161access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
162 int write, void *arg)
163{
164 unw_word_t *addr;
165 ucontext_t *uc = arg;
166
Cody P Schafer0abc36e2012-09-14 17:11:57 -0700167 if (UNW_PPC64_F0 <= reg && reg <= UNW_PPC64_F31)
Jose Flavio Aguilar Paulinob33021e2007-08-02 09:59:43 -0600168 goto badreg;
Cody P Schafer0abc36e2012-09-14 17:11:57 -0700169 if (UNW_PPC64_V0 <= reg && reg <= UNW_PPC64_V31)
Jose Flavio Aguilar Paulinob33021e2007-08-02 09:59:43 -0600170 goto badreg;
171
172 addr = uc_addr (uc, reg);
173 if (!addr)
174 goto badreg;
175
176 if (write)
177 {
178 *(unw_word_t *) addr = *val;
179 Debug (12, "%s <- %lx\n", unw_regname (reg), *val);
180 }
181 else
182 {
183 *val = *(unw_word_t *) addr;
184 Debug (12, "%s -> %lx\n", unw_regname (reg), *val);
185 }
186 return 0;
187
188badreg:
189 Debug (1, "bad register number %u\n", reg);
190 return -UNW_EBADREG;
191}
192
193static int
194access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
195 int write, void *arg)
196{
197 ucontext_t *uc = arg;
198 unw_fpreg_t *addr;
199
200 if ((unsigned) (reg - UNW_PPC64_F0) < 0)
201 goto badreg;
202
203 if ((unsigned) (reg - UNW_PPC64_V0) >= 32)
204 goto badreg;
205
206
207 addr = uc_addr (uc, reg);
208 if (!addr)
209 goto badreg;
210
211 if (write)
212 {
213 Debug (12, "%s <- %016Lf\n", unw_regname (reg), *val);
214 *(unw_fpreg_t *) addr = *val;
215 }
216 else
217 {
218 *val = *(unw_fpreg_t *) addr;
219 Debug (12, "%s -> %016Lf\n", unw_regname (reg), *val);
220 }
221 return 0;
222
223badreg:
224 Debug (1, "bad register number %u\n", reg);
225 /* attempt to access a non-preserved register */
226 return -UNW_EBADREG;
227}
228
229static int
230get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
231 char *buf, size_t buf_len, unw_word_t *offp,
232 void *arg)
233{
David Mosberger-Tange6b9f352007-08-22 13:02:09 -0600234 return _Uelf64_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
Jose Flavio Aguilar Paulinob33021e2007-08-02 09:59:43 -0600235}
236
237HIDDEN void
238ppc64_local_addr_space_init (void)
239{
240 memset (&local_addr_space, 0, sizeof (local_addr_space));
241 local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
242 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
243 local_addr_space.acc.put_unwind_info = put_unwind_info;
244 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
245 local_addr_space.acc.access_mem = access_mem;
246 local_addr_space.acc.access_reg = access_reg;
247 local_addr_space.acc.access_fpreg = access_fpreg;
248 local_addr_space.acc.resume = ppc64_local_resume;
249 local_addr_space.acc.get_proc_name = get_static_proc_name;
250 unw_flush_cache (&local_addr_space, 0, 0);
251}
252
253#endif /* !UNW_REMOTE_ONLY */