xref: /xnu-8796.121.2/pexpert/gen/pe_gen.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1 /*
2  * Copyright (c) 2000-2021 Apple Computer, 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  * @OSF_FREE_COPYRIGHT@
30  */
31 
32 #include <pexpert/pexpert.h>
33 #include <pexpert/protos.h>
34 #include <pexpert/device_tree.h>
35 #include <kern/debug.h>
36 
37 #include <libkern/section_keywords.h>
38 
39 #if defined(__arm64__)
40 SECURITY_READ_ONLY_LATE(static uint32_t) gPEKernelConfigurationBitmask;
41 #else
42 static uint32_t gPEKernelConfigurationBitmask;
43 #endif
44 
45 int32_t gPESerialBaud = -1;
46 
47 int debug_cpu_performance_degradation_factor = 1;
48 
49 void
pe_init_debug(void)50 pe_init_debug(void)
51 {
52 	boolean_t boot_arg_value;
53 
54 	gPEKernelConfigurationBitmask = 0;
55 
56 	if (!PE_parse_boot_argn("assertions", &boot_arg_value, sizeof(boot_arg_value))) {
57 #if MACH_ASSERT
58 		boot_arg_value = TRUE;
59 #else
60 		boot_arg_value = FALSE;
61 #endif
62 	}
63 	gPEKernelConfigurationBitmask |= (boot_arg_value ? kPEICanHasAssertions : 0);
64 
65 	if (!PE_parse_boot_argn("statistics", &boot_arg_value, sizeof(boot_arg_value))) {
66 #if DEVELOPMENT || DEBUG
67 		boot_arg_value = TRUE;
68 #else
69 		boot_arg_value = FALSE;
70 #endif
71 	}
72 	gPEKernelConfigurationBitmask |= (boot_arg_value ? kPEICanHasStatistics : 0);
73 
74 #if SECURE_KERNEL
75 	boot_arg_value = FALSE;
76 #else
77 	if (!PE_i_can_has_debugger(NULL)) {
78 		boot_arg_value = FALSE;
79 	} else if (!PE_parse_boot_argn("diagnostic_api", &boot_arg_value, sizeof(boot_arg_value))) {
80 		boot_arg_value = TRUE;
81 	}
82 #endif
83 	gPEKernelConfigurationBitmask |= (boot_arg_value ? kPEICanHasDiagnosticAPI : 0);
84 
85 
86 	int factor = 1;
87 	boolean_t have_bootarg = PE_parse_boot_argn("cpu-factor", &factor, sizeof(factor));
88 	if (have_bootarg) {
89 		debug_cpu_performance_degradation_factor = factor;
90 	} else {
91 		DTEntry         root;
92 		if (SecureDTLookupEntry(NULL, "/", &root) == kSuccess) {
93 			void const *prop = NULL;
94 			uint32_t size = 0;
95 			if (SecureDTGetProperty(root, "target-is-fpga", &prop, &size) == kSuccess) {
96 				debug_cpu_performance_degradation_factor = 10;
97 			}
98 		}
99 	}
100 }
101 
102 void
PE_enter_debugger(const char * cause)103 PE_enter_debugger(const char *cause)
104 {
105 	if (debug_boot_arg & DB_NMI) {
106 		Debugger(cause);
107 	}
108 }
109 
110 uint32_t
PE_i_can_has_kernel_configuration(void)111 PE_i_can_has_kernel_configuration(void)
112 {
113 	return gPEKernelConfigurationBitmask;
114 }
115 
116 /* extern references */
117 extern void vcattach(void);
118 
119 /* Globals */
120 typedef void (*PE_putc_t)(char);
121 
122 #if XNU_TARGET_OS_OSX
123 PE_putc_t PE_putc;
124 #else
125 SECURITY_READ_ONLY_LATE(PE_putc_t) PE_putc;
126 #endif
127 
128 extern void console_write_char(char);
129 
130 void
PE_init_printf(boolean_t vm_initialized)131 PE_init_printf(boolean_t vm_initialized)
132 {
133 	if (!vm_initialized) {
134 		PE_putc = console_write_char;
135 	} else {
136 		vcattach();
137 	}
138 }
139 
140 uint32_t
PE_get_random_seed(unsigned char * dst_random_seed,uint32_t request_size)141 PE_get_random_seed(unsigned char *dst_random_seed, uint32_t request_size)
142 {
143 	DTEntry         entryP;
144 	uint32_t        size = 0;
145 	void            *dt_random_seed;
146 
147 	if ((SecureDTLookupEntry(NULL, "/chosen", &entryP) == kSuccess)
148 	    && (SecureDTGetProperty(entryP, "random-seed",
149 	    /* casting away the const is permissible here, since
150 	     * this function runs before lockdown. */
151 	    (const void **)(uintptr_t)&dt_random_seed, &size) == kSuccess)) {
152 		unsigned char *src_random_seed;
153 		unsigned int i;
154 		unsigned int null_count = 0;
155 
156 		src_random_seed = (unsigned char *)dt_random_seed;
157 
158 		if (size > request_size) {
159 			size = request_size;
160 		}
161 
162 		/*
163 		 * Copy from the device tree into the destination buffer,
164 		 * count the number of null bytes and null out the device tree.
165 		 */
166 		for (i = 0; i < size; i++, src_random_seed++, dst_random_seed++) {
167 			*dst_random_seed = *src_random_seed;
168 			null_count += *src_random_seed == (unsigned char)0;
169 			*src_random_seed = (unsigned char)0;
170 		}
171 		if (null_count == size) {
172 			/* All nulls is no seed - return 0 */
173 			size = 0;
174 		}
175 	}
176 
177 	return size;
178 }
179 
180 unsigned char appleClut8[256 * 3] = {
181 // 00
182 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xCC, 0xFF, 0xFF, 0x99, 0xFF, 0xFF, 0x66,
183 	0xFF, 0xFF, 0x33, 0xFF, 0xFF, 0x00, 0xFF, 0xCC, 0xFF, 0xFF, 0xCC, 0xCC,
184 	0xFF, 0xCC, 0x99, 0xFF, 0xCC, 0x66, 0xFF, 0xCC, 0x33, 0xFF, 0xCC, 0x00,
185 	0xFF, 0x99, 0xFF, 0xFF, 0x99, 0xCC, 0xFF, 0x99, 0x99, 0xFF, 0x99, 0x66,
186 // 10
187 	0xFF, 0x99, 0x33, 0xFF, 0x99, 0x00, 0xFF, 0x66, 0xFF, 0xFF, 0x66, 0xCC,
188 	0xFF, 0x66, 0x99, 0xFF, 0x66, 0x66, 0xFF, 0x66, 0x33, 0xFF, 0x66, 0x00,
189 	0xFF, 0x33, 0xFF, 0xFF, 0x33, 0xCC, 0xFF, 0x33, 0x99, 0xFF, 0x33, 0x66,
190 	0xFF, 0x33, 0x33, 0xFF, 0x33, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xCC,
191 // 20
192 	0xFF, 0x00, 0x99, 0xFF, 0x00, 0x66, 0xFF, 0x00, 0x33, 0xFF, 0x00, 0x00,
193 	0xCC, 0xFF, 0xFF, 0xCC, 0xFF, 0xCC, 0xCC, 0xFF, 0x99, 0xCC, 0xFF, 0x66,
194 	0xCC, 0xFF, 0x33, 0xCC, 0xFF, 0x00, 0xCC, 0xCC, 0xFF, 0xCC, 0xCC, 0xCC,
195 	0xCC, 0xCC, 0x99, 0xCC, 0xCC, 0x66, 0xCC, 0xCC, 0x33, 0xCC, 0xCC, 0x00,
196 // 30
197 	0xCC, 0x99, 0xFF, 0xCC, 0x99, 0xCC, 0xCC, 0x99, 0x99, 0xCC, 0x99, 0x66,
198 	0xCC, 0x99, 0x33, 0xCC, 0x99, 0x00, 0xCC, 0x66, 0xFF, 0xCC, 0x66, 0xCC,
199 	0xCC, 0x66, 0x99, 0xCC, 0x66, 0x66, 0xCC, 0x66, 0x33, 0xCC, 0x66, 0x00,
200 	0xCC, 0x33, 0xFF, 0xCC, 0x33, 0xCC, 0xCC, 0x33, 0x99, 0xCC, 0x33, 0x66,
201 // 40
202 	0xCC, 0x33, 0x33, 0xCC, 0x33, 0x00, 0xCC, 0x00, 0xFF, 0xCC, 0x00, 0xCC,
203 	0xCC, 0x00, 0x99, 0xCC, 0x00, 0x66, 0xCC, 0x00, 0x33, 0xCC, 0x00, 0x00,
204 	0x99, 0xFF, 0xFF, 0x99, 0xFF, 0xCC, 0x99, 0xFF, 0x99, 0x99, 0xFF, 0x66,
205 	0x99, 0xFF, 0x33, 0x99, 0xFF, 0x00, 0x99, 0xCC, 0xFF, 0x99, 0xCC, 0xCC,
206 // 50
207 	0x99, 0xCC, 0x99, 0x99, 0xCC, 0x66, 0x99, 0xCC, 0x33, 0x99, 0xCC, 0x00,
208 	0x99, 0x99, 0xFF, 0x99, 0x99, 0xCC, 0x99, 0x99, 0x99, 0x99, 0x99, 0x66,
209 	0x99, 0x99, 0x33, 0x99, 0x99, 0x00, 0x99, 0x66, 0xFF, 0x99, 0x66, 0xCC,
210 	0x99, 0x66, 0x99, 0x99, 0x66, 0x66, 0x99, 0x66, 0x33, 0x99, 0x66, 0x00,
211 // 60
212 	0x99, 0x33, 0xFF, 0x99, 0x33, 0xCC, 0x99, 0x33, 0x99, 0x99, 0x33, 0x66,
213 	0x99, 0x33, 0x33, 0x99, 0x33, 0x00, 0x99, 0x00, 0xFF, 0x99, 0x00, 0xCC,
214 	0x99, 0x00, 0x99, 0x99, 0x00, 0x66, 0x99, 0x00, 0x33, 0x99, 0x00, 0x00,
215 	0x66, 0xFF, 0xFF, 0x66, 0xFF, 0xCC, 0x66, 0xFF, 0x99, 0x66, 0xFF, 0x66,
216 // 70
217 	0x66, 0xFF, 0x33, 0x66, 0xFF, 0x00, 0x66, 0xCC, 0xFF, 0x66, 0xCC, 0xCC,
218 	0x66, 0xCC, 0x99, 0x66, 0xCC, 0x66, 0x66, 0xCC, 0x33, 0x66, 0xCC, 0x00,
219 	0x66, 0x99, 0xFF, 0x66, 0x99, 0xCC, 0x66, 0x99, 0x99, 0x66, 0x99, 0x66,
220 	0x66, 0x99, 0x33, 0x66, 0x99, 0x00, 0x66, 0x66, 0xFF, 0x66, 0x66, 0xCC,
221 // 80
222 	0x66, 0x66, 0x99, 0x66, 0x66, 0x66, 0x66, 0x66, 0x33, 0x66, 0x66, 0x00,
223 	0x66, 0x33, 0xFF, 0x66, 0x33, 0xCC, 0x66, 0x33, 0x99, 0x66, 0x33, 0x66,
224 	0x66, 0x33, 0x33, 0x66, 0x33, 0x00, 0x66, 0x00, 0xFF, 0x66, 0x00, 0xCC,
225 	0x66, 0x00, 0x99, 0x66, 0x00, 0x66, 0x66, 0x00, 0x33, 0x66, 0x00, 0x00,
226 // 90
227 	0x33, 0xFF, 0xFF, 0x33, 0xFF, 0xCC, 0x33, 0xFF, 0x99, 0x33, 0xFF, 0x66,
228 	0x33, 0xFF, 0x33, 0x33, 0xFF, 0x00, 0x33, 0xCC, 0xFF, 0x33, 0xCC, 0xCC,
229 	0x33, 0xCC, 0x99, 0x33, 0xCC, 0x66, 0x33, 0xCC, 0x33, 0x33, 0xCC, 0x00,
230 	0x33, 0x99, 0xFF, 0x33, 0x99, 0xCC, 0x33, 0x99, 0x99, 0x33, 0x99, 0x66,
231 // a0
232 	0x33, 0x99, 0x33, 0x33, 0x99, 0x00, 0x33, 0x66, 0xFF, 0x33, 0x66, 0xCC,
233 	0x33, 0x66, 0x99, 0x33, 0x66, 0x66, 0x33, 0x66, 0x33, 0x33, 0x66, 0x00,
234 	0x33, 0x33, 0xFF, 0x33, 0x33, 0xCC, 0x33, 0x33, 0x99, 0x33, 0x33, 0x66,
235 	0x33, 0x33, 0x33, 0x33, 0x33, 0x00, 0x33, 0x00, 0xFF, 0x33, 0x00, 0xCC,
236 // b0
237 	0x33, 0x00, 0x99, 0x33, 0x00, 0x66, 0x33, 0x00, 0x33, 0x33, 0x00, 0x00,
238 	0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xCC, 0x00, 0xFF, 0x99, 0x00, 0xFF, 0x66,
239 	0x00, 0xFF, 0x33, 0x00, 0xFF, 0x00, 0x00, 0xCC, 0xFF, 0x00, 0xCC, 0xCC,
240 	0x00, 0xCC, 0x99, 0x00, 0xCC, 0x66, 0x00, 0xCC, 0x33, 0x00, 0xCC, 0x00,
241 // c0
242 	0x00, 0x99, 0xFF, 0x00, 0x99, 0xCC, 0x00, 0x99, 0x99, 0x00, 0x99, 0x66,
243 	0x00, 0x99, 0x33, 0x00, 0x99, 0x00, 0x00, 0x66, 0xFF, 0x00, 0x66, 0xCC,
244 	0x00, 0x66, 0x99, 0x00, 0x66, 0x66, 0x00, 0x66, 0x33, 0x00, 0x66, 0x00,
245 	0x00, 0x33, 0xFF, 0x00, 0x33, 0xCC, 0x00, 0x33, 0x99, 0x00, 0x33, 0x66,
246 // d0
247 	0x00, 0x33, 0x33, 0x00, 0x33, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0xCC,
248 	0x00, 0x00, 0x99, 0x00, 0x00, 0x66, 0x00, 0x00, 0x33, 0xEE, 0x00, 0x00,
249 	0xDD, 0x00, 0x00, 0xBB, 0x00, 0x00, 0xAA, 0x00, 0x00, 0x88, 0x00, 0x00,
250 	0x77, 0x00, 0x00, 0x55, 0x00, 0x00, 0x44, 0x00, 0x00, 0x22, 0x00, 0x00,
251 // e0
252 	0x11, 0x00, 0x00, 0x00, 0xEE, 0x00, 0x00, 0xDD, 0x00, 0x00, 0xBB, 0x00,
253 	0x00, 0xAA, 0x00, 0x00, 0x88, 0x00, 0x00, 0x77, 0x00, 0x00, 0x55, 0x00,
254 	0x00, 0x44, 0x00, 0x00, 0x22, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0xEE,
255 	0x00, 0x00, 0xDD, 0x00, 0x00, 0xBB, 0x00, 0x00, 0xAA, 0x00, 0x00, 0x88,
256 // f0
257 	0x00, 0x00, 0x77, 0x00, 0x00, 0x55, 0x00, 0x00, 0x44, 0x00, 0x00, 0x22,
258 	0x00, 0x00, 0x11, 0xEE, 0xEE, 0xEE, 0xDD, 0xDD, 0xDD, 0xBB, 0xBB, 0xBB,
259 	0xAA, 0xAA, 0xAA, 0x88, 0x88, 0x88, 0x77, 0x77, 0x77, 0x55, 0x55, 0x55,
260 	0x44, 0x44, 0x44, 0x22, 0x22, 0x22, 0x11, 0x11, 0x11, 0x00, 0x00, 0x00
261 };
262