b14e419bdd30a561fdf0f3bcee9cb6d267fd0919
[platform/upstream/libunwind.git] / src / sh / 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 sh_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 == SH_SCF_NONE)
40     {
41       /* Since there are no signals involved here we restore the non scratch
42          registers only.  */
43       unsigned long regs[8];
44       regs[0] = uc->uc_mcontext.gregs[8];
45       regs[1] = uc->uc_mcontext.gregs[9];
46       regs[2] = uc->uc_mcontext.gregs[10];
47       regs[3] = uc->uc_mcontext.gregs[11];
48       regs[4] = uc->uc_mcontext.gregs[12];
49       regs[5] = uc->uc_mcontext.gregs[13];
50       regs[6] = uc->uc_mcontext.gregs[14];
51       regs[7] = uc->uc_mcontext.gregs[15];
52       unsigned long pc = uc->uc_mcontext.pr;
53
54       struct regs_overlay {
55         char x[sizeof(regs)];
56       };
57
58       asm volatile (
59         "mov.l @%0+, r8\n"
60         "mov.l @%0+, r9\n"
61         "mov.l @%0+, r10\n"
62         "mov.l @%0+, r11\n"
63         "mov.l @%0+, r12\n"
64         "mov.l @%0+, r13\n"
65         "mov.l @%0+, r14\n"
66         "mov.l @%0,  r15\n"
67         "lds %1, pr\n"
68         "rts\n"
69         "nop\n"
70         :
71         : "r" (regs),
72           "r" (pc),
73           "m" (*(struct regs_overlay *)regs)
74       );
75     }
76   else
77     {
78       /* In case a signal frame is involved, we're using its trampoline which
79          calls sigreturn.  */
80       struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
81       sc->sc_regs[0] = uc->uc_mcontext.gregs[0];
82       sc->sc_regs[1] = uc->uc_mcontext.gregs[1];
83       sc->sc_regs[2] = uc->uc_mcontext.gregs[2];
84       sc->sc_regs[3] = uc->uc_mcontext.gregs[3];
85       sc->sc_regs[4] = uc->uc_mcontext.gregs[4];
86       sc->sc_regs[5] = uc->uc_mcontext.gregs[5];
87       sc->sc_regs[6] = uc->uc_mcontext.gregs[6];
88       sc->sc_regs[7] = uc->uc_mcontext.gregs[7];
89       sc->sc_regs[8] = uc->uc_mcontext.gregs[8];
90       sc->sc_regs[9] = uc->uc_mcontext.gregs[9];
91       sc->sc_regs[10] = uc->uc_mcontext.gregs[10];
92       sc->sc_regs[11] = uc->uc_mcontext.gregs[11];
93       sc->sc_regs[12] = uc->uc_mcontext.gregs[12];
94       sc->sc_regs[13] = uc->uc_mcontext.gregs[13];
95       sc->sc_regs[14] = uc->uc_mcontext.gregs[14];
96       sc->sc_regs[15] = uc->uc_mcontext.gregs[15];
97       sc->sc_pc = uc->uc_mcontext.pc;
98       sc->sc_pr = uc->uc_mcontext.pr;
99
100       /* Set the SP and the PC in order to continue execution at the modified
101          trampoline which restores the signal mask and the registers.  */
102       asm __volatile__ (
103         "mov %0, r15\n"
104         "lds %1, pr\n"
105         "rts\n"
106         "nop\n"
107         :
108         : "r" (c->sigcontext_sp),
109           "r" (c->sigcontext_pc)
110       );
111    }
112   unreachable();
113 #endif
114   return -UNW_EINVAL;
115 }
116
117 #endif /* !UNW_REMOTE_ONLY */
118
119 static inline void
120 establish_machine_state (struct cursor *c)
121 {
122   unw_addr_space_t as = c->dwarf.as;
123   void *arg = c->dwarf.as_arg;
124   unw_fpreg_t fpval;
125   unw_word_t val;
126   int reg;
127
128   Debug (8, "copying out cursor state\n");
129
130   for (reg = 0; reg <= UNW_REG_LAST; ++reg)
131     {
132       Debug (16, "copying %s %d\n", unw_regname (reg), reg);
133       if (unw_is_fpreg (reg))
134         {
135           if (tdep_access_fpreg (c, reg, &fpval, 0) >= 0)
136             as->acc.access_fpreg (as, reg, &fpval, 1, arg);
137         }
138       else
139         {
140           if (tdep_access_reg (c, reg, &val, 0) >= 0)
141             as->acc.access_reg (as, reg, &val, 1, arg);
142         }
143     }
144 }
145
146 PROTECTED int
147 unw_resume (unw_cursor_t *cursor)
148 {
149   struct cursor *c = (struct cursor *) cursor;
150
151   Debug (1, "(cursor=%p)\n", c);
152
153   if (!c->dwarf.ip)
154     {
155       /* This can happen easily when the frame-chain gets truncated
156          due to bad or missing unwind-info.  */
157       Debug (1, "refusing to resume execution at address 0\n");
158       return -UNW_EINVAL;
159     }
160
161   establish_machine_state (c);
162
163   return (*c->dwarf.as->acc.resume) (c->dwarf.as, (unw_cursor_t *) c,
164                                      c->dwarf.as_arg);
165 }