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 #endif /* __arm64__ */
33
34 #include <arm/cpuid_internal.h>
35 #include <arm/pmap.h>
36 #include <arm/proc_reg.h>
37 #include <machine/machine_cpuid.h>
38 #include <machine/machine_routines.h>
39
40
41 #if __arm64__
42
43 void configure_misc_apple_boot_args(void);
44 void configure_misc_apple_regs(void);
45 void configure_timer_apple_regs(void);
46
47 void
configure_misc_apple_boot_args(void)48 configure_misc_apple_boot_args(void)
49 {
50 }
51
52 void
configure_misc_apple_regs(void)53 configure_misc_apple_regs(void)
54 {
55 }
56
57 void
configure_timer_apple_regs(void)58 configure_timer_apple_regs(void)
59 {
60 }
61
62 #endif /* __arm64__ */
63
64 #if HAS_APPLE_PAC
65
66 #if HAS_PARAVIRTUALIZED_PAC
67 static uint64_t vmapple_default_rop_pid;
68 static uint64_t vmapple_default_jop_pid;
69
70 static inline void
vmapple_pac_get_default_keys()71 vmapple_pac_get_default_keys()
72 {
73 static bool initialized = false;
74 if (os_atomic_xchg(&initialized, true, relaxed)) {
75 return;
76 }
77
78 const uint64_t fn = VMAPPLE_PAC_GET_DEFAULT_KEYS;
79 asm volatile (
80 "mov x0, %[fn]" "\n"
81 "hvc #0" "\n"
82 "str x2, %[b_key]" "\n"
83 "str x3, %[el0_key]" "\n"
84 : [b_key] "=m"(vmapple_default_rop_pid),
85 [el0_key] "=m"(vmapple_default_jop_pid)
86 : [fn] "r"(fn)
87 : "x0", "x1", "x2", "x3", "x4"
88 );
89 }
90
91 #endif /* HAS_PARAVIRTUALIZED_PAC */
92
93 /**
94 * Returns the default ROP key.
95 */
96 uint64_t
ml_default_rop_pid(void)97 ml_default_rop_pid(void)
98 {
99 #if HAS_PARAVIRTUALIZED_PAC
100 vmapple_pac_get_default_keys();
101 return vmapple_default_rop_pid;
102 #else
103 return 0;
104 #endif /* HAS_PARAVIRTUALIZED_PAC */
105 }
106
107 /**
108 * Returns the default JOP key.
109 */
110 uint64_t
ml_default_jop_pid(void)111 ml_default_jop_pid(void)
112 {
113 #if HAS_PARAVIRTUALIZED_PAC
114 vmapple_pac_get_default_keys();
115 return vmapple_default_jop_pid;
116 #else
117 return 0;
118 #endif /* HAS_PARAVIRTUALIZED_PAC */
119 }
120 #endif /* HAS_APPLE_PAC */
121