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