• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdatomic.h>
36 
37 #include "elf64.h"
38 /* Add For Cache MAP And ELF */
39 #include "map_info.h"
40 /* Add For Cache MAP And ELF */
41 #include "mempool.h"
42 #include "dwarf.h"
43 
44 typedef enum
45   {
46     UNW_AARCH64_FRAME_STANDARD = -2,     /* regular fp, sp +/- offset */
47     UNW_AARCH64_FRAME_SIGRETURN = -1,    /* special sigreturn frame */
48     UNW_AARCH64_FRAME_OTHER = 0,         /* not cacheable (special or unrecognised) */
49     UNW_AARCH64_FRAME_GUESSED = 1        /* guessed it was regular, but not known */
50   }
51 unw_tdep_frame_type_t;
52 
53 typedef struct
54   {
55     uint64_t virtual_address;
56     int64_t frame_type     : 2;  /* unw_tdep_frame_type_t classification */
57     int64_t last_frame     : 1;  /* non-zero if last frame in chain */
58     int64_t cfa_reg_sp     : 1;  /* cfa dwarf base register is sp vs. fp */
59     int64_t cfa_reg_offset : 30; /* cfa is at this offset from base register value */
60     int64_t fp_cfa_offset  : 30; /* fp saved at this offset from cfa (-1 = not saved) */
61     int64_t lr_cfa_offset  : 30; /* lr saved at this offset from cfa (-1 = not saved) */
62     int64_t sp_cfa_offset  : 30; /* sp saved at this offset from cfa (-1 = not saved) */
63   }
64 unw_tdep_frame_t;
65 
66 #ifdef UNW_LOCAL_ONLY
67 
68 typedef unw_word_t aarch64_loc_t;
69 
70 #else /* !UNW_LOCAL_ONLY */
71 
72 typedef struct aarch64_loc
73   {
74     unw_word_t w0, w1;
75   }
76 aarch64_loc_t;
77 
78 #endif /* !UNW_LOCAL_ONLY */
79 
80 struct unw_addr_space
81   {
82     struct unw_accessors acc;
83     int big_endian;
84     unw_caching_policy_t caching_policy;
85     _Atomic uint32_t cache_generation;
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     /* Add For Cache MAP And ELF */
91     struct map_info *map_list;
92     /* Add For Cache MAP And ELF */
93     struct cursor *cursor;
94     int pid;
95    };
96 
97 static inline struct cursor *
get_cursor_from_as(unw_addr_space_t as)98 get_cursor_from_as(unw_addr_space_t as)
99 {
100   if (as->cursor) {
101     return (struct cursor *)(as->cursor);
102   }
103 
104   return NULL;
105 }
106 
107 struct cursor
108   {
109     struct dwarf_cursor dwarf;          /* must be first */
110 
111     unw_tdep_frame_t frame_info;        /* quick tracing assist info */
112 
113     enum
114       {
115         AARCH64_SCF_NONE,
116         AARCH64_SCF_LINUX_RT_SIGFRAME,
117       }
118     sigcontext_format;
119     unw_word_t sigcontext_addr;
120     unw_word_t sigcontext_sp;
121     unw_word_t sigcontext_pc;
122     int validate;
123     ucontext_t *uc;
124   };
125 
126 static inline ucontext_t *
dwarf_get_uc(const struct dwarf_cursor * cursor)127 dwarf_get_uc(const struct dwarf_cursor *cursor)
128 {
129   const struct cursor *c = (struct cursor *) cursor->as_arg;
130   return c->uc;
131 }
132 
133 #define DWARF_GET_LOC(l)        ((l).val)
134 
135 #ifdef UNW_LOCAL_ONLY
136 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
137 # define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
138 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r) })
139 # define DWARF_IS_REG_LOC(l)    0
140 # define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
141                                  tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
142 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
143 # define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
144                                  tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
145 
146 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)147 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
148 {
149   if (!DWARF_GET_LOC (loc))
150     return -1;
151   *val = *(unw_fpreg_t *) DWARF_GET_LOC (loc);
152   return 0;
153 }
154 
155 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)156 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
157 {
158   if (!DWARF_GET_LOC (loc))
159     return -1;
160   *(unw_fpreg_t *) DWARF_GET_LOC (loc) = val;
161   return 0;
162 }
163 
164 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)165 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
166 {
167   if (!DWARF_GET_LOC (loc))
168     return -1;
169   *val = *(unw_word_t *) DWARF_GET_LOC (loc);
170   return 0;
171 }
172 
173 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)174 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
175 {
176   if (!DWARF_GET_LOC (loc))
177     return -1;
178   *(unw_word_t *) DWARF_GET_LOC (loc) = val;
179   return 0;
180 }
181 
182 #else /* !UNW_LOCAL_ONLY */
183 # define DWARF_LOC_TYPE_FP      (1 << 0)
184 # define DWARF_LOC_TYPE_REG     (1 << 1)
185 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
186 
187 static inline int
dwarf_is_null_loc(dwarf_loc_t l)188 dwarf_is_null_loc(dwarf_loc_t l)
189 {
190   return l.val == 0 && l.type == 0;
191 }
192 
193 # define DWARF_IS_NULL_LOC(l)   dwarf_is_null_loc(l)
194 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
195 # define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
196 # define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
197 # define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
198 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
199 # define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
200                                                 | DWARF_LOC_TYPE_FP))
201 
202 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)203 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
204 {
205   char *valp = (char *) &val;
206   unw_word_t addr;
207   int ret;
208 
209   if (DWARF_IS_NULL_LOC (loc))
210     return -UNW_EBADREG;
211 
212   if (DWARF_IS_REG_LOC (loc))
213     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
214                                        val, 0, c->as_arg);
215 
216   addr = DWARF_GET_LOC (loc);
217   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
218                                        0, c->as_arg)) < 0)
219     return ret;
220 
221   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
222                                    c->as_arg);
223 }
224 
225 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)226 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
227 {
228   char *valp = (char *) &val;
229   unw_word_t addr;
230   int ret;
231 
232   if (DWARF_IS_NULL_LOC (loc))
233     return -UNW_EBADREG;
234 
235   if (DWARF_IS_REG_LOC (loc))
236     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
237                                        &val, 1, c->as_arg);
238 
239   addr = DWARF_GET_LOC (loc);
240   if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
241                                        1, c->as_arg)) < 0)
242     return ret;
243 
244   return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
245                                    1, c->as_arg);
246 }
247 
248 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)249 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
250 {
251   int reg_num;
252   if (DWARF_IS_NULL_LOC (loc))
253     return -UNW_EBADREG;
254 
255   /* If a code-generator were to save a value of type unw_word_t in a
256      floating-point register, we would have to support this case.  I
257      suppose it could happen with MMX registers, but does it really
258      happen?  */
259   assert (!DWARF_IS_FP_LOC (loc));
260 
261   if (DWARF_IS_REG_LOC (loc)) {
262     reg_num = DWARF_GET_LOC (loc);
263     if (reg_num >= 0 && reg_num < c->reg_sz) {
264       *val = c->ctx[reg_num];
265       return 1;
266     } else {
267       return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
268                                       0, c->as_arg);
269     }
270   }
271   else
272     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
273                                      0, c->as_arg);
274 }
275 
276 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)277 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
278 {
279   if (DWARF_IS_NULL_LOC (loc))
280     return -UNW_EBADREG;
281 
282   /* If a code-generator were to save a value of type unw_word_t in a
283      floating-point register, we would have to support this case.  I
284      suppose it could happen with MMX registers, but does it really
285      happen?  */
286   assert (!DWARF_IS_FP_LOC (loc));
287 
288   if (DWARF_IS_REG_LOC (loc))
289     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
290                                      1, c->as_arg);
291   else
292     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
293                                      1, c->as_arg);
294 }
295 
296 #endif /* !UNW_LOCAL_ONLY */
297 
298 
299 
300 #define tdep_getcontext_trace           UNW_ARCH_OBJ(getcontext_trace)
301 #define tdep_init_done                  UNW_OBJ(init_done)
302 #define tdep_init_mem_validate          UNW_OBJ(init_mem_validate)
303 #define tdep_init                       UNW_OBJ(init)
304 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
305    tdep_search_unwind_table.  */
306 #define tdep_search_unwind_table        dwarf_search_unwind_table
307 #define tdep_find_unwind_table          dwarf_find_unwind_table
308 #define tdep_uc_addr                    UNW_OBJ(uc_addr)
309 #define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
310 #define tdep_get_exe_image_path         UNW_ARCH_OBJ(get_exe_image_path)
311 #define tdep_access_reg                 UNW_OBJ(access_reg)
312 #define tdep_access_fpreg               UNW_OBJ(access_fpreg)
313 #define tdep_fetch_frame(c,ip,n)        do {} while(0)
314 #define tdep_cache_frame(c)             0
315 #define tdep_reuse_frame(c,frame)       do {} while(0)
316 #define tdep_stash_frame                UNW_OBJ(tdep_stash_frame)
317 #define tdep_trace                      UNW_OBJ(tdep_trace)
318 
319 #ifdef UNW_LOCAL_ONLY
320 # define tdep_find_proc_info(c,ip,n)                            \
321         dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),      \
322                                        (c)->as_arg)
323 # define tdep_put_unwind_info(as,pi,arg)                \
324         dwarf_put_unwind_info((as), (pi), (arg))
325 #else
326 # define tdep_find_proc_info(c,ip,n)                                    \
327         (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
328                                        (c)->as_arg)
329 # define tdep_put_unwind_info(as,pi,arg)                \
330         (*(as)->acc.put_unwind_info)((as), (pi), (arg))
331 #endif
332 
333 #define tdep_get_as(c)                  ((c)->dwarf.as)
334 #define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
335 #define tdep_get_ip(c)                  ((c)->dwarf.ip)
336 #define tdep_big_endian(as)             ((as)->big_endian)
337 
338 extern atomic_bool tdep_init_done;
339 
340 extern void tdep_init (void);
341 extern void tdep_init_mem_validate (void);
342 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
343                                      unw_dyn_info_t *di, unw_proc_info_t *pi,
344                                      int need_unwind_info, void *arg);
345 extern void *tdep_uc_addr (unw_tdep_context_t *uc, int reg);
346 /* Add For Cache MAP And ELF */
347 extern struct map_info *tdep_get_elf_image (unw_addr_space_t as, pid_t pid,
348 					    unw_word_t ip);
349 /* Add For Cache MAP And ELF */
350 extern void tdep_get_exe_image_path (char *path);
351 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
352                             unw_word_t *valp, int write);
353 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
354                               unw_fpreg_t *valp, int write);
355 extern int tdep_trace (unw_cursor_t *cursor, void **addresses, int *n);
356 extern void tdep_stash_frame (struct dwarf_cursor *c,
357                               struct dwarf_reg_state *rs);
358 extern int tdep_getcontext_trace (unw_tdep_context_t *);
359 
360 #endif /* AARCH64_LIBUNWIND_I_H */
361