1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2008 CodeSourcery
3   Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
4
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25
26#include <stdlib.h>
27#include <string.h>
28
29#include "unwind_i.h"
30
31#ifdef UNW_REMOTE_ONLY
32
33/* unw_local_addr_space is a NULL pointer in this case.  */
34PROTECTED unw_addr_space_t unw_local_addr_space;
35
36#else /* !UNW_REMOTE_ONLY */
37
38static struct unw_addr_space local_addr_space;
39
40PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
41
42static inline void *
43uc_addr (ucontext_t *uc, int reg)
44{
45  if (reg >= UNW_SH_R0 && reg <= UNW_SH_PR)
46    return &uc->uc_mcontext.gregs[reg];
47  else
48    return NULL;
49}
50
51# ifdef UNW_LOCAL_ONLY
52
53HIDDEN void *
54tdep_uc_addr (ucontext_t *uc, int reg)
55{
56  return uc_addr (uc, reg);
57}
58
59# endif /* UNW_LOCAL_ONLY */
60
61HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
62
63/* XXX fix me: there is currently no way to locate the dyn-info list
64       by a remote unwinder.  On ia64, this is done via a special
65       unwind-table entry.  Perhaps something similar can be done with
66       DWARF2 unwind info.  */
67
68static void
69put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
70{
71  /* it's a no-op */
72}
73
74static int
75get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
76			void *arg)
77{
78  *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
79  return 0;
80}
81
82static int
83access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
84	    void *arg)
85{
86  if (write)
87    {
88      /* ANDROID support update. */
89#ifdef UNW_LOCAL_ONLY
90      if (map_local_is_writable (addr, sizeof(unw_word_t)))
91        {
92#endif
93          Debug (16, "mem[%x] <- %x\n", addr, *val);
94          *(unw_word_t *) addr = *val;
95#ifdef UNW_LOCAL_ONLY
96        }
97      else
98        {
99          Debug (16, "Unwritable memory mem[%x] <- %x\n", addr, *val);
100          return -1;
101        }
102#endif
103      /* End of ANDROID update. */
104    }
105  else
106    {
107      /* ANDROID support update. */
108#ifdef UNW_LOCAL_ONLY
109      if (map_local_is_readable (addr, sizeof(unw_word_t)))
110        {
111#endif
112          *val = *(unw_word_t *) addr;
113          Debug (16, "mem[%x] -> %x\n", addr, *val);
114#ifdef UNW_LOCAL_ONLY
115        }
116      else
117        {
118          Debug (16, "Unreadable memory mem[%x] -> XXX\n", addr);
119          return -1;
120        }
121#endif
122      /* End of ANDROID update. */
123    }
124  return 0;
125}
126
127static int
128access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
129	    void *arg)
130{
131  unw_word_t *addr;
132  ucontext_t *uc = arg;
133
134  if (unw_is_fpreg (reg))
135    goto badreg;
136
137  if (!(addr = uc_addr (uc, reg)))
138    goto badreg;
139
140  if (write)
141    {
142      *(unw_word_t *) addr = *val;
143      Debug (12, "%s <- %x\n", unw_regname (reg), *val);
144    }
145  else
146    {
147      *val = *(unw_word_t *) addr;
148      Debug (12, "%s -> %x\n", unw_regname (reg), *val);
149    }
150  return 0;
151
152 badreg:
153  Debug (1, "bad register number %u\n", reg);
154  return -UNW_EBADREG;
155}
156
157static int
158access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
159	      int write, void *arg)
160{
161  ucontext_t *uc = arg;
162  unw_fpreg_t *addr;
163
164  if (!unw_is_fpreg (reg))
165    goto badreg;
166
167  if (!(addr = uc_addr (uc, reg)))
168    goto badreg;
169
170  if (write)
171    {
172      Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
173	     ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
174      *(unw_fpreg_t *) addr = *val;
175    }
176  else
177    {
178      *val = *(unw_fpreg_t *) addr;
179      Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
180	     ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
181    }
182  return 0;
183
184 badreg:
185  Debug (1, "bad register number %u\n", reg);
186  /* attempt to access a non-preserved register */
187  return -UNW_EBADREG;
188}
189
190static int
191get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
192		      char *buf, size_t buf_len, unw_word_t *offp,
193		      void *arg)
194{
195  return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp, arg);
196}
197
198HIDDEN void
199sh_local_addr_space_init (void)
200{
201  memset (&local_addr_space, 0, sizeof (local_addr_space));
202  local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
203  local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
204  local_addr_space.acc.put_unwind_info = put_unwind_info;
205  local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
206  local_addr_space.acc.access_mem = access_mem;
207  local_addr_space.acc.access_reg = access_reg;
208  local_addr_space.acc.access_fpreg = access_fpreg;
209  local_addr_space.acc.resume = sh_local_resume;
210  local_addr_space.acc.get_proc_name = get_static_proc_name;
211  unw_flush_cache (&local_addr_space, 0, 0);
212
213  map_local_init ();
214}
215
216#endif /* !UNW_REMOTE_ONLY */
217