• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2002-2005 Hewlett-Packard Co
3         Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4 
5    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
6 
7 This file is part of libunwind.
8 
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16 
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
27 
28 #ifndef S390X_LIBUNWIND_I_H
29 #define S390X_LIBUNWIND_I_H
30 
31 /* Target-dependent definitions that are internal to libunwind but need
32    to be shared with target-independent code.  */
33 
34 #include <stdlib.h>
35 #include <libunwind.h>
36 #include <stdatomic.h>
37 
38 #include "elf64.h"
39 #include "mempool.h"
40 #include "dwarf.h"
41 
42 struct unw_addr_space
43   {
44     struct unw_accessors acc;
45     unw_caching_policy_t caching_policy;
46     _Atomic uint32_t cache_generation;
47     unw_word_t dyn_generation;          /* see dyn-common.h */
48     unw_word_t dyn_info_list_addr;      /* (cached) dyn_info_list_addr */
49     struct dwarf_rs_cache global_cache;
50     struct unw_debug_frame_list *debug_frames;
51    };
52 
53 struct cursor
54   {
55     struct dwarf_cursor dwarf;          /* must be first */
56 
57     /* Format of sigcontext structure and address at which it is
58        stored: */
59     enum
60       {
61         S390X_SCF_NONE              = 0, /* no signal frame encountered */
62         S390X_SCF_LINUX_SIGFRAME    = 1, /* Linux struct sigcontext */
63         S390X_SCF_LINUX_RT_SIGFRAME = 2, /* Linux ucontext_t */
64       }
65     sigcontext_format;
66     unw_word_t sigcontext_addr;
67     unw_word_t sigcontext_sp;
68     unw_word_t sigcontext_pc;
69     int validate;
70     ucontext_t *uc;
71   };
72 
73 static inline ucontext_t *
dwarf_get_uc(const struct dwarf_cursor * cursor)74 dwarf_get_uc(const struct dwarf_cursor *cursor)
75 {
76   const struct cursor *c = (struct cursor *) cursor->as_arg;
77   return c->uc;
78 }
79 
80 #define DWARF_GET_LOC(l)        ((l).val)
81 # define DWARF_LOC_TYPE_MEM     (0 << 0)
82 # define DWARF_LOC_TYPE_FP      (1 << 0)
83 # define DWARF_LOC_TYPE_REG     (1 << 1)
84 # define DWARF_LOC_TYPE_VAL     (1 << 2)
85 
86 # define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
87 # define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
88 # define DWARF_IS_MEM_LOC(l)    ((l).type == DWARF_LOC_TYPE_MEM)
89 # define DWARF_IS_VAL_LOC(l)    (((l).type & DWARF_LOC_TYPE_VAL) != 0)
90 
91 # define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
92 # define DWARF_VAL_LOC(c,v)     DWARF_LOC ((v), DWARF_LOC_TYPE_VAL)
93 # define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), DWARF_LOC_TYPE_MEM)
94 
95 #ifdef UNW_LOCAL_ONLY
96 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
97 # define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
98 # define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
99                                  tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
100 # define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
101                                  tdep_uc_addr(dwarf_get_uc(c), (r)), 0))
102 
103 #else /* !UNW_LOCAL_ONLY */
104 
105 # define DWARF_NULL_LOC         DWARF_LOC (0, 0)
106 # define DWARF_IS_NULL_LOC(l)                                           \
107                 ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
108 # define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
109 # define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
110                                                 | DWARF_LOC_TYPE_FP))
111 
112 #endif /* !UNW_LOCAL_ONLY */
113 
114 static inline int
dwarf_getfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t * val)115 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
116 {
117   assert(sizeof(unw_fpreg_t) == sizeof(unw_word_t));
118 
119   if (DWARF_IS_NULL_LOC (loc))
120     return -UNW_EBADREG;
121 
122   if (DWARF_IS_FP_LOC (loc))
123     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc), val,
124                                        0, c->as_arg);
125   /* FPRs may be saved in GPRs */
126   if (DWARF_IS_REG_LOC (loc))
127     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), (unw_word_t*)val,
128                                      0, c->as_arg);
129   if (DWARF_IS_MEM_LOC (loc))
130     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), (unw_word_t*)val,
131                                      0, c->as_arg);
132   assert(DWARF_IS_VAL_LOC (loc));
133   *val = *(unw_fpreg_t*) DWARF_GET_LOC (loc);
134   return 0;
135 }
136 
137 static inline int
dwarf_putfp(struct dwarf_cursor * c,dwarf_loc_t loc,unw_fpreg_t val)138 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
139 {
140   assert(sizeof(unw_fpreg_t) == sizeof(unw_word_t));
141   assert(!DWARF_IS_VAL_LOC (loc));
142 
143   if (DWARF_IS_NULL_LOC (loc))
144     return -UNW_EBADREG;
145 
146   if (DWARF_IS_FP_LOC (loc))
147     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc), &val,
148                                        1, c->as_arg);
149   /* FPRs may be saved in GPRs */
150   if (DWARF_IS_REG_LOC (loc))
151     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), (unw_word_t*) &val,
152                                      1, c->as_arg);
153 
154   assert(DWARF_IS_MEM_LOC (loc));
155   return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), (unw_word_t*) &val,
156                                    1, c->as_arg);
157 }
158 
159 static inline int
dwarf_get(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t * val)160 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
161 {
162   assert(sizeof(unw_fpreg_t) == sizeof(unw_word_t));
163 
164   if (DWARF_IS_NULL_LOC (loc))
165     return -UNW_EBADREG;
166 
167   /* GPRs may be saved in FPRs */
168   if (DWARF_IS_FP_LOC (loc))
169     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc), (unw_fpreg_t*)val,
170                                        0, c->as_arg);
171   if (DWARF_IS_REG_LOC (loc))
172     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
173                                      0, c->as_arg);
174   if (DWARF_IS_MEM_LOC (loc))
175     return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
176                                      0, c->as_arg);
177   assert(DWARF_IS_VAL_LOC (loc));
178   *val = DWARF_GET_LOC (loc);
179   return 0;
180 }
181 
182 static inline int
dwarf_put(struct dwarf_cursor * c,dwarf_loc_t loc,unw_word_t val)183 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
184 {
185   assert(sizeof(unw_fpreg_t) == sizeof(unw_word_t));
186   assert(!DWARF_IS_VAL_LOC (loc));
187 
188   if (DWARF_IS_NULL_LOC (loc))
189     return -UNW_EBADREG;
190 
191   /* GPRs may be saved in FPRs */
192   if (DWARF_IS_FP_LOC (loc))
193     return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc), (unw_fpreg_t*) &val,
194                                        1, c->as_arg);
195   if (DWARF_IS_REG_LOC (loc))
196     return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
197                                      1, c->as_arg);
198 
199   assert(DWARF_IS_MEM_LOC (loc));
200   return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
201                                    1, c->as_arg);
202 }
203 
204 #define tdep_getcontext_trace           unw_getcontext
205 #define tdep_init_done                  UNW_OBJ(init_done)
206 #define tdep_init_mem_validate          UNW_OBJ(init_mem_validate)
207 #define tdep_init                       UNW_OBJ(init)
208 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
209    tdep_search_unwind_table.  */
210 #define tdep_search_unwind_table        dwarf_search_unwind_table
211 #define tdep_find_unwind_table          dwarf_find_unwind_table
212 #define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
213 #define tdep_get_exe_image_path         UNW_ARCH_OBJ(get_exe_image_path)
214 #define tdep_access_reg                 UNW_OBJ(access_reg)
215 #define tdep_access_fpreg               UNW_OBJ(access_fpreg)
216 #define tdep_fetch_frame(c,ip,n)        do {} while(0)
217 #define tdep_cache_frame(c)             0
218 #define tdep_reuse_frame(c,rs)          do {} while(0)
219 #define tdep_stash_frame(cs,rs)         do {} while(0)
220 #define tdep_trace(cur,addr,n)          (-UNW_ENOINFO)
221 #define tdep_uc_addr                    UNW_OBJ(uc_addr)
222 
223 #ifdef UNW_LOCAL_ONLY
224 # define tdep_find_proc_info(c,ip,n)                            \
225         dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),      \
226                                        (c)->as_arg)
227 # define tdep_put_unwind_info(as,pi,arg)                \
228         dwarf_put_unwind_info((as), (pi), (arg))
229 #else
230 # define tdep_find_proc_info(c,ip,n)                                    \
231         (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
232                                        (c)->as_arg)
233 # define tdep_put_unwind_info(as,pi,arg)                        \
234         (*(as)->acc.put_unwind_info)((as), (pi), (arg))
235 #endif
236 
237 #define tdep_get_as(c)                  ((c)->dwarf.as)
238 #define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
239 #define tdep_get_ip(c)                  ((c)->dwarf.ip)
240 #define tdep_big_endian(as)             1
241 
242 extern atomic_bool tdep_init_done;
243 
244 extern void tdep_init (void);
245 extern void tdep_init_mem_validate (void);
246 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
247                                      unw_dyn_info_t *di, unw_proc_info_t *pi,
248                                      int need_unwind_info, void *arg);
249 extern void *tdep_uc_addr (unw_tdep_context_t *uc, int reg);
250 extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
251                                unsigned long *segbase, unsigned long *mapoff,
252                                char *path, size_t pathlen);
253 extern void tdep_get_exe_image_path (char *path);
254 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
255                             unw_word_t *valp, int write);
256 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
257                               unw_fpreg_t *valp, int write);
258 
259 #endif /* S390X_LIBUNWIND_I_H */
260