• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2002 Hewlett-Packard Co
3    Copyright (C) 2007 David Mosberger-Tang
4 	Contributed by David Mosberger-Tang <dmosberger@gmail.com>
5 
6 This file is part of libunwind.
7 
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15 
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
26 
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 
31 #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.  */
39 PROTECTED unw_addr_space_t unw_local_addr_space;
40 
41 #else /* !UNW_REMOTE_ONLY */
42 
43 static struct unw_addr_space local_addr_space;
44 
45 PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
46 
47 # ifdef UNW_LOCAL_ONLY
48 
49 HIDDEN void *
tdep_uc_addr(ucontext_t * uc,int reg)50 tdep_uc_addr (ucontext_t *uc, int reg)
51 {
52   return x86_r_uc_addr (uc, reg);
53 }
54 
55 # endif /* UNW_LOCAL_ONLY */
56 
57 HIDDEN 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 
64 static void
put_unwind_info(unw_addr_space_t as,unw_proc_info_t * proc_info,void * arg)65 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
66 {
67   /* it's a no-op */
68 }
69 
70 static int
get_dyn_info_list_addr(unw_addr_space_t as,unw_word_t * dyn_info_list_addr,void * arg)71 get_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 
78 /* ANDROID support update. */
79 #ifndef PAGE_SIZE
80 #define PAGE_SIZE 4096
81 #endif
82 /* End of ANDROID update. */
83 
84 #define PAGE_START(a)	((a) & ~(PAGE_SIZE-1))
85 
86 /* Cache of already validated addresses */
87 #define NLGA 4
88 static unw_word_t last_good_addr[NLGA];
89 static int lga_victim;
90 
91 static int
validate_mem(unw_word_t addr)92 validate_mem (unw_word_t addr)
93 {
94   int i, victim;
95 #ifdef HAVE_MINCORE
96   unsigned char mvec[2]; /* Unaligned access may cross page boundary */
97 #endif
98   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;
104 
105   addr = PAGE_START(addr);
106 
107   if (addr == 0)
108     return -1;
109 
110   for (i = 0; i < NLGA; i++)
111     {
112       if (last_good_addr[i] && (addr == last_good_addr[i]))
113 	return 0;
114     }
115 
116 #ifdef HAVE_MINCORE
117   if (mincore ((void *) addr, len, mvec) == -1)
118 #else
119   if (msync ((void *) addr, len, MS_ASYNC) == -1)
120 #endif
121     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 
140 static int
access_mem(unw_addr_space_t as,unw_word_t addr,unw_word_t * val,int write,void * arg)141 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
142 	    void *arg)
143 {
144   if (write)
145     {
146       /* ANDROID support update. */
147 #ifdef UNW_LOCAL_ONLY
148       if (map_local_is_writable (addr, sizeof(unw_word_t)))
149         {
150 #endif
151           Debug (16, "mem[%x] <- %x\n", addr, *val);
152           *(unw_word_t *) addr = *val;
153 #ifdef UNW_LOCAL_ONLY
154         }
155       else
156         {
157           Debug (16, "Unwritable memory mem[%x] <- %x\n", addr, *val);
158           return -1;
159         }
160 #endif
161       /* End of ANDROID update. */
162     }
163   else
164     {
165       /* ANDROID support update. */
166 #ifdef CONSERVATIVE_CHECKS
167       if (validate_mem(addr))
168         return -1;
169 #endif
170       /* End of ANDROID update. */
171 
172       /* ANDROID support update. */
173 #ifdef UNW_LOCAL_ONLY
174       if (map_local_is_readable (addr, sizeof(unw_word_t)))
175         {
176 #endif
177           *val = *(unw_word_t *) addr;
178           Debug (16, "mem[%x] -> %x\n", addr, *val);
179 #ifdef UNW_LOCAL_ONLY
180         }
181       else
182         {
183           Debug (16, "Unreadable memory mem[%x] -> XXX\n", addr);
184           return -1;
185         }
186 #endif
187       /* End of ANDROID update. */
188     }
189   return 0;
190 }
191 
192 static int
access_reg(unw_addr_space_t as,unw_regnum_t reg,unw_word_t * val,int write,void * arg)193 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
194 	    void *arg)
195 {
196   unw_word_t *addr;
197   ucontext_t *uc = ((struct cursor *)arg)->uc;
198 
199   if (unw_is_fpreg (reg))
200     goto badreg;
201 
202   if (!(addr = x86_r_uc_addr (uc, reg)))
203     goto badreg;
204 
205   if (write)
206     {
207       *(unw_word_t *) addr = *val;
208       Debug (12, "%s <- %x\n", unw_regname (reg), *val);
209     }
210   else
211     {
212       *val = *(unw_word_t *) addr;
213       Debug (12, "%s -> %x\n", unw_regname (reg), *val);
214     }
215   return 0;
216 
217  badreg:
218   Debug (1, "bad register number %u\n", reg);
219   return -UNW_EBADREG;
220 }
221 
222 static int
access_fpreg(unw_addr_space_t as,unw_regnum_t reg,unw_fpreg_t * val,int write,void * arg)223 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
224 	      int write, void *arg)
225 {
226   ucontext_t *uc = ((struct cursor *)arg)->uc;
227   unw_fpreg_t *addr;
228 
229   if (!unw_is_fpreg (reg))
230     goto badreg;
231 
232   if (!(addr = x86_r_uc_addr (uc, reg)))
233     goto badreg;
234 
235   if (write)
236     {
237       Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
238 	     ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
239       *(unw_fpreg_t *) addr = *val;
240     }
241   else
242     {
243       *val = *(unw_fpreg_t *) addr;
244       Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
245 	     ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
246     }
247   return 0;
248 
249  badreg:
250   Debug (1, "bad register number %u\n", reg);
251   /* attempt to access a non-preserved register */
252   return -UNW_EBADREG;
253 }
254 
255 static int
get_static_proc_name(unw_addr_space_t as,unw_word_t ip,char * buf,size_t buf_len,unw_word_t * offp,void * arg)256 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
257 		      char *buf, size_t buf_len, unw_word_t *offp,
258 		      void *arg)
259 {
260   return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp, arg);
261 }
262 
263 static int
access_mem_unrestricted(unw_addr_space_t as,unw_word_t addr,unw_word_t * val,int write,void * arg)264 access_mem_unrestricted (unw_addr_space_t as, unw_word_t addr, unw_word_t *val,
265                          int write, void *arg)
266 {
267   if (write)
268     return -1;
269 
270   *val = *(unw_word_t *) addr;
271   Debug (16, "mem[%x] -> %x\n", addr, *val);
272   return 0;
273 }
274 
275 // This initializes just enough of the address space to call the
276 // access memory function.
277 PROTECTED void
unw_local_access_addr_space_init(unw_addr_space_t as)278 unw_local_access_addr_space_init (unw_addr_space_t as)
279 {
280   memset (as, 0, sizeof (*as));
281   as->acc.access_mem = access_mem_unrestricted;
282 }
283 
284 HIDDEN void
x86_local_addr_space_init(void)285 x86_local_addr_space_init (void)
286 {
287   memset (&local_addr_space, 0, sizeof (local_addr_space));
288   local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
289   local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
290   local_addr_space.acc.put_unwind_info = put_unwind_info;
291   local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
292   local_addr_space.acc.access_mem = access_mem;
293   local_addr_space.acc.access_reg = access_reg;
294   local_addr_space.acc.access_fpreg = access_fpreg;
295   local_addr_space.acc.resume = x86_local_resume;
296   local_addr_space.acc.get_proc_name = get_static_proc_name;
297   unw_flush_cache (&local_addr_space, 0, 0);
298 
299   map_local_init ();
300 }
301 
302 #endif /* !UNW_REMOTE_ONLY */
303