Make UEFI boot-platform build again
[haiku.git] / src / libs / libunwind / x86 / Ginit.c
blob17cbb0a450f89b959d308702ce2d55e7f818a5e1
1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2002 Hewlett-Packard Co
3 Copyright (C) 2007 David Mosberger-Tang
4 Contributed by David Mosberger-Tang <dmosberger@gmail.com>
6 This file is part of libunwind.
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:
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
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. */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
31 #include <stdlib.h>
32 #include <string.h>
34 #include "unwind_i.h"
36 #ifdef UNW_REMOTE_ONLY
38 /* unw_local_addr_space is a NULL pointer in this case. */
39 PROTECTED unw_addr_space_t unw_local_addr_space;
41 #else /* !UNW_REMOTE_ONLY */
43 static struct unw_addr_space local_addr_space;
45 PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
47 # ifdef UNW_LOCAL_ONLY
49 HIDDEN void *
50 tdep_uc_addr (ucontext_t *uc, int reg)
52 return x86_r_uc_addr (uc, reg);
55 # endif /* UNW_LOCAL_ONLY */
57 HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
59 /* XXX fix me: there is currently no way to locate the dyn-info list
60 by a remote unwinder. On ia64, this is done via a special
61 unwind-table entry. Perhaps something similar can be done with
62 DWARF2 unwind info. */
64 static void
65 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
67 /* it's a no-op */
70 static int
71 get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
72 void *arg)
74 *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
75 return 0;
78 #define PAGE_SIZE 4096
79 #define PAGE_START(a) ((a) & ~(PAGE_SIZE-1))
81 /* Cache of already validated addresses */
82 #define NLGA 4
83 static unw_word_t last_good_addr[NLGA];
84 static int lga_victim;
86 static int
87 validate_mem (unw_word_t addr)
89 int i, victim;
90 #ifdef HAVE_MINCORE
91 unsigned char mvec[2]; /* Unaligned access may cross page boundary */
92 #endif
93 size_t len;
95 if (PAGE_START(addr + sizeof (unw_word_t) - 1) == PAGE_START(addr))
96 len = PAGE_SIZE;
97 else
98 len = PAGE_SIZE * 2;
100 addr = PAGE_START(addr);
102 if (addr == 0)
103 return -1;
105 for (i = 0; i < NLGA; i++)
107 if (last_good_addr[i] && (addr == last_good_addr[i]))
108 return 0;
111 #ifdef HAVE_MINCORE
112 if (mincore ((void *) addr, len, mvec) == -1)
113 #else
114 if (msync ((void *) addr, len, MS_ASYNC) == -1)
115 #endif
116 return -1;
118 victim = lga_victim;
119 for (i = 0; i < NLGA; i++) {
120 if (!last_good_addr[victim]) {
121 last_good_addr[victim++] = addr;
122 return 0;
124 victim = (victim + 1) % NLGA;
127 /* All slots full. Evict the victim. */
128 last_good_addr[victim] = addr;
129 victim = (victim + 1) % NLGA;
130 lga_victim = victim;
132 return 0;
135 static int
136 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
137 void *arg)
139 if (write)
141 Debug (16, "mem[%x] <- %x\n", addr, *val);
142 *(unw_word_t *) addr = *val;
144 else
146 /* validate address */
147 const struct cursor *c = (const struct cursor *)arg;
148 if (c && c->validate && validate_mem(addr))
149 return -1;
150 *val = *(unw_word_t *) addr;
151 Debug (16, "mem[%x] -> %x\n", addr, *val);
153 return 0;
156 static int
157 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
158 void *arg)
160 unw_word_t *addr;
161 ucontext_t *uc = ((struct cursor *)arg)->uc;
163 if (unw_is_fpreg (reg))
164 goto badreg;
166 if (!(addr = x86_r_uc_addr (uc, reg)))
167 goto badreg;
169 if (write)
171 *(unw_word_t *) addr = *val;
172 Debug (12, "%s <- %x\n", unw_regname (reg), *val);
174 else
176 *val = *(unw_word_t *) addr;
177 Debug (12, "%s -> %x\n", unw_regname (reg), *val);
179 return 0;
181 badreg:
182 Debug (1, "bad register number %u\n", reg);
183 return -UNW_EBADREG;
186 static int
187 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
188 int write, void *arg)
190 ucontext_t *uc = ((struct cursor *)arg)->uc;
191 unw_fpreg_t *addr;
193 if (!unw_is_fpreg (reg))
194 goto badreg;
196 if (!(addr = x86_r_uc_addr (uc, reg)))
197 goto badreg;
199 if (write)
201 Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
202 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
203 *(unw_fpreg_t *) addr = *val;
205 else
207 *val = *(unw_fpreg_t *) addr;
208 Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
209 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
211 return 0;
213 badreg:
214 Debug (1, "bad register number %u\n", reg);
215 /* attempt to access a non-preserved register */
216 return -UNW_EBADREG;
219 static int
220 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
221 char *buf, size_t buf_len, unw_word_t *offp,
222 void *arg)
224 return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
227 HIDDEN void
228 x86_local_addr_space_init (void)
230 memset (&local_addr_space, 0, sizeof (local_addr_space));
231 local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
232 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
233 local_addr_space.acc.put_unwind_info = put_unwind_info;
234 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
235 local_addr_space.acc.access_mem = access_mem;
236 local_addr_space.acc.access_reg = access_reg;
237 local_addr_space.acc.access_fpreg = access_fpreg;
238 local_addr_space.acc.resume = x86_local_resume;
239 local_addr_space.acc.get_proc_name = get_static_proc_name;
240 unw_flush_cache (&local_addr_space, 0, 0);
243 #endif /* !UNW_REMOTE_ONLY */