1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2008 CodeSourcery
3 Copyright (C) 2014 Tilera Corp.
4
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "unwind_i.h"
30
31 #ifdef UNW_REMOTE_ONLY
32
33 /* unw_local_addr_space is a NULL pointer in this case. */
34 unw_addr_space_t unw_local_addr_space;
35
36 #else /* !UNW_REMOTE_ONLY */
37
38 static struct unw_addr_space local_addr_space;
39
40 unw_addr_space_t unw_local_addr_space = &local_addr_space;
41
42 /* Return the address of the 64-bit slot in UC for REG (even for o32,
43 where registers are 32-bit, the slots are still 64-bit). */
44
45 static inline void *
uc_addr(ucontext_t * uc,int reg)46 uc_addr (ucontext_t *uc, int reg)
47 {
48 if (reg >= UNW_TILEGX_R0 && reg < UNW_TILEGX_R0 + 56)
49 return &uc->uc_mcontext.gregs[reg - UNW_TILEGX_R0];
50 else if (reg == UNW_TILEGX_PC)
51 return &uc->uc_mcontext.pc;
52 else
53 return NULL;
54 }
55
56 # ifdef UNW_LOCAL_ONLY
57
58 HIDDEN void *
tdep_uc_addr(ucontext_t * uc,int reg)59 tdep_uc_addr (ucontext_t *uc, int reg)
60 {
61 char *addr = uc_addr (uc, reg);
62 return addr;
63 }
64
65 # endif /* UNW_LOCAL_ONLY */
66
67 static void
put_unwind_info(unw_addr_space_t as,unw_proc_info_t * proc_info,void * arg)68 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
69 {
70 /* it's a no-op */
71 }
72
73 static int
get_dyn_info_list_addr(unw_addr_space_t as,unw_word_t * dyn_info_list_addr,void * arg)74 get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
75 void *arg)
76 {
77 #ifndef UNW_LOCAL_ONLY
78 # pragma weak _U_dyn_info_list_addr
79 if (!_U_dyn_info_list_addr)
80 return -UNW_ENOINFO;
81 #endif
82 // Access the `_U_dyn_info_list` from `LOCAL_ONLY` library, i.e. libunwind.so.
83 *dyn_info_list_addr = _U_dyn_info_list_addr ();
84 return 0;
85 }
86
87 static int
access_mem(unw_addr_space_t as,unw_word_t addr,unw_word_t * val,int write,void * arg)88 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
89 void *arg)
90 {
91 if ((long long)addr & (sizeof(unw_word_t) - 1))
92 return 0;
93
94 if (write)
95 {
96 Debug (16, "mem[%llx] <- %llx\n", (long long) addr, (long long) *val);
97 *(unw_word_t *) (intptr_t) addr = *val;
98 }
99 else
100 {
101 *val = *(unw_word_t *) (intptr_t) addr;
102 Debug (16, "mem[%llx] -> %llx\n", (long long) addr, (long long) *val);
103 }
104 return 0;
105 }
106
107 static int
access_reg(unw_addr_space_t as,unw_regnum_t reg,unw_word_t * val,int write,void * arg)108 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
109 void *arg)
110 {
111 unw_word_t *addr;
112 ucontext_t *uc = arg;
113
114 if (unw_is_fpreg (reg))
115 goto badreg;
116
117 Debug (16, "reg = %s\n", unw_regname (reg));
118 if (!(addr = uc_addr (uc, reg)))
119 goto badreg;
120
121 if (write)
122 {
123 *(unw_word_t *) (intptr_t) addr = (tilegx_reg_t) *val;
124 Debug (12, "%s <- %llx\n", unw_regname (reg), (long long) *val);
125 }
126 else
127 {
128 *val = (tilegx_reg_t) *(unw_word_t *) (intptr_t) addr;
129 Debug (12, "%s -> %llx\n", unw_regname (reg), (long long) *val);
130 }
131 return 0;
132
133 badreg:
134 Debug (1, "bad register number %u\n", reg);
135 return -UNW_EBADREG;
136 }
137
138 static int
get_static_proc_name(unw_addr_space_t as,unw_word_t ip,char * buf,size_t buf_len,unw_word_t * offp,void * arg)139 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
140 char *buf, size_t buf_len, unw_word_t *offp,
141 void *arg)
142 {
143 return elf_w (get_proc_name) (as, getpid (), ip, buf, buf_len, offp);
144 }
145
146 __attribute__((weak)) void
tilegx_local_addr_space_init(void)147 tilegx_local_addr_space_init (void)
148 {
149 memset (&local_addr_space, 0, sizeof (local_addr_space));
150 local_addr_space.big_endian = target_is_big_endian();
151
152 local_addr_space.abi = UNW_TILEGX_ABI_N64;
153 local_addr_space.addr_size = sizeof (void *);
154 local_addr_space.caching_policy = UNWI_DEFAULT_CACHING_POLICY;
155 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
156 local_addr_space.acc.put_unwind_info = put_unwind_info;
157 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
158 local_addr_space.acc.access_mem = access_mem;
159 local_addr_space.acc.access_reg = access_reg;
160 local_addr_space.acc.access_fpreg = NULL;
161 local_addr_space.acc.resume = tilegx_local_resume;
162 local_addr_space.acc.get_proc_name = get_static_proc_name;
163 unw_flush_cache (&local_addr_space, 0, 0);
164 }
165
166 #endif /* !UNW_REMOTE_ONLY */
167