2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989, 1988 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
62 * Author: Avadis Tevanian, Jr., Michael Wayne Young
64 * Copyright (C) 1986, Avadis Tevanian, Jr., Michael Wayne Young
66 * Basic initialization for I386 - ISA bus machines.
70 #define __APPLE_API_PRIVATE 1
71 #define __APPLE_API_UNSTABLE 1
72 #include <kern/debug.h>
74 #include <mach/i386/vm_param.h>
77 #include <mach/vm_param.h>
78 #include <mach/vm_prot.h>
79 #include <mach/machine.h>
80 #include <mach/time_value.h>
81 #include <sys/kdebug.h>
83 #include <kern/assert.h>
84 #include <kern/lock_group.h>
85 #include <kern/misc_protos.h>
86 #include <kern/startup.h>
87 #include <kern/clock.h>
88 #include <kern/cpu_data.h>
89 #include <kern/machine.h>
90 #include <i386/postcode.h>
91 #include <i386/mp_desc.h>
92 #include <i386/misc_protos.h>
93 #include <i386/thread.h>
94 #include <i386/trap.h>
95 #include <i386/machine_routines.h>
96 #include <i386/mp.h> /* mp_rendezvous_break_lock */
97 #include <i386/cpuid.h>
99 #include <i386/machine_cpu.h>
100 #include <i386/pmap.h>
102 #include <i386/mtrr.h>
104 #include <i386/ucode.h>
105 #include <i386/pmCPU.h>
106 #include <i386/panic_hooks.h>
108 #include <architecture/i386/pio.h> /* inb() */
109 #include <pexpert/i386/boot.h>
111 #include <kdp/kdp_dyld.h>
112 #include <kdp/kdp_core.h>
114 #include <vm/vm_map.h>
115 #include <vm/vm_kern.h>
117 #include <IOKit/IOBSD.h>
118 #include <IOKit/IOPlatformExpert.h>
119 #include <IOKit/IOHibernatePrivate.h>
121 #include <pexpert/i386/efi.h>
123 #include <kern/thread.h>
124 #include <kern/sched.h>
125 #include <mach-o/loader.h>
126 #include <mach-o/nlist.h>
128 #include <libkern/kernel_mach_header.h>
129 #include <libkern/OSKextLibPrivate.h>
130 #include <libkern/crc.h>
132 #if DEBUG || DEVELOPMENT
133 #define DPRINTF(x...) kprintf(x)
135 #define DPRINTF(x...)
139 #define ROUNDUP(a, b) (((a) + ((b) - 1)) & (~((b) - 1)))
143 #define ROUNDDOWN(x, y) (((x)/(y))*(y))
146 static void machine_conf(void);
147 void panic_print_symbol_name(vm_address_t search
);
148 void RecordPanicStackshot(void);
150 typedef enum paniclog_flush_type
{
151 kPaniclogFlushBase
= 1,/* Flush the initial log and paniclog header */
152 kPaniclogFlushStackshot
= 2,/* Flush only the stackshot data, then flush the header */
153 kPaniclogFlushOtherLog
= 3/* Flush the other log, then flush the header */
154 } paniclog_flush_type_t
;
156 void paniclog_flush_internal(paniclog_flush_type_t variant
);
158 extern const char version
[];
159 extern char osversion
[];
160 extern int max_unsafe_quanta
;
161 extern int max_poll_quanta
;
162 extern unsigned int panic_is_inited
;
164 extern int proc_pid(void *p
);
166 /* Definitions for frame pointers */
167 #define FP_ALIGNMENT_MASK ((uint32_t)(0x3))
168 #define FP_LR_OFFSET ((uint32_t)4)
169 #define FP_LR_OFFSET64 ((uint32_t)8)
170 #define FP_MAX_NUM_TO_EVALUATE (50)
172 volatile int pbtcpu
= -1;
173 hw_lock_data_t pbtlock
; /* backtrace print lock */
176 volatile int panic_double_fault_cpu
= -1;
178 #define PRINT_ARGS_FROM_STACK_FRAME 0
180 typedef struct _cframe_t
{
181 struct _cframe_t
*prev
;
183 #if PRINT_ARGS_FROM_STACK_FRAME
188 static unsigned panic_io_port
;
189 static unsigned commit_paniclog_to_nvram
;
190 boolean_t coprocessor_paniclog_flush
= FALSE
;
192 struct kcdata_descriptor kc_panic_data
;
193 static boolean_t begun_panic_stackshot
= FALSE
;
194 extern kern_return_t
do_stackshot(void *);
196 extern void kdp_snapshot_preflight(int pid
, void *tracebuf
,
197 uint32_t tracebuf_size
, uint32_t flags
,
198 kcdata_descriptor_t data_p
,
199 boolean_t enable_faulting
);
200 extern int kdp_stack_snapshot_bytes_traced(void);
202 vm_offset_t panic_stackshot_buf
= 0;
203 size_t panic_stackshot_buf_len
= 0;
205 size_t panic_stackshot_len
= 0;
207 * Backtrace a single frame.
210 print_one_backtrace(pmap_t pmap
, vm_offset_t topfp
, const char *cur_marker
,
218 boolean_t dump_kernel_stack
;
224 if (fp
>= VM_MIN_KERNEL_ADDRESS
) {
225 dump_kernel_stack
= TRUE
;
227 dump_kernel_stack
= FALSE
;
231 if ((fp
== 0) || ((fp
& FP_ALIGNMENT_MASK
) != 0)) {
234 if (dump_kernel_stack
&& ((fp
< VM_MIN_KERNEL_ADDRESS
) || (fp
> VM_MAX_KERNEL_ADDRESS
))) {
237 if ((!dump_kernel_stack
) && (fp
>= VM_MIN_KERNEL_ADDRESS
)) {
241 /* Check to see if current address will result in a different
242 * ppn than previously computed (to avoid recomputation) via
243 * (addr) ^ fp_for_ppn) >> PAGE_SHIFT) */
245 if ((((fp
+ FP_LR_OFFSET
) ^ fp_for_ppn
) >> PAGE_SHIFT
) != 0x0U
) {
246 ppn
= pmap_find_phys(pmap
, fp
+ FP_LR_OFFSET
);
247 fp_for_ppn
= fp
+ (is_64_bit
? FP_LR_OFFSET64
: FP_LR_OFFSET
);
249 if (ppn
!= (ppnum_t
)NULL
) {
251 lr
= ml_phys_read_double_64(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | ((fp
+ FP_LR_OFFSET64
) & PAGE_MASK
));
253 lr
= ml_phys_read_word(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | ((fp
+ FP_LR_OFFSET
) & PAGE_MASK
));
257 paniclog_append_noflush("%s\t Could not read LR from frame at 0x%016llx\n", cur_marker
, fp
+ FP_LR_OFFSET64
);
259 paniclog_append_noflush("%s\t Could not read LR from frame at 0x%08x\n", cur_marker
, (uint32_t)(fp
+ FP_LR_OFFSET
));
263 if (((fp
^ fp_for_ppn
) >> PAGE_SHIFT
) != 0x0U
) {
264 ppn
= pmap_find_phys(pmap
, fp
);
267 if (ppn
!= (ppnum_t
)NULL
) {
269 fp
= ml_phys_read_double_64(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | (fp
& PAGE_MASK
));
271 fp
= ml_phys_read_word(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | (fp
& PAGE_MASK
));
275 paniclog_append_noflush("%s\t Could not read FP from frame at 0x%016llx\n", cur_marker
, fp
);
277 paniclog_append_noflush("%s\t Could not read FP from frame at 0x%08x\n", cur_marker
, (uint32_t)fp
);
283 paniclog_append_noflush("%s\t0x%016llx\n", cur_marker
, lr
);
285 paniclog_append_noflush("%s\t0x%08x\n", cur_marker
, (uint32_t)lr
);
287 } while ((++i
< FP_MAX_NUM_TO_EVALUATE
) && (fp
!= topfp
));
290 machine_startup(void)
295 if (PE_get_hotkey( kPEControlKey
)) {
296 halt_in_debugger
= halt_in_debugger
? 0 : 1;
300 if (!PE_parse_boot_argn("nvram_paniclog", &commit_paniclog_to_nvram
, sizeof(commit_paniclog_to_nvram
))) {
301 commit_paniclog_to_nvram
= 1;
305 * Entering the debugger will put the CPUs into a "safe"
308 if (PE_parse_boot_argn("pmsafe_debug", &boot_arg
, sizeof(boot_arg
))) {
309 pmsafe_debug
= boot_arg
;
312 hw_lock_init(&pbtlock
); /* initialize print backtrace lock */
314 if (PE_parse_boot_argn("preempt", &boot_arg
, sizeof(boot_arg
))) {
315 default_preemption_rate
= boot_arg
;
317 if (PE_parse_boot_argn("unsafe", &boot_arg
, sizeof(boot_arg
))) {
318 max_unsafe_quanta
= boot_arg
;
320 if (PE_parse_boot_argn("poll", &boot_arg
, sizeof(boot_arg
))) {
321 max_poll_quanta
= boot_arg
;
323 if (PE_parse_boot_argn("yield", &boot_arg
, sizeof(boot_arg
))) {
324 sched_poll_yield_shift
= boot_arg
;
326 /* The I/O port to issue a read from, in the event of a panic. Useful for
327 * triggering logic analyzers.
329 if (PE_parse_boot_argn("panic_io_port", &boot_arg
, sizeof(boot_arg
))) {
330 /*I/O ports range from 0 through 0xFFFF */
331 panic_io_port
= boot_arg
& 0xffff;
349 machine_info
.memory_size
= (typeof(machine_info
.memory_size
))mem_size
;
352 extern void *gPEEFIRuntimeServices
;
353 extern void *gPEEFISystemTable
;
356 efi_set_tables_64(EFI_SYSTEM_TABLE_64
* system_table
)
358 EFI_RUNTIME_SERVICES_64
*runtime
;
362 DPRINTF("Processing 64-bit EFI tables at %p\n", system_table
);
364 DPRINTF("Header:\n");
365 DPRINTF(" Signature: 0x%016llx\n", system_table
->Hdr
.Signature
);
366 DPRINTF(" Revision: 0x%08x\n", system_table
->Hdr
.Revision
);
367 DPRINTF(" HeaderSize: 0x%08x\n", system_table
->Hdr
.HeaderSize
);
368 DPRINTF(" CRC32: 0x%08x\n", system_table
->Hdr
.CRC32
);
369 DPRINTF("RuntimeServices: 0x%016llx\n", system_table
->RuntimeServices
);
370 if (system_table
->Hdr
.Signature
!= EFI_SYSTEM_TABLE_SIGNATURE
) {
371 kprintf("Bad EFI system table signature\n");
374 // Verify signature of the system table
375 hdr_cksum
= system_table
->Hdr
.CRC32
;
376 system_table
->Hdr
.CRC32
= 0;
377 cksum
= crc32(0L, system_table
, system_table
->Hdr
.HeaderSize
);
379 DPRINTF("System table calculated CRC32 = 0x%x, header = 0x%x\n", cksum
, hdr_cksum
);
380 system_table
->Hdr
.CRC32
= hdr_cksum
;
381 if (cksum
!= hdr_cksum
) {
382 kprintf("Bad EFI system table checksum\n");
386 gPEEFISystemTable
= system_table
;
388 if (system_table
->RuntimeServices
== 0) {
389 kprintf("No runtime table present\n");
392 DPRINTF("RuntimeServices table at 0x%qx\n", system_table
->RuntimeServices
);
393 // 64-bit virtual address is OK for 64-bit EFI and 64/32-bit kernel.
394 runtime
= (EFI_RUNTIME_SERVICES_64
*) (uintptr_t)system_table
->RuntimeServices
;
395 DPRINTF("Checking runtime services table %p\n", runtime
);
396 if (runtime
->Hdr
.Signature
!= EFI_RUNTIME_SERVICES_SIGNATURE
) {
397 kprintf("Bad EFI runtime table signature\n");
401 // Verify signature of runtime services table
402 hdr_cksum
= runtime
->Hdr
.CRC32
;
403 runtime
->Hdr
.CRC32
= 0;
404 cksum
= crc32(0L, runtime
, runtime
->Hdr
.HeaderSize
);
406 DPRINTF("Runtime table calculated CRC32 = 0x%x, header = 0x%x\n", cksum
, hdr_cksum
);
407 runtime
->Hdr
.CRC32
= hdr_cksum
;
408 if (cksum
!= hdr_cksum
) {
409 kprintf("Bad EFI runtime table checksum\n");
413 gPEEFIRuntimeServices
= runtime
;
418 efi_set_tables_32(EFI_SYSTEM_TABLE_32
* system_table
)
420 EFI_RUNTIME_SERVICES_32
*runtime
;
424 DPRINTF("Processing 32-bit EFI tables at %p\n", system_table
);
426 DPRINTF("Header:\n");
427 DPRINTF(" Signature: 0x%016llx\n", system_table
->Hdr
.Signature
);
428 DPRINTF(" Revision: 0x%08x\n", system_table
->Hdr
.Revision
);
429 DPRINTF(" HeaderSize: 0x%08x\n", system_table
->Hdr
.HeaderSize
);
430 DPRINTF(" CRC32: 0x%08x\n", system_table
->Hdr
.CRC32
);
431 DPRINTF("RuntimeServices: 0x%08x\n", system_table
->RuntimeServices
);
432 if (system_table
->Hdr
.Signature
!= EFI_SYSTEM_TABLE_SIGNATURE
) {
433 kprintf("Bad EFI system table signature\n");
436 // Verify signature of the system table
437 hdr_cksum
= system_table
->Hdr
.CRC32
;
438 system_table
->Hdr
.CRC32
= 0;
439 DPRINTF("System table at %p HeaderSize 0x%x\n", system_table
, system_table
->Hdr
.HeaderSize
);
440 cksum
= crc32(0L, system_table
, system_table
->Hdr
.HeaderSize
);
442 DPRINTF("System table calculated CRC32 = 0x%x, header = 0x%x\n", cksum
, hdr_cksum
);
443 system_table
->Hdr
.CRC32
= hdr_cksum
;
444 if (cksum
!= hdr_cksum
) {
445 kprintf("Bad EFI system table checksum\n");
449 gPEEFISystemTable
= system_table
;
451 if (system_table
->RuntimeServices
== 0) {
452 kprintf("No runtime table present\n");
455 DPRINTF("RuntimeServices table at 0x%x\n", system_table
->RuntimeServices
);
456 // 32-bit virtual address is OK for 32-bit EFI and 32-bit kernel.
457 // For a 64-bit kernel, booter provides a virtual address mod 4G
458 runtime
= (EFI_RUNTIME_SERVICES_32
*)
459 (system_table
->RuntimeServices
| VM_MIN_KERNEL_ADDRESS
);
460 DPRINTF("Runtime table addressed at %p\n", runtime
);
461 if (runtime
->Hdr
.Signature
!= EFI_RUNTIME_SERVICES_SIGNATURE
) {
462 kprintf("Bad EFI runtime table signature\n");
466 // Verify signature of runtime services table
467 hdr_cksum
= runtime
->Hdr
.CRC32
;
468 runtime
->Hdr
.CRC32
= 0;
469 cksum
= crc32(0L, runtime
, runtime
->Hdr
.HeaderSize
);
471 DPRINTF("Runtime table calculated CRC32 = 0x%x, header = 0x%x\n", cksum
, hdr_cksum
);
472 runtime
->Hdr
.CRC32
= hdr_cksum
;
473 if (cksum
!= hdr_cksum
) {
474 kprintf("Bad EFI runtime table checksum\n");
478 DPRINTF("Runtime functions\n");
479 DPRINTF(" GetTime : 0x%x\n", runtime
->GetTime
);
480 DPRINTF(" SetTime : 0x%x\n", runtime
->SetTime
);
481 DPRINTF(" GetWakeupTime : 0x%x\n", runtime
->GetWakeupTime
);
482 DPRINTF(" SetWakeupTime : 0x%x\n", runtime
->SetWakeupTime
);
483 DPRINTF(" SetVirtualAddressMap : 0x%x\n", runtime
->SetVirtualAddressMap
);
484 DPRINTF(" ConvertPointer : 0x%x\n", runtime
->ConvertPointer
);
485 DPRINTF(" GetVariable : 0x%x\n", runtime
->GetVariable
);
486 DPRINTF(" GetNextVariableName : 0x%x\n", runtime
->GetNextVariableName
);
487 DPRINTF(" SetVariable : 0x%x\n", runtime
->SetVariable
);
488 DPRINTF(" GetNextHighMonotonicCount: 0x%x\n", runtime
->GetNextHighMonotonicCount
);
489 DPRINTF(" ResetSystem : 0x%x\n", runtime
->ResetSystem
);
491 gPEEFIRuntimeServices
= runtime
;
496 /* Map in EFI runtime areas. */
500 boot_args
*args
= (boot_args
*)PE_state
.bootArgs
;
502 kprintf("Initializing EFI runtime services\n");
505 vm_offset_t vm_size
, vm_addr
;
506 vm_map_offset_t phys_addr
;
507 EfiMemoryRange
*mptr
;
508 unsigned int msize
, mcount
;
511 msize
= args
->MemoryMapDescriptorSize
;
512 mcount
= args
->MemoryMapSize
/ msize
;
514 DPRINTF("efi_init() kernel base: 0x%x size: 0x%x\n",
515 args
->kaddr
, args
->ksize
);
516 DPRINTF(" efiSystemTable physical: 0x%x virtual: %p\n",
517 args
->efiSystemTable
,
518 (void *) ml_static_ptovirt(args
->efiSystemTable
));
519 DPRINTF(" efiRuntimeServicesPageStart: 0x%x\n",
520 args
->efiRuntimeServicesPageStart
);
521 DPRINTF(" efiRuntimeServicesPageCount: 0x%x\n",
522 args
->efiRuntimeServicesPageCount
);
523 DPRINTF(" efiRuntimeServicesVirtualPageStart: 0x%016llx\n",
524 args
->efiRuntimeServicesVirtualPageStart
);
525 mptr
= (EfiMemoryRange
*)ml_static_ptovirt(args
->MemoryMap
);
526 for (i
= 0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
527 if (((mptr
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
)) {
528 vm_size
= (vm_offset_t
)i386_ptob((uint32_t)mptr
->NumberOfPages
);
529 vm_addr
= (vm_offset_t
) mptr
->VirtualStart
;
530 /* For K64 on EFI32, shadow-map into high KVA */
531 if (vm_addr
< VM_MIN_KERNEL_ADDRESS
) {
532 vm_addr
|= VM_MIN_KERNEL_ADDRESS
;
534 phys_addr
= (vm_map_offset_t
) mptr
->PhysicalStart
;
535 DPRINTF(" Type: %x phys: %p EFIv: %p kv: %p size: %p\n",
537 (void *) (uintptr_t) phys_addr
,
538 (void *) (uintptr_t) mptr
->VirtualStart
,
541 pmap_map_bd(vm_addr
, phys_addr
, phys_addr
+ round_page(vm_size
),
542 (mptr
->Type
== kEfiRuntimeServicesCode
) ? VM_PROT_READ
| VM_PROT_EXECUTE
: VM_PROT_READ
| VM_PROT_WRITE
,
543 (mptr
->Type
== EfiMemoryMappedIO
) ? VM_WIMG_IO
: VM_WIMG_USE_DEFAULT
);
547 if (args
->Version
!= kBootArgsVersion2
) {
548 panic("Incompatible boot args version %d revision %d\n", args
->Version
, args
->Revision
);
551 DPRINTF("Boot args version %d revision %d mode %d\n", args
->Version
, args
->Revision
, args
->efiMode
);
552 if (args
->efiMode
== kBootArgsEfiMode64
) {
553 efi_set_tables_64((EFI_SYSTEM_TABLE_64
*) ml_static_ptovirt(args
->efiSystemTable
));
555 efi_set_tables_32((EFI_SYSTEM_TABLE_32
*) ml_static_ptovirt(args
->efiSystemTable
));
562 /* Returns TRUE if a page belongs to the EFI Runtime Services (code or data) */
564 bootloader_valid_page(ppnum_t ppn
)
566 boot_args
*args
= (boot_args
*)PE_state
.bootArgs
;
567 ppnum_t pstart
= args
->efiRuntimeServicesPageStart
;
568 ppnum_t pend
= pstart
+ args
->efiRuntimeServicesPageCount
;
570 return pstart
<= ppn
&& ppn
< pend
;
573 /* Remap EFI runtime areas. */
575 hibernate_newruntime_map(void * map
, vm_size_t map_size
, uint32_t system_table_offset
)
577 boot_args
*args
= (boot_args
*)PE_state
.bootArgs
;
579 kprintf("Reinitializing EFI runtime services\n");
582 vm_offset_t vm_size
, vm_addr
;
583 vm_map_offset_t phys_addr
;
584 EfiMemoryRange
*mptr
;
585 unsigned int msize
, mcount
;
588 gPEEFISystemTable
= 0;
589 gPEEFIRuntimeServices
= 0;
591 system_table_offset
+= ptoa_32(args
->efiRuntimeServicesPageStart
);
593 kprintf("Old system table 0x%x, new 0x%x\n",
594 (uint32_t)args
->efiSystemTable
, system_table_offset
);
596 args
->efiSystemTable
= system_table_offset
;
598 kprintf("Old map:\n");
599 msize
= args
->MemoryMapDescriptorSize
;
600 mcount
= args
->MemoryMapSize
/ msize
;
601 mptr
= (EfiMemoryRange
*)ml_static_ptovirt(args
->MemoryMap
);
602 for (i
= 0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
603 if ((mptr
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
604 vm_size
= (vm_offset_t
)i386_ptob((uint32_t)mptr
->NumberOfPages
);
605 vm_addr
= (vm_offset_t
) mptr
->VirtualStart
;
607 if (vm_addr
< VM_MIN_KERNEL_ADDRESS
) {
608 vm_addr
|= VM_MIN_KERNEL_ADDRESS
;
610 phys_addr
= (vm_map_offset_t
) mptr
->PhysicalStart
;
612 kprintf("mapping[%u] %qx @ %lx, %llu\n", mptr
->Type
, phys_addr
, (unsigned long)vm_addr
, mptr
->NumberOfPages
);
616 pmap_remove(kernel_pmap
, i386_ptob(args
->efiRuntimeServicesPageStart
),
617 i386_ptob(args
->efiRuntimeServicesPageStart
+ args
->efiRuntimeServicesPageCount
));
619 kprintf("New map:\n");
620 msize
= args
->MemoryMapDescriptorSize
;
621 mcount
= (unsigned int)(map_size
/ msize
);
623 for (i
= 0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
624 if ((mptr
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
625 vm_size
= (vm_offset_t
)i386_ptob((uint32_t)mptr
->NumberOfPages
);
626 vm_addr
= (vm_offset_t
) mptr
->VirtualStart
;
627 if (vm_addr
< VM_MIN_KERNEL_ADDRESS
) {
628 vm_addr
|= VM_MIN_KERNEL_ADDRESS
;
630 phys_addr
= (vm_map_offset_t
) mptr
->PhysicalStart
;
632 kprintf("mapping[%u] %qx @ %lx, %llu\n", mptr
->Type
, phys_addr
, (unsigned long)vm_addr
, mptr
->NumberOfPages
);
634 pmap_map(vm_addr
, phys_addr
, phys_addr
+ round_page(vm_size
),
635 (mptr
->Type
== kEfiRuntimeServicesCode
) ? VM_PROT_READ
| VM_PROT_EXECUTE
: VM_PROT_READ
| VM_PROT_WRITE
,
636 (mptr
->Type
== EfiMemoryMappedIO
) ? VM_WIMG_IO
: VM_WIMG_USE_DEFAULT
);
640 if (args
->Version
!= kBootArgsVersion2
) {
641 panic("Incompatible boot args version %d revision %d\n", args
->Version
, args
->Revision
);
644 kprintf("Boot args version %d revision %d mode %d\n", args
->Version
, args
->Revision
, args
->efiMode
);
645 if (args
->efiMode
== kBootArgsEfiMode64
) {
646 efi_set_tables_64((EFI_SYSTEM_TABLE_64
*) ml_static_ptovirt(args
->efiSystemTable
));
648 efi_set_tables_32((EFI_SYSTEM_TABLE_32
*) ml_static_ptovirt(args
->efiSystemTable
));
652 kprintf("Done reinitializing EFI runtime services\n");
658 * Find devices. The system is alive.
663 /* Now with VM up, switch to dynamically allocated cpu data */
666 /* Ensure panic buffer is initialized. */
670 * Display CPU identification
672 cpuid_cpu_display("CPU identification");
673 cpuid_feature_display("CPU features");
674 cpuid_extfeature_display("CPU extended features");
677 * Initialize EFI runtime services.
684 * Set up to use floating point.
689 * Configure clock devices.
695 * Initialize MTRR from boot processor.
700 * Set up PAT for boot processor.
706 * Free lowmem pages and complete other setup
708 pmap_lowmem_finalize();
717 halt_all_cpus(FALSE
);
720 int reset_mem_on_reboot
= 1;
723 * Halt the system or reboot.
725 __attribute__((noreturn
))
727 halt_all_cpus(boolean_t reboot
)
730 printf("MACH Reboot\n");
731 PEHaltRestart( kPERestartCPU
);
733 printf("CPU halted\n");
734 PEHaltRestart( kPEHaltCPU
);
742 /* Issue an I/O port read if one has been requested - this is an event logic
743 * analyzers can use as a trigger point.
747 panic_io_port_read(void)
750 (void)inb(panic_io_port
);
754 /* For use with the MP rendezvous mechanism
757 uint64_t panic_restart_timeout
= ~(0ULL);
759 #define PANIC_RESTART_TIMEOUT (3ULL * NSEC_PER_SEC)
762 * We should always return from this function with the other log offset
763 * set in the panic_info structure.
766 RecordPanicStackshot()
769 size_t bytes_traced
= 0, bytes_used
= 0, bytes_remaining
= 0;
770 char *stackshot_begin_loc
= NULL
;
772 /* Don't re-enter this code if we panic here */
773 if (begun_panic_stackshot
) {
774 if (panic_info
->mph_other_log_offset
== 0) {
775 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
779 begun_panic_stackshot
= TRUE
;
781 /* The panic log length should have been set before we came to capture a stackshot */
782 if (panic_info
->mph_panic_log_len
== 0) {
783 kdb_printf("Found zero length panic log, skipping capturing panic stackshot\n");
784 if (panic_info
->mph_other_log_offset
== 0) {
785 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
790 if (stackshot_active()) {
791 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_FAILED_NESTED
;
792 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
793 kdb_printf("Panicked during stackshot, skipping panic stackshot\n");
797 /* Try to capture an in memory panic_stackshot */
798 if (extended_debug_log_enabled
) {
799 /* On coprocessor systems we write this into the extended debug log */
800 stackshot_begin_loc
= debug_buf_ptr
;
801 bytes_remaining
= debug_buf_size
- (unsigned int)((uintptr_t)stackshot_begin_loc
- (uintptr_t)debug_buf_base
);
802 } else if (panic_stackshot_buf
!= 0) {
803 /* On other systems we use the panic stackshot_buf */
804 stackshot_begin_loc
= (char *) panic_stackshot_buf
;
805 bytes_remaining
= panic_stackshot_buf_len
;
807 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
812 err
= kcdata_memory_static_init(&kc_panic_data
, (mach_vm_address_t
)stackshot_begin_loc
,
813 KCDATA_BUFFER_BEGIN_STACKSHOT
, (unsigned int) bytes_remaining
, KCFLAG_USE_MEMCOPY
);
814 if (err
!= KERN_SUCCESS
) {
815 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_FAILED_ERROR
;
816 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
817 kdb_printf("Failed to initialize kcdata buffer for in-memory panic stackshot, skipping ...\n");
821 uint32_t stackshot_flags
= (STACKSHOT_SAVE_KEXT_LOADINFO
| STACKSHOT_SAVE_LOADINFO
| STACKSHOT_KCDATA_FORMAT
|
822 STACKSHOT_ENABLE_BT_FAULTING
| STACKSHOT_ENABLE_UUID_FAULTING
| STACKSHOT_FROM_PANIC
|
823 STACKSHOT_NO_IO_STATS
| STACKSHOT_THREAD_WAITINFO
);
826 * Include the shared cache layout in panic stackshots on DEVELOPMENT kernels so that we can symbolicate
827 * panic stackshots from corefiles.
829 stackshot_flags
|= STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT
;
832 kdp_snapshot_preflight(-1, (void *) stackshot_begin_loc
, (uint32_t) bytes_remaining
, stackshot_flags
, &kc_panic_data
, 0);
833 err
= do_stackshot(NULL
);
834 bytes_traced
= (int) kdp_stack_snapshot_bytes_traced();
835 bytes_used
= (int) kcdata_memory_get_used_bytes(&kc_panic_data
);
837 if ((err
!= KERN_SUCCESS
) && (bytes_used
> 0)) {
839 * We ran out of space while trying to capture a stackshot, try again without user frames.
840 * It's not safe to log from here (in case we're writing in the middle of the debug buffer on coprocessor systems)
841 * but append a flag to the panic flags.
843 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_KERNEL_ONLY
;
844 panic_stackshot_reset_state();
846 /* Erase the stackshot data (this region is pre-populated with the NULL character) */
847 memset(stackshot_begin_loc
, '\0', bytes_used
);
849 err
= kcdata_memory_static_init(&kc_panic_data
, (mach_vm_address_t
)stackshot_begin_loc
,
850 KCDATA_BUFFER_BEGIN_STACKSHOT
, (unsigned int) bytes_remaining
, KCFLAG_USE_MEMCOPY
);
851 if (err
!= KERN_SUCCESS
) {
852 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_FAILED_ERROR
;
853 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
854 kdb_printf("Failed to re-initialize kcdata buffer for kernel only in-memory panic stackshot, skipping ...\n");
858 stackshot_flags
= (STACKSHOT_SAVE_KEXT_LOADINFO
| STACKSHOT_KCDATA_FORMAT
| STACKSHOT_FROM_PANIC
|
859 STACKSHOT_NO_IO_STATS
| STACKSHOT_THREAD_WAITINFO
| STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY
);
862 * Include the shared cache layout in panic stackshots on DEVELOPMENT kernels so that we can symbolicate
863 * panic stackshots from corefiles.
865 stackshot_flags
|= STACKSHOT_COLLECT_SHAREDCACHE_LAYOUT
;
868 kdp_snapshot_preflight(-1, (void *) stackshot_begin_loc
, (uint32_t) bytes_remaining
, stackshot_flags
, &kc_panic_data
, 0);
869 err
= do_stackshot(NULL
);
870 bytes_traced
= (int) kdp_stack_snapshot_bytes_traced();
871 bytes_used
= (int) kcdata_memory_get_used_bytes(&kc_panic_data
);
874 if (err
== KERN_SUCCESS
) {
875 if (extended_debug_log_enabled
) {
876 debug_buf_ptr
+= bytes_traced
;
878 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_SUCCEEDED
;
879 panic_info
->mph_stackshot_offset
= PE_get_offset_into_panic_region(stackshot_begin_loc
);
880 panic_info
->mph_stackshot_len
= (uint32_t) bytes_traced
;
882 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
883 kdb_printf("\n** In Memory Panic Stackshot Succeeded ** Bytes Traced %zu **\n", bytes_traced
);
885 /* Used by the code that writes the buffer to disk */
886 panic_stackshot_buf
= (vm_offset_t
) stackshot_begin_loc
;
887 panic_stackshot_len
= bytes_traced
;
889 if (!extended_debug_log_enabled
&& (gIOPolledCoreFileMode
== kIOPolledCoreFileModeStackshot
)) {
890 /* System configured to write panic stackshot to disk */
891 kern_dump(KERN_DUMP_STACKSHOT_DISK
);
894 if (bytes_used
> 0) {
895 /* Erase the stackshot data (this region is pre-populated with the NULL character) */
896 memset(stackshot_begin_loc
, '\0', bytes_used
);
897 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_FAILED_INCOMPLETE
;
899 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
900 kdb_printf("\n** In Memory Panic Stackshot Incomplete ** Bytes Filled %zu ** Err %d\n", bytes_used
, err
);
902 bzero(stackshot_begin_loc
, bytes_used
);
903 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_FAILED_ERROR
;
905 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
906 kdb_printf("\n** In Memory Panic Stackshot Failed ** Bytes Traced %zu, err %d\n", bytes_traced
, err
);
915 __unused
const char *message
, void *panic_data
, uint64_t panic_options
)
917 void *stackptr
= NULL
;
918 thread_t thread_to_trace
= (thread_t
) panic_data
;
919 cframe_t synthetic_stack_frame
= { };
920 char *debugger_msg
= NULL
;
921 int cn
= cpu_number();
924 * Issue an I/O port read if one has been requested - this is an event logic
925 * analyzers can use as a trigger point.
927 panic_io_port_read();
929 /* Obtain frame pointer for stack to trace */
930 if (panic_options
& DEBUGGER_INTERNAL_OPTION_THREAD_BACKTRACE
) {
931 if (!mp_kdp_all_cpus_halted()) {
932 debugger_msg
= "Backtracing panicked thread because failed to halt all CPUs\n";
933 } else if (thread_to_trace
== THREAD_NULL
) {
934 debugger_msg
= "Backtracing panicked thread because no thread pointer provided\n";
935 } else if (kvtophys((vm_offset_t
)thread_to_trace
) == 0ULL) {
936 debugger_msg
= "Backtracing panicked thread because unable to access specified thread\n";
937 } else if (thread_to_trace
->kernel_stack
== 0) {
938 debugger_msg
= "Backtracing panicked thread because kernel_stack is NULL for specified thread\n";
939 } else if (kvtophys(STACK_IKS(thread_to_trace
->kernel_stack
) == 0ULL)) {
940 debugger_msg
= "Backtracing panicked thread because unable to access kernel_stack for specified thread\n";
942 debugger_msg
= "Backtracing specified thread\n";
943 /* We construct a synthetic stack frame so we can include the current instruction pointer */
944 synthetic_stack_frame
.prev
= (cframe_t
*)STACK_IKS(thread_to_trace
->kernel_stack
)->k_rbp
;
945 synthetic_stack_frame
.caller
= (uintptr_t) STACK_IKS(thread_to_trace
->kernel_stack
)->k_rip
;
946 stackptr
= (void *) &synthetic_stack_frame
;
950 if (stackptr
== NULL
) {
951 __asm__
volatile ("movq %%rbp, %0" : "=m" (stackptr
));
954 /* Print backtrace - callee is internally synchronized */
955 if (panic_options
& DEBUGGER_OPTION_INITPROC_PANIC
) {
956 /* Special handling of launchd died panics */
957 print_launchd_info();
959 panic_i386_backtrace(stackptr
, ((panic_double_fault_cpu
== cn
) ? 80: 48), debugger_msg
, FALSE
, NULL
);
962 if (panic_options
& DEBUGGER_OPTION_COPROC_INITIATED_PANIC
) {
963 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_COPROC_INITIATED_PANIC
;
966 if (PE_get_offset_into_panic_region(debug_buf_ptr
) < panic_info
->mph_panic_log_offset
) {
967 kdb_printf("Invalid panic log offset found (not properly initialized?): debug_buf_ptr : 0x%p, panic_info: 0x%p mph_panic_log_offset: 0x%x\n",
968 debug_buf_ptr
, panic_info
, panic_info
->mph_panic_log_offset
);
969 panic_info
->mph_panic_log_len
= 0;
971 panic_info
->mph_panic_log_len
= PE_get_offset_into_panic_region(debug_buf_ptr
) - panic_info
->mph_panic_log_offset
;
974 /* Flush the panic log */
975 paniclog_flush_internal(kPaniclogFlushBase
);
977 /* Try to take a panic stackshot */
978 RecordPanicStackshot();
981 * Flush the panic log again with the stackshot or any relevant logging
982 * from when we tried to capture it.
984 paniclog_flush_internal(kPaniclogFlushStackshot
);
988 paniclog_flush_internal(paniclog_flush_type_t variant
)
990 /* Update the other log offset if we've opened the other log */
991 if (panic_info
->mph_other_log_offset
!= 0) {
992 panic_info
->mph_other_log_len
= PE_get_offset_into_panic_region(debug_buf_ptr
) - panic_info
->mph_other_log_offset
;
996 * If we've detected that we're on a co-processor system, we flush the panic log via the kPEPanicSync
997 * panic callbacks, otherwise we flush via nvram (unless that has been disabled).
999 if (coprocessor_paniclog_flush
) {
1000 uint32_t overall_buffer_size
= debug_buf_size
;
1001 uint32_t size_to_flush
= 0, offset_to_flush
= 0;
1002 if (extended_debug_log_enabled
) {
1004 * debug_buf_size for the extended log does not include the length of the header.
1005 * There may be some extra data at the end of the 'basic' log that wouldn't get flushed
1006 * for the non-extended case (this is a concession we make to not shrink the paniclog data
1007 * for non-coprocessor systems that only use the basic log).
1009 overall_buffer_size
= debug_buf_size
+ sizeof(struct macos_panic_header
);
1012 /* Update the CRC */
1013 panic_info
->mph_crc
= crc32(0L, &panic_info
->mph_version
, (overall_buffer_size
- offsetof(struct macos_panic_header
, mph_version
)));
1015 if (variant
== kPaniclogFlushBase
) {
1016 /* Flush the header and base panic log. */
1017 kprintf("Flushing base panic log\n");
1018 size_to_flush
= ROUNDUP((panic_info
->mph_panic_log_offset
+ panic_info
->mph_panic_log_len
), PANIC_FLUSH_BOUNDARY
);
1019 offset_to_flush
= 0;
1020 PESavePanicInfoAction(panic_info
, offset_to_flush
, size_to_flush
);
1021 } else if ((variant
== kPaniclogFlushStackshot
) || (variant
== kPaniclogFlushOtherLog
)) {
1022 if (variant
== kPaniclogFlushStackshot
) {
1024 * We flush the stackshot before flushing the updated header because the stackshot
1025 * can take a while to flush. We want the paniclog header to be as consistent as possible even
1026 * if the stackshot isn't flushed completely. Flush starting from the end of the panic log.
1028 kprintf("Flushing panic log stackshot\n");
1029 offset_to_flush
= ROUNDDOWN((panic_info
->mph_panic_log_offset
+ panic_info
->mph_panic_log_len
), PANIC_FLUSH_BOUNDARY
);
1030 size_to_flush
= ROUNDUP((panic_info
->mph_stackshot_len
+ (panic_info
->mph_stackshot_offset
- offset_to_flush
)), PANIC_FLUSH_BOUNDARY
);
1031 PESavePanicInfoAction(panic_info
, offset_to_flush
, size_to_flush
);
1034 /* Flush the other log -- everything after the stackshot */
1035 kprintf("Flushing panic 'other' log\n");
1036 offset_to_flush
= ROUNDDOWN((panic_info
->mph_stackshot_offset
+ panic_info
->mph_stackshot_len
), PANIC_FLUSH_BOUNDARY
);
1037 size_to_flush
= ROUNDUP((panic_info
->mph_other_log_len
+ (panic_info
->mph_other_log_offset
- offset_to_flush
)), PANIC_FLUSH_BOUNDARY
);
1038 PESavePanicInfoAction(panic_info
, offset_to_flush
, size_to_flush
);
1040 /* Flush the header -- everything before the paniclog */
1041 kprintf("Flushing panic log header\n");
1042 size_to_flush
= ROUNDUP(panic_info
->mph_panic_log_offset
, PANIC_FLUSH_BOUNDARY
);
1043 offset_to_flush
= 0;
1044 PESavePanicInfoAction(panic_info
, offset_to_flush
, size_to_flush
);
1046 } else if (commit_paniclog_to_nvram
) {
1047 assert(debug_buf_size
!= 0);
1048 unsigned int bufpos
;
1049 unsigned long pi_size
= 0;
1055 * Now call the compressor
1056 * XXX Consider using the WKdm compressor in the
1057 * future, rather than just packing - would need to
1058 * be co-ordinated with crashreporter, which decodes
1059 * this post-restart. The compressor should be
1060 * capable of in-place compression.
1062 * Don't include the macOS panic header (for co-processor systems only)
1064 bufpos
= packA(debug_buf_base
, (unsigned int) (debug_buf_ptr
- debug_buf_base
),
1067 * If compression was successful, use the compressed length
1069 pi_size
= bufpos
? bufpos
: (unsigned) (debug_buf_ptr
- debug_buf_base
);
1072 * The following sequence is a workaround for:
1073 * <rdar://problem/5915669> SnowLeopard10A67: AppleEFINVRAM should not invoke
1074 * any routines that use floating point (MMX in this case) when saving panic
1075 * logs to nvram/flash.
1081 * Save panic log to non-volatile store
1082 * Panic info handler must truncate data that is
1083 * too long for this platform.
1084 * This call must save data synchronously,
1085 * since we can subsequently halt the system.
1087 kprintf("Attempting to commit panic log to NVRAM\n");
1088 pi_size
= PESavePanicInfo((unsigned char *)debug_buf_base
,
1089 (uint32_t)pi_size
);
1093 * Uncompress in-place, to permit examination of
1094 * the panic log by debuggers.
1097 unpackA(debug_buf_base
, bufpos
);
1105 /* Called outside of this file to update logging appended to the "other" log */
1106 paniclog_flush_internal(kPaniclogFlushOtherLog
);
1111 machine_boot_info(char *buf
, __unused vm_size_t size
)
1117 /* Routines for address - symbol translation. Not called unless the "keepsyms"
1118 * boot-arg is supplied.
1122 panic_print_macho_symbol_name(kernel_mach_header_t
*mh
, vm_address_t search
, const char *module_name
)
1124 kernel_nlist_t
*sym
= NULL
;
1125 struct load_command
*cmd
;
1126 kernel_segment_command_t
*orig_ts
= NULL
, *orig_le
= NULL
;
1127 struct symtab_command
*orig_st
= NULL
;
1129 char *strings
, *bestsym
= NULL
;
1130 vm_address_t bestaddr
= 0, diff
, curdiff
;
1132 /* Assume that if it's loaded and linked into the kernel, it's a valid Mach-O */
1134 cmd
= (struct load_command
*) &mh
[1];
1135 for (i
= 0; i
< mh
->ncmds
; i
++) {
1136 if (cmd
->cmd
== LC_SEGMENT_KERNEL
) {
1137 kernel_segment_command_t
*orig_sg
= (kernel_segment_command_t
*) cmd
;
1139 if (strncmp(SEG_TEXT
, orig_sg
->segname
,
1140 sizeof(orig_sg
->segname
)) == 0) {
1142 } else if (strncmp(SEG_LINKEDIT
, orig_sg
->segname
,
1143 sizeof(orig_sg
->segname
)) == 0) {
1145 } else if (strncmp("", orig_sg
->segname
,
1146 sizeof(orig_sg
->segname
)) == 0) {
1147 orig_ts
= orig_sg
; /* pre-Lion i386 kexts have a single unnamed segment */
1149 } else if (cmd
->cmd
== LC_SYMTAB
) {
1150 orig_st
= (struct symtab_command
*) cmd
;
1153 cmd
= (struct load_command
*) ((uintptr_t) cmd
+ cmd
->cmdsize
);
1156 if ((orig_ts
== NULL
) || (orig_st
== NULL
) || (orig_le
== NULL
)) {
1160 if ((search
< orig_ts
->vmaddr
) ||
1161 (search
>= orig_ts
->vmaddr
+ orig_ts
->vmsize
)) {
1162 /* search out of range for this mach header */
1166 sym
= (kernel_nlist_t
*)(uintptr_t)(orig_le
->vmaddr
+ orig_st
->symoff
- orig_le
->fileoff
);
1167 strings
= (char *)(uintptr_t)(orig_le
->vmaddr
+ orig_st
->stroff
- orig_le
->fileoff
);
1170 for (i
= 0; i
< orig_st
->nsyms
; i
++) {
1171 if (sym
[i
].n_type
& N_STAB
) {
1175 if (sym
[i
].n_value
<= search
) {
1176 curdiff
= search
- (vm_address_t
)sym
[i
].n_value
;
1177 if (curdiff
< diff
) {
1179 bestaddr
= sym
[i
].n_value
;
1180 bestsym
= strings
+ sym
[i
].n_un
.n_strx
;
1185 if (bestsym
!= NULL
) {
1187 paniclog_append_noflush("%s : %s + 0x%lx", module_name
, bestsym
, (unsigned long)diff
);
1189 paniclog_append_noflush("%s : %s", module_name
, bestsym
);
1196 extern kmod_info_t
* kmod
; /* the list of modules */
1199 panic_print_kmod_symbol_name(vm_address_t search
)
1203 if (gLoadedKextSummaries
== NULL
) {
1206 for (i
= 0; i
< gLoadedKextSummaries
->numSummaries
; ++i
) {
1207 OSKextLoadedKextSummary
*summary
= gLoadedKextSummaries
->summaries
+ i
;
1209 if ((search
>= summary
->address
) &&
1210 (search
< (summary
->address
+ summary
->size
))) {
1211 kernel_mach_header_t
*header
= (kernel_mach_header_t
*)(uintptr_t) summary
->address
;
1212 if (panic_print_macho_symbol_name(header
, search
, summary
->name
) == 0) {
1213 paniclog_append_noflush("%s + %llu", summary
->name
, (unsigned long)search
- summary
->address
);
1221 panic_print_symbol_name(vm_address_t search
)
1223 /* try searching in the kernel */
1224 if (panic_print_macho_symbol_name(&_mh_execute_header
, search
, "mach_kernel") == 0) {
1225 /* that failed, now try to search for the right kext */
1226 panic_print_kmod_symbol_name(search
);
1230 /* Generate a backtrace, given a frame pointer - this routine
1231 * should walk the stack safely. The trace is appended to the panic log
1232 * and conditionally, to the console. If the trace contains kernel module
1233 * addresses, display the module name, load address and dependencies.
1236 #define DUMPFRAMES 32
1237 #define PBT_TIMEOUT_CYCLES (5 * 1000 * 1000 * 1000ULL)
1239 panic_i386_backtrace(void *_frame
, int nframes
, const char *msg
, boolean_t regdump
, x86_saved_state_t
*regs
)
1241 cframe_t
*frame
= (cframe_t
*)_frame
;
1242 vm_offset_t raddrs
[DUMPFRAMES
];
1245 volatile uint32_t *ppbtcnt
= &pbtcnt
;
1246 uint64_t bt_tsc_timeout
;
1247 boolean_t keepsyms
= FALSE
;
1248 int cn
= cpu_number();
1249 boolean_t old_doprnt_hide_pointers
= doprnt_hide_pointers
;
1252 os_atomic_inc(&pbtcnt
, relaxed
);
1253 /* Spin on print backtrace lock, which serializes output
1254 * Continue anyway if a timeout occurs.
1256 hw_lock_to(&pbtlock
, ~0U, LCK_GRP_NULL
);
1260 if (__improbable(doprnt_hide_pointers
== TRUE
)) {
1261 /* If we're called directly, the Debugger() function will not be called,
1262 * so we need to reset the value in here. */
1263 doprnt_hide_pointers
= FALSE
;
1268 PE_parse_boot_argn("keepsyms", &keepsyms
, sizeof(keepsyms
));
1271 paniclog_append_noflush("%s", msg
);
1274 if ((regdump
== TRUE
) && (regs
!= NULL
)) {
1275 x86_saved_state64_t
*ss64p
= saved_state64(regs
);
1276 paniclog_append_noflush(
1277 "RAX: 0x%016llx, RBX: 0x%016llx, RCX: 0x%016llx, RDX: 0x%016llx\n"
1278 "RSP: 0x%016llx, RBP: 0x%016llx, RSI: 0x%016llx, RDI: 0x%016llx\n"
1279 "R8: 0x%016llx, R9: 0x%016llx, R10: 0x%016llx, R11: 0x%016llx\n"
1280 "R12: 0x%016llx, R13: 0x%016llx, R14: 0x%016llx, R15: 0x%016llx\n"
1281 "RFL: 0x%016llx, RIP: 0x%016llx, CS: 0x%016llx, SS: 0x%016llx\n",
1282 ss64p
->rax
, ss64p
->rbx
, ss64p
->rcx
, ss64p
->rdx
,
1283 ss64p
->isf
.rsp
, ss64p
->rbp
, ss64p
->rsi
, ss64p
->rdi
,
1284 ss64p
->r8
, ss64p
->r9
, ss64p
->r10
, ss64p
->r11
,
1285 ss64p
->r12
, ss64p
->r13
, ss64p
->r14
, ss64p
->r15
,
1286 ss64p
->isf
.rflags
, ss64p
->isf
.rip
, ss64p
->isf
.cs
,
1288 PC
= ss64p
->isf
.rip
;
1291 paniclog_append_noflush("Backtrace (CPU %d), "
1292 #if PRINT_ARGS_FROM_STACK_FRAME
1293 "Frame : Return Address (4 potential args on stack)\n", cn
);
1295 "Frame : Return Address\n", cn
);
1298 for (frame_index
= 0; frame_index
< nframes
; frame_index
++) {
1299 vm_offset_t curframep
= (vm_offset_t
) frame
;
1305 if (curframep
& 0x3) {
1306 paniclog_append_noflush("Unaligned frame\n");
1310 if (!kvtophys(curframep
) ||
1311 !kvtophys(curframep
+ sizeof(cframe_t
) - 1)) {
1312 paniclog_append_noflush("No mapping exists for frame pointer\n");
1316 paniclog_append_noflush("%p : 0x%lx ", frame
, frame
->caller
);
1317 if (frame_index
< DUMPFRAMES
) {
1318 raddrs
[frame_index
] = frame
->caller
;
1321 #if PRINT_ARGS_FROM_STACK_FRAME
1322 if (kvtophys((vm_offset_t
)&(frame
->args
[3]))) {
1323 paniclog_append_noflush("(0x%x 0x%x 0x%x 0x%x) ",
1324 frame
->args
[0], frame
->args
[1],
1325 frame
->args
[2], frame
->args
[3]);
1329 /* Display address-symbol translation only if the "keepsyms"
1330 * boot-arg is suppplied, since we unload LINKEDIT otherwise.
1331 * This routine is potentially unsafe; also, function
1332 * boundary identification is unreliable after a strip -x.
1335 panic_print_symbol_name((vm_address_t
)frame
->caller
);
1338 paniclog_append_noflush("\n");
1340 frame
= frame
->prev
;
1343 if (frame_index
>= nframes
) {
1344 paniclog_append_noflush("\tBacktrace continues...\n");
1350 paniclog_append_noflush("Backtrace terminated-invalid frame pointer %p\n", frame
);
1353 /* Identify kernel modules in the backtrace and display their
1354 * load addresses and dependencies. This routine should walk
1355 * the kmod list safely.
1358 kmod_panic_dump((vm_offset_t
*)&raddrs
[0], frame_index
);
1362 kmod_panic_dump(&PC
, 1);
1365 panic_display_system_configuration(FALSE
);
1367 doprnt_hide_pointers
= old_doprnt_hide_pointers
;
1369 /* Release print backtrace lock, to permit other callers in the
1370 * event of panics on multiple processors.
1372 hw_lock_unlock(&pbtlock
);
1373 os_atomic_dec(&pbtcnt
, relaxed
);
1374 /* Wait for other processors to complete output
1375 * Timeout and continue after PBT_TIMEOUT_CYCLES.
1377 bt_tsc_timeout
= rdtsc64() + PBT_TIMEOUT_CYCLES
;
1378 while (*ppbtcnt
&& (rdtsc64() < bt_tsc_timeout
)) {
1384 debug_copyin(pmap_t p
, uint64_t uaddr
, void *dest
, size_t size
)
1387 char *kvaddr
= dest
;
1390 ppnum_t upn
= pmap_find_phys(p
, uaddr
);
1391 uint64_t phys_src
= ptoa_64(upn
) | (uaddr
& PAGE_MASK
);
1392 uint64_t phys_dest
= kvtophys((vm_offset_t
)kvaddr
);
1393 uint64_t src_rem
= PAGE_SIZE
- (phys_src
& PAGE_MASK
);
1394 uint64_t dst_rem
= PAGE_SIZE
- (phys_dest
& PAGE_MASK
);
1395 size_t cur_size
= (uint32_t) MIN(src_rem
, dst_rem
);
1396 cur_size
= MIN(cur_size
, rem
);
1398 if (upn
&& pmap_valid_page(upn
) && phys_dest
) {
1399 bcopy_phys(phys_src
, phys_dest
, cur_size
);
1411 print_threads_registers(thread_t thread
)
1413 x86_saved_state_t
*savestate
;
1415 savestate
= get_user_regs(thread
);
1416 paniclog_append_noflush(
1417 "\nRAX: 0x%016llx, RBX: 0x%016llx, RCX: 0x%016llx, RDX: 0x%016llx\n"
1418 "RSP: 0x%016llx, RBP: 0x%016llx, RSI: 0x%016llx, RDI: 0x%016llx\n"
1419 "R8: 0x%016llx, R9: 0x%016llx, R10: 0x%016llx, R11: 0x%016llx\n"
1420 "R12: 0x%016llx, R13: 0x%016llx, R14: 0x%016llx, R15: 0x%016llx\n"
1421 "RFL: 0x%016llx, RIP: 0x%016llx, CS: 0x%016llx, SS: 0x%016llx\n\n",
1422 savestate
->ss_64
.rax
, savestate
->ss_64
.rbx
, savestate
->ss_64
.rcx
, savestate
->ss_64
.rdx
,
1423 savestate
->ss_64
.isf
.rsp
, savestate
->ss_64
.rbp
, savestate
->ss_64
.rsi
, savestate
->ss_64
.rdi
,
1424 savestate
->ss_64
.r8
, savestate
->ss_64
.r9
, savestate
->ss_64
.r10
, savestate
->ss_64
.r11
,
1425 savestate
->ss_64
.r12
, savestate
->ss_64
.r13
, savestate
->ss_64
.r14
, savestate
->ss_64
.r15
,
1426 savestate
->ss_64
.isf
.rflags
, savestate
->ss_64
.isf
.rip
, savestate
->ss_64
.isf
.cs
,
1427 savestate
->ss_64
.isf
.ss
);
1431 print_tasks_user_threads(task_t task
)
1433 thread_t thread
= current_thread();
1434 x86_saved_state_t
*savestate
;
1437 const char *cur_marker
= 0;
1440 for (j
= 0, thread
= (thread_t
) queue_first(&task
->threads
); j
< task
->thread_count
;
1441 ++j
, thread
= (thread_t
) queue_next(&thread
->task_threads
)) {
1442 paniclog_append_noflush("Thread %d: %p\n", j
, thread
);
1443 pmap
= get_task_pmap(task
);
1444 savestate
= get_user_regs(thread
);
1445 rbp
= savestate
->ss_64
.rbp
;
1446 paniclog_append_noflush("\t0x%016llx\n", savestate
->ss_64
.isf
.rip
);
1447 print_one_backtrace(pmap
, (vm_offset_t
)rbp
, cur_marker
, TRUE
);
1448 paniclog_append_noflush("\n");
1453 print_thread_num_that_crashed(task_t task
)
1455 thread_t c_thread
= current_thread();
1459 for (j
= 0, thread
= (thread_t
) queue_first(&task
->threads
); j
< task
->thread_count
;
1460 ++j
, thread
= (thread_t
) queue_next(&thread
->task_threads
)) {
1461 if (c_thread
== thread
) {
1462 paniclog_append_noflush("\nThread %d crashed\n", j
);
1468 #define PANICLOG_UUID_BUF_SIZE 256
1471 print_uuid_info(task_t task
)
1473 uint32_t uuid_info_count
= 0;
1474 mach_vm_address_t uuid_info_addr
= 0;
1475 boolean_t have_map
= (task
->map
!= NULL
) && (ml_validate_nofault((vm_offset_t
)(task
->map
), sizeof(struct _vm_map
)));
1476 boolean_t have_pmap
= have_map
&& (task
->map
->pmap
!= NULL
) && (ml_validate_nofault((vm_offset_t
)(task
->map
->pmap
), sizeof(struct pmap
)));
1477 int task_pid
= pid_from_task(task
);
1478 char uuidbuf
[PANICLOG_UUID_BUF_SIZE
] = {0};
1479 char *uuidbufptr
= uuidbuf
;
1482 if (have_pmap
&& task
->active
&& task_pid
> 0) {
1483 /* Read dyld_all_image_infos struct from task memory to get UUID array count & location */
1484 struct user64_dyld_all_image_infos task_image_infos
;
1485 if (debug_copyin(task
->map
->pmap
, task
->all_image_info_addr
,
1486 &task_image_infos
, sizeof(struct user64_dyld_all_image_infos
))) {
1487 uuid_info_count
= (uint32_t)task_image_infos
.uuidArrayCount
;
1488 uuid_info_addr
= task_image_infos
.uuidArray
;
1491 /* If we get a NULL uuid_info_addr (which can happen when we catch dyld
1492 * in the middle of updating this data structure), we zero the
1493 * uuid_info_count so that we won't even try to save load info for this task
1495 if (!uuid_info_addr
) {
1496 uuid_info_count
= 0;
1500 if (task_pid
> 0 && uuid_info_count
> 0) {
1501 uint32_t uuid_info_size
= sizeof(struct user64_dyld_uuid_info
);
1502 uint32_t uuid_array_size
= uuid_info_count
* uuid_info_size
;
1503 uint32_t uuid_copy_size
= 0;
1504 uint32_t uuid_image_count
= 0;
1505 char *current_uuid_buffer
= NULL
;
1506 /* Copy in the UUID info array. It may be nonresident, in which case just fix up nloadinfos to 0 */
1508 paniclog_append_noflush("\nuuid info:\n");
1509 while (uuid_array_size
) {
1510 if (uuid_array_size
<= PANICLOG_UUID_BUF_SIZE
) {
1511 uuid_copy_size
= uuid_array_size
;
1512 uuid_image_count
= uuid_array_size
/ uuid_info_size
;
1514 uuid_image_count
= PANICLOG_UUID_BUF_SIZE
/ uuid_info_size
;
1515 uuid_copy_size
= uuid_image_count
* uuid_info_size
;
1517 if (have_pmap
&& !debug_copyin(task
->map
->pmap
, uuid_info_addr
, uuidbufptr
,
1519 paniclog_append_noflush("Error!! Failed to copy UUID info for task %p pid %d\n", task
, task_pid
);
1520 uuid_image_count
= 0;
1524 if (uuid_image_count
> 0) {
1525 current_uuid_buffer
= uuidbufptr
;
1526 for (k
= 0; k
< uuid_image_count
; k
++) {
1527 paniclog_append_noflush(" %#llx", *(uint64_t *)current_uuid_buffer
);
1528 current_uuid_buffer
+= sizeof(uint64_t);
1529 uint8_t *uuid
= (uint8_t *)current_uuid_buffer
;
1530 paniclog_append_noflush("\tuuid = <%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x>\n",
1531 uuid
[0], uuid
[1], uuid
[2], uuid
[3], uuid
[4], uuid
[5], uuid
[6], uuid
[7], uuid
[8],
1532 uuid
[9], uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1533 current_uuid_buffer
+= 16;
1535 bzero(&uuidbuf
, sizeof(uuidbuf
));
1537 uuid_info_addr
+= uuid_copy_size
;
1538 uuid_array_size
-= uuid_copy_size
;
1544 print_launchd_info(void)
1546 task_t task
= current_task();
1547 thread_t thread
= current_thread();
1548 volatile uint32_t *ppbtcnt
= &pbtcnt
;
1549 uint64_t bt_tsc_timeout
;
1550 int cn
= cpu_number();
1553 os_atomic_inc(&pbtcnt
, relaxed
);
1554 /* Spin on print backtrace lock, which serializes output
1555 * Continue anyway if a timeout occurs.
1557 hw_lock_to(&pbtlock
, ~0U, LCK_GRP_NULL
);
1561 print_uuid_info(task
);
1562 print_thread_num_that_crashed(task
);
1563 print_threads_registers(thread
);
1564 print_tasks_user_threads(task
);
1566 panic_display_system_configuration(TRUE
);
1568 /* Release print backtrace lock, to permit other callers in the
1569 * event of panics on multiple processors.
1571 hw_lock_unlock(&pbtlock
);
1572 os_atomic_dec(&pbtcnt
, relaxed
);
1573 /* Wait for other processors to complete output
1574 * Timeout and continue after PBT_TIMEOUT_CYCLES.
1576 bt_tsc_timeout
= rdtsc64() + PBT_TIMEOUT_CYCLES
;
1577 while (*ppbtcnt
&& (rdtsc64() < bt_tsc_timeout
)) {