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