Make UEFI boot-platform build again
[haiku.git] / src / libs / libunwind / arm / Ginit.c
blob1ed3dbfc508001215fb886b5d13355e4afed5505
1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2008 CodeSourcery
4 This file is part of libunwind.
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25 #include <stdlib.h>
26 #include <string.h>
28 #include "unwind_i.h"
30 #ifdef UNW_REMOTE_ONLY
32 /* unw_local_addr_space is a NULL pointer in this case. */
33 PROTECTED unw_addr_space_t unw_local_addr_space;
35 #else /* !UNW_REMOTE_ONLY */
37 static struct unw_addr_space local_addr_space;
39 PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
41 static inline void *
42 uc_addr (unw_tdep_context_t *uc, int reg)
44 if (reg >= UNW_ARM_R0 && reg < UNW_ARM_R0 + 16)
45 return &uc->regs[reg - UNW_ARM_R0];
46 else
47 return NULL;
50 # ifdef UNW_LOCAL_ONLY
52 HIDDEN void *
53 tdep_uc_addr (unw_tdep_context_t *uc, int reg)
55 return uc_addr (uc, reg);
58 # endif /* UNW_LOCAL_ONLY */
60 HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
62 /* XXX fix me: there is currently no way to locate the dyn-info list
63 by a remote unwinder. On ia64, this is done via a special
64 unwind-table entry. Perhaps something similar can be done with
65 DWARF2 unwind info. */
67 static int
68 get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
69 void *arg)
71 *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
72 return 0;
75 #define PAGE_SIZE 4096
76 #define PAGE_START(a) ((a) & ~(PAGE_SIZE-1))
78 /* Cache of already validated addresses */
79 #define NLGA 4
80 static unw_word_t last_good_addr[NLGA];
81 static int lga_victim;
83 static int
84 validate_mem (unw_word_t addr)
86 int i, victim;
87 size_t len;
89 if (PAGE_START(addr + sizeof (unw_word_t) - 1) == PAGE_START(addr))
90 len = PAGE_SIZE;
91 else
92 len = PAGE_SIZE * 2;
94 addr = PAGE_START(addr);
96 if (addr == 0)
97 return -1;
99 for (i = 0; i < NLGA; i++)
101 if (last_good_addr[i] && (addr == last_good_addr[i]))
102 return 0;
105 if (msync ((void *) addr, len, MS_ASYNC) == -1)
106 return -1;
108 victim = lga_victim;
109 for (i = 0; i < NLGA; i++) {
110 if (!last_good_addr[victim]) {
111 last_good_addr[victim++] = addr;
112 return 0;
114 victim = (victim + 1) % NLGA;
117 /* All slots full. Evict the victim. */
118 last_good_addr[victim] = addr;
119 victim = (victim + 1) % NLGA;
120 lga_victim = victim;
122 return 0;
125 static int
126 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
127 void *arg)
129 if (write)
131 Debug (16, "mem[%x] <- %x\n", addr, *val);
132 *(unw_word_t *) addr = *val;
134 else
136 /* validate address */
137 const struct cursor *c = (const struct cursor *) arg;
138 if (c && validate_mem(addr))
139 return -1;
141 *val = *(unw_word_t *) addr;
142 Debug (16, "mem[%x] -> %x\n", addr, *val);
144 return 0;
147 static int
148 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
149 void *arg)
151 unw_word_t *addr;
152 unw_tdep_context_t *uc = arg;
154 if (unw_is_fpreg (reg))
155 goto badreg;
157 Debug (16, "reg = %s\n", unw_regname (reg));
158 if (!(addr = uc_addr (uc, reg)))
159 goto badreg;
161 if (write)
163 *(unw_word_t *) addr = *val;
164 Debug (12, "%s <- %x\n", unw_regname (reg), *val);
166 else
168 *val = *(unw_word_t *) addr;
169 Debug (12, "%s -> %x\n", unw_regname (reg), *val);
171 return 0;
173 badreg:
174 Debug (1, "bad register number %u\n", reg);
175 return -UNW_EBADREG;
178 static int
179 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
180 int write, void *arg)
182 unw_tdep_context_t *uc = arg;
183 unw_fpreg_t *addr;
185 if (!unw_is_fpreg (reg))
186 goto badreg;
188 if (!(addr = uc_addr (uc, reg)))
189 goto badreg;
191 if (write)
193 Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
194 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
195 *(unw_fpreg_t *) addr = *val;
197 else
199 *val = *(unw_fpreg_t *) addr;
200 Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
201 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
203 return 0;
205 badreg:
206 Debug (1, "bad register number %u\n", reg);
207 /* attempt to access a non-preserved register */
208 return -UNW_EBADREG;
211 static int
212 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
213 char *buf, size_t buf_len, unw_word_t *offp,
214 void *arg)
216 return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
219 HIDDEN void
220 arm_local_addr_space_init (void)
222 memset (&local_addr_space, 0, sizeof (local_addr_space));
223 local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
224 local_addr_space.acc.find_proc_info = arm_find_proc_info;
225 local_addr_space.acc.put_unwind_info = arm_put_unwind_info;
226 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
227 local_addr_space.acc.access_mem = access_mem;
228 local_addr_space.acc.access_reg = access_reg;
229 local_addr_space.acc.access_fpreg = access_fpreg;
230 local_addr_space.acc.resume = arm_local_resume;
231 local_addr_space.acc.get_proc_name = get_static_proc_name;
232 unw_flush_cache (&local_addr_space, 0, 0);
235 #endif /* !UNW_REMOTE_ONLY */