1 /*
2 * Copyright (c) 2007-2021 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 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or [email protected]
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58
59 #ifndef _ARM_THREAD_H_
60 #define _ARM_THREAD_H_
61
62 #include <mach/mach_types.h>
63 #include <mach/boolean.h>
64 #include <mach/arm/vm_types.h>
65 #include <mach/thread_status.h>
66
67 #ifdef MACH_KERNEL_PRIVATE
68 #include <arm/cpu_data.h>
69 #include <arm64/proc_reg.h>
70 #include <os/base.h>
71 #if SCHED_HYGIENE_DEBUG
72 #include <kern/timeout_decl.h>
73 #endif
74 #endif /* MACH_KERNEL_PRIVATE */
75
76 struct perfcontrol_state {
77 uint64_t opaque[8] __attribute__((aligned(8)));
78 };
79
80 /*
81 * Maps state flavor to number of words in the state:
82 */
83 extern unsigned int _MachineStateCount[];
84
85 #ifdef MACH_KERNEL_PRIVATE
86 typedef arm_kernel_context_t machine_thread_kernel_state;
87 #include <kern/thread_kernel_state.h>
88
89 #if (!__arm64__)
90 #error Unknown arch
91 #endif
92
93 #if HAS_ARM_FEAT_SME
94 #define HAVE_MACHINE_THREAD_MATRIX_STATE 1
95 #endif
96
97
98 #if HAVE_MACHINE_THREAD_MATRIX_STATE
99 #define UMATRIX_PTRAUTH XNU_PTRAUTH_SIGNED_PTR("machine_thread.umatrix_hdr")
100 #endif
101
102 /*
103 * Machine Thread Structure
104 */
105 struct machine_thread {
106 #if __ARM_USER_PROTECT__
107 unsigned int uptw_ttb;
108 unsigned int kptw_ttb;
109 unsigned int asid;
110 #else
111 unsigned int reserved0;
112 unsigned int reserved1;
113 unsigned int reserved2;
114 #endif
115
116 uint32_t arm_machine_flags; /* thread flags (arm64/machine_machdep.h) */
117 arm_context_t * contextData; /* allocated user context */
118 arm_saved_state_t * XNU_PTRAUTH_SIGNED_PTR("machine_thread.upcb") upcb; /* pointer to user GPR state */
119 arm_neon_saved_state_t * XNU_PTRAUTH_SIGNED_PTR("machine_thread.uNeon") uNeon; /* pointer to user VFP state */
120 arm_saved_state_t * kpcb; /* pointer to kernel GPR state */
121
122 #if HAVE_MACHINE_THREAD_MATRIX_STATE
123 union {
124 arm_state_hdr_t *UMATRIX_PTRAUTH umatrix_hdr;
125 #if HAS_ARM_FEAT_SME
126 arm_sme_saved_state_t *UMATRIX_PTRAUTH usme; /* pointer to user SME state */
127 #endif
128 };
129 #endif /* HAVE_MACHINE_THREAD_MATRIX_STATE */
130
131 long reserved4;
132 uint64_t recover_far;
133
134 arm_debug_state_t *DebugData;
135 vm_address_t cthread_self; /* for use of cthread package */
136
137 uint64_t recover_esr;
138
139 void * XNU_PTRAUTH_SIGNED_PTR("machine_thread.kstackptr") kstackptr; /* top of kernel stack */
140 struct perfcontrol_state perfctrl_state;
141 uint64_t reserved5;
142
143 #if SCHED_HYGIENE_DEBUG
144 kern_timeout_t int_timeout; /* for interrupt disabled timeout mechanism */
145 unsigned int int_type; /* interrupt type of the interrupt that was processed */
146 uintptr_t int_handler_addr; /* slid, ptrauth-stripped virtual address of the interrupt handler */
147 uintptr_t int_vector; /* IOInterruptVector */
148 uint64_t int_time_mt; /* total time spent in interrupt context */
149 #endif /* SCHED_HYGIENE_DEBUG */
150
151 #if defined(CONFIG_XNUPOST)
152 volatile expected_fault_handler_t expected_fault_handler;
153 volatile uintptr_t expected_fault_addr; /* Address due to which an exception is expected to be thrown (FAR_ELx) */
154 volatile uintptr_t expected_fault_pc; /* PC at which an exception is expected to be thrown (ELR_ELx) */
155 #endif
156
157 uint64_t reserved6;
158 union {
159 long pcpu_data_base_and_cpu_number;
160 const uint16_t cpu_number;
161 };
162 struct cpu_data * CpuDatap; /* current per cpu data */
163 unsigned int preemption_count; /* preemption count */
164 uint16_t exception_trace_code;
165 bool reserved7;
166 #if HAS_MTE
167 bool sec_override; /* disable MTE for this thread, regardless of the current map's MTE policy */
168 bool el0_synchronous_trap; /* is this thread inside an EL0 synchronous trap handler? */
169 #else
170 bool reserved8;
171 bool reserved9;
172 #endif
173 #if defined(HAS_APPLE_PAC)
174 uint64_t rop_pid;
175 uint64_t jop_pid;
176 #else
177 uint64_t reserved10;
178 uint64_t reserved11;
179 #endif
180
181 uint64_t reserved12;
182
183 #if HAS_ARM_FEAT_SME
184 uint64_t tpidr2_el0;
185 #else
186 uint64_t reserved13;
187 #endif
188
189 uint64_t reserved14;
190
191 #if HAS_MTE
192 bool in_unprivileged_access;
193 #else
194 bool reserved15;
195 #endif
196 };
197 #endif
198
199 static inline long
ml_make_pcpu_base_and_cpu_number(long base,uint16_t cpu)200 ml_make_pcpu_base_and_cpu_number(long base, uint16_t cpu)
201 {
202 return (base << 16) | cpu;
203 }
204
205 extern struct arm_saved_state * get_user_regs(thread_t);
206 extern struct arm_saved_state * find_user_regs(thread_t);
207 extern struct arm_saved_state * find_kern_regs(thread_t);
208 extern struct arm_vfpsaved_state * find_user_vfp(thread_t);
209 extern arm_debug_state32_t * find_debug_state32(thread_t);
210 extern arm_debug_state32_t * find_or_allocate_debug_state32(thread_t);
211 extern arm_debug_state64_t * find_debug_state64(thread_t);
212 extern arm_debug_state64_t * find_or_allocate_debug_state64(thread_t);
213 extern void set_user_neon_reg(thread_t, unsigned int, uint128_t);
214
215 #define FIND_PERFCONTROL_STATE(th) (&th->machine.perfctrl_state)
216
217 #ifdef MACH_KERNEL_PRIVATE
218 #if __ARM_VFP__
219 extern void vfp_state_initialize(struct arm_vfpsaved_state *vfp_state);
220 extern void vfp_save(struct arm_vfpsaved_state *vfp_ss);
221 extern void vfp_load(struct arm_vfpsaved_state *vfp_ss);
222 #endif /* __ARM_VFP__ */
223 extern void arm_debug_set(arm_debug_state_t *debug_state);
224 extern void arm_debug_set32(arm_debug_state_t *debug_state);
225 extern void arm_debug_set64(arm_debug_state_t *debug_state);
226 #endif /* MACH_KERNEL_PRIVATE */
227
228 extern void *act_thread_csave(void);
229 extern void act_thread_catt(void *ctx);
230 extern void act_thread_cfree(void *ctx);
231
232 #if MACH_KERNEL_PRIVATE
233
234 #if HAS_ARM_FEAT_SME
235 extern arm_sme_saved_state_t *machine_thread_get_sme_state(thread_t thread);
236 extern kern_return_t machine_thread_sme_state_alloc(thread_t thread);
237 #endif
238
239 #if HAVE_MACHINE_THREAD_MATRIX_STATE
240 extern void machine_thread_matrix_state_dup(thread_t target);
241 #endif
242 #endif /* MACH_KERNEL_PRIVATE */
243
244 #if HAS_APPLE_GENERIC_TIMER
245 extern void agt_thread_bootstrap(void);
246 #endif /* HAS_MACHINE_GENERIC_TIMER */
247
248
249 /*
250 * Return address of the function that called current function, given
251 * address of the first parameter of current function.
252 */
253 #define GET_RETURN_PC(addr) (__builtin_return_address(0))
254
255 #endif /* _ARM_THREAD_H_ */
256