Make UEFI boot-platform build again
[haiku.git] / src / libs / libunwind / ppc64 / Ginit.c
blob0740961085169e00708a10e3a46b88933dc42df2
1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2006-2007 IBM
3 Contributed by
4 Corey Ashford <cjashfor@us.ibm.com>
5 Jose Flavio Aguilar Paulino <jflavio@br.ibm.com> <joseflavio@gmail.com>
7 This file is part of libunwind.
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:
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
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. */
28 #include <stdlib.h>
29 #include <string.h>
31 #include "ucontext_i.h"
32 #include "unwind_i.h"
34 #ifdef UNW_REMOTE_ONLY
36 /* unw_local_addr_space is a NULL pointer in this case. */
37 PROTECTED unw_addr_space_t unw_local_addr_space;
39 #else /* !UNW_REMOTE_ONLY */
41 static struct unw_addr_space local_addr_space;
43 PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
45 static void *
46 uc_addr (ucontext_t *uc, int reg)
48 void *addr;
50 if ((unsigned) (reg - UNW_PPC64_R0) < 32)
51 addr = &uc->uc_mcontext.gp_regs[reg - UNW_PPC64_R0];
53 else if ((unsigned) (reg - UNW_PPC64_F0) < 32)
54 addr = &uc->uc_mcontext.fp_regs[reg - UNW_PPC64_F0];
56 else if ((unsigned) (reg - UNW_PPC64_V0) < 32)
57 addr = (uc->uc_mcontext.v_regs == 0) ? NULL : &uc->uc_mcontext.v_regs->vrregs[reg - UNW_PPC64_V0][0];
59 else
61 unsigned gregs_idx;
63 switch (reg)
65 case UNW_PPC64_NIP:
66 gregs_idx = NIP_IDX;
67 break;
68 case UNW_PPC64_CTR:
69 gregs_idx = CTR_IDX;
70 break;
71 case UNW_PPC64_LR:
72 gregs_idx = LINK_IDX;
73 break;
74 case UNW_PPC64_XER:
75 gregs_idx = XER_IDX;
76 break;
77 case UNW_PPC64_CR0:
78 gregs_idx = CCR_IDX;
79 break;
80 default:
81 return NULL;
83 addr = &uc->uc_mcontext.gp_regs[gregs_idx];
85 return addr;
88 # ifdef UNW_LOCAL_ONLY
90 HIDDEN void *
91 tdep_uc_addr (ucontext_t *uc, int reg)
93 return uc_addr (uc, reg);
96 # endif /* UNW_LOCAL_ONLY */
98 HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
101 static void
102 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
104 /* it's a no-op */
107 static int
108 get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
109 void *arg)
111 *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
112 return 0;
115 static int
116 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
117 void *arg)
119 if (write)
121 Debug (12, "mem[%lx] <- %lx\n", addr, *val);
122 *(unw_word_t *) addr = *val;
124 else
126 *val = *(unw_word_t *) addr;
127 Debug (12, "mem[%lx] -> %lx\n", addr, *val);
129 return 0;
132 static int
133 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
134 int write, void *arg)
136 unw_word_t *addr;
137 ucontext_t *uc = arg;
139 if (UNW_PPC64_F0 <= reg && reg <= UNW_PPC64_F31)
140 goto badreg;
141 if (UNW_PPC64_V0 <= reg && reg <= UNW_PPC64_V31)
142 goto badreg;
144 addr = uc_addr (uc, reg);
145 if (!addr)
146 goto badreg;
148 if (write)
150 *(unw_word_t *) addr = *val;
151 Debug (12, "%s <- %lx\n", unw_regname (reg), *val);
153 else
155 *val = *(unw_word_t *) addr;
156 Debug (12, "%s -> %lx\n", unw_regname (reg), *val);
158 return 0;
160 badreg:
161 Debug (1, "bad register number %u\n", reg);
162 return -UNW_EBADREG;
165 static int
166 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
167 int write, void *arg)
169 ucontext_t *uc = arg;
170 unw_fpreg_t *addr;
172 if ((reg - UNW_PPC64_F0) < 0)
173 goto badreg;
175 if ((unsigned) (reg - UNW_PPC64_V0) >= 32)
176 goto badreg;
179 addr = uc_addr (uc, reg);
180 if (!addr)
181 goto badreg;
183 if (write)
185 Debug (12, "%s <- %016Lf\n", unw_regname (reg), *val);
186 *(unw_fpreg_t *) addr = *val;
188 else
190 *val = *(unw_fpreg_t *) addr;
191 Debug (12, "%s -> %016Lf\n", unw_regname (reg), *val);
193 return 0;
195 badreg:
196 Debug (1, "bad register number %u\n", reg);
197 /* attempt to access a non-preserved register */
198 return -UNW_EBADREG;
201 static int
202 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
203 char *buf, size_t buf_len, unw_word_t *offp,
204 void *arg)
206 return _Uelf64_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
209 HIDDEN void
210 ppc64_local_addr_space_init (void)
212 memset (&local_addr_space, 0, sizeof (local_addr_space));
213 local_addr_space.big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
214 #if _CALL_ELF == 2
215 local_addr_space.abi = UNW_PPC64_ABI_ELFv2;
216 #else
217 local_addr_space.abi = UNW_PPC64_ABI_ELFv1;
218 #endif
219 local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
220 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
221 local_addr_space.acc.put_unwind_info = put_unwind_info;
222 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
223 local_addr_space.acc.access_mem = access_mem;
224 local_addr_space.acc.access_reg = access_reg;
225 local_addr_space.acc.access_fpreg = access_fpreg;
226 local_addr_space.acc.resume = ppc64_local_resume;
227 local_addr_space.acc.get_proc_name = get_static_proc_name;
228 unw_flush_cache (&local_addr_space, 0, 0);
231 #endif /* !UNW_REMOTE_ONLY */