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