]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/trap.c
xnu-3247.1.106.tar.gz
[apple/xnu.git] / osfmk / i386 / trap.c
CommitLineData
1c79356b 1/*
39236c6e 2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
2d21ac55
A
29* @OSF_COPYRIGHT@
30*/
1c79356b 31/*
2d21ac55
A
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 Software.Distribution@CS.CMU.EDU
49* School of Computer Science
50* Carnegie Mellon University
51* Pittsburgh PA 15213-3890
52*
53* any improvements or extensions that they make and grant Carnegie Mellon
54* the rights to redistribute these changes.
55*/
1c79356b 56/*
2d21ac55
A
57*/
58
1c79356b 59/*
2d21ac55 60* Hardware trap/fault handler.
1c79356b
A
61 */
62
1c79356b
A
63#include <mach_kdp.h>
64#include <mach_ldebug.h>
65
66#include <types.h>
67#include <i386/eflags.h>
68#include <i386/trap.h>
69#include <i386/pmap.h>
70#include <i386/fpu.h>
0c530ab8 71#include <i386/misc_protos.h> /* panic_io_port_read() */
b0d623f7 72#include <i386/lapic.h>
1c79356b
A
73
74#include <mach/exception.h>
75#include <mach/kern_return.h>
76#include <mach/vm_param.h>
77#include <mach/i386/thread_status.h>
78
79#include <vm/vm_kern.h>
80#include <vm/vm_fault.h>
81
1c79356b 82#include <kern/kern_types.h>
91447636 83#include <kern/processor.h>
1c79356b
A
84#include <kern/thread.h>
85#include <kern/task.h>
86#include <kern/sched.h>
87#include <kern/sched_prim.h>
88#include <kern/exception.h>
89#include <kern/spl.h>
90#include <kern/misc_protos.h>
b0d623f7 91#include <kern/debug.h>
39236c6e
A
92#if CONFIG_TELEMETRY
93#include <kern/telemetry.h>
94#endif
0c530ab8 95#include <sys/kdebug.h>
fe8ab488 96#include <prng/random.h>
0c530ab8 97
1c79356b
A
98#include <string.h>
99
0c530ab8
A
100#include <i386/postcode.h>
101#include <i386/mp_desc.h>
102#include <i386/proc_reg.h>
b0d623f7 103#if CONFIG_MCA
0c530ab8 104#include <i386/machine_check.h>
b0d623f7 105#endif
0c530ab8 106#include <mach/i386/syscall_sw.h>
1c79356b 107
b0d623f7 108#include <libkern/OSDebug.h>
bd504ef0 109#include <i386/cpu_threads.h>
6d2010ae 110#include <machine/pal_routines.h>
593a1d5f 111
6d2010ae
A
112extern void throttle_lowpri_io(int);
113extern void kprint_state(x86_saved_state64_t *saved_state);
b0d623f7 114
1c79356b
A
115/*
116 * Forward declarations
117 */
0c530ab8 118static void user_page_fault_continue(kern_return_t kret);
3e170ce0 119static void panic_trap(x86_saved_state64_t *saved_state, uint32_t pl);
b0d623f7 120static void set_recovery_ip(x86_saved_state64_t *saved_state, vm_offset_t ip);
6601e61a 121
b0d623f7 122volatile perfCallback perfTrapHook = NULL; /* Pointer to CHUD trap hook routine */
1c79356b 123
2d21ac55
A
124#if CONFIG_DTRACE
125/* See <rdar://problem/4613924> */
126perfCallback tempDTraceTrapHook = NULL; /* Pointer to DTrace fbt trap hook routine */
127
128extern boolean_t dtrace_tally_fault(user_addr_t);
129#endif
130
13f56ec4 131extern boolean_t pmap_smep_enabled;
fe8ab488 132extern boolean_t pmap_smap_enabled;
7ddcb079 133
1c79356b
A
134void
135thread_syscall_return(
136 kern_return_t ret)
137{
0c530ab8 138 thread_t thr_act = current_thread();
b0d623f7
A
139 boolean_t is_mach;
140 int code;
141
6d2010ae 142 pal_register_cache_state(thr_act, DIRTY);
0c530ab8
A
143
144 if (thread_is_64bit(thr_act)) {
145 x86_saved_state64_t *regs;
146
147 regs = USER_REGS64(thr_act);
148
b0d623f7
A
149 code = (int) (regs->rax & SYSCALL_NUMBER_MASK);
150 is_mach = (regs->rax & SYSCALL_CLASS_MASK)
151 == (SYSCALL_CLASS_MACH << SYSCALL_CLASS_SHIFT);
152 if (kdebug_enable && is_mach) {
0c530ab8 153 /* Mach trap */
316670eb 154 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
b0d623f7
A
155 MACHDBG_CODE(DBG_MACH_EXCP_SC,code)|DBG_FUNC_END,
156 ret, 0, 0, 0, 0);
0c530ab8
A
157 }
158 regs->rax = ret;
b0d623f7
A
159#if DEBUG
160 if (is_mach)
161 DEBUG_KPRINT_SYSCALL_MACH(
162 "thread_syscall_return: 64-bit mach ret=%u\n",
163 ret);
164 else
165 DEBUG_KPRINT_SYSCALL_UNIX(
166 "thread_syscall_return: 64-bit unix ret=%u\n",
167 ret);
168#endif
0c530ab8
A
169 } else {
170 x86_saved_state32_t *regs;
171
172 regs = USER_REGS32(thr_act);
173
b0d623f7
A
174 code = ((int) regs->eax);
175 is_mach = (code < 0);
176 if (kdebug_enable && is_mach) {
0c530ab8 177 /* Mach trap */
316670eb 178 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
b0d623f7
A
179 MACHDBG_CODE(DBG_MACH_EXCP_SC,-code)|DBG_FUNC_END,
180 ret, 0, 0, 0, 0);
0c530ab8
A
181 }
182 regs->eax = ret;
b0d623f7
A
183#if DEBUG
184 if (is_mach)
185 DEBUG_KPRINT_SYSCALL_MACH(
186 "thread_syscall_return: 32-bit mach ret=%u\n",
187 ret);
188 else
189 DEBUG_KPRINT_SYSCALL_UNIX(
190 "thread_syscall_return: 32-bit unix ret=%u\n",
191 ret);
192#endif
0c530ab8 193 }
39236c6e 194 throttle_lowpri_io(1);
593a1d5f
A
195
196 thread_exception_return();
1c79356b
A
197 /*NOTREACHED*/
198}
199
200
6d2010ae 201static inline void
1c79356b 202user_page_fault_continue(
0c530ab8 203 kern_return_t kr)
1c79356b 204{
0c530ab8 205 thread_t thread = current_thread();
0c530ab8 206 user_addr_t vaddr;
b0d623f7 207
316670eb
A
208 if (thread_is_64bit(thread)) {
209 x86_saved_state64_t *uregs;
0c530ab8
A
210
211 uregs = USER_REGS64(thread);
212
0c530ab8
A
213 vaddr = (user_addr_t)uregs->cr2;
214 } else {
215 x86_saved_state32_t *uregs;
216
217 uregs = USER_REGS32(thread);
218
0c530ab8
A
219 vaddr = uregs->cr2;
220 }
1c79356b 221
1c79356b 222
6d2010ae
A
223 /* PAL debug hook */
224 pal_dbg_page_fault( thread, vaddr, kr );
b0d623f7 225
0c530ab8 226 i386_exception(EXC_BAD_ACCESS, kr, vaddr);
1c79356b
A
227 /*NOTREACHED*/
228}
229
230/*
231 * Fault recovery in copyin/copyout routines.
232 */
233struct recovery {
b0d623f7
A
234 uintptr_t fault_addr;
235 uintptr_t recover_addr;
1c79356b
A
236};
237
238extern struct recovery recover_table[];
239extern struct recovery recover_table_end[];
240
0c530ab8
A
241const char * trap_type[] = {TRAP_NAMES};
242unsigned TRAP_TYPES = sizeof(trap_type)/sizeof(trap_type[0]);
91447636 243
6d2010ae
A
244extern void PE_incoming_interrupt(int interrupt);
245
b0d623f7 246#if defined(__x86_64__) && DEBUG
6d2010ae
A
247void
248kprint_state(x86_saved_state64_t *saved_state)
b0d623f7
A
249{
250 kprintf("current_cpu_datap() 0x%lx\n", (uintptr_t)current_cpu_datap());
251 kprintf("Current GS base MSR 0x%llx\n", rdmsr64(MSR_IA32_GS_BASE));
252 kprintf("Kernel GS base MSR 0x%llx\n", rdmsr64(MSR_IA32_KERNEL_GS_BASE));
253 kprintf("state at 0x%lx:\n", (uintptr_t) saved_state);
254
255 kprintf(" rdi 0x%llx\n", saved_state->rdi);
256 kprintf(" rsi 0x%llx\n", saved_state->rsi);
257 kprintf(" rdx 0x%llx\n", saved_state->rdx);
258 kprintf(" r10 0x%llx\n", saved_state->r10);
259 kprintf(" r8 0x%llx\n", saved_state->r8);
260 kprintf(" r9 0x%llx\n", saved_state->r9);
b0d623f7
A
261
262 kprintf(" cr2 0x%llx\n", saved_state->cr2);
263 kprintf("real cr2 0x%lx\n", get_cr2());
264 kprintf(" r15 0x%llx\n", saved_state->r15);
265 kprintf(" r14 0x%llx\n", saved_state->r14);
266 kprintf(" r13 0x%llx\n", saved_state->r13);
267 kprintf(" r12 0x%llx\n", saved_state->r12);
268 kprintf(" r11 0x%llx\n", saved_state->r11);
269 kprintf(" rbp 0x%llx\n", saved_state->rbp);
270 kprintf(" rbx 0x%llx\n", saved_state->rbx);
271 kprintf(" rcx 0x%llx\n", saved_state->rcx);
272 kprintf(" rax 0x%llx\n", saved_state->rax);
273
274 kprintf(" gs 0x%x\n", saved_state->gs);
275 kprintf(" fs 0x%x\n", saved_state->fs);
276
277 kprintf(" isf.trapno 0x%x\n", saved_state->isf.trapno);
278 kprintf(" isf._pad 0x%x\n", saved_state->isf._pad);
279 kprintf(" isf.trapfn 0x%llx\n", saved_state->isf.trapfn);
280 kprintf(" isf.err 0x%llx\n", saved_state->isf.err);
281 kprintf(" isf.rip 0x%llx\n", saved_state->isf.rip);
282 kprintf(" isf.cs 0x%llx\n", saved_state->isf.cs);
283 kprintf(" isf.rflags 0x%llx\n", saved_state->isf.rflags);
284 kprintf(" isf.rsp 0x%llx\n", saved_state->isf.rsp);
285 kprintf(" isf.ss 0x%llx\n", saved_state->isf.ss);
286}
b0d623f7
A
287#endif
288
060df5ea 289
060df5ea
A
290/*
291 * Non-zero indicates latency assert is enabled and capped at valued
292 * absolute time units.
293 */
294
295uint64_t interrupt_latency_cap = 0;
296boolean_t ilat_assert = FALSE;
297
298void
299interrupt_latency_tracker_setup(void) {
300 uint32_t ilat_cap_us;
301 if (PE_parse_boot_argn("interrupt_latency_cap_us", &ilat_cap_us, sizeof(ilat_cap_us))) {
302 interrupt_latency_cap = ilat_cap_us * NSEC_PER_USEC;
303 nanoseconds_to_absolutetime(interrupt_latency_cap, &interrupt_latency_cap);
304 } else {
305 interrupt_latency_cap = LockTimeOut;
306 }
307 PE_parse_boot_argn("-interrupt_latency_assert_enable", &ilat_assert, sizeof(ilat_assert));
308}
309
310void interrupt_reset_latency_stats(void) {
311 uint32_t i;
312 for (i = 0; i < real_ncpus; i++) {
313 cpu_data_ptr[i]->cpu_max_observed_int_latency =
314 cpu_data_ptr[i]->cpu_max_observed_int_latency_vector = 0;
315 }
316}
317
318void interrupt_populate_latency_stats(char *buf, unsigned bufsize) {
319 uint32_t i, tcpu = ~0;
320 uint64_t cur_max = 0;
321
322 for (i = 0; i < real_ncpus; i++) {
323 if (cur_max < cpu_data_ptr[i]->cpu_max_observed_int_latency) {
324 cur_max = cpu_data_ptr[i]->cpu_max_observed_int_latency;
325 tcpu = i;
326 }
327 }
328
329 if (tcpu < real_ncpus)
330 snprintf(buf, bufsize, "0x%x 0x%x 0x%llx", tcpu, cpu_data_ptr[tcpu]->cpu_max_observed_int_latency_vector, cpu_data_ptr[tcpu]->cpu_max_observed_int_latency);
331}
b0d623f7 332
39236c6e
A
333uint32_t interrupt_timer_coalescing_enabled = 1;
334uint64_t interrupt_coalesced_timers;
335
b0d623f7
A
336/*
337 * Handle interrupts:
338 * - local APIC interrupts (IPIs, timers, etc) are handled by the kernel,
339 * - device interrupts go to the platform expert.
340 */
341void
342interrupt(x86_saved_state_t *state)
343{
344 uint64_t rip;
345 uint64_t rsp;
346 int interrupt_num;
347 boolean_t user_mode = FALSE;
6d2010ae 348 int ipl;
060df5ea 349 int cnum = cpu_number();
39236c6e 350 cpu_data_t *cdp = cpu_data_ptr[cnum];
316670eb 351 int itype = 0;
bd504ef0 352
060df5ea 353 if (is_saved_state64(state) == TRUE) {
b0d623f7
A
354 x86_saved_state64_t *state64;
355
356 state64 = saved_state64(state);
357 rip = state64->isf.rip;
358 rsp = state64->isf.rsp;
359 interrupt_num = state64->isf.trapno;
360#ifdef __x86_64__
361 if(state64->isf.cs & 0x03)
362#endif
363 user_mode = TRUE;
364 } else {
365 x86_saved_state32_t *state32;
366
367 state32 = saved_state32(state);
368 if (state32->cs & 0x03)
369 user_mode = TRUE;
370 rip = state32->eip;
371 rsp = state32->uesp;
372 interrupt_num = state32->trapno;
373 }
374
bd504ef0
A
375 if (cpu_data_ptr[cnum]->lcpu.package->num_idle == topoParms.nLThreadsPerPackage)
376 cpu_data_ptr[cnum]->cpu_hwIntpexits[interrupt_num]++;
377
316670eb
A
378 if (interrupt_num == (LAPIC_DEFAULT_INTERRUPT_BASE + LAPIC_INTERPROCESSOR_INTERRUPT))
379 itype = 1;
380 else if (interrupt_num == (LAPIC_DEFAULT_INTERRUPT_BASE + LAPIC_TIMER_INTERRUPT))
381 itype = 2;
382 else
383 itype = 3;
384
385 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
b0d623f7 386 MACHDBG_CODE(DBG_MACH_EXCP_INTR, 0) | DBG_FUNC_START,
316670eb
A
387 interrupt_num,
388 (user_mode ? rip : VM_KERNEL_UNSLIDE(rip)),
389 user_mode, itype, 0);
6d2010ae
A
390
391 SCHED_STATS_INTERRUPT(current_processor());
392
39236c6e 393#if CONFIG_TELEMETRY
3e170ce0 394 if (telemetry_needs_record) {
39236c6e
A
395 telemetry_mark_curthread(user_mode);
396 }
397#endif
398
6d2010ae 399 ipl = get_preemption_level();
316670eb 400
b0d623f7
A
401 /*
402 * Handle local APIC interrupts
403 * else call platform expert for devices.
6d2010ae 404 */
fe8ab488 405 if (!lapic_interrupt(interrupt_num, state)) {
b0d623f7 406 PE_incoming_interrupt(interrupt_num);
fe8ab488 407 }
6d2010ae
A
408
409 if (__improbable(get_preemption_level() != ipl)) {
410 panic("Preemption level altered by interrupt vector 0x%x: initial 0x%x, final: 0x%x\n", interrupt_num, ipl, get_preemption_level());
060df5ea 411 }
b0d623f7 412
316670eb 413
39236c6e
A
414 if (__improbable(cdp->cpu_nested_istack)) {
415 cdp->cpu_nested_istack_events++;
060df5ea 416 }
6d2010ae 417 else {
39236c6e
A
418 uint64_t ctime = mach_absolute_time();
419 uint64_t int_latency = ctime - cdp->cpu_int_event_time;
420 uint64_t esdeadline, ehdeadline;
421 /* Attempt to process deferred timers in the context of
422 * this interrupt, unless interrupt time has already exceeded
423 * TCOAL_ILAT_THRESHOLD.
424 */
425#define TCOAL_ILAT_THRESHOLD (30000ULL)
426
427 if ((int_latency < TCOAL_ILAT_THRESHOLD) &&
428 interrupt_timer_coalescing_enabled) {
429 esdeadline = cdp->rtclock_timer.queue.earliest_soft_deadline;
430 ehdeadline = cdp->rtclock_timer.deadline;
431 if ((ctime >= esdeadline) && (ctime < ehdeadline)) {
432 interrupt_coalesced_timers++;
433 TCOAL_DEBUG(0x88880000 | DBG_FUNC_START, ctime, esdeadline, ehdeadline, interrupt_coalesced_timers, 0);
434 rtclock_intr(state);
435 TCOAL_DEBUG(0x88880000 | DBG_FUNC_END, ctime, esdeadline, interrupt_coalesced_timers, 0, 0);
436 } else {
437 TCOAL_DEBUG(0x77770000, ctime, cdp->rtclock_timer.queue.earliest_soft_deadline, cdp->rtclock_timer.deadline, interrupt_coalesced_timers, 0);
438 }
060df5ea 439 }
39236c6e
A
440
441 if (__improbable(ilat_assert && (int_latency > interrupt_latency_cap) && !machine_timeout_suspended())) {
442 panic("Interrupt vector 0x%x exceeded interrupt latency threshold, 0x%llx absolute time delta, prior signals: 0x%x, current signals: 0x%x", interrupt_num, int_latency, cdp->cpu_prior_signals, cdp->cpu_signals);
443 }
444
445 if (__improbable(int_latency > cdp->cpu_max_observed_int_latency)) {
446 cdp->cpu_max_observed_int_latency = int_latency;
447 cdp->cpu_max_observed_int_latency_vector = interrupt_num;
060df5ea
A
448 }
449 }
450
b0d623f7
A
451 /*
452 * Having serviced the interrupt first, look at the interrupted stack depth.
453 */
454 if (!user_mode) {
39236c6e 455 uint64_t depth = cdp->cpu_kernel_stack
b0d623f7
A
456 + sizeof(struct x86_kernel_state)
457 + sizeof(struct i386_exception_link *)
458 - rsp;
39236c6e 459 if (__improbable(depth > kernel_stack_depth_max)) {
b0d623f7
A
460 kernel_stack_depth_max = (vm_offset_t)depth;
461 KERNEL_DEBUG_CONSTANT(
462 MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_DEPTH),
316670eb 463 (long) depth, (long) VM_KERNEL_UNSLIDE(rip), 0, 0, 0);
b0d623f7
A
464 }
465 }
39236c6e 466
fe8ab488
A
467 if (cnum == master_cpu)
468 ml_entropy_collect();
469
39236c6e
A
470 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
471 MACHDBG_CODE(DBG_MACH_EXCP_INTR, 0) | DBG_FUNC_END,
472 interrupt_num, 0, 0, 0, 0);
473
3e170ce0 474 assert(ml_get_interrupts_enabled() == FALSE);
b0d623f7 475}
2d21ac55 476
0c530ab8
A
477static inline void
478reset_dr7(void)
479{
b0d623f7
A
480 long dr7 = 0x400; /* magic dr7 reset value; 32 bit on i386, 64 bit on x86_64 */
481 __asm__ volatile("mov %0,%%dr7" : : "r" (dr7));
0c530ab8
A
482}
483#if MACH_KDP
484unsigned kdp_has_active_watchpoints = 0;
b0d623f7
A
485#define NO_WATCHPOINTS (!kdp_has_active_watchpoints)
486#else
487#define NO_WATCHPOINTS 1
0c530ab8 488#endif
1c79356b
A
489/*
490 * Trap from kernel mode. Only page-fault errors are recoverable,
491 * and then only in special circumstances. All other errors are
492 * fatal. Return value indicates if trap was handled.
493 */
b0d623f7 494
0c530ab8 495void
1c79356b 496kernel_trap(
6d2010ae
A
497 x86_saved_state_t *state,
498 uintptr_t *lo_spp)
1c79356b 499{
b0d623f7 500 x86_saved_state64_t *saved_state;
91447636 501 int code;
0c530ab8
A
502 user_addr_t vaddr;
503 int type;
2d21ac55 504 vm_map_t map = 0; /* protected by T_PAGE_FAULT */
91447636 505 kern_return_t result = KERN_FAILURE;
0c530ab8
A
506 thread_t thread;
507 ast_t *myast;
508 boolean_t intr;
509 vm_prot_t prot;
510 struct recovery *rp;
511 vm_offset_t kern_ip;
b0d623f7 512#if NCOPY_WINDOWS > 0
0c530ab8 513 int fault_in_copy_window = -1;
b0d623f7 514#endif
0c530ab8 515 int is_user = 0;
3e170ce0
A
516 int trap_pl = get_preemption_level();
517
1c79356b 518 thread = current_thread();
1c79356b 519
6d2010ae 520 if (__improbable(is_saved_state32(state)))
b0d623f7
A
521 panic("kernel_trap(%p) with 32-bit state", state);
522 saved_state = saved_state64(state);
6d2010ae
A
523
524 /* Record cpu where state was captured */
525 saved_state->isf.cpu = cpu_number();
526
b0d623f7
A
527 vaddr = (user_addr_t)saved_state->cr2;
528 type = saved_state->isf.trapno;
529 code = (int)(saved_state->isf.err & 0xffff);
530 intr = (saved_state->isf.rflags & EFL_IF) != 0; /* state of ints at trap */
531 kern_ip = (vm_offset_t)saved_state->isf.rip;
0c530ab8
A
532
533 myast = ast_pending();
534
6d2010ae
A
535 perfASTCallback astfn = perfASTHook;
536 if (__improbable(astfn != NULL)) {
0c530ab8 537 if (*myast & AST_CHUD_ALL)
6d2010ae 538 astfn(AST_CHUD_ALL, myast);
0c530ab8
A
539 } else
540 *myast &= ~AST_CHUD_ALL;
541
2d21ac55
A
542
543#if CONFIG_DTRACE
fe8ab488
A
544 /*
545 * Is there a DTrace hook?
546 */
6d2010ae
A
547 if (__improbable(tempDTraceTrapHook != NULL)) {
548 if (tempDTraceTrapHook(type, state, lo_spp, 0) == KERN_SUCCESS) {
2d21ac55
A
549 /*
550 * If it succeeds, we are done...
551 */
552 return;
553 }
554 }
555#endif /* CONFIG_DTRACE */
556
0c530ab8
A
557 /*
558 * we come here with interrupts off as we don't want to recurse
559 * on preemption below. but we do want to re-enable interrupts
560 * as soon we possibly can to hold latency down
561 */
6d2010ae 562 if (__improbable(T_PREEMPT == type)) {
2d21ac55 563 ast_taken(AST_PREEMPTION, FALSE);
0c530ab8 564
316670eb
A
565 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
566 (MACHDBG_CODE(DBG_MACH_EXCP_KTRAP_x86, type)) | DBG_FUNC_NONE,
567 0, 0, 0, VM_KERNEL_UNSLIDE(kern_ip), 0);
0c530ab8
A
568 return;
569 }
570
571 if (T_PAGE_FAULT == type) {
572 /*
573 * assume we're faulting in the kernel map
574 */
575 map = kernel_map;
576
6d2010ae 577 if (__probable(thread != THREAD_NULL && thread->map != kernel_map)) {
b0d623f7
A
578#if NCOPY_WINDOWS > 0
579 vm_offset_t copy_window_base;
0c530ab8
A
580 vm_offset_t kvaddr;
581 int window_index;
582
583 kvaddr = (vm_offset_t)vaddr;
b0d623f7 584 /*
0c530ab8
A
585 * must determine if fault occurred in
586 * the copy window while pre-emption is
587 * disabled for this processor so that
588 * we only need to look at the window
589 * associated with this processor
590 */
6d2010ae 591 copy_window_base = current_cpu_datap()->cpu_copywindow_base;
0c530ab8
A
592
593 if (kvaddr >= copy_window_base && kvaddr < (copy_window_base + (NBPDE * NCOPY_WINDOWS)) ) {
594
6d2010ae 595 window_index = (int)((kvaddr - copy_window_base) / NBPDE);
0c530ab8
A
596
597 if (thread->machine.copy_window[window_index].user_base != (user_addr_t)-1) {
598
599 kvaddr -= (copy_window_base + (NBPDE * window_index));
600 vaddr = thread->machine.copy_window[window_index].user_base + kvaddr;
601
602 map = thread->map;
603 fault_in_copy_window = window_index;
604 }
605 is_user = -1;
606 }
b0d623f7 607#else
7ddcb079 608 if (__probable(vaddr < VM_MAX_USER_PAGE_ADDRESS)) {
b0d623f7
A
609 /* fault occurred in userspace */
610 map = thread->map;
611 is_user = -1;
7ddcb079 612
13f56ec4
A
613 /* Intercept a potential Supervisor Mode Execute
614 * Protection fault. These criteria identify
615 * both NX faults and SMEP faults, but both
616 * are fatal. We avoid checking PTEs (racy).
617 * (The VM could just redrive a SMEP fault, hence
618 * the intercept).
619 */
620 if (__improbable((code == (T_PF_PROT | T_PF_EXECUTE)) && (pmap_smep_enabled) && (saved_state->isf.rip == vaddr))) {
621 goto debugger_entry;
622 }
623
04b8595b
A
624 /*
625 * Additionally check for SMAP faults...
626 * which are characterized by page-present and
627 * the AC bit unset (i.e. not from copyin/out path).
628 */
629 if (__improbable(code & T_PF_PROT &&
630 pmap_smap_enabled &&
631 (saved_state->isf.rflags & EFL_AC) == 0)) {
632 goto debugger_entry;
633 }
634
b0d623f7
A
635 /*
636 * If we're not sharing cr3 with the user
637 * and we faulted in copyio,
638 * then switch cr3 here and dismiss the fault.
639 */
640 if (no_shared_cr3 &&
641 (thread->machine.specFlags&CopyIOActive) &&
6d2010ae
A
642 map->pmap->pm_cr3 != get_cr3_base()) {
643 pmap_assert(current_cpu_datap()->cpu_pmap_pcid_enabled == FALSE);
644 set_cr3_raw(map->pmap->pm_cr3);
b0d623f7
A
645 return;
646 }
fe8ab488 647
b0d623f7
A
648 }
649#endif
0c530ab8
A
650 }
651 }
316670eb
A
652 user_addr_t kd_vaddr = is_user ? vaddr : VM_KERNEL_UNSLIDE(vaddr);
653 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
b0d623f7 654 (MACHDBG_CODE(DBG_MACH_EXCP_KTRAP_x86, type)) | DBG_FUNC_NONE,
316670eb
A
655 (unsigned)(kd_vaddr >> 32), (unsigned)kd_vaddr, is_user,
656 VM_KERNEL_UNSLIDE(kern_ip), 0);
0c530ab8
A
657
658
659 (void) ml_set_interrupts_enabled(intr);
660
1c79356b 661 switch (type) {
1c79356b
A
662
663 case T_NO_FPU:
664 fpnoextflt();
0c530ab8 665 return;
1c79356b
A
666
667 case T_FPU_FAULT:
668 fpextovrflt();
0c530ab8 669 return;
1c79356b
A
670
671 case T_FLOATING_POINT_ERROR:
672 fpexterrflt();
0c530ab8 673 return;
1c79356b 674
0c530ab8
A
675 case T_SSE_FLOAT_ERROR:
676 fpSSEexterrflt();
677 return;
678 case T_DEBUG:
b0d623f7 679 if ((saved_state->isf.rflags & EFL_TF) == 0 && NO_WATCHPOINTS)
2d21ac55 680 {
0c530ab8
A
681 /* We've somehow encountered a debug
682 * register match that does not belong
683 * to the kernel debugger.
684 * This isn't supposed to happen.
685 */
686 reset_dr7();
687 return;
2d21ac55 688 }
0c530ab8 689 goto debugger_entry;
b0d623f7
A
690#ifdef __x86_64__
691 case T_INT3:
692 goto debugger_entry;
693#endif
1c79356b 694 case T_PAGE_FAULT:
0c530ab8 695
2d21ac55 696#if CONFIG_DTRACE
6d2010ae 697 if (thread != THREAD_NULL && thread->options & TH_OPT_DTRACE) { /* Executing under dtrace_probe? */
2d21ac55
A
698 if (dtrace_tally_fault(vaddr)) { /* Should a fault under dtrace be ignored? */
699 /*
700 * DTrace has "anticipated" the possibility of this fault, and has
701 * established the suitable recovery state. Drop down now into the
702 * recovery handling code in "case T_GENERAL_PROTECTION:".
703 */
704 goto FALL_THROUGH;
705 }
706 }
707#endif /* CONFIG_DTRACE */
7ddcb079
A
708
709 prot = VM_PROT_READ;
710
711 if (code & T_PF_WRITE)
712 prot |= VM_PROT_WRITE;
7ddcb079
A
713 if (code & T_PF_EXECUTE)
714 prot |= VM_PROT_EXECUTE;
7ddcb079 715
0c530ab8 716 result = vm_fault(map,
39236c6e
A
717 vm_map_trunc_page(vaddr,
718 PAGE_MASK),
0c530ab8
A
719 prot,
720 FALSE,
721 THREAD_UNINT, NULL, 0);
722
1c79356b 723 if (result == KERN_SUCCESS) {
b0d623f7 724#if NCOPY_WINDOWS > 0
0c530ab8 725 if (fault_in_copy_window != -1) {
6d2010ae
A
726 ml_set_interrupts_enabled(FALSE);
727 copy_window_fault(thread, map,
728 fault_in_copy_window);
0c530ab8 729 (void) ml_set_interrupts_enabled(intr);
1c79356b 730 }
b0d623f7 731#endif /* NCOPY_WINDOWS > 0 */
0c530ab8 732 return;
1c79356b 733 }
0c530ab8
A
734 /*
735 * fall through
736 */
2d21ac55
A
737#if CONFIG_DTRACE
738FALL_THROUGH:
739#endif /* CONFIG_DTRACE */
1c79356b
A
740
741 case T_GENERAL_PROTECTION:
1c79356b
A
742 /*
743 * If there is a failure recovery address
744 * for this fault, go there.
745 */
0c530ab8
A
746 for (rp = recover_table; rp < recover_table_end; rp++) {
747 if (kern_ip == rp->fault_addr) {
748 set_recovery_ip(saved_state, rp->recover_addr);
749 return;
1c79356b 750 }
1c79356b
A
751 }
752
753 /*
0c530ab8 754 * Check thread recovery address also.
1c79356b 755 */
6d2010ae 756 if (thread != THREAD_NULL && thread->recover) {
b0d623f7 757 set_recovery_ip(saved_state, thread->recover);
0c530ab8
A
758 thread->recover = 0;
759 return;
1c79356b 760 }
1c79356b
A
761 /*
762 * Unanticipated page-fault errors in kernel
763 * should not happen.
0c530ab8
A
764 *
765 * fall through...
1c79356b 766 */
1c79356b 767 default:
91447636
A
768 /*
769 * Exception 15 is reserved but some chips may generate it
770 * spuriously. Seen at startup on AMD Athlon-64.
771 */
772 if (type == 15) {
773 kprintf("kernel_trap() ignoring spurious trap 15\n");
0c530ab8 774 return;
91447636 775 }
0c530ab8
A
776debugger_entry:
777 /* Ensure that the i386_kernel_state at the base of the
778 * current thread's stack (if any) is synchronized with the
779 * context at the moment of the trap, to facilitate
780 * access through the debugger.
1c79356b 781 */
b0d623f7 782 sync_iss_to_iks(state);
1c79356b 783#if MACH_KDP
316670eb 784 if (current_debugger != KDB_CUR_DB) {
b0d623f7 785 if (kdp_i386_trap(type, saved_state, result, (vm_offset_t)vaddr))
0c530ab8 786 return;
2d21ac55
A
787 }
788#endif
4452a7af 789 }
316670eb 790 pal_cli();
3e170ce0 791 panic_trap(saved_state, trap_pl);
0c530ab8
A
792 /*
793 * NO RETURN
794 */
795}
796
797
b0d623f7
A
798static void
799set_recovery_ip(x86_saved_state64_t *saved_state, vm_offset_t ip)
800{
801 saved_state->isf.rip = ip;
802}
0c530ab8
A
803
804
7ddcb079
A
805
806
b0d623f7 807static void
3e170ce0 808panic_trap(x86_saved_state64_t *regs, uint32_t pl)
4452a7af 809{
b0d623f7 810 const char *trapname = "Unknown";
6d2010ae 811 pal_cr_t cr0, cr2, cr3, cr4;
316670eb 812 boolean_t potential_smep_fault = FALSE, potential_kernel_NX_fault = FALSE;
04b8595b 813 boolean_t potential_smap_fault = FALSE;
0c530ab8 814
6d2010ae
A
815 pal_get_control_registers( &cr0, &cr2, &cr3, &cr4 );
816 assert(ml_get_interrupts_enabled() == FALSE);
817 current_cpu_datap()->cpu_fatal_trap_state = regs;
2d21ac55
A
818 /*
819 * Issue an I/O port read if one has been requested - this is an
820 * event logic analyzers can use as a trigger point.
821 */
0c530ab8
A
822 panic_io_port_read();
823
b0d623f7
A
824 kprintf("panic trap number 0x%x, rip 0x%016llx\n",
825 regs->isf.trapno, regs->isf.rip);
826 kprintf("cr0 0x%016llx cr2 0x%016llx cr3 0x%016llx cr4 0x%016llx\n",
827 cr0, cr2, cr3, cr4);
4452a7af 828
b0d623f7
A
829 if (regs->isf.trapno < TRAP_TYPES)
830 trapname = trap_type[regs->isf.trapno];
7ddcb079 831
316670eb
A
832 if ((regs->isf.trapno == T_PAGE_FAULT) && (regs->isf.err == (T_PF_PROT | T_PF_EXECUTE)) && (regs->isf.rip == regs->cr2)) {
833 if (pmap_smep_enabled && (regs->isf.rip < VM_MAX_USER_PAGE_ADDRESS)) {
834 potential_smep_fault = TRUE;
835 } else if (regs->isf.rip >= VM_MIN_KERNEL_AND_KEXT_ADDRESS) {
836 potential_kernel_NX_fault = TRUE;
837 }
04b8595b
A
838 } else if (pmap_smap_enabled &&
839 regs->isf.trapno == T_PAGE_FAULT &&
840 regs->isf.err & T_PF_PROT &&
841 regs->cr2 < VM_MAX_USER_PAGE_ADDRESS &&
842 regs->isf.rip >= VM_MIN_KERNEL_AND_KEXT_ADDRESS) {
843 potential_smap_fault = TRUE;
13f56ec4
A
844 }
845
b0d623f7
A
846#undef panic
847 panic("Kernel trap at 0x%016llx, type %d=%s, registers:\n"
848 "CR0: 0x%016llx, CR2: 0x%016llx, CR3: 0x%016llx, CR4: 0x%016llx\n"
849 "RAX: 0x%016llx, RBX: 0x%016llx, RCX: 0x%016llx, RDX: 0x%016llx\n"
850 "RSP: 0x%016llx, RBP: 0x%016llx, RSI: 0x%016llx, RDI: 0x%016llx\n"
851 "R8: 0x%016llx, R9: 0x%016llx, R10: 0x%016llx, R11: 0x%016llx\n"
852 "R12: 0x%016llx, R13: 0x%016llx, R14: 0x%016llx, R15: 0x%016llx\n"
853 "RFL: 0x%016llx, RIP: 0x%016llx, CS: 0x%016llx, SS: 0x%016llx\n"
3e170ce0 854 "Fault CR2: 0x%016llx, Error code: 0x%016llx, Fault CPU: 0x%x%s%s%s%s, PL: %d\n",
b0d623f7
A
855 regs->isf.rip, regs->isf.trapno, trapname,
856 cr0, cr2, cr3, cr4,
857 regs->rax, regs->rbx, regs->rcx, regs->rdx,
858 regs->isf.rsp, regs->rbp, regs->rsi, regs->rdi,
859 regs->r8, regs->r9, regs->r10, regs->r11,
860 regs->r12, regs->r13, regs->r14, regs->r15,
6d2010ae 861 regs->isf.rflags, regs->isf.rip, regs->isf.cs & 0xFFFF,
7ddcb079 862 regs->isf.ss & 0xFFFF,regs->cr2, regs->isf.err, regs->isf.cpu,
316670eb
A
863 virtualized ? " VMM" : "",
864 potential_kernel_NX_fault ? " Kernel NX fault" : "",
fe8ab488 865 potential_smep_fault ? " SMEP/User NX fault" : "",
3e170ce0 866 potential_smap_fault ? " SMAP fault" : "", pl);
0c530ab8 867 /*
b0d623f7
A
868 * This next statement is not executed,
869 * but it's needed to stop the compiler using tail call optimization
870 * for the panic call - which confuses the subsequent backtrace.
0c530ab8 871 */
b0d623f7 872 cr0 = 0;
4452a7af
A
873}
874
2d21ac55
A
875#if CONFIG_DTRACE
876extern kern_return_t dtrace_user_probe(x86_saved_state_t *);
877#endif
878
1c79356b
A
879/*
880 * Trap from user mode.
881 */
882void
883user_trap(
0c530ab8 884 x86_saved_state_t *saved_state)
1c79356b 885{
2d21ac55
A
886 int exc;
887 int err;
888 mach_exception_code_t code;
889 mach_exception_subcode_t subcode;
890 int type;
891 user_addr_t vaddr;
892 vm_prot_t prot;
893 thread_t thread = current_thread();
894 ast_t *myast;
895 kern_return_t kret;
896 user_addr_t rip;
6d2010ae 897 unsigned long dr6 = 0; /* 32 bit for i386, 64 bit for x86_64 */
0c530ab8
A
898
899 assert((is_saved_state32(saved_state) && !thread_is_64bit(thread)) ||
900 (is_saved_state64(saved_state) && thread_is_64bit(thread)));
901
902 if (is_saved_state64(saved_state)) {
903 x86_saved_state64_t *regs;
904
905 regs = saved_state64(saved_state);
906
6d2010ae
A
907 /* Record cpu where state was captured */
908 regs->isf.cpu = cpu_number();
909
0c530ab8 910 type = regs->isf.trapno;
b0d623f7 911 err = (int)regs->isf.err & 0xffff;
0c530ab8
A
912 vaddr = (user_addr_t)regs->cr2;
913 rip = (user_addr_t)regs->isf.rip;
914 } else {
2d21ac55 915 x86_saved_state32_t *regs;
0c530ab8
A
916
917 regs = saved_state32(saved_state);
918
6d2010ae
A
919 /* Record cpu where state was captured */
920 regs->cpu = cpu_number();
921
0c530ab8
A
922 type = regs->trapno;
923 err = regs->err & 0xffff;
924 vaddr = (user_addr_t)regs->cr2;
925 rip = (user_addr_t)regs->eip;
1c79356b
A
926 }
927
6d2010ae
A
928 if ((type == T_DEBUG) && thread->machine.ids) {
929 unsigned long clear = 0;
930 /* Stash and clear this processor's DR6 value, in the event
931 * this was a debug register match
932 */
933 __asm__ volatile ("mov %%db6, %0" : "=r" (dr6));
934 __asm__ volatile ("mov %0, %%db6" : : "r" (clear));
935 }
936
937 pal_sti();
938
316670eb 939 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
b0d623f7
A
940 (MACHDBG_CODE(DBG_MACH_EXCP_UTRAP_x86, type)) | DBG_FUNC_NONE,
941 (unsigned)(vaddr>>32), (unsigned)vaddr,
942 (unsigned)(rip>>32), (unsigned)rip, 0);
0c530ab8 943
1c79356b
A
944 code = 0;
945 subcode = 0;
91447636 946 exc = 0;
1c79356b 947
0c530ab8
A
948#if DEBUG_TRACE
949 kprintf("user_trap(0x%08x) type=%d vaddr=0x%016llx\n",
950 saved_state, type, vaddr);
951#endif
6d2010ae
A
952
953 perfASTCallback astfn = perfASTHook;
954 if (__improbable(astfn != NULL)) {
d41d1dae 955 myast = ast_pending();
0c530ab8 956 if (*myast & AST_CHUD_ALL) {
6d2010ae 957 astfn(AST_CHUD_ALL, myast);
0c530ab8 958 }
0c530ab8
A
959 }
960
961 /* Is there a hook? */
6d2010ae
A
962 perfCallback fn = perfTrapHook;
963 if (__improbable(fn != NULL)) {
b0d623f7 964 if (fn(type, saved_state, 0, 0) == KERN_SUCCESS)
0c530ab8
A
965 return; /* If it succeeds, we are done... */
966 }
967
fe8ab488 968#if CONFIG_DTRACE
2d21ac55
A
969 /*
970 * DTrace does not consume all user traps, only INT_3's for now.
971 * Avoid needlessly calling tempDTraceTrapHook here, and let the
972 * INT_3 case handle them.
973 */
fe8ab488
A
974#endif
975
b0d623f7
A
976 DEBUG_KPRINT_SYSCALL_MASK(1,
977 "user_trap: type=0x%x(%s) err=0x%x cr2=%p rip=%p\n",
978 type, trap_type[type], err, (void *)(long) vaddr, (void *)(long) rip);
6d2010ae 979
1c79356b
A
980 switch (type) {
981
982 case T_DIVIDE_ERROR:
983 exc = EXC_ARITHMETIC;
984 code = EXC_I386_DIV;
985 break;
986
987 case T_DEBUG:
0c530ab8
A
988 {
989 pcb_t pcb;
0c530ab8 990 /*
6d2010ae
A
991 * Update the PCB with this processor's DR6 value
992 * in the event this was a debug register match.
0c530ab8 993 */
6d2010ae 994 pcb = THREAD_TO_PCB(thread);
0c530ab8
A
995 if (pcb->ids) {
996 /*
997 * We can get and set the status register
998 * in 32-bit mode even on a 64-bit thread
999 * because the high order bits are not
1000 * used on x86_64
1001 */
1002 if (thread_is_64bit(thread)) {
0c530ab8 1003 x86_debug_state64_t *ids = pcb->ids;
6d2010ae 1004 ids->dr6 = dr6;
0c530ab8
A
1005 } else { /* 32 bit thread */
1006 x86_debug_state32_t *ids = pcb->ids;
6d2010ae 1007 ids->dr6 = (uint32_t) dr6;
0c530ab8 1008 }
0c530ab8
A
1009 }
1010 exc = EXC_BREAKPOINT;
1011 code = EXC_I386_SGL;
1012 break;
1013 }
1c79356b 1014 case T_INT3:
2d21ac55
A
1015#if CONFIG_DTRACE
1016 if (dtrace_user_probe(saved_state) == KERN_SUCCESS)
1017 return; /* If it succeeds, we are done... */
1018#endif
1c79356b
A
1019 exc = EXC_BREAKPOINT;
1020 code = EXC_I386_BPT;
1021 break;
1022
1023 case T_OVERFLOW:
1024 exc = EXC_ARITHMETIC;
1025 code = EXC_I386_INTO;
1026 break;
1027
1028 case T_OUT_OF_BOUNDS:
1029 exc = EXC_SOFTWARE;
1030 code = EXC_I386_BOUND;
1031 break;
1032
1033 case T_INVALID_OPCODE:
1034 exc = EXC_BAD_INSTRUCTION;
1035 code = EXC_I386_INVOP;
1036 break;
1037
1038 case T_NO_FPU:
1c79356b
A
1039 fpnoextflt();
1040 return;
1041
1042 case T_FPU_FAULT:
2d21ac55 1043 fpextovrflt(); /* Propagates exception directly, doesn't return */
1c79356b
A
1044 return;
1045
2d21ac55 1046 case T_INVALID_TSS: /* invalid TSS == iret with NT flag set */
1c79356b
A
1047 exc = EXC_BAD_INSTRUCTION;
1048 code = EXC_I386_INVTSSFLT;
0c530ab8 1049 subcode = err;
1c79356b
A
1050 break;
1051
1052 case T_SEGMENT_NOT_PRESENT:
1053 exc = EXC_BAD_INSTRUCTION;
1054 code = EXC_I386_SEGNPFLT;
0c530ab8 1055 subcode = err;
1c79356b
A
1056 break;
1057
1058 case T_STACK_FAULT:
1059 exc = EXC_BAD_INSTRUCTION;
1060 code = EXC_I386_STKFLT;
0c530ab8 1061 subcode = err;
1c79356b
A
1062 break;
1063
1064 case T_GENERAL_PROTECTION:
2d21ac55
A
1065 /*
1066 * There's a wide range of circumstances which generate this
1067 * class of exception. From user-space, many involve bad
1068 * addresses (such as a non-canonical 64-bit address).
1069 * So we map this to EXC_BAD_ACCESS (and thereby SIGSEGV).
1070 * The trouble is cr2 doesn't contain the faulting address;
1071 * we'd need to decode the faulting instruction to really
1072 * determine this. We'll leave that to debuggers.
1073 * However, attempted execution of privileged instructions
1074 * (e.g. cli) also generate GP faults and so we map these to
1075 * to EXC_BAD_ACCESS (and thence SIGSEGV) also - rather than
1076 * EXC_BAD_INSTRUCTION which is more accurate. We just can't
1077 * win!
1078 */
1079 exc = EXC_BAD_ACCESS;
1c79356b 1080 code = EXC_I386_GPFLT;
0c530ab8 1081 subcode = err;
1c79356b
A
1082 break;
1083
1084 case T_PAGE_FAULT:
db609669 1085 {
39236c6e 1086 prot = VM_PROT_READ;
0c530ab8
A
1087
1088 if (err & T_PF_WRITE)
1089 prot |= VM_PROT_WRITE;
6d2010ae 1090 if (__improbable(err & T_PF_EXECUTE))
0c530ab8 1091 prot |= VM_PROT_EXECUTE;
39236c6e
A
1092 kret = vm_fault(thread->map,
1093 vm_map_trunc_page(vaddr,
1094 PAGE_MASK),
1095 prot, FALSE,
1096 THREAD_ABORTSAFE, NULL, 0);
0c530ab8 1097
db609669
A
1098 if (__probable((kret == KERN_SUCCESS) || (kret == KERN_ABORTED))) {
1099 thread_exception_return();
39236c6e 1100 /*NOTREACHED*/
db609669
A
1101 }
1102
1103 user_page_fault_continue(kret);
1104 } /* NOTREACHED */
1c79356b
A
1105 break;
1106
0c530ab8 1107 case T_SSE_FLOAT_ERROR:
2d21ac55 1108 fpSSEexterrflt(); /* Propagates exception directly, doesn't return */
0c530ab8
A
1109 return;
1110
1111
1c79356b 1112 case T_FLOATING_POINT_ERROR:
2d21ac55 1113 fpexterrflt(); /* Propagates exception directly, doesn't return */
1c79356b
A
1114 return;
1115
2d21ac55
A
1116 case T_DTRACE_RET:
1117#if CONFIG_DTRACE
1118 if (dtrace_user_probe(saved_state) == KERN_SUCCESS)
1119 return; /* If it succeeds, we are done... */
1120#endif
1121 /*
1122 * If we get an INT 0x7f when we do not expect to,
1123 * treat it as an illegal instruction
1124 */
1125 exc = EXC_BAD_INSTRUCTION;
1126 code = EXC_I386_INVOP;
1127 break;
1128
1c79356b 1129 default:
2d21ac55 1130 panic("Unexpected user trap, type %d", type);
1c79356b
A
1131 return;
1132 }
2d21ac55
A
1133 /* Note: Codepaths that directly return from user_trap() have pending
1134 * ASTs processed in locore
1135 */
1c79356b 1136 i386_exception(exc, code, subcode);
2d21ac55 1137 /* NOTREACHED */
1c79356b
A
1138}
1139
1c79356b
A
1140
1141/*
1142 * Handle AST traps for i386.
1c79356b
A
1143 */
1144
1145extern void log_thread_action (thread_t, char *);
1146
1147void
1148i386_astintr(int preemption)
1149{
0c530ab8 1150 ast_t mask = AST_ALL;
1c79356b 1151 spl_t s;
1c79356b 1152
0c530ab8
A
1153 if (preemption)
1154 mask = AST_PREEMPTION;
1155
1156 s = splsched();
1c79356b 1157
91447636
A
1158 ast_taken(mask, s);
1159
0c530ab8 1160 splx(s);
1c79356b
A
1161}
1162
1163/*
1164 * Handle exceptions for i386.
1165 *
1166 * If we are an AT bus machine, we must turn off the AST for a
1167 * delayed floating-point exception.
1168 *
1169 * If we are providing floating-point emulation, we may have
1170 * to retrieve the real register values from the floating point
1171 * emulator.
1172 */
1173void
1174i386_exception(
1175 int exc,
2d21ac55
A
1176 mach_exception_code_t code,
1177 mach_exception_subcode_t subcode)
1c79356b 1178{
2d21ac55 1179 mach_exception_data_type_t codes[EXCEPTION_CODE_MAX];
1c79356b 1180
b0d623f7
A
1181 DEBUG_KPRINT_SYSCALL_MACH("i386_exception: exc=%d code=0x%llx subcode=0x%llx\n",
1182 exc, code, subcode);
1c79356b
A
1183 codes[0] = code; /* new exception interface */
1184 codes[1] = subcode;
91447636 1185 exception_triage(exc, codes, 2);
1c79356b
A
1186 /*NOTREACHED*/
1187}
1188
0c530ab8 1189
fe8ab488
A
1190/* Synchronize a thread's x86_kernel_state (if any) with the given
1191 * x86_saved_state_t obtained from the trap/IPI handler; called in
0c530ab8 1192 * kernel_trap() prior to entering the debugger, and when receiving
fe8ab488
A
1193 * an "MP_KDP" IPI. Called with null saved_state if an incoming IPI
1194 * was detected from the kernel while spinning with interrupts masked.
0c530ab8
A
1195 */
1196
1197void
b0d623f7 1198sync_iss_to_iks(x86_saved_state_t *saved_state)
0c530ab8 1199{
b0d623f7 1200 struct x86_kernel_state *iks;
0c530ab8
A
1201 vm_offset_t kstack;
1202 boolean_t record_active_regs = FALSE;
1203
6d2010ae 1204 /* The PAL may have a special way to sync registers */
fe8ab488 1205 if (saved_state && saved_state->flavor == THREAD_STATE_NONE)
6d2010ae
A
1206 pal_get_kern_regs( saved_state );
1207
0c530ab8 1208 if ((kstack = current_thread()->kernel_stack) != 0) {
b0d623f7 1209 x86_saved_state64_t *regs = saved_state64(saved_state);
0c530ab8
A
1210
1211 iks = STACK_IKS(kstack);
1212
6d2010ae 1213 /* Did we take the trap/interrupt in kernel mode? */
fe8ab488
A
1214 if (saved_state == NULL || /* NULL => polling in kernel */
1215 regs == USER_REGS64(current_thread()))
b0d623f7
A
1216 record_active_regs = TRUE;
1217 else {
1218 iks->k_rbx = regs->rbx;
1219 iks->k_rsp = regs->isf.rsp;
1220 iks->k_rbp = regs->rbp;
1221 iks->k_r12 = regs->r12;
1222 iks->k_r13 = regs->r13;
1223 iks->k_r14 = regs->r14;
1224 iks->k_r15 = regs->r15;
1225 iks->k_rip = regs->isf.rip;
1226 }
0c530ab8
A
1227 }
1228
1229 if (record_active_regs == TRUE) {
b0d623f7
A
1230 /* Show the trap handler path */
1231 __asm__ volatile("movq %%rbx, %0" : "=m" (iks->k_rbx));
1232 __asm__ volatile("movq %%rsp, %0" : "=m" (iks->k_rsp));
1233 __asm__ volatile("movq %%rbp, %0" : "=m" (iks->k_rbp));
1234 __asm__ volatile("movq %%r12, %0" : "=m" (iks->k_r12));
1235 __asm__ volatile("movq %%r13, %0" : "=m" (iks->k_r13));
1236 __asm__ volatile("movq %%r14, %0" : "=m" (iks->k_r14));
1237 __asm__ volatile("movq %%r15, %0" : "=m" (iks->k_r15));
1238 /* "Current" instruction pointer */
1239 __asm__ volatile("leaq 1f(%%rip), %%rax; mov %%rax, %0\n1:"
1240 : "=m" (iks->k_rip)
1241 :
1242 : "rax");
0c530ab8
A
1243 }
1244}
1245
1246/*
1247 * This is used by the NMI interrupt handler (from mp.c) to
1248 * uncondtionally sync the trap handler context to the IKS
1249 * irrespective of whether the NMI was fielded in kernel
1250 * or user space.
1251 */
1252void
2d21ac55 1253sync_iss_to_iks_unconditionally(__unused x86_saved_state_t *saved_state) {
b0d623f7 1254 struct x86_kernel_state *iks;
0c530ab8 1255 vm_offset_t kstack;
0c530ab8
A
1256
1257 if ((kstack = current_thread()->kernel_stack) != 0) {
0c530ab8 1258 iks = STACK_IKS(kstack);
b0d623f7
A
1259 /* Display the trap handler path */
1260 __asm__ volatile("movq %%rbx, %0" : "=m" (iks->k_rbx));
1261 __asm__ volatile("movq %%rsp, %0" : "=m" (iks->k_rsp));
1262 __asm__ volatile("movq %%rbp, %0" : "=m" (iks->k_rbp));
1263 __asm__ volatile("movq %%r12, %0" : "=m" (iks->k_r12));
1264 __asm__ volatile("movq %%r13, %0" : "=m" (iks->k_r13));
1265 __asm__ volatile("movq %%r14, %0" : "=m" (iks->k_r14));
1266 __asm__ volatile("movq %%r15, %0" : "=m" (iks->k_r15));
1267 /* "Current" instruction pointer */
1268 __asm__ volatile("leaq 1f(%%rip), %%rax; mov %%rax, %0\n1:" : "=m" (iks->k_rip)::"rax");
0c530ab8
A
1269 }
1270}