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 /* Add For Cache MAP And ELF */
44 #include "map_info.h"
45 /* Add For Cache MAP And ELF */
46 #include "mempool.h"
47 #include "dwarf.h"
48
49 typedef struct
50 {
51 /* no ppc64-specific fast trace */
52 }
53 unw_tdep_frame_t;
54
55 struct unw_addr_space
56 {
57 struct unw_accessors acc;
58 int big_endian;
59 ppc64_abi_t abi;
60 unw_caching_policy_t caching_policy;
61 _Atomic uint32_t cache_generation;
62 unw_word_t dyn_generation; /* see dyn-common.h */
63 unw_word_t dyn_info_list_addr; /* (cached) dyn_info_list_addr */
64 struct dwarf_rs_cache global_cache;
65 struct unw_debug_frame_list *debug_frames;
66 int validate;
67 /* Add For Cache MAP And ELF */
68 struct map_info *map_list;
69 /* Add For Cache MAP And ELF */
70 };
71
72 struct cursor
73 {
74 struct dwarf_cursor dwarf; /* must be first */
75
76 /* Format of sigcontext structure and address at which it is
77 stored: */
78 enum
79 {
80 PPC_SCF_NONE, /* no signal frame encountered */
81 PPC_SCF_LINUX_RT_SIGFRAME /* POSIX ucontext_t */
82 }
83 sigcontext_format;
84 unw_word_t sigcontext_addr;
85 };
86
87 #define DWARF_GET_LOC(l) ((l).val)
88
89 #ifdef UNW_LOCAL_ONLY
90 # define DWARF_NULL_LOC DWARF_LOC (0, 0)
91 # define DWARF_IS_NULL_LOC(l) (DWARF_GET_LOC (l) == 0)
92 # define DWARF_LOC(r, t) ((dwarf_loc_t) { .val = (r) })
93 # define DWARF_IS_REG_LOC(l) 0
94 # define DWARF_REG_LOC(c,r) (DWARF_LOC((unw_word_t) \
95 tdep_uc_addr((c)->as_arg, (r)), 0))
96 # define DWARF_MEM_LOC(c,m) DWARF_LOC ((m), 0)
97 # define DWARF_FPREG_LOC(c,r) (DWARF_LOC((unw_word_t) \
98 tdep_uc_addr((c)->as_arg, (r)), 0))
99 # define DWARF_VREG_LOC(c,r) (DWARF_LOC((unw_word_t) \
100 tdep_uc_addr((c)->as_arg, (r)), 0))
101
102 static inline int
dwarf_getvr(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)103 dwarf_getvr (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t * val)
104 {
105 if (!DWARF_GET_LOC (loc))
106 return -1;
107 *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
108 return 0;
109 }
110
111 static inline int
dwarf_putvr(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)112 dwarf_putvr (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
113 {
114 if (!DWARF_GET_LOC (loc))
115 return -1;
116 *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
117 return 0;
118 }
119
120 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)121 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
122 {
123 if (!DWARF_GET_LOC (loc))
124 return -1;
125 *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
126 return 0;
127 }
128
129 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)130 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
131 {
132 if (!DWARF_GET_LOC (loc))
133 return -1;
134 *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
135 return 0;
136 }
137
138 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)139 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
140 {
141 if (!DWARF_GET_LOC (loc))
142 return -1;
143 *val = *(unw_word_t *) DWARF_GET_LOC (loc);
144 return 0;
145 }
146
147 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)148 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
149 {
150 if (!DWARF_GET_LOC (loc))
151 return -1;
152 *(unw_word_t *) DWARF_GET_LOC (loc) = val;
153 return 0;
154 }
155
156 #else /* !UNW_LOCAL_ONLY */
157
158 # define DWARF_LOC_TYPE_FP (1 << 0)
159 # define DWARF_LOC_TYPE_REG (1 << 1)
160 # define DWARF_LOC_TYPE_V (1 << 2)
161 # define DWARF_NULL_LOC DWARF_LOC (0, 0)
162 # define DWARF_IS_NULL_LOC(l) \
163 ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
164 # define DWARF_LOC(r, t) ((dwarf_loc_t) { .val = (r), .type = (t) })
165 # define DWARF_IS_REG_LOC(l) (((l).type & DWARF_LOC_TYPE_REG) != 0)
166 # define DWARF_IS_FP_LOC(l) (((l).type & DWARF_LOC_TYPE_FP) != 0)
167 # define DWARF_IS_V_LOC(l) (((l).type & DWARF_LOC_TYPE_V) != 0)
168 # define DWARF_MEM_LOC(c,m) DWARF_LOC ((m), 0)
169 # define DWARF_REG_LOC(c,r) DWARF_LOC((r), DWARF_LOC_TYPE_REG)
170 # define DWARF_FPREG_LOC(c,r) DWARF_LOC((r), (DWARF_LOC_TYPE_REG \
171 | DWARF_LOC_TYPE_FP))
172 # define DWARF_VREG_LOC(c,r) DWARF_LOC((r), (DWARF_LOC_TYPE_REG \
173 | DWARF_LOC_TYPE_V))
174
175 static inline int
dwarf_getvr(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)176 dwarf_getvr (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t * val)
177 {
178 unw_word_t *valp = (unw_word_t *) val;
179 unw_word_t addr;
180 int ret;
181
182 if (DWARF_IS_NULL_LOC (loc))
183 return -UNW_EBADREG;
184
185 assert (DWARF_IS_V_LOC (loc));
186 assert (!DWARF_IS_FP_LOC (loc));
187
188 if (DWARF_IS_REG_LOC (loc))
189 return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
190 val, 0, c->as_arg);
191
192 addr = DWARF_GET_LOC (loc);
193
194 if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, valp,
195 0, c->as_arg)) < 0)
196 return ret;
197
198 return (*c->as->acc.access_mem) (c->as, addr + 8, valp + 1, 0, c->as_arg);
199 }
200
201 static inline int
dwarf_putvr(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)202 dwarf_putvr (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
203 {
204 unw_word_t *valp = (unw_word_t *) & val;
205 unw_word_t addr;
206 int ret;
207
208 if (DWARF_IS_NULL_LOC (loc))
209 return -UNW_EBADREG;
210
211 assert (DWARF_IS_V_LOC (loc));
212 assert (!DWARF_IS_FP_LOC (loc));
213
214 if (DWARF_IS_REG_LOC (loc))
215 return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
216 &val, 1, c->as_arg);
217
218 addr = DWARF_GET_LOC (loc);
219 if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, valp,
220 1, c->as_arg)) < 0)
221 return ret;
222
223 return (*c->as->acc.access_mem) (c->as, addr + 8, valp + 1, 1, c->as_arg);
224 }
225
226 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)227 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t * val)
228 {
229 unw_word_t *valp = (unw_word_t *) val;
230 unw_word_t addr;
231
232 if (DWARF_IS_NULL_LOC (loc))
233 return -UNW_EBADREG;
234
235 assert (DWARF_IS_FP_LOC (loc));
236 assert (!DWARF_IS_V_LOC (loc));
237
238 if (DWARF_IS_REG_LOC (loc))
239 return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
240 val, 0, c->as_arg);
241
242 addr = DWARF_GET_LOC (loc);
243 return (*c->as->acc.access_mem) (c->as, addr + 0, valp, 0, c->as_arg);
244
245 }
246
247 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)248 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
249 {
250 unw_word_t *valp = (unw_word_t *) & val;
251 unw_word_t addr;
252
253 if (DWARF_IS_NULL_LOC (loc))
254 return -UNW_EBADREG;
255
256 assert (DWARF_IS_FP_LOC (loc));
257 assert (!DWARF_IS_V_LOC (loc));
258
259 if (DWARF_IS_REG_LOC (loc))
260 return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
261 &val, 1, c->as_arg);
262
263 addr = DWARF_GET_LOC (loc);
264
265 return (*c->as->acc.access_mem) (c->as, addr + 0, valp, 1, c->as_arg);
266 }
267
268 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)269 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t * val)
270 {
271 if (DWARF_IS_NULL_LOC (loc))
272 return -UNW_EBADREG;
273
274 /* If a code-generator were to save a value of type unw_word_t in a
275 floating-point register, we would have to support this case. I
276 suppose it could happen with MMX registers, but does it really
277 happen? */
278 assert (!DWARF_IS_FP_LOC (loc));
279 assert (!DWARF_IS_V_LOC (loc));
280
281 if (DWARF_IS_REG_LOC (loc))
282 return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
283 0, c->as_arg);
284 else
285 return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
286 0, c->as_arg);
287 }
288
289 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)290 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
291 {
292 if (DWARF_IS_NULL_LOC (loc))
293 return -UNW_EBADREG;
294
295 /* If a code-generator were to save a value of type unw_word_t in a
296 floating-point register, we would have to support this case. I
297 suppose it could happen with MMX registers, but does it really
298 happen? */
299 assert (!DWARF_IS_FP_LOC (loc));
300 assert (!DWARF_IS_V_LOC (loc));
301
302 if (DWARF_IS_REG_LOC (loc))
303 return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
304 1, c->as_arg);
305 else
306 return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
307 1, c->as_arg);
308 }
309
310 #endif /* !UNW_LOCAL_ONLY */
311
312 #define tdep_getcontext_trace unw_getcontext
313 #define tdep_init_done UNW_OBJ(init_done)
314 #define tdep_init UNW_OBJ(init)
315 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
316 tdep_search_unwind_table. */
317 #define tdep_search_unwind_table dwarf_search_unwind_table
318 #define tdep_find_unwind_table dwarf_find_unwind_table
319 #define tdep_uc_addr UNW_ARCH_OBJ(uc_addr)
320 #define tdep_get_elf_image UNW_ARCH_OBJ(get_elf_image)
321 #define tdep_get_exe_image_path UNW_ARCH_OBJ(get_exe_image_path)
322 #define tdep_access_reg UNW_OBJ(access_reg)
323 #define tdep_access_fpreg UNW_OBJ(access_fpreg)
324 #define tdep_fetch_frame(c,ip,n) do {} while(0)
325 #define tdep_cache_frame(c) 0
326 #define tdep_reuse_frame(c,frame) do {} while(0)
327 #define tdep_stash_frame(c,rs) do {} while(0)
328 #define tdep_trace(cur,addr,n) (-UNW_ENOINFO)
329 #define tdep_get_func_addr UNW_OBJ(get_func_addr)
330
331 #ifdef UNW_LOCAL_ONLY
332 # define tdep_find_proc_info(c,ip,n) \
333 dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n), \
334 (c)->as_arg)
335 # define tdep_put_unwind_info(as,pi,arg) \
336 dwarf_put_unwind_info((as), (pi), (arg))
337 #else
338 # define tdep_find_proc_info(c,ip,n) \
339 (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n), \
340 (c)->as_arg)
341 # define tdep_put_unwind_info(as,pi,arg) \
342 (*(as)->acc.put_unwind_info)((as), (pi), (arg))
343 #endif
344
345 extern int tdep_fetch_proc_info_post (struct dwarf_cursor *c, unw_word_t ip,
346 int need_unwind_info);
347
348 #define tdep_get_as(c) ((c)->dwarf.as)
349 #define tdep_get_as_arg(c) ((c)->dwarf.as_arg)
350 #define tdep_get_ip(c) ((c)->dwarf.ip)
351 #define tdep_big_endian(as) ((as)->big_endian)
352
353 extern atomic_bool tdep_init_done;
354
355 extern void tdep_init (void);
356 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
357 unw_dyn_info_t * di,
358 unw_proc_info_t * pi,
359 int need_unwind_info, void *arg);
360 extern void *tdep_uc_addr (ucontext_t * uc, int reg);
361 /* Add For Cache MAP And ELF */
362 extern struct map_info *tdep_get_elf_image (unw_addr_space_t as, pid_t pid,
363 unw_word_t ip);
364 /* Add For Cache MAP And ELF */
365 extern void tdep_get_exe_image_path (char *path);
366 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
367 unw_word_t * valp, int write);
368 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
369 unw_fpreg_t * valp, int write);
370 extern int tdep_get_func_addr (unw_addr_space_t as, unw_word_t addr,
371 unw_word_t *entry_point);
372
373 #endif /* PPC64_LIBUNWIND_I_H */
374