Imported Upstream version 1.2
[platform/upstream/libunwind.git] / src / aarch64 / Gresume.c
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2008 CodeSourcery
3    Copyright (C) 2011-2013 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 aarch64_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 == AARCH64_SCF_NONE)
40     {
41       /* Since there are no signals involved here we restore EH and non scratch
42          registers only.  */
43       unsigned long regs[15];
44       regs[0] = uc->uc_mcontext.regs[0];
45       regs[1] = uc->uc_mcontext.regs[1];
46       regs[2] = uc->uc_mcontext.regs[2];
47       regs[3] = uc->uc_mcontext.regs[3];
48       regs[4] = uc->uc_mcontext.regs[19];
49       regs[5] = uc->uc_mcontext.regs[20];
50       regs[6] = uc->uc_mcontext.regs[21];
51       regs[7] = uc->uc_mcontext.regs[22];
52       regs[8] = uc->uc_mcontext.regs[23];
53       regs[9] = uc->uc_mcontext.regs[24];
54       regs[10] = uc->uc_mcontext.regs[25];
55       regs[11] = uc->uc_mcontext.regs[26];
56       regs[12] = uc->uc_mcontext.regs[27];
57       regs[13] = uc->uc_mcontext.regs[28];
58       regs[14] = uc->uc_mcontext.regs[30]; /* LR */
59       unsigned long sp = uc->uc_mcontext.sp;
60
61       struct regs_overlay {
62         char x[sizeof(regs)];
63       };
64
65       asm volatile (
66         "mov x4, %0\n"
67         "mov x5, %1\n"
68         "ldp x0,  x1,  [x4]\n"
69         "ldp x2,  x3,  [x4,16]\n"
70         "ldp x19, x20, [x4,32]\n"
71         "ldp x21, x22, [x4,48]\n"
72         "ldp x23, x24, [x4,64]\n"
73         "ldp x25, x26, [x4,80]\n"
74         "ldp x27, x28, [x4,96]\n"
75         "ldr x30, [x4,112]\n"
76         "mov sp, x5\n"
77         "ret \n"
78         :
79         : "r" (regs),
80           "r" (sp),
81           "m" (*(struct regs_overlay *)regs)
82       );
83     }
84   else
85     {
86       struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
87
88       if (c->dwarf.eh_valid_mask & 0x1) sc->regs[0] = c->dwarf.eh_args[0];
89       if (c->dwarf.eh_valid_mask & 0x2) sc->regs[1] = c->dwarf.eh_args[1];
90       if (c->dwarf.eh_valid_mask & 0x4) sc->regs[2] = c->dwarf.eh_args[2];
91       if (c->dwarf.eh_valid_mask & 0x8) sc->regs[3] = c->dwarf.eh_args[3];
92
93       sc->regs[4] = uc->uc_mcontext.regs[4];
94       sc->regs[5] = uc->uc_mcontext.regs[5];
95       sc->regs[6] = uc->uc_mcontext.regs[6];
96       sc->regs[7] = uc->uc_mcontext.regs[7];
97       sc->regs[8] = uc->uc_mcontext.regs[8];
98       sc->regs[9] = uc->uc_mcontext.regs[9];
99       sc->regs[10] = uc->uc_mcontext.regs[10];
100       sc->regs[11] = uc->uc_mcontext.regs[11];
101       sc->regs[12] = uc->uc_mcontext.regs[12];
102       sc->regs[13] = uc->uc_mcontext.regs[13];
103       sc->regs[14] = uc->uc_mcontext.regs[14];
104       sc->regs[15] = uc->uc_mcontext.regs[15];
105       sc->regs[16] = uc->uc_mcontext.regs[16];
106       sc->regs[17] = uc->uc_mcontext.regs[17];
107       sc->regs[18] = uc->uc_mcontext.regs[18];
108       sc->regs[19] = uc->uc_mcontext.regs[19];
109       sc->regs[20] = uc->uc_mcontext.regs[20];
110       sc->regs[21] = uc->uc_mcontext.regs[21];
111       sc->regs[22] = uc->uc_mcontext.regs[22];
112       sc->regs[23] = uc->uc_mcontext.regs[23];
113       sc->regs[24] = uc->uc_mcontext.regs[24];
114       sc->regs[25] = uc->uc_mcontext.regs[25];
115       sc->regs[26] = uc->uc_mcontext.regs[26];
116       sc->regs[27] = uc->uc_mcontext.regs[27];
117       sc->regs[28] = uc->uc_mcontext.regs[28];
118       sc->regs[29] = uc->uc_mcontext.regs[29];
119       sc->regs[30] = uc->uc_mcontext.regs[30];
120       sc->sp = uc->uc_mcontext.sp;
121       sc->pc = uc->uc_mcontext.pc;
122       sc->pstate = uc->uc_mcontext.pstate;
123
124       asm volatile (
125         "mov sp, %0\n"
126         "ret %1\n"
127         : : "r" (c->sigcontext_sp), "r" (c->sigcontext_pc)
128       );
129    }
130   unreachable();
131 #else
132   printf ("%s: implement me\n", __FUNCTION__);
133 #endif
134   return -UNW_EINVAL;
135 }
136
137 #endif /* !UNW_REMOTE_ONLY */
138
139 static inline void
140 establish_machine_state (struct cursor *c)
141 {
142   unw_addr_space_t as = c->dwarf.as;
143   void *arg = c->dwarf.as_arg;
144   unw_fpreg_t fpval;
145   unw_word_t val;
146   int reg;
147
148   Debug (8, "copying out cursor state\n");
149
150   for (reg = 0; reg <= UNW_AARCH64_PSTATE; ++reg)
151     {
152       Debug (16, "copying %s %d\n", unw_regname (reg), reg);
153       if (unw_is_fpreg (reg))
154         {
155           if (tdep_access_fpreg (c, reg, &fpval, 0) >= 0)
156             as->acc.access_fpreg (as, reg, &fpval, 1, arg);
157         }
158       else
159         {
160           if (tdep_access_reg (c, reg, &val, 0) >= 0)
161             as->acc.access_reg (as, reg, &val, 1, arg);
162         }
163     }
164 }
165
166 PROTECTED int
167 unw_resume (unw_cursor_t *cursor)
168 {
169   struct cursor *c = (struct cursor *) cursor;
170
171   Debug (1, "(cursor=%p)\n", c);
172
173   if (!c->dwarf.ip)
174     {
175       /* This can happen easily when the frame-chain gets truncated
176          due to bad or missing unwind-info.  */
177       Debug (1, "refusing to resume execution at address 0\n");
178       return -UNW_EINVAL;
179     }
180
181   establish_machine_state (c);
182
183   return (*c->dwarf.as->acc.resume) (c->dwarf.as, (unw_cursor_t *) c,
184                                      c->dwarf.as_arg);
185 }