xref: /xnu-8796.141.3/osfmk/arm/machine_routines_apple.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1 /*
2  * Copyright (c) 2020 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 #include <pexpert/pexpert.h>
30 #if __arm64__
31 #include <pexpert/arm64/board_config.h>
32 #include <arm64/hv_hvc.h>
33 #endif /* __arm64__ */
34 
35 #include <arm/cpuid_internal.h>
36 #include <arm/pmap.h>
37 #include <arm64/proc_reg.h>
38 #include <machine/machine_cpuid.h>
39 #include <machine/machine_routines.h>
40 
41 
42 #if __arm64__
43 
44 void configure_misc_apple_boot_args(void);
45 void configure_misc_apple_regs(void);
46 void configure_timer_apple_regs(void);
47 void configure_late_apple_regs(void);
48 
49 void
configure_misc_apple_boot_args(void)50 configure_misc_apple_boot_args(void)
51 {
52 }
53 
54 void
configure_misc_apple_regs(void)55 configure_misc_apple_regs(void)
56 {
57 }
58 
59 // machine_routines_apple.c gets built on non-Apple platforms but it won't
60 // #include apple_arm64_regs.h so some of the constants referenced below
61 // won't exist in those builds
62 #if APPLE_ARM64_ARCH_FAMILY
63 
64 static bool
cpu_needs_throttle_tunable(uint32_t midr_pnum)65 cpu_needs_throttle_tunable(uint32_t midr_pnum)
66 {
67 	switch (midr_pnum) {
68 
69 	default:
70 		return false;
71 	}
72 }
73 
74 /*
75  * configure_late_apple_regs()
76  *
77  * Normal tunables (HID bits) are applied early on, in the APPLY_TUNABLES
78  * asm macro.  This C function is intended to handle special cases where that
79  * isn't possible, e.g.
80  *  - Tunables that require PIO mappings
81  *  - Tunables that need access to the parsed CPU topology info
82  *
83  * Unlike configure_misc_apple_regs(), it is guaranteed to execute after
84  * ml_parse_cpu_topology() / ml_map_cpu_pio() are done,
85  * and after cpu_number() is valid.
86  */
87 void
configure_late_apple_regs(void)88 configure_late_apple_regs(void)
89 {
90 	const ml_topology_info_t *tinfo = ml_get_topology_info();
91 	uint32_t midr_pnum = machine_read_midr() & MIDR_EL1_PNUM_MASK;
92 	uint64_t reg_val;
93 
94 	if (cpu_needs_throttle_tunable(midr_pnum)) {
95 		vm_offset_t cpu_impl = tinfo->cpus[cpu_number()].cpu_IMPL_regs;
96 		const uint64_t c1pptThrtlRate = 0xb2;
97 
98 		reg_val = ml_io_read64(cpu_impl + CORE_THRTL_CFG2_OFFSET);
99 		reg_val &= ~(0xffULL << 56);
100 		reg_val |= c1pptThrtlRate << 56;
101 		ml_io_write64(cpu_impl + CORE_THRTL_CFG2_OFFSET, reg_val);
102 	}
103 
104 }
105 #endif /* APPLE_ARM64_ARCH_FAMILY */
106 
107 void
configure_timer_apple_regs(void)108 configure_timer_apple_regs(void)
109 {
110 }
111 
112 #endif /* __arm64__ */
113 
114 #if HAS_APPLE_PAC
115 
116 #if HAS_PARAVIRTUALIZED_PAC
117 static uint64_t vmapple_default_rop_pid;
118 static uint64_t vmapple_default_jop_pid;
119 
120 static inline void
vmapple_pac_get_default_keys()121 vmapple_pac_get_default_keys()
122 {
123 	static bool initialized = false;
124 	if (os_atomic_xchg(&initialized, true, relaxed)) {
125 		return;
126 	}
127 
128 	const uint64_t fn = VMAPPLE_PAC_GET_DEFAULT_KEYS;
129 	asm volatile (
130                 "mov	x0, %[fn]"      "\n"
131                 "hvc	#0"             "\n"
132                 "str	x2, %[b_key]"   "\n"
133                 "str	x3, %[el0_key]" "\n"
134                 : [b_key] "=m"(vmapple_default_rop_pid),
135                   [el0_key] "=m"(vmapple_default_jop_pid)
136                 : [fn] "r"(fn)
137                 : "x0", "x1", "x2", "x3", "x4"
138         );
139 }
140 
141 #endif /* HAS_PARAVIRTUALIZED_PAC */
142 
143 /**
144  * Returns the default ROP key.
145  */
146 uint64_t
ml_default_rop_pid(void)147 ml_default_rop_pid(void)
148 {
149 #if HAS_PARAVIRTUALIZED_PAC
150 	vmapple_pac_get_default_keys();
151 	return vmapple_default_rop_pid;
152 #else
153 	return 0;
154 #endif /* HAS_PARAVIRTUALIZED_PAC */
155 }
156 
157 /**
158  * Returns the default JOP key.
159  */
160 uint64_t
ml_default_jop_pid(void)161 ml_default_jop_pid(void)
162 {
163 #if HAS_PARAVIRTUALIZED_PAC
164 	vmapple_pac_get_default_keys();
165 	return vmapple_default_jop_pid;
166 #else
167 	return 0;
168 #endif /* HAS_PARAVIRTUALIZED_PAC */
169 }
170 #endif /* HAS_APPLE_PAC */
171