]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/x86_64/idt64.s
xnu-4570.1.46.tar.gz
[apple/xnu.git] / osfmk / x86_64 / idt64.s
... / ...
CommitLineData
1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28#include <i386/asm.h>
29#include <assym.s>
30#include <debug.h>
31#include <i386/eflags.h>
32#include <i386/rtclock_asm.h>
33#include <i386/trap.h>
34#define _ARCH_I386_ASM_HELP_H_ /* Prevent inclusion of user header */
35#include <mach/i386/syscall_sw.h>
36#include <i386/postcode.h>
37#include <i386/proc_reg.h>
38#include <mach/exception_types.h>
39
40#if DEBUG
41#define DEBUG_IDT64 1
42#endif
43
44/*
45 * This is the low-level trap and interrupt handling code associated with
46 * the IDT. It also includes system call handlers for sysenter/syscall.
47 * The IDT itself is defined in mp_desc.c.
48 *
49 * Code here is structured as follows:
50 *
51 * stubs Code called directly from an IDT vector.
52 * All entry points have the "idt64_" prefix and they are built
53 * using macros expanded by the inclusion of idt_table.h.
54 * This code performs vector-dependent identification and jumps
55 * into the dispatch code.
56 *
57 * dispatch The dispatch code is responsible for saving the thread state
58 * (which is either 64-bit or 32-bit) and then jumping to the
59 * class handler identified by the stub.
60 *
61 * returns Code to restore state and return to the previous context.
62 *
63 * handlers There are several classes of handlers:
64 * interrupt - asynchronous events typically from external devices
65 * trap - synchronous events due to thread execution
66 * syscall - synchronous system call request
67 * fatal - fatal traps
68 */
69
70/*
71 * Handlers:
72 */
73#define HNDL_ALLINTRS EXT(hndl_allintrs)
74#define HNDL_ALLTRAPS EXT(hndl_alltraps)
75#define HNDL_SYSENTER EXT(hndl_sysenter)
76#define HNDL_SYSCALL EXT(hndl_syscall)
77#define HNDL_UNIX_SCALL EXT(hndl_unix_scall)
78#define HNDL_MACH_SCALL EXT(hndl_mach_scall)
79#define HNDL_MDEP_SCALL EXT(hndl_mdep_scall)
80#define HNDL_DOUBLE_FAULT EXT(hndl_double_fault)
81#define HNDL_MACHINE_CHECK EXT(hndl_machine_check)
82
83
84#if 1
85#define PUSH_FUNCTION(func) \
86 sub $8, %rsp ;\
87 push %rax ;\
88 leaq func(%rip), %rax ;\
89 movq %rax, 8(%rsp) ;\
90 pop %rax
91#else
92#define PUSH_FUNCTION(func) pushq func
93#endif
94
95/* The wrapper for all non-special traps/interrupts */
96/* Everything up to PUSH_FUNCTION is just to output
97 * the interrupt number out to the postcode display
98 */
99#if DEBUG_IDT64
100#define IDT_ENTRY_WRAPPER(n, f) \
101 push %rax ;\
102 POSTCODE2(0x6400+n) ;\
103 pop %rax ;\
104 PUSH_FUNCTION(f) ;\
105 pushq $(n) ;\
106 jmp L_dispatch
107#else
108#define IDT_ENTRY_WRAPPER(n, f) \
109 PUSH_FUNCTION(f) ;\
110 pushq $(n) ;\
111 jmp L_dispatch
112#endif
113
114/* A trap that comes with an error code already on the stack */
115#define TRAP_ERR(n, f) \
116 Entry(f) ;\
117 IDT_ENTRY_WRAPPER(n, HNDL_ALLTRAPS)
118
119/* A normal trap */
120#define TRAP(n, f) \
121 Entry(f) ;\
122 pushq $0 ;\
123 IDT_ENTRY_WRAPPER(n, HNDL_ALLTRAPS)
124
125#define USER_TRAP TRAP
126
127/* An interrupt */
128#define INTERRUPT(n) \
129 Entry(_intr_ ## n) ;\
130 pushq $0 ;\
131 IDT_ENTRY_WRAPPER(n, HNDL_ALLINTRS)
132
133/* A trap with a special-case handler, hence we don't need to define anything */
134#define TRAP_SPC(n, f)
135#define TRAP_IST1(n, f)
136#define TRAP_IST2(n, f)
137#define USER_TRAP_SPC(n, f)
138
139/* Generate all the stubs */
140#include "idt_table.h"
141
142/*
143 * Common dispatch point.
144 * Determine what mode has been interrupted and save state accordingly.
145 * Here with:
146 * rsp from user-space: interrupt state in PCB, or
147 * from kernel-space: interrupt state in kernel or interrupt stack
148 * GSBASE from user-space: pthread area, or
149 * from kernel-space: cpu_data
150 */
151L_dispatch:
152 cmpl $(KERNEL64_CS), ISF64_CS(%rsp)
153 je L_dispatch_kernel
154
155 swapgs
156
157L_dispatch_user:
158 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP
159 je L_dispatch_U32 /* 32-bit user task */
160
161L_dispatch_U64:
162 subq $(ISS64_OFFSET), %rsp
163 mov %r15, R64_R15(%rsp)
164 mov %rsp, %r15
165 mov %gs:CPU_KERNEL_STACK, %rsp
166 jmp L_dispatch_64bit
167
168L_dispatch_kernel:
169 subq $(ISS64_OFFSET), %rsp
170 mov %r15, R64_R15(%rsp)
171 mov %rsp, %r15
172
173/*
174 * Here for 64-bit user task or kernel
175 */
176L_dispatch_64bit:
177 movl $(SS_64), SS_FLAVOR(%r15)
178
179 /*
180 * Save segment regs - for completeness since theyre not used.
181 */
182 movl %fs, R64_FS(%r15)
183 movl %gs, R64_GS(%r15)
184
185 /* Save general-purpose registers */
186 mov %rax, R64_RAX(%r15)
187 mov %rbx, R64_RBX(%r15)
188 mov %rcx, R64_RCX(%r15)
189 mov %rdx, R64_RDX(%r15)
190 mov %rbp, R64_RBP(%r15)
191 mov %rdi, R64_RDI(%r15)
192 mov %rsi, R64_RSI(%r15)
193 mov %r8, R64_R8(%r15)
194 mov %r9, R64_R9(%r15)
195 mov %r10, R64_R10(%r15)
196 mov %r11, R64_R11(%r15)
197 mov %r12, R64_R12(%r15)
198 mov %r13, R64_R13(%r15)
199 mov %r14, R64_R14(%r15)
200
201 /* cr2 is significant only for page-faults */
202 mov %cr2, %rax
203 mov %rax, R64_CR2(%r15)
204
205 mov R64_TRAPNO(%r15), %ebx /* %ebx := trapno for later */
206 mov R64_TRAPFN(%r15), %rdx /* %rdx := trapfn for later */
207 mov R64_CS(%r15), %esi /* %esi := cs for later */
208
209 jmp L_common_dispatch
210
211L_64bit_entry_reject:
212 /*
213 * Here for a 64-bit user attempting an invalid kernel entry.
214 */
215 pushq %rax
216 leaq HNDL_ALLTRAPS(%rip), %rax
217 movq %rax, ISF64_TRAPFN+8(%rsp)
218 popq %rax
219 movq $(T_INVALID_OPCODE), ISF64_TRAPNO(%rsp)
220 jmp L_dispatch_U64
221
222L_32bit_entry_check:
223 /*
224 * Check we're not a confused 64-bit user.
225 */
226 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP
227 jne L_64bit_entry_reject
228 /* fall through to 32-bit handler: */
229
230L_dispatch_U32: /* 32-bit user task */
231 subq $(ISS64_OFFSET), %rsp
232 mov %rsp, %r15
233 mov %gs:CPU_KERNEL_STACK, %rsp
234 movl $(SS_32), SS_FLAVOR(%r15)
235
236 /*
237 * Save segment regs
238 */
239 movl %ds, R32_DS(%r15)
240 movl %es, R32_ES(%r15)
241 movl %fs, R32_FS(%r15)
242 movl %gs, R32_GS(%r15)
243
244 /*
245 * Save general 32-bit registers
246 */
247 mov %eax, R32_EAX(%r15)
248 mov %ebx, R32_EBX(%r15)
249 mov %ecx, R32_ECX(%r15)
250 mov %edx, R32_EDX(%r15)
251 mov %ebp, R32_EBP(%r15)
252 mov %esi, R32_ESI(%r15)
253 mov %edi, R32_EDI(%r15)
254
255 /* Unconditionally save cr2; only meaningful on page faults */
256 mov %cr2, %rax
257 mov %eax, R32_CR2(%r15)
258
259 /*
260 * Copy registers already saved in the machine state
261 * (in the interrupt stack frame) into the compat save area.
262 */
263 mov R64_RIP(%r15), %eax
264 mov %eax, R32_EIP(%r15)
265 mov R64_RFLAGS(%r15), %eax
266 mov %eax, R32_EFLAGS(%r15)
267 mov R64_RSP(%r15), %eax
268 mov %eax, R32_UESP(%r15)
269 mov R64_SS(%r15), %eax
270 mov %eax, R32_SS(%r15)
271L_dispatch_U32_after_fault:
272 mov R64_CS(%r15), %esi /* %esi := %cs for later */
273 mov %esi, R32_CS(%r15)
274 mov R64_TRAPNO(%r15), %ebx /* %ebx := trapno for later */
275 mov %ebx, R32_TRAPNO(%r15)
276 mov R64_ERR(%r15), %eax
277 mov %eax, R32_ERR(%r15)
278 mov R64_TRAPFN(%r15), %rdx /* %rdx := trapfn for later */
279
280L_common_dispatch:
281 cld /* Ensure the direction flag is clear in the kernel */
282 cmpl $0, EXT(pmap_smap_enabled)(%rip)
283 je 1f
284 clac /* Clear EFLAGS.AC if SMAP is present/enabled */
2851:
286 /*
287 * On entering the kernel, we typically don't switch CR3
288 * because the kernel shares the user's address space.
289 * But we mark the kernel's cr3 as "active" for TLB coherency evaluation
290 * If, however, the CPU's invalid TLB flag is set, we have to invalidate the TLB
291 * since the kernel pagetables were changed while we were in userspace.
292 *
293 * For threads with a mapped pagezero (some WINE games) on non-SMAP platforms,
294 * we switch to the kernel's address space on entry. Also,
295 * if the global no_shared_cr3 is TRUE we do switch to the kernel's cr3
296 * so that illicit accesses to userspace can be trapped.
297 */
298 mov %gs:CPU_KERNEL_CR3, %rcx
299 mov %rcx, %gs:CPU_ACTIVE_CR3
300 test $3, %esi /* user/kernel? */
301 jz 2f /* skip cr3 reload from kernel */
302 xor %rbp, %rbp
303 cmpl $0, %gs:CPU_PAGEZERO_MAPPED
304 jnz 11f
305 cmpl $0, EXT(no_shared_cr3)(%rip)
306 je 2f
30711:
308 xor %eax, %eax
309 movw %gs:CPU_KERNEL_PCID, %ax
310 or %rax, %rcx
311 mov %rcx, %cr3 /* load kernel cr3 */
312 jmp 4f /* and skip tlb flush test */
3132:
314 mov %gs:CPU_ACTIVE_CR3+4, %rcx
315 shr $32, %rcx
316 testl %ecx, %ecx
317 jz 4f
318 testl $(1<<16), %ecx /* Global? */
319 jz 3f
320 movl $0, %gs:CPU_TLB_INVALID
321 mov %cr4, %rcx /* RMWW CR4, for lack of an alternative*/
322 and $(~CR4_PGE), %rcx
323 mov %rcx, %cr4
324 or $(CR4_PGE), %rcx
325 mov %rcx, %cr4
326 jmp 4f
3273:
328 movb $0, %gs:CPU_TLB_INVALID_LOCAL
329 mov %cr3, %rcx
330 mov %rcx, %cr3
3314:
332 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Get the active thread */
333 testq %rcx, %rcx
334 je 5f
335 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling trap */
336 cmpq $0, TH_PCB_IDS(%rcx) /* Is there a debug register state? */
337 je 5f
338 xor %ecx, %ecx /* If so, reset DR7 (the control) */
339 mov %rcx, %dr7
3405:
341 incl %gs:hwIntCnt(,%ebx,4) // Bump the trap/intr count
342 /* Dispatch the designated handler */
343 jmp *%rdx
344
345/*
346 * Control is passed here to return to user.
347 */
348Entry(return_to_user)
349 TIME_TRAP_UEXIT
350
351Entry(ret_to_user)
352// XXX 'Be nice to tidy up this debug register restore sequence...
353 mov %gs:CPU_ACTIVE_THREAD, %rdx
354 movq TH_PCB_IDS(%rdx),%rax /* Obtain this thread's debug state */
355
356 test %rax, %rax /* Is there a debug register context? */
357 je 2f /* branch if not */
358 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP /* Are we a 32-bit task? */
359 jne 1f
360 movl DS_DR0(%rax), %ecx /* If so, load the 32 bit DRs */
361 movq %rcx, %dr0
362 movl DS_DR1(%rax), %ecx
363 movq %rcx, %dr1
364 movl DS_DR2(%rax), %ecx
365 movq %rcx, %dr2
366 movl DS_DR3(%rax), %ecx
367 movq %rcx, %dr3
368 movl DS_DR7(%rax), %ecx
369 movq %rcx, %gs:CPU_DR7
370 jmp 2f
3711:
372 mov DS64_DR0(%rax), %rcx /* Load the full width DRs*/
373 mov %rcx, %dr0
374 mov DS64_DR1(%rax), %rcx
375 mov %rcx, %dr1
376 mov DS64_DR2(%rax), %rcx
377 mov %rcx, %dr2
378 mov DS64_DR3(%rax), %rcx
379 mov %rcx, %dr3
380 mov DS64_DR7(%rax), %rcx
381 mov %rcx, %gs:CPU_DR7
3822:
383 /*
384 * On exiting the kernel there's typically no need to switch cr3 since we're
385 * already running in the user's address space which includes the
386 * kernel. We now mark the task's cr3 as active, for TLB coherency.
387 * If the target address space has a pagezero mapping present, or
388 * if no_shared_cr3 is set, we do need to switch cr3 at this point.
389 */
390 mov %gs:CPU_TASK_CR3, %rcx
391 mov %rcx, %gs:CPU_ACTIVE_CR3
392 cmpl $0, %gs:CPU_PAGEZERO_MAPPED
393 jnz L_cr3_switch_island
394 movl EXT(no_shared_cr3)(%rip), %eax
395 test %eax, %eax /* -no_shared_cr3 */
396 jnz L_cr3_switch_island
397
398L_cr3_switch_return:
399 mov %gs:CPU_DR7, %rax /* Is there a debug control register?*/
400 cmp $0, %rax
401 je 4f
402 mov %rax, %dr7 /* Set DR7 */
403 movq $0, %gs:CPU_DR7
4044:
405 cmpl $(SS_64), SS_FLAVOR(%r15) /* 64-bit state? */
406 je L_64bit_return
407
408L_32bit_return:
409#if DEBUG_IDT64
410 cmpl $(SS_32), SS_FLAVOR(%r15) /* 32-bit state? */
411 je 1f
412 cli
413 POSTCODE2(0x6432)
414 CCALL1(panic_idt64, %r15)
4151:
416#endif /* DEBUG_IDT64 */
417
418 /*
419 * Restore registers into the machine state for iret.
420 * Here on fault stack and PCB address in R11.
421 */
422 movl R32_EIP(%r15), %eax
423 movl %eax, R64_RIP(%r15)
424 movl R32_EFLAGS(%r15), %eax
425 movl %eax, R64_RFLAGS(%r15)
426 movl R32_CS(%r15), %eax
427 movl %eax, R64_CS(%r15)
428 movl R32_UESP(%r15), %eax
429 movl %eax, R64_RSP(%r15)
430 movl R32_SS(%r15), %eax
431 movl %eax, R64_SS(%r15)
432
433 /*
434 * Restore general 32-bit registers
435 */
436 movl R32_EAX(%r15), %eax
437 movl R32_EBX(%r15), %ebx
438 movl R32_ECX(%r15), %ecx
439 movl R32_EDX(%r15), %edx
440 movl R32_EBP(%r15), %ebp
441 movl R32_ESI(%r15), %esi
442 movl R32_EDI(%r15), %edi
443
444 /*
445 * Restore segment registers. A segment exception taken here will
446 * push state on the IST1 stack and will not affect the "PCB stack".
447 */
448 mov %r15, %rsp /* Set the PCB as the stack */
449 swapgs
450
451 xor %r8, %r8
452 xor %r9, %r9
453 xor %r10, %r10
454 xor %r11, %r11
455 xor %r12, %r12
456 xor %r13, %r13
457 xor %r14, %r14
458 xor %r15, %r15
459
460EXT(ret32_set_ds):
461 movl R32_DS(%rsp), %ds
462EXT(ret32_set_es):
463 movl R32_ES(%rsp), %es
464EXT(ret32_set_fs):
465 movl R32_FS(%rsp), %fs
466EXT(ret32_set_gs):
467 movl R32_GS(%rsp), %gs
468
469 /* pop compat frame + trapno, trapfn and error */
470 add $(ISS64_OFFSET)+8+8+8, %rsp
471 cmpl $(SYSENTER_CS),ISF64_CS-8-8-8(%rsp)
472 /* test for fast entry/exit */
473 je L_fast_exit
474EXT(ret32_iret):
475 iretq /* return from interrupt */
476
477
478L_fast_exit:
479 pop %rdx /* user return eip */
480 pop %rcx /* pop and toss cs */
481 andl $(~EFL_IF), (%rsp) /* clear interrupts enable, sti below */
482 popf /* flags - carry denotes failure */
483 pop %rcx /* user return esp */
484 sti /* interrupts enabled after sysexit */
485 sysexitl /* 32-bit sysexit */
486
487L_cr3_switch_island:
488 xor %eax, %eax
489 movw %gs:CPU_ACTIVE_PCID, %ax
490 or %rax, %rcx
491 mov %rcx, %cr3
492 jmp L_cr3_switch_return
493
494ret_to_kernel:
495#if DEBUG_IDT64
496 cmpl $(SS_64), SS_FLAVOR(%r15) /* 64-bit state? */
497 je 1f
498 cli
499 POSTCODE2(0x6464)
500 CCALL1(panic_idt64, %r15)
501 hlt
5021:
503 cmpl $(KERNEL64_CS), R64_CS(%r15)
504 je 2f
505 CCALL1(panic_idt64, %r15)
506 hlt
5072:
508#endif
509
510L_64bit_return:
511 /*
512 * Restore general 64-bit registers.
513 * Here on fault stack and PCB address in R15.
514 */
515 mov R64_R14(%r15), %r14
516 mov R64_R13(%r15), %r13
517 mov R64_R12(%r15), %r12
518 mov R64_R11(%r15), %r11
519 mov R64_R10(%r15), %r10
520 mov R64_R9(%r15), %r9
521 mov R64_R8(%r15), %r8
522 mov R64_RSI(%r15), %rsi
523 mov R64_RDI(%r15), %rdi
524 mov R64_RBP(%r15), %rbp
525 mov R64_RDX(%r15), %rdx
526 mov R64_RCX(%r15), %rcx
527 mov R64_RBX(%r15), %rbx
528 mov R64_RAX(%r15), %rax
529
530 /*
531 * We must swap GS base if we're returning to user-space,
532 * or we're returning from an NMI that occurred in a trampoline
533 * before the user GS had been swapped. In the latter case, the NMI
534 * handler will have flagged the high-order 32-bits of the CS.
535 */
536 cmpq $(KERNEL64_CS), R64_CS(%r15)
537 jz 1f
538 swapgs
5391:
540 mov R64_R15(%r15), %rsp
541 xchg %r15, %rsp
542 add $(ISS64_OFFSET)+24, %rsp /* pop saved state */
543 /* + trapno/trapfn/error */
544 cmpl $(SYSCALL_CS),ISF64_CS-24(%rsp)
545 /* test for fast entry/exit */
546 je L_sysret
547.globl _dump_iretq
548EXT(ret64_iret):
549 iretq /* return from interrupt */
550
551L_sysret:
552 /*
553 * Here to load rcx/r11/rsp and perform the sysret back to user-space.
554 * rcx user rip
555 * r11 user rflags
556 * rsp user stack pointer
557 */
558 mov ISF64_RIP-24(%rsp), %rcx
559 mov ISF64_RFLAGS-24(%rsp), %r11
560 mov ISF64_RSP-24(%rsp), %rsp
561 sysretq /* return from systen call */
562
563
564
565/*
566 * System call handlers.
567 * These are entered via a syscall interrupt. The system call number in %rax
568 * is saved to the error code slot in the stack frame. We then branch to the
569 * common state saving code.
570 */
571
572#ifndef UNIX_INT
573#error NO UNIX INT!!!
574#endif
575Entry(idt64_unix_scall)
576 swapgs /* switch to kernel gs (cpu_data) */
577 pushq %rax /* save system call number */
578 PUSH_FUNCTION(HNDL_UNIX_SCALL)
579 pushq $(UNIX_INT)
580 jmp L_32bit_entry_check
581
582
583Entry(idt64_mach_scall)
584 swapgs /* switch to kernel gs (cpu_data) */
585 pushq %rax /* save system call number */
586 PUSH_FUNCTION(HNDL_MACH_SCALL)
587 pushq $(MACH_INT)
588 jmp L_32bit_entry_check
589
590
591Entry(idt64_mdep_scall)
592 swapgs /* switch to kernel gs (cpu_data) */
593 pushq %rax /* save system call number */
594 PUSH_FUNCTION(HNDL_MDEP_SCALL)
595 pushq $(MACHDEP_INT)
596 jmp L_32bit_entry_check
597
598/* Programmed into MSR_IA32_LSTAR by mp_desc.c */
599Entry(hi64_syscall)
600Entry(idt64_syscall)
601L_syscall_continue:
602 swapgs /* Kapow! get per-cpu data area */
603 mov %rsp, %gs:CPU_UBER_TMP /* save user stack */
604 mov %gs:CPU_UBER_ISF, %rsp /* switch stack to pcb */
605
606 /*
607 * Save values in the ISF frame in the PCB
608 * to cons up the saved machine state.
609 */
610 movl $(USER_DS), ISF64_SS(%rsp)
611 movl $(SYSCALL_CS), ISF64_CS(%rsp) /* cs - a pseudo-segment */
612 mov %r11, ISF64_RFLAGS(%rsp) /* rflags */
613 mov %rcx, ISF64_RIP(%rsp) /* rip */
614 mov %gs:CPU_UBER_TMP, %rcx
615 mov %rcx, ISF64_RSP(%rsp) /* user stack */
616 mov %rax, ISF64_ERR(%rsp) /* err/rax - syscall code */
617 movq $(T_SYSCALL), ISF64_TRAPNO(%rsp) /* trapno */
618 leaq HNDL_SYSCALL(%rip), %r11;
619 movq %r11, ISF64_TRAPFN(%rsp)
620 mov ISF64_RFLAGS(%rsp), %r11 /* Avoid leak, restore R11 */
621 jmp L_dispatch_U64 /* this can only be 64-bit */
622
623/*
624 * sysenter entry point
625 * Requires user code to set up:
626 * edx: user instruction pointer (return address)
627 * ecx: user stack pointer
628 * on which is pushed stub ret addr and saved ebx
629 * Return to user-space is made using sysexit.
630 * Note: sysenter/sysexit cannot be used for calls returning a value in edx,
631 * or requiring ecx to be preserved.
632 */
633Entry(hi64_sysenter)
634Entry(idt64_sysenter)
635 movq (%rsp), %rsp
636 /*
637 * Push values on to the PCB stack
638 * to cons up the saved machine state.
639 */
640 push $(USER_DS) /* ss */
641 push %rcx /* uesp */
642 pushf /* flags */
643 /*
644 * Clear, among others, the Nested Task (NT) flags bit;
645 * this is zeroed by INT, but not by SYSENTER.
646 */
647 push $0
648 popf
649 push $(SYSENTER_CS) /* cs */
650L_sysenter_continue:
651 swapgs /* switch to kernel gs (cpu_data) */
652 push %rdx /* eip */
653 push %rax /* err/eax - syscall code */
654 PUSH_FUNCTION(HNDL_SYSENTER)
655 pushq $(T_SYSENTER)
656 orl $(EFL_IF), ISF64_RFLAGS(%rsp)
657 jmp L_32bit_entry_check
658
659
660Entry(idt64_page_fault)
661 PUSH_FUNCTION(HNDL_ALLTRAPS)
662 push $(T_PAGE_FAULT)
663 push %rax /* save %rax temporarily */
664 testb $3, 8+ISF64_CS(%rsp) /* was trap from kernel? */
665 jz L_kernel_trap /* - yes, handle with care */
666 pop %rax /* restore %rax, swapgs, and continue */
667 swapgs
668 jmp L_dispatch_user
669
670
671/*
672 * Debug trap. Check for single-stepping across system call into
673 * kernel. If this is the case, taking the debug trap has turned
674 * off single-stepping - save the flags register with the trace
675 * bit set.
676 */
677Entry(idt64_debug)
678 push $0 /* error code */
679 PUSH_FUNCTION(HNDL_ALLTRAPS)
680 pushq $(T_DEBUG)
681
682 testb $3, ISF64_CS(%rsp)
683 jnz L_dispatch
684
685 /*
686 * trap came from kernel mode
687 */
688
689 push %rax /* save %rax temporarily */
690 lea EXT(idt64_sysenter)(%rip), %rax
691 cmp %rax, ISF64_RIP+8(%rsp)
692 pop %rax
693 jne L_dispatch
694 /*
695 * Interrupt stack frame has been pushed on the temporary stack.
696 * We have to switch to pcb stack and patch up the saved state.
697 */
698 mov %rcx, ISF64_ERR(%rsp) /* save %rcx in error slot */
699 mov ISF64_SS+8(%rsp), %rcx /* top of temp stack -> pcb stack */
700 xchg %rcx,%rsp /* switch to pcb stack */
701 push $(USER_DS) /* ss */
702 push ISF64_ERR(%rcx) /* saved %rcx into rsp slot */
703 push ISF64_RFLAGS(%rcx) /* rflags */
704 push $(SYSENTER_TF_CS) /* cs - not SYSENTER_CS for iret path */
705 mov ISF64_ERR(%rcx),%rcx /* restore %rcx */
706 jmp L_sysenter_continue /* continue sysenter entry */
707
708
709Entry(idt64_double_fault)
710 PUSH_FUNCTION(HNDL_DOUBLE_FAULT)
711 pushq $(T_DOUBLE_FAULT)
712 jmp L_dispatch_kernel
713
714
715/*
716 * For GP/NP/SS faults, we use the IST1 stack.
717 * For faults from user-space, we have to copy the machine state to the
718 * PCB stack and then dispatch as normal.
719 * For faults in kernel-space, we need to scrub for kernel exit faults and
720 * treat these as user-space faults. But for all other kernel-space faults
721 * we continue to run on the IST1 stack and we dispatch to handle the fault
722 * as fatal.
723 */
724Entry(idt64_gen_prot)
725 PUSH_FUNCTION(HNDL_ALLTRAPS)
726 pushq $(T_GENERAL_PROTECTION)
727 jmp trap_check_kernel_exit /* check for kernel exit sequence */
728
729Entry(idt64_stack_fault)
730 PUSH_FUNCTION(HNDL_ALLTRAPS)
731 pushq $(T_STACK_FAULT)
732 jmp trap_check_kernel_exit /* check for kernel exit sequence */
733
734Entry(idt64_segnp)
735 PUSH_FUNCTION(HNDL_ALLTRAPS)
736 pushq $(T_SEGMENT_NOT_PRESENT)
737 /* indicate fault type */
738trap_check_kernel_exit:
739 testb $3,ISF64_CS(%rsp)
740 jz L_kernel_gpf
741
742 /* Here for fault from user-space. Copy interrupt state to PCB. */
743 swapgs
744 push %rax
745 mov %rcx, %gs:CPU_UBER_TMP /* save user RCX */
746 mov %gs:CPU_UBER_ISF, %rcx /* PCB stack addr */
747 mov ISF64_SS+8(%rsp), %rax
748 mov %rax, ISF64_SS(%rcx)
749 mov ISF64_RSP+8(%rsp), %rax
750 mov %rax, ISF64_RSP(%rcx)
751 mov ISF64_RFLAGS+8(%rsp), %rax
752 mov %rax, ISF64_RFLAGS(%rcx)
753 mov ISF64_CS+8(%rsp), %rax
754 mov %rax, ISF64_CS(%rcx)
755 mov ISF64_RIP+8(%rsp), %rax
756 mov %rax, ISF64_RIP(%rcx)
757 mov ISF64_ERR+8(%rsp), %rax
758 mov %rax, ISF64_ERR(%rcx)
759 mov ISF64_TRAPFN+8(%rsp), %rax
760 mov %rax, ISF64_TRAPFN(%rcx)
761 mov ISF64_TRAPNO+8(%rsp), %rax
762 mov %rax, ISF64_TRAPNO(%rcx)
763 pop %rax
764 mov %gs:CPU_UBER_TMP, %rsp /* user RCX into RSP */
765 xchg %rcx, %rsp /* to PCB stack with user RCX */
766 jmp L_dispatch_user
767
768L_kernel_gpf:
769 /* Here for GPF from kernel_space. Check for recoverable cases. */
770 push %rax
771 leaq EXT(ret32_iret)(%rip), %rax
772 cmp %rax, 8+ISF64_RIP(%rsp)
773 je L_fault_iret
774 leaq EXT(ret64_iret)(%rip), %rax
775 cmp %rax, 8+ISF64_RIP(%rsp)
776 je L_fault_iret
777 leaq EXT(ret32_set_ds)(%rip), %rax
778 cmp %rax, 8+ISF64_RIP(%rsp)
779 je L_32bit_fault_set_seg
780 leaq EXT(ret32_set_es)(%rip), %rax
781 cmp %rax, 8+ISF64_RIP(%rsp)
782 je L_32bit_fault_set_seg
783 leaq EXT(ret32_set_fs)(%rip), %rax
784 cmp %rax, 8+ISF64_RIP(%rsp)
785 je L_32bit_fault_set_seg
786 leaq EXT(ret32_set_gs)(%rip), %rax
787 cmp %rax, 8+ISF64_RIP(%rsp)
788 je L_32bit_fault_set_seg
789
790 /* Fall through */
791
792L_kernel_trap:
793 /*
794 * Here after taking an unexpected trap from kernel mode - perhaps
795 * while running in the trampolines hereabouts.
796 * Note: %rax has been pushed on stack.
797 * Make sure we're not on the PCB stack, if so move to the kernel stack.
798 * This is likely a fatal condition.
799 * But first, ensure we have the kernel gs base active...
800 */
801 push %rcx
802 push %rdx
803 mov $(MSR_IA32_GS_BASE), %ecx
804 rdmsr /* read kernel gsbase */
805 test $0x80000000, %edx /* test MSB of address */
806 jne 1f
807 swapgs /* so swap */
8081:
809 pop %rdx
810 pop %rcx
811
812 movq %gs:CPU_UBER_ISF, %rax /* PCB stack addr */
813 subq %rsp, %rax
814 cmpq $(PAGE_SIZE), %rax /* current stack in PCB? */
815 jb 2f /* - yes, deal with it */
816 pop %rax /* - no, restore %rax */
817 jmp L_dispatch_kernel
8182:
819 /*
820 * Here if %rsp is in the PCB
821 * Copy the interrupt stack frame from PCB stack to kernel stack
822 */
823 movq %gs:CPU_KERNEL_STACK, %rax
824 xchgq %rax, %rsp
825 pushq 8+ISF64_SS(%rax)
826 pushq 8+ISF64_RSP(%rax)
827 pushq 8+ISF64_RFLAGS(%rax)
828 pushq 8+ISF64_CS(%rax)
829 pushq 8+ISF64_RIP(%rax)
830 pushq 8+ISF64_ERR(%rax)
831 pushq 8+ISF64_TRAPFN(%rax)
832 pushq 8+ISF64_TRAPNO(%rax)
833 movq (%rax), %rax
834 jmp L_dispatch_kernel
835
836
837/*
838 * GP/NP fault on IRET: CS or SS is in error.
839 * User GSBASE is active.
840 * On IST1 stack containing:
841 * (rax saved above, which is immediately popped)
842 * 0 ISF64_TRAPNO: trap code (NP or GP)
843 * 8 ISF64_TRAPFN: trap function
844 * 16 ISF64_ERR: segment number in error (error code)
845 * 24 ISF64_RIP: kernel RIP
846 * 32 ISF64_CS: kernel CS
847 * 40 ISF64_RFLAGS: kernel RFLAGS
848 * 48 ISF64_RSP: kernel RSP
849 * 56 ISF64_SS: kernel SS
850 * On the PCB stack, pointed to by the kernel's RSP is:
851 * 0 user RIP
852 * 8 user CS
853 * 16 user RFLAGS
854 * 24 user RSP
855 * 32 user SS
856 *
857 * We need to move the kernel's TRAPNO, TRAPFN and ERR to the PCB and handle
858 * as a user fault with:
859 * 0 ISF64_TRAPNO: trap code (NP or GP)
860 * 8 ISF64_TRAPFN: trap function
861 * 16 ISF64_ERR: segment number in error (error code)
862 * 24 user RIP
863 * 32 user CS
864 * 40 user RFLAGS
865 * 48 user RSP
866 * 56 user SS
867 */
868L_fault_iret:
869 pop %rax /* recover saved %rax */
870 mov %rax, ISF64_RIP(%rsp) /* save rax (we don`t need saved rip) */
871 mov ISF64_RSP(%rsp), %rax
872 xchg %rax, %rsp /* switch to PCB stack */
873 push ISF64_ERR(%rax)
874 push ISF64_TRAPFN(%rax)
875 push ISF64_TRAPNO(%rax)
876 mov ISF64_RIP(%rax), %rax /* restore rax */
877 /* now treat as fault from user */
878 jmp L_dispatch
879
880/*
881 * Fault restoring a segment register. All of the saved state is still
882 * on the stack untouched since we haven't yet moved the stack pointer.
883 * On IST1 stack containing:
884 * (rax saved above, which is immediately popped)
885 * 0 ISF64_TRAPNO: trap code (NP or GP)
886 * 8 ISF64_TRAPFN: trap function
887 * 16 ISF64_ERR: segment number in error (error code)
888 * 24 ISF64_RIP: kernel RIP
889 * 32 ISF64_CS: kernel CS
890 * 40 ISF64_RFLAGS: kernel RFLAGS
891 * 48 ISF64_RSP: kernel RSP
892 * 56 ISF64_SS: kernel SS
893 * On the PCB stack, pointed to by the kernel's RSP is:
894 * 0 user trap code
895 * 8 user trap function
896 * 16 user err
897 * 24 user RIP
898 * 32 user CS
899 * 40 user RFLAGS
900 * 48 user RSP
901 * 56 user SS
902 */
903L_32bit_fault_set_seg:
904 swapgs
905 pop %rax /* toss saved %rax from stack */
906 mov ISF64_TRAPNO(%rsp), %rax
907 mov ISF64_TRAPFN(%rsp), %rcx
908 mov ISF64_ERR(%rsp), %rdx
909 mov ISF64_RSP(%rsp), %rsp /* reset stack to saved state */
910 mov %rax,R64_TRAPNO(%rsp)
911 mov %rcx,R64_TRAPFN(%rsp)
912 mov %rdx,R64_ERR(%rsp)
913 /* now treat as fault from user */
914 /* except that all the state is */
915 /* already saved - we just have to */
916 /* move the trapno and error into */
917 /* the compatibility frame */
918 jmp L_dispatch_U32_after_fault
919
920/*
921 * Fatal exception handlers:
922 */
923Entry(idt64_db_task_dbl_fault)
924 PUSH_FUNCTION(HNDL_DOUBLE_FAULT)
925 pushq $(T_DOUBLE_FAULT)
926 jmp L_dispatch
927
928Entry(idt64_db_task_stk_fault)
929 PUSH_FUNCTION(HNDL_DOUBLE_FAULT)
930 pushq $(T_STACK_FAULT)
931 jmp L_dispatch
932
933Entry(idt64_mc)
934 push $(0) /* Error */
935 PUSH_FUNCTION(HNDL_MACHINE_CHECK)
936 pushq $(T_MACHINE_CHECK)
937 jmp L_dispatch
938
939/*
940 * NMI
941 * This may or may not be fatal but extreme care is required
942 * because it may fall when control was already in another trampoline.
943 *
944 * We get here on IST2 stack which is used for NMIs only.
945 * We must be aware of the interrupted state:
946 * - from user-space, we
947 * - copy state to the PCB and continue;
948 * - from kernel-space, we
949 * - copy state to the kernel stack and continue, but
950 * - check what GSBASE was active, set the kernel base and
951 * - ensure that the active state is restored when the NMI is dismissed.
952 */
953Entry(idt64_nmi)
954 push %rax /* save RAX to ISF64_ERR */
955 push %rcx /* save RCX to ISF64_TRAPFN */
956 push %rdx /* save RDX to ISF64_TRAPNO */
957 testb $3, ISF64_CS(%rsp) /* NMI from user-space? */
958 je 1f
959
960 /* From user-space: copy interrupt state to user PCB */
961 swapgs
962 mov %gs:CPU_UBER_ISF, %rcx /* PCB stack addr */
963 add $(ISF64_SIZE), %rcx /* adjust to base of ISF */
964 swapgs /* swap back for L_dispatch */
965 jmp 4f /* Copy state to PCB */
966
9671:
968 /*
969 * From kernel-space:
970 * Determine whether the kernel or user GS is set.
971 * Set the kernel and ensure that we'll swap back correctly at IRET.
972 */
973 mov $(MSR_IA32_GS_BASE), %ecx
974 rdmsr /* read kernel gsbase */
975 test $0x80000000, %edx /* test MSB of address */
976 jne 2f
977 swapgs /* so swap */
978 movl $1, ISF64_CS+4(%rsp) /* and set flag in CS slot */
9792:
980 /*
981 * Determine whether we're on the kernel or interrupt stack
982 * when the NMI hit.
983 */
984 mov ISF64_RSP(%rsp), %rcx
985 mov %gs:CPU_KERNEL_STACK, %rax
986 xor %rcx, %rax
987 and EXT(kernel_stack_mask)(%rip), %rax
988 test %rax, %rax /* are we on the kernel stack? */
989 je 3f /* yes */
990
991 mov %gs:CPU_INT_STACK_TOP, %rax
992 dec %rax /* intr stack top is byte above max */
993 xor %rcx, %rax
994 and EXT(kernel_stack_mask)(%rip), %rax
995 test %rax, %rax /* are we on the interrupt stack? */
996 je 3f /* yes */
997
998 mov %gs:CPU_KERNEL_STACK, %rcx
9993:
1000 /* 16-byte-align kernel/interrupt stack for state push */
1001 and $0xFFFFFFFFFFFFFFF0, %rcx
1002
10034:
1004 /*
1005 * Copy state from NMI stack (RSP) to the save area (RCX) which is
1006 * the PCB for user or kernel/interrupt stack from kernel.
1007 * ISF64_ERR(RSP) saved RAX
1008 * ISF64_TRAPFN(RSP) saved RCX
1009 * ISF64_TRAPNO(RSP) saved RDX
1010 */
1011 xchg %rsp, %rcx /* set for pushes */
1012 push ISF64_SS(%rcx)
1013 push ISF64_RSP(%rcx)
1014 push ISF64_RFLAGS(%rcx)
1015 push ISF64_CS(%rcx)
1016 push ISF64_RIP(%rcx)
1017 push $(0) /* error code 0 */
1018 lea HNDL_ALLINTRS(%rip), %rax
1019 push %rax /* trapfn allintrs */
1020 push $(T_NMI) /* trapno T_NMI */
1021 mov ISF64_ERR(%rcx), %rax
1022 mov ISF64_TRAPNO(%rcx), %rdx
1023 mov ISF64_TRAPFN(%rcx), %rcx
1024 jmp L_dispatch
1025
1026
1027/* All 'exceptions' enter hndl_alltraps, with:
1028 * r15 x86_saved_state_t address
1029 * rsp kernel stack if user-space, otherwise interrupt or kernel stack
1030 * esi cs at trap
1031 *
1032 * The rest of the state is set up as:
1033 * both rsp and r15 are 16-byte aligned
1034 * interrupts disabled
1035 * direction flag cleared
1036 */
1037Entry(hndl_alltraps)
1038 mov %esi, %eax
1039 testb $3, %al
1040 jz trap_from_kernel
1041
1042 TIME_TRAP_UENTRY
1043
1044 /* Check for active vtimers in the current task */
1045 mov %gs:CPU_ACTIVE_THREAD, %rcx
1046 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling trap/exception */
1047 mov TH_TASK(%rcx), %rbx
1048 TASK_VTIMER_CHECK(%rbx, %rcx)
1049
1050 CCALL1(user_trap, %r15) /* call user trap routine */
1051 /* user_trap() unmasks interrupts */
1052 cli /* hold off intrs - critical section */
1053 xorl %ecx, %ecx /* don't check if we're in the PFZ */
1054
1055
1056Entry(return_from_trap)
1057 movq %gs:CPU_ACTIVE_THREAD,%r15 /* Get current thread */
1058 movl $-1, TH_IOTIER_OVERRIDE(%r15) /* Reset IO tier override to -1 before returning to userspace */
1059 cmpl $0, TH_RWLOCK_COUNT(%r15) /* Check if current thread has pending RW locks held */
1060 jz 1f
1061 xorq %rbp, %rbp /* clear framepointer */
1062 mov %r15, %rdi /* Set RDI to current thread */
1063 CCALL(lck_rw_clear_promotions_x86) /* Clear promotions if needed */
10641:
1065 movq TH_PCB_ISS(%r15), %r15 /* PCB stack */
1066 movl %gs:CPU_PENDING_AST,%eax
1067 testl %eax,%eax
1068 je EXT(return_to_user) /* branch if no AST */
1069
1070L_return_from_trap_with_ast:
1071 testl %ecx, %ecx /* see if we need to check for an EIP in the PFZ */
1072 je 2f /* no, go handle the AST */
1073 cmpl $(SS_64), SS_FLAVOR(%r15) /* are we a 64-bit task? */
1074 je 1f
1075 /* no... 32-bit user mode */
1076 movl R32_EIP(%r15), %edi
1077 xorq %rbp, %rbp /* clear framepointer */
1078 CCALL(commpage_is_in_pfz32)
1079 testl %eax, %eax
1080 je 2f /* not in the PFZ... go service AST */
1081 movl %eax, R32_EBX(%r15) /* let the PFZ know we've pended an AST */
1082 jmp EXT(return_to_user)
10831:
1084 movq R64_RIP(%r15), %rdi
1085 xorq %rbp, %rbp /* clear framepointer */
1086 CCALL(commpage_is_in_pfz64)
1087 testl %eax, %eax
1088 je 2f /* not in the PFZ... go service AST */
1089 movl %eax, R64_RBX(%r15) /* let the PFZ know we've pended an AST */
1090 jmp EXT(return_to_user)
10912:
1092
1093 xorq %rbp, %rbp /* clear framepointer */
1094 CCALL(ast_taken_user) /* handle all ASTs (enables interrupts, may return via continuation) */
1095
1096 cli
1097 mov %rsp, %r15 /* AST changes stack, saved state */
1098 xorl %ecx, %ecx /* don't check if we're in the PFZ */
1099 jmp EXT(return_from_trap) /* and check again (rare) */
1100
1101/*
1102 * Trap from kernel mode. No need to switch stacks.
1103 * Interrupts must be off here - we will set them to state at time of trap
1104 * as soon as it's safe for us to do so and not recurse doing preemption
1105 *
1106 */
1107trap_from_kernel:
1108 movq %r15, %rdi /* saved state addr */
1109 pushq R64_RIP(%r15) /* Simulate a CALL from fault point */
1110 pushq %rbp /* Extend framepointer chain */
1111 movq %rsp, %rbp
1112 CCALLWITHSP(kernel_trap) /* to kernel trap routine */
1113 popq %rbp
1114 addq $8, %rsp
1115 mov %rsp, %r15 /* DTrace slides stack/saved-state */
1116 cli
1117
1118 movl %gs:CPU_PENDING_AST,%eax /* get pending asts */
1119 testl $(AST_URGENT),%eax /* any urgent preemption? */
1120 je ret_to_kernel /* no, nothing to do */
1121 cmpl $(T_PREEMPT),R64_TRAPNO(%r15)
1122 je ret_to_kernel /* T_PREEMPT handled in kernel_trap() */
1123 testl $(EFL_IF),R64_RFLAGS(%r15) /* interrupts disabled? */
1124 je ret_to_kernel
1125 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
1126 jne ret_to_kernel
1127 movq %gs:CPU_KERNEL_STACK,%rax
1128 movq %rsp,%rcx
1129 xorq %rax,%rcx
1130 andq EXT(kernel_stack_mask)(%rip),%rcx
1131 testq %rcx,%rcx /* are we on the kernel stack? */
1132 jne ret_to_kernel /* no, skip it */
1133
1134 CCALL(ast_taken_kernel) /* take the AST */
1135
1136 mov %rsp, %r15 /* AST changes stack, saved state */
1137 jmp ret_to_kernel
1138
1139
1140/*
1141 * All interrupts on all tasks enter here with:
1142 * r15 x86_saved_state_t
1143 * rsp kernel or interrupt stack
1144 * esi cs at trap
1145 *
1146 * both rsp and r15 are 16-byte aligned
1147 * interrupts disabled
1148 * direction flag cleared
1149 */
1150Entry(hndl_allintrs)
1151 /*
1152 * test whether already on interrupt stack
1153 */
1154 movq %gs:CPU_INT_STACK_TOP,%rcx
1155 cmpq %rsp,%rcx
1156 jb 1f
1157 leaq -INTSTACK_SIZE(%rcx),%rdx
1158 cmpq %rsp,%rdx
1159 jb int_from_intstack
11601:
1161 xchgq %rcx,%rsp /* switch to interrupt stack */
1162
1163 mov %cr0,%rax /* get cr0 */
1164 orl $(CR0_TS),%eax /* or in TS bit */
1165 mov %rax,%cr0 /* set cr0 */
1166
1167 pushq %rcx /* save pointer to old stack */
1168 pushq %gs:CPU_INT_STATE /* save previous intr state */
1169 movq %r15,%gs:CPU_INT_STATE /* set intr state */
1170
1171 TIME_INT_ENTRY /* do timing */
1172
1173 /* Check for active vtimers in the current task */
1174 mov %gs:CPU_ACTIVE_THREAD, %rcx
1175 mov TH_TASK(%rcx), %rbx
1176 TASK_VTIMER_CHECK(%rbx, %rcx)
1177
1178 incl %gs:CPU_PREEMPTION_LEVEL
1179 incl %gs:CPU_INTERRUPT_LEVEL
1180
1181 CCALL1(interrupt, %r15) /* call generic interrupt routine */
1182
1183 .globl EXT(return_to_iret)
1184LEXT(return_to_iret) /* (label for kdb_kintr and hardclock) */
1185
1186 decl %gs:CPU_INTERRUPT_LEVEL
1187 decl %gs:CPU_PREEMPTION_LEVEL
1188
1189 TIME_INT_EXIT /* do timing */
1190
1191 popq %gs:CPU_INT_STATE /* reset/clear intr state pointer */
1192 popq %rsp /* switch back to old stack */
1193
1194 movq %gs:CPU_ACTIVE_THREAD,%rax
1195 movq TH_PCB_FPS(%rax),%rax /* get pcb's ifps */
1196 cmpq $0,%rax /* Is there a context */
1197 je 1f /* Branch if not */
1198 movl FP_VALID(%rax),%eax /* Load fp_valid */
1199 cmpl $0,%eax /* Check if valid */
1200 jne 1f /* Branch if valid */
1201 clts /* Clear TS */
1202 jmp 2f
12031:
1204 mov %cr0,%rax /* get cr0 */
1205 orl $(CR0_TS),%eax /* or in TS bit */
1206 mov %rax,%cr0 /* set cr0 */
12072:
1208 /* Load interrupted code segment into %eax */
1209 movl R32_CS(%r15),%eax /* assume 32-bit state */
1210 cmpl $(SS_64),SS_FLAVOR(%r15)/* 64-bit? */
1211#if DEBUG_IDT64
1212 jne 4f
1213 movl R64_CS(%r15),%eax /* 64-bit user mode */
1214 jmp 3f
12154:
1216 cmpl $(SS_32),SS_FLAVOR(%r15)
1217 je 3f
1218 POSTCODE2(0x6431)
1219 CCALL1(panic_idt64, %r15)
1220 hlt
1221#else
1222 jne 3f
1223 movl R64_CS(%r15),%eax /* 64-bit user mode */
1224#endif
12253:
1226 testb $3,%al /* user mode, */
1227 jnz ast_from_interrupt_user /* go handle potential ASTs */
1228 /*
1229 * we only want to handle preemption requests if
1230 * the interrupt fell in the kernel context
1231 * and preemption isn't disabled
1232 */
1233 movl %gs:CPU_PENDING_AST,%eax
1234 testl $(AST_URGENT),%eax /* any urgent requests? */
1235 je ret_to_kernel /* no, nothing to do */
1236
1237 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
1238 jne ret_to_kernel /* yes, skip it */
1239
1240 /*
1241 * Take an AST from kernel space. We don't need (and don't want)
1242 * to do as much as the case where the interrupt came from user
1243 * space.
1244 */
1245 CCALL(ast_taken_kernel)
1246
1247 mov %rsp, %r15 /* AST changes stack, saved state */
1248 jmp ret_to_kernel
1249
1250
1251/*
1252 * nested int - simple path, can't preempt etc on way out
1253 */
1254int_from_intstack:
1255 incl %gs:CPU_PREEMPTION_LEVEL
1256 incl %gs:CPU_INTERRUPT_LEVEL
1257 incl %gs:CPU_NESTED_ISTACK
1258
1259 push %gs:CPU_INT_STATE
1260 mov %r15, %gs:CPU_INT_STATE
1261
1262 CCALL1(interrupt, %r15)
1263
1264 pop %gs:CPU_INT_STATE
1265
1266 decl %gs:CPU_INTERRUPT_LEVEL
1267 decl %gs:CPU_PREEMPTION_LEVEL
1268 decl %gs:CPU_NESTED_ISTACK
1269
1270 jmp ret_to_kernel
1271
1272/*
1273 * Take an AST from an interrupted user
1274 */
1275ast_from_interrupt_user:
1276 movl %gs:CPU_PENDING_AST,%eax
1277 testl %eax,%eax /* pending ASTs? */
1278 je EXT(ret_to_user) /* no, nothing to do */
1279
1280 TIME_TRAP_UENTRY
1281
1282 movl $1, %ecx /* check if we're in the PFZ */
1283 jmp L_return_from_trap_with_ast /* return */
1284
1285
1286/* Syscall dispatch routines! */
1287
1288/*
1289 *
1290 * 32bit Tasks
1291 * System call entries via INTR_GATE or sysenter:
1292 *
1293 * r15 x86_saved_state32_t
1294 * rsp kernel stack
1295 *
1296 * both rsp and r15 are 16-byte aligned
1297 * interrupts disabled
1298 * direction flag cleared
1299 */
1300
1301Entry(hndl_sysenter)
1302 /*
1303 * We can be here either for a mach syscall or a unix syscall,
1304 * as indicated by the sign of the code:
1305 */
1306 movl R32_EAX(%r15),%eax
1307 testl %eax,%eax
1308 js EXT(hndl_mach_scall) /* < 0 => mach */
1309 /* > 0 => unix */
1310
1311Entry(hndl_unix_scall)
1312
1313 TIME_TRAP_UENTRY
1314
1315 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1316 movq TH_TASK(%rcx),%rbx /* point to current task */
1317 incl TH_SYSCALLS_UNIX(%rcx) /* increment call count */
1318
1319 /* Check for active vtimers in the current task */
1320 TASK_VTIMER_CHECK(%rbx,%rcx)
1321
1322 sti
1323
1324 CCALL1(unix_syscall, %r15)
1325 /*
1326 * always returns through thread_exception_return
1327 */
1328
1329
1330Entry(hndl_mach_scall)
1331 TIME_TRAP_UENTRY
1332
1333 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1334 movq TH_TASK(%rcx),%rbx /* point to current task */
1335 incl TH_SYSCALLS_MACH(%rcx) /* increment call count */
1336
1337 /* Check for active vtimers in the current task */
1338 TASK_VTIMER_CHECK(%rbx,%rcx)
1339
1340 sti
1341
1342 CCALL1(mach_call_munger, %r15)
1343 /*
1344 * always returns through thread_exception_return
1345 */
1346
1347
1348Entry(hndl_mdep_scall)
1349 TIME_TRAP_UENTRY
1350
1351 /* Check for active vtimers in the current task */
1352 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1353 movq TH_TASK(%rcx),%rbx /* point to current task */
1354 TASK_VTIMER_CHECK(%rbx,%rcx)
1355
1356 sti
1357
1358 CCALL1(machdep_syscall, %r15)
1359 /*
1360 * always returns through thread_exception_return
1361 */
1362
1363/*
1364 * 64bit Tasks
1365 * System call entries via syscall only:
1366 *
1367 * r15 x86_saved_state64_t
1368 * rsp kernel stack
1369 *
1370 * both rsp and r15 are 16-byte aligned
1371 * interrupts disabled
1372 * direction flag cleared
1373 */
1374
1375Entry(hndl_syscall)
1376 TIME_TRAP_UENTRY
1377
1378 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1379 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling syscall */
1380 movq TH_TASK(%rcx),%rbx /* point to current task */
1381
1382 /* Check for active vtimers in the current task */
1383 TASK_VTIMER_CHECK(%rbx,%rcx)
1384
1385 /*
1386 * We can be here either for a mach, unix machdep or diag syscall,
1387 * as indicated by the syscall class:
1388 */
1389 movl R64_RAX(%r15), %eax /* syscall number/class */
1390 movl %eax, %edx
1391 andl $(SYSCALL_CLASS_MASK), %edx /* syscall class */
1392 cmpl $(SYSCALL_CLASS_MACH<<SYSCALL_CLASS_SHIFT), %edx
1393 je EXT(hndl_mach_scall64)
1394 cmpl $(SYSCALL_CLASS_UNIX<<SYSCALL_CLASS_SHIFT), %edx
1395 je EXT(hndl_unix_scall64)
1396 cmpl $(SYSCALL_CLASS_MDEP<<SYSCALL_CLASS_SHIFT), %edx
1397 je EXT(hndl_mdep_scall64)
1398 cmpl $(SYSCALL_CLASS_DIAG<<SYSCALL_CLASS_SHIFT), %edx
1399 je EXT(hndl_diag_scall64)
1400
1401 /* Syscall class unknown */
1402 sti
1403 CCALL3(i386_exception, $(EXC_SYSCALL), %rax, $1)
1404 /* no return */
1405
1406
1407Entry(hndl_unix_scall64)
1408 incl TH_SYSCALLS_UNIX(%rcx) /* increment call count */
1409 sti
1410
1411 CCALL1(unix_syscall64, %r15)
1412 /*
1413 * always returns through thread_exception_return
1414 */
1415
1416
1417Entry(hndl_mach_scall64)
1418 incl TH_SYSCALLS_MACH(%rcx) /* increment call count */
1419 sti
1420
1421 CCALL1(mach_call_munger64, %r15)
1422 /*
1423 * always returns through thread_exception_return
1424 */
1425
1426
1427
1428Entry(hndl_mdep_scall64)
1429 sti
1430
1431 CCALL1(machdep_syscall64, %r15)
1432 /*
1433 * always returns through thread_exception_return
1434 */
1435
1436Entry(hndl_diag_scall64)
1437 CCALL1(diagCall64, %r15) // Call diagnostics
1438 test %eax, %eax // What kind of return is this?
1439 je 1f // - branch if bad (zero)
1440 jmp EXT(return_to_user) // Normal return, do not check asts...
14411:
1442 sti
1443 CCALL3(i386_exception, $EXC_SYSCALL, $0x6000, $1)
1444 /* no return */
1445
1446Entry(hndl_machine_check)
1447 CCALL1(panic_machine_check64, %r15)
1448 hlt
1449
1450Entry(hndl_double_fault)
1451 CCALL1(panic_double_fault64, %r15)
1452 hlt