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/misc_protos.h>
85 #include <kern/startup.h>
86 #include <kern/clock.h>
87 #include <kern/cpu_data.h>
88 #include <kern/machine.h>
89 #include <i386/postcode.h>
90 #include <i386/mp_desc.h>
91 #include <i386/misc_protos.h>
92 #include <i386/thread.h>
93 #include <i386/trap.h>
94 #include <i386/machine_routines.h>
95 #include <i386/mp.h> /* mp_rendezvous_break_lock */
96 #include <i386/cpuid.h>
98 #include <i386/machine_cpu.h>
99 #include <i386/pmap.h>
101 #include <i386/mtrr.h>
103 #include <i386/ucode.h>
104 #include <i386/pmCPU.h>
105 #include <i386/panic_hooks.h>
107 #include <architecture/i386/pio.h> /* inb() */
108 #include <pexpert/i386/boot.h>
110 #include <kdp/kdp_dyld.h>
111 #include <kdp/kdp_core.h>
113 #include <vm/vm_map.h>
114 #include <vm/vm_kern.h>
116 #include <IOKit/IOPlatformExpert.h>
117 #include <IOKit/IOHibernatePrivate.h>
119 #include <pexpert/i386/efi.h>
121 #include <kern/thread.h>
122 #include <kern/sched.h>
123 #include <mach-o/loader.h>
124 #include <mach-o/nlist.h>
126 #include <libkern/kernel_mach_header.h>
127 #include <libkern/OSKextLibPrivate.h>
128 #include <libkern/crc.h>
130 #if DEBUG || DEVELOPMENT
131 #define DPRINTF(x...) kprintf(x)
133 #define DPRINTF(x...)
137 #define ROUNDUP(a, b) (((a) + ((b) - 1)) & (~((b) - 1)))
141 #define ROUNDDOWN(x,y) (((x)/(y))*(y))
144 static void machine_conf(void);
145 void panic_print_symbol_name(vm_address_t search
);
146 void RecordPanicStackshot(void);
148 typedef enum paniclog_flush_type
{
149 kPaniclogFlushBase
= 1, /* Flush the initial log and paniclog header */
150 kPaniclogFlushStackshot
= 2, /* Flush only the stackshot data, then flush the header */
151 kPaniclogFlushOtherLog
= 3 /* Flush the other log, then flush the header */
152 } paniclog_flush_type_t
;
154 void paniclog_flush_internal(paniclog_flush_type_t variant
);
156 extern const char version
[];
157 extern char osversion
[];
158 extern int max_unsafe_quanta
;
159 extern int max_poll_quanta
;
160 extern unsigned int panic_is_inited
;
162 extern int proc_pid(void *p
);
164 /* Definitions for frame pointers */
165 #define FP_ALIGNMENT_MASK ((uint32_t)(0x3))
166 #define FP_LR_OFFSET ((uint32_t)4)
167 #define FP_LR_OFFSET64 ((uint32_t)8)
168 #define FP_MAX_NUM_TO_EVALUATE (50)
170 volatile int pbtcpu
= -1;
171 hw_lock_data_t pbtlock
; /* backtrace print lock */
174 volatile int panic_double_fault_cpu
= -1;
176 #define PRINT_ARGS_FROM_STACK_FRAME 0
178 typedef struct _cframe_t
{
179 struct _cframe_t
*prev
;
181 #if PRINT_ARGS_FROM_STACK_FRAME
186 static unsigned panic_io_port
;
187 static unsigned commit_paniclog_to_nvram
;
188 boolean_t coprocessor_paniclog_flush
= FALSE
;
190 struct kcdata_descriptor kc_panic_data
;
191 static boolean_t begun_panic_stackshot
= FALSE
;
192 extern kern_return_t
do_stackshot(void *);
194 extern void kdp_snapshot_preflight(int pid
, void *tracebuf
,
195 uint32_t tracebuf_size
, uint32_t flags
,
196 kcdata_descriptor_t data_p
,
197 boolean_t enable_faulting
);
198 extern int kdp_stack_snapshot_bytes_traced(void);
200 #if DEVELOPMENT || DEBUG
201 vm_offset_t panic_stackshot_buf
= 0;
202 size_t panic_stackshot_len
= 0;
206 * Backtrace a single frame.
209 print_one_backtrace(pmap_t pmap
, vm_offset_t topfp
, const char *cur_marker
,
217 boolean_t dump_kernel_stack
;
223 if (fp
>= VM_MIN_KERNEL_ADDRESS
)
224 dump_kernel_stack
= TRUE
;
226 dump_kernel_stack
= FALSE
;
229 if ((fp
== 0) || ((fp
& FP_ALIGNMENT_MASK
) != 0))
231 if (dump_kernel_stack
&& ((fp
< VM_MIN_KERNEL_ADDRESS
) || (fp
> VM_MAX_KERNEL_ADDRESS
)))
233 if ((!dump_kernel_stack
) && (fp
>=VM_MIN_KERNEL_ADDRESS
))
236 /* Check to see if current address will result in a different
237 ppn than previously computed (to avoid recomputation) via
238 (addr) ^ fp_for_ppn) >> PAGE_SHIFT) */
240 if ((((fp
+ FP_LR_OFFSET
) ^ fp_for_ppn
) >> PAGE_SHIFT
) != 0x0U
) {
241 ppn
= pmap_find_phys(pmap
, fp
+ FP_LR_OFFSET
);
242 fp_for_ppn
= fp
+ (is_64_bit
? FP_LR_OFFSET64
: FP_LR_OFFSET
);
244 if (ppn
!= (ppnum_t
)NULL
) {
246 lr
= ml_phys_read_double_64(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | ((fp
+ FP_LR_OFFSET64
) & PAGE_MASK
));
248 lr
= ml_phys_read_word(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | ((fp
+ FP_LR_OFFSET
) & PAGE_MASK
));
252 paniclog_append_noflush("%s\t Could not read LR from frame at 0x%016llx\n", cur_marker
, fp
+ FP_LR_OFFSET64
);
254 paniclog_append_noflush("%s\t Could not read LR from frame at 0x%08x\n", cur_marker
, (uint32_t)(fp
+ FP_LR_OFFSET
));
258 if (((fp
^ fp_for_ppn
) >> PAGE_SHIFT
) != 0x0U
) {
259 ppn
= pmap_find_phys(pmap
, fp
);
262 if (ppn
!= (ppnum_t
)NULL
) {
264 fp
= ml_phys_read_double_64(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | (fp
& PAGE_MASK
));
266 fp
= ml_phys_read_word(((((vm_offset_t
)ppn
) << PAGE_SHIFT
)) | (fp
& PAGE_MASK
));
270 paniclog_append_noflush("%s\t Could not read FP from frame at 0x%016llx\n", cur_marker
, fp
);
272 paniclog_append_noflush("%s\t Could not read FP from frame at 0x%08x\n", cur_marker
, (uint32_t)fp
);
278 paniclog_append_noflush("%s\t0x%016llx\n", cur_marker
, lr
);
280 paniclog_append_noflush("%s\t0x%08x\n", cur_marker
, (uint32_t)lr
);
282 } while ((++i
< FP_MAX_NUM_TO_EVALUATE
) && (fp
!= topfp
));
285 machine_startup(void)
290 if( PE_get_hotkey( kPEControlKey
))
291 halt_in_debugger
= halt_in_debugger
? 0 : 1;
294 if (!PE_parse_boot_argn("nvram_paniclog", &commit_paniclog_to_nvram
, sizeof (commit_paniclog_to_nvram
)))
295 commit_paniclog_to_nvram
= 1;
298 * Entering the debugger will put the CPUs into a "safe"
301 if (PE_parse_boot_argn("pmsafe_debug", &boot_arg
, sizeof (boot_arg
)))
302 pmsafe_debug
= boot_arg
;
304 hw_lock_init(&pbtlock
); /* initialize print backtrace lock */
306 if (PE_parse_boot_argn("preempt", &boot_arg
, sizeof (boot_arg
))) {
307 default_preemption_rate
= boot_arg
;
309 if (PE_parse_boot_argn("unsafe", &boot_arg
, sizeof (boot_arg
))) {
310 max_unsafe_quanta
= boot_arg
;
312 if (PE_parse_boot_argn("poll", &boot_arg
, sizeof (boot_arg
))) {
313 max_poll_quanta
= boot_arg
;
315 if (PE_parse_boot_argn("yield", &boot_arg
, sizeof (boot_arg
))) {
316 sched_poll_yield_shift
= boot_arg
;
318 /* The I/O port to issue a read from, in the event of a panic. Useful for
319 * triggering logic analyzers.
321 if (PE_parse_boot_argn("panic_io_port", &boot_arg
, sizeof (boot_arg
))) {
322 /*I/O ports range from 0 through 0xFFFF */
323 panic_io_port
= boot_arg
& 0xffff;
341 machine_info
.memory_size
= (typeof(machine_info
.memory_size
))mem_size
;
344 extern void *gPEEFIRuntimeServices
;
345 extern void *gPEEFISystemTable
;
348 efi_set_tables_64(EFI_SYSTEM_TABLE_64
* system_table
)
350 EFI_RUNTIME_SERVICES_64
*runtime
;
354 DPRINTF("Processing 64-bit EFI tables at %p\n", system_table
);
356 DPRINTF("Header:\n");
357 DPRINTF(" Signature: 0x%016llx\n", system_table
->Hdr
.Signature
);
358 DPRINTF(" Revision: 0x%08x\n", system_table
->Hdr
.Revision
);
359 DPRINTF(" HeaderSize: 0x%08x\n", system_table
->Hdr
.HeaderSize
);
360 DPRINTF(" CRC32: 0x%08x\n", system_table
->Hdr
.CRC32
);
361 DPRINTF("RuntimeServices: 0x%016llx\n", system_table
->RuntimeServices
);
362 if (system_table
->Hdr
.Signature
!= EFI_SYSTEM_TABLE_SIGNATURE
) {
363 kprintf("Bad EFI system table signature\n");
366 // Verify signature of the system table
367 hdr_cksum
= system_table
->Hdr
.CRC32
;
368 system_table
->Hdr
.CRC32
= 0;
369 cksum
= crc32(0L, system_table
, system_table
->Hdr
.HeaderSize
);
371 DPRINTF("System table calculated CRC32 = 0x%x, header = 0x%x\n", cksum
, hdr_cksum
);
372 system_table
->Hdr
.CRC32
= hdr_cksum
;
373 if (cksum
!= hdr_cksum
) {
374 kprintf("Bad EFI system table checksum\n");
378 gPEEFISystemTable
= system_table
;
380 if(system_table
->RuntimeServices
== 0) {
381 kprintf("No runtime table present\n");
384 DPRINTF("RuntimeServices table at 0x%qx\n", system_table
->RuntimeServices
);
385 // 64-bit virtual address is OK for 64-bit EFI and 64/32-bit kernel.
386 runtime
= (EFI_RUNTIME_SERVICES_64
*) (uintptr_t)system_table
->RuntimeServices
;
387 DPRINTF("Checking runtime services table %p\n", runtime
);
388 if (runtime
->Hdr
.Signature
!= EFI_RUNTIME_SERVICES_SIGNATURE
) {
389 kprintf("Bad EFI runtime table signature\n");
393 // Verify signature of runtime services table
394 hdr_cksum
= runtime
->Hdr
.CRC32
;
395 runtime
->Hdr
.CRC32
= 0;
396 cksum
= crc32(0L, runtime
, runtime
->Hdr
.HeaderSize
);
398 DPRINTF("Runtime table calculated CRC32 = 0x%x, header = 0x%x\n", cksum
, hdr_cksum
);
399 runtime
->Hdr
.CRC32
= hdr_cksum
;
400 if (cksum
!= hdr_cksum
) {
401 kprintf("Bad EFI runtime table checksum\n");
405 gPEEFIRuntimeServices
= runtime
;
411 efi_set_tables_32(EFI_SYSTEM_TABLE_32
* system_table
)
413 EFI_RUNTIME_SERVICES_32
*runtime
;
417 DPRINTF("Processing 32-bit EFI tables at %p\n", system_table
);
419 DPRINTF("Header:\n");
420 DPRINTF(" Signature: 0x%016llx\n", system_table
->Hdr
.Signature
);
421 DPRINTF(" Revision: 0x%08x\n", system_table
->Hdr
.Revision
);
422 DPRINTF(" HeaderSize: 0x%08x\n", system_table
->Hdr
.HeaderSize
);
423 DPRINTF(" CRC32: 0x%08x\n", system_table
->Hdr
.CRC32
);
424 DPRINTF("RuntimeServices: 0x%08x\n", system_table
->RuntimeServices
);
425 if (system_table
->Hdr
.Signature
!= EFI_SYSTEM_TABLE_SIGNATURE
) {
426 kprintf("Bad EFI system table signature\n");
429 // Verify signature of the system table
430 hdr_cksum
= system_table
->Hdr
.CRC32
;
431 system_table
->Hdr
.CRC32
= 0;
432 DPRINTF("System table at %p HeaderSize 0x%x\n", system_table
, system_table
->Hdr
.HeaderSize
);
433 cksum
= crc32(0L, system_table
, system_table
->Hdr
.HeaderSize
);
435 DPRINTF("System table calculated CRC32 = 0x%x, header = 0x%x\n", cksum
, hdr_cksum
);
436 system_table
->Hdr
.CRC32
= hdr_cksum
;
437 if (cksum
!= hdr_cksum
) {
438 kprintf("Bad EFI system table checksum\n");
442 gPEEFISystemTable
= system_table
;
444 if(system_table
->RuntimeServices
== 0) {
445 kprintf("No runtime table present\n");
448 DPRINTF("RuntimeServices table at 0x%x\n", system_table
->RuntimeServices
);
449 // 32-bit virtual address is OK for 32-bit EFI and 32-bit kernel.
450 // For a 64-bit kernel, booter provides a virtual address mod 4G
451 runtime
= (EFI_RUNTIME_SERVICES_32
*)
452 (system_table
->RuntimeServices
| VM_MIN_KERNEL_ADDRESS
);
453 DPRINTF("Runtime table addressed at %p\n", runtime
);
454 if (runtime
->Hdr
.Signature
!= EFI_RUNTIME_SERVICES_SIGNATURE
) {
455 kprintf("Bad EFI runtime table signature\n");
459 // Verify signature of runtime services table
460 hdr_cksum
= runtime
->Hdr
.CRC32
;
461 runtime
->Hdr
.CRC32
= 0;
462 cksum
= crc32(0L, runtime
, runtime
->Hdr
.HeaderSize
);
464 DPRINTF("Runtime table calculated CRC32 = 0x%x, header = 0x%x\n", cksum
, hdr_cksum
);
465 runtime
->Hdr
.CRC32
= hdr_cksum
;
466 if (cksum
!= hdr_cksum
) {
467 kprintf("Bad EFI runtime table checksum\n");
471 DPRINTF("Runtime functions\n");
472 DPRINTF(" GetTime : 0x%x\n", runtime
->GetTime
);
473 DPRINTF(" SetTime : 0x%x\n", runtime
->SetTime
);
474 DPRINTF(" GetWakeupTime : 0x%x\n", runtime
->GetWakeupTime
);
475 DPRINTF(" SetWakeupTime : 0x%x\n", runtime
->SetWakeupTime
);
476 DPRINTF(" SetVirtualAddressMap : 0x%x\n", runtime
->SetVirtualAddressMap
);
477 DPRINTF(" ConvertPointer : 0x%x\n", runtime
->ConvertPointer
);
478 DPRINTF(" GetVariable : 0x%x\n", runtime
->GetVariable
);
479 DPRINTF(" GetNextVariableName : 0x%x\n", runtime
->GetNextVariableName
);
480 DPRINTF(" SetVariable : 0x%x\n", runtime
->SetVariable
);
481 DPRINTF(" GetNextHighMonotonicCount: 0x%x\n", runtime
->GetNextHighMonotonicCount
);
482 DPRINTF(" ResetSystem : 0x%x\n", runtime
->ResetSystem
);
484 gPEEFIRuntimeServices
= runtime
;
490 /* Map in EFI runtime areas. */
494 boot_args
*args
= (boot_args
*)PE_state
.bootArgs
;
496 kprintf("Initializing EFI runtime services\n");
500 vm_offset_t vm_size
, vm_addr
;
501 vm_map_offset_t phys_addr
;
502 EfiMemoryRange
*mptr
;
503 unsigned int msize
, mcount
;
506 msize
= args
->MemoryMapDescriptorSize
;
507 mcount
= args
->MemoryMapSize
/ msize
;
509 DPRINTF("efi_init() kernel base: 0x%x size: 0x%x\n",
510 args
->kaddr
, args
->ksize
);
511 DPRINTF(" efiSystemTable physical: 0x%x virtual: %p\n",
512 args
->efiSystemTable
,
513 (void *) ml_static_ptovirt(args
->efiSystemTable
));
514 DPRINTF(" efiRuntimeServicesPageStart: 0x%x\n",
515 args
->efiRuntimeServicesPageStart
);
516 DPRINTF(" efiRuntimeServicesPageCount: 0x%x\n",
517 args
->efiRuntimeServicesPageCount
);
518 DPRINTF(" efiRuntimeServicesVirtualPageStart: 0x%016llx\n",
519 args
->efiRuntimeServicesVirtualPageStart
);
520 mptr
= (EfiMemoryRange
*)ml_static_ptovirt(args
->MemoryMap
);
521 for (i
=0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
522 if (((mptr
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) ) {
523 vm_size
= (vm_offset_t
)i386_ptob((uint32_t)mptr
->NumberOfPages
);
524 vm_addr
= (vm_offset_t
) mptr
->VirtualStart
;
525 /* For K64 on EFI32, shadow-map into high KVA */
526 if (vm_addr
< VM_MIN_KERNEL_ADDRESS
)
527 vm_addr
|= VM_MIN_KERNEL_ADDRESS
;
528 phys_addr
= (vm_map_offset_t
) mptr
->PhysicalStart
;
529 DPRINTF(" Type: %x phys: %p EFIv: %p kv: %p size: %p\n",
531 (void *) (uintptr_t) phys_addr
,
532 (void *) (uintptr_t) mptr
->VirtualStart
,
535 pmap_map_bd(vm_addr
, phys_addr
, phys_addr
+ round_page(vm_size
),
536 (mptr
->Type
== kEfiRuntimeServicesCode
) ? VM_PROT_READ
| VM_PROT_EXECUTE
: VM_PROT_READ
|VM_PROT_WRITE
,
537 (mptr
->Type
== EfiMemoryMappedIO
) ? VM_WIMG_IO
: VM_WIMG_USE_DEFAULT
);
541 if (args
->Version
!= kBootArgsVersion2
)
542 panic("Incompatible boot args version %d revision %d\n", args
->Version
, args
->Revision
);
544 DPRINTF("Boot args version %d revision %d mode %d\n", args
->Version
, args
->Revision
, args
->efiMode
);
545 if (args
->efiMode
== kBootArgsEfiMode64
) {
546 efi_set_tables_64((EFI_SYSTEM_TABLE_64
*) ml_static_ptovirt(args
->efiSystemTable
));
548 efi_set_tables_32((EFI_SYSTEM_TABLE_32
*) ml_static_ptovirt(args
->efiSystemTable
));
556 /* Returns TRUE if a page belongs to the EFI Runtime Services (code or data) */
558 efi_valid_page(ppnum_t ppn
)
560 boot_args
*args
= (boot_args
*)PE_state
.bootArgs
;
561 ppnum_t pstart
= args
->efiRuntimeServicesPageStart
;
562 ppnum_t pend
= pstart
+ args
->efiRuntimeServicesPageCount
;
564 return pstart
<= ppn
&& ppn
< pend
;
567 /* Remap EFI runtime areas. */
569 hibernate_newruntime_map(void * map
, vm_size_t map_size
, uint32_t system_table_offset
)
571 boot_args
*args
= (boot_args
*)PE_state
.bootArgs
;
573 kprintf("Reinitializing EFI runtime services\n");
577 vm_offset_t vm_size
, vm_addr
;
578 vm_map_offset_t phys_addr
;
579 EfiMemoryRange
*mptr
;
580 unsigned int msize
, mcount
;
583 gPEEFISystemTable
= 0;
584 gPEEFIRuntimeServices
= 0;
586 system_table_offset
+= ptoa_32(args
->efiRuntimeServicesPageStart
);
588 kprintf("Old system table 0x%x, new 0x%x\n",
589 (uint32_t)args
->efiSystemTable
, system_table_offset
);
591 args
->efiSystemTable
= system_table_offset
;
593 kprintf("Old map:\n");
594 msize
= args
->MemoryMapDescriptorSize
;
595 mcount
= args
->MemoryMapSize
/ msize
;
596 mptr
= (EfiMemoryRange
*)ml_static_ptovirt(args
->MemoryMap
);
597 for (i
=0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
598 if ((mptr
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
600 vm_size
= (vm_offset_t
)i386_ptob((uint32_t)mptr
->NumberOfPages
);
601 vm_addr
= (vm_offset_t
) mptr
->VirtualStart
;
603 if (vm_addr
< VM_MIN_KERNEL_ADDRESS
)
604 vm_addr
|= VM_MIN_KERNEL_ADDRESS
;
605 phys_addr
= (vm_map_offset_t
) mptr
->PhysicalStart
;
607 kprintf("mapping[%u] %qx @ %lx, %llu\n", mptr
->Type
, phys_addr
, (unsigned long)vm_addr
, mptr
->NumberOfPages
);
611 pmap_remove(kernel_pmap
, i386_ptob(args
->efiRuntimeServicesPageStart
),
612 i386_ptob(args
->efiRuntimeServicesPageStart
+ args
->efiRuntimeServicesPageCount
));
614 kprintf("New map:\n");
615 msize
= args
->MemoryMapDescriptorSize
;
616 mcount
= (unsigned int )(map_size
/ msize
);
618 for (i
=0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
619 if ((mptr
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
621 vm_size
= (vm_offset_t
)i386_ptob((uint32_t)mptr
->NumberOfPages
);
622 vm_addr
= (vm_offset_t
) mptr
->VirtualStart
;
623 if (vm_addr
< VM_MIN_KERNEL_ADDRESS
)
624 vm_addr
|= VM_MIN_KERNEL_ADDRESS
;
625 phys_addr
= (vm_map_offset_t
) mptr
->PhysicalStart
;
627 kprintf("mapping[%u] %qx @ %lx, %llu\n", mptr
->Type
, phys_addr
, (unsigned long)vm_addr
, mptr
->NumberOfPages
);
629 pmap_map(vm_addr
, phys_addr
, phys_addr
+ round_page(vm_size
),
630 (mptr
->Type
== kEfiRuntimeServicesCode
) ? VM_PROT_READ
| VM_PROT_EXECUTE
: VM_PROT_READ
|VM_PROT_WRITE
,
631 (mptr
->Type
== EfiMemoryMappedIO
) ? VM_WIMG_IO
: VM_WIMG_USE_DEFAULT
);
635 if (args
->Version
!= kBootArgsVersion2
)
636 panic("Incompatible boot args version %d revision %d\n", args
->Version
, args
->Revision
);
638 kprintf("Boot args version %d revision %d mode %d\n", args
->Version
, args
->Revision
, args
->efiMode
);
639 if (args
->efiMode
== kBootArgsEfiMode64
) {
640 efi_set_tables_64((EFI_SYSTEM_TABLE_64
*) ml_static_ptovirt(args
->efiSystemTable
));
642 efi_set_tables_32((EFI_SYSTEM_TABLE_32
*) ml_static_ptovirt(args
->efiSystemTable
));
647 kprintf("Done reinitializing EFI runtime services\n");
653 * Find devices. The system is alive.
658 /* Now with VM up, switch to dynamically allocated cpu data */
661 /* Ensure panic buffer is initialized. */
665 * Display CPU identification
667 cpuid_cpu_display("CPU identification");
668 cpuid_feature_display("CPU features");
669 cpuid_extfeature_display("CPU extended features");
672 * Initialize EFI runtime services.
679 * Set up to use floating point.
684 * Configure clock devices.
690 * Initialize MTRR from boot processor.
695 * Set up PAT for boot processor.
701 * Free lowmem pages and complete other setup
703 pmap_lowmem_finalize();
712 halt_all_cpus(FALSE
);
715 int reset_mem_on_reboot
= 1;
718 * Halt the system or reboot.
720 __attribute__((noreturn
))
722 halt_all_cpus(boolean_t reboot
)
725 printf("MACH Reboot\n");
726 PEHaltRestart( kPERestartCPU
);
728 printf("CPU halted\n");
729 PEHaltRestart( kPEHaltCPU
);
735 /* Issue an I/O port read if one has been requested - this is an event logic
736 * analyzers can use as a trigger point.
740 panic_io_port_read(void) {
742 (void)inb(panic_io_port
);
745 /* For use with the MP rendezvous mechanism
748 uint64_t panic_restart_timeout
= ~(0ULL);
750 #define PANIC_RESTART_TIMEOUT (3ULL * NSEC_PER_SEC)
753 * We should always return from this function with the other log offset
754 * set in the panic_info structure.
757 RecordPanicStackshot()
759 int err
= 0, bytes_traced
= 0, bytes_used
= 0, bytes_remaining
= 0;
760 char *stackshot_begin_loc
= NULL
;
762 /* Don't re-enter this code if we panic here */
763 if (begun_panic_stackshot
) {
764 if (panic_info
->mph_other_log_offset
== 0) {
765 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
769 begun_panic_stackshot
= TRUE
;
771 /* The panic log length should have been set before we came to capture a stackshot */
772 if (panic_info
->mph_panic_log_len
== 0) {
773 kdb_printf("Found zero length panic log, skipping capturing panic stackshot\n");
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
);
781 * Try to capture an in memory panic_stackshot (enabled during boot
782 * on systems with co-processors).
784 if (extended_debug_log_enabled
) {
785 if (stackshot_active()) {
786 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_FAILED_NESTED
;
787 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
788 kdb_printf("Panicked during stackshot, skipping panic stackshot\n");
791 stackshot_begin_loc
= debug_buf_ptr
;
793 bytes_remaining
= debug_buf_size
- (unsigned int)((uintptr_t)stackshot_begin_loc
- (uintptr_t)debug_buf_base
);
794 err
= kcdata_memory_static_init(&kc_panic_data
, (mach_vm_address_t
)stackshot_begin_loc
,
795 KCDATA_BUFFER_BEGIN_STACKSHOT
, bytes_remaining
, KCFLAG_USE_MEMCOPY
);
796 if (err
!= KERN_SUCCESS
) {
797 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_FAILED_ERROR
;
798 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
799 kdb_printf("Failed to initialize kcdata buffer for in-memory panic stackshot, skipping ...\n");
803 kdp_snapshot_preflight(-1, (void *) stackshot_begin_loc
, bytes_remaining
,
804 (STACKSHOT_SAVE_KEXT_LOADINFO
| STACKSHOT_SAVE_LOADINFO
| STACKSHOT_KCDATA_FORMAT
|
805 STACKSHOT_ENABLE_BT_FAULTING
| STACKSHOT_ENABLE_UUID_FAULTING
| STACKSHOT_FROM_PANIC
|
806 STACKSHOT_NO_IO_STATS
| STACKSHOT_THREAD_WAITINFO
), &kc_panic_data
, 0);
807 err
= do_stackshot(NULL
);
808 bytes_traced
= (int) kdp_stack_snapshot_bytes_traced();
809 bytes_used
= (int) kcdata_memory_get_used_bytes(&kc_panic_data
);
811 if ((err
!= KERN_SUCCESS
) && (bytes_used
> 0)) {
813 * We ran out of space while trying to capture a stackshot, try again without user frames.
814 * It's not safe to log from here, but append a flag to the panic flags.
816 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_KERNEL_ONLY
;
817 panic_stackshot_reset_state();
819 /* Erase the stackshot data (this region is pre-populated with the NULL character) */
820 memset(stackshot_begin_loc
, '\0', bytes_used
);
822 err
= kcdata_memory_static_init(&kc_panic_data
, (mach_vm_address_t
)stackshot_begin_loc
,
823 KCDATA_BUFFER_BEGIN_STACKSHOT
, bytes_remaining
, KCFLAG_USE_MEMCOPY
);
824 if (err
!= KERN_SUCCESS
) {
825 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_FAILED_ERROR
;
826 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
827 kdb_printf("Failed to re-initialize kcdata buffer for kernel only in-memory panic stackshot, skipping ...\n");
831 kdp_snapshot_preflight(-1, (void *) stackshot_begin_loc
, bytes_remaining
, (STACKSHOT_KCDATA_FORMAT
|
832 STACKSHOT_NO_IO_STATS
| STACKSHOT_SAVE_KEXT_LOADINFO
| STACKSHOT_ACTIVE_KERNEL_THREADS_ONLY
|
833 STACKSHOT_FROM_PANIC
| STACKSHOT_THREAD_WAITINFO
), &kc_panic_data
, 0);
834 err
= do_stackshot(NULL
);
835 bytes_traced
= (int) kdp_stack_snapshot_bytes_traced();
836 bytes_used
= (int) kcdata_memory_get_used_bytes(&kc_panic_data
);
839 if (err
== KERN_SUCCESS
) {
840 debug_buf_ptr
+= bytes_traced
;
841 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_SUCCEEDED
;
842 panic_info
->mph_stackshot_offset
= PE_get_offset_into_panic_region(stackshot_begin_loc
);
843 panic_info
->mph_stackshot_len
= bytes_traced
;
845 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
846 kdb_printf("\n** In Memory Panic Stackshot Succeeded ** Bytes Traced %d **\n", bytes_traced
);
848 if (bytes_used
> 0) {
849 /* Erase the stackshot data (this region is pre-populated with the NULL character) */
850 memset(stackshot_begin_loc
, '\0', bytes_used
);
851 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_FAILED_INCOMPLETE
;
853 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
854 kdb_printf("\n** In Memory Panic Stackshot Incomplete ** Bytes Filled %d ** Err %d\n", bytes_used
, err
);
856 bzero(stackshot_begin_loc
, bytes_used
);
857 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_STACKSHOT_FAILED_ERROR
;
859 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
860 kdb_printf("\n** In Memory Panic Stackshot Failed ** Bytes Traced %d, err %d\n", bytes_traced
, err
);
864 #if DEVELOPMENT || DEBUG
865 if (panic_stackshot_buf
!= 0) {
866 /* We're going to try to take another stackshot, reset the state. */
867 panic_stackshot_reset_state();
869 #endif /* DEVELOPMENT || DEBUG */
871 panic_info
->mph_other_log_offset
= PE_get_offset_into_panic_region(debug_buf_ptr
);
874 #if DEVELOPMENT || DEBUG
876 if (panic_stackshot_buf
== 0) {
877 kdb_printf("No stackshot buffer allocated for file backed panic stackshot, skipping...\n");
881 if (stackshot_active()) {
882 kdb_printf("Panicked during stackshot, skipping file backed panic stackshot\n");
886 err
= kcdata_memory_static_init(&kc_panic_data
, (mach_vm_address_t
)panic_stackshot_buf
, KCDATA_BUFFER_BEGIN_STACKSHOT
,
887 PANIC_STACKSHOT_BUFSIZE
, KCFLAG_USE_MEMCOPY
);
888 if (err
!= KERN_SUCCESS
) {
889 kdb_printf("Failed to initialize kcdata buffer for file backed panic stackshot, skipping ...\n");
893 kdp_snapshot_preflight(-1, (void *) panic_stackshot_buf
, PANIC_STACKSHOT_BUFSIZE
, (STACKSHOT_GET_GLOBAL_MEM_STATS
| STACKSHOT_SAVE_LOADINFO
| STACKSHOT_KCDATA_FORMAT
|
894 STACKSHOT_ENABLE_BT_FAULTING
| STACKSHOT_ENABLE_UUID_FAULTING
| STACKSHOT_FROM_PANIC
| STACKSHOT_NO_IO_STATS
895 | STACKSHOT_THREAD_WAITINFO
), &kc_panic_data
, 0);
896 err
= do_stackshot(NULL
);
897 bytes_traced
= (int) kdp_stack_snapshot_bytes_traced();
898 if (bytes_traced
> 0 && !err
) {
899 panic_stackshot_len
= bytes_traced
;
900 kdb_printf("File backed panic stackshot succeeded, length: %u bytes\n", bytes_traced
);
902 bytes_used
= (int) kcdata_memory_get_used_bytes(&kc_panic_data
);
903 if (bytes_used
> 0) {
904 kdb_printf("File backed panic stackshot incomplete, consumed %u bytes, error : %d \n", bytes_used
, err
);
906 kdb_printf("File backed panic stackshot incomplete, consumed %u bytes, error : %d \n", bytes_used
, err
);
909 #endif /* DEVELOPMENT || DEBUG */
916 __unused
const char *message
, void *panic_data
, uint64_t panic_options
)
918 void *stackptr
= NULL
;
919 thread_t thread_to_trace
= (thread_t
) panic_data
;
920 cframe_t synthetic_stack_frame
= { };
921 char *debugger_msg
= NULL
;
922 int cn
= cpu_number();
925 * Issue an I/O port read if one has been requested - this is an event logic
926 * analyzers can use as a trigger point.
928 panic_io_port_read();
930 /* Obtain frame pointer for stack to trace */
931 if (panic_options
& DEBUGGER_INTERNAL_OPTION_THREAD_BACKTRACE
) {
932 if (!mp_kdp_all_cpus_halted()) {
933 debugger_msg
= "Backtracing panicked thread because failed to halt all CPUs\n";
934 } else if (thread_to_trace
== THREAD_NULL
) {
935 debugger_msg
= "Backtracing panicked thread because no thread pointer provided\n";
936 } else if (kvtophys((vm_offset_t
)thread_to_trace
) == 0ULL) {
937 debugger_msg
= "Backtracing panicked thread because unable to access specified thread\n";
938 } else if (thread_to_trace
->kernel_stack
== 0) {
939 debugger_msg
= "Backtracing panicked thread because kernel_stack is NULL for specified thread\n";
940 } else if (kvtophys(STACK_IKS(thread_to_trace
->kernel_stack
) == 0ULL)) {
941 debugger_msg
= "Backtracing panicked thread because unable to access kernel_stack for specified thread\n";
943 debugger_msg
= "Backtracing specified thread\n";
944 /* We construct a synthetic stack frame so we can include the current instruction pointer */
945 synthetic_stack_frame
.prev
= (cframe_t
*)STACK_IKS(thread_to_trace
->kernel_stack
)->k_rbp
;
946 synthetic_stack_frame
.caller
= (uintptr_t) STACK_IKS(thread_to_trace
->kernel_stack
)->k_rip
;
947 stackptr
= (void *) &synthetic_stack_frame
;
951 if (stackptr
== NULL
) {
952 __asm__
volatile("movq %%rbp, %0" : "=m" (stackptr
));
955 /* Print backtrace - callee is internally synchronized */
956 if (panic_options
& DEBUGGER_OPTION_INITPROC_PANIC
) {
957 /* Special handling of launchd died panics */
958 print_launchd_info();
960 panic_i386_backtrace(stackptr
, ((panic_double_fault_cpu
== cn
) ? 80: 48), debugger_msg
, FALSE
, NULL
);
963 if (panic_options
& DEBUGGER_OPTION_COPROC_INITIATED_PANIC
) {
964 panic_info
->mph_panic_flags
|= MACOS_PANIC_HEADER_FLAG_COPROC_INITIATED_PANIC
;
967 if (PE_get_offset_into_panic_region(debug_buf_ptr
) < panic_info
->mph_panic_log_offset
) {
968 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",
969 debug_buf_ptr
, panic_info
, panic_info
->mph_panic_log_offset
);
970 panic_info
->mph_panic_log_len
= 0;
972 panic_info
->mph_panic_log_len
= PE_get_offset_into_panic_region(debug_buf_ptr
) - panic_info
->mph_panic_log_offset
;
975 /* Flush the panic log */
976 paniclog_flush_internal(kPaniclogFlushBase
);
978 /* Try to take a panic stackshot */
979 RecordPanicStackshot();
982 * Flush the panic log again with the stackshot or any relevant logging
983 * from when we tried to capture it.
985 if (extended_debug_log_enabled
) {
986 paniclog_flush_internal(kPaniclogFlushStackshot
);
990 void paniclog_flush_internal(paniclog_flush_type_t variant
)
992 /* Update the other log offset if we've opened the other log */
993 if (panic_info
->mph_other_log_offset
!= 0) {
994 panic_info
->mph_other_log_len
= PE_get_offset_into_panic_region(debug_buf_ptr
) - panic_info
->mph_other_log_offset
;
998 * If we've detected that we're on a co-processor system, we flush the panic log via the kPEPanicSync
999 * panic callbacks, otherwise we flush via nvram (unless that has been disabled).
1001 if (coprocessor_paniclog_flush
) {
1002 uint32_t overall_buffer_size
= debug_buf_size
;
1003 uint32_t size_to_flush
= 0, offset_to_flush
= 0;
1004 if (extended_debug_log_enabled
) {
1006 * debug_buf_size for the extended log does not include the length of the header.
1007 * There may be some extra data at the end of the 'basic' log that wouldn't get flushed
1008 * for the non-extended case (this is a concession we make to not shrink the paniclog data
1009 * for non-coprocessor systems that only use the basic log).
1011 overall_buffer_size
= debug_buf_size
+ sizeof(struct macos_panic_header
);
1014 /* Update the CRC */
1015 panic_info
->mph_crc
= crc32(0L, &panic_info
->mph_version
, (overall_buffer_size
- offsetof(struct macos_panic_header
, mph_version
)));
1017 if (variant
== kPaniclogFlushBase
) {
1018 /* Flush the header and base panic log. */
1019 kprintf("Flushing base panic log\n");
1020 size_to_flush
= ROUNDUP((panic_info
->mph_panic_log_offset
+ panic_info
->mph_panic_log_len
), PANIC_FLUSH_BOUNDARY
);
1021 offset_to_flush
= 0;
1022 PESavePanicInfoAction(panic_info
, offset_to_flush
, size_to_flush
);
1023 } else if ((variant
== kPaniclogFlushStackshot
) || (variant
== kPaniclogFlushOtherLog
)) {
1024 if (variant
== kPaniclogFlushStackshot
) {
1026 * We flush the stackshot before flushing the updated header because the stackshot
1027 * can take a while to flush. We want the paniclog header to be as consistent as possible even
1028 * if the stackshot isn't flushed completely. Flush starting from the end of the panic log.
1030 kprintf("Flushing panic log stackshot\n");
1031 offset_to_flush
= ROUNDDOWN((panic_info
->mph_panic_log_offset
+ panic_info
->mph_panic_log_len
), PANIC_FLUSH_BOUNDARY
);
1032 size_to_flush
= ROUNDUP((panic_info
->mph_stackshot_len
+ (panic_info
->mph_stackshot_offset
- offset_to_flush
)), PANIC_FLUSH_BOUNDARY
);
1033 PESavePanicInfoAction(panic_info
, offset_to_flush
, size_to_flush
);
1036 /* Flush the other log -- everything after the stackshot */
1037 kprintf("Flushing panic 'other' log\n");
1038 offset_to_flush
= ROUNDDOWN((panic_info
->mph_stackshot_offset
+ panic_info
->mph_stackshot_len
), PANIC_FLUSH_BOUNDARY
);
1039 size_to_flush
= ROUNDUP((panic_info
->mph_other_log_len
+ (panic_info
->mph_other_log_offset
- offset_to_flush
)), PANIC_FLUSH_BOUNDARY
);
1040 PESavePanicInfoAction(panic_info
, offset_to_flush
, size_to_flush
);
1042 /* Flush the header -- everything before the paniclog */
1043 kprintf("Flushing panic log header\n");
1044 size_to_flush
= ROUNDUP(panic_info
->mph_panic_log_offset
, PANIC_FLUSH_BOUNDARY
);
1045 offset_to_flush
= 0;
1046 PESavePanicInfoAction(panic_info
, offset_to_flush
, size_to_flush
);
1048 } else if (commit_paniclog_to_nvram
) {
1049 assert(debug_buf_size
!= 0);
1050 unsigned int bufpos
;
1051 unsigned long pi_size
= 0;
1057 * Now call the compressor
1058 * XXX Consider using the WKdm compressor in the
1059 * future, rather than just packing - would need to
1060 * be co-ordinated with crashreporter, which decodes
1061 * this post-restart. The compressor should be
1062 * capable of in-place compression.
1064 * Don't include the macOS panic header (for co-processor systems only)
1066 bufpos
= packA(debug_buf_base
, (unsigned int) (debug_buf_ptr
- debug_buf_base
),
1069 * If compression was successful, use the compressed length
1071 pi_size
= bufpos
? bufpos
: (unsigned) (debug_buf_ptr
- debug_buf_base
);
1074 * The following sequence is a workaround for:
1075 * <rdar://problem/5915669> SnowLeopard10A67: AppleEFINVRAM should not invoke
1076 * any routines that use floating point (MMX in this case) when saving panic
1077 * logs to nvram/flash.
1083 * Save panic log to non-volatile store
1084 * Panic info handler must truncate data that is
1085 * too long for this platform.
1086 * This call must save data synchronously,
1087 * since we can subsequently halt the system.
1089 kprintf("Attempting to commit panic log to NVRAM\n");
1090 pi_size
= PESavePanicInfo((unsigned char *)debug_buf_base
,
1091 (uint32_t)pi_size
);
1095 * Uncompress in-place, to permit examination of
1096 * the panic log by debuggers.
1099 unpackA(debug_buf_base
, bufpos
);
1107 /* Called outside of this file to update logging appended to the "other" log */
1108 paniclog_flush_internal(kPaniclogFlushOtherLog
);
1113 machine_boot_info(char *buf
, __unused vm_size_t size
)
1119 /* Routines for address - symbol translation. Not called unless the "keepsyms"
1120 * boot-arg is supplied.
1124 panic_print_macho_symbol_name(kernel_mach_header_t
*mh
, vm_address_t search
, const char *module_name
)
1126 kernel_nlist_t
*sym
= NULL
;
1127 struct load_command
*cmd
;
1128 kernel_segment_command_t
*orig_ts
= NULL
, *orig_le
= NULL
;
1129 struct symtab_command
*orig_st
= NULL
;
1131 char *strings
, *bestsym
= NULL
;
1132 vm_address_t bestaddr
= 0, diff
, curdiff
;
1134 /* Assume that if it's loaded and linked into the kernel, it's a valid Mach-O */
1136 cmd
= (struct load_command
*) &mh
[1];
1137 for (i
= 0; i
< mh
->ncmds
; i
++) {
1138 if (cmd
->cmd
== LC_SEGMENT_KERNEL
) {
1139 kernel_segment_command_t
*orig_sg
= (kernel_segment_command_t
*) cmd
;
1141 if (strncmp(SEG_TEXT
, orig_sg
->segname
,
1142 sizeof(orig_sg
->segname
)) == 0)
1144 else if (strncmp(SEG_LINKEDIT
, orig_sg
->segname
,
1145 sizeof(orig_sg
->segname
)) == 0)
1147 else if (strncmp("", orig_sg
->segname
,
1148 sizeof(orig_sg
->segname
)) == 0)
1149 orig_ts
= orig_sg
; /* pre-Lion i386 kexts have a single unnamed segment */
1151 else if (cmd
->cmd
== LC_SYMTAB
)
1152 orig_st
= (struct symtab_command
*) cmd
;
1154 cmd
= (struct load_command
*) ((uintptr_t) cmd
+ cmd
->cmdsize
);
1157 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
) continue;
1173 if (sym
[i
].n_value
<= search
) {
1174 curdiff
= search
- (vm_address_t
)sym
[i
].n_value
;
1175 if (curdiff
< diff
) {
1177 bestaddr
= sym
[i
].n_value
;
1178 bestsym
= strings
+ sym
[i
].n_un
.n_strx
;
1183 if (bestsym
!= NULL
) {
1185 paniclog_append_noflush("%s : %s + 0x%lx", module_name
, bestsym
, (unsigned long)diff
);
1187 paniclog_append_noflush("%s : %s", module_name
, bestsym
);
1194 extern kmod_info_t
* kmod
; /* the list of modules */
1197 panic_print_kmod_symbol_name(vm_address_t search
)
1201 if (gLoadedKextSummaries
== NULL
)
1203 for (i
= 0; i
< gLoadedKextSummaries
->numSummaries
; ++i
) {
1204 OSKextLoadedKextSummary
*summary
= gLoadedKextSummaries
->summaries
+ i
;
1206 if ((search
>= summary
->address
) &&
1207 (search
< (summary
->address
+ summary
->size
)))
1209 kernel_mach_header_t
*header
= (kernel_mach_header_t
*)(uintptr_t) summary
->address
;
1210 if (panic_print_macho_symbol_name(header
, search
, summary
->name
) == 0) {
1211 paniclog_append_noflush("%s + %llu", summary
->name
, (unsigned long)search
- summary
->address
);
1219 panic_print_symbol_name(vm_address_t search
)
1221 /* try searching in the kernel */
1222 if (panic_print_macho_symbol_name(&_mh_execute_header
, search
, "mach_kernel") == 0) {
1223 /* that failed, now try to search for the right kext */
1224 panic_print_kmod_symbol_name(search
);
1228 /* Generate a backtrace, given a frame pointer - this routine
1229 * should walk the stack safely. The trace is appended to the panic log
1230 * and conditionally, to the console. If the trace contains kernel module
1231 * addresses, display the module name, load address and dependencies.
1234 #define DUMPFRAMES 32
1235 #define PBT_TIMEOUT_CYCLES (5 * 1000 * 1000 * 1000ULL)
1237 panic_i386_backtrace(void *_frame
, int nframes
, const char *msg
, boolean_t regdump
, x86_saved_state_t
*regs
)
1239 cframe_t
*frame
= (cframe_t
*)_frame
;
1240 vm_offset_t raddrs
[DUMPFRAMES
];
1243 volatile uint32_t *ppbtcnt
= &pbtcnt
;
1244 uint64_t bt_tsc_timeout
;
1245 boolean_t keepsyms
= FALSE
;
1246 int cn
= cpu_number();
1247 boolean_t old_doprnt_hide_pointers
= doprnt_hide_pointers
;
1250 hw_atomic_add(&pbtcnt
, 1);
1251 /* Spin on print backtrace lock, which serializes output
1252 * Continue anyway if a timeout occurs.
1254 hw_lock_to(&pbtlock
, ~0U);
1258 if (__improbable(doprnt_hide_pointers
== TRUE
)) {
1259 /* If we're called directly, the Debugger() function will not be called,
1260 * so we need to reset the value in here. */
1261 doprnt_hide_pointers
= FALSE
;
1266 PE_parse_boot_argn("keepsyms", &keepsyms
, sizeof (keepsyms
));
1269 paniclog_append_noflush("%s", msg
);
1272 if ((regdump
== TRUE
) && (regs
!= NULL
)) {
1273 x86_saved_state64_t
*ss64p
= saved_state64(regs
);
1274 paniclog_append_noflush(
1275 "RAX: 0x%016llx, RBX: 0x%016llx, RCX: 0x%016llx, RDX: 0x%016llx\n"
1276 "RSP: 0x%016llx, RBP: 0x%016llx, RSI: 0x%016llx, RDI: 0x%016llx\n"
1277 "R8: 0x%016llx, R9: 0x%016llx, R10: 0x%016llx, R11: 0x%016llx\n"
1278 "R12: 0x%016llx, R13: 0x%016llx, R14: 0x%016llx, R15: 0x%016llx\n"
1279 "RFL: 0x%016llx, RIP: 0x%016llx, CS: 0x%016llx, SS: 0x%016llx\n",
1280 ss64p
->rax
, ss64p
->rbx
, ss64p
->rcx
, ss64p
->rdx
,
1281 ss64p
->isf
.rsp
, ss64p
->rbp
, ss64p
->rsi
, ss64p
->rdi
,
1282 ss64p
->r8
, ss64p
->r9
, ss64p
->r10
, ss64p
->r11
,
1283 ss64p
->r12
, ss64p
->r13
, ss64p
->r14
, ss64p
->r15
,
1284 ss64p
->isf
.rflags
, ss64p
->isf
.rip
, ss64p
->isf
.cs
,
1286 PC
= ss64p
->isf
.rip
;
1289 paniclog_append_noflush("Backtrace (CPU %d), "
1290 #if PRINT_ARGS_FROM_STACK_FRAME
1291 "Frame : Return Address (4 potential args on stack)\n", cn
);
1293 "Frame : Return Address\n", cn
);
1296 for (frame_index
= 0; frame_index
< nframes
; frame_index
++) {
1297 vm_offset_t curframep
= (vm_offset_t
) frame
;
1302 if (curframep
& 0x3) {
1303 paniclog_append_noflush("Unaligned frame\n");
1307 if (!kvtophys(curframep
) ||
1308 !kvtophys(curframep
+ sizeof(cframe_t
) - 1)) {
1309 paniclog_append_noflush("No mapping exists for frame pointer\n");
1313 paniclog_append_noflush("%p : 0x%lx ", frame
, frame
->caller
);
1314 if (frame_index
< DUMPFRAMES
)
1315 raddrs
[frame_index
] = frame
->caller
;
1317 #if PRINT_ARGS_FROM_STACK_FRAME
1318 if (kvtophys((vm_offset_t
)&(frame
->args
[3])))
1319 paniclog_append_noflush("(0x%x 0x%x 0x%x 0x%x) ",
1320 frame
->args
[0], frame
->args
[1],
1321 frame
->args
[2], frame
->args
[3]);
1324 /* Display address-symbol translation only if the "keepsyms"
1325 * boot-arg is suppplied, since we unload LINKEDIT otherwise.
1326 * This routine is potentially unsafe; also, function
1327 * boundary identification is unreliable after a strip -x.
1330 panic_print_symbol_name((vm_address_t
)frame
->caller
);
1332 paniclog_append_noflush("\n");
1334 frame
= frame
->prev
;
1337 if (frame_index
>= nframes
)
1338 paniclog_append_noflush("\tBacktrace continues...\n");
1343 paniclog_append_noflush("Backtrace terminated-invalid frame pointer %p\n",frame
);
1346 /* Identify kernel modules in the backtrace and display their
1347 * load addresses and dependencies. This routine should walk
1348 * the kmod list safely.
1351 kmod_panic_dump((vm_offset_t
*)&raddrs
[0], frame_index
);
1354 kmod_panic_dump(&PC
, 1);
1356 panic_display_system_configuration(FALSE
);
1358 doprnt_hide_pointers
= old_doprnt_hide_pointers
;
1360 /* Release print backtrace lock, to permit other callers in the
1361 * event of panics on multiple processors.
1363 hw_lock_unlock(&pbtlock
);
1364 hw_atomic_sub(&pbtcnt
, 1);
1365 /* Wait for other processors to complete output
1366 * Timeout and continue after PBT_TIMEOUT_CYCLES.
1368 bt_tsc_timeout
= rdtsc64() + PBT_TIMEOUT_CYCLES
;
1369 while(*ppbtcnt
&& (rdtsc64() < bt_tsc_timeout
));
1373 debug_copyin(pmap_t p
, uint64_t uaddr
, void *dest
, size_t size
)
1376 char *kvaddr
= dest
;
1379 ppnum_t upn
= pmap_find_phys(p
, uaddr
);
1380 uint64_t phys_src
= ptoa_64(upn
) | (uaddr
& PAGE_MASK
);
1381 uint64_t phys_dest
= kvtophys((vm_offset_t
)kvaddr
);
1382 uint64_t src_rem
= PAGE_SIZE
- (phys_src
& PAGE_MASK
);
1383 uint64_t dst_rem
= PAGE_SIZE
- (phys_dest
& PAGE_MASK
);
1384 size_t cur_size
= (uint32_t) MIN(src_rem
, dst_rem
);
1385 cur_size
= MIN(cur_size
, rem
);
1387 if (upn
&& pmap_valid_page(upn
) && phys_dest
) {
1388 bcopy_phys(phys_src
, phys_dest
, cur_size
);
1400 print_threads_registers(thread_t thread
)
1402 x86_saved_state_t
*savestate
;
1404 savestate
= get_user_regs(thread
);
1405 paniclog_append_noflush(
1406 "\nRAX: 0x%016llx, RBX: 0x%016llx, RCX: 0x%016llx, RDX: 0x%016llx\n"
1407 "RSP: 0x%016llx, RBP: 0x%016llx, RSI: 0x%016llx, RDI: 0x%016llx\n"
1408 "R8: 0x%016llx, R9: 0x%016llx, R10: 0x%016llx, R11: 0x%016llx\n"
1409 "R12: 0x%016llx, R13: 0x%016llx, R14: 0x%016llx, R15: 0x%016llx\n"
1410 "RFL: 0x%016llx, RIP: 0x%016llx, CS: 0x%016llx, SS: 0x%016llx\n\n",
1411 savestate
->ss_64
.rax
, savestate
->ss_64
.rbx
, savestate
->ss_64
.rcx
, savestate
->ss_64
.rdx
,
1412 savestate
->ss_64
.isf
.rsp
, savestate
->ss_64
.rbp
, savestate
->ss_64
.rsi
, savestate
->ss_64
.rdi
,
1413 savestate
->ss_64
.r8
, savestate
->ss_64
.r9
, savestate
->ss_64
.r10
, savestate
->ss_64
.r11
,
1414 savestate
->ss_64
.r12
, savestate
->ss_64
.r13
, savestate
->ss_64
.r14
, savestate
->ss_64
.r15
,
1415 savestate
->ss_64
.isf
.rflags
, savestate
->ss_64
.isf
.rip
, savestate
->ss_64
.isf
.cs
,
1416 savestate
->ss_64
.isf
.ss
);
1420 print_tasks_user_threads(task_t task
)
1422 thread_t thread
= current_thread();
1423 x86_saved_state_t
*savestate
;
1426 const char *cur_marker
= 0;
1429 for (j
= 0, thread
= (thread_t
) queue_first(&task
->threads
); j
< task
->thread_count
;
1430 ++j
, thread
= (thread_t
) queue_next(&thread
->task_threads
)) {
1432 paniclog_append_noflush("Thread %d: %p\n", j
, thread
);
1433 pmap
= get_task_pmap(task
);
1434 savestate
= get_user_regs(thread
);
1435 rbp
= savestate
->ss_64
.rbp
;
1436 paniclog_append_noflush("\t0x%016llx\n", savestate
->ss_64
.isf
.rip
);
1437 print_one_backtrace(pmap
, (vm_offset_t
)rbp
, cur_marker
, TRUE
);
1438 paniclog_append_noflush("\n");
1443 print_thread_num_that_crashed(task_t task
)
1445 thread_t c_thread
= current_thread();
1449 for (j
= 0, thread
= (thread_t
) queue_first(&task
->threads
); j
< task
->thread_count
;
1450 ++j
, thread
= (thread_t
) queue_next(&thread
->task_threads
)) {
1452 if (c_thread
== thread
) {
1453 paniclog_append_noflush("\nThread %d crashed\n", j
);
1459 #define PANICLOG_UUID_BUF_SIZE 256
1461 void print_uuid_info(task_t task
)
1463 uint32_t uuid_info_count
= 0;
1464 mach_vm_address_t uuid_info_addr
= 0;
1465 boolean_t have_map
= (task
->map
!= NULL
) && (ml_validate_nofault((vm_offset_t
)(task
->map
), sizeof(struct _vm_map
)));
1466 boolean_t have_pmap
= have_map
&& (task
->map
->pmap
!= NULL
) && (ml_validate_nofault((vm_offset_t
)(task
->map
->pmap
), sizeof(struct pmap
)));
1467 int task_pid
= pid_from_task(task
);
1468 char uuidbuf
[PANICLOG_UUID_BUF_SIZE
] = {0};
1469 char *uuidbufptr
= uuidbuf
;
1472 if (have_pmap
&& task
->active
&& task_pid
> 0) {
1473 /* Read dyld_all_image_infos struct from task memory to get UUID array count & location */
1474 struct user64_dyld_all_image_infos task_image_infos
;
1475 if (debug_copyin(task
->map
->pmap
, task
->all_image_info_addr
,
1476 &task_image_infos
, sizeof(struct user64_dyld_all_image_infos
))) {
1477 uuid_info_count
= (uint32_t)task_image_infos
.uuidArrayCount
;
1478 uuid_info_addr
= task_image_infos
.uuidArray
;
1481 /* If we get a NULL uuid_info_addr (which can happen when we catch dyld
1482 * in the middle of updating this data structure), we zero the
1483 * uuid_info_count so that we won't even try to save load info for this task
1485 if (!uuid_info_addr
) {
1486 uuid_info_count
= 0;
1490 if (task_pid
> 0 && uuid_info_count
> 0) {
1491 uint32_t uuid_info_size
= sizeof(struct user64_dyld_uuid_info
);
1492 uint32_t uuid_array_size
= uuid_info_count
* uuid_info_size
;
1493 uint32_t uuid_copy_size
= 0;
1494 uint32_t uuid_image_count
= 0;
1495 char *current_uuid_buffer
= NULL
;
1496 /* Copy in the UUID info array. It may be nonresident, in which case just fix up nloadinfos to 0 */
1498 paniclog_append_noflush("\nuuid info:\n");
1499 while (uuid_array_size
) {
1500 if (uuid_array_size
<= PANICLOG_UUID_BUF_SIZE
) {
1501 uuid_copy_size
= uuid_array_size
;
1502 uuid_image_count
= uuid_array_size
/uuid_info_size
;
1504 uuid_image_count
= PANICLOG_UUID_BUF_SIZE
/uuid_info_size
;
1505 uuid_copy_size
= uuid_image_count
* uuid_info_size
;
1507 if (have_pmap
&& !debug_copyin(task
->map
->pmap
, uuid_info_addr
, uuidbufptr
,
1509 paniclog_append_noflush("Error!! Failed to copy UUID info for task %p pid %d\n", task
, task_pid
);
1510 uuid_image_count
= 0;
1514 if (uuid_image_count
> 0) {
1515 current_uuid_buffer
= uuidbufptr
;
1516 for (k
= 0; k
< uuid_image_count
; k
++) {
1517 paniclog_append_noflush(" %#llx", *(uint64_t *)current_uuid_buffer
);
1518 current_uuid_buffer
+= sizeof(uint64_t);
1519 uint8_t *uuid
= (uint8_t *)current_uuid_buffer
;
1520 paniclog_append_noflush("\tuuid = <%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x>\n",
1521 uuid
[0], uuid
[1], uuid
[2], uuid
[3], uuid
[4], uuid
[5], uuid
[6], uuid
[7], uuid
[8],
1522 uuid
[9], uuid
[10], uuid
[11], uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
1523 current_uuid_buffer
+= 16;
1525 bzero(&uuidbuf
, sizeof(uuidbuf
));
1527 uuid_info_addr
+= uuid_copy_size
;
1528 uuid_array_size
-= uuid_copy_size
;
1533 void print_launchd_info(void)
1535 task_t task
= current_task();
1536 thread_t thread
= current_thread();
1537 volatile uint32_t *ppbtcnt
= &pbtcnt
;
1538 uint64_t bt_tsc_timeout
;
1539 int cn
= cpu_number();
1542 hw_atomic_add(&pbtcnt
, 1);
1543 /* Spin on print backtrace lock, which serializes output
1544 * Continue anyway if a timeout occurs.
1546 hw_lock_to(&pbtlock
, ~0U);
1550 print_uuid_info(task
);
1551 print_thread_num_that_crashed(task
);
1552 print_threads_registers(thread
);
1553 print_tasks_user_threads(task
);
1555 panic_display_system_configuration(TRUE
);
1557 /* Release print backtrace lock, to permit other callers in the
1558 * event of panics on multiple processors.
1560 hw_lock_unlock(&pbtlock
);
1561 hw_atomic_sub(&pbtcnt
, 1);
1562 /* Wait for other processors to complete output
1563 * Timeout and continue after PBT_TIMEOUT_CYCLES.
1565 bt_tsc_timeout
= rdtsc64() + PBT_TIMEOUT_CYCLES
;
1566 while(*ppbtcnt
&& (rdtsc64() < bt_tsc_timeout
));