Imported Upstream version 1.3.1
[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[24];
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[29]; /* FP */
59       regs[15] = uc->uc_mcontext.regs[30]; /* LR */
60       regs[16] = GET_FPCTX(uc)->vregs[8];
61       regs[17] = GET_FPCTX(uc)->vregs[9];
62       regs[18] = GET_FPCTX(uc)->vregs[10];
63       regs[19] = GET_FPCTX(uc)->vregs[11];
64       regs[20] = GET_FPCTX(uc)->vregs[12];
65       regs[21] = GET_FPCTX(uc)->vregs[13];
66       regs[22] = GET_FPCTX(uc)->vregs[14];
67       regs[23] = GET_FPCTX(uc)->vregs[15];
68       unsigned long sp = uc->uc_mcontext.sp;
69
70       struct regs_overlay {
71         char x[sizeof(regs)];
72       };
73
74       asm volatile (
75         "mov x4, %0\n"
76         "mov x5, %1\n"
77         "ldp x0,  x1,  [x4]\n"
78         "ldp x2,  x3,  [x4,16]\n"
79         "ldp x19, x20, [x4,32]\n"
80         "ldp x21, x22, [x4,48]\n"
81         "ldp x23, x24, [x4,64]\n"
82         "ldp x25, x26, [x4,80]\n"
83         "ldp x27, x28, [x4,96]\n"
84         "ldp x29, x30, [x4,112]\n"
85         "ldp d8, d9,   [x4,128]\n"
86         "ldp d10, d11, [x4,144]\n"
87         "ldp d12, d13, [x4,160]\n"
88         "ldp d14, d15, [x4,176]\n"
89         "mov sp, x5\n"
90         "ret \n"
91         :
92         : "r" (regs),
93           "r" (sp),
94           "m" (*(struct regs_overlay *)regs)
95       );
96     }
97   else
98     {
99       struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
100
101       if (c->dwarf.eh_valid_mask & 0x1) sc->regs[0] = c->dwarf.eh_args[0];
102       if (c->dwarf.eh_valid_mask & 0x2) sc->regs[1] = c->dwarf.eh_args[1];
103       if (c->dwarf.eh_valid_mask & 0x4) sc->regs[2] = c->dwarf.eh_args[2];
104       if (c->dwarf.eh_valid_mask & 0x8) sc->regs[3] = c->dwarf.eh_args[3];
105
106       sc->regs[4] = uc->uc_mcontext.regs[4];
107       sc->regs[5] = uc->uc_mcontext.regs[5];
108       sc->regs[6] = uc->uc_mcontext.regs[6];
109       sc->regs[7] = uc->uc_mcontext.regs[7];
110       sc->regs[8] = uc->uc_mcontext.regs[8];
111       sc->regs[9] = uc->uc_mcontext.regs[9];
112       sc->regs[10] = uc->uc_mcontext.regs[10];
113       sc->regs[11] = uc->uc_mcontext.regs[11];
114       sc->regs[12] = uc->uc_mcontext.regs[12];
115       sc->regs[13] = uc->uc_mcontext.regs[13];
116       sc->regs[14] = uc->uc_mcontext.regs[14];
117       sc->regs[15] = uc->uc_mcontext.regs[15];
118       sc->regs[16] = uc->uc_mcontext.regs[16];
119       sc->regs[17] = uc->uc_mcontext.regs[17];
120       sc->regs[18] = uc->uc_mcontext.regs[18];
121       sc->regs[19] = uc->uc_mcontext.regs[19];
122       sc->regs[20] = uc->uc_mcontext.regs[20];
123       sc->regs[21] = uc->uc_mcontext.regs[21];
124       sc->regs[22] = uc->uc_mcontext.regs[22];
125       sc->regs[23] = uc->uc_mcontext.regs[23];
126       sc->regs[24] = uc->uc_mcontext.regs[24];
127       sc->regs[25] = uc->uc_mcontext.regs[25];
128       sc->regs[26] = uc->uc_mcontext.regs[26];
129       sc->regs[27] = uc->uc_mcontext.regs[27];
130       sc->regs[28] = uc->uc_mcontext.regs[28];
131       sc->regs[29] = uc->uc_mcontext.regs[29];
132       sc->regs[30] = uc->uc_mcontext.regs[30];
133       sc->sp = uc->uc_mcontext.sp;
134       sc->pc = uc->uc_mcontext.pc;
135       sc->pstate = uc->uc_mcontext.pstate;
136
137       asm volatile (
138         "mov sp, %0\n"
139         "ret %1\n"
140         : : "r" (c->sigcontext_sp), "r" (c->sigcontext_pc)
141       );
142    }
143   unreachable();
144 #else
145   printf ("%s: implement me\n", __FUNCTION__);
146 #endif
147   return -UNW_EINVAL;
148 }
149
150 #endif /* !UNW_REMOTE_ONLY */
151
152 static inline void
153 establish_machine_state (struct cursor *c)
154 {
155   unw_addr_space_t as = c->dwarf.as;
156   void *arg = c->dwarf.as_arg;
157   unw_fpreg_t fpval;
158   unw_word_t val;
159   int reg;
160
161   Debug (8, "copying out cursor state\n");
162
163   for (reg = 0; reg <= UNW_AARCH64_V31; ++reg)
164     {
165       Debug (16, "copying %s %d\n", unw_regname (reg), reg);
166       if (unw_is_fpreg (reg))
167         {
168           if (tdep_access_fpreg (c, reg, &fpval, 0) >= 0)
169             as->acc.access_fpreg (as, reg, &fpval, 1, arg);
170         }
171       else
172         {
173           if (tdep_access_reg (c, reg, &val, 0) >= 0)
174             as->acc.access_reg (as, reg, &val, 1, arg);
175         }
176     }
177 }
178
179 int
180 unw_resume (unw_cursor_t *cursor)
181 {
182   struct cursor *c = (struct cursor *) cursor;
183
184   Debug (1, "(cursor=%p)\n", c);
185
186   if (!c->dwarf.ip)
187     {
188       /* This can happen easily when the frame-chain gets truncated
189          due to bad or missing unwind-info.  */
190       Debug (1, "refusing to resume execution at address 0\n");
191       return -UNW_EINVAL;
192     }
193
194   establish_machine_state (c);
195
196   return (*c->dwarf.as->acc.resume) (c->dwarf.as, (unw_cursor_t *) c,
197                                      c->dwarf.as_arg);
198 }