blob: 97c9d78cbd61bad66c67b41b7c093fcfe2321715 [file] [log] [blame]
Jose Flavio Aguilar Paulinob33021e2007-08-02 09:59:43 -06001/* 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>
6
7This file is part of libunwind.
8
9Permission is hereby granted, free of charge, to any person obtaining
10a copy of this software and associated documentation files (the
11"Software"), to deal in the Software without restriction, including
12without limitation the rights to use, copy, modify, merge, publish,
13distribute, sublicense, and/or sell copies of the Software, and to
14permit persons to whom the Software is furnished to do so, subject to
15the following conditions:
16
17The above copyright notice and this permission notice shall be
18included in all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
27
28#include <stdlib.h>
29#include <string.h>
30
31#include "ucontext_i.h"
32#include "unwind_i.h"
33
34#ifdef UNW_REMOTE_ONLY
35
36/* unw_local_addr_space is a NULL pointer in this case. */
37PROTECTED unw_addr_space_t unw_local_addr_space;
38
39#else /* !UNW_REMOTE_ONLY */
40
41static struct unw_addr_space local_addr_space;
42
43PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
44
45
46#define PAGE_SIZE 4096
47#define PAGE_START(a) ((a) & ~(PAGE_SIZE-1))
48
49
50static void *
51uc_addr (ucontext_t *uc, int reg)
52{
53 void *addr;
54
55 if ((unsigned) (reg - UNW_PPC64_R0) < 32)
56 addr = &uc->uc_mcontext.gp_regs[reg - UNW_PPC64_R0];
57
58 else if ((unsigned) (reg - UNW_PPC64_F0) < 32)
59 addr = &uc->uc_mcontext.fp_regs[reg - UNW_PPC64_F0];
60
61 else if ((unsigned) (reg - UNW_PPC64_V0) < 32)
62 addr = (uc->uc_mcontext.v_regs == 0) ? NULL : &uc->uc_mcontext.v_regs->vrregs[reg - UNW_PPC64_V0][0];
63
64 else
65 {
66 unsigned gregs_idx;
67
68 switch (reg)
69 {
70 case UNW_PPC64_NIP:
71 gregs_idx = NIP_IDX;
72 break;
73 case UNW_PPC64_CTR:
74 gregs_idx = CTR_IDX;
75 break;
76 case UNW_PPC64_LR:
77 gregs_idx = LINK_IDX;
78 break;
79 case UNW_PPC64_XER:
80 gregs_idx = XER_IDX;
81 break;
82 case UNW_PPC64_CR0:
83 gregs_idx = CCR_IDX;
84 break;
85 default:
86 return NULL;
87 }
88 addr = &uc->uc_mcontext.gp_regs[gregs_idx];
89 }
90 return addr;
91}
92
93# ifdef UNW_LOCAL_ONLY
94
95HIDDEN void *
96tdep_uc_addr (ucontext_t *uc, int reg)
97{
98 return uc_addr (uc, reg);
99}
100
101# endif /* UNW_LOCAL_ONLY */
102
103HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
104
105
106static void
107put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
108{
109 /* it's a no-op */
110}
111
112static int
113get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
114 void *arg)
115{
116 *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
117 return 0;
118}
119
120static int
121access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
122 void *arg)
123{
124 if (write)
125 {
126 Debug (12, "mem[%lx] <- %lx\n", addr, *val);
127 *(unw_word_t *) addr = *val;
128 }
129 else
130 {
131 *val = *(unw_word_t *) addr;
132 Debug (12, "mem[%lx] -> %lx\n", addr, *val);
133 }
134 return 0;
135}
136
137static int
138access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
139 int write, void *arg)
140{
141 unw_word_t *addr;
142 ucontext_t *uc = arg;
143
144 if ((unsigned int) (reg - UNW_PPC64_F0) < 32)
145 goto badreg;
146 if ((unsigned int) (reg - UNW_PPC64_V0) < 32)
147 goto badreg;
148
149 addr = uc_addr (uc, reg);
150 if (!addr)
151 goto badreg;
152
153 if (write)
154 {
155 *(unw_word_t *) addr = *val;
156 Debug (12, "%s <- %lx\n", unw_regname (reg), *val);
157 }
158 else
159 {
160 *val = *(unw_word_t *) addr;
161 Debug (12, "%s -> %lx\n", unw_regname (reg), *val);
162 }
163 return 0;
164
165badreg:
166 Debug (1, "bad register number %u\n", reg);
167 return -UNW_EBADREG;
168}
169
170static int
171access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
172 int write, void *arg)
173{
174 ucontext_t *uc = arg;
175 unw_fpreg_t *addr;
176
177 if ((unsigned) (reg - UNW_PPC64_F0) < 0)
178 goto badreg;
179
180 if ((unsigned) (reg - UNW_PPC64_V0) >= 32)
181 goto badreg;
182
183
184 addr = uc_addr (uc, reg);
185 if (!addr)
186 goto badreg;
187
188 if (write)
189 {
190 Debug (12, "%s <- %016Lf\n", unw_regname (reg), *val);
191 *(unw_fpreg_t *) addr = *val;
192 }
193 else
194 {
195 *val = *(unw_fpreg_t *) addr;
196 Debug (12, "%s -> %016Lf\n", unw_regname (reg), *val);
197 }
198 return 0;
199
200badreg:
201 Debug (1, "bad register number %u\n", reg);
202 /* attempt to access a non-preserved register */
203 return -UNW_EBADREG;
204}
205
206static int
207get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
208 char *buf, size_t buf_len, unw_word_t *offp,
209 void *arg)
210{
David Mosberger-Tange6b9f352007-08-22 13:02:09 -0600211 return _Uelf64_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
Jose Flavio Aguilar Paulinob33021e2007-08-02 09:59:43 -0600212}
213
214HIDDEN void
215ppc64_local_addr_space_init (void)
216{
217 memset (&local_addr_space, 0, sizeof (local_addr_space));
218 local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
219 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
220 local_addr_space.acc.put_unwind_info = put_unwind_info;
221 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
222 local_addr_space.acc.access_mem = access_mem;
223 local_addr_space.acc.access_reg = access_reg;
224 local_addr_space.acc.access_fpreg = access_fpreg;
225 local_addr_space.acc.resume = ppc64_local_resume;
226 local_addr_space.acc.get_proc_name = get_static_proc_name;
227 unw_flush_cache (&local_addr_space, 0, 0);
228}
229
230#endif /* !UNW_REMOTE_ONLY */