• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2008 CodeSourcery
3 
4    Modified for riscv by Zhaofeng Li <hello@zhaofeng.li>
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 RISCV_LIBUNWIND_I_H
28 #define RISCV_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 /* FIXME: Remote across address sizes? */
38 
39 #if __riscv_xlen == 64
40 # include "elf64.h"
41 #elif __riscv_xlen == 32
42 # include "elf32.h"
43 #else
44 # error "Unsupported address size"
45 #endif
46 
47 #include "mempool.h"
48 #include "dwarf.h"
49 
50 typedef struct
51   {
52     /* no riscv-specific fast trace */
53   }
54 unw_tdep_frame_t;
55 
56 struct unw_addr_space
57   {
58     struct unw_accessors acc;
59 
60     int big_endian;
61     unsigned int addr_size;
62 
63     unw_caching_policy_t caching_policy;
64     _Atomic uint32_t cache_generation;
65     unw_word_t dyn_generation;          /* see dyn-common.h */
66     unw_word_t dyn_info_list_addr;      /* (cached) dyn_info_list_addr */
67     struct dwarf_rs_cache global_cache;
68     struct unw_debug_frame_list *debug_frames;
69 };
70 
71 #define tdep_big_endian(as)             ((as)->big_endian)
72 
73 struct cursor
74   {
75     struct dwarf_cursor dwarf;          /* must be first */
76     enum
77       {
78         RISCV_SCF_NONE, // 0
79         RISCV_SCF_LINUX_RT_SIGFRAME, // 1
80       }
81     sigcontext_format;
82     unw_word_t sigcontext_addr;
83     unw_word_t sigcontext_sp;
84     unw_word_t sigcontext_pc;
85     int validate;
86     ucontext_t *uc;
87   };
88 
89 static inline ucontext_t *
dwarf_get_uc(const struct dwarf_cursor * cursor)90 dwarf_get_uc(const struct dwarf_cursor *cursor)
91 {
92   const struct cursor *c = (struct cursor *) cursor->as_arg;
93   return c->uc;
94 }
95 
96 #define DWARF_GET_LOC(l)        ((l).val)
97 
98 #ifdef UNW_LOCAL_ONLY
99 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
100 # define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
101 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r) })
102 # define DWARF_IS_REG_LOC(l)    0
103 # define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
104                                  tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
105 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
106 # define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
107                                  tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
108 
109 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)110 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
111 {
112   if (!DWARF_GET_LOC (loc))
113     return -1;
114   *val = *(unw_fpreg_t *) (intptr_t) DWARF_GET_LOC (loc);
115   return 0;
116 }
117 
118 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)119 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
120 {
121   if (!DWARF_GET_LOC (loc))
122     return -1;
123   *(unw_fpreg_t *) (intptr_t) DWARF_GET_LOC (loc) = val;
124   return 0;
125 }
126 
127 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)128 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
129 {
130   if (!DWARF_GET_LOC (loc))
131     return -1;
132   *val = *(unw_word_t *) (intptr_t) DWARF_GET_LOC (loc);
133   return 0;
134 }
135 
136 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)137 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
138 {
139   if (!DWARF_GET_LOC (loc))
140     return -1;
141   *(unw_word_t *) (intptr_t) DWARF_GET_LOC (loc) = val;
142   return 0;
143 }
144 
145 #else /* !UNW_LOCAL_ONLY */
146 # define DWARF_LOC_TYPE_FP      (1 << 0)
147 # define DWARF_LOC_TYPE_REG     (1 << 1)
148 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
149 # define DWARF_IS_NULL_LOC(l)                                           \
150                 ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
151 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
152 # define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
153 # define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
154 # define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
155 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), 0)
156 # define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
157                                                 | DWARF_LOC_TYPE_FP))
158 
159 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)160 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
161 {
162   char *valp = (char *) &val;
163   unw_word_t addr;
164   int ret;
165 
166   if (DWARF_IS_NULL_LOC (loc))
167     return -UNW_EBADREG;
168 
169   if (DWARF_IS_REG_LOC (loc))
170     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
171                                        val, 0, c->as_arg);
172 
173   /* FIXME: unw_word_t may not be equal to FLEN */
174   addr = DWARF_GET_LOC (loc);
175 #if __riscv_xlen == __riscv_flen
176   return (*c->as->acc.access_mem) (c->as, addr, (unw_word_t *) valp,
177                                        0, c->as_arg);
178 #else
179 # error "FIXME"
180 #endif
181 }
182 
183 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)184 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
185 {
186   char *valp = (char *) &val;
187   unw_word_t addr;
188   int ret;
189 
190   if (DWARF_IS_NULL_LOC (loc))
191     return -UNW_EBADREG;
192 
193   if (DWARF_IS_REG_LOC (loc))
194     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
195                                        &val, 1, c->as_arg);
196 
197   /* FIXME: unw_word_t may not be equal to FLEN */
198   addr = DWARF_GET_LOC (loc);
199 #if __riscv_xlen == __riscv_flen
200   return (*c->as->acc.access_mem) (c->as, addr, (unw_word_t *) valp,
201                                        1, c->as_arg);
202 #else
203 # error "FIXME"
204 #endif
205 }
206 
207 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)208 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
209 {
210   if (DWARF_IS_NULL_LOC (loc))
211     return -UNW_EBADREG;
212 
213   /* If a code-generator were to save a value of type unw_word_t in a
214      floating-point register, we would have to support this case.  I
215      suppose it could happen with MMX registers, but does it really
216      happen?  */
217   assert (!DWARF_IS_FP_LOC (loc));
218 
219   if (DWARF_IS_REG_LOC (loc))
220     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
221                                      0, c->as_arg);
222   else
223     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
224                                      0, c->as_arg);
225 }
226 
227 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)228 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
229 {
230   if (DWARF_IS_NULL_LOC (loc))
231     return -UNW_EBADREG;
232 
233   /* If a code-generator were to save a value of type unw_word_t in a
234      floating-point register, we would have to support this case.  I
235      suppose it could happen with MMX registers, but does it really
236      happen?  */
237   assert (!DWARF_IS_FP_LOC (loc));
238 
239   if (DWARF_IS_REG_LOC (loc))
240     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
241                                      1, c->as_arg);
242   else
243     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
244                                      1, c->as_arg);
245 }
246 
247 #endif /* !UNW_LOCAL_ONLY */
248 
249 #define tdep_getcontext_trace           unw_getcontext
250 #define tdep_init_mem_validate          UNW_OBJ(init_mem_validate)
251 #define tdep_init_done                  UNW_OBJ(init_done)
252 #define tdep_init                       UNW_OBJ(init)
253 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
254    tdep_search_unwind_table.  */
255 #define tdep_search_unwind_table        dwarf_search_unwind_table
256 #define tdep_find_unwind_table          dwarf_find_unwind_table
257 #define tdep_uc_addr                    UNW_ARCH_OBJ(uc_addr)
258 #define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
259 #define tdep_get_exe_image_path         UNW_ARCH_OBJ(get_exe_image_path)
260 #define tdep_access_reg                 UNW_OBJ(access_reg)
261 #define tdep_access_fpreg               UNW_OBJ(access_fpreg)
262 #define tdep_fetch_frame(c,ip,n)        do {} while(0)
263 #define tdep_cache_frame(c)             0
264 #define tdep_reuse_frame(c,frame)       do {} while(0)
265 #define tdep_stash_frame(c,rs)          do {} while(0)
266 #define tdep_trace(cur,addr,n)          (-UNW_ENOINFO)
267 
268 #ifdef UNW_LOCAL_ONLY
269 # define tdep_find_proc_info(c,ip,n)                            \
270         dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),      \
271                                        (c)->as_arg)
272 # define tdep_put_unwind_info(as,pi,arg)                \
273         dwarf_put_unwind_info((as), (pi), (arg))
274 #else
275 # define tdep_find_proc_info(c,ip,n)                                    \
276         (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
277                                        (c)->as_arg)
278 # define tdep_put_unwind_info(as,pi,arg)                \
279         (*(as)->acc.put_unwind_info)((as), (pi), (arg))
280 #endif
281 
282 #define tdep_get_as(c)                  ((c)->dwarf.as)
283 #define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
284 #define tdep_get_ip(c)                  ((c)->dwarf.ip)
285 
286 extern atomic_bool tdep_init_done;
287 
288 extern void tdep_init (void);
289 extern void tdep_init_mem_validate (void);
290 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
291                                      unw_dyn_info_t *di, unw_proc_info_t *pi,
292                                      int need_unwind_info, void *arg);
293 extern void *tdep_uc_addr (ucontext_t *uc, int reg);
294 extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
295                                unsigned long *segbase, unsigned long *mapoff,
296                                char *path, size_t pathlen);
297 extern void tdep_get_exe_image_path (char *path);
298 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
299                             unw_word_t *valp, int write);
300 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
301                               unw_fpreg_t *valp, int write);
302 
303 #endif /* RISCV_LIBUNWIND_I_H */
304