xref: /xnu-10063.141.1/osfmk/arm/machine_routines_apple.c (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
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 #include <vm/vm_protos.h>
41 
42 
43 #if __arm64__
44 
45 void configure_misc_apple_boot_args(void);
46 void configure_misc_apple_regs(bool is_boot_cpu);
47 void configure_timer_apple_regs(void);
48 void configure_late_apple_regs(bool cold_boot);
49 
50 void
configure_misc_apple_boot_args(void)51 configure_misc_apple_boot_args(void)
52 {
53 }
54 
55 
56 void
configure_misc_apple_regs(bool is_boot_cpu)57 configure_misc_apple_regs(bool is_boot_cpu)
58 {
59 #pragma unused(is_boot_cpu)
60 
61 }
62 
63 // machine_routines_apple.c gets built on non-Apple platforms but it won't
64 // #include apple_arm64_regs.h so some of the constants referenced below
65 // won't exist in those builds
66 #if APPLE_ARM64_ARCH_FAMILY
67 
68 static bool
cpu_needs_throttle_tunable(uint32_t midr_pnum)69 cpu_needs_throttle_tunable(uint32_t midr_pnum)
70 {
71 	switch (midr_pnum) {
72 #if defined(APPLEAVALANCHE)
73 	/* ACCP only */
74 	case MIDR_RHODES_DIE_AVALANCHE:
75 		return true;
76 #endif /* APPLEAVALANCHE */
77 
78 	default:
79 		return false;
80 	}
81 }
82 
83 /*
84  * configure_late_apple_regs()
85  *
86  * Normal tunables (HID bits) are applied early on, in the APPLY_TUNABLES
87  * asm macro.  This C function is intended to handle special cases where that
88  * isn't possible, e.g.
89  *  - Tunables that require PIO mappings
90  *  - Tunables that need access to the parsed CPU topology info
91  *
92  * Unlike configure_misc_apple_regs(), it is guaranteed to execute after
93  * ml_parse_cpu_topology() / ml_map_cpu_pio() are done,
94  * and after cpu_number() is valid.
95  */
96 void
configure_late_apple_regs(bool cold_boot)97 configure_late_apple_regs(bool cold_boot)
98 {
99 	const ml_topology_info_t *tinfo = ml_get_topology_info();
100 	uint32_t midr_pnum = machine_read_midr() & MIDR_EL1_PNUM_MASK;
101 	uint64_t reg_val;
102 
103 	bool apply_late_pio_regs = cold_boot;
104 	if (apply_late_pio_regs) {
105 		if (cpu_needs_throttle_tunable(midr_pnum)) {
106 			vm_offset_t cpu_impl = tinfo->cpus[cpu_number()].cpu_IMPL_regs;
107 			const uint64_t c1pptThrtlRate = 0xb2;
108 			reg_val = ml_io_read64(cpu_impl + CORE_THRTL_CFG2_OFFSET);
109 			reg_val &= ~CORE_THRTL_CFG2_c1pptThrtlRate_mask;
110 			reg_val |= c1pptThrtlRate << CORE_THRTL_CFG2_c1pptThrtlRate_shift;
111 			ml_io_write64(cpu_impl + CORE_THRTL_CFG2_OFFSET, reg_val);
112 		}
113 	}
114 
115 #if defined(APPLEAVALANCHE)
116 #endif /* APPLEAVALANCHE */
117 
118 }
119 #endif /* APPLE_ARM64_ARCH_FAMILY */
120 
121 void
configure_timer_apple_regs(void)122 configure_timer_apple_regs(void)
123 {
124 }
125 
126 #endif /* __arm64__ */
127 
128 #if HAS_APPLE_PAC
129 
130 #if HAS_PARAVIRTUALIZED_PAC
131 static uint64_t vmapple_default_rop_pid;
132 static uint64_t vmapple_default_jop_pid;
133 
134 static inline void
vmapple_pac_get_default_keys()135 vmapple_pac_get_default_keys()
136 {
137 	static bool initialized = false;
138 	if (os_atomic_xchg(&initialized, true, relaxed)) {
139 		return;
140 	}
141 
142 	const uint64_t fn = VMAPPLE_PAC_GET_DEFAULT_KEYS;
143 	asm volatile (
144                 "mov	x0, %[fn]"      "\n"
145                 "hvc	#0"             "\n"
146                 "cbnz	x0, ."          "\n"
147                 "str	x2, %[b_key]"   "\n"
148                 "str	x3, %[el0_key]" "\n"
149                 : [b_key] "=m"(vmapple_default_rop_pid),
150                   [el0_key] "=m"(vmapple_default_jop_pid)
151                 : [fn] "r"(fn)
152                 : "x0", "x1", "x2", "x3", "x4"
153         );
154 }
155 
156 #endif /* HAS_PARAVIRTUALIZED_PAC */
157 
158 /**
159  * Returns the default ROP key.
160  */
161 uint64_t
ml_default_rop_pid(void)162 ml_default_rop_pid(void)
163 {
164 #if HAS_PARAVIRTUALIZED_PAC
165 	vmapple_pac_get_default_keys();
166 	return vmapple_default_rop_pid;
167 #else
168 	return 0;
169 #endif /* HAS_PARAVIRTUALIZED_PAC */
170 }
171 
172 /**
173  * Returns the default JOP key.
174  */
175 uint64_t
ml_default_jop_pid(void)176 ml_default_jop_pid(void)
177 {
178 #if HAS_PARAVIRTUALIZED_PAC
179 	vmapple_pac_get_default_keys();
180 	return vmapple_default_jop_pid;
181 #else
182 	return 0;
183 #endif /* HAS_PARAVIRTUALIZED_PAC */
184 }
185 
186 /**
187  * Returns an appropriate JOP key for non-arm64e userspace processes.  The
188  * return value may vary from call to call.
189  */
190 uint64_t
ml_non_arm64e_user_jop_pid(void)191 ml_non_arm64e_user_jop_pid(void)
192 {
193 #if HAS_PARAVIRTUALIZED_PAC
194 	return generate_jop_key();
195 #else
196 	return 0;
197 #endif /* HAS_PARAVIRTUALIZED_PAC */
198 }
199 #endif /* HAS_APPLE_PAC */
200