1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2008 CodeSourcery
3   Copyright 2011 Linaro Limited
4   Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
5
6This file is part of libunwind.
7
8Permission is hereby granted, free of charge, to any person obtaining
9a copy of this software and associated documentation files (the
10"Software"), to deal in the Software without restriction, including
11without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense, and/or sell copies of the Software, and to
13permit persons to whom the Software is furnished to do so, subject to
14the following conditions:
15
16The above copyright notice and this permission notice shall be
17included in all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
26
27#if defined(__ANDROID__)
28#include <asm/sigcontext.h>
29#endif
30
31#include "unwind_i.h"
32#include "offsets.h"
33
34#ifndef UNW_REMOTE_ONLY
35
36HIDDEN inline int
37arm_local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
38{
39#ifdef __linux__
40  struct cursor *c = (struct cursor *) cursor;
41  unw_tdep_context_t *uc = c->dwarf.as_arg;
42
43  if (c->sigcontext_format == ARM_SCF_NONE)
44    {
45      /* Since there are no signals involved here we restore the non scratch
46	 registers only.  */
47      unsigned long regs[10];
48      regs[0] = uc->regs[4];
49      regs[1] = uc->regs[5];
50      regs[2] = uc->regs[6];
51      regs[3] = uc->regs[7];
52      regs[4] = uc->regs[8];
53      regs[5] = uc->regs[9];
54      regs[6] = uc->regs[10];
55      regs[7] = uc->regs[11]; /* FP */
56      regs[8] = uc->regs[13]; /* SP */
57      regs[9] = uc->regs[14]; /* LR */
58
59      struct regs_overlay {
60	      char x[sizeof(regs)];
61      };
62
63      asm __volatile__ (
64	"ldmia %0, {r4-r12, lr}\n"
65	"mov sp, r12\n"
66	"bx lr\n"
67	: : "r" (regs),
68	    "m" (*(struct regs_overlay *)regs)
69      );
70    }
71  else
72    {
73      /* In case a signal frame is involved, we're using its trampoline which
74	 calls sigreturn.  */
75      struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
76      sc->arm_r0 = uc->regs[0];
77      sc->arm_r1 = uc->regs[1];
78      sc->arm_r2 = uc->regs[2];
79      sc->arm_r3 = uc->regs[3];
80      sc->arm_r4 = uc->regs[4];
81      sc->arm_r5 = uc->regs[5];
82      sc->arm_r6 = uc->regs[6];
83      sc->arm_r7 = uc->regs[7];
84      sc->arm_r8 = uc->regs[8];
85      sc->arm_r9 = uc->regs[9];
86      sc->arm_r10 = uc->regs[10];
87      sc->arm_fp = uc->regs[11]; /* FP */
88      sc->arm_ip = uc->regs[12]; /* IP */
89      sc->arm_sp = uc->regs[13]; /* SP */
90      sc->arm_lr = uc->regs[14]; /* LR */
91      sc->arm_pc = uc->regs[15]; /* PC */
92      /* clear the ITSTATE bits.  */
93      sc->arm_cpsr &= 0xf9ff03ffUL;
94
95      /* Set the SP and the PC in order to continue execution at the modified
96	 trampoline which restores the signal mask and the registers.  */
97      asm __volatile__ (
98	"mov sp, %0\n"
99	"bx %1\n"
100	: : "r" (c->sigcontext_sp), "r" (c->sigcontext_pc)
101      );
102   }
103  unreachable();
104#else
105  printf ("%s: implement me\n", __FUNCTION__);
106#endif
107  return -UNW_EINVAL;
108}
109
110#endif /* !UNW_REMOTE_ONLY */
111
112static inline void
113establish_machine_state (struct cursor *c)
114{
115  unw_addr_space_t as = c->dwarf.as;
116  void *arg = c->dwarf.as_arg;
117  unw_fpreg_t fpval;
118  unw_word_t val;
119  int reg;
120
121  Debug (8, "copying out cursor state\n");
122
123  for (reg = 0; reg <= UNW_REG_LAST; ++reg)
124    {
125      Debug (16, "copying %s %d\n", unw_regname (reg), reg);
126      if (unw_is_fpreg (reg))
127	{
128	  if (tdep_access_fpreg (c, reg, &fpval, 0) >= 0)
129	    as->acc.access_fpreg (as, reg, &fpval, 1, arg);
130	}
131      else
132	{
133	  if (tdep_access_reg (c, reg, &val, 0) >= 0)
134	    as->acc.access_reg (as, reg, &val, 1, arg);
135	}
136    }
137}
138
139PROTECTED int
140unw_resume (unw_cursor_t *cursor)
141{
142  struct cursor *c = (struct cursor *) cursor;
143
144  Debug (1, "(cursor=%p)\n", c);
145
146  if (!c->dwarf.ip)
147    {
148      /* This can happen easily when the frame-chain gets truncated
149	 due to bad or missing unwind-info.  */
150      Debug (1, "refusing to resume execution at address 0\n");
151      return -UNW_EINVAL;
152    }
153
154  establish_machine_state (c);
155
156  return (*c->dwarf.as->acc.resume) (c->dwarf.as, (unw_cursor_t *) c,
157				     c->dwarf.as_arg);
158}
159