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