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