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