ca2815500ca8a02212c7d8c80a31000358a79262
[platform/upstream/libunwind.git] / include / tdep-aarch64 / libunwind_i.h
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2001-2005 Hewlett-Packard Co
3         Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4    Copyright (C) 2013 Linaro Limited
5
6 This file is part of libunwind.
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
26
27 #ifndef AARCH64_LIBUNWIND_I_H
28 #define AARCH64_LIBUNWIND_I_H
29
30 /* Target-dependent definitions that are internal to libunwind but need
31    to be shared with target-independent code.  */
32
33 #include <stdlib.h>
34 #include <libunwind.h>
35
36 #include "elf64.h"
37 #include "mempool.h"
38 #include "dwarf.h"
39
40 typedef enum
41   {
42     UNW_AARCH64_FRAME_STANDARD = -2,     /* regular fp, sp +/- offset */
43     UNW_AARCH64_FRAME_SIGRETURN = -1,    /* special sigreturn frame */
44     UNW_AARCH64_FRAME_OTHER = 0,         /* not cacheable (special or unrecognised) */
45     UNW_AARCH64_FRAME_GUESSED = 1        /* guessed it was regular, but not known */
46   }
47 unw_tdep_frame_type_t;
48
49 typedef struct
50   {
51     uint64_t virtual_address;
52     int64_t frame_type     : 2;  /* unw_tdep_frame_type_t classification */
53     int64_t last_frame     : 1;  /* non-zero if last frame in chain */
54     int64_t cfa_reg_sp     : 1;  /* cfa dwarf base register is sp vs. fp */
55     int64_t cfa_reg_offset : 30; /* cfa is at this offset from base register value */
56     int64_t fp_cfa_offset  : 30; /* fp saved at this offset from cfa (-1 = not saved) */
57     int64_t lr_cfa_offset  : 30; /* lr saved at this offset from cfa (-1 = not saved) */
58     int64_t sp_cfa_offset  : 30; /* sp saved at this offset from cfa (-1 = not saved) */
59   }
60 unw_tdep_frame_t;
61
62 #ifdef UNW_LOCAL_ONLY
63
64 typedef unw_word_t aarch64_loc_t;
65
66 #else /* !UNW_LOCAL_ONLY */
67
68 typedef struct aarch64_loc
69   {
70     unw_word_t w0, w1;
71   }
72 aarch64_loc_t;
73
74 #endif /* !UNW_LOCAL_ONLY */
75
76 struct unw_addr_space
77   {
78     struct unw_accessors acc;
79     int big_endian;
80     unw_caching_policy_t caching_policy;
81 #ifdef HAVE_ATOMIC_OPS_H
82     AO_t cache_generation;
83 #else
84     uint32_t cache_generation;
85 #endif
86     unw_word_t dyn_generation;          /* see dyn-common.h */
87     unw_word_t dyn_info_list_addr;      /* (cached) dyn_info_list_addr */
88     struct dwarf_rs_cache global_cache;
89     struct unw_debug_frame_list *debug_frames;
90    };
91
92 struct cursor
93   {
94     struct dwarf_cursor dwarf;          /* must be first */
95
96     unw_tdep_frame_t frame_info;        /* quick tracing assist info */
97
98     enum
99       {
100         AARCH64_SCF_NONE,
101         AARCH64_SCF_LINUX_RT_SIGFRAME,
102       }
103     sigcontext_format;
104     unw_word_t sigcontext_addr;
105     unw_word_t sigcontext_sp;
106     unw_word_t sigcontext_pc;
107     int validate;
108   };
109
110 #define DWARF_GET_LOC(l)        ((l).val)
111
112 #ifdef UNW_LOCAL_ONLY
113 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
114 # define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
115 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r) })
116 # define DWARF_IS_REG_LOC(l)    0
117 # define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
118                                  tdep_uc_addr((c)->as_arg, (r)), 0))
119 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
120 # define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
121                                  tdep_uc_addr((c)->as_arg, (r)), 0))
122
123 static inline int
124 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
125 {
126   if (!DWARF_GET_LOC (loc))
127     return -1;
128   *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
129   return 0;
130 }
131
132 static inline int
133 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
134 {
135   if (!DWARF_GET_LOC (loc))
136     return -1;
137   *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
138   return 0;
139 }
140
141 static inline int
142 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
143 {
144   if (!DWARF_GET_LOC (loc))
145     return -1;
146   *val = *(unw_word_t *) DWARF_GET_LOC (loc);
147   return 0;
148 }
149
150 static inline int
151 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
152 {
153   if (!DWARF_GET_LOC (loc))
154     return -1;
155   *(unw_word_t *) DWARF_GET_LOC (loc) = val;
156   return 0;
157 }
158
159 #else /* !UNW_LOCAL_ONLY */
160 # define DWARF_LOC_TYPE_FP      (1 << 0)
161 # define DWARF_LOC_TYPE_REG     (1 << 1)
162 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
163 # define DWARF_IS_NULL_LOC(l)                                           \
164                 ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
165 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
166 # define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
167 # define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
168 # define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
169 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
170 # define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
171                                                 | DWARF_LOC_TYPE_FP))
172
173 static inline int
174 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
175 {
176   char *valp = (char *) &val;
177   unw_word_t addr;
178   int ret;
179
180   if (DWARF_IS_NULL_LOC (loc))
181     return -UNW_EBADREG;
182
183   if (DWARF_IS_REG_LOC (loc))
184     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
185                                        val, 0, c->as_arg);
186
187   addr = DWARF_GET_LOC (loc);
188   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
189                                        0, c->as_arg)) < 0)
190     return ret;
191
192   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
193                                    c->as_arg);
194 }
195
196 static inline int
197 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
198 {
199   char *valp = (char *) &val;
200   unw_word_t addr;
201   int ret;
202
203   if (DWARF_IS_NULL_LOC (loc))
204     return -UNW_EBADREG;
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   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
212                                        1, c->as_arg)) < 0)
213     return ret;
214
215   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
216                                    1, c->as_arg);
217 }
218
219 static inline int
220 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
221 {
222   if (DWARF_IS_NULL_LOC (loc))
223     return -UNW_EBADREG;
224
225   /* If a code-generator were to save a value of type unw_word_t in a
226      floating-point register, we would have to support this case.  I
227      suppose it could happen with MMX registers, but does it really
228      happen?  */
229   assert (!DWARF_IS_FP_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
239 static inline int
240 dwarf_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
251   if (DWARF_IS_REG_LOC (loc))
252     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
253                                      1, c->as_arg);
254   else
255     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
256                                      1, c->as_arg);
257 }
258
259 #endif /* !UNW_LOCAL_ONLY */
260
261
262
263 #define tdep_getcontext_trace           UNW_ARCH_OBJ(getcontext_trace)
264 #define tdep_init_done                  UNW_OBJ(init_done)
265 #define tdep_init                       UNW_OBJ(init)
266 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
267    tdep_search_unwind_table.  */
268 #define tdep_search_unwind_table        dwarf_search_unwind_table
269 #define tdep_find_unwind_table          dwarf_find_unwind_table
270 #define tdep_uc_addr                    UNW_OBJ(uc_addr)
271 #define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
272 #define tdep_access_reg                 UNW_OBJ(access_reg)
273 #define tdep_access_fpreg               UNW_OBJ(access_fpreg)
274 #define tdep_fetch_frame(c,ip,n)        do {} while(0)
275 #define tdep_cache_frame(c,rs)          do {} while(0)
276 #define tdep_reuse_frame(c,rs)          do {} while(0)
277 #define tdep_stash_frame                UNW_OBJ(tdep_stash_frame)
278 #define tdep_trace                      UNW_OBJ(tdep_trace)
279
280 #ifdef UNW_LOCAL_ONLY
281 # define tdep_find_proc_info(c,ip,n)                            \
282         dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),      \
283                                        (c)->as_arg)
284 # define tdep_put_unwind_info(as,pi,arg)                \
285         dwarf_put_unwind_info((as), (pi), (arg))
286 #else
287 # define tdep_find_proc_info(c,ip,n)                                    \
288         (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
289                                        (c)->as_arg)
290 # define tdep_put_unwind_info(as,pi,arg)                \
291         (*(as)->acc.put_unwind_info)((as), (pi), (arg))
292 #endif
293
294 #define tdep_get_as(c)                  ((c)->dwarf.as)
295 #define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
296 #define tdep_get_ip(c)                  ((c)->dwarf.ip)
297 #define tdep_big_endian(as)             ((as)->big_endian)
298
299 extern int tdep_init_done;
300
301 extern void tdep_init (void);
302 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
303                                      unw_dyn_info_t *di, unw_proc_info_t *pi,
304                                      int need_unwind_info, void *arg);
305 extern void *tdep_uc_addr (unw_tdep_context_t *uc, int reg);
306 extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
307                                unsigned long *segbase, unsigned long *mapoff,
308                                char *path, size_t pathlen);
309 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
310                             unw_word_t *valp, int write);
311 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
312                               unw_fpreg_t *valp, int write);
313 extern int tdep_trace (unw_cursor_t *cursor, void **addresses, int *n);
314 extern void tdep_stash_frame (struct dwarf_cursor *c,
315                               struct dwarf_reg_state *rs);
316 extern int tdep_getcontext_trace (unw_tdep_context_t *);
317
318 #endif /* AARCH64_LIBUNWIND_I_H */