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