xref: /xnu-12377.61.12/osfmk/mach/arm/_structs.h (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*
2  * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * @OSF_COPYRIGHT@
30  */
31 #ifndef _MACH_ARM__STRUCTS_H_
32 #define _MACH_ARM__STRUCTS_H_
33 
34 #if defined (__arm__) || defined (__arm64__)
35 
36 #include <sys/cdefs.h> /* __DARWIN_UNIX03 */
37 #include <machine/types.h> /* __uint32_t */
38 
39 #if __DARWIN_UNIX03
40 #define _STRUCT_ARM_EXCEPTION_STATE struct __darwin_arm_exception_state
41 _STRUCT_ARM_EXCEPTION_STATE
42 {
43 	__uint32_t __exception; /* number of arm exception taken */
44 	__uint32_t __fsr;       /* Fault status */
45 	__uint32_t __far;       /* Virtual Fault Address */
46 };
47 #else /* !__DARWIN_UNIX03 */
48 #define _STRUCT_ARM_EXCEPTION_STATE struct arm_exception_state
49 _STRUCT_ARM_EXCEPTION_STATE
50 {
51 	__uint32_t exception;   /* number of arm exception taken */
52 	__uint32_t fsr;         /* Fault status */
53 	__uint32_t far;         /* Virtual Fault Address */
54 };
55 #endif /* __DARWIN_UNIX03 */
56 
57 #if __DARWIN_UNIX03
58 #define _STRUCT_ARM_EXCEPTION_STATE64 struct __darwin_arm_exception_state64
59 _STRUCT_ARM_EXCEPTION_STATE64
60 {
61 	__uint64_t __far;       /* Virtual Fault Address */
62 	__uint32_t __esr;       /* Exception syndrome */
63 	__uint32_t __exception; /* number of arm exception taken */
64 };
65 #define _STRUCT_ARM_EXCEPTION_STATE64_V2 struct __darwin_arm_exception_state64_v2
66 _STRUCT_ARM_EXCEPTION_STATE64_V2
67 {
68 	__uint64_t __far;       /* Virtual Fault Address */
69 	__uint64_t __esr;       /* Exception syndrome */
70 };
71 #else /* !__DARWIN_UNIX03 */
72 #define _STRUCT_ARM_EXCEPTION_STATE64 struct arm_exception_state64
73 _STRUCT_ARM_EXCEPTION_STATE64
74 {
75 	__uint64_t far;         /* Virtual Fault Address */
76 	__uint32_t esr;         /* Exception syndrome */
77 	__uint32_t exception;   /* number of arm exception taken */
78 };
79 #define _STRUCT_ARM_EXCEPTION_STATE64_V2 struct arm_exception_state64_v2
80 _STRUCT_ARM_EXCEPTION_STATE64_V2
81 {
82 	__uint64_t far;         /* Virtual Fault Address */
83 	__uint64_t esr;         /* Exception syndrome */
84 };
85 #endif /* __DARWIN_UNIX03 */
86 
87 #if __DARWIN_UNIX03
88 #define _STRUCT_ARM_THREAD_STATE struct __darwin_arm_thread_state
89 _STRUCT_ARM_THREAD_STATE
90 {
91 	__uint32_t __r[13]; /* General purpose register r0-r12 */
92 	__uint32_t __sp;    /* Stack pointer r13 */
93 	__uint32_t __lr;    /* Link register r14 */
94 	__uint32_t __pc;    /* Program counter r15 */
95 	__uint32_t __cpsr;  /* Current program status register */
96 };
97 #else /* !__DARWIN_UNIX03 */
98 #define _STRUCT_ARM_THREAD_STATE struct arm_thread_state
99 _STRUCT_ARM_THREAD_STATE
100 {
101 	__uint32_t r[13];   /* General purpose register r0-r12 */
102 	__uint32_t sp;      /* Stack pointer r13 */
103 	__uint32_t lr;      /* Link register r14 */
104 	__uint32_t pc;      /* Program counter r15 */
105 	__uint32_t cpsr;    /* Current program status register */
106 };
107 #endif /* __DARWIN_UNIX03 */
108 
109 #if defined(KERNEL)
110 
111 #define __DARWIN_OPAQUE_ARM_THREAD_STATE64 0
112 #define __DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH 0x1
113 #define __DARWIN_ARM_THREAD_STATE64_FLAGS_IB_SIGNED_LR 0x2
114 #define __DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_PC 0x4
115 #define __DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_LR 0x8
116 
117 #define __DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK 0xff000000
118 #define __DARWIN_ARM_THREAD_STATE64_SIGRETURN_PC_MASK 0x000f0000
119 #define __DARWIN_ARM_THREAD_STATE64_SIGRETURN_LR_MASK 0x00f00000
120 
121 #define __DARWIN_ARM_THREAD_STATE64_SET_SIGRETURN_TOKEN(ts, token, mask) \
122     ((ts)->flags |= (((uint32_t)(token)) & (mask)))
123 
124 #define __DARWIN_ARM_THREAD_STATE64_CHECK_SIGRETURN_TOKEN(ts, token, mask) \
125     (((ts)->flags & (mask)) == \
126     (((uint32_t)(token)) & (mask)))
127 
128 #define _STRUCT_ARM_THREAD_STATE64      struct arm_thread_state64
129 _STRUCT_ARM_THREAD_STATE64
130 {
131 	__uint64_t    x[29];    /* General purpose registers x0-x28 */
132 	__uint64_t    fp;               /* Frame pointer x29 */
133 	__uint64_t    lr;               /* Link register x30 */
134 	__uint64_t    sp;               /* Stack pointer x31 */
135 	__uint64_t    pc;               /* Program counter */
136 	__uint32_t    cpsr;             /* Current program status register */
137 	__uint32_t    flags;    /* Flags describing structure format */
138 };
139 
140 #else /* defined(KERNEL) */
141 
142 /*
143  * By default, the pointer fields in the arm_thread_state64_t structure are
144  * opaque on the arm64e architecture and require the use of accessor macros.
145  * This mode can also be enabled on the arm64 architecture by building with
146  * -D__DARWIN_OPAQUE_ARM_THREAD_STATE64=1.
147  */
148 #if defined(__arm64__) && defined(__LP64__)
149 
150 #if __has_feature(ptrauth_calls)
151 #define __DARWIN_OPAQUE_ARM_THREAD_STATE64 1
152 #define __DARWIN_PTRAUTH_ARM_THREAD_STATE64 1
153 #endif /* __has_feature(ptrauth_calls) */
154 
155 #ifndef __DARWIN_OPAQUE_ARM_THREAD_STATE64
156 #define __DARWIN_OPAQUE_ARM_THREAD_STATE64 0
157 #endif
158 
159 #else /* defined(__arm64__) && defined(__LP64__) */
160 
161 #undef __DARWIN_OPAQUE_ARM_THREAD_STATE64
162 #define __DARWIN_OPAQUE_ARM_THREAD_STATE64 0
163 
164 #endif /* defined(__arm64__) && defined(__LP64__) */
165 
166 #if __DARWIN_UNIX03
167 #define _STRUCT_ARM_THREAD_STATE64 struct __darwin_arm_thread_state64
168 #if __DARWIN_OPAQUE_ARM_THREAD_STATE64
169 _STRUCT_ARM_THREAD_STATE64
170 {
171 	__uint64_t __x[29];     /* General purpose registers x0-x28 */
172 	void*      __opaque_fp; /* Frame pointer x29 */
173 	void*      __opaque_lr; /* Link register x30 */
174 	void*      __opaque_sp; /* Stack pointer x31 */
175 	void*      __opaque_pc; /* Program counter */
176 	__uint32_t __cpsr;      /* Current program status register */
177 	__uint32_t __opaque_flags; /* Flags describing structure format */
178 };
179 #else /* __DARWIN_OPAQUE_ARM_THREAD_STATE64 */
180 _STRUCT_ARM_THREAD_STATE64
181 {
182 	__uint64_t __x[29]; /* General purpose registers x0-x28 */
183 	__uint64_t __fp;    /* Frame pointer x29 */
184 	__uint64_t __lr;    /* Link register x30 */
185 	__uint64_t __sp;    /* Stack pointer x31 */
186 	__uint64_t __pc;    /* Program counter */
187 	__uint32_t __cpsr;  /* Current program status register */
188 	__uint32_t __pad;   /* Same size for 32-bit or 64-bit clients */
189 };
190 #endif /* __DARWIN_OPAQUE_ARM_THREAD_STATE64 */
191 #else /* !__DARWIN_UNIX03 */
192 #define _STRUCT_ARM_THREAD_STATE64 struct arm_thread_state64
193 #if __DARWIN_OPAQUE_ARM_THREAD_STATE64
194 _STRUCT_ARM_THREAD_STATE64
195 {
196 	__uint64_t x[29];       /* General purpose registers x0-x28 */
197 	void*      __opaque_fp; /* Frame pointer x29 */
198 	void*      __opaque_lr; /* Link register x30 */
199 	void*      __opaque_sp; /* Stack pointer x31 */
200 	void*      __opaque_pc; /* Program counter */
201 	__uint32_t cpsr;        /* Current program status register */
202 	__uint32_t __opaque_flags; /* Flags describing structure format */
203 };
204 #else /* __DARWIN_OPAQUE_ARM_THREAD_STATE64 */
205 _STRUCT_ARM_THREAD_STATE64
206 {
207 	__uint64_t x[29]; /* General purpose registers x0-x28 */
208 	__uint64_t fp;    /* Frame pointer x29 */
209 	__uint64_t lr;    /* Link register x30 */
210 	__uint64_t sp;    /* Stack pointer x31 */
211 	__uint64_t pc;    /* Program counter */
212 	__uint32_t cpsr;  /* Current program status register */
213 	__uint32_t __pad; /* Same size for 32-bit or 64-bit clients */
214 };
215 #endif /* __DARWIN_OPAQUE_ARM_THREAD_STATE64 */
216 #endif /* __DARWIN_UNIX03 */
217 
218 #if __DARWIN_C_LEVEL >= __DARWIN_C_FULL && defined(__arm64__)
219 
220 /* Accessor macros for arm_thread_state64_t pointer fields */
221 
222 #if __has_feature(ptrauth_calls) && defined(__LP64__)
223 #include <ptrauth.h>
224 
225 #if !__DARWIN_OPAQUE_ARM_THREAD_STATE64 || !__DARWIN_PTRAUTH_ARM_THREAD_STATE64
226 #error "Invalid configuration"
227 #endif
228 
229 #define __DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH 0x1
230 #define __DARWIN_ARM_THREAD_STATE64_FLAGS_IB_SIGNED_LR 0x2
231 #define __DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_PC 0x4
232 #define __DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_LR 0x8
233 
234 #define __DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK 0xff000000
235 
236 /* Return pc field of arm_thread_state64_t as a data pointer value */
237 #define __darwin_arm_thread_state64_get_pc(ts) \
238 	__extension__ ({ const _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts); \
239 	(uintptr_t)(__tsp->__opaque_pc && !(__tsp->__opaque_flags &       \
240 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH) ?                   \
241 	ptrauth_auth_data(__tsp->__opaque_pc,                             \
242 	ptrauth_key_process_independent_code,                             \
243 	((__tsp->__opaque_flags &                                         \
244 	__DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_PC) == 0 &&       \
245 	(__tsp->__opaque_flags &                                          \
246 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK)) ?             \
247 	ptrauth_blend_discriminator((void *)(unsigned long)               \
248 	(__tsp->__opaque_flags &                                          \
249 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK),               \
250 	ptrauth_string_discriminator("pc")) :                             \
251 	ptrauth_string_discriminator("pc")) : __tsp->__opaque_pc); })
252 /* Return pc field of arm_thread_state64_t as a function pointer. May return
253  * NULL if a valid function pointer cannot be constructed, the caller should
254  * fall back to the __darwin_arm_thread_state64_get_pc() macro in that case. */
255 #define __darwin_arm_thread_state64_get_pc_fptr(ts) \
256 	__extension__ ({ const _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts); \
257 	(__tsp->__opaque_pc && !(__tsp->__opaque_flags &                  \
258 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH) ?                   \
259 	ptrauth_auth_function(__tsp->__opaque_pc,                         \
260 	ptrauth_key_process_independent_code,                             \
261 	((__tsp->__opaque_flags &                                         \
262 	__DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_PC) == 0 &&       \
263 	(__tsp->__opaque_flags &                                          \
264 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK)) ?             \
265 	ptrauth_blend_discriminator((void *)(unsigned long)               \
266 	(__tsp->__opaque_flags &                                          \
267 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK),               \
268 	ptrauth_string_discriminator("pc")) :                             \
269 	ptrauth_string_discriminator("pc")) : NULL); })
270 /* Set pc field of arm_thread_state64_t to a function pointer */
271 #define __darwin_arm_thread_state64_set_pc_fptr(ts, fptr) \
272 	__extension__ ({ _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts);   \
273 	__typeof__(fptr) __f = (fptr); __tsp->__opaque_pc =           \
274 	(__f ? (!(__tsp->__opaque_flags &                             \
275 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH) ?               \
276 	ptrauth_auth_and_resign(__f, ptrauth_key_function_pointer, 0, \
277 	ptrauth_key_process_independent_code,                         \
278 	(__tsp->__opaque_flags &                                      \
279 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK) ?          \
280 	ptrauth_blend_discriminator((void *)(unsigned long)           \
281 	(__tsp->__opaque_flags &                                      \
282 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK),           \
283 	ptrauth_string_discriminator("pc")) :                         \
284 	ptrauth_string_discriminator("pc")) : ptrauth_auth_data(__f,  \
285 	ptrauth_key_function_pointer, 0)) : __f);                     \
286 	__tsp->__opaque_flags &=                                      \
287 	~__DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_PC; })
288 /* Set pc field of arm_thread_state64_t to an already signed function pointer */
289 #define __darwin_arm_thread_state64_set_pc_presigned_fptr(ts, presigned_fptr)           \
290 	__extension__ ({ _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts);                                     \
291 	__typeof__(presigned_fptr) __f = (presigned_fptr); __tsp->__opaque_pc = __f;    \
292 	__tsp->__opaque_flags &=                                                                        \
293 	~__DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_PC; })
294 /* Return lr field of arm_thread_state64_t as a data pointer value */
295 #define __darwin_arm_thread_state64_get_lr(ts) \
296 	__extension__ ({ const _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts); \
297 	(uintptr_t)(__tsp->__opaque_lr && !(__tsp->__opaque_flags & (     \
298 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH |                    \
299 	__DARWIN_ARM_THREAD_STATE64_FLAGS_IB_SIGNED_LR)) ?                \
300 	ptrauth_auth_data(__tsp->__opaque_lr,                             \
301 	ptrauth_key_process_independent_code,                             \
302 	((__tsp->__opaque_flags &                                         \
303 	__DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_LR) == 0 &&       \
304 	(__tsp->__opaque_flags &                                          \
305 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK)) ?             \
306 	ptrauth_blend_discriminator((void *)(unsigned long)               \
307 	(__tsp->__opaque_flags &                                          \
308 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK),               \
309 	ptrauth_string_discriminator("lr")) :                             \
310 	ptrauth_string_discriminator("lr")) : __tsp->__opaque_lr); })
311 /* Return lr field of arm_thread_state64_t as a function pointer. May return
312  * NULL if a valid function pointer cannot be constructed, the caller should
313  * fall back to the __darwin_arm_thread_state64_get_lr() macro in that case. */
314 #define __darwin_arm_thread_state64_get_lr_fptr(ts) \
315 	__extension__ ({ const _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts); \
316 	(__tsp->__opaque_lr && !(__tsp->__opaque_flags & (                \
317 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH |                    \
318 	__DARWIN_ARM_THREAD_STATE64_FLAGS_IB_SIGNED_LR)) ?                \
319 	ptrauth_auth_function(__tsp->__opaque_lr,                         \
320 	ptrauth_key_process_independent_code,                             \
321 	((__tsp->__opaque_flags &                                         \
322 	__DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_LR) == 0 &&       \
323 	(__tsp->__opaque_flags &                                          \
324 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK)) ?             \
325 	ptrauth_blend_discriminator((void *)(unsigned long)               \
326 	(__tsp->__opaque_flags &                                          \
327 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK),               \
328 	ptrauth_string_discriminator("lr")) :                             \
329 	ptrauth_string_discriminator("lr")) : NULL); })
330 /* Set lr field of arm_thread_state64_t to a function pointer */
331 #define __darwin_arm_thread_state64_set_lr_fptr(ts, fptr) \
332 	__extension__ ({ _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts);            \
333 	__typeof__(fptr) __f = (fptr); __tsp->__opaque_lr =                    \
334 	(__f ? (!(__tsp->__opaque_flags &                                      \
335 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH) ? (__tsp->__opaque_flags \
336 	&= ~__DARWIN_ARM_THREAD_STATE64_FLAGS_IB_SIGNED_LR ,                   \
337 	ptrauth_auth_and_resign(__f, ptrauth_key_function_pointer, 0,          \
338 	ptrauth_key_process_independent_code,                                  \
339 	(__tsp->__opaque_flags &                                               \
340 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK) ?                   \
341 	ptrauth_blend_discriminator((void *)(unsigned long)                    \
342 	(__tsp->__opaque_flags &                                               \
343 	__DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK),                    \
344 	ptrauth_string_discriminator("lr")) :                                  \
345 	ptrauth_string_discriminator("lr"))) : ptrauth_auth_data(__f,          \
346 	ptrauth_key_function_pointer, 0)) : __f); __tsp->__opaque_flags &=     \
347 	~__DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_LR; })
348 /* Set lr field of arm_thread_state64_t to an already signed function pointer */
349 #define __darwin_arm_thread_state64_set_lr_presigned_fptr(ts, presigned_fptr)           \
350 	__extension__ ({ _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts);                                     \
351 	__typeof__(presigned_fptr) __f = (presigned_fptr); __tsp->__opaque_lr = __f;    \
352 	__tsp->__opaque_flags &=                                                                        \
353 	~__DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_LR & ~__DARWIN_ARM_THREAD_STATE64_FLAGS_IB_SIGNED_LR; })
354 /* Return sp field of arm_thread_state64_t as a data pointer value */
355 #define __darwin_arm_thread_state64_get_sp(ts) \
356 	__extension__ ({ const _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts); \
357 	(uintptr_t)(__tsp->__opaque_sp && !(__tsp->__opaque_flags &       \
358 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH) ?                   \
359 	ptrauth_auth_data(__tsp->__opaque_sp,                             \
360 	ptrauth_key_process_independent_data,                             \
361 	ptrauth_string_discriminator("sp")) : __tsp->__opaque_sp); })
362 /* Set sp field of arm_thread_state64_t to a data pointer value */
363 #define __darwin_arm_thread_state64_set_sp(ts, ptr) \
364 	__extension__ ({ _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts); \
365 	void *__p = (void*)(uintptr_t)(ptr); __tsp->__opaque_sp =   \
366 	(__p && !(__tsp->__opaque_flags &                           \
367 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH) ?             \
368 	ptrauth_sign_unauthenticated(__p,                           \
369 	ptrauth_key_process_independent_data,                       \
370 	ptrauth_string_discriminator("sp")) : __p); })
371 /* Return fp field of arm_thread_state64_t as a data pointer value */
372 #define __darwin_arm_thread_state64_get_fp(ts) \
373 	__extension__ ({ const _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts); \
374 	(uintptr_t)(__tsp->__opaque_fp && !(__tsp->__opaque_flags &       \
375 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH) ?                   \
376 	ptrauth_auth_data(__tsp->__opaque_fp,                             \
377 	ptrauth_key_process_independent_data,                             \
378 	ptrauth_string_discriminator("fp")) : __tsp->__opaque_fp); })
379 /* Set fp field of arm_thread_state64_t to a data pointer value */
380 #define __darwin_arm_thread_state64_set_fp(ts, ptr) \
381 	__extension__ ({ _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts); \
382 	void *__p = (void*)(uintptr_t)(ptr); __tsp->__opaque_fp =   \
383 	(__p && !(__tsp->__opaque_flags &                           \
384 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH) ?             \
385 	ptrauth_sign_unauthenticated(__p,                           \
386 	ptrauth_key_process_independent_data,                       \
387 	ptrauth_string_discriminator("fp")) : __p); })
388 
389 /* Strip ptr auth bits from pc, lr, sp and fp field of arm_thread_state64_t */
390 #define __darwin_arm_thread_state64_ptrauth_strip(ts) \
391 	__extension__ ({ _STRUCT_ARM_THREAD_STATE64 *__tsp = &(ts);               \
392 	__tsp->__opaque_pc = ((__tsp->__opaque_flags &                            \
393 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH) ? __tsp->__opaque_pc :      \
394 	ptrauth_strip(__tsp->__opaque_pc, ptrauth_key_process_independent_code)); \
395 	__tsp->__opaque_lr = ((__tsp->__opaque_flags &                            \
396 	(__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH |                           \
397 	__DARWIN_ARM_THREAD_STATE64_FLAGS_IB_SIGNED_LR)) ? __tsp->__opaque_lr :   \
398 	ptrauth_strip(__tsp->__opaque_lr, ptrauth_key_process_independent_code)); \
399 	__tsp->__opaque_sp = ((__tsp->__opaque_flags &                            \
400 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH) ? __tsp->__opaque_sp :      \
401 	ptrauth_strip(__tsp->__opaque_sp, ptrauth_key_process_independent_data)); \
402 	__tsp->__opaque_fp = ((__tsp->__opaque_flags &                            \
403 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH) ? __tsp->__opaque_fp :      \
404 	ptrauth_strip(__tsp->__opaque_fp, ptrauth_key_process_independent_data)); \
405 	__tsp->__opaque_flags |=                                                  \
406 	__DARWIN_ARM_THREAD_STATE64_FLAGS_NO_PTRAUTH; __tsp->__opaque_flags &=    \
407 	~(__DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_PC |                    \
408 	__DARWIN_ARM_THREAD_STATE64_FLAGS_KERNEL_SIGNED_LR); })
409 
410 #else /* __has_feature(ptrauth_calls) && defined(__LP64__) */
411 
412 #if __DARWIN_OPAQUE_ARM_THREAD_STATE64
413 
414 #ifndef __LP64__
415 #error "Invalid configuration"
416 #endif
417 
418 /* Return pc field of arm_thread_state64_t as a data pointer value */
419 #define __darwin_arm_thread_state64_get_pc(ts) \
420 	((uintptr_t)((ts).__opaque_pc))
421 /* Return pc field of arm_thread_state64_t as a function pointer */
422 #define __darwin_arm_thread_state64_get_pc_fptr(ts) \
423 	((ts).__opaque_pc)
424 /* Set pc field of arm_thread_state64_t to a function pointer */
425 #define __darwin_arm_thread_state64_set_pc_fptr(ts, fptr) \
426 	((ts).__opaque_pc = (fptr))
427 /* Set pc field of arm_thread_state64_t to an already signed function pointer */
428 #define __darwin_arm_thread_state64_set_pc_presigned_fptr(ts, presigned_fptr)           \
429 	((ts).__opaque_pc = (presigned_fptr))
430 /* Return lr field of arm_thread_state64_t as a data pointer value */
431 #define __darwin_arm_thread_state64_get_lr(ts) \
432 	((uintptr_t)((ts).__opaque_lr))
433 /* Return lr field of arm_thread_state64_t as a function pointer */
434 #define __darwin_arm_thread_state64_get_lr_fptr(ts) \
435 	((ts).__opaque_lr)
436 /* Set lr field of arm_thread_state64_t to a function pointer */
437 #define __darwin_arm_thread_state64_set_lr_fptr(ts, fptr) \
438 	((ts).__opaque_lr = (fptr))
439 /* Set lr field of arm_thread_state64_t to a presigned function pointer */
440 #define __darwin_arm_thread_state64_set_lr_presigned_fptr(ts, presigned_fptr) \
441 	((ts).__opaque_lr = (presigned_fptr))
442 /* Return sp field of arm_thread_state64_t as a data pointer value */
443 #define __darwin_arm_thread_state64_get_sp(ts) \
444 	((uintptr_t)((ts).__opaque_sp))
445 /* Set sp field of arm_thread_state64_t to a data pointer value */
446 #define __darwin_arm_thread_state64_set_sp(ts, ptr) \
447 	((ts).__opaque_sp = (void*)(uintptr_t)(ptr))
448 /* Return fp field of arm_thread_state64_t as a data pointer value */
449 #define __darwin_arm_thread_state64_get_fp(ts) \
450 	((uintptr_t)((ts).__opaque_fp))
451 /* Set fp field of arm_thread_state64_t to a data pointer value */
452 #define __darwin_arm_thread_state64_set_fp(ts, ptr) \
453 	((ts).__opaque_fp = (void*)(uintptr_t)(ptr))
454 /* Strip ptr auth bits from pc, lr, sp and fp field of arm_thread_state64_t */
455 #define __darwin_arm_thread_state64_ptrauth_strip(ts) \
456 	(void)(ts)
457 
458 #else /* __DARWIN_OPAQUE_ARM_THREAD_STATE64 */
459 #if __DARWIN_UNIX03
460 
461 /* Return pc field of arm_thread_state64_t as a data pointer value */
462 #define __darwin_arm_thread_state64_get_pc(ts) \
463 	((ts).__pc)
464 /* Return pc field of arm_thread_state64_t as a function pointer */
465 #define __darwin_arm_thread_state64_get_pc_fptr(ts) \
466 	((void*)(uintptr_t)((ts).__pc))
467 /* Set pc field of arm_thread_state64_t to a function pointer */
468 #define __darwin_arm_thread_state64_set_pc_fptr(ts, fptr) \
469 	((ts).__pc = (uintptr_t)(fptr))
470 /* Set pc field of arm_thread_state64_t to an already signed function pointer */
471 #define __darwin_arm_thread_state64_set_pc_presigned_fptr(ts, presigned_fptr)           \
472 	((ts).__pc = (uintptr_t)(presigned_fptr))
473 /* Return lr field of arm_thread_state64_t as a data pointer value */
474 #define __darwin_arm_thread_state64_get_lr(ts) \
475 	((ts).__lr)
476 /* Return lr field of arm_thread_state64_t as a function pointer */
477 #define __darwin_arm_thread_state64_get_lr_fptr(ts) \
478 	((void*)(uintptr_t)((ts).__lr))
479 /* Set lr field of arm_thread_state64_t to a function pointer */
480 #define __darwin_arm_thread_state64_set_lr_fptr(ts, fptr) \
481 	((ts).__lr = (uintptr_t)(fptr))
482 /* Set lr field of arm_thread_state64_t to a presigned function pointer */
483 #define __darwin_arm_thread_state64_set_lr_presigned_fptr(ts, presigned_fptr) \
484 	((ts).__lr = ((uintptr_t)presigned_fptr))
485 /* Return sp field of arm_thread_state64_t as a data pointer value */
486 #define __darwin_arm_thread_state64_get_sp(ts) \
487 	((ts).__sp)
488 /* Set sp field of arm_thread_state64_t to a data pointer value */
489 #define __darwin_arm_thread_state64_set_sp(ts, ptr) \
490 	((ts).__sp = (uintptr_t)(ptr))
491 /* Return fp field of arm_thread_state64_t as a data pointer value */
492 #define __darwin_arm_thread_state64_get_fp(ts) \
493 	((ts).__fp)
494 /* Set fp field of arm_thread_state64_t to a data pointer value */
495 #define __darwin_arm_thread_state64_set_fp(ts, ptr) \
496 	((ts).__fp = (uintptr_t)(ptr))
497 /* Strip ptr auth bits from pc, lr, sp and fp field of arm_thread_state64_t */
498 #define __darwin_arm_thread_state64_ptrauth_strip(ts) \
499 	(void)(ts)
500 
501 #else /* __DARWIN_UNIX03 */
502 
503 /* Return pc field of arm_thread_state64_t as a data pointer value */
504 #define __darwin_arm_thread_state64_get_pc(ts) \
505 	((ts).pc)
506 /* Return pc field of arm_thread_state64_t as a function pointer */
507 #define __darwin_arm_thread_state64_get_pc_fptr(ts) \
508 	((void*)(uintptr_t)((ts).pc))
509 /* Set pc field of arm_thread_state64_t to a function pointer */
510 #define __darwin_arm_thread_state64_set_pc_fptr(ts, fptr) \
511 	((ts).pc = (uintptr_t)(fptr))
512 /* Set pc field of arm_thread_state64_t to an already signed function pointer */
513 #define __darwin_arm_thread_state64_set_pc_presigned_fptr(ts, presigned_fptr)           \
514 	((ts).pc = (uintptr_t)(presigned_fptr))
515 /* Return lr field of arm_thread_state64_t as a data pointer value */
516 #define __darwin_arm_thread_state64_get_lr(ts) \
517 	((ts).lr)
518 /* Return lr field of arm_thread_state64_t as a function pointer */
519 #define __darwin_arm_thread_state64_get_lr_fptr(ts) \
520 	((void*)(uintptr_t)((ts).lr))
521 /* Set lr field of arm_thread_state64_t to a function pointer */
522 #define __darwin_arm_thread_state64_set_lr_fptr(ts, fptr) \
523 	((ts).lr = (uintptr_t)(fptr))
524 /* Set lr field of arm_thread_state64_t to a presigned function pointer */
525 #define __darwin_arm_thread_state64_set_lr_presigned_fptr(ts, presigned_fptr) \
526 	((ts).lr = ((uintptr_t)presigned_fptr))
527 /* Return sp field of arm_thread_state64_t as a data pointer value */
528 #define __darwin_arm_thread_state64_get_sp(ts) \
529 	((ts).sp)
530 /* Set sp field of arm_thread_state64_t to a data pointer value */
531 #define __darwin_arm_thread_state64_set_sp(ts, ptr) \
532 	((ts).sp = (uintptr_t)(ptr))
533 /* Return fp field of arm_thread_state64_t as a data pointer value */
534 #define __darwin_arm_thread_state64_get_fp(ts) \
535 	((ts).fp)
536 /* Set fp field of arm_thread_state64_t to a data pointer value */
537 #define __darwin_arm_thread_state64_set_fp(ts, ptr) \
538 	((ts).fp = (uintptr_t)(ptr))
539 /* Strip ptr auth bits from pc, lr, sp and fp field of arm_thread_state64_t */
540 #define __darwin_arm_thread_state64_ptrauth_strip(ts) \
541 	(void)(ts)
542 
543 #endif /* __DARWIN_UNIX03 */
544 #endif /* __DARWIN_OPAQUE_ARM_THREAD_STATE64 */
545 
546 #endif /* __has_feature(ptrauth_calls) && defined(__LP64__) */
547 #endif /* __DARWIN_C_LEVEL >= __DARWIN_C_FULL && defined(__arm64__) */
548 #endif /* !defined(KERNEL) */
549 
550 #if __DARWIN_UNIX03
551 #define _STRUCT_ARM_VFP_STATE struct __darwin_arm_vfp_state
552 _STRUCT_ARM_VFP_STATE
553 {
554 	__uint32_t __r[64];
555 	__uint32_t __fpscr;
556 };
557 #else /* !__DARWIN_UNIX03 */
558 #define _STRUCT_ARM_VFP_STATE struct arm_vfp_state
559 _STRUCT_ARM_VFP_STATE
560 {
561 	__uint32_t r[64];
562 	__uint32_t fpscr;
563 };
564 #endif /* __DARWIN_UNIX03 */
565 
566 #if __DARWIN_UNIX03
567 #define _STRUCT_ARM_NEON_STATE64 struct __darwin_arm_neon_state64
568 #define _STRUCT_ARM_NEON_STATE   struct __darwin_arm_neon_state
569 
570 #if defined(__arm64__)
571 _STRUCT_ARM_NEON_STATE64
572 {
573 	__uint128_t __v[32];
574 	__uint32_t  __fpsr;
575 	__uint32_t  __fpcr;
576 };
577 
578 _STRUCT_ARM_NEON_STATE
579 {
580 	__uint128_t __v[16];
581 	__uint32_t  __fpsr;
582 	__uint32_t  __fpcr;
583 };
584 #elif defined(__arm__)
585 /*
586  * No 128-bit intrinsic for ARM; leave it opaque for now.
587  */
588 _STRUCT_ARM_NEON_STATE64
589 {
590 	char opaque[(32 * 16) + (2 * sizeof(__uint32_t))];
591 } __attribute__((aligned(16)));
592 
593 _STRUCT_ARM_NEON_STATE
594 {
595 	char opaque[(16 * 16) + (2 * sizeof(__uint32_t))];
596 } __attribute__((aligned(16)));
597 
598 #else
599 #error Unknown architecture.
600 #endif
601 
602 #else /* !__DARWIN_UNIX03 */
603 #define _STRUCT_ARM_NEON_STATE64 struct arm_neon_state64
604 #define _STRUCT_ARM_NEON_STATE struct arm_neon_state
605 
606 #if defined(__arm64__)
607 _STRUCT_ARM_NEON_STATE64
608 {
609 	__uint128_t q[32];
610 	uint32_t    fpsr;
611 	uint32_t    fpcr;
612 };
613 
614 _STRUCT_ARM_NEON_STATE
615 {
616 	__uint128_t q[16];
617 	uint32_t    fpsr;
618 	uint32_t    fpcr;
619 };
620 #elif defined(__arm__)
621 /*
622  * No 128-bit intrinsic for ARM; leave it opaque for now.
623  */
624 _STRUCT_ARM_NEON_STATE64
625 {
626 	char opaque[(32 * 16) + (2 * sizeof(__uint32_t))];
627 } __attribute__((aligned(16)));
628 
629 _STRUCT_ARM_NEON_STATE
630 {
631 	char opaque[(16 * 16) + (2 * sizeof(__uint32_t))];
632 } __attribute__((aligned(16)));
633 
634 #else
635 #error Unknown architecture.
636 #endif
637 
638 #endif /* __DARWIN_UNIX03 */
639 
640 
641 #define _STRUCT_ARM_PAGEIN_STATE struct __arm_pagein_state
642 _STRUCT_ARM_PAGEIN_STATE
643 {
644 	int __pagein_error;
645 };
646 
647 #if __DARWIN_UNIX03
648 #define _STRUCT_ARM_SME_STATE struct __darwin_arm_sme_state
649 _STRUCT_ARM_SME_STATE
650 {
651 	__uint64_t      __svcr;
652 	__uint64_t      __tpidr2_el0;
653 	__uint16_t      __svl_b;
654 };
655 
656 #define _STRUCT_ARM_SVE_Z_STATE struct __darwin_arm_sve_z_state
657 _STRUCT_ARM_SVE_Z_STATE
658 {
659 	char            __z[16][256];
660 } __attribute__((aligned(4)));
661 
662 #define _STRUCT_ARM_SVE_P_STATE struct __darwin_arm_sve_p_state
663 _STRUCT_ARM_SVE_P_STATE
664 {
665 	char            __p[16][256 / 8];
666 } __attribute__((aligned(4)));
667 
668 #define _STRUCT_ARM_SME_ZA_STATE struct __darwin_arm_sme_za_state
669 _STRUCT_ARM_SME_ZA_STATE
670 {
671 	char            __za[4096];
672 } __attribute__((aligned(4)));
673 
674 #define _STRUCT_ARM_SME2_STATE struct __darwin_arm_sme2_state
675 _STRUCT_ARM_SME2_STATE
676 {
677 	char            __zt0[64];
678 } __attribute__((aligned(4)));
679 #else /* !__DARWIN_UNIX03 */
680 #define _STRUCT_ARM_SME_STATE struct arm_sme_state
681 _STRUCT_ARM_SME_STATE
682 {
683 	__uint64_t      svcr;
684 	__uint64_t      tpidr2_el0;
685 	__uint16_t      svl_b;
686 };
687 
688 #define _STRUCT_ARM_SVE_Z_STATE struct arm_sve_z_state
689 _STRUCT_ARM_SVE_Z_STATE
690 {
691 	char            z[16][256];
692 } __attribute__((aligned(4)));
693 
694 #define _STRUCT_ARM_SVE_P_STATE struct arm_sve_p_state
695 _STRUCT_ARM_SVE_P_STATE
696 {
697 	char            p[16][256 / 8];
698 } __attribute__((aligned(4)));
699 
700 #define _STRUCT_ARM_SME_ZA_STATE struct arm_sme_za_state
701 _STRUCT_ARM_SME_ZA_STATE
702 {
703 	char            za[4096];
704 } __attribute__((aligned(4)));
705 
706 #define _STRUCT_ARM_SME2_STATE struct arm_sme2_state
707 _STRUCT_ARM_SME2_STATE
708 {
709 	char            zt0[64];
710 } __attribute__((aligned(4)));
711 #endif /* __DARWIN_UNIX03 */
712 
713 /*
714  * Debug State
715  */
716 #if defined(__arm__)
717 /* Old-fashioned debug state is only for ARM */
718 
719 #if __DARWIN_UNIX03
720 #define _STRUCT_ARM_DEBUG_STATE struct __darwin_arm_debug_state
721 _STRUCT_ARM_DEBUG_STATE
722 {
723 	__uint32_t __bvr[16];
724 	__uint32_t __bcr[16];
725 	__uint32_t __wvr[16];
726 	__uint32_t __wcr[16];
727 };
728 #else /* !__DARWIN_UNIX03 */
729 #define _STRUCT_ARM_DEBUG_STATE struct arm_debug_state
730 _STRUCT_ARM_DEBUG_STATE
731 {
732 	__uint32_t bvr[16];
733 	__uint32_t bcr[16];
734 	__uint32_t wvr[16];
735 	__uint32_t wcr[16];
736 };
737 #endif /* __DARWIN_UNIX03 */
738 
739 #elif defined(__arm64__)
740 
741 /* ARM's arm_debug_state is ARM64's arm_legacy_debug_state */
742 
743 #if __DARWIN_UNIX03
744 #define _STRUCT_ARM_LEGACY_DEBUG_STATE struct __arm_legacy_debug_state
745 _STRUCT_ARM_LEGACY_DEBUG_STATE
746 {
747 	__uint32_t __bvr[16];
748 	__uint32_t __bcr[16];
749 	__uint32_t __wvr[16];
750 	__uint32_t __wcr[16];
751 };
752 #else /* __DARWIN_UNIX03 */
753 #define _STRUCT_ARM_LEGACY_DEBUG_STATE struct arm_legacy_debug_state
754 _STRUCT_ARM_LEGACY_DEBUG_STATE
755 {
756 	__uint32_t bvr[16];
757 	__uint32_t bcr[16];
758 	__uint32_t wvr[16];
759 	__uint32_t wcr[16];
760 };
761 #endif /* __DARWIN_UNIX03 */
762 #else
763 #error unknown architecture
764 #endif
765 
766 #if __DARWIN_UNIX03
767 #define _STRUCT_ARM_DEBUG_STATE32 struct __darwin_arm_debug_state32
768 _STRUCT_ARM_DEBUG_STATE32
769 {
770 	__uint32_t __bvr[16];
771 	__uint32_t __bcr[16];
772 	__uint32_t __wvr[16];
773 	__uint32_t __wcr[16];
774 	__uint64_t __mdscr_el1; /* Bit 0 is SS (Hardware Single Step) */
775 };
776 
777 #define _STRUCT_ARM_DEBUG_STATE64 struct __darwin_arm_debug_state64
778 _STRUCT_ARM_DEBUG_STATE64
779 {
780 	__uint64_t __bvr[16];
781 	__uint64_t __bcr[16];
782 	__uint64_t __wvr[16];
783 	__uint64_t __wcr[16];
784 	__uint64_t __mdscr_el1; /* Bit 0 is SS (Hardware Single Step) */
785 };
786 #else /* !__DARWIN_UNIX03 */
787 #define _STRUCT_ARM_DEBUG_STATE32 struct arm_debug_state32
788 _STRUCT_ARM_DEBUG_STATE32
789 {
790 	__uint32_t bvr[16];
791 	__uint32_t bcr[16];
792 	__uint32_t wvr[16];
793 	__uint32_t wcr[16];
794 	__uint64_t mdscr_el1; /* Bit 0 is SS (Hardware Single Step) */
795 };
796 
797 #define _STRUCT_ARM_DEBUG_STATE64 struct arm_debug_state64
798 _STRUCT_ARM_DEBUG_STATE64
799 {
800 	__uint64_t bvr[16];
801 	__uint64_t bcr[16];
802 	__uint64_t wvr[16];
803 	__uint64_t wcr[16];
804 	__uint64_t mdscr_el1; /* Bit 0 is SS (Hardware Single Step) */
805 };
806 #endif /* __DARWIN_UNIX03 */
807 
808 #if __DARWIN_UNIX03
809 #define _STRUCT_ARM_CPMU_STATE64 struct __darwin_arm_cpmu_state64
810 _STRUCT_ARM_CPMU_STATE64
811 {
812 	__uint64_t __ctrs[16];
813 };
814 #else /* __DARWIN_UNIX03 */
815 #define _STRUCT_ARM_CPMU_STATE64 struct arm_cpmu_state64
816 _STRUCT_ARM_CPMU_STATE64
817 {
818 	__uint64_t ctrs[16];
819 };
820 #endif /* !__DARWIN_UNIX03 */
821 
822 #endif /* defined (__arm__) || defined (__arm64__) */
823 
824 #endif /* _MACH_ARM__STRUCTS_H_ */
825