]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/debug.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / osfmk / kern / debug.c
CommitLineData
1c79356b 1/*
cb323159 2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0a7de745 31/*
1c79356b
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
0a7de745 35 *
1c79356b
A
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
0a7de745 41 *
1c79356b
A
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
0a7de745 45 *
1c79356b 46 * Carnegie Mellon requests users of this software to return to
0a7de745 47 *
1c79356b
A
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
0a7de745 52 *
1c79356b
A
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56
57#include <mach_assert.h>
1c79356b 58#include <mach_kdp.h>
5ba3f43e
A
59#include <kdp/kdp.h>
60#include <kdp/kdp_core.h>
61#include <kdp/kdp_internal.h>
62#include <kdp/kdp_callout.h>
1c79356b 63#include <kern/cpu_number.h>
b0d623f7 64#include <kern/kalloc.h>
1c79356b
A
65#include <kern/spl.h>
66#include <kern/thread.h>
67#include <kern/assert.h>
68#include <kern/sched_prim.h>
69#include <kern/misc_protos.h>
c910b4d9 70#include <kern/clock.h>
39236c6e 71#include <kern/telemetry.h>
fe8ab488 72#include <kern/ecc.h>
3e170ce0 73#include <kern/kern_cdata.h>
39037602 74#include <kern/zalloc.h>
9bccf70c 75#include <vm/vm_kern.h>
cb323159 76#include <vm/vm_map.h>
91447636 77#include <vm/pmap.h>
1c79356b 78#include <stdarg.h>
5ba3f43e
A
79#include <stdatomic.h>
80#include <sys/pgo.h>
81#include <console/serial_protos.h>
82
fe8ab488 83#if !(MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
2d21ac55
A
84#include <kdp/kdp_udp.h>
85#endif
5ba3f43e 86#include <kern/processor.h>
1c79356b 87
b0d623f7 88#if defined(__i386__) || defined(__x86_64__)
cb323159
A
89#include <IOKit/IOBSD.h>
90
2d21ac55
A
91#include <i386/cpu_threads.h>
92#include <i386/pmCPU.h>
93#endif
94
95#include <IOKit/IOPlatformExpert.h>
6d2010ae 96#include <machine/pal_routines.h>
2d21ac55 97
b0d623f7
A
98#include <sys/kdebug.h>
99#include <libkern/OSKextLibPrivate.h>
6d2010ae
A
100#include <libkern/OSAtomic.h>
101#include <libkern/kernel_mach_header.h>
cc8bc92a 102#include <libkern/section_keywords.h>
6d2010ae 103#include <uuid/uuid.h>
3e170ce0 104#include <mach_debug/zone_info.h>
b0d623f7 105
39037602
A
106#include <os/log_private.h>
107
5ba3f43e 108#if CONFIG_EMBEDDED
fe8ab488 109#include <pexpert/pexpert.h> /* For gPanicBase */
5ba3f43e
A
110#include <arm/caches_internal.h>
111#include <arm/misc_protos.h>
112extern volatile struct xnu_hw_shmem_dbg_command_info *hwsd_info;
fe8ab488
A
113#endif
114
d9a64523
A
115#if CONFIG_XNUPOST
116#include <tests/xnupost.h>
117extern int vsnprintf(char *, size_t, const char *, va_list);
118#endif
39037602 119
0a7de745
A
120unsigned int halt_in_debugger = 0;
121unsigned int current_debugger = 0;
122unsigned int active_debugger = 0;
123unsigned int panicDebugging = FALSE;
124unsigned int kdebug_serial = FALSE;
125unsigned int kernel_debugger_entry_count = 0;
5ba3f43e 126
0a7de745
A
127#if !defined (__x86_64__)
128struct additional_panic_data_buffer *panic_data_buffers = NULL;
129#endif
5ba3f43e
A
130
131#if defined(__arm__)
132#define TRAP_DEBUGGER __asm__ volatile("trap")
133#elif defined(__arm64__)
134/*
135 * Magic number; this should be identical to the __arm__ encoding for trap.
136 */
137#define TRAP_DEBUGGER __asm__ volatile(".long 0xe7ffdeff")
138#elif defined (__x86_64__)
139#define TRAP_DEBUGGER __asm__("int3")
140#else
141#error No TRAP_DEBUGGER for this architecture
142#endif
143
144#if defined(__i386__) || defined(__x86_64__)
0a7de745 145#define panic_stop() pmCPUHalt(PM_HALT_PANIC)
5ba3f43e 146#else
0a7de745 147#define panic_stop() panic_spin_forever()
5ba3f43e
A
148#endif
149
150#define CPUDEBUGGEROP PROCESSOR_DATA(current_processor(), debugger_state).db_current_op
151#define CPUDEBUGGERMSG PROCESSOR_DATA(current_processor(), debugger_state).db_message
152#define CPUPANICSTR PROCESSOR_DATA(current_processor(), debugger_state).db_panic_str
153#define CPUPANICARGS PROCESSOR_DATA(current_processor(), debugger_state).db_panic_args
154#define CPUPANICOPTS PROCESSOR_DATA(current_processor(), debugger_state).db_panic_options
d9a64523 155#define CPUPANICDATAPTR PROCESSOR_DATA(current_processor(), debugger_state).db_panic_data_ptr
5ba3f43e
A
156#define CPUDEBUGGERSYNC PROCESSOR_DATA(current_processor(), debugger_state).db_proceed_on_sync_failure
157#define CPUDEBUGGERCOUNT PROCESSOR_DATA(current_processor(), debugger_state).db_entry_count
158#define CPUDEBUGGERRET PROCESSOR_DATA(current_processor(), debugger_state).db_op_return
159#define CPUPANICCALLER PROCESSOR_DATA(current_processor(), debugger_state).db_panic_caller
160
161#if DEVELOPMENT || DEBUG
0a7de745
A
162#define DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED(requested) \
163MACRO_BEGIN \
164 if (requested) { \
165 volatile int *badpointer = (int *)4; \
166 *badpointer = 0; \
167 } \
5ba3f43e
A
168MACRO_END
169#endif /* DEVELOPMENT || DEBUG */
170
171debugger_op debugger_current_op = DBOP_NONE;
172const char *debugger_panic_str = NULL;
173va_list *debugger_panic_args = NULL;
d9a64523 174void *debugger_panic_data = NULL;
5ba3f43e
A
175uint64_t debugger_panic_options = 0;
176const char *debugger_message = NULL;
177unsigned long debugger_panic_caller = 0;
178
cb323159
A
179void panic_trap_to_debugger(const char *panic_format_str, va_list *panic_args,
180 unsigned int reason, void *ctx, uint64_t panic_options_mask, void *panic_data,
181 unsigned long panic_caller) __dead2;
182static void kdp_machine_reboot_type(unsigned int type, uint64_t debugger_flags);
183void panic_spin_forever(void) __dead2;
5ba3f43e 184extern kern_return_t do_stackshot(void);
cb323159 185extern void PE_panic_hook(const char*);
1c79356b 186
cb323159 187#if CONFIG_NONFATAL_ASSERTS
1c79356b 188int mach_assert = 1;
cb323159 189#endif
1c79356b 190
5ba3f43e 191#define NESTEDDEBUGGERENTRYMAX 5
cb323159 192static unsigned int max_debugger_entry_count = NESTEDDEBUGGERENTRYMAX;
1c79356b 193
5ba3f43e
A
194#if CONFIG_EMBEDDED
195#define DEBUG_BUF_SIZE (4096)
196#define KDBG_TRACE_PANIC_FILENAME "/var/log/panic.trace"
197#else
5ba3f43e 198#define DEBUG_BUF_SIZE ((3 * PAGE_SIZE) + offsetof(struct macos_panic_header, mph_data))
cb323159 199/* EXTENDED_DEBUG_BUF_SIZE definition is now in debug.h */
5c9f4661 200static_assert(((EXTENDED_DEBUG_BUF_SIZE % PANIC_FLUSH_BOUNDARY) == 0), "Extended debug buf size must match SMC alignment requirements");
5ba3f43e
A
201#define KDBG_TRACE_PANIC_FILENAME "/var/tmp/panic.trace"
202#endif
c910b4d9 203
5ba3f43e
A
204/* debug_buf is directly linked with iBoot panic region for embedded targets */
205#if CONFIG_EMBEDDED
206char *debug_buf_base = NULL;
fe8ab488
A
207char *debug_buf_ptr = NULL;
208unsigned int debug_buf_size = 0;
209#else
c910b4d9 210char debug_buf[DEBUG_BUF_SIZE];
5ba3f43e
A
211struct macos_panic_header *panic_info = (struct macos_panic_header *)debug_buf;
212char *debug_buf_base = (debug_buf + offsetof(struct macos_panic_header, mph_data));
213char *debug_buf_ptr = (debug_buf + offsetof(struct macos_panic_header, mph_data));
214
215/*
216 * We don't include the size of the panic header in the length of the data we actually write.
217 * On co-processor platforms, we lose sizeof(struct macos_panic_header) bytes from the end of
218 * the end of the log because we only support writing (3*PAGESIZE) bytes.
219 */
cc8bc92a
A
220unsigned int debug_buf_size = (DEBUG_BUF_SIZE - offsetof(struct macos_panic_header, mph_data));
221
222boolean_t extended_debug_log_enabled = FALSE;
fe8ab488 223#endif
2d21ac55 224
5ba3f43e
A
225/* Debugger state */
226atomic_int debugger_cpu = ATOMIC_VAR_INIT(DEBUGGER_NO_CPU);
227boolean_t debugger_allcpus_halted = FALSE;
228boolean_t debugger_safe_to_return = TRUE;
229unsigned int debugger_context = 0;
39037602 230
2d21ac55 231static char model_name[64];
39236c6e 232unsigned char *kernel_uuid;
2d21ac55 233
d9a64523
A
234boolean_t kernelcache_uuid_valid = FALSE;
235uuid_t kernelcache_uuid;
236uuid_string_t kernelcache_uuid_string;
237
5ba3f43e
A
238/*
239 * By default we treat Debugger() the same as calls to panic(), unless
240 * we have debug boot-args present and the DB_KERN_DUMP_ON_NMI *NOT* set.
241 * If DB_KERN_DUMP_ON_NMI is *NOT* set, return from Debugger() is supported.
242 *
243 * Return from Debugger() is currently only implemented on x86
244 */
245static boolean_t debugger_is_panic = TRUE;
316670eb 246
cc8bc92a
A
247#if DEVELOPMENT || DEBUG
248boolean_t debug_boot_arg_inited = FALSE;
249#endif
250
251SECURITY_READ_ONLY_LATE(unsigned int) debug_boot_arg;
2d21ac55 252
5ba3f43e 253char kernel_uuid_string[37]; /* uuid_string_t */
d9a64523 254char kernelcache_uuid_string[37]; /* uuid_string_t */
5ba3f43e
A
255char panic_disk_error_description[512];
256size_t panic_disk_error_description_size = sizeof(panic_disk_error_description);
9bccf70c 257
5ba3f43e 258extern unsigned int write_trace_on_panic;
39037602
A
259int kext_assertions_enable =
260#if DEBUG || DEVELOPMENT
0a7de745 261 TRUE;
39037602 262#else
0a7de745 263 FALSE;
39037602
A
264#endif
265
cb323159
A
266/*
267 * Maintain the physically-contiguous carveout for the `phys_carveout_mb`
268 * boot-arg.
269 */
270SECURITY_READ_ONLY_LATE(vm_offset_t) phys_carveout = 0;
271SECURITY_READ_ONLY_LATE(uintptr_t) phys_carveout_pa = 0;
272SECURITY_READ_ONLY_LATE(size_t) phys_carveout_size = 0;
273
1c79356b
A
274void
275panic_init(void)
276{
6d2010ae
A
277 unsigned long uuidlen = 0;
278 void *uuid;
279
280 uuid = getuuidfromheader(&_mh_execute_header, &uuidlen);
281 if ((uuid != NULL) && (uuidlen == sizeof(uuid_t))) {
39236c6e
A
282 kernel_uuid = uuid;
283 uuid_unparse_upper(*(uuid_t *)uuid, kernel_uuid_string);
6d2010ae
A
284 }
285
cb323159 286#if CONFIG_NONFATAL_ASSERTS
3e170ce0
A
287 if (!PE_parse_boot_argn("assertions", &mach_assert, sizeof(mach_assert))) {
288 mach_assert = 1;
289 }
cb323159 290#endif
5ba3f43e 291
cc8bc92a
A
292 /*
293 * Initialize the value of the debug boot-arg
294 */
295 debug_boot_arg = 0;
296#if ((CONFIG_EMBEDDED && MACH_KDP) || defined(__x86_64__))
0a7de745 297 if (PE_parse_boot_argn("debug", &debug_boot_arg, sizeof(debug_boot_arg))) {
cc8bc92a
A
298#if DEVELOPMENT || DEBUG
299 if (debug_boot_arg & DB_HALT) {
0a7de745 300 halt_in_debugger = 1;
cc8bc92a
A
301 }
302#endif
5ba3f43e 303
cc8bc92a
A
304#if CONFIG_EMBEDDED
305 if (debug_boot_arg & DB_NMI) {
306 panicDebugging = TRUE;
307 }
308#else
309 panicDebugging = TRUE;
310#if KDEBUG_MOJO_TRACE
311 if (debug_boot_arg & DB_PRT_KDEBUG) {
312 kdebug_serial = TRUE;
313 }
314#endif
315#endif /* CONFIG_EMBEDDED */
316 }
cb323159
A
317
318 if (!PE_parse_boot_argn("nested_panic_max", &max_debugger_entry_count, sizeof(max_debugger_entry_count))) {
319 max_debugger_entry_count = NESTEDDEBUGGERENTRYMAX;
320 }
321
cc8bc92a
A
322#endif /* ((CONFIG_EMBEDDED && MACH_KDP) || defined(__x86_64__)) */
323
324#if DEVELOPMENT || DEBUG
325 debug_boot_arg_inited = TRUE;
326#endif
327
328#if !CONFIG_EMBEDDED
329 /*
330 * By default we treat Debugger() the same as calls to panic(), unless
331 * we have debug boot-args present and the DB_KERN_DUMP_ON_NMI *NOT* set.
332 * If DB_KERN_DUMP_ON_NMI is *NOT* set, return from Debugger() is supported.
333 * This is because writing an on-device corefile is a destructive operation.
334 *
335 * Return from Debugger() is currently only implemented on x86
336 */
337 if (PE_i_can_has_debugger(NULL) && !(debug_boot_arg & DB_KERN_DUMP_ON_NMI)) {
5ba3f43e
A
338 debugger_is_panic = FALSE;
339 }
340#endif
1c79356b
A
341}
342
cc8bc92a
A
343#if defined (__x86_64__)
344void
345extended_debug_log_init(void)
346{
347 assert(coprocessor_paniclog_flush);
348 /*
349 * Allocate an extended panic log buffer that has space for the panic
350 * stackshot at the end. Update the debug buf pointers appropriately
351 * to point at this new buffer.
352 */
353 char *new_debug_buf = kalloc(EXTENDED_DEBUG_BUF_SIZE);
5c9f4661
A
354 /*
355 * iBoot pre-initializes the panic region with the NULL character. We set this here
356 * so we can accurately calculate the CRC for the region without needing to flush the
357 * full region over SMC.
358 */
359 memset(new_debug_buf, '\0', EXTENDED_DEBUG_BUF_SIZE);
cc8bc92a
A
360
361 panic_info = (struct macos_panic_header *)new_debug_buf;
362 debug_buf_ptr = debug_buf_base = (new_debug_buf + offsetof(struct macos_panic_header, mph_data));
363 debug_buf_size = (EXTENDED_DEBUG_BUF_SIZE - offsetof(struct macos_panic_header, mph_data));
364
365 extended_debug_log_enabled = TRUE;
cb323159
A
366
367 /*
368 * Insert a compiler barrier so we don't free the other panic stackshot buffer
369 * until after we've marked the new one as available
370 */
371 __compiler_barrier();
372 kmem_free(kernel_map, panic_stackshot_buf, panic_stackshot_buf_len);
373 panic_stackshot_buf = 0;
374 panic_stackshot_buf_len = 0;
cc8bc92a
A
375}
376#endif /* defined (__x86_64__) */
377
2d21ac55
A
378void
379debug_log_init(void)
380{
5ba3f43e 381#if CONFIG_EMBEDDED
fe8ab488
A
382 if (!gPanicBase) {
383 printf("debug_log_init: Error!! gPanicBase is still not initialized\n");
384 return;
385 }
5ba3f43e
A
386 /* Shift debug buf start location and size by the length of the panic header */
387 debug_buf_base = (char *)gPanicBase + sizeof(struct embedded_panic_header);
388 debug_buf_ptr = debug_buf_base;
389 debug_buf_size = gPanicSize - sizeof(struct embedded_panic_header);
fe8ab488 390#else
cb323159 391 kern_return_t kr = KERN_SUCCESS;
cc8bc92a
A
392 bzero(panic_info, DEBUG_BUF_SIZE);
393
5ba3f43e
A
394 assert(debug_buf_base != NULL);
395 assert(debug_buf_ptr != NULL);
396 assert(debug_buf_size != 0);
cb323159
A
397
398 /*
399 * We allocate a buffer to store a panic time stackshot. If we later discover that this is a
400 * system that supports flushing a stackshot via an extended debug log (see above), we'll free this memory
401 * as it's not necessary on this platform. This information won't be available until the IOPlatform has come
402 * up.
403 */
404 kr = kmem_alloc(kernel_map, &panic_stackshot_buf, PANIC_STACKSHOT_BUFSIZE, VM_KERN_MEMORY_DIAG);
405 assert(kr == KERN_SUCCESS);
406 if (kr == KERN_SUCCESS) {
407 panic_stackshot_buf_len = PANIC_STACKSHOT_BUFSIZE;
408 }
fe8ab488 409#endif
2d21ac55
A
410}
411
cb323159
A
412void
413phys_carveout_init(void)
414{
415 if (!PE_i_can_has_debugger(NULL)) {
416 return;
417 }
418
419 unsigned int phys_carveout_mb = 0;
420
421 if (!PE_parse_boot_argn("phys_carveout_mb", &phys_carveout_mb,
422 sizeof(phys_carveout_mb))) {
423 return;
424 }
425 if (phys_carveout_mb == 0) {
426 return;
427 }
428
429 size_t size = 0;
430 if (os_mul_overflow(phys_carveout_mb, 1024 * 1024, &size)) {
431 printf("phys_carveout_mb size overflowed (%uMB)\n",
432 phys_carveout_mb);
433 return;
434 }
435
436 kern_return_t kr = kmem_alloc_contig(kernel_map, &phys_carveout, size,
437 VM_MAP_PAGE_MASK(kernel_map), 0, 0, KMA_NOPAGEWAIT,
438 VM_KERN_MEMORY_DIAG);
439 if (kr != KERN_SUCCESS) {
440 printf("failed to allocate %uMB for phys_carveout_mb: %u\n",
441 phys_carveout_mb, (unsigned int)kr);
442 return;
443 }
444
445 phys_carveout_pa = kvtophys(phys_carveout);
446 phys_carveout_size = size;
447}
448
5ba3f43e
A
449static void
450DebuggerLock()
451{
452 int my_cpu = cpu_number();
453 int debugger_exp_cpu = DEBUGGER_NO_CPU;
454 assert(ml_get_interrupts_enabled() == FALSE);
455
cb323159 456 if (atomic_load(&debugger_cpu) == my_cpu) {
5ba3f43e
A
457 return;
458 }
459
0a7de745 460 while (!atomic_compare_exchange_strong(&debugger_cpu, &debugger_exp_cpu, my_cpu)) {
5ba3f43e
A
461 debugger_exp_cpu = DEBUGGER_NO_CPU;
462 }
463
464 return;
465}
466
467static void
468DebuggerUnlock()
469{
cb323159 470 assert(atomic_load_explicit(&debugger_cpu, memory_order_relaxed) == cpu_number());
5ba3f43e
A
471
472 /*
473 * We don't do an atomic exchange here in case
474 * there's another CPU spinning to acquire the debugger_lock
475 * and we never get a chance to update it. We already have the
476 * lock so we can simply store DEBUGGER_NO_CPU and follow with
477 * a barrier.
478 */
cb323159 479 atomic_store(&debugger_cpu, DEBUGGER_NO_CPU);
5ba3f43e
A
480 OSMemoryBarrier();
481
482 return;
483}
484
485static kern_return_t
486DebuggerHaltOtherCores(boolean_t proceed_on_failure)
487{
488#if CONFIG_EMBEDDED
489 return DebuggerXCallEnter(proceed_on_failure);
490#else /* CONFIG_EMBEDDED */
491#pragma unused(proceed_on_failure)
492 mp_kdp_enter(proceed_on_failure);
493 return KERN_SUCCESS;
2d21ac55 494#endif
5ba3f43e
A
495}
496
497static void
498DebuggerResumeOtherCores()
499{
500#if CONFIG_EMBEDDED
501 DebuggerXCallReturn();
502#else /* CONFIG_EMBEDDED */
503 mp_kdp_exit();
504#endif
505}
506
507static void
508DebuggerSaveState(debugger_op db_op, const char *db_message, const char *db_panic_str,
0a7de745
A
509 va_list *db_panic_args, uint64_t db_panic_options, void *db_panic_data_ptr,
510 boolean_t db_proceed_on_sync_failure, unsigned long db_panic_caller)
5ba3f43e
A
511{
512 CPUDEBUGGEROP = db_op;
513
514 /* Preserve the original panic message */
515 if (CPUDEBUGGERCOUNT == 1 || CPUPANICSTR == NULL) {
516 CPUDEBUGGERMSG = db_message;
517 CPUPANICSTR = db_panic_str;
518 CPUPANICARGS = db_panic_args;
d9a64523 519 CPUPANICDATAPTR = db_panic_data_ptr;
5ba3f43e
A
520 CPUPANICCALLER = db_panic_caller;
521 } else if (CPUDEBUGGERCOUNT > 1 && db_panic_str != NULL) {
cc8bc92a 522 kprintf("Nested panic detected:");
0a7de745 523 if (db_panic_str != NULL) {
cc8bc92a 524 _doprnt(db_panic_str, db_panic_args, PE_kputc, 0);
0a7de745 525 }
5ba3f43e
A
526 }
527
528 CPUDEBUGGERSYNC = db_proceed_on_sync_failure;
529 CPUDEBUGGERRET = KERN_SUCCESS;
530
531 /* Reset these on any nested panics */
532 CPUPANICOPTS = db_panic_options;
533
534 return;
535}
2d21ac55 536
b0d623f7 537/*
5ba3f43e
A
538 * Save the requested debugger state/action into the current processor's processor_data
539 * and trap to the debugger.
b0d623f7 540 */
5ba3f43e
A
541kern_return_t
542DebuggerTrapWithState(debugger_op db_op, const char *db_message, const char *db_panic_str,
0a7de745
A
543 va_list *db_panic_args, uint64_t db_panic_options, void *db_panic_data_ptr,
544 boolean_t db_proceed_on_sync_failure, unsigned long db_panic_caller)
5ba3f43e
A
545{
546 kern_return_t ret;
547
548 assert(ml_get_interrupts_enabled() == FALSE);
d9a64523 549 DebuggerSaveState(db_op, db_message, db_panic_str, db_panic_args,
0a7de745
A
550 db_panic_options, db_panic_data_ptr,
551 db_proceed_on_sync_failure, db_panic_caller);
5ba3f43e
A
552
553 TRAP_DEBUGGER;
554
555 ret = CPUDEBUGGERRET;
b0d623f7 556
d9a64523 557 DebuggerSaveState(DBOP_NONE, NULL, NULL, NULL, 0, NULL, FALSE, 0);
5ba3f43e
A
558
559 return ret;
560}
561
562void __attribute__((noinline))
563Assert(
0a7de745
A
564 const char *file,
565 int line,
566 const char *expression
567 )
b0d623f7 568{
cb323159 569#if CONFIG_NONFATAL_ASSERTS
5ba3f43e
A
570 if (!mach_assert) {
571 kprintf("%s:%d non-fatal Assertion: %s", file, line, expression);
572 return;
573 }
cb323159 574#endif
5ba3f43e
A
575
576 panic_plain("%s:%d Assertion failed: %s", file, line, expression);
b0d623f7
A
577}
578
fe8ab488 579
5ba3f43e
A
580void
581Debugger(const char *message)
1c79356b 582{
5ba3f43e
A
583 DebuggerWithContext(0, NULL, message, DEBUGGER_OPTION_NONE);
584}
1c79356b 585
5ba3f43e
A
586void
587DebuggerWithContext(unsigned int reason, void *ctx, const char *message,
0a7de745 588 uint64_t debugger_options_mask)
5ba3f43e
A
589{
590 spl_t previous_interrupts_state;
591 boolean_t old_doprnt_hide_pointers = doprnt_hide_pointers;
7ddcb079 592
5ba3f43e 593 previous_interrupts_state = ml_set_interrupts_enabled(FALSE);
7ddcb079
A
594 disable_preemption();
595
5ba3f43e 596 CPUDEBUGGERCOUNT++;
b0d623f7 597
cb323159 598 if (CPUDEBUGGERCOUNT > max_debugger_entry_count) {
5ba3f43e 599 static boolean_t in_panic_kprintf = FALSE;
2d21ac55 600
5ba3f43e
A
601 /* Notify any listeners that we've started a panic */
602 PEHaltRestart(kPEPanicBegin);
91447636 603
5ba3f43e
A
604 if (!in_panic_kprintf) {
605 in_panic_kprintf = TRUE;
606 kprintf("Detected nested debugger entry count exceeding %d\n",
cb323159 607 max_debugger_entry_count);
5ba3f43e
A
608 in_panic_kprintf = FALSE;
609 }
316670eb 610
5ba3f43e 611 if (!panicDebugging) {
cb323159 612 kdp_machine_reboot_type(kPEPanicRestartCPU, debugger_options_mask);
1c79356b 613 }
5ba3f43e
A
614
615 panic_spin_forever();
1c79356b 616 }
1c79356b 617
5ba3f43e
A
618#if DEVELOPMENT || DEBUG
619 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_options_mask & DEBUGGER_OPTION_RECURPANIC_ENTRY));
620#endif
621
622 doprnt_hide_pointers = FALSE;
623
624 if (ctx != NULL) {
625 DebuggerSaveState(DBOP_DEBUGGER, message,
0a7de745 626 NULL, NULL, debugger_options_mask, NULL, TRUE, 0);
5ba3f43e
A
627 handle_debugger_trap(reason, 0, 0, ctx);
628 DebuggerSaveState(DBOP_NONE, NULL, NULL,
0a7de745 629 NULL, 0, NULL, FALSE, 0);
5ba3f43e
A
630 } else {
631 DebuggerTrapWithState(DBOP_DEBUGGER, message,
0a7de745 632 NULL, NULL, debugger_options_mask, NULL, TRUE, 0);
5ba3f43e 633 }
39037602 634
5ba3f43e
A
635 CPUDEBUGGERCOUNT--;
636 doprnt_hide_pointers = old_doprnt_hide_pointers;
637 enable_preemption();
638 ml_set_interrupts_enabled(previous_interrupts_state);
639}
640
641static struct kdp_callout {
642 struct kdp_callout * callout_next;
643 kdp_callout_fn_t callout_fn;
644 boolean_t callout_in_progress;
645 void * callout_arg;
646} * kdp_callout_list = NULL;
647
648/*
649 * Called from kernel context to register a kdp event callout.
650 */
651void
652kdp_register_callout(kdp_callout_fn_t fn, void * arg)
653{
654 struct kdp_callout * kcp;
655 struct kdp_callout * list_head;
656
657 kcp = kalloc(sizeof(*kcp));
0a7de745 658 if (kcp == NULL) {
5ba3f43e 659 panic("kdp_register_callout() kalloc failed");
0a7de745 660 }
5ba3f43e
A
661
662 kcp->callout_fn = fn;
663 kcp->callout_arg = arg;
664 kcp->callout_in_progress = FALSE;
665
666 /* Lock-less list insertion using compare and exchange. */
667 do {
668 list_head = kdp_callout_list;
669 kcp->callout_next = list_head;
670 } while (!OSCompareAndSwapPtr(list_head, kcp, &kdp_callout_list));
316670eb 671}
1c79356b 672
39037602 673static void
5ba3f43e 674kdp_callouts(kdp_event_t event)
316670eb 675{
0a7de745 676 struct kdp_callout *kcp = kdp_callout_list;
2d21ac55 677
5ba3f43e
A
678 while (kcp) {
679 if (!kcp->callout_in_progress) {
680 kcp->callout_in_progress = TRUE;
681 kcp->callout_fn(kcp->callout_arg, event);
682 kcp->callout_in_progress = FALSE;
683 }
684 kcp = kcp->callout_next;
55e303ae 685 }
1c79356b
A
686}
687
0a7de745
A
688#if !defined (__x86_64__)
689/*
690 * Register an additional buffer with data to include in the panic log
691 *
692 * <rdar://problem/50137705> tracks supporting more than one buffer
693 *
694 * Note that producer_name and buf should never be de-allocated as we reference these during panic.
695 */
696void
697register_additional_panic_data_buffer(const char *producer_name, void *buf, int len)
698{
699 if (panic_data_buffers != NULL) {
700 panic("register_additional_panic_data_buffer called with buffer already registered");
701 }
702
703 if (producer_name == NULL || (strlen(producer_name) == 0)) {
704 panic("register_additional_panic_data_buffer called with invalid producer_name");
705 }
706
707 if (buf == NULL) {
708 panic("register_additional_panic_data_buffer called with invalid buffer pointer");
709 }
710
711 if ((len <= 0) || (len > ADDITIONAL_PANIC_DATA_BUFFER_MAX_LEN)) {
712 panic("register_additional_panic_data_buffer called with invalid length");
713 }
714
715 struct additional_panic_data_buffer *new_panic_data_buffer = kalloc(sizeof(struct additional_panic_data_buffer));
716 new_panic_data_buffer->producer_name = producer_name;
717 new_panic_data_buffer->buf = buf;
718 new_panic_data_buffer->len = len;
719
720 if (!OSCompareAndSwapPtr(NULL, new_panic_data_buffer, &panic_data_buffers)) {
721 panic("register_additional_panic_data_buffer called with buffer already registered");
722 }
723
724 return;
725}
726#endif /* !defined (__x86_64__) */
727
5ba3f43e
A
728/*
729 * An overview of the xnu panic path:
730 *
731 * Several panic wrappers (panic(), panic_with_options(), etc.) all funnel into panic_trap_to_debugger().
732 * panic_trap_to_debugger() sets the panic state in the current processor's processor_data_t prior
733 * to trapping into the debugger. Once we trap to the debugger, we end up in handle_debugger_trap()
734 * which tries to acquire the panic lock by atomically swapping the current CPU number into debugger_cpu.
735 * debugger_cpu acts as a synchronization point, from which the winning CPU can halt the other cores and
736 * continue to debugger_collect_diagnostics() where we write the paniclog, corefile (if appropriate) and proceed
737 * according to the device's boot-args.
738 */
739#undef panic
316670eb
A
740void
741panic(const char *str, ...)
742{
5ba3f43e
A
743 va_list panic_str_args;
744
745 va_start(panic_str_args, str);
d9a64523 746 panic_trap_to_debugger(str, &panic_str_args, 0, NULL, 0, NULL, (unsigned long)(char *)__builtin_return_address(0));
5ba3f43e
A
747 va_end(panic_str_args);
748}
749
750void
751panic_with_options(unsigned int reason, void *ctx, uint64_t debugger_options_mask, const char *str, ...)
752{
753 va_list panic_str_args;
754
755 va_start(panic_str_args, str);
d9a64523 756 panic_trap_to_debugger(str, &panic_str_args, reason, ctx, (debugger_options_mask & ~DEBUGGER_INTERNAL_OPTIONS_MASK),
0a7de745 757 NULL, (unsigned long)(char *)__builtin_return_address(0));
5ba3f43e
A
758 va_end(panic_str_args);
759}
760
d9a64523
A
761#if defined (__x86_64__)
762/*
763 * panic_with_thread_context() is used on x86 platforms to specify a different thread that should be backtraced in the paniclog.
764 * We don't generally need this functionality on embedded platforms because embedded platforms include a panic time stackshot
765 * from customer devices. We plumb the thread pointer via the debugger trap mechanism and backtrace the kernel stack from the
766 * thread when writing the panic log.
767 *
768 * NOTE: panic_with_thread_context() should be called with an explicit thread reference held on the passed thread.
769 */
5ba3f43e 770void
d9a64523 771panic_with_thread_context(unsigned int reason, void *ctx, uint64_t debugger_options_mask, thread_t thread, const char *str, ...)
5ba3f43e
A
772{
773 va_list panic_str_args;
cb323159 774 __assert_only os_ref_count_t th_ref_count;
d9a64523
A
775
776 assert_thread_magic(thread);
cb323159
A
777 th_ref_count = os_ref_get_count(&thread->ref_count);
778 assertf(th_ref_count > 0, "panic_with_thread_context called with invalid thread %p with refcount %u", thread, th_ref_count);
d9a64523
A
779
780 /* Take a reference on the thread so it doesn't disappear by the time we try to backtrace it */
781 thread_reference(thread);
5ba3f43e
A
782
783 va_start(panic_str_args, str);
d9a64523 784 panic_trap_to_debugger(str, &panic_str_args, reason, ctx, ((debugger_options_mask & ~DEBUGGER_INTERNAL_OPTIONS_MASK) | DEBUGGER_INTERNAL_OPTION_THREAD_BACKTRACE),
0a7de745 785 thread, (unsigned long)(char *)__builtin_return_address(0));
d9a64523 786
5ba3f43e
A
787 va_end(panic_str_args);
788}
d9a64523 789#endif /* defined (__x86_64__) */
5ba3f43e
A
790
791#pragma clang diagnostic push
792#pragma clang diagnostic ignored "-Wmissing-noreturn"
793void
d9a64523 794panic_trap_to_debugger(const char *panic_format_str, va_list *panic_args, unsigned int reason, void *ctx,
0a7de745 795 uint64_t panic_options_mask, void *panic_data_ptr, unsigned long panic_caller)
5ba3f43e
A
796{
797#pragma clang diagnostic pop
798
0a7de745
A
799#if defined(__x86_64__) && (DEVELOPMENT || DEBUG)
800 /* Turn off I/O tracing once we've panicked */
801 mmiotrace_enabled = 0;
802#endif
803
5ba3f43e
A
804 if (ml_wants_panic_trap_to_debugger()) {
805 ml_panic_trap_to_debugger(panic_format_str, panic_args, reason, ctx, panic_options_mask, panic_caller);
cb323159 806 __builtin_trap();
5ba3f43e
A
807 }
808
809 CPUDEBUGGERCOUNT++;
810
cb323159 811 if (CPUDEBUGGERCOUNT > max_debugger_entry_count) {
5ba3f43e
A
812 static boolean_t in_panic_kprintf = FALSE;
813
814 /* Notify any listeners that we've started a panic */
815 PEHaltRestart(kPEPanicBegin);
816
817 if (!in_panic_kprintf) {
818 in_panic_kprintf = TRUE;
819 kprintf("Detected nested debugger entry count exceeding %d\n",
cb323159 820 max_debugger_entry_count);
5ba3f43e
A
821 in_panic_kprintf = FALSE;
822 }
823
824 if (!panicDebugging) {
cb323159 825 kdp_machine_reboot_type(kPEPanicRestartCPU, panic_options_mask);
5ba3f43e
A
826 }
827
828 panic_spin_forever();
829 }
830
831#if DEVELOPMENT || DEBUG
832 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((panic_options_mask & DEBUGGER_OPTION_RECURPANIC_ENTRY));
833#endif
834
cb323159 835 PE_panic_hook(panic_format_str);
316670eb 836
39037602
A
837#if defined (__x86_64__)
838 plctrace_disable();
839#endif
39037602 840
5ba3f43e
A
841 if (write_trace_on_panic && kdebug_enable) {
842 if (get_preemption_level() == 0 && !ml_at_interrupt_context()) {
843 ml_set_interrupts_enabled(TRUE);
a39ff7e2 844 KDBG_RELEASE(TRACE_PANIC);
5ba3f43e
A
845 kdbg_dump_trace_to_file(KDBG_TRACE_PANIC_FILENAME);
846 }
847 }
848
849 ml_set_interrupts_enabled(FALSE);
850 disable_preemption();
39037602 851
5ba3f43e
A
852#if defined (__x86_64__)
853 pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE);
854#endif /* defined (__x86_64__) */
3e170ce0
A
855
856 /* Never hide pointers from panic logs. */
857 doprnt_hide_pointers = FALSE;
858
5ba3f43e
A
859 if (ctx != NULL) {
860 /*
861 * We called into panic from a trap, no need to trap again. Set the
862 * state on the current CPU and then jump to handle_debugger_trap.
863 */
864 DebuggerSaveState(DBOP_PANIC, "panic",
0a7de745
A
865 panic_format_str, panic_args,
866 panic_options_mask, panic_data_ptr, TRUE, panic_caller);
5ba3f43e 867 handle_debugger_trap(reason, 0, 0, ctx);
316670eb 868 }
316670eb 869
5ba3f43e 870#if defined(__arm64__)
316670eb 871 /*
5ba3f43e 872 * Signal to fastsim that it should open debug ports (nop on hardware)
316670eb 873 */
0a7de745 874 __asm__ volatile ("HINT 0x45");
5ba3f43e 875#endif /* defined(__arm64__) */
3e170ce0 876
5ba3f43e 877 DebuggerTrapWithState(DBOP_PANIC, "panic", panic_format_str,
0a7de745 878 panic_args, panic_options_mask, panic_data_ptr, TRUE, panic_caller);
3e170ce0 879
5ba3f43e
A
880 /*
881 * Not reached.
882 */
883 panic_stop();
cb323159 884 __builtin_unreachable();
316670eb
A
885}
886
39037602 887void
cb323159 888panic_spin_forever(void)
39037602 889{
5ba3f43e
A
890 paniclog_append_noflush("\nPlease go to https://panic.apple.com to report this panic\n");
891
0a7de745
A
892 for (;;) {
893 }
5ba3f43e
A
894}
895
896static void
cb323159 897kdp_machine_reboot_type(unsigned int type, uint64_t debugger_flags)
5ba3f43e
A
898{
899 printf("Attempting system restart...");
cb323159
A
900 if ((type == kPEPanicRestartCPU) && (debugger_flags & DEBUGGER_OPTION_SKIP_PANICEND_CALLOUTS)) {
901 PEHaltRestart(kPEPanicRestartCPUNoPanicEndCallouts);
902 } else {
903 PEHaltRestart(type);
904 }
5ba3f43e
A
905 halt_all_cpus(TRUE);
906}
907
908void
909kdp_machine_reboot(void)
910{
cb323159 911 kdp_machine_reboot_type(kPEPanicRestartCPU, 0);
5ba3f43e
A
912}
913
914/*
915 * Gather and save diagnostic information about a panic (or Debugger call).
916 *
917 * On embedded, Debugger and Panic are treated very similarly -- WDT uses Debugger so we can
918 * theoretically return from it. On desktop, Debugger is treated as a conventional debugger -- i.e no
919 * paniclog is written and no core is written unless we request a core on NMI.
920 *
921 * This routine handles kicking off local coredumps, paniclogs, calling into the Debugger/KDP (if it's configured),
922 * and calling out to any other functions we have for collecting diagnostic info.
923 */
924static void
925debugger_collect_diagnostics(unsigned int exception, unsigned int code, unsigned int subcode, void *state)
926{
927#if DEVELOPMENT || DEBUG
928 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_panic_options & DEBUGGER_OPTION_RECURPANIC_PRELOG));
929#endif
930
931#if defined(__x86_64__)
cc8bc92a 932 kprintf("Debugger called: <%s>\n", debugger_message ? debugger_message : "");
5ba3f43e
A
933#endif
934 /*
935 * DB_HALT (halt_in_debugger) can be requested on startup, we shouldn't generate
936 * a coredump/paniclog for this type of debugger entry. If KDP isn't configured,
937 * we'll just spin in kdp_raise_exception.
938 */
939 if (debugger_current_op == DBOP_DEBUGGER && halt_in_debugger) {
940 kdp_raise_exception(exception, code, subcode, state);
941 if (debugger_safe_to_return && !debugger_is_panic) {
942 return;
943 }
944 }
945
946 if ((debugger_current_op == DBOP_PANIC) ||
0a7de745 947 ((debugger_current_op == DBOP_DEBUGGER) && debugger_is_panic)) {
5ba3f43e
A
948 /*
949 * Attempt to notify listeners once and only once that we've started
950 * panicking. Only do this for Debugger() calls if we're treating
951 * Debugger() calls like panic().
952 */
953 PEHaltRestart(kPEPanicBegin);
954
955 /*
956 * Set the begin pointer in the panic log structure. We key off of this
957 * static variable rather than contents from the panic header itself in case someone
958 * has stomped over the panic_info structure. Also initializes the header magic.
959 */
960 static boolean_t began_writing_paniclog = FALSE;
961 if (!began_writing_paniclog) {
962 PE_init_panicheader();
963 began_writing_paniclog = TRUE;
964 } else {
965 /*
966 * If we reached here, update the panic header to keep it as consistent
967 * as possible during a nested panic
968 */
969 PE_update_panicheader_nestedpanic();
970 }
971 }
972
973 /*
974 * Write panic string if this was a panic.
975 *
976 * TODO: Consider moving to SavePanicInfo as this is part of the panic log.
977 */
978 if (debugger_current_op == DBOP_PANIC) {
979 paniclog_append_noflush("panic(cpu %d caller 0x%lx): ", (unsigned) cpu_number(), debugger_panic_caller);
980 if (debugger_panic_str) {
981 _doprnt(debugger_panic_str, debugger_panic_args, consdebug_putc, 0);
982 }
983 paniclog_append_noflush("\n");
984 }
cc8bc92a
A
985#if defined(__x86_64__)
986 else if (((debugger_current_op == DBOP_DEBUGGER) && debugger_is_panic)) {
987 paniclog_append_noflush("Debugger called: <%s>\n", debugger_message ? debugger_message : "");
988 }
5ba3f43e
A
989
990 /*
991 * Debugger() is treated like panic() on embedded -- for example we use it for WDT
992 * panics (so we need to write a paniclog). On desktop Debugger() is used in the
993 * conventional sense.
994 */
cc8bc92a 995 if (debugger_current_op == DBOP_PANIC || ((debugger_current_op == DBOP_DEBUGGER) && debugger_is_panic))
5ba3f43e
A
996#endif
997 {
998 kdp_callouts(KDP_EVENT_PANICLOG);
39037602 999
5ba3f43e
A
1000 /*
1001 * Write paniclog and panic stackshot (if supported)
1002 * TODO: Need to clear panic log when return from debugger
1003 * hooked up for embedded
1004 */
d9a64523 1005 SavePanicInfo(debugger_message, debugger_panic_data, debugger_panic_options);
39037602 1006
5ba3f43e
A
1007#if DEVELOPMENT || DEBUG
1008 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_panic_options & DEBUGGER_OPTION_RECURPANIC_POSTLOG));
1009#endif
39037602 1010
5ba3f43e 1011 /* DEBUGGER_OPTION_PANICLOGANDREBOOT is used for two finger resets on embedded so we get a paniclog */
0a7de745 1012 if (debugger_panic_options & DEBUGGER_OPTION_PANICLOGANDREBOOT) {
cb323159 1013 PEHaltRestart(kPEPanicRestartCPUNoCallouts);
0a7de745 1014 }
39037602 1015 }
5ba3f43e
A
1016
1017#if CONFIG_KDP_INTERACTIVE_DEBUGGING
39037602 1018 /*
5ba3f43e
A
1019 * If reboot on panic is enabled and the caller of panic indicated that we should skip
1020 * local coredumps, don't try to write these and instead go straight to reboot. This
1021 * allows us to persist any data that's stored in the panic log.
39037602 1022 */
5ba3f43e 1023 if ((debugger_panic_options & DEBUGGER_OPTION_SKIP_LOCAL_COREDUMP) &&
0a7de745 1024 (debug_boot_arg & DB_REBOOT_POST_CORE)) {
cb323159 1025 kdp_machine_reboot_type(kPEPanicRestartCPU, debugger_panic_options);
5ba3f43e
A
1026 }
1027
1028 /*
1029 * Consider generating a local corefile if the infrastructure is configured
1030 * and we haven't disabled on-device coredumps.
1031 */
cb323159 1032 if (on_device_corefile_enabled()) {
d9a64523
A
1033 if (!kdp_has_polled_corefile()) {
1034 if (debug_boot_arg & (DB_KERN_DUMP_ON_PANIC | DB_KERN_DUMP_ON_NMI)) {
1035 paniclog_append_noflush("skipping local kernel core because core file could not be opened prior to panic (error : 0x%x)",
0a7de745 1036 kdp_polled_corefile_error());
d9a64523
A
1037#if CONFIG_EMBEDDED
1038 panic_info->eph_panic_flags |= EMBEDDED_PANIC_HEADER_FLAG_COREDUMP_FAILED;
1039 paniclog_flush();
1040#else /* CONFIG_EMBEDDED */
1041 if (panic_info->mph_panic_log_offset != 0) {
1042 panic_info->mph_panic_flags |= MACOS_PANIC_HEADER_FLAG_COREDUMP_FAILED;
1043 paniclog_flush();
1044 }
1045#endif /* CONFIG_EMBEDDED */
1046 }
1047 } else {
1048 int ret = -1;
5ba3f43e
A
1049
1050#if defined (__x86_64__)
d9a64523
A
1051 /* On x86 we don't do a coredump on Debugger unless the DB_KERN_DUMP_ON_NMI boot-arg is specified. */
1052 if (debugger_current_op != DBOP_DEBUGGER || (debug_boot_arg & DB_KERN_DUMP_ON_NMI))
5ba3f43e 1053#endif
d9a64523
A
1054 {
1055 /*
1056 * Doing an on-device coredump leaves the disk driver in a state
1057 * that can not be resumed.
1058 */
1059 debugger_safe_to_return = FALSE;
1060 begin_panic_transfer();
1061 ret = kern_dump(KERN_DUMP_DISK);
1062 abort_panic_transfer();
5ba3f43e
A
1063
1064#if DEVELOPMENT || DEBUG
d9a64523 1065 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_panic_options & DEBUGGER_OPTION_RECURPANIC_POSTCORE));
5ba3f43e 1066#endif
d9a64523 1067 }
5ba3f43e 1068
d9a64523
A
1069 /*
1070 * If DB_REBOOT_POST_CORE is set, then reboot if coredump is sucessfully saved
1071 * or if option to ignore failures is set.
1072 */
1073 if ((debug_boot_arg & DB_REBOOT_POST_CORE) &&
0a7de745 1074 ((ret == 0) || (debugger_panic_options & DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT))) {
cb323159 1075 kdp_machine_reboot_type(kPEPanicRestartCPU, debugger_panic_options);
d9a64523 1076 }
cc8bc92a 1077 }
5ba3f43e
A
1078 }
1079
5c9f4661 1080 if (debug_boot_arg & DB_REBOOT_ALWAYS) {
cb323159 1081 kdp_machine_reboot_type(kPEPanicRestartCPU, debugger_panic_options);
5c9f4661
A
1082 }
1083
5ba3f43e
A
1084 /* If KDP is configured, try to trap to the debugger */
1085 if (current_debugger != NO_CUR_DB) {
1086 kdp_raise_exception(exception, code, subcode, state);
1087 /*
1088 * Only return if we entered via Debugger and it's safe to return
1089 * (we halted the other cores successfully, this isn't a nested panic, etc)
0a7de745 1090 */
5ba3f43e 1091 if (debugger_current_op == DBOP_DEBUGGER &&
0a7de745
A
1092 debugger_safe_to_return &&
1093 kernel_debugger_entry_count == 1 &&
1094 !debugger_is_panic) {
5ba3f43e
A
1095 return;
1096 }
1097 }
1098
1099#if CONFIG_EMBEDDED
1100 if (panicDebugging) {
1101 /* If panic debugging is configured, spin for astris to connect */
1102 panic_spin_shmcon();
1103 }
1104#endif /* CONFIG_EMBEDDED */
1105#endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
1106
1107 if (!panicDebugging) {
cb323159 1108 kdp_machine_reboot_type(kPEPanicRestartCPU, debugger_panic_options);
5ba3f43e
A
1109 }
1110
1111 panic_spin_forever();
39037602
A
1112}
1113
5ba3f43e
A
1114#if INTERRUPT_MASKED_DEBUG
1115uint64_t debugger_trap_timestamps[9];
1116# define DEBUGGER_TRAP_TIMESTAMP(i) debugger_trap_timestamps[i] = mach_absolute_time();
1117#else
1118# define DEBUGGER_TRAP_TIMESTAMP(i)
39037602 1119#endif
5ba3f43e 1120
316670eb 1121void
5ba3f43e 1122handle_debugger_trap(unsigned int exception, unsigned int code, unsigned int subcode, void *state)
316670eb 1123{
5ba3f43e
A
1124 unsigned int initial_not_in_kdp = not_in_kdp;
1125 kern_return_t ret;
1126 debugger_op db_prev_op = debugger_current_op;
1127
1128 DEBUGGER_TRAP_TIMESTAMP(0);
1129
1130 DebuggerLock();
1131 ret = DebuggerHaltOtherCores(CPUDEBUGGERSYNC);
316670eb 1132
5ba3f43e 1133 DEBUGGER_TRAP_TIMESTAMP(1);
39236c6e 1134
5ba3f43e
A
1135#if INTERRUPT_MASKED_DEBUG
1136 if (serialmode & SERIALMODE_OUTPUT) {
1137 ml_spin_debug_reset(current_thread());
1138 }
1139#endif
1140 if (ret != KERN_SUCCESS) {
1141 CPUDEBUGGERRET = ret;
1142 DebuggerUnlock();
1143 return;
1144 }
1145
1146 /* Update the global panic/debugger nested entry level */
1147 kernel_debugger_entry_count = CPUDEBUGGERCOUNT;
1148
1149 /*
1150 * TODO: Should we do anything special for nested panics here? i.e. if we've trapped more than twice
1151 * should we call into the debugger if it's configured and then reboot if the panic log has been written?
1152 */
1153
1154 if (CPUDEBUGGEROP == DBOP_NONE) {
1155 /* If there was no debugger context setup, we trapped due to a software breakpoint */
1156 debugger_current_op = DBOP_BREAKPOINT;
1157 } else {
1158 /* Not safe to return from a nested panic/debugger call */
1159 if (debugger_current_op == DBOP_PANIC ||
0a7de745 1160 debugger_current_op == DBOP_DEBUGGER) {
5ba3f43e
A
1161 debugger_safe_to_return = FALSE;
1162 }
1163
1164 debugger_current_op = CPUDEBUGGEROP;
1165
1166 /* Only overwrite the panic message if there is none already - save the data from the first call */
1167 if (debugger_panic_str == NULL) {
1168 debugger_panic_str = CPUPANICSTR;
1169 debugger_panic_args = CPUPANICARGS;
d9a64523 1170 debugger_panic_data = CPUPANICDATAPTR;
5ba3f43e
A
1171 debugger_message = CPUDEBUGGERMSG;
1172 debugger_panic_caller = CPUPANICCALLER;
1173 }
1174
1175 debugger_panic_options = CPUPANICOPTS;
316670eb 1176 }
316670eb
A
1177
1178 /*
5ba3f43e
A
1179 * Clear the op from the processor debugger context so we can handle
1180 * breakpoints in the debugger
316670eb 1181 */
5ba3f43e
A
1182 CPUDEBUGGEROP = DBOP_NONE;
1183
1184 DEBUGGER_TRAP_TIMESTAMP(2);
1185
1186 kdp_callouts(KDP_EVENT_ENTER);
1187 not_in_kdp = 0;
1188
1189 DEBUGGER_TRAP_TIMESTAMP(3);
1190
1191 if (debugger_current_op == DBOP_BREAKPOINT) {
1192 kdp_raise_exception(exception, code, subcode, state);
1193 } else if (debugger_current_op == DBOP_STACKSHOT) {
1194 CPUDEBUGGERRET = do_stackshot();
1195#if PGO
0a7de745 1196 } else if (debugger_current_op == DBOP_RESET_PGO_COUNTERS) {
5ba3f43e
A
1197 CPUDEBUGGERRET = do_pgo_reset_counters();
1198#endif
1199 } else {
1200 debugger_collect_diagnostics(exception, code, subcode, state);
1201 }
1202
1203 DEBUGGER_TRAP_TIMESTAMP(4);
1204
1205 not_in_kdp = initial_not_in_kdp;
1206 kdp_callouts(KDP_EVENT_EXIT);
1207
1208 DEBUGGER_TRAP_TIMESTAMP(5);
1209
1210 if (debugger_current_op != DBOP_BREAKPOINT) {
1211 debugger_panic_str = NULL;
1212 debugger_panic_args = NULL;
d9a64523 1213 debugger_panic_data = NULL;
5ba3f43e
A
1214 debugger_panic_options = 0;
1215 debugger_message = NULL;
1216 }
1217
1218 /* Restore the previous debugger state */
1219 debugger_current_op = db_prev_op;
1220
1221 DEBUGGER_TRAP_TIMESTAMP(6);
1222
1223 DebuggerResumeOtherCores();
1224
1225 DEBUGGER_TRAP_TIMESTAMP(7);
1226
1227 DebuggerUnlock();
1228
1229 DEBUGGER_TRAP_TIMESTAMP(8);
1230
1231 return;
316670eb
A
1232}
1233
0a7de745
A
1234__attribute__((noinline, not_tail_called))
1235void
1236log(__unused int level, char *fmt, ...)
1c79356b 1237{
39037602 1238 void *caller = __builtin_return_address(0);
0a7de745
A
1239 va_list listp;
1240 va_list listp2;
39037602 1241
1c79356b
A
1242
1243#ifdef lint
1244 level++;
1245#endif /* lint */
0a7de745 1246#ifdef MACH_BSD
1c79356b 1247 va_start(listp, fmt);
39037602
A
1248 va_copy(listp2, listp);
1249
1250 disable_preemption();
1251 _doprnt(fmt, &listp, cons_putc_locked, 0);
1c79356b 1252 enable_preemption();
39037602
A
1253
1254 va_end(listp);
1255
1256 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller);
1257 va_end(listp2);
1c79356b
A
1258#endif
1259}
9bccf70c 1260
39037602 1261/*
5ba3f43e
A
1262 * Per <rdar://problem/24974766>, skip appending log messages to
1263 * the new logging infrastructure in contexts where safety is
1264 * uncertain. These contexts include:
39037602
A
1265 * - When we're in the debugger
1266 * - We're in a panic
1267 * - Interrupts are disabled
1268 * - Or Pre-emption is disabled
1269 * In all the above cases, it is potentially unsafe to log messages.
1270 */
1271
5ba3f43e 1272boolean_t
0a7de745
A
1273oslog_is_safe(void)
1274{
1275 return kernel_debugger_entry_count == 0 &&
1276 not_in_kdp == 1 &&
1277 get_preemption_level() == 0 &&
1278 ml_get_interrupts_enabled() == TRUE;
39037602
A
1279}
1280
5ba3f43e
A
1281boolean_t
1282debug_mode_active(void)
1283{
0a7de745 1284 return (0 != kernel_debugger_entry_count != 0) || (0 == not_in_kdp);
5ba3f43e
A
1285}
1286
9bccf70c
A
1287void
1288debug_putc(char c)
1289{
2d21ac55 1290 if ((debug_buf_size != 0) &&
0a7de745
A
1291 ((debug_buf_ptr - debug_buf_base) < (int)debug_buf_size)) {
1292 *debug_buf_ptr = c;
9bccf70c
A
1293 debug_buf_ptr++;
1294 }
1295}
2d21ac55 1296
5ba3f43e
A
1297#if defined (__x86_64__)
1298struct pasc {
0a7de745
A
1299 unsigned a: 7;
1300 unsigned b: 7;
1301 unsigned c: 7;
1302 unsigned d: 7;
1303 unsigned e: 7;
1304 unsigned f: 7;
1305 unsigned g: 7;
1306 unsigned h: 7;
5ba3f43e 1307} __attribute__((packed));
2d21ac55 1308
5ba3f43e
A
1309typedef struct pasc pasc_t;
1310
1311/*
1312 * In-place packing routines -- inefficient, but they're called at most once.
1313 * Assumes "buflen" is a multiple of 8. Used for compressing paniclogs on x86.
1314 */
1315int
1316packA(char *inbuf, uint32_t length, uint32_t buflen)
2d21ac55 1317{
0a7de745
A
1318 unsigned int i, j = 0;
1319 pasc_t pack;
1320
1321 length = MIN(((length + 7) & ~7), buflen);
1322
1323 for (i = 0; i < length; i += 8) {
1324 pack.a = inbuf[i];
1325 pack.b = inbuf[i + 1];
1326 pack.c = inbuf[i + 2];
1327 pack.d = inbuf[i + 3];
1328 pack.e = inbuf[i + 4];
1329 pack.f = inbuf[i + 5];
1330 pack.g = inbuf[i + 6];
1331 pack.h = inbuf[i + 7];
1332 bcopy((char *) &pack, inbuf + j, 7);
1333 j += 7;
1334 }
1335 return j;
2d21ac55
A
1336}
1337
5ba3f43e
A
1338void
1339unpackA(char *inbuf, uint32_t length)
2d21ac55
A
1340{
1341 pasc_t packs;
1342 unsigned i = 0;
0a7de745 1343 length = (length * 8) / 7;
2d21ac55
A
1344
1345 while (i < length) {
0a7de745
A
1346 packs = *(pasc_t *)&inbuf[i];
1347 bcopy(&inbuf[i + 7], &inbuf[i + 8], MAX(0, (int) (length - i - 8)));
1348 inbuf[i++] = packs.a;
1349 inbuf[i++] = packs.b;
1350 inbuf[i++] = packs.c;
1351 inbuf[i++] = packs.d;
1352 inbuf[i++] = packs.e;
1353 inbuf[i++] = packs.f;
1354 inbuf[i++] = packs.g;
1355 inbuf[i++] = packs.h;
2d21ac55
A
1356 }
1357}
5ba3f43e 1358#endif /* defined (__x86_64__) */
2d21ac55
A
1359
1360extern void *proc_name_address(void *p);
1361
1362static void
0a7de745
A
1363panic_display_process_name(void)
1364{
3e170ce0
A
1365 /* because of scoping issues len(p_comm) from proc_t is hard coded here */
1366 char proc_name[17] = "Unknown";
2d21ac55
A
1367 task_t ctask = 0;
1368 void *cbsd_info = 0;
1369
0a7de745
A
1370 if (ml_nofault_copy((vm_offset_t)&current_thread()->task, (vm_offset_t) &ctask, sizeof(task_t)) == sizeof(task_t)) {
1371 if (ml_nofault_copy((vm_offset_t)&ctask->bsd_info, (vm_offset_t)&cbsd_info, sizeof(cbsd_info)) == sizeof(cbsd_info)) {
1372 if (cbsd_info && (ml_nofault_copy((vm_offset_t) proc_name_address(cbsd_info), (vm_offset_t) &proc_name, sizeof(proc_name)) > 0)) {
2d21ac55 1373 proc_name[sizeof(proc_name) - 1] = '\0';
0a7de745
A
1374 }
1375 }
1376 }
5ba3f43e 1377 paniclog_append_noflush("\nBSD process name corresponding to current thread: %s\n", proc_name);
2d21ac55
A
1378}
1379
5ba3f43e 1380unsigned
0a7de745
A
1381panic_active(void)
1382{
1383 return debugger_panic_str != (char *) 0;
2d21ac55
A
1384}
1385
5ba3f43e 1386void
0a7de745
A
1387populate_model_name(char *model_string)
1388{
2d21ac55
A
1389 strlcpy(model_name, model_string, sizeof(model_name));
1390}
1391
5ba3f43e 1392void
0a7de745
A
1393panic_display_model_name(void)
1394{
2d21ac55
A
1395 char tmp_model_name[sizeof(model_name)];
1396
0a7de745 1397 if (ml_nofault_copy((vm_offset_t) &model_name, (vm_offset_t) &tmp_model_name, sizeof(model_name)) != sizeof(model_name)) {
2d21ac55 1398 return;
0a7de745 1399 }
2d21ac55 1400
6d2010ae
A
1401 tmp_model_name[sizeof(tmp_model_name) - 1] = '\0';
1402
0a7de745 1403 if (tmp_model_name[0] != 0) {
5ba3f43e 1404 paniclog_append_noflush("System model name: %s\n", tmp_model_name);
0a7de745 1405 }
6d2010ae
A
1406}
1407
5ba3f43e 1408void
0a7de745
A
1409panic_display_kernel_uuid(void)
1410{
39236c6e 1411 char tmp_kernel_uuid[sizeof(kernel_uuid_string)];
6d2010ae 1412
0a7de745 1413 if (ml_nofault_copy((vm_offset_t) &kernel_uuid_string, (vm_offset_t) &tmp_kernel_uuid, sizeof(kernel_uuid_string)) != sizeof(kernel_uuid_string)) {
6d2010ae 1414 return;
0a7de745 1415 }
2d21ac55 1416
0a7de745 1417 if (tmp_kernel_uuid[0] != '\0') {
5ba3f43e 1418 paniclog_append_noflush("Kernel UUID: %s\n", tmp_kernel_uuid);
0a7de745 1419 }
2d21ac55
A
1420}
1421
5ba3f43e 1422void
0a7de745
A
1423panic_display_kernel_aslr(void)
1424{
316670eb 1425 if (vm_kernel_slide) {
5ba3f43e
A
1426 paniclog_append_noflush("Kernel slide: 0x%016lx\n", (unsigned long) vm_kernel_slide);
1427 paniclog_append_noflush("Kernel text base: %p\n", (void *) vm_kernel_stext);
316670eb 1428 }
316670eb
A
1429}
1430
5ba3f43e 1431void
0a7de745
A
1432panic_display_hibb(void)
1433{
fe8ab488 1434#if defined(__i386__) || defined (__x86_64__)
5ba3f43e 1435 paniclog_append_noflush("__HIB text base: %p\n", (void *) vm_hib_base);
fe8ab488
A
1436#endif
1437}
1438
5ba3f43e 1439static void
0a7de745
A
1440panic_display_uptime(void)
1441{
1442 uint64_t uptime;
c910b4d9
A
1443 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime);
1444
5ba3f43e 1445 paniclog_append_noflush("\nSystem uptime in nanoseconds: %llu\n", uptime);
c910b4d9
A
1446}
1447
5ba3f43e 1448static void
0a7de745
A
1449panic_display_disk_errors(void)
1450{
d190cdc3
A
1451 if (panic_disk_error_description[0]) {
1452 panic_disk_error_description[sizeof(panic_disk_error_description) - 1] = '\0';
5ba3f43e 1453 paniclog_append_noflush("Root disk errors: \"%s\"\n", panic_disk_error_description);
d190cdc3 1454 }
cb323159
A
1455}
1456
1457static void
1458panic_display_shutdown_status(void)
1459{
1460#if defined(__i386__) || defined(__x86_64__)
1461 paniclog_append_noflush("System shutdown begun: %s\n", IOPMRootDomainGetWillShutdown() ? "YES" : "NO");
1462 if (gIOPolledCoreFileMode == kIOPolledCoreFileModeNotInitialized) {
1463 paniclog_append_noflush("Panic diags file unavailable, panic occurred prior to initialization\n");
1464 } else if (gIOPolledCoreFileMode != kIOPolledCoreFileModeDisabled) {
1465 /*
1466 * If we haven't marked the corefile as explicitly disabled, and we've made it past initialization, then we know the current
1467 * system was configured to use disk based diagnostics at some point.
1468 */
1469 paniclog_append_noflush("Panic diags file available: %s (0x%x)\n", (gIOPolledCoreFileMode != kIOPolledCoreFileModeClosed) ? "YES" : "NO", kdp_polled_corefile_error());
1470 }
1471#endif
1472}
d190cdc3 1473
2d21ac55
A
1474extern const char version[];
1475extern char osversion[];
1476
6d2010ae
A
1477static volatile uint32_t config_displayed = 0;
1478
5ba3f43e 1479__private_extern__ void
0a7de745
A
1480panic_display_system_configuration(boolean_t launchd_exit)
1481{
1482 if (!launchd_exit) {
1483 panic_display_process_name();
1484 }
6d2010ae
A
1485 if (OSCompareAndSwap(0, 1, &config_displayed)) {
1486 char buf[256];
0a7de745 1487 if (!launchd_exit && strlcpy(buf, PE_boot_args(), sizeof(buf))) {
5ba3f43e 1488 paniclog_append_noflush("Boot args: %s\n", buf);
0a7de745 1489 }
5ba3f43e 1490 paniclog_append_noflush("\nMac OS version:\n%s\n",
2d21ac55 1491 (osversion[0] != 0) ? osversion : "Not yet set");
0a7de745 1492 paniclog_append_noflush("\nKernel version:\n%s\n", version);
6d2010ae 1493 panic_display_kernel_uuid();
d190cdc3
A
1494 if (!launchd_exit) {
1495 panic_display_kernel_aslr();
1496 panic_display_hibb();
1497 panic_display_pal_info();
1498 }
2d21ac55 1499 panic_display_model_name();
d190cdc3 1500 panic_display_disk_errors();
cb323159 1501 panic_display_shutdown_status();
d190cdc3
A
1502 if (!launchd_exit) {
1503 panic_display_uptime();
1504 panic_display_zprint();
6d2010ae 1505#if CONFIG_ZLEAKS
d190cdc3 1506 panic_display_ztrace();
6d2010ae 1507#endif /* CONFIG_ZLEAKS */
5ba3f43e 1508 kext_dump_panic_lists(&paniclog_append_noflush);
d190cdc3 1509 }
2d21ac55
A
1510 }
1511}
1512
0a7de745 1513extern unsigned int stack_total;
6d2010ae 1514extern unsigned long long stack_allocs;
c910b4d9 1515
5ba3f43e 1516#if defined (__x86_64__)
0a7de745 1517extern unsigned int inuse_ptepages_count;
6d2010ae 1518extern long long alloc_ptepages_count;
c910b4d9
A
1519#endif
1520
5ba3f43e
A
1521extern boolean_t panic_include_zprint;
1522extern mach_memory_info_t *panic_kext_memory_info;
1523extern vm_size_t panic_kext_memory_size;
c910b4d9 1524
5ba3f43e
A
1525__private_extern__ void
1526panic_display_zprint()
c910b4d9 1527{
0a7de745
A
1528 if (panic_include_zprint == TRUE) {
1529 unsigned int i;
1530 struct zone zone_copy;
c910b4d9 1531
5ba3f43e 1532 paniclog_append_noflush("%-20s %10s %10s\n", "Zone Name", "Cur Size", "Free Size");
39037602 1533 for (i = 0; i < num_zones; i++) {
0a7de745
A
1534 if (ml_nofault_copy((vm_offset_t)(&zone_array[i]), (vm_offset_t)&zone_copy, sizeof(struct zone)) == sizeof(struct zone)) {
1535 if (zone_copy.cur_size > (1024 * 1024)) {
1536 paniclog_append_noflush("%-20s %10lu %10lu\n", zone_copy.zone_name, (uintptr_t)zone_copy.cur_size, (uintptr_t)(zone_copy.countfree * zone_copy.elem_size));
c910b4d9 1537 }
0a7de745 1538 }
c910b4d9
A
1539 }
1540
5ba3f43e 1541 paniclog_append_noflush("%-20s %10lu\n", "Kernel Stacks", (uintptr_t)(kernel_stack_size * stack_total));
b0d623f7 1542
5ba3f43e 1543#if defined (__x86_64__)
0a7de745 1544 paniclog_append_noflush("%-20s %10lu\n", "PageTables", (uintptr_t)(PAGE_SIZE * inuse_ptepages_count));
c910b4d9 1545#endif
b0d623f7 1546
5ba3f43e 1547 paniclog_append_noflush("%-20s %10lu\n", "Kalloc.Large", (uintptr_t)kalloc_large_total);
3e170ce0 1548 if (panic_kext_memory_info) {
5ba3f43e
A
1549 mach_memory_info_t *mem_info = panic_kext_memory_info;
1550 paniclog_append_noflush("\n%-5s %10s\n", "Kmod", "Size");
1551 for (i = 0; i < (panic_kext_memory_size / sizeof(mach_zone_info_t)); i++) {
3e170ce0 1552 if (((mem_info[i].flags & VM_KERN_SITE_TYPE) == VM_KERN_SITE_KMOD) && (mem_info[i].size > (1024 * 1024))) {
5ba3f43e 1553 paniclog_append_noflush("%-5lld %10lld\n", mem_info[i].site, mem_info[i].size);
3e170ce0
A
1554 }
1555 }
1556 }
c910b4d9
A
1557 }
1558}
1559
fe8ab488 1560#if CONFIG_ECC_LOGGING
5ba3f43e 1561__private_extern__ void
0a7de745 1562panic_display_ecc_errors()
fe8ab488
A
1563{
1564 uint32_t count = ecc_log_get_correction_count();
1565
1566 if (count > 0) {
5ba3f43e 1567 paniclog_append_noflush("ECC Corrections:%u\n", count);
fe8ab488
A
1568 }
1569}
1570#endif /* CONFIG_ECC_LOGGING */
1571
6d2010ae 1572#if CONFIG_ZLEAKS
0a7de745 1573extern boolean_t panic_include_ztrace;
6d2010ae 1574extern struct ztrace* top_ztrace;
04b8595b
A
1575void panic_print_symbol_name(vm_address_t search);
1576
6d2010ae
A
1577/*
1578 * Prints the backtrace most suspected of being a leaker, if we paniced in the zone allocator.
1579 * top_ztrace and panic_include_ztrace comes from osfmk/kern/zalloc.c
1580 */
5ba3f43e
A
1581__private_extern__ void
1582panic_display_ztrace(void)
6d2010ae 1583{
0a7de745 1584 if (panic_include_ztrace == TRUE) {
6d2010ae 1585 unsigned int i = 0;
0a7de745 1586 boolean_t keepsyms = FALSE;
04b8595b 1587
0a7de745 1588 PE_parse_boot_argn("keepsyms", &keepsyms, sizeof(keepsyms));
6d2010ae 1589 struct ztrace top_ztrace_copy;
0a7de745 1590
6d2010ae 1591 /* Make sure not to trip another panic if there's something wrong with memory */
0a7de745 1592 if (ml_nofault_copy((vm_offset_t)top_ztrace, (vm_offset_t)&top_ztrace_copy, sizeof(struct ztrace)) == sizeof(struct ztrace)) {
5ba3f43e 1593 paniclog_append_noflush("\nBacktrace suspected of leaking: (outstanding bytes: %lu)\n", (uintptr_t)top_ztrace_copy.zt_size);
6d2010ae 1594 /* Print the backtrace addresses */
0a7de745 1595 for (i = 0; (i < top_ztrace_copy.zt_depth && i < MAX_ZTRACE_DEPTH); i++) {
5ba3f43e 1596 paniclog_append_noflush("%p ", top_ztrace_copy.zt_stack[i]);
04b8595b
A
1597 if (keepsyms) {
1598 panic_print_symbol_name((vm_address_t)top_ztrace_copy.zt_stack[i]);
1599 }
5ba3f43e 1600 paniclog_append_noflush("\n");
6d2010ae
A
1601 }
1602 /* Print any kexts in that backtrace, along with their link addresses so we can properly blame them */
1603 kmod_panic_dump((vm_offset_t *)&top_ztrace_copy.zt_stack[0], top_ztrace_copy.zt_depth);
0a7de745 1604 } else {
5ba3f43e 1605 paniclog_append_noflush("\nCan't access top_ztrace...\n");
6d2010ae 1606 }
5ba3f43e 1607 paniclog_append_noflush("\n");
6d2010ae
A
1608 }
1609}
1610#endif /* CONFIG_ZLEAKS */
1611
39236c6e 1612#if !CONFIG_TELEMETRY
5ba3f43e
A
1613int
1614telemetry_gather(user_addr_t buffer __unused, uint32_t *length __unused, boolean_t mark __unused)
39236c6e
A
1615{
1616 return KERN_NOT_SUPPORTED;
1617}
1618#endif
5ba3f43e
A
1619
1620#include <machine/machine_cpu.h>
1621
1622uint32_t kern_feature_overrides = 0;
1623
0a7de745
A
1624boolean_t
1625kern_feature_override(uint32_t fmask)
1626{
5ba3f43e
A
1627 if (kern_feature_overrides == 0) {
1628 uint32_t fdisables = 0;
cb323159
A
1629 /*
1630 * Expected to be first invoked early, in a single-threaded
5ba3f43e
A
1631 * environment
1632 */
1633 if (PE_parse_boot_argn("validation_disables", &fdisables, sizeof(fdisables))) {
1634 fdisables |= KF_INITIALIZED;
1635 kern_feature_overrides = fdisables;
1636 } else {
1637 kern_feature_overrides |= KF_INITIALIZED;
1638 }
1639 }
0a7de745 1640 return (kern_feature_overrides & fmask) == fmask;
5ba3f43e 1641}
cb323159
A
1642
1643boolean_t
1644on_device_corefile_enabled(void)
1645{
1646 assert(debug_boot_arg_inited);
1647#if CONFIG_KDP_INTERACTIVE_DEBUGGING
1648 if ((debug_boot_arg != 0) && !(debug_boot_arg & DB_DISABLE_LOCAL_CORE)) {
1649 return TRUE;
1650 }
1651#endif
1652 return FALSE;
1653}
1654
1655boolean_t
1656panic_stackshot_to_disk_enabled(void)
1657{
1658 assert(debug_boot_arg_inited);
1659#if defined(__x86_64__)
1660 if (PEGetCoprocessorVersion() < kCoprocessorVersion2) {
1661 /* Only enabled on pre-Gibraltar machines where it hasn't been disabled explicitly */
1662 if ((debug_boot_arg != 0) && (debug_boot_arg & DB_DISABLE_STACKSHOT_TO_DISK)) {
1663 return FALSE;
1664 }
1665
1666 return TRUE;
1667 }
1668#endif
1669 return FALSE;
1670}