xref: /xnu-11215.41.3/osfmk/arm/thread.h (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
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 #endif
72 
73 struct perfcontrol_state {
74 	uint64_t opaque[8] __attribute__((aligned(8)));
75 };
76 
77 /*
78  * Maps state flavor to number of words in the state:
79  */
80 extern unsigned int _MachineStateCount[];
81 
82 #ifdef MACH_KERNEL_PRIVATE
83 typedef arm_kernel_context_t machine_thread_kernel_state;
84 #include <kern/thread_kernel_state.h>
85 
86 #if (!__arm64__)
87 #error Unknown arch
88 #endif
89 
90 #if HAS_ARM_FEAT_SME
91 #define HAVE_MACHINE_THREAD_MATRIX_STATE        1
92 #endif
93 
94 
95 /*
96  * Machine Thread Structure
97  */
98 struct machine_thread {
99 #if __ARM_USER_PROTECT__
100 	unsigned int              uptw_ttb;
101 	unsigned int              kptw_ttb;
102 	unsigned int              asid;
103 #else
104 	unsigned int              reserved0;
105 	unsigned int              reserved1;
106 	unsigned int              reserved2;
107 #endif
108 
109 	uint32_t                  arm_machine_flags;       /* thread flags (arm64/machine_machdep.h) */
110 	arm_context_t *           contextData;             /* allocated user context */
111 	arm_saved_state_t *       XNU_PTRAUTH_SIGNED_PTR("machine_thread.upcb") upcb;   /* pointer to user GPR state */
112 	arm_neon_saved_state_t *  XNU_PTRAUTH_SIGNED_PTR("machine_thread.uNeon") uNeon; /* pointer to user VFP state */
113 	arm_saved_state_t *       kpcb;                    /* pointer to kernel GPR state */
114 
115 #if HAVE_MACHINE_THREAD_MATRIX_STATE
116 	union {
117 		arm_state_hdr_t *umatrix_hdr;
118 #if HAS_ARM_FEAT_SME
119 		arm_sme_saved_state_t *usme;               /* pointer to user SME state */
120 #endif
121 	};
122 #endif /* HAVE_MACHINE_THREAD_MATRIX_STATE */
123 
124 	long                      reserved4;
125 	uint64_t                  recover_far;
126 
127 	arm_debug_state_t        *DebugData;
128 	vm_address_t              cthread_self;               /* for use of cthread package */
129 
130 	uint64_t                  recover_esr;
131 
132 	void *                    XNU_PTRAUTH_SIGNED_PTR("machine_thread.kstackptr") kstackptr; /* top of kernel stack */
133 	struct perfcontrol_state  perfctrl_state;
134 	uint64_t                  reserved5;
135 
136 #if SCHED_HYGIENE_DEBUG
137 	uint64_t                  intmask_timestamp;          /* timestamp of when interrupts were manually masked */
138 	uint64_t                  inthandler_timestamp;       /* timestamp of when interrupt handler started */
139 	uint64_t                  intmask_cycles;             /* cycle count snapshot of when interrupts were masked */
140 	uint64_t                  intmask_instr;              /* instruction count snapshot of when interrupts were masked */
141 	bool                      inthandler_abandon;         /* whether to abandon the current measurement */
142 
143 	unsigned int              int_type;                   /* interrupt type of the interrupt that was processed */
144 	uintptr_t                 int_handler_addr;           /* slid, ptrauth-stripped virtual address of the interrupt handler */
145 	uintptr_t                 int_vector;                 /* IOInterruptVector */
146 	uint64_t                  int_time_mt;                /* total time spent in interrupt context */
147 #endif /* SCHED_HYGIENE_DEBUG */
148 
149 #if defined(CONFIG_XNUPOST)
150 	volatile expected_fault_handler_t  expected_fault_handler;
151 	volatile uintptr_t                 expected_fault_addr;  /* Address due to which an exception is expected to be thrown (FAR_ELx) */
152 	volatile uintptr_t                 expected_fault_pc;    /* PC at which an exception is expected to be thrown (ELR_ELx) */
153 #endif
154 
155 	uint64_t                  reserved6;
156 	union {
157 		long              pcpu_data_base_and_cpu_number;
158 		const uint16_t    cpu_number;
159 	};
160 	struct cpu_data *         CpuDatap;               /* current per cpu data */
161 	unsigned int              preemption_count;       /* preemption count */
162 	uint16_t                  exception_trace_code;
163 	bool                      reserved7;
164 #if defined(HAS_APPLE_PAC)
165 	uint64_t                  rop_pid;
166 	uint64_t                  jop_pid;
167 #else
168 	uint64_t                  reserved8;
169 	uint64_t                  reserved9;
170 #endif
171 
172 	uint64_t                  reserved10;
173 
174 #if HAS_ARM_FEAT_SME
175 	uint64_t                  tpidr2_el0;
176 #else
177 	uint64_t                  reserved12;
178 #endif
179 };
180 #endif
181 
182 static inline long
ml_make_pcpu_base_and_cpu_number(long base,uint16_t cpu)183 ml_make_pcpu_base_and_cpu_number(long base, uint16_t cpu)
184 {
185 	return (base << 16) | cpu;
186 }
187 
188 extern struct arm_saved_state *    get_user_regs(thread_t);
189 extern struct arm_saved_state *    find_user_regs(thread_t);
190 extern struct arm_saved_state *    find_kern_regs(thread_t);
191 extern struct arm_vfpsaved_state * find_user_vfp(thread_t);
192 extern arm_debug_state32_t *       find_debug_state32(thread_t);
193 extern arm_debug_state32_t *       find_or_allocate_debug_state32(thread_t);
194 extern arm_debug_state64_t *       find_debug_state64(thread_t);
195 extern arm_debug_state64_t *       find_or_allocate_debug_state64(thread_t);
196 extern arm_neon_saved_state_t *    get_user_neon_regs(thread_t);
197 
198 #define FIND_PERFCONTROL_STATE(th) (&th->machine.perfctrl_state)
199 
200 #ifdef MACH_KERNEL_PRIVATE
201 #if __ARM_VFP__
202 extern void vfp_state_initialize(struct arm_vfpsaved_state *vfp_state);
203 extern void vfp_save(struct arm_vfpsaved_state *vfp_ss);
204 extern void vfp_load(struct arm_vfpsaved_state *vfp_ss);
205 #endif /* __ARM_VFP__ */
206 extern void arm_debug_set(arm_debug_state_t *debug_state);
207 extern void arm_debug_set32(arm_debug_state_t *debug_state);
208 extern void arm_debug_set64(arm_debug_state_t *debug_state);
209 #endif /* MACH_KERNEL_PRIVATE */
210 
211 extern void *act_thread_csave(void);
212 extern void act_thread_catt(void *ctx);
213 extern void act_thread_cfree(void *ctx);
214 
215 #if MACH_KERNEL_PRIVATE
216 
217 #if HAS_ARM_FEAT_SME
218 extern arm_sme_saved_state_t *machine_thread_get_sme_state(thread_t thread);
219 extern kern_return_t machine_thread_sme_state_alloc(thread_t thread);
220 extern void machine_thread_sme_state_free(thread_t thread);
221 #endif
222 
223 #if HAVE_MACHINE_THREAD_MATRIX_STATE
224 extern void machine_thread_matrix_state_dup(thread_t target);
225 #endif
226 #endif /* MACH_KERNEL_PRIVATE */
227 
228 #if HAS_APPLE_GENERIC_TIMER
229 extern void agt_thread_bootstrap(void);
230 #endif /* HAS_MACHINE_GENERIC_TIMER */
231 /*
232  * Return address of the function that called current function, given
233  * address of the first parameter of current function.
234  */
235 #define GET_RETURN_PC(addr) (__builtin_return_address(0))
236 
237 #endif /* _ARM_THREAD_H_ */
238