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
30PROTECTED int
31unw_handle_signal_frame (unw_cursor_t *cursor)
32{
33  struct cursor *c = (struct cursor *) cursor;
34  int ret;
35  unw_word_t sc_addr, sp, sp_addr = c->dwarf.cfa;
36  struct dwarf_loc sp_loc = DWARF_LOC (sp_addr, 0);
37
38  if ((ret = dwarf_get (&c->dwarf, sp_loc, &sp)) < 0)
39    return -UNW_EUNSPEC;
40
41  ret = unw_is_signal_frame (cursor);
42  Debug(1, "unw_is_signal_frame()=%d\n", ret);
43
44  /* Save the SP and PC to be able to return execution at this point
45     later in time (unw_resume).  */
46  c->sigcontext_sp = c->dwarf.cfa;
47  c->sigcontext_pc = c->dwarf.ip;
48
49  if (ret)
50    {
51      c->sigcontext_format = AARCH64_SCF_LINUX_RT_SIGFRAME;
52      sc_addr = sp_addr + sizeof (siginfo_t) + LINUX_UC_MCONTEXT_OFF;
53    }
54  else
55    return -UNW_EUNSPEC;
56
57  c->sigcontext_addr = sc_addr;
58
59  /* Update the dwarf cursor.
60     Set the location of the registers to the corresponding addresses of the
61     uc_mcontext / sigcontext structure contents.  */
62  c->dwarf.loc[UNW_AARCH64_X0]  = DWARF_LOC (sc_addr + LINUX_SC_X0_OFF, 0);
63  c->dwarf.loc[UNW_AARCH64_X1]  = DWARF_LOC (sc_addr + LINUX_SC_X1_OFF, 0);
64  c->dwarf.loc[UNW_AARCH64_X2]  = DWARF_LOC (sc_addr + LINUX_SC_X2_OFF, 0);
65  c->dwarf.loc[UNW_AARCH64_X3]  = DWARF_LOC (sc_addr + LINUX_SC_X3_OFF, 0);
66  c->dwarf.loc[UNW_AARCH64_X4]  = DWARF_LOC (sc_addr + LINUX_SC_X4_OFF, 0);
67  c->dwarf.loc[UNW_AARCH64_X5]  = DWARF_LOC (sc_addr + LINUX_SC_X5_OFF, 0);
68  c->dwarf.loc[UNW_AARCH64_X6]  = DWARF_LOC (sc_addr + LINUX_SC_X6_OFF, 0);
69  c->dwarf.loc[UNW_AARCH64_X7]  = DWARF_LOC (sc_addr + LINUX_SC_X7_OFF, 0);
70  c->dwarf.loc[UNW_AARCH64_X8]  = DWARF_LOC (sc_addr + LINUX_SC_X8_OFF, 0);
71  c->dwarf.loc[UNW_AARCH64_X9]  = DWARF_LOC (sc_addr + LINUX_SC_X9_OFF, 0);
72  c->dwarf.loc[UNW_AARCH64_X10] = DWARF_LOC (sc_addr + LINUX_SC_X10_OFF, 0);
73  c->dwarf.loc[UNW_AARCH64_X11] = DWARF_LOC (sc_addr + LINUX_SC_X11_OFF, 0);
74  c->dwarf.loc[UNW_AARCH64_X12] = DWARF_LOC (sc_addr + LINUX_SC_X12_OFF, 0);
75  c->dwarf.loc[UNW_AARCH64_X13] = DWARF_LOC (sc_addr + LINUX_SC_X13_OFF, 0);
76  c->dwarf.loc[UNW_AARCH64_X14] = DWARF_LOC (sc_addr + LINUX_SC_X14_OFF, 0);
77  c->dwarf.loc[UNW_AARCH64_X15] = DWARF_LOC (sc_addr + LINUX_SC_X15_OFF, 0);
78  c->dwarf.loc[UNW_AARCH64_X16] = DWARF_LOC (sc_addr + LINUX_SC_X16_OFF, 0);
79  c->dwarf.loc[UNW_AARCH64_X17] = DWARF_LOC (sc_addr + LINUX_SC_X17_OFF, 0);
80  c->dwarf.loc[UNW_AARCH64_X18] = DWARF_LOC (sc_addr + LINUX_SC_X18_OFF, 0);
81  c->dwarf.loc[UNW_AARCH64_X19] = DWARF_LOC (sc_addr + LINUX_SC_X19_OFF, 0);
82  c->dwarf.loc[UNW_AARCH64_X20] = DWARF_LOC (sc_addr + LINUX_SC_X20_OFF, 0);
83  c->dwarf.loc[UNW_AARCH64_X21] = DWARF_LOC (sc_addr + LINUX_SC_X21_OFF, 0);
84  c->dwarf.loc[UNW_AARCH64_X22] = DWARF_LOC (sc_addr + LINUX_SC_X22_OFF, 0);
85  c->dwarf.loc[UNW_AARCH64_X23] = DWARF_LOC (sc_addr + LINUX_SC_X23_OFF, 0);
86  c->dwarf.loc[UNW_AARCH64_X24] = DWARF_LOC (sc_addr + LINUX_SC_X24_OFF, 0);
87  c->dwarf.loc[UNW_AARCH64_X25] = DWARF_LOC (sc_addr + LINUX_SC_X25_OFF, 0);
88  c->dwarf.loc[UNW_AARCH64_X26] = DWARF_LOC (sc_addr + LINUX_SC_X26_OFF, 0);
89  c->dwarf.loc[UNW_AARCH64_X27] = DWARF_LOC (sc_addr + LINUX_SC_X27_OFF, 0);
90  c->dwarf.loc[UNW_AARCH64_X28] = DWARF_LOC (sc_addr + LINUX_SC_X28_OFF, 0);
91  c->dwarf.loc[UNW_AARCH64_X29] = DWARF_LOC (sc_addr + LINUX_SC_X29_OFF, 0);
92  c->dwarf.loc[UNW_AARCH64_X30] = DWARF_LOC (sc_addr + LINUX_SC_X30_OFF, 0);
93  c->dwarf.loc[UNW_AARCH64_SP]  = DWARF_LOC (sc_addr + LINUX_SC_SP_OFF, 0);
94  c->dwarf.loc[UNW_AARCH64_PC]  = DWARF_LOC (sc_addr + LINUX_SC_PC_OFF, 0);
95  c->dwarf.loc[UNW_AARCH64_PSTATE]  = DWARF_LOC (sc_addr + LINUX_SC_PSTATE_OFF, 0);
96
97  /* Set SP/CFA and PC/IP.  */
98  dwarf_get (&c->dwarf, c->dwarf.loc[UNW_AARCH64_SP], &c->dwarf.cfa);
99  dwarf_get (&c->dwarf, c->dwarf.loc[UNW_AARCH64_PC], &c->dwarf.ip);
100
101  c->dwarf.pi_valid = 0;
102
103  return 1;
104}
105
106PROTECTED int
107unw_step (unw_cursor_t *cursor)
108{
109  struct cursor *c = (struct cursor *) cursor;
110  int ret = -UNW_ENOINFO;
111
112  Debug (1, "(cursor=%p, ip=0x%016lx, cfa=0x%016lx))\n",
113	 c, c->dwarf.ip, c->dwarf.cfa);
114
115  unw_word_t old_ip = c->dwarf.ip;
116  unw_word_t old_cfa = c->dwarf.cfa;
117
118  /* Check if this is a signal frame. */
119  if (unw_is_signal_frame (cursor))
120    /* ANDROID support update. */
121    ret = unw_handle_signal_frame (cursor);
122    /* End ANDROID update. */
123
124  /* ANDROID support update. */
125  if (ret < 0)
126    {
127      ret = dwarf_step (&c->dwarf);
128      Debug(1, "dwarf_step()=%d\n", ret);
129    }
130
131  if (ret < 0 && c->dwarf.frame == 0)
132    {
133      /* If this is the first frame, the code may be executing garbage
134       * in the middle of nowhere. In this case, try using the lr as
135       * the pc.
136       */
137      unw_word_t lr;
138      if (dwarf_get(&c->dwarf, c->dwarf.loc[UNW_AARCH64_X30], &lr) >= 0)
139        {
140          if (lr != c->dwarf.ip)
141            {
142              ret = 1;
143              c->dwarf.ip = lr;
144            }
145        }
146    }
147  /* End ANDROID update. */
148
149  if (ret >= 0)
150    {
151      if (c->dwarf.ip >= 4)
152        c->dwarf.ip -= 4;
153      /* If the decode yields the exact same ip/cfa as before, then indicate
154         the unwind is complete. */
155      if (c->dwarf.ip == old_ip && c->dwarf.cfa == old_cfa)
156        {
157          Dprintf ("%s: ip and cfa unchanged; stopping here (ip=0x%lx)\n",
158                   __FUNCTION__, (long) c->dwarf.ip);
159          return -UNW_EBADFRAME;
160        }
161      c->dwarf.frame++;
162    }
163
164  if (unlikely (ret == -UNW_ESTOPUNWIND))
165    return ret;
166
167  if (unlikely (ret <= 0))
168    return 0;
169
170  return (c->dwarf.ip == 0) ? 0 : 1;
171}
172