• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2001-2004 Hewlett-Packard Co
3         Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4    Copyright (C) 2013 Linaro Limited
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 #ifndef LIBUNWIND_H
28 #define LIBUNWIND_H
29 
30 #if defined(__cplusplus) || defined(c_plusplus)
31 extern "C" {
32 #endif
33 
34 #include <inttypes.h>
35 #include <stddef.h>
36 #include <ucontext.h>
37 #include <stdalign.h>
38 
39 #ifndef UNW_EMPTY_STRUCT
40 #  define UNW_EMPTY_STRUCT uint8_t unused;
41 #endif
42 
43 #define UNW_TARGET      aarch64
44 #define UNW_TARGET_AARCH64      1
45 
46 #define _U_TDEP_QP_TRUE 0       /* see libunwind-dynamic.h  */
47 
48 /* This needs to be big enough to accommodate "struct cursor", while
49    leaving some slack for future expansion.  Changing this value will
50    require recompiling all users of this library.  Stack allocation is
51    relatively cheap and unwind-state copying is relatively rare, so we
52    want to err on making it rather too big than too small.
53 
54    Calculation is regs used (64 + 34) * 2 + 40 (bytes of rest of
55    cursor) + padding
56 */
57 
58 #define UNW_TDEP_CURSOR_LEN     1024
59 
60 typedef uint64_t unw_word_t;
61 typedef int64_t unw_sword_t;
62 
63 typedef long double unw_tdep_fpreg_t;
64 
65 typedef struct
66   {
67     /* no aarch64-specific auxiliary proc-info */
68     UNW_EMPTY_STRUCT
69   }
70 unw_tdep_proc_info_t;
71 
72 typedef enum
73   {
74     /* 64-bit general registers.  */
75     UNW_AARCH64_X0,
76     UNW_AARCH64_X1,
77     UNW_AARCH64_X2,
78     UNW_AARCH64_X3,
79     UNW_AARCH64_X4,
80     UNW_AARCH64_X5,
81     UNW_AARCH64_X6,
82     UNW_AARCH64_X7,
83     UNW_AARCH64_X8,
84 
85     /* Temporary registers.  */
86     UNW_AARCH64_X9,
87     UNW_AARCH64_X10,
88     UNW_AARCH64_X11,
89     UNW_AARCH64_X12,
90     UNW_AARCH64_X13,
91     UNW_AARCH64_X14,
92     UNW_AARCH64_X15,
93 
94     /* Intra-procedure-call temporary registers.  */
95     UNW_AARCH64_X16,
96     UNW_AARCH64_X17,
97 
98     /* Callee-saved registers.  */
99     UNW_AARCH64_X18,
100     UNW_AARCH64_X19,
101     UNW_AARCH64_X20,
102     UNW_AARCH64_X21,
103     UNW_AARCH64_X22,
104     UNW_AARCH64_X23,
105     UNW_AARCH64_X24,
106     UNW_AARCH64_X25,
107     UNW_AARCH64_X26,
108     UNW_AARCH64_X27,
109     UNW_AARCH64_X28,
110 
111     /* 64-bit frame pointer.  */
112     UNW_AARCH64_X29,
113 
114     /* 64-bit link register.  */
115     UNW_AARCH64_X30,
116 
117     /* 64-bit stack pointer.  */
118     UNW_AARCH64_SP =  31,
119     UNW_AARCH64_PC,
120     UNW_AARCH64_PSTATE,
121 
122     /* 128-bit FP/Advanced SIMD registers.  */
123     UNW_AARCH64_V0 = 64,
124     UNW_AARCH64_V1,
125     UNW_AARCH64_V2,
126     UNW_AARCH64_V3,
127     UNW_AARCH64_V4,
128     UNW_AARCH64_V5,
129     UNW_AARCH64_V6,
130     UNW_AARCH64_V7,
131     UNW_AARCH64_V8,
132     UNW_AARCH64_V9,
133     UNW_AARCH64_V10,
134     UNW_AARCH64_V11,
135     UNW_AARCH64_V12,
136     UNW_AARCH64_V13,
137     UNW_AARCH64_V14,
138     UNW_AARCH64_V15,
139     UNW_AARCH64_V16,
140     UNW_AARCH64_V17,
141     UNW_AARCH64_V18,
142     UNW_AARCH64_V19,
143     UNW_AARCH64_V20,
144     UNW_AARCH64_V21,
145     UNW_AARCH64_V22,
146     UNW_AARCH64_V23,
147     UNW_AARCH64_V24,
148     UNW_AARCH64_V25,
149     UNW_AARCH64_V26,
150     UNW_AARCH64_V27,
151     UNW_AARCH64_V28,
152     UNW_AARCH64_V29,
153     UNW_AARCH64_V30,
154     UNW_AARCH64_V31,
155 
156     UNW_AARCH64_FPSR,
157     UNW_AARCH64_FPCR,
158 
159     /* For AArch64, the CFA is the value of SP (x31) at the call site of the
160        previous frame.  */
161     UNW_AARCH64_CFA = UNW_AARCH64_SP,
162 
163     UNW_TDEP_LAST_REG = UNW_AARCH64_FPCR,
164 
165     UNW_TDEP_IP = UNW_AARCH64_X30,
166     UNW_TDEP_SP = UNW_AARCH64_SP,
167     UNW_TDEP_EH = UNW_AARCH64_X0
168 
169   }
170 aarch64_regnum_t;
171 
172 /* Use R0 through R3 to pass exception handling information.  */
173 #define UNW_TDEP_NUM_EH_REGS    4
174 
175 typedef struct unw_tdep_save_loc
176   {
177     /* Additional target-dependent info on a save location.  */
178     UNW_EMPTY_STRUCT
179   }
180 unw_tdep_save_loc_t;
181 
182 #ifdef __linux__
183 /* On AArch64, we can directly use ucontext_t as the unwind context,
184  * however, the __reserved struct is quite large: tune it down to only
185  * the necessary used fields.  */
186 
187 struct unw_sigcontext
188   {
189 	uint64_t fault_address;
190 	uint64_t regs[31];
191 	uint64_t sp;
192 	uint64_t pc;
193 	uint64_t pstate;
194 	alignas(16) uint8_t __reserved[(66 * 8)];
195 };
196 
197 typedef struct
198   {
199 	unsigned long uc_flags;
200 	struct ucontext *uc_link;
201 	stack_t uc_stack;
202 #ifndef __ANDROID__
203 	sigset_t uc_sigmask;
204 #else
205 	union {
206 		sigset_t uc_sigmask;
207 		sigset64_t uc_sigmask64;
208 	};
209 	char __padding[128 - sizeof(sigset_t)];
210 #endif
211 	struct unw_sigcontext uc_mcontext;
212   } unw_tdep_context_t;
213 
214 typedef struct
215   {
216 	uint32_t _ctx_magic;
217 	uint32_t _ctx_size;
218 	uint32_t fpsr;
219 	uint32_t fpcr;
220 	uint64_t vregs[64];
221   } unw_fpsimd_context_t;
222 #else
223 /* On AArch64, we can directly use ucontext_t as the unwind context.  */
224 typedef ucontext_t unw_tdep_context_t;
225 #endif
226 
227 
228 #include "libunwind-common.h"
229 #include "libunwind-dynamic.h"
230 
231 #define UNW_BASE register uint64_t unw_base __asm__ ("x0") = (uint64_t) unw_ctx->uc_mcontext.regs;
232 
233 #define unw_tdep_getcontext(uc) ({					\
234   unw_tdep_context_t *unw_ctx = (uc);					\
235   UNW_BASE \
236   __asm__ __volatile__ (					        \
237      "stp x0, x1, [%[base], #0]\n" \
238      "stp x2, x3, [%[base], #16]\n" \
239      "stp x4, x5, [%[base], #32]\n" \
240      "stp x6, x7, [%[base], #48]\n" \
241      "stp x8, x9, [%[base], #64]\n" \
242      "stp x10, x11, [%[base], #80]\n" \
243      "stp x12, x13, [%[base], #96]\n" \
244      "stp x14, x15, [%[base], #112]\n" \
245      "stp x16, x17, [%[base], #128]\n" \
246      "stp x18, x19, [%[base], #144]\n" \
247      "stp x20, x21, [%[base], #160]\n" \
248      "stp x22, x23, [%[base], #176]\n" \
249      "stp x24, x25, [%[base], #192]\n" \
250      "stp x26, x27, [%[base], #208]\n" \
251      "stp x28, x29, [%[base], #224]\n" \
252      "mov x1, sp\n" \
253      "stp x30, x1, [%[base], #240]\n" \
254      "adr x1, ret%=\n" \
255      "str x1, [%[base], #256]\n" \
256      "mov %[base], #0\n" \
257      "ret%=:\n" \
258      : [base] "+r" (unw_base) : : "x1", "memory"); \
259   (int)unw_base; })
260 #define unw_tdep_is_fpreg		UNW_ARCH_OBJ(is_fpreg)
261 
262 extern int unw_tdep_is_fpreg (int);
263 
264 #if defined(__cplusplus) || defined(c_plusplus)
265 }
266 #endif
267 
268 #endif /* LIBUNWIND_H */
269