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