blob: 0995f7174ade875eeea887cf475e0229d782d173 [file] [log] [blame]
homeip.net!davidm588192d2004-08-17 15:34:28 +00001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2002 Hewlett-Packard Co
David Mosberger-Tange6b9f352007-08-22 13:02:09 -06003 Copyright (C) 2007 David Mosberger-Tang
4 Contributed by David Mosberger-Tang <dmosberger@gmail.com>
homeip.net!davidm588192d2004-08-17 15:34:28 +00005
6This file is part of libunwind.
7
8Permission is hereby granted, free of charge, to any person obtaining
9a copy of this software and associated documentation files (the
10"Software"), to deal in the Software without restriction, including
11without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense, and/or sell copies of the Software, and to
13permit persons to whom the Software is furnished to do so, subject to
14the following conditions:
15
16The above copyright notice and this permission notice shall be
17included in all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
26
Konstantin Belousovee99dbe2010-04-20 17:45:18 +030027#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
homeip.net!davidm588192d2004-08-17 15:34:28 +000031#include <stdlib.h>
32#include <string.h>
33
34#include "unwind_i.h"
35
36#ifdef UNW_REMOTE_ONLY
37
38/* unw_local_addr_space is a NULL pointer in this case. */
39PROTECTED unw_addr_space_t unw_local_addr_space;
40
41#else /* !UNW_REMOTE_ONLY */
42
43static struct unw_addr_space local_addr_space;
44
45PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
46
homeip.net!davidm588192d2004-08-17 15:34:28 +000047# ifdef UNW_LOCAL_ONLY
48
49HIDDEN void *
50tdep_uc_addr (ucontext_t *uc, int reg)
51{
Konstantin Belousov0dbeeeb2010-04-05 22:42:23 +030052 return x86_r_uc_addr (uc, reg);
homeip.net!davidm588192d2004-08-17 15:34:28 +000053}
54
55# endif /* UNW_LOCAL_ONLY */
56
57HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
58
59/* XXX fix me: there is currently no way to locate the dyn-info list
60 by a remote unwinder. On ia64, this is done via a special
61 unwind-table entry. Perhaps something similar can be done with
62 DWARF2 unwind info. */
63
64static void
65put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
66{
67 /* it's a no-op */
68}
69
70static int
71get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
72 void *arg)
73{
74 *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
75 return 0;
76}
77
Christopher Ferrisefb75a02013-10-01 12:38:42 -070078/* ANDROID support update. */
79#ifndef PAGE_SIZE
Arun Sharmaff0ae702009-03-16 11:06:26 -070080#define PAGE_SIZE 4096
Christopher Ferrisefb75a02013-10-01 12:38:42 -070081#endif
82/* End of ANDROID update. */
83
Arun Sharmaff0ae702009-03-16 11:06:26 -070084#define PAGE_START(a) ((a) & ~(PAGE_SIZE-1))
85
86/* Cache of already validated addresses */
87#define NLGA 4
88static unw_word_t last_good_addr[NLGA];
89static int lga_victim;
90
91static int
92validate_mem (unw_word_t addr)
93{
94 int i, victim;
Konstantin Belousovee99dbe2010-04-20 17:45:18 +030095#ifdef HAVE_MINCORE
Arun Sharma99e60be2010-06-08 14:44:07 -070096 unsigned char mvec[2]; /* Unaligned access may cross page boundary */
Konstantin Belousovee99dbe2010-04-20 17:45:18 +030097#endif
Arun Sharma99e60be2010-06-08 14:44:07 -070098 size_t len;
99
100 if (PAGE_START(addr + sizeof (unw_word_t) - 1) == PAGE_START(addr))
101 len = PAGE_SIZE;
102 else
103 len = PAGE_SIZE * 2;
Arun Sharmaff0ae702009-03-16 11:06:26 -0700104
105 addr = PAGE_START(addr);
106
Paul Pluzhnikov0cf76ed2009-12-01 13:59:45 -0800107 if (addr == 0)
108 return -1;
109
Arun Sharmaff0ae702009-03-16 11:06:26 -0700110 for (i = 0; i < NLGA; i++)
111 {
112 if (last_good_addr[i] && (addr == last_good_addr[i]))
113 return 0;
114 }
115
Konstantin Belousovee99dbe2010-04-20 17:45:18 +0300116#ifdef HAVE_MINCORE
Arun Sharma99e60be2010-06-08 14:44:07 -0700117 if (mincore ((void *) addr, len, mvec) == -1)
Konstantin Belousovee99dbe2010-04-20 17:45:18 +0300118#else
Arun Sharma99e60be2010-06-08 14:44:07 -0700119 if (msync ((void *) addr, len, MS_ASYNC) == -1)
Konstantin Belousovee99dbe2010-04-20 17:45:18 +0300120#endif
Arun Sharmaff0ae702009-03-16 11:06:26 -0700121 return -1;
122
123 victim = lga_victim;
124 for (i = 0; i < NLGA; i++) {
125 if (!last_good_addr[victim]) {
126 last_good_addr[victim++] = addr;
127 return 0;
128 }
129 victim = (victim + 1) % NLGA;
130 }
131
132 /* All slots full. Evict the victim. */
133 last_good_addr[victim] = addr;
134 victim = (victim + 1) % NLGA;
135 lga_victim = victim;
136
137 return 0;
138}
139
homeip.net!davidm588192d2004-08-17 15:34:28 +0000140static int
141access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
142 void *arg)
143{
144 if (write)
145 {
Christopher Ferris7d46a212013-11-14 14:03:33 -0800146 /* ANDROID support update. */
147 if (maps_is_writable(as->map_list, addr))
148 {
149 Debug (16, "mem[%x] <- %x\n", addr, *val);
150 *(unw_word_t *) addr = *val;
151 }
152 else
153 {
154 Debug (16, "Unwritable memory mem[%x] <- %x\n", addr, *val);
155 return -1;
156 }
157 /* End of ANDROID update. */
homeip.net!davidm588192d2004-08-17 15:34:28 +0000158 }
159 else
160 {
Arun Sharmaff0ae702009-03-16 11:06:26 -0700161 /* validate address */
162 const struct cursor *c = (const struct cursor *)arg;
163 if (c && c->validate && validate_mem(addr))
164 return -1;
Christopher Ferris7d46a212013-11-14 14:03:33 -0800165 /* ANDROID support update. */
166 if (maps_is_readable(as->map_list, addr))
167 {
168 *val = *(unw_word_t *) addr;
169 Debug (16, "mem[%x] -> %x\n", addr, *val);
170 }
171 else
172 {
173 Debug (16, "Unreadable memory mem[%x] -> XXX\n", addr);
174 return -1;
175 }
176 /* End of ANDROID update. */
homeip.net!davidm588192d2004-08-17 15:34:28 +0000177 }
178 return 0;
179}
180
181static int
182access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
183 void *arg)
184{
185 unw_word_t *addr;
Arun Sharmaff0ae702009-03-16 11:06:26 -0700186 ucontext_t *uc = ((struct cursor *)arg)->uc;
homeip.net!davidm588192d2004-08-17 15:34:28 +0000187
188 if (unw_is_fpreg (reg))
189 goto badreg;
190
Konstantin Belousov0dbeeeb2010-04-05 22:42:23 +0300191 if (!(addr = x86_r_uc_addr (uc, reg)))
homeip.net!davidm588192d2004-08-17 15:34:28 +0000192 goto badreg;
193
194 if (write)
195 {
196 *(unw_word_t *) addr = *val;
197 Debug (12, "%s <- %x\n", unw_regname (reg), *val);
198 }
199 else
200 {
201 *val = *(unw_word_t *) addr;
202 Debug (12, "%s -> %x\n", unw_regname (reg), *val);
203 }
204 return 0;
205
206 badreg:
207 Debug (1, "bad register number %u\n", reg);
208 return -UNW_EBADREG;
209}
210
211static int
212access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
213 int write, void *arg)
214{
Arun Sharmaff0ae702009-03-16 11:06:26 -0700215 ucontext_t *uc = ((struct cursor *)arg)->uc;
homeip.net!davidm588192d2004-08-17 15:34:28 +0000216 unw_fpreg_t *addr;
217
218 if (!unw_is_fpreg (reg))
219 goto badreg;
220
Konstantin Belousov0dbeeeb2010-04-05 22:42:23 +0300221 if (!(addr = x86_r_uc_addr (uc, reg)))
homeip.net!davidm588192d2004-08-17 15:34:28 +0000222 goto badreg;
223
224 if (write)
225 {
226 Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
227 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
228 *(unw_fpreg_t *) addr = *val;
229 }
230 else
231 {
232 *val = *(unw_fpreg_t *) addr;
233 Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
234 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
235 }
236 return 0;
237
238 badreg:
239 Debug (1, "bad register number %u\n", reg);
240 /* attempt to access a non-preserved register */
241 return -UNW_EBADREG;
242}
243
244static int
245get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
246 char *buf, size_t buf_len, unw_word_t *offp,
247 void *arg)
248{
David Mosberger-Tange6b9f352007-08-22 13:02:09 -0600249 return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
homeip.net!davidm588192d2004-08-17 15:34:28 +0000250}
251
Christopher Ferris7d46a212013-11-14 14:03:33 -0800252/* ANDROID support update. */
253static define_lock (_U_map_init_lock);
254static struct map_info *_U_map_list = NULL;
255/* End of ANDROID update. */
256
homeip.net!davidm588192d2004-08-17 15:34:28 +0000257HIDDEN void
258x86_local_addr_space_init (void)
259{
260 memset (&local_addr_space, 0, sizeof (local_addr_space));
261 local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
262 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
263 local_addr_space.acc.put_unwind_info = put_unwind_info;
264 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
265 local_addr_space.acc.access_mem = access_mem;
266 local_addr_space.acc.access_reg = access_reg;
267 local_addr_space.acc.access_fpreg = access_fpreg;
268 local_addr_space.acc.resume = x86_local_resume;
269 local_addr_space.acc.get_proc_name = get_static_proc_name;
270 unw_flush_cache (&local_addr_space, 0, 0);
Christopher Ferris7d46a212013-11-14 14:03:33 -0800271
272 /* ANDROID support update. */
273 mutex_lock (&_U_map_init_lock);
274 if (_U_map_list == NULL)
275 {
276 _U_map_list = maps_create_list(getpid());
277 }
278 mutex_unlock (&_U_map_init_lock);
279 local_addr_space.map_list = _U_map_list;
280 /* End of ANDROID update. */
homeip.net!davidm588192d2004-08-17 15:34:28 +0000281}
282
283#endif /* !UNW_REMOTE_ONLY */