1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2008 CodeSourcery
3 
4 This file is part of libunwind.
5 
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13 
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
24 
25 #ifndef MIPS_LIBUNWIND_I_H
26 #define MIPS_LIBUNWIND_I_H
27 
28 /* Target-dependent definitions that are internal to libunwind but need
29    to be shared with target-independent code.  */
30 
31 #include <stdlib.h>
32 #include <libunwind.h>
33 
34 #if !defined(UNW_REMOTE_ONLY) && _MIPS_SIM == _ABI64
35 # include "elf64.h"
36 #else
37 # include "elf32.h"
38 #endif
39 /* ANDROID support update. */
40 #include "map_info.h"
41 /* End of ANDROID update. */
42 #include "mempool.h"
43 #include "dwarf.h"
44 
45 typedef struct
46   {
47     /* no mips-specific fast trace */
48   }
49 unw_tdep_frame_t;
50 
51 struct unw_addr_space
52   {
53     struct unw_accessors acc;
54 
55     int big_endian;
56     mips_abi_t abi;
57     unsigned int addr_size;
58 
59     unw_caching_policy_t caching_policy;
60 #ifdef HAVE_ATOMIC_OPS_H
61     AO_t cache_generation;
62 #else
63     uint32_t cache_generation;
64 #endif
65     unw_word_t dyn_generation;		/* see dyn-common.h */
66     unw_word_t dyn_info_list_addr;	/* (cached) dyn_info_list_addr */
67     struct dwarf_rs_cache global_cache;
68     struct unw_debug_frame_list *debug_frames;
69     /* ANDROID support update. */
70     struct map_info *map_list;
71     /* End of ANDROID update. */
72 };
73 
74 #define tdep_big_endian(as)		((as)->big_endian)
75 
76 struct cursor
77   {
78     struct dwarf_cursor dwarf;		/* must be first */
79     unw_word_t sigcontext_addr;
80   };
81 
82 #define DWARF_GET_LOC(l)	((l).val)
83 
84 #ifndef UNW_REMOTE_ONLY
85 # if _MIPS_SIM == _ABIN32
86 typedef long long mips_reg_t;
87 # else
88 typedef long mips_reg_t;
89 # endif
90 #endif
91 
92 #ifdef UNW_LOCAL_ONLY
93 # define DWARF_NULL_LOC		DWARF_LOC (0, 0)
94 # define DWARF_IS_NULL_LOC(l)	(DWARF_GET_LOC (l) == 0)
95 # define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r) })
96 # define DWARF_IS_REG_LOC(l)	0
97 # define DWARF_REG_LOC(c,r)	(DWARF_LOC((unw_word_t) (intptr_t)	     \
98 				 tdep_uc_addr((c)->as_arg, (r)), 0))
99 # define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
100 # define DWARF_FPREG_LOC(c,r)	(DWARF_LOC((unw_word_t) (intptr_t)	     \
101 				 tdep_uc_addr((c)->as_arg, (r)), 0))
102 
103 /* FIXME: Implement these for the MIPS FPU.  */
104 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)105 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
106 {
107 /* ANDROID support update. */
108   unw_fpreg_t *addr = (unw_fpreg_t *) (uintptr_t) DWARF_GET_LOC (loc);
109   if (!addr || !map_local_is_readable ((unw_word_t) (uintptr_t) addr, sizeof(unw_fpreg_t)))
110     return -1;
111 
112   *val = *addr;
113   return 0;
114 /* End of ANDROID update. */
115 }
116 
117 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)118 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
119 {
120 /* ANDROID support update. */
121   unw_fpreg_t *addr = (unw_fpreg_t *) (uintptr_t) DWARF_GET_LOC (loc);
122   if (!addr || !map_local_is_writable ((unw_word_t) (uintptr_t) addr, sizeof(unw_fpreg_t)))
123     return -1;
124 
125   *addr = val;
126   return 0;
127 /* End of ANDROID update. */
128 }
129 
130 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)131 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
132 {
133 /* ANDROID support update. */
134   mips_reg_t *addr = (mips_reg_t *) (uintptr_t) DWARF_GET_LOC (loc);
135   if (!addr || !map_local_is_readable ((unw_word_t) (uintptr_t) addr, sizeof(mips_reg_t)))
136     return -1;
137 
138   *val = *addr;
139   return 0;
140 /* End of ANDROID update. */
141 }
142 
143 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)144 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
145 {
146 /* ANDROID support update. */
147   mips_reg_t *addr = (mips_reg_t *) (uintptr_t) DWARF_GET_LOC (loc);
148   if (!addr || !map_local_is_writable ((unw_word_t) (uintptr_t) addr, sizeof(mips_reg_t)))
149     return -1;
150 
151   *addr = val;
152   return 0;
153 /* End of ANDROID update. */
154 }
155 
156 #else /* !UNW_LOCAL_ONLY */
157 # define DWARF_LOC_TYPE_FP	(1 << 0)
158 # define DWARF_LOC_TYPE_REG	(1 << 1)
159 # define DWARF_NULL_LOC		DWARF_LOC (0, 0)
160 # define DWARF_IS_NULL_LOC(l)						\
161 		({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
162 # define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r), .type = (t) })
163 # define DWARF_IS_REG_LOC(l)	(((l).type & DWARF_LOC_TYPE_REG) != 0)
164 # define DWARF_IS_FP_LOC(l)	(((l).type & DWARF_LOC_TYPE_FP) != 0)
165 # define DWARF_REG_LOC(c,r)	DWARF_LOC((r), DWARF_LOC_TYPE_REG)
166 # define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
167 # define DWARF_FPREG_LOC(c,r)	DWARF_LOC((r), (DWARF_LOC_TYPE_REG	\
168 						| DWARF_LOC_TYPE_FP))
169 
170 static inline int
read_s32(struct dwarf_cursor * c,unw_word_t addr,unw_word_t * val)171 read_s32 (struct dwarf_cursor *c, unw_word_t addr, unw_word_t *val)
172 {
173   int offset = addr & 4;
174   int ret;
175   unw_word_t memval;
176 
177   ret = (*c->as->acc.access_mem) (c->as, addr - offset, &memval, 0, c->as_arg);
178   if (ret < 0)
179     return ret;
180 
181   if ((offset != 0) == tdep_big_endian (c->as))
182     *val = (int32_t) memval;
183   else
184     *val = (int32_t) (memval >> 32);
185 
186   return 0;
187 }
188 
189 static inline int
write_s32(struct dwarf_cursor * c,unw_word_t addr,const unw_word_t * val)190 write_s32 (struct dwarf_cursor *c, unw_word_t addr, const unw_word_t *val)
191 {
192   int offset = addr & 4;
193   int ret;
194   unw_word_t memval;
195 
196   ret = (*c->as->acc.access_mem) (c->as, addr - offset, &memval, 0, c->as_arg);
197   if (ret < 0)
198     return ret;
199 
200   if ((offset != 0) == tdep_big_endian (c->as))
201     memval = (memval & ~0xffffffffLL) | (uint32_t) *val;
202   else
203     memval = (memval & 0xffffffffLL) | (uint32_t) (*val << 32);
204 
205   return (*c->as->acc.access_mem) (c->as, addr - offset, &memval, 1, c->as_arg);
206 }
207 
208 /* FIXME: Implement these for the MIPS FPU.  */
209 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)210 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
211 {
212   char *valp = (char *) &val;
213   unw_word_t addr;
214   int ret;
215 
216   if (DWARF_IS_NULL_LOC (loc))
217     return -UNW_EBADREG;
218 
219   if (DWARF_IS_REG_LOC (loc))
220     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
221 				       val, 0, c->as_arg);
222 
223   addr = DWARF_GET_LOC (loc);
224   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
225 				       0, c->as_arg)) < 0)
226     return ret;
227 
228   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
229 				   c->as_arg);
230 }
231 
232 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)233 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
234 {
235   char *valp = (char *) &val;
236   unw_word_t addr;
237   int ret;
238 
239   if (DWARF_IS_NULL_LOC (loc))
240     return -UNW_EBADREG;
241 
242   if (DWARF_IS_REG_LOC (loc))
243     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
244 				       &val, 1, c->as_arg);
245 
246   addr = DWARF_GET_LOC (loc);
247   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
248 				       1, c->as_arg)) < 0)
249     return ret;
250 
251   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
252 				   1, c->as_arg);
253 }
254 
255 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)256 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
257 {
258   if (DWARF_IS_NULL_LOC (loc))
259     return -UNW_EBADREG;
260 
261   /* If a code-generator were to save a value of type unw_word_t in a
262      floating-point register, we would have to support this case.  I
263      suppose it could happen with MMX registers, but does it really
264      happen?  */
265   assert (!DWARF_IS_FP_LOC (loc));
266 
267   if (DWARF_IS_REG_LOC (loc))
268     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
269 				     0, c->as_arg);
270   else if (c->as->abi == UNW_MIPS_ABI_O32)
271     return read_s32 (c, DWARF_GET_LOC (loc), val);
272   else
273     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
274 				     0, c->as_arg);
275 }
276 
277 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)278 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
279 {
280   if (DWARF_IS_NULL_LOC (loc))
281     return -UNW_EBADREG;
282 
283   /* If a code-generator were to save a value of type unw_word_t in a
284      floating-point register, we would have to support this case.  I
285      suppose it could happen with MMX registers, but does it really
286      happen?  */
287   assert (!DWARF_IS_FP_LOC (loc));
288 
289   if (DWARF_IS_REG_LOC (loc))
290     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
291 				     1, c->as_arg);
292   else if (c->as->abi == UNW_MIPS_ABI_O32)
293     return write_s32 (c, DWARF_GET_LOC (loc), &val);
294   else
295     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
296 				     1, c->as_arg);
297 }
298 
299 #endif /* !UNW_LOCAL_ONLY */
300 
301 #define tdep_getcontext_trace           unw_getcontext
302 #define tdep_init_done			UNW_OBJ(init_done)
303 #define tdep_init			UNW_OBJ(init)
304 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
305    tdep_search_unwind_table.  */
306 #define tdep_search_unwind_table	dwarf_search_unwind_table
307 #define tdep_find_unwind_table		dwarf_find_unwind_table
308 #define tdep_uc_addr			UNW_ARCH_OBJ(uc_addr)
309 #define tdep_get_elf_image		UNW_ARCH_OBJ(get_elf_image)
310 #define tdep_access_reg			UNW_OBJ(access_reg)
311 #define tdep_access_fpreg		UNW_OBJ(access_fpreg)
312 #define tdep_fetch_frame(c,ip,n)	do {} while(0)
313 #define tdep_cache_frame(c,rs)		do {} while(0)
314 #define tdep_reuse_frame(c,rs)		do {} while(0)
315 #define tdep_stash_frame(c,rs)		do {} while(0)
316 #define tdep_trace(cur,addr,n)		(-UNW_ENOINFO)
317 
318 #ifdef UNW_LOCAL_ONLY
319 # define tdep_find_proc_info(c,ip,n)				\
320 	dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),	\
321 				       (c)->as_arg)
322 # define tdep_put_unwind_info(as,pi,arg)		\
323 	dwarf_put_unwind_info((as), (pi), (arg))
324 #else
325 # define tdep_find_proc_info(c,ip,n)					\
326 	(*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),	\
327 				       (c)->as_arg)
328 # define tdep_put_unwind_info(as,pi,arg)		\
329 	(*(as)->acc.put_unwind_info)((as), (pi), (arg))
330 #endif
331 
332 #define tdep_get_as(c)			((c)->dwarf.as)
333 #define tdep_get_as_arg(c)		((c)->dwarf.as_arg)
334 #define tdep_get_ip(c)			((c)->dwarf.ip)
335 
336 extern int tdep_init_done;
337 
338 extern void tdep_init (void);
339 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
340 				     unw_dyn_info_t *di, unw_proc_info_t *pi,
341 				     int need_unwind_info, void *arg);
342 extern void *tdep_uc_addr (ucontext_t *uc, int reg);
343 /* ANDROID support update. */
344 extern int tdep_get_elf_image (unw_addr_space_t as, struct elf_image *ei,
345 			       pid_t pid, unw_word_t ip,
346 			       unsigned long *segbase, unsigned long *mapoff,
347 			       char **path, void *as_arg);
348 /* End of ANDROID update. */
349 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
350 			    unw_word_t *valp, int write);
351 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
352 			      unw_fpreg_t *valp, int write);
353 
354 #endif /* MIPS_LIBUNWIND_I_H */
355