xref: /xnu-8020.101.4/pexpert/pexpert/pexpert.h (revision e7776783b89a353188416a9a346c6cdb4928faad)
1 /*
2  * Copyright (c) 2000-2019 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 #ifndef _PEXPERT_PEXPERT_H_
29 #define _PEXPERT_PEXPERT_H_
30 
31 #include <sys/cdefs.h>
32 
33 #ifdef KERNEL
34 #include <IOKit/IOInterrupts.h>
35 #include <kern/kern_types.h>
36 #endif
37 
38 #if XNU_KERNEL_PRIVATE
39 #include <libkern/kernel_mach_header.h>
40 #endif
41 
42 __BEGIN_DECLS
43 #include <mach/boolean.h>
44 #include <mach/kern_return.h>
45 #include <mach/machine/vm_types.h>
46 
47 #ifdef PEXPERT_KERNEL_PRIVATE
48 #include <pexpert/protos.h>
49 #endif
50 #include <pexpert/boot.h>
51 
52 #if     defined(PEXPERT_KERNEL_PRIVATE) || defined(IOKIT_KERNEL_PRIVATE)
53 typedef void *cpu_id_t;
54 #else
55 typedef void *cpu_id_t;
56 #endif
57 
58 #if XNU_KERNEL_PRIVATE
59 #if defined(__arm__) || defined(__arm64__)
60 extern struct embedded_panic_header *panic_info;
61 extern vm_offset_t gPanicBase;
62 extern unsigned int gPanicSize;
63 
64 /*
65  * If invoked with NULL first argument, return the max buffer size that can
66  * be saved in the second argument
67  */
68 void PE_save_buffer_to_vram(
69 	unsigned char *,
70 	unsigned int *);
71 
72 #else /* defined(__arm__) || defined(__arm64__) */
73 extern struct macos_panic_header *panic_info;
74 #endif /* defined(__arm__) || defined(__arm64__) */
75 #endif /* XNU_KERNEL_PRIVATE */
76 
77 extern void lpss_uart_enable(boolean_t on_off);
78 
79 void PE_enter_debugger(
80 	const char *cause);
81 
82 void PE_init_platform(
83 	boolean_t vm_initialized,
84 	void *args);
85 
86 /*
87  * Copies the requested number of bytes from the "random-seed" property in
88  * the device tree, and zeros the corresponding bytes in the device tree.
89  * Returns the number of bytes actually copied.
90  */
91 uint32_t PE_get_random_seed(
92 	unsigned char * dst_random_seed,
93 	uint32_t request_size);
94 
95 uint32_t PE_i_can_has_debugger(
96 	uint32_t *);
97 
98 int PE_stub_poll_input(unsigned int options, char *c);
99 
100 #if defined(__arm__) || defined(__arm64__)
101 boolean_t PE_panic_debugging_enabled(void);
102 
103 void PE_mark_hwaccess(uint64_t thread);
104 #endif /* defined(__arm__) || defined(__arm64__) */
105 
106 /* Return the offset of the specified address into the panic region */
107 uint32_t PE_get_offset_into_panic_region(
108 	char *location);
109 
110 /* Zeroes the panic header, sets the panic magic and initializes the header to be used */
111 void PE_init_panicheader(
112 	void);
113 
114 /* Updates the panic header during a nested panic */
115 void PE_update_panicheader_nestedpanic(
116 	void);
117 
118 /* Invokes AppleARMIO::handlePlatformError() if present */
119 bool PE_handle_platform_error(
120 	vm_offset_t far);
121 
122 #if KERNEL_PRIVATE
123 
124 /*
125  * Kexts should consult this bitmask to change behavior, since the kernel
126  * may be configured as RELEASE but have MACH_ASSERT enabled, or boot args
127  * may have changed the kernel behavior for statistics and kexts should
128  * participate similarly
129  */
130 
131 #define kPEICanHasAssertions    0x00000001      /* Exceptional conditions should panic() instead of printf() */
132 #define kPEICanHasStatistics    0x00000002      /* Gather expensive statistics (that don't otherwise change behavior */
133 #define kPEICanHasDiagnosticAPI 0x00000004      /* Vend API to userspace or kexts that introspect kernel state */
134 
135 extern uint32_t PE_i_can_has_kernel_configuration(void);
136 
137 #endif /* KERNEL_PRIVATE */
138 
139 extern int32_t gPESerialBaud;
140 
141 extern uint8_t gPlatformECID[8];
142 
143 extern uint32_t gPlatformMemoryID;
144 
145 unsigned int PE_init_taproot(vm_offset_t *taddr);
146 
147 extern void (*PE_kputc)(char c);
148 
149 void PE_init_printf(
150 	boolean_t vm_initialized);
151 
152 extern void (*PE_putc)(char c);
153 
154 /*
155  * Perform pre-lockdown IOKit initialization.
156  * This is guaranteed to execute prior to machine_lockdown().
157  * The precise operations performed by this function depend upon
158  * the security model employed by the platform, but in general this
159  * function should be expected to at least perform basic C++ runtime
160  * and I/O registry initialization.
161  */
162 void PE_init_iokit(
163 	void);
164 
165 /*
166  * Perform post-lockdown IOKit initialization.
167  * This is guaranteed to execute after machine_lockdown().
168  * The precise operations performed by this function depend upon
169  * the security model employed by the platform.  For example, if
170  * the platform treats machine_lockdown() as a strict security
171  * checkpoint, general-purpose IOKit matching may not begin until
172  * this function is called.
173  */
174 void PE_lockdown_iokit(
175 	void);
176 
177 struct clock_frequency_info_t {
178 	unsigned long bus_clock_rate_hz;
179 	unsigned long cpu_clock_rate_hz;
180 	unsigned long dec_clock_rate_hz;
181 	unsigned long bus_clock_rate_num;
182 	unsigned long bus_clock_rate_den;
183 	unsigned long bus_to_cpu_rate_num;
184 	unsigned long bus_to_cpu_rate_den;
185 	unsigned long bus_to_dec_rate_num;
186 	unsigned long bus_to_dec_rate_den;
187 	unsigned long timebase_frequency_hz;
188 	unsigned long timebase_frequency_num;
189 	unsigned long timebase_frequency_den;
190 	unsigned long long bus_frequency_hz;
191 	unsigned long long bus_frequency_min_hz;
192 	unsigned long long bus_frequency_max_hz;
193 	unsigned long long cpu_frequency_hz;
194 	unsigned long long cpu_frequency_min_hz;
195 	unsigned long long cpu_frequency_max_hz;
196 	unsigned long long prf_frequency_hz;
197 	unsigned long long prf_frequency_min_hz;
198 	unsigned long long prf_frequency_max_hz;
199 	unsigned long long mem_frequency_hz;
200 	unsigned long long mem_frequency_min_hz;
201 	unsigned long long mem_frequency_max_hz;
202 	unsigned long long fix_frequency_hz;
203 };
204 
205 extern int debug_cpu_performance_degradation_factor;
206 
207 typedef struct clock_frequency_info_t clock_frequency_info_t;
208 
209 extern clock_frequency_info_t gPEClockFrequencyInfo;
210 
211 struct timebase_freq_t {
212 	unsigned long timebase_num;
213 	unsigned long timebase_den;
214 };
215 
216 typedef void (*timebase_callback_func)(struct timebase_freq_t *timebase_freq);
217 
218 void PE_register_timebase_callback(timebase_callback_func callback);
219 
220 void PE_call_timebase_callback(void);
221 
222 #ifdef KERNEL
223 void PE_install_interrupt_handler(
224 	void *nub, int source,
225 	void *target, IOInterruptHandler handler, void *refCon);
226 #endif
227 
228 extern bool disable_serial_output;
229 extern bool disable_kprintf_output;
230 
231 #ifndef _FN_KPRINTF
232 #define _FN_KPRINTF
233 void kprintf(const char *fmt, ...) __printflike(1, 2);
234 #endif
235 
236 #if KERNEL_PRIVATE
237 void _consume_kprintf_args(int, ...);
238 #endif
239 
240 #if CONFIG_NO_KPRINTF_STRINGS
241 #if KERNEL_PRIVATE
242 #define kprintf(x, ...) _consume_kprintf_args( 0, ## __VA_ARGS__ )
243 #else
244 #define kprintf(x, ...) do {} while (0)
245 #endif
246 #endif
247 
248 void init_display_putc(unsigned char *baseaddr, int rowbytes, int height);
249 void display_putc(char c);
250 
251 enum {
252 	kPEReadTOD,
253 	kPEWriteTOD
254 };
255 
256 enum {
257 	kPEWaitForInput     = 0x00000001,
258 	kPERawInput         = 0x00000002
259 };
260 
261 /* Private Stuff - eventually put in pexpertprivate.h */
262 enum {
263 	kDebugTypeNone    = 0,
264 	kDebugTypeDisplay = 1,
265 	kDebugTypeSerial  = 2
266 };
267 
268 /*  Scale factor values for PE_Video.v_scale */
269 enum {
270 	kPEScaleFactorUnknown = 0,
271 	kPEScaleFactor1x      = 1,
272 	kPEScaleFactor2x      = 2
273 };
274 
275 struct PE_Video {
276 	unsigned long   v_baseAddr;     /* Base address of video memory */
277 	unsigned long   v_rowBytes;     /* Number of bytes per pixel row */
278 	unsigned long   v_width;        /* Width */
279 	unsigned long   v_height;       /* Height */
280 	unsigned long   v_depth;        /* Pixel Depth */
281 	unsigned long   v_display;      /* Text or Graphics */
282 	char            v_pixelFormat[64];
283 	unsigned long   v_offset;       /* offset into video memory to start at */
284 	unsigned long   v_length;       /* length of video memory (0 for v_rowBytes * v_height) */
285 	unsigned char   v_rotate;       /* Rotation: 0:normal, 1:right 90, 2:left 180, 3:left 90 */
286 	unsigned char   v_scale;        /* Scale Factor for both X & Y */
287 	char            reserved1[2];
288 #ifdef __LP64__
289 	long            reserved2;
290 #else
291 	long            v_baseAddrHigh;
292 #endif
293 };
294 
295 typedef struct PE_Video       PE_Video;
296 
297 extern void initialize_screen(PE_Video *, unsigned int);
298 
299 extern void dim_screen(void);
300 
301 extern int PE_current_console(
302 	PE_Video *info);
303 
304 extern void PE_create_console(
305 	void);
306 
307 extern int PE_initialize_console(
308 	PE_Video *newInfo,
309 	int op);
310 
311 #define kPEGraphicsMode         1
312 #define kPETextMode             2
313 #define kPETextScreen           3
314 #define kPEAcquireScreen        4
315 #define kPEReleaseScreen        5
316 #define kPEEnableScreen         6
317 #define kPEDisableScreen        7
318 #define kPEBaseAddressChange    8
319 #define kPERefreshBootGraphics  9
320 
321 extern void PE_display_icon( unsigned int flags,
322     const char * name );
323 
324 typedef struct PE_state {
325 	boolean_t       initialized;
326 	PE_Video        video;
327 	void            *deviceTreeHead;
328 	void            *bootArgs;
329 	vm_size_t       deviceTreeSize;
330 } PE_state_t;
331 
332 extern PE_state_t PE_state;
333 
334 extern char * PE_boot_args(
335 	void);
336 
337 extern boolean_t PE_parse_boot_argn(
338 	const char      *arg_string,
339 	void            *arg_ptr,
340 	int                     max_arg);
341 
342 #if XNU_KERNEL_PRIVATE
343 extern boolean_t PE_parse_boot_arg_str(
344 	const char *arg_string,
345 	char *      arg_ptr,
346 	int         size);
347 #endif /* XNU_KERNEL_PRIVATE */
348 
349 extern boolean_t PE_get_default(
350 	const char      *property_name,
351 	void            *property_ptr,
352 	unsigned int max_property);
353 
354 #define PE_default_value(_key, _variable, _default)     \
355 	do {                                                                                                                      \
356 	        if (!PE_get_default((_key), &(_variable), sizeof(_variable))) \
357 	                _variable = _default;                                                                     \
358 	} while(0)
359 
360 enum {
361 	kPEOptionKey        = 0x3a,
362 	kPECommandKey       = 0x37,
363 	kPEControlKey       = 0x36,
364 	kPEShiftKey         = 0x38
365 };
366 
367 extern boolean_t PE_get_hotkey(
368 	unsigned char   key);
369 
370 extern kern_return_t PE_cpu_start(
371 	cpu_id_t target,
372 	vm_offset_t start_paddr,
373 	vm_offset_t arg_paddr);
374 
375 extern void PE_cpu_halt(
376 	cpu_id_t target);
377 
378 extern bool PE_cpu_down(
379 	cpu_id_t target);
380 
381 extern void PE_cpu_signal(
382 	cpu_id_t source,
383 	cpu_id_t target);
384 
385 extern void PE_cpu_signal_deferred(
386 	cpu_id_t source,
387 	cpu_id_t target);
388 
389 extern void PE_cpu_signal_cancel(
390 	cpu_id_t source,
391 	cpu_id_t target);
392 
393 extern void PE_cpu_machine_init(
394 	cpu_id_t target,
395 	boolean_t bootb);
396 
397 extern void PE_cpu_machine_quiesce(
398 	cpu_id_t target);
399 
400 extern void pe_init_debug(void);
401 
402 extern boolean_t PE_imgsrc_mount_supported(void);
403 
404 extern void PE_panic_hook(const char *str);
405 
406 extern void PE_init_cpu(void);
407 
408 extern void PE_handle_ext_interrupt(void);
409 
410 #if defined(__arm__) || defined(__arm64__)
411 typedef void (*perfmon_interrupt_handler_func)(cpu_id_t source);
412 extern kern_return_t PE_cpu_perfmon_interrupt_install_handler(perfmon_interrupt_handler_func handler);
413 extern void PE_cpu_perfmon_interrupt_enable(cpu_id_t target, boolean_t enable);
414 
415 #if DEVELOPMENT || DEBUG
416 /* panic_trace boot-arg modes */
417 typedef enum {
418 	panic_trace_disabled = 0,       /* Tracing disabled (default) */
419 	panic_trace_unused,             /* Unused for backward compatibility */
420 	panic_trace_enabled,            /* Tracing enabled to SRAM */
421 	panic_trace_alt_enabled,        /* Tracing enabled to L2 */
422 } panic_trace_t;
423 extern panic_trace_t panic_trace;
424 
425 extern void PE_arm_debug_enable_trace(void);
426 extern void (*PE_arm_debug_panic_hook)(const char *str);
427 #else
428 extern void(*const PE_arm_debug_panic_hook)(const char *str);
429 #endif
430 #endif
431 
432 
433 typedef enum kc_kind {
434 	KCKindNone      = -1,
435 	KCKindUnknown   = 0,
436 	KCKindPrimary   = 1,
437 	KCKindPageable  = 2,
438 	KCKindAuxiliary = 3,
439 	KCNumKinds      = 4,
440 } kc_kind_t;
441 
442 typedef enum kc_format {
443 	KCFormatUnknown = 0,
444 	KCFormatStatic  = 1,
445 	KCFormatDynamic = 2,
446 	KCFormatFileset = 3,
447 	KCFormatKCGEN   = 4,
448 } kc_format_t;
449 
450 #if XNU_KERNEL_PRIVATE
451 /* set the mach-o header for a given KC type */
452 extern void PE_set_kc_header(kc_kind_t type, kernel_mach_header_t *header, uintptr_t slide);
453 void PE_reset_kc_header(kc_kind_t type);
454 /* set both lowest VA (base) and mach-o header for a given KC type */
455 extern void PE_set_kc_header_and_base(kc_kind_t type, kernel_mach_header_t *header, void *base, uintptr_t slide);
456 /* The highest non-LINKEDIT virtual address */
457 extern vm_offset_t kc_highest_nonlinkedit_vmaddr;
458 #endif
459 /* returns a pointer to the mach-o header for a give KC type, returns NULL if nothing's been set */
460 extern void *PE_get_kc_header(kc_kind_t type);
461 /* returns a pointer to the lowest VA of of the KC of the given type */
462 extern void *PE_get_kc_baseaddress(kc_kind_t type);
463 /* returns an array of length KCNumKinds of the lowest VAs of each KC type - members could be NULL */
464 extern const void * const*PE_get_kc_base_pointers(void);
465 /* returns the slide for the kext collection */
466 extern uintptr_t PE_get_kc_slide(kc_kind_t type);
467 /* quickly accesss the format of the primary kc */
468 extern bool PE_get_primary_kc_format(kc_format_t *type);
469 /* set vnode ptr for kc fileset */
470 extern void PE_set_kc_vp(kc_kind_t type, void *vp);
471 /* quickly set vnode ptr for kc fileset */
472 void * PE_get_kc_vp(kc_kind_t type);
473 /* drop reference to kc fileset vnodes */
474 void PE_reset_all_kc_vp(void);
475 
476 #if KERNEL_PRIVATE
477 #if defined(__arm64__)
478 extern uint8_t PE_smc_stashed_x86_power_state;
479 extern uint8_t PE_smc_stashed_x86_efi_boot_state;
480 extern uint8_t PE_smc_stashed_x86_system_state;
481 extern uint8_t PE_smc_stashed_x86_shutdown_cause;
482 extern uint64_t PE_smc_stashed_x86_prev_power_transitions;
483 extern uint32_t PE_pcie_stashed_link_state;
484 #endif
485 
486 boolean_t PE_reboot_on_panic(void);
487 void PE_sync_panic_buffers(void);
488 
489 typedef struct PE_panic_save_context {
490 	void *psc_buffer;
491 	uint32_t psc_offset;
492 	uint32_t psc_length;
493 } PE_panic_save_context_t;
494 #endif
495 
496 extern vm_size_t PE_init_socd_client(void);
497 extern void PE_write_socd_client_buffer(vm_offset_t offset, const void *buff, vm_size_t size);
498 
499 __END_DECLS
500 
501 #endif /* _PEXPERT_PEXPERT_H_ */
502