xref: /xnu-10002.1.13/osfmk/i386/trap_native.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a) !
1 /*
2  * Copyright (c) 2009-2012 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * @OSF_COPYRIGHT@
30  */
31 /*
32  * Mach Operating System
33  * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34  * All Rights Reserved.
35  *
36  * Permission to use, copy, modify and distribute this software and its
37  * documentation is hereby granted, provided that both the copyright
38  * notice and this permission notice appear in all copies of the
39  * software, derivative works or modified versions, and any portions
40  * thereof, and that both notices appear in supporting documentation.
41  *
42  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45  *
46  * Carnegie Mellon requests users of this software to return to
47  *
48  *  Software Distribution Coordinator  or  [email protected]
49  *  School of Computer Science
50  *  Carnegie Mellon University
51  *  Pittsburgh PA 15213-3890
52  *
53  * any improvements or extensions that they make and grant Carnegie Mellon
54  * the rights to redistribute these changes.
55  */
56 /*
57  */
58 
59 /*
60  * Hardware trap/fault handler.
61  */
62 
63 #include <types.h>
64 #include <i386/eflags.h>
65 #include <i386/trap.h>
66 #include <i386/pmap.h>
67 #include <i386/fpu.h>
68 #include <i386/panic_notify.h>
69 
70 #include <mach/exception.h>
71 #include <mach/kern_return.h>
72 #include <mach/vm_param.h>
73 #include <mach/i386/thread_status.h>
74 
75 #include <kern/kern_types.h>
76 #include <kern/processor.h>
77 #include <kern/thread.h>
78 #include <kern/task.h>
79 #include <kern/sched.h>
80 #include <kern/sched_prim.h>
81 #include <kern/exception.h>
82 #include <kern/spl.h>
83 #include <kern/misc_protos.h>
84 #include <kern/debug.h>
85 
86 #include <sys/kdebug.h>
87 
88 #include <string.h>
89 
90 #include <i386/postcode.h>
91 #include <i386/mp_desc.h>
92 #include <i386/proc_reg.h>
93 #if CONFIG_MCA
94 #include <i386/machine_check.h>
95 #endif
96 #include <mach/i386/syscall_sw.h>
97 
98 #include <machine/pal_routines.h>
99 #include <libkern/OSAtomic.h>
100 
101 extern void kprintf_break_lock(void);
102 extern void kprint_state(x86_saved_state64_t *saved_state);
103 
104 extern volatile int panic_double_fault_cpu;
105 
106 
107 #if defined(__x86_64__) && DEBUG
108 /*
109  * K64 debug - fatal handler for debug code in the trap vectors.
110  */
111 extern void __dead2
112 panic_idt64(x86_saved_state_t *rsp);
113 void
panic_idt64(x86_saved_state_t * rsp)114 panic_idt64(x86_saved_state_t *rsp)
115 {
116 	kprint_state(saved_state64(rsp));
117 	panic("panic_idt64");
118 }
119 #endif
120 
121 
122 __dead2
123 static void
panic_64(x86_saved_state_t * sp,__unused int pc,__unused const char * msg,boolean_t do_mca_dump)124 panic_64(x86_saved_state_t *sp, __unused int pc, __unused const char *msg, boolean_t do_mca_dump)
125 {
126 	/* Set postcode (DEBUG only) */
127 	postcode(pc);
128 
129 	/*
130 	 * Issue an I/O port read if one has been requested - this is an
131 	 * event logic analyzers can use as a trigger point.
132 	 */
133 	panic_notify();
134 
135 
136 	/*
137 	 * Break kprintf lock in case of recursion,
138 	 * and record originally faulted instruction address.
139 	 */
140 	kprintf_break_lock();
141 
142 	if (do_mca_dump) {
143 #if CONFIG_MCA
144 		/*
145 		 * Dump the contents of the machine check MSRs (if any).
146 		 */
147 		mca_dump();
148 #endif
149 	}
150 
151 	x86_saved_state64_t *regs = saved_state64(sp);
152 	panic("%s at 0x%016llx, registers:\n"
153 	    "CR0: 0x%016lx, CR2: 0x%016lx, CR3: 0x%016lx, CR4: 0x%016lx\n"
154 	    "RAX: 0x%016llx, RBX: 0x%016llx, RCX: 0x%016llx, RDX: 0x%016llx\n"
155 	    "RSP: 0x%016llx, RBP: 0x%016llx, RSI: 0x%016llx, RDI: 0x%016llx\n"
156 	    "R8:  0x%016llx, R9:  0x%016llx, R10: 0x%016llx, R11: 0x%016llx\n"
157 	    "R12: 0x%016llx, R13: 0x%016llx, R14: 0x%016llx, R15: 0x%016llx\n"
158 	    "RFL: 0x%016llx, RIP: 0x%016llx, CS:  0x%016llx, SS:  0x%016llx\n"
159 	    "Error code: 0x%016llx%s\n",
160 	    msg,
161 	    regs->isf.rip,
162 	    get_cr0(), get_cr2(), get_cr3_raw(), get_cr4(),
163 	    regs->rax, regs->rbx, regs->rcx, regs->rdx,
164 	    regs->isf.rsp, regs->rbp, regs->rsi, regs->rdi,
165 	    regs->r8, regs->r9, regs->r10, regs->r11,
166 	    regs->r12, regs->r13, regs->r14, regs->r15,
167 	    regs->isf.rflags, regs->isf.rip, regs->isf.cs & 0xFFFF, regs->isf.ss & 0xFFFF,
168 	    regs->isf.err, virtualized ? " VMM" : "");
169 }
170 
171 void
panic_double_fault64(x86_saved_state_t * sp)172 panic_double_fault64(x86_saved_state_t *sp)
173 {
174 #if DEVELOPMENT || DEBUG
175 	uint64_t frameptr = is_saved_state64(sp) ? saved_state64(sp)->rbp : saved_state32(sp)->ebp;
176 	(void) traptrace_start(T_DOUBLE_FAULT, saved_state64(sp)->isf.rip, mach_absolute_time(), frameptr);
177 #endif
178 	(void)OSCompareAndSwap((UInt32) - 1, (UInt32) cpu_number(), (volatile UInt32 *)&panic_double_fault_cpu);
179 	panic_64(sp, PANIC_DOUBLE_FAULT, "Double fault", FALSE);
180 }
181 void
panic_machine_check64(x86_saved_state_t * sp)182 panic_machine_check64(x86_saved_state_t *sp)
183 {
184 #if DEVELOPMENT || DEBUG
185 	uint64_t frameptr = is_saved_state64(sp) ? saved_state64(sp)->rbp : saved_state32(sp)->ebp;
186 	(void) traptrace_start(T_MACHINE_CHECK, saved_state64(sp)->isf.rip, mach_absolute_time(), frameptr);
187 #endif
188 	panic_64(sp, PANIC_MACHINE_CHECK, "Machine Check", TRUE);
189 }
190