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