Ginit.c revision 9ea6af8ed5ee7b07f8de6d774bc6df2b7320f0e8
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>
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_PPC32_R0) < 32)
56    addr = &uc->uc_mcontext.uc_regs->gregs[reg - UNW_PPC32_R0];
57
58  else
59  if ( ((unsigned) (reg - UNW_PPC32_F0) < 32) &&
60       ((unsigned) (reg - UNW_PPC32_F0) >= 0) )
61    addr = &uc->uc_mcontext.uc_regs->fpregs.fpregs[reg - UNW_PPC32_F0];
62
63  else
64    {
65      unsigned gregs_idx;
66
67      switch (reg)
68	{
69	case UNW_PPC32_CTR:
70	  gregs_idx = CTR_IDX;
71	  break;
72	case UNW_PPC32_LR:
73	  gregs_idx = LINK_IDX;
74	  break;
75	case UNW_PPC32_XER:
76	  gregs_idx = XER_IDX;
77	  break;
78	case UNW_PPC32_CCR:
79	  gregs_idx = CCR_IDX;
80	  break;
81	default:
82	  return NULL;
83	}
84      addr = &uc->uc_mcontext.uc_regs->gregs[gregs_idx];
85    }
86  return addr;
87}
88
89# ifdef UNW_LOCAL_ONLY
90
91HIDDEN void *
92tdep_uc_addr (ucontext_t *uc, int reg)
93{
94  return uc_addr (uc, reg);
95}
96
97# endif	/* UNW_LOCAL_ONLY */
98
99HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
100
101
102static void
103put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
104{
105  /* it's a no-op */
106}
107
108static int
109get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
110			void *arg)
111{
112  *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
113  return 0;
114}
115
116static int
117access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
118	    void *arg)
119{
120  if (write)
121    {
122      Debug (12, "mem[%lx] <- %lx\n", addr, *val);
123      *(unw_word_t *) addr = *val;
124    }
125  else
126    {
127      *val = *(unw_word_t *) addr;
128      Debug (12, "mem[%lx] -> %lx\n", addr, *val);
129    }
130  return 0;
131}
132
133static int
134access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
135	    int write, void *arg)
136{
137  unw_word_t *addr;
138  ucontext_t *uc = arg;
139
140  if ( ((unsigned int) (reg - UNW_PPC32_F0) < 32) &&
141       ((unsigned int) (reg - UNW_PPC32_F0) >= 0))
142    goto badreg;
143
144  addr = uc_addr (uc, reg);
145  if (!addr)
146    goto badreg;
147
148  if (write)
149    {
150      *(unw_word_t *) addr = *val;
151      Debug (12, "%s <- %lx\n", unw_regname (reg), *val);
152    }
153  else
154    {
155      *val = *(unw_word_t *) addr;
156      Debug (12, "%s -> %lx\n", unw_regname (reg), *val);
157    }
158  return 0;
159
160badreg:
161  Debug (1, "bad register number %u\n", reg);
162  return -UNW_EBADREG;
163}
164
165static int
166access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
167	      int write, void *arg)
168{
169  ucontext_t *uc = arg;
170  unw_fpreg_t *addr;
171
172  if ((unsigned) (reg - UNW_PPC32_F0) < 0)
173    goto badreg;
174
175  addr = uc_addr (uc, reg);
176  if (!addr)
177    goto badreg;
178
179  if (write)
180    {
181      Debug (12, "%s <- %016Lf\n", unw_regname (reg), *val);
182      *(unw_fpreg_t *) addr = *val;
183    }
184  else
185    {
186      *val = *(unw_fpreg_t *) addr;
187      Debug (12, "%s -> %016Lf\n", unw_regname (reg), *val);
188    }
189  return 0;
190
191badreg:
192  Debug (1, "bad register number %u\n", reg);
193  /* attempt to access a non-preserved register */
194  return -UNW_EBADREG;
195}
196
197static int
198get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
199		      char *buf, size_t buf_len, unw_word_t *offp,
200		      void *arg)
201{
202  return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
203}
204
205HIDDEN void
206ppc32_local_addr_space_init (void)
207{
208  memset (&local_addr_space, 0, sizeof (local_addr_space));
209  local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
210  local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
211  local_addr_space.acc.put_unwind_info = put_unwind_info;
212  local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
213  local_addr_space.acc.access_mem = access_mem;
214  local_addr_space.acc.access_reg = access_reg;
215  local_addr_space.acc.access_fpreg = access_fpreg;
216  local_addr_space.acc.resume = ppc32_local_resume;
217  local_addr_space.acc.get_proc_name = get_static_proc_name;
218  unw_flush_cache (&local_addr_space, 0, 0);
219}
220
221#endif /* !UNW_REMOTE_ONLY */
222