blob: 9205fb43d08795dd3a98ae263785fbac3dceed7f [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
27#include <stdlib.h>
28#include <string.h>
29
30#include "unwind_i.h"
31
32#ifdef UNW_REMOTE_ONLY
33
34/* unw_local_addr_space is a NULL pointer in this case. */
35PROTECTED unw_addr_space_t unw_local_addr_space;
36
37#else /* !UNW_REMOTE_ONLY */
38
39static struct unw_addr_space local_addr_space;
40
41PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
42
43static inline void *
44uc_addr (ucontext_t *uc, int reg)
45{
46 void *addr;
47
48 switch (reg)
49 {
50 case UNW_X86_GS: addr = &uc->uc_mcontext.gregs[REG_GS]; break;
51 case UNW_X86_FS: addr = &uc->uc_mcontext.gregs[REG_FS]; break;
52 case UNW_X86_ES: addr = &uc->uc_mcontext.gregs[REG_ES]; break;
53 case UNW_X86_DS: addr = &uc->uc_mcontext.gregs[REG_DS]; break;
54 case UNW_X86_EAX: addr = &uc->uc_mcontext.gregs[REG_EAX]; break;
55 case UNW_X86_EBX: addr = &uc->uc_mcontext.gregs[REG_EBX]; break;
56 case UNW_X86_ECX: addr = &uc->uc_mcontext.gregs[REG_ECX]; break;
57 case UNW_X86_EDX: addr = &uc->uc_mcontext.gregs[REG_EDX]; break;
58 case UNW_X86_ESI: addr = &uc->uc_mcontext.gregs[REG_ESI]; break;
59 case UNW_X86_EDI: addr = &uc->uc_mcontext.gregs[REG_EDI]; break;
60 case UNW_X86_EBP: addr = &uc->uc_mcontext.gregs[REG_EBP]; break;
61 case UNW_X86_EIP: addr = &uc->uc_mcontext.gregs[REG_EIP]; break;
62 case UNW_X86_ESP: addr = &uc->uc_mcontext.gregs[REG_ESP]; break;
63 case UNW_X86_TRAPNO: addr = &uc->uc_mcontext.gregs[REG_TRAPNO]; break;
64 case UNW_X86_CS: addr = &uc->uc_mcontext.gregs[REG_CS]; break;
65 case UNW_X86_EFLAGS: addr = &uc->uc_mcontext.gregs[REG_EFL]; break;
66 case UNW_X86_SS: addr = &uc->uc_mcontext.gregs[REG_SS]; break;
67
68 default:
69 addr = NULL;
70 }
71 return addr;
72}
73
74# ifdef UNW_LOCAL_ONLY
75
76HIDDEN void *
77tdep_uc_addr (ucontext_t *uc, int reg)
78{
79 return uc_addr (uc, reg);
80}
81
82# endif /* UNW_LOCAL_ONLY */
83
84HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
85
86/* XXX fix me: there is currently no way to locate the dyn-info list
87 by a remote unwinder. On ia64, this is done via a special
88 unwind-table entry. Perhaps something similar can be done with
89 DWARF2 unwind info. */
90
91static void
92put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
93{
94 /* it's a no-op */
95}
96
97static int
98get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
99 void *arg)
100{
101 *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
102 return 0;
103}
104
Arun Sharmaff0ae702009-03-16 11:06:26 -0700105#define PAGE_SIZE 4096
106#define PAGE_START(a) ((a) & ~(PAGE_SIZE-1))
107
108/* Cache of already validated addresses */
109#define NLGA 4
110static unw_word_t last_good_addr[NLGA];
111static int lga_victim;
112
113static int
114validate_mem (unw_word_t addr)
115{
116 int i, victim;
117
118 addr = PAGE_START(addr);
119
Paul Pluzhnikov0cf76ed2009-12-01 13:59:45 -0800120 if (addr == 0)
121 return -1;
122
Arun Sharmaff0ae702009-03-16 11:06:26 -0700123 for (i = 0; i < NLGA; i++)
124 {
125 if (last_good_addr[i] && (addr == last_good_addr[i]))
126 return 0;
127 }
128
Arun Sharma3468a6b2010-02-23 10:35:47 -0800129 if (msync ((void *) addr, 1, MS_ASYNC) == -1)
Arun Sharmaff0ae702009-03-16 11:06:26 -0700130 return -1;
131
132 victim = lga_victim;
133 for (i = 0; i < NLGA; i++) {
134 if (!last_good_addr[victim]) {
135 last_good_addr[victim++] = addr;
136 return 0;
137 }
138 victim = (victim + 1) % NLGA;
139 }
140
141 /* All slots full. Evict the victim. */
142 last_good_addr[victim] = addr;
143 victim = (victim + 1) % NLGA;
144 lga_victim = victim;
145
146 return 0;
147}
148
homeip.net!davidm588192d2004-08-17 15:34:28 +0000149static int
150access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
151 void *arg)
152{
153 if (write)
154 {
155 Debug (16, "mem[%x] <- %x\n", addr, *val);
156 *(unw_word_t *) addr = *val;
157 }
158 else
159 {
Arun Sharmaff0ae702009-03-16 11:06:26 -0700160 /* validate address */
161 const struct cursor *c = (const struct cursor *)arg;
162 if (c && c->validate && validate_mem(addr))
163 return -1;
homeip.net!davidm588192d2004-08-17 15:34:28 +0000164 *val = *(unw_word_t *) addr;
165 Debug (16, "mem[%x] -> %x\n", addr, *val);
166 }
167 return 0;
168}
169
170static int
171access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
172 void *arg)
173{
174 unw_word_t *addr;
Arun Sharmaff0ae702009-03-16 11:06:26 -0700175 ucontext_t *uc = ((struct cursor *)arg)->uc;
homeip.net!davidm588192d2004-08-17 15:34:28 +0000176
177 if (unw_is_fpreg (reg))
178 goto badreg;
179
homeip.net!davidm588192d2004-08-17 15:34:28 +0000180 if (!(addr = uc_addr (uc, reg)))
181 goto badreg;
182
183 if (write)
184 {
185 *(unw_word_t *) addr = *val;
186 Debug (12, "%s <- %x\n", unw_regname (reg), *val);
187 }
188 else
189 {
190 *val = *(unw_word_t *) addr;
191 Debug (12, "%s -> %x\n", unw_regname (reg), *val);
192 }
193 return 0;
194
195 badreg:
196 Debug (1, "bad register number %u\n", reg);
197 return -UNW_EBADREG;
198}
199
200static int
201access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
202 int write, void *arg)
203{
Arun Sharmaff0ae702009-03-16 11:06:26 -0700204 ucontext_t *uc = ((struct cursor *)arg)->uc;
homeip.net!davidm588192d2004-08-17 15:34:28 +0000205 unw_fpreg_t *addr;
206
207 if (!unw_is_fpreg (reg))
208 goto badreg;
209
210 if (!(addr = uc_addr (uc, reg)))
211 goto badreg;
212
213 if (write)
214 {
215 Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
216 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
217 *(unw_fpreg_t *) addr = *val;
218 }
219 else
220 {
221 *val = *(unw_fpreg_t *) addr;
222 Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
223 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
224 }
225 return 0;
226
227 badreg:
228 Debug (1, "bad register number %u\n", reg);
229 /* attempt to access a non-preserved register */
230 return -UNW_EBADREG;
231}
232
233static int
234get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
235 char *buf, size_t buf_len, unw_word_t *offp,
236 void *arg)
237{
David Mosberger-Tange6b9f352007-08-22 13:02:09 -0600238 return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
homeip.net!davidm588192d2004-08-17 15:34:28 +0000239}
240
241HIDDEN void
242x86_local_addr_space_init (void)
243{
244 memset (&local_addr_space, 0, sizeof (local_addr_space));
245 local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
246 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
247 local_addr_space.acc.put_unwind_info = put_unwind_info;
248 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
249 local_addr_space.acc.access_mem = access_mem;
250 local_addr_space.acc.access_reg = access_reg;
251 local_addr_space.acc.access_fpreg = access_fpreg;
252 local_addr_space.acc.resume = x86_local_resume;
253 local_addr_space.acc.get_proc_name = get_static_proc_name;
254 unw_flush_cache (&local_addr_space, 0, 0);
255}
256
257#endif /* !UNW_REMOTE_ONLY */