xref: /xnu-10002.61.3/osfmk/arm/cpu_data.h (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1*0f4c859eSApple OSS Distributions /*
2*0f4c859eSApple OSS Distributions  * Copyright (c) 2007 Apple Inc. All rights reserved.
3*0f4c859eSApple OSS Distributions  *
4*0f4c859eSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*0f4c859eSApple OSS Distributions  *
6*0f4c859eSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*0f4c859eSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*0f4c859eSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*0f4c859eSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*0f4c859eSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*0f4c859eSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*0f4c859eSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*0f4c859eSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*0f4c859eSApple OSS Distributions  *
15*0f4c859eSApple OSS Distributions  * Please obtain a copy of the License at
16*0f4c859eSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*0f4c859eSApple OSS Distributions  *
18*0f4c859eSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*0f4c859eSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*0f4c859eSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*0f4c859eSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*0f4c859eSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*0f4c859eSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*0f4c859eSApple OSS Distributions  * limitations under the License.
25*0f4c859eSApple OSS Distributions  *
26*0f4c859eSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*0f4c859eSApple OSS Distributions  */
28*0f4c859eSApple OSS Distributions /*
29*0f4c859eSApple OSS Distributions  * @OSF_COPYRIGHT@
30*0f4c859eSApple OSS Distributions  *
31*0f4c859eSApple OSS Distributions  */
32*0f4c859eSApple OSS Distributions 
33*0f4c859eSApple OSS Distributions #ifndef ARM_CPU_DATA
34*0f4c859eSApple OSS Distributions #define ARM_CPU_DATA
35*0f4c859eSApple OSS Distributions 
36*0f4c859eSApple OSS Distributions #ifdef  MACH_KERNEL_PRIVATE
37*0f4c859eSApple OSS Distributions 
38*0f4c859eSApple OSS Distributions #include <mach_assert.h>
39*0f4c859eSApple OSS Distributions #include <kern/assert.h>
40*0f4c859eSApple OSS Distributions #include <kern/kern_types.h>
41*0f4c859eSApple OSS Distributions #include <kern/processor.h>
42*0f4c859eSApple OSS Distributions #include <pexpert/pexpert.h>
43*0f4c859eSApple OSS Distributions #include <arm/thread.h>
44*0f4c859eSApple OSS Distributions #include <arm64/proc_reg.h>
45*0f4c859eSApple OSS Distributions 
46*0f4c859eSApple OSS Distributions #include <mach/mach_types.h>
47*0f4c859eSApple OSS Distributions #include <machine/thread.h>
48*0f4c859eSApple OSS Distributions 
49*0f4c859eSApple OSS Distributions __ASSUME_PTR_ABI_SINGLE_BEGIN
50*0f4c859eSApple OSS Distributions 
51*0f4c859eSApple OSS Distributions static inline __attribute__((const)) thread_t
current_thread_fast(void)52*0f4c859eSApple OSS Distributions current_thread_fast(void)
53*0f4c859eSApple OSS Distributions {
54*0f4c859eSApple OSS Distributions #if defined(__arm64__)
55*0f4c859eSApple OSS Distributions 	/*
56*0f4c859eSApple OSS Distributions 	 * rdar://73762648 clang nowadays insists that this is not constant
57*0f4c859eSApple OSS Distributions 	 *
58*0f4c859eSApple OSS Distributions 	 *     __builtin_arm_rsr64("TPIDR_EL1")
59*0f4c859eSApple OSS Distributions 	 *
60*0f4c859eSApple OSS Distributions 	 * and ignores the "attribute const", so do it the "dumb" way.
61*0f4c859eSApple OSS Distributions 	 */
62*0f4c859eSApple OSS Distributions 	unsigned long result;
63*0f4c859eSApple OSS Distributions 	__asm__ ("mrs %0, TPIDR_EL1" : "=r" (result));
64*0f4c859eSApple OSS Distributions 	return __unsafe_forge_single(thread_t, result);
65*0f4c859eSApple OSS Distributions #else
66*0f4c859eSApple OSS Distributions 	// TPIDRPRW
67*0f4c859eSApple OSS Distributions 	return __unsafe_forge_single(thread_t, __builtin_arm_mrc(15, 0, 13, 0, 4));
68*0f4c859eSApple OSS Distributions #endif
69*0f4c859eSApple OSS Distributions }
70*0f4c859eSApple OSS Distributions 
71*0f4c859eSApple OSS Distributions /*
72*0f4c859eSApple OSS Distributions  * The "volatile" flavor of current_thread() is intended for use by
73*0f4c859eSApple OSS Distributions  * scheduler code which may need to update the thread pointer in the
74*0f4c859eSApple OSS Distributions  * course of a context switch.  Any call to current_thread() made
75*0f4c859eSApple OSS Distributions  * prior to the thread pointer update should be safe to optimize away
76*0f4c859eSApple OSS Distributions  * as it should be consistent with that thread's state to the extent
77*0f4c859eSApple OSS Distributions  * the compiler can reason about it.  Likewise, the context switch
78*0f4c859eSApple OSS Distributions  * path will eventually result in an arbitrary branch to the new
79*0f4c859eSApple OSS Distributions  * thread's pc, about which the compiler won't be able to reason.
80*0f4c859eSApple OSS Distributions  * Thus any compile-time optimization of current_thread() calls made
81*0f4c859eSApple OSS Distributions  * within the new thread should be safely encapsulated in its
82*0f4c859eSApple OSS Distributions  * register/stack state.  The volatile form therefore exists to cover
83*0f4c859eSApple OSS Distributions  * the window between the thread pointer update and the branch to
84*0f4c859eSApple OSS Distributions  * the new pc.
85*0f4c859eSApple OSS Distributions  */
86*0f4c859eSApple OSS Distributions static inline thread_t
current_thread_volatile(void)87*0f4c859eSApple OSS Distributions current_thread_volatile(void)
88*0f4c859eSApple OSS Distributions {
89*0f4c859eSApple OSS Distributions 	/*
90*0f4c859eSApple OSS Distributions 	 * The compiler might decide to treat rsr64 as const (comes and goes),
91*0f4c859eSApple OSS Distributions 	 * which can allow it to eliminate redundant calls, which we don't want
92*0f4c859eSApple OSS Distributions 	 * here. Thus we use volatile asm. Which gives us control on semantics.
93*0f4c859eSApple OSS Distributions 	 *
94*0f4c859eSApple OSS Distributions 	 * The mrc used for arm32 should be treated as volatile however.
95*0f4c859eSApple OSS Distributions 	 */
96*0f4c859eSApple OSS Distributions #if defined(__arm64__)
97*0f4c859eSApple OSS Distributions 	unsigned long result;
98*0f4c859eSApple OSS Distributions 	__asm__ volatile ("mrs %0, TPIDR_EL1" : "=r" (result));
99*0f4c859eSApple OSS Distributions 	return __unsafe_forge_single(thread_t, result);
100*0f4c859eSApple OSS Distributions #else
101*0f4c859eSApple OSS Distributions 	// TPIDRPRW
102*0f4c859eSApple OSS Distributions 	return __unsafe_forge_single(thread_t, __builtin_arm_mrc(15, 0, 13, 0, 4));
103*0f4c859eSApple OSS Distributions #endif
104*0f4c859eSApple OSS Distributions }
105*0f4c859eSApple OSS Distributions 
106*0f4c859eSApple OSS Distributions #if defined(__arm64__)
107*0f4c859eSApple OSS Distributions 
108*0f4c859eSApple OSS Distributions static inline vm_offset_t
exception_stack_pointer(void)109*0f4c859eSApple OSS Distributions exception_stack_pointer(void)
110*0f4c859eSApple OSS Distributions {
111*0f4c859eSApple OSS Distributions 	vm_offset_t result = 0;
112*0f4c859eSApple OSS Distributions 	__asm__ volatile (
113*0f4c859eSApple OSS Distributions                  "msr		SPSel, #1  \n"
114*0f4c859eSApple OSS Distributions                  "mov		%0, sp     \n"
115*0f4c859eSApple OSS Distributions                  "msr		SPSel, #0  \n"
116*0f4c859eSApple OSS Distributions                  : "=r" (result));
117*0f4c859eSApple OSS Distributions 
118*0f4c859eSApple OSS Distributions 	return result;
119*0f4c859eSApple OSS Distributions }
120*0f4c859eSApple OSS Distributions 
121*0f4c859eSApple OSS Distributions #endif /* defined(__arm64__) */
122*0f4c859eSApple OSS Distributions 
123*0f4c859eSApple OSS Distributions #define getCpuDatap()            current_thread()->machine.CpuDatap
124*0f4c859eSApple OSS Distributions #define current_cpu_datap()      getCpuDatap()
125*0f4c859eSApple OSS Distributions 
126*0f4c859eSApple OSS Distributions extern int                       get_preemption_level(void);
127*0f4c859eSApple OSS Distributions extern unsigned int              get_preemption_level_for_thread(thread_t);
128*0f4c859eSApple OSS Distributions 
129*0f4c859eSApple OSS Distributions #define mp_disable_preemption()  _disable_preemption()
130*0f4c859eSApple OSS Distributions #define mp_enable_preemption()   _enable_preemption()
131*0f4c859eSApple OSS Distributions 
132*0f4c859eSApple OSS Distributions __ASSUME_PTR_ABI_SINGLE_END
133*0f4c859eSApple OSS Distributions 
134*0f4c859eSApple OSS Distributions #endif  /* MACH_KERNEL_PRIVATE */
135*0f4c859eSApple OSS Distributions 
136*0f4c859eSApple OSS Distributions #endif  /* ARM_CPU_DATA */
137