]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/kern/debug.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / osfmk / kern / debug.c
... / ...
CommitLineData
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/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
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>
58#include <mach_kdp.h>
59#include <kdp/kdp.h>
60#include <kdp/kdp_core.h>
61#include <kdp/kdp_internal.h>
62#include <kdp/kdp_callout.h>
63#include <kern/cpu_number.h>
64#include <kern/kalloc.h>
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>
70#include <kern/clock.h>
71#include <kern/telemetry.h>
72#include <kern/ecc.h>
73#include <kern/kern_cdata.h>
74#include <kern/zalloc.h>
75#include <vm/vm_kern.h>
76#include <vm/vm_map.h>
77#include <vm/pmap.h>
78#include <stdarg.h>
79#include <stdatomic.h>
80#include <sys/pgo.h>
81#include <console/serial_protos.h>
82
83#if !(MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
84#include <kdp/kdp_udp.h>
85#endif
86#include <kern/processor.h>
87
88#if defined(__i386__) || defined(__x86_64__)
89#include <IOKit/IOBSD.h>
90
91#include <i386/cpu_threads.h>
92#include <i386/pmCPU.h>
93#endif
94
95#include <IOKit/IOPlatformExpert.h>
96#include <machine/pal_routines.h>
97
98#include <sys/kdebug.h>
99#include <libkern/OSKextLibPrivate.h>
100#include <libkern/OSAtomic.h>
101#include <libkern/kernel_mach_header.h>
102#include <libkern/section_keywords.h>
103#include <uuid/uuid.h>
104#include <mach_debug/zone_info.h>
105
106#include <os/log_private.h>
107
108#if CONFIG_EMBEDDED
109#include <pexpert/pexpert.h> /* For gPanicBase */
110#include <arm/caches_internal.h>
111#include <arm/misc_protos.h>
112extern volatile struct xnu_hw_shmem_dbg_command_info *hwsd_info;
113#endif
114
115#if CONFIG_XNUPOST
116#include <tests/xnupost.h>
117extern int vsnprintf(char *, size_t, const char *, va_list);
118#endif
119
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;
126
127#if !defined (__x86_64__)
128struct additional_panic_data_buffer *panic_data_buffers = NULL;
129#endif
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__)
145#define panic_stop() pmCPUHalt(PM_HALT_PANIC)
146#else
147#define panic_stop() panic_spin_forever()
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
155#define CPUPANICDATAPTR PROCESSOR_DATA(current_processor(), debugger_state).db_panic_data_ptr
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
162#define DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED(requested) \
163MACRO_BEGIN \
164 if (requested) { \
165 volatile int *badpointer = (int *)4; \
166 *badpointer = 0; \
167 } \
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;
174void *debugger_panic_data = NULL;
175uint64_t debugger_panic_options = 0;
176const char *debugger_message = NULL;
177unsigned long debugger_panic_caller = 0;
178
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;
184extern kern_return_t do_stackshot(void);
185extern void PE_panic_hook(const char*);
186
187#if CONFIG_NONFATAL_ASSERTS
188int mach_assert = 1;
189#endif
190
191#define NESTEDDEBUGGERENTRYMAX 5
192static unsigned int max_debugger_entry_count = NESTEDDEBUGGERENTRYMAX;
193
194#if CONFIG_EMBEDDED
195#define DEBUG_BUF_SIZE (4096)
196#define KDBG_TRACE_PANIC_FILENAME "/var/log/panic.trace"
197#else
198#define DEBUG_BUF_SIZE ((3 * PAGE_SIZE) + offsetof(struct macos_panic_header, mph_data))
199/* EXTENDED_DEBUG_BUF_SIZE definition is now in debug.h */
200static_assert(((EXTENDED_DEBUG_BUF_SIZE % PANIC_FLUSH_BOUNDARY) == 0), "Extended debug buf size must match SMC alignment requirements");
201#define KDBG_TRACE_PANIC_FILENAME "/var/tmp/panic.trace"
202#endif
203
204/* debug_buf is directly linked with iBoot panic region for embedded targets */
205#if CONFIG_EMBEDDED
206char *debug_buf_base = NULL;
207char *debug_buf_ptr = NULL;
208unsigned int debug_buf_size = 0;
209#else
210char debug_buf[DEBUG_BUF_SIZE];
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 */
220unsigned int debug_buf_size = (DEBUG_BUF_SIZE - offsetof(struct macos_panic_header, mph_data));
221
222boolean_t extended_debug_log_enabled = FALSE;
223#endif
224
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;
230
231static char model_name[64];
232unsigned char *kernel_uuid;
233
234boolean_t kernelcache_uuid_valid = FALSE;
235uuid_t kernelcache_uuid;
236uuid_string_t kernelcache_uuid_string;
237
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;
246
247#if DEVELOPMENT || DEBUG
248boolean_t debug_boot_arg_inited = FALSE;
249#endif
250
251SECURITY_READ_ONLY_LATE(unsigned int) debug_boot_arg;
252
253char kernel_uuid_string[37]; /* uuid_string_t */
254char kernelcache_uuid_string[37]; /* uuid_string_t */
255char panic_disk_error_description[512];
256size_t panic_disk_error_description_size = sizeof(panic_disk_error_description);
257
258extern unsigned int write_trace_on_panic;
259int kext_assertions_enable =
260#if DEBUG || DEVELOPMENT
261 TRUE;
262#else
263 FALSE;
264#endif
265
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
274void
275panic_init(void)
276{
277 unsigned long uuidlen = 0;
278 void *uuid;
279
280 uuid = getuuidfromheader(&_mh_execute_header, &uuidlen);
281 if ((uuid != NULL) && (uuidlen == sizeof(uuid_t))) {
282 kernel_uuid = uuid;
283 uuid_unparse_upper(*(uuid_t *)uuid, kernel_uuid_string);
284 }
285
286#if CONFIG_NONFATAL_ASSERTS
287 if (!PE_parse_boot_argn("assertions", &mach_assert, sizeof(mach_assert))) {
288 mach_assert = 1;
289 }
290#endif
291
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__))
297 if (PE_parse_boot_argn("debug", &debug_boot_arg, sizeof(debug_boot_arg))) {
298#if DEVELOPMENT || DEBUG
299 if (debug_boot_arg & DB_HALT) {
300 halt_in_debugger = 1;
301 }
302#endif
303
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 }
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
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)) {
338 debugger_is_panic = FALSE;
339 }
340#endif
341}
342
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);
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);
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;
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;
375}
376#endif /* defined (__x86_64__) */
377
378void
379debug_log_init(void)
380{
381#if CONFIG_EMBEDDED
382 if (!gPanicBase) {
383 printf("debug_log_init: Error!! gPanicBase is still not initialized\n");
384 return;
385 }
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);
390#else
391 kern_return_t kr = KERN_SUCCESS;
392 bzero(panic_info, DEBUG_BUF_SIZE);
393
394 assert(debug_buf_base != NULL);
395 assert(debug_buf_ptr != NULL);
396 assert(debug_buf_size != 0);
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 }
409#endif
410}
411
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
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
456 if (atomic_load(&debugger_cpu) == my_cpu) {
457 return;
458 }
459
460 while (!atomic_compare_exchange_strong(&debugger_cpu, &debugger_exp_cpu, my_cpu)) {
461 debugger_exp_cpu = DEBUGGER_NO_CPU;
462 }
463
464 return;
465}
466
467static void
468DebuggerUnlock()
469{
470 assert(atomic_load_explicit(&debugger_cpu, memory_order_relaxed) == cpu_number());
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 */
479 atomic_store(&debugger_cpu, DEBUGGER_NO_CPU);
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;
494#endif
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,
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)
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;
519 CPUPANICDATAPTR = db_panic_data_ptr;
520 CPUPANICCALLER = db_panic_caller;
521 } else if (CPUDEBUGGERCOUNT > 1 && db_panic_str != NULL) {
522 kprintf("Nested panic detected:");
523 if (db_panic_str != NULL) {
524 _doprnt(db_panic_str, db_panic_args, PE_kputc, 0);
525 }
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}
536
537/*
538 * Save the requested debugger state/action into the current processor's processor_data
539 * and trap to the debugger.
540 */
541kern_return_t
542DebuggerTrapWithState(debugger_op db_op, const char *db_message, const char *db_panic_str,
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)
545{
546 kern_return_t ret;
547
548 assert(ml_get_interrupts_enabled() == FALSE);
549 DebuggerSaveState(db_op, db_message, db_panic_str, db_panic_args,
550 db_panic_options, db_panic_data_ptr,
551 db_proceed_on_sync_failure, db_panic_caller);
552
553 TRAP_DEBUGGER;
554
555 ret = CPUDEBUGGERRET;
556
557 DebuggerSaveState(DBOP_NONE, NULL, NULL, NULL, 0, NULL, FALSE, 0);
558
559 return ret;
560}
561
562void __attribute__((noinline))
563Assert(
564 const char *file,
565 int line,
566 const char *expression
567 )
568{
569#if CONFIG_NONFATAL_ASSERTS
570 if (!mach_assert) {
571 kprintf("%s:%d non-fatal Assertion: %s", file, line, expression);
572 return;
573 }
574#endif
575
576 panic_plain("%s:%d Assertion failed: %s", file, line, expression);
577}
578
579
580void
581Debugger(const char *message)
582{
583 DebuggerWithContext(0, NULL, message, DEBUGGER_OPTION_NONE);
584}
585
586void
587DebuggerWithContext(unsigned int reason, void *ctx, const char *message,
588 uint64_t debugger_options_mask)
589{
590 spl_t previous_interrupts_state;
591 boolean_t old_doprnt_hide_pointers = doprnt_hide_pointers;
592
593 previous_interrupts_state = ml_set_interrupts_enabled(FALSE);
594 disable_preemption();
595
596 CPUDEBUGGERCOUNT++;
597
598 if (CPUDEBUGGERCOUNT > max_debugger_entry_count) {
599 static boolean_t in_panic_kprintf = FALSE;
600
601 /* Notify any listeners that we've started a panic */
602 PEHaltRestart(kPEPanicBegin);
603
604 if (!in_panic_kprintf) {
605 in_panic_kprintf = TRUE;
606 kprintf("Detected nested debugger entry count exceeding %d\n",
607 max_debugger_entry_count);
608 in_panic_kprintf = FALSE;
609 }
610
611 if (!panicDebugging) {
612 kdp_machine_reboot_type(kPEPanicRestartCPU, debugger_options_mask);
613 }
614
615 panic_spin_forever();
616 }
617
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,
626 NULL, NULL, debugger_options_mask, NULL, TRUE, 0);
627 handle_debugger_trap(reason, 0, 0, ctx);
628 DebuggerSaveState(DBOP_NONE, NULL, NULL,
629 NULL, 0, NULL, FALSE, 0);
630 } else {
631 DebuggerTrapWithState(DBOP_DEBUGGER, message,
632 NULL, NULL, debugger_options_mask, NULL, TRUE, 0);
633 }
634
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));
658 if (kcp == NULL) {
659 panic("kdp_register_callout() kalloc failed");
660 }
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));
671}
672
673static void
674kdp_callouts(kdp_event_t event)
675{
676 struct kdp_callout *kcp = kdp_callout_list;
677
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;
685 }
686}
687
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
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
740void
741panic(const char *str, ...)
742{
743 va_list panic_str_args;
744
745 va_start(panic_str_args, str);
746 panic_trap_to_debugger(str, &panic_str_args, 0, NULL, 0, NULL, (unsigned long)(char *)__builtin_return_address(0));
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);
756 panic_trap_to_debugger(str, &panic_str_args, reason, ctx, (debugger_options_mask & ~DEBUGGER_INTERNAL_OPTIONS_MASK),
757 NULL, (unsigned long)(char *)__builtin_return_address(0));
758 va_end(panic_str_args);
759}
760
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 */
770void
771panic_with_thread_context(unsigned int reason, void *ctx, uint64_t debugger_options_mask, thread_t thread, const char *str, ...)
772{
773 va_list panic_str_args;
774 __assert_only os_ref_count_t th_ref_count;
775
776 assert_thread_magic(thread);
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);
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);
782
783 va_start(panic_str_args, str);
784 panic_trap_to_debugger(str, &panic_str_args, reason, ctx, ((debugger_options_mask & ~DEBUGGER_INTERNAL_OPTIONS_MASK) | DEBUGGER_INTERNAL_OPTION_THREAD_BACKTRACE),
785 thread, (unsigned long)(char *)__builtin_return_address(0));
786
787 va_end(panic_str_args);
788}
789#endif /* defined (__x86_64__) */
790
791#pragma clang diagnostic push
792#pragma clang diagnostic ignored "-Wmissing-noreturn"
793void
794panic_trap_to_debugger(const char *panic_format_str, va_list *panic_args, unsigned int reason, void *ctx,
795 uint64_t panic_options_mask, void *panic_data_ptr, unsigned long panic_caller)
796{
797#pragma clang diagnostic pop
798
799#if defined(__x86_64__) && (DEVELOPMENT || DEBUG)
800 /* Turn off I/O tracing once we've panicked */
801 mmiotrace_enabled = 0;
802#endif
803
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);
806 __builtin_trap();
807 }
808
809 CPUDEBUGGERCOUNT++;
810
811 if (CPUDEBUGGERCOUNT > max_debugger_entry_count) {
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",
820 max_debugger_entry_count);
821 in_panic_kprintf = FALSE;
822 }
823
824 if (!panicDebugging) {
825 kdp_machine_reboot_type(kPEPanicRestartCPU, panic_options_mask);
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
835 PE_panic_hook(panic_format_str);
836
837#if defined (__x86_64__)
838 plctrace_disable();
839#endif
840
841 if (write_trace_on_panic && kdebug_enable) {
842 if (get_preemption_level() == 0 && !ml_at_interrupt_context()) {
843 ml_set_interrupts_enabled(TRUE);
844 KDBG_RELEASE(TRACE_PANIC);
845 kdbg_dump_trace_to_file(KDBG_TRACE_PANIC_FILENAME);
846 }
847 }
848
849 ml_set_interrupts_enabled(FALSE);
850 disable_preemption();
851
852#if defined (__x86_64__)
853 pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE);
854#endif /* defined (__x86_64__) */
855
856 /* Never hide pointers from panic logs. */
857 doprnt_hide_pointers = FALSE;
858
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",
865 panic_format_str, panic_args,
866 panic_options_mask, panic_data_ptr, TRUE, panic_caller);
867 handle_debugger_trap(reason, 0, 0, ctx);
868 }
869
870#if defined(__arm64__)
871 /*
872 * Signal to fastsim that it should open debug ports (nop on hardware)
873 */
874 __asm__ volatile ("HINT 0x45");
875#endif /* defined(__arm64__) */
876
877 DebuggerTrapWithState(DBOP_PANIC, "panic", panic_format_str,
878 panic_args, panic_options_mask, panic_data_ptr, TRUE, panic_caller);
879
880 /*
881 * Not reached.
882 */
883 panic_stop();
884 __builtin_unreachable();
885}
886
887void
888panic_spin_forever(void)
889{
890 paniclog_append_noflush("\nPlease go to https://panic.apple.com to report this panic\n");
891
892 for (;;) {
893 }
894}
895
896static void
897kdp_machine_reboot_type(unsigned int type, uint64_t debugger_flags)
898{
899 printf("Attempting system restart...");
900 if ((type == kPEPanicRestartCPU) && (debugger_flags & DEBUGGER_OPTION_SKIP_PANICEND_CALLOUTS)) {
901 PEHaltRestart(kPEPanicRestartCPUNoPanicEndCallouts);
902 } else {
903 PEHaltRestart(type);
904 }
905 halt_all_cpus(TRUE);
906}
907
908void
909kdp_machine_reboot(void)
910{
911 kdp_machine_reboot_type(kPEPanicRestartCPU, 0);
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__)
932 kprintf("Debugger called: <%s>\n", debugger_message ? debugger_message : "");
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) ||
947 ((debugger_current_op == DBOP_DEBUGGER) && debugger_is_panic)) {
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 }
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 }
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 */
995 if (debugger_current_op == DBOP_PANIC || ((debugger_current_op == DBOP_DEBUGGER) && debugger_is_panic))
996#endif
997 {
998 kdp_callouts(KDP_EVENT_PANICLOG);
999
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 */
1005 SavePanicInfo(debugger_message, debugger_panic_data, debugger_panic_options);
1006
1007#if DEVELOPMENT || DEBUG
1008 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_panic_options & DEBUGGER_OPTION_RECURPANIC_POSTLOG));
1009#endif
1010
1011 /* DEBUGGER_OPTION_PANICLOGANDREBOOT is used for two finger resets on embedded so we get a paniclog */
1012 if (debugger_panic_options & DEBUGGER_OPTION_PANICLOGANDREBOOT) {
1013 PEHaltRestart(kPEPanicRestartCPUNoCallouts);
1014 }
1015 }
1016
1017#if CONFIG_KDP_INTERACTIVE_DEBUGGING
1018 /*
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.
1022 */
1023 if ((debugger_panic_options & DEBUGGER_OPTION_SKIP_LOCAL_COREDUMP) &&
1024 (debug_boot_arg & DB_REBOOT_POST_CORE)) {
1025 kdp_machine_reboot_type(kPEPanicRestartCPU, debugger_panic_options);
1026 }
1027
1028 /*
1029 * Consider generating a local corefile if the infrastructure is configured
1030 * and we haven't disabled on-device coredumps.
1031 */
1032 if (on_device_corefile_enabled()) {
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)",
1036 kdp_polled_corefile_error());
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;
1049
1050#if defined (__x86_64__)
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))
1053#endif
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();
1063
1064#if DEVELOPMENT || DEBUG
1065 DEBUGGER_DEBUGGING_NESTED_PANIC_IF_REQUESTED((debugger_panic_options & DEBUGGER_OPTION_RECURPANIC_POSTCORE));
1066#endif
1067 }
1068
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) &&
1074 ((ret == 0) || (debugger_panic_options & DEBUGGER_OPTION_ATTEMPTCOREDUMPANDREBOOT))) {
1075 kdp_machine_reboot_type(kPEPanicRestartCPU, debugger_panic_options);
1076 }
1077 }
1078 }
1079
1080 if (debug_boot_arg & DB_REBOOT_ALWAYS) {
1081 kdp_machine_reboot_type(kPEPanicRestartCPU, debugger_panic_options);
1082 }
1083
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)
1090 */
1091 if (debugger_current_op == DBOP_DEBUGGER &&
1092 debugger_safe_to_return &&
1093 kernel_debugger_entry_count == 1 &&
1094 !debugger_is_panic) {
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) {
1108 kdp_machine_reboot_type(kPEPanicRestartCPU, debugger_panic_options);
1109 }
1110
1111 panic_spin_forever();
1112}
1113
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)
1119#endif
1120
1121void
1122handle_debugger_trap(unsigned int exception, unsigned int code, unsigned int subcode, void *state)
1123{
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);
1132
1133 DEBUGGER_TRAP_TIMESTAMP(1);
1134
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 ||
1160 debugger_current_op == DBOP_DEBUGGER) {
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;
1170 debugger_panic_data = CPUPANICDATAPTR;
1171 debugger_message = CPUDEBUGGERMSG;
1172 debugger_panic_caller = CPUPANICCALLER;
1173 }
1174
1175 debugger_panic_options = CPUPANICOPTS;
1176 }
1177
1178 /*
1179 * Clear the op from the processor debugger context so we can handle
1180 * breakpoints in the debugger
1181 */
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
1196 } else if (debugger_current_op == DBOP_RESET_PGO_COUNTERS) {
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;
1213 debugger_panic_data = NULL;
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;
1232}
1233
1234__attribute__((noinline, not_tail_called))
1235void
1236log(__unused int level, char *fmt, ...)
1237{
1238 void *caller = __builtin_return_address(0);
1239 va_list listp;
1240 va_list listp2;
1241
1242
1243#ifdef lint
1244 level++;
1245#endif /* lint */
1246#ifdef MACH_BSD
1247 va_start(listp, fmt);
1248 va_copy(listp2, listp);
1249
1250 disable_preemption();
1251 _doprnt(fmt, &listp, cons_putc_locked, 0);
1252 enable_preemption();
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);
1258#endif
1259}
1260
1261/*
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:
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
1272boolean_t
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;
1279}
1280
1281boolean_t
1282debug_mode_active(void)
1283{
1284 return (0 != kernel_debugger_entry_count != 0) || (0 == not_in_kdp);
1285}
1286
1287void
1288debug_putc(char c)
1289{
1290 if ((debug_buf_size != 0) &&
1291 ((debug_buf_ptr - debug_buf_base) < (int)debug_buf_size)) {
1292 *debug_buf_ptr = c;
1293 debug_buf_ptr++;
1294 }
1295}
1296
1297#if defined (__x86_64__)
1298struct pasc {
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;
1307} __attribute__((packed));
1308
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)
1317{
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;
1336}
1337
1338void
1339unpackA(char *inbuf, uint32_t length)
1340{
1341 pasc_t packs;
1342 unsigned i = 0;
1343 length = (length * 8) / 7;
1344
1345 while (i < length) {
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;
1356 }
1357}
1358#endif /* defined (__x86_64__) */
1359
1360extern void *proc_name_address(void *p);
1361
1362static void
1363panic_display_process_name(void)
1364{
1365 /* because of scoping issues len(p_comm) from proc_t is hard coded here */
1366 char proc_name[17] = "Unknown";
1367 task_t ctask = 0;
1368 void *cbsd_info = 0;
1369
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)) {
1373 proc_name[sizeof(proc_name) - 1] = '\0';
1374 }
1375 }
1376 }
1377 paniclog_append_noflush("\nBSD process name corresponding to current thread: %s\n", proc_name);
1378}
1379
1380unsigned
1381panic_active(void)
1382{
1383 return debugger_panic_str != (char *) 0;
1384}
1385
1386void
1387populate_model_name(char *model_string)
1388{
1389 strlcpy(model_name, model_string, sizeof(model_name));
1390}
1391
1392void
1393panic_display_model_name(void)
1394{
1395 char tmp_model_name[sizeof(model_name)];
1396
1397 if (ml_nofault_copy((vm_offset_t) &model_name, (vm_offset_t) &tmp_model_name, sizeof(model_name)) != sizeof(model_name)) {
1398 return;
1399 }
1400
1401 tmp_model_name[sizeof(tmp_model_name) - 1] = '\0';
1402
1403 if (tmp_model_name[0] != 0) {
1404 paniclog_append_noflush("System model name: %s\n", tmp_model_name);
1405 }
1406}
1407
1408void
1409panic_display_kernel_uuid(void)
1410{
1411 char tmp_kernel_uuid[sizeof(kernel_uuid_string)];
1412
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)) {
1414 return;
1415 }
1416
1417 if (tmp_kernel_uuid[0] != '\0') {
1418 paniclog_append_noflush("Kernel UUID: %s\n", tmp_kernel_uuid);
1419 }
1420}
1421
1422void
1423panic_display_kernel_aslr(void)
1424{
1425 if (vm_kernel_slide) {
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);
1428 }
1429}
1430
1431void
1432panic_display_hibb(void)
1433{
1434#if defined(__i386__) || defined (__x86_64__)
1435 paniclog_append_noflush("__HIB text base: %p\n", (void *) vm_hib_base);
1436#endif
1437}
1438
1439static void
1440panic_display_uptime(void)
1441{
1442 uint64_t uptime;
1443 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime);
1444
1445 paniclog_append_noflush("\nSystem uptime in nanoseconds: %llu\n", uptime);
1446}
1447
1448static void
1449panic_display_disk_errors(void)
1450{
1451 if (panic_disk_error_description[0]) {
1452 panic_disk_error_description[sizeof(panic_disk_error_description) - 1] = '\0';
1453 paniclog_append_noflush("Root disk errors: \"%s\"\n", panic_disk_error_description);
1454 }
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}
1473
1474extern const char version[];
1475extern char osversion[];
1476
1477static volatile uint32_t config_displayed = 0;
1478
1479__private_extern__ void
1480panic_display_system_configuration(boolean_t launchd_exit)
1481{
1482 if (!launchd_exit) {
1483 panic_display_process_name();
1484 }
1485 if (OSCompareAndSwap(0, 1, &config_displayed)) {
1486 char buf[256];
1487 if (!launchd_exit && strlcpy(buf, PE_boot_args(), sizeof(buf))) {
1488 paniclog_append_noflush("Boot args: %s\n", buf);
1489 }
1490 paniclog_append_noflush("\nMac OS version:\n%s\n",
1491 (osversion[0] != 0) ? osversion : "Not yet set");
1492 paniclog_append_noflush("\nKernel version:\n%s\n", version);
1493 panic_display_kernel_uuid();
1494 if (!launchd_exit) {
1495 panic_display_kernel_aslr();
1496 panic_display_hibb();
1497 panic_display_pal_info();
1498 }
1499 panic_display_model_name();
1500 panic_display_disk_errors();
1501 panic_display_shutdown_status();
1502 if (!launchd_exit) {
1503 panic_display_uptime();
1504 panic_display_zprint();
1505#if CONFIG_ZLEAKS
1506 panic_display_ztrace();
1507#endif /* CONFIG_ZLEAKS */
1508 kext_dump_panic_lists(&paniclog_append_noflush);
1509 }
1510 }
1511}
1512
1513extern unsigned int stack_total;
1514extern unsigned long long stack_allocs;
1515
1516#if defined (__x86_64__)
1517extern unsigned int inuse_ptepages_count;
1518extern long long alloc_ptepages_count;
1519#endif
1520
1521extern boolean_t panic_include_zprint;
1522extern mach_memory_info_t *panic_kext_memory_info;
1523extern vm_size_t panic_kext_memory_size;
1524
1525__private_extern__ void
1526panic_display_zprint()
1527{
1528 if (panic_include_zprint == TRUE) {
1529 unsigned int i;
1530 struct zone zone_copy;
1531
1532 paniclog_append_noflush("%-20s %10s %10s\n", "Zone Name", "Cur Size", "Free Size");
1533 for (i = 0; i < num_zones; i++) {
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));
1537 }
1538 }
1539 }
1540
1541 paniclog_append_noflush("%-20s %10lu\n", "Kernel Stacks", (uintptr_t)(kernel_stack_size * stack_total));
1542
1543#if defined (__x86_64__)
1544 paniclog_append_noflush("%-20s %10lu\n", "PageTables", (uintptr_t)(PAGE_SIZE * inuse_ptepages_count));
1545#endif
1546
1547 paniclog_append_noflush("%-20s %10lu\n", "Kalloc.Large", (uintptr_t)kalloc_large_total);
1548 if (panic_kext_memory_info) {
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++) {
1552 if (((mem_info[i].flags & VM_KERN_SITE_TYPE) == VM_KERN_SITE_KMOD) && (mem_info[i].size > (1024 * 1024))) {
1553 paniclog_append_noflush("%-5lld %10lld\n", mem_info[i].site, mem_info[i].size);
1554 }
1555 }
1556 }
1557 }
1558}
1559
1560#if CONFIG_ECC_LOGGING
1561__private_extern__ void
1562panic_display_ecc_errors()
1563{
1564 uint32_t count = ecc_log_get_correction_count();
1565
1566 if (count > 0) {
1567 paniclog_append_noflush("ECC Corrections:%u\n", count);
1568 }
1569}
1570#endif /* CONFIG_ECC_LOGGING */
1571
1572#if CONFIG_ZLEAKS
1573extern boolean_t panic_include_ztrace;
1574extern struct ztrace* top_ztrace;
1575void panic_print_symbol_name(vm_address_t search);
1576
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 */
1581__private_extern__ void
1582panic_display_ztrace(void)
1583{
1584 if (panic_include_ztrace == TRUE) {
1585 unsigned int i = 0;
1586 boolean_t keepsyms = FALSE;
1587
1588 PE_parse_boot_argn("keepsyms", &keepsyms, sizeof(keepsyms));
1589 struct ztrace top_ztrace_copy;
1590
1591 /* Make sure not to trip another panic if there's something wrong with memory */
1592 if (ml_nofault_copy((vm_offset_t)top_ztrace, (vm_offset_t)&top_ztrace_copy, sizeof(struct ztrace)) == sizeof(struct ztrace)) {
1593 paniclog_append_noflush("\nBacktrace suspected of leaking: (outstanding bytes: %lu)\n", (uintptr_t)top_ztrace_copy.zt_size);
1594 /* Print the backtrace addresses */
1595 for (i = 0; (i < top_ztrace_copy.zt_depth && i < MAX_ZTRACE_DEPTH); i++) {
1596 paniclog_append_noflush("%p ", top_ztrace_copy.zt_stack[i]);
1597 if (keepsyms) {
1598 panic_print_symbol_name((vm_address_t)top_ztrace_copy.zt_stack[i]);
1599 }
1600 paniclog_append_noflush("\n");
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);
1604 } else {
1605 paniclog_append_noflush("\nCan't access top_ztrace...\n");
1606 }
1607 paniclog_append_noflush("\n");
1608 }
1609}
1610#endif /* CONFIG_ZLEAKS */
1611
1612#if !CONFIG_TELEMETRY
1613int
1614telemetry_gather(user_addr_t buffer __unused, uint32_t *length __unused, boolean_t mark __unused)
1615{
1616 return KERN_NOT_SUPPORTED;
1617}
1618#endif
1619
1620#include <machine/machine_cpu.h>
1621
1622uint32_t kern_feature_overrides = 0;
1623
1624boolean_t
1625kern_feature_override(uint32_t fmask)
1626{
1627 if (kern_feature_overrides == 0) {
1628 uint32_t fdisables = 0;
1629 /*
1630 * Expected to be first invoked early, in a single-threaded
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 }
1640 return (kern_feature_overrides & fmask) == fmask;
1641}
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}