Initial import
[external/libunwind.git] / src / mips / Ginit.c
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2008 CodeSourcery
3
4 This file is part of libunwind.
5
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:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
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.  */
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "unwind_i.h"
29
30 #ifdef UNW_REMOTE_ONLY
31
32 /* unw_local_addr_space is a NULL pointer in this case.  */
33 PROTECTED unw_addr_space_t unw_local_addr_space;
34
35 #else /* !UNW_REMOTE_ONLY */
36
37 static struct unw_addr_space local_addr_space;
38
39 PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
40
41 /* Return the address of the 64-bit slot in UC for REG (even for o32,
42    where registers are 32-bit, the slots are still 64-bit).  */
43
44 static inline void *
45 uc_addr (ucontext_t *uc, int reg)
46 {
47   if (reg >= UNW_MIPS_R0 && reg < UNW_MIPS_R0 + 32)
48     return &uc->uc_mcontext.gregs[reg - UNW_MIPS_R0];
49   else if (reg == UNW_MIPS_PC)
50     return &uc->uc_mcontext.pc;
51   else
52     return NULL;
53 }
54
55 # ifdef UNW_LOCAL_ONLY
56
57 HIDDEN void *
58 tdep_uc_addr (ucontext_t *uc, int reg)
59 {
60   char *addr = uc_addr (uc, reg);
61
62   if (reg >= UNW_MIPS_R0 && reg <= UNW_MIPS_R31
63       && tdep_big_endian (unw_local_addr_space)
64       && unw_local_addr_space->abi == UNW_MIPS_ABI_O32)
65     addr += 4;
66
67   return addr;
68 }
69
70 # endif /* UNW_LOCAL_ONLY */
71
72 HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
73
74 /* XXX fix me: there is currently no way to locate the dyn-info list
75        by a remote unwinder.  On ia64, this is done via a special
76        unwind-table entry.  Perhaps something similar can be done with
77        DWARF2 unwind info.  */
78
79 static void
80 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
81 {
82   /* it's a no-op */
83 }
84
85 static int
86 get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
87                         void *arg)
88 {
89   *dyn_info_list_addr = (unw_word_t) (intptr_t) &_U_dyn_info_list;
90   return 0;
91 }
92
93 static int
94 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
95             void *arg)
96 {
97   if (write)
98     {
99       Debug (16, "mem[%llx] <- %llx\n", (long long) addr, (long long) *val);
100       *(unw_word_t *) (intptr_t) addr = *val;
101     }
102   else
103     {
104       *val = *(unw_word_t *) (intptr_t) addr;
105       Debug (16, "mem[%llx] -> %llx\n", (long long) addr, (long long) *val);
106     }
107   return 0;
108 }
109
110 static int
111 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
112             void *arg)
113 {
114   unw_word_t *addr;
115   ucontext_t *uc = arg;
116
117   if (unw_is_fpreg (reg))
118     goto badreg;
119
120   Debug (16, "reg = %s\n", unw_regname (reg));
121   if (!(addr = uc_addr (uc, reg)))
122     goto badreg;
123
124   if (write)
125     {
126       *(unw_word_t *) (intptr_t) addr = (mips_reg_t) *val;
127       Debug (12, "%s <- %llx\n", unw_regname (reg), (long long) *val);
128     }
129   else
130     {
131       *val = (mips_reg_t) *(unw_word_t *) (intptr_t) addr;
132       Debug (12, "%s -> %llx\n", unw_regname (reg), (long long) *val);
133     }
134   return 0;
135
136  badreg:
137   Debug (1, "bad register number %u\n", reg);
138   return -UNW_EBADREG;
139 }
140
141 static int
142 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
143               int write, void *arg)
144 {
145   ucontext_t *uc = arg;
146   unw_fpreg_t *addr;
147
148   if (!unw_is_fpreg (reg))
149     goto badreg;
150
151   if (!(addr = uc_addr (uc, reg)))
152     goto badreg;
153
154   if (write)
155     {
156       Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
157              ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
158       *(unw_fpreg_t *) (intptr_t) addr = *val;
159     }
160   else
161     {
162       *val = *(unw_fpreg_t *) (intptr_t) addr;
163       Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
164              ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
165     }
166   return 0;
167
168  badreg:
169   Debug (1, "bad register number %u\n", reg);
170   /* attempt to access a non-preserved register */
171   return -UNW_EBADREG;
172 }
173
174 static int
175 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
176                       char *buf, size_t buf_len, unw_word_t *offp,
177                       void *arg)
178 {
179
180   return elf_w (get_proc_name) (as, getpid (), ip, buf, buf_len, offp);
181 }
182
183 HIDDEN void
184 mips_local_addr_space_init (void)
185 {
186   memset (&local_addr_space, 0, sizeof (local_addr_space));
187   local_addr_space.big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
188 #if _MIPS_SIM == _ABIO32
189   local_addr_space.abi = UNW_MIPS_ABI_O32;
190 #elif _MIPS_SIM == _ABIN32
191   local_addr_space.abi = UNW_MIPS_ABI_N32;
192 #elif _MIPS_SIM == _ABI64
193   local_addr_space.abi = UNW_MIPS_ABI_N64;
194 #else
195 # error Unsupported ABI
196 #endif
197   local_addr_space.addr_size = sizeof (void *);
198   local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
199   local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
200   local_addr_space.acc.put_unwind_info = put_unwind_info;
201   local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
202   local_addr_space.acc.access_mem = access_mem;
203   local_addr_space.acc.access_reg = access_reg;
204   local_addr_space.acc.access_fpreg = access_fpreg;
205   local_addr_space.acc.resume = NULL;  /* mips_local_resume?  FIXME!  */
206   local_addr_space.acc.get_proc_name = get_static_proc_name;
207   unw_flush_cache (&local_addr_space, 0, 0);
208 }
209
210 #endif /* !UNW_REMOTE_ONLY */