]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/exception.S
Fix Machine Check Exception on Page Size Change.
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / exception.S
1 /*-
2  * Copyright (c) 1989, 1990 William F. Jolitz.
3  * Copyright (c) 1990 The Regents of the University of California.
4  * Copyright (c) 2007-2018 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Portions of this software were developed by A. Joseph Koshy under
8  * sponsorship from the FreeBSD Foundation and Google, Inc.
9  *
10  * Portions of this software were developed by
11  * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from
12  * the FreeBSD Foundation.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * $FreeBSD$
39  */
40
41 #include "opt_atpic.h"
42 #include "opt_compat.h"
43 #include "opt_hwpmc_hooks.h"
44
45 #include "assym.s"
46
47 #include <machine/asmacros.h>
48 #include <machine/psl.h>
49 #include <machine/trap.h>
50 #include <machine/specialreg.h>
51
52 #ifdef KDTRACE_HOOKS
53         .bss
54         .globl  dtrace_invop_jump_addr
55         .align  8
56         .type   dtrace_invop_jump_addr,@object
57         .size   dtrace_invop_jump_addr,8
58 dtrace_invop_jump_addr:
59         .zero   8
60         .globl  dtrace_invop_calltrap_addr
61         .align  8
62         .type   dtrace_invop_calltrap_addr,@object
63         .size   dtrace_invop_calltrap_addr,8
64 dtrace_invop_calltrap_addr:
65         .zero   8
66 #endif
67         .text
68 #ifdef HWPMC_HOOKS
69         ENTRY(start_exceptions)
70 #endif
71
72 /*****************************************************************************/
73 /* Trap handling                                                             */
74 /*****************************************************************************/
75 /*
76  * Trap and fault vector routines.
77  *
78  * All traps are 'interrupt gates', SDT_SYSIGT.  An interrupt gate pushes
79  * state on the stack but also disables interrupts.  This is important for
80  * us for the use of the swapgs instruction.  We cannot be interrupted
81  * until the GS.base value is correct.  For most traps, we automatically
82  * then enable interrupts if the interrupted context had them enabled.
83  * This is equivalent to the i386 port's use of SDT_SYS386TGT.
84  *
85  * The cpu will push a certain amount of state onto the kernel stack for
86  * the current process.  See amd64/include/frame.h.
87  * This includes the current RFLAGS (status register, which includes
88  * the interrupt disable state prior to the trap), the code segment register,
89  * and the return instruction pointer are pushed by the cpu.  The cpu
90  * will also push an 'error' code for certain traps.  We push a dummy
91  * error code for those traps where the cpu doesn't in order to maintain
92  * a consistent frame.  We also push a contrived 'trap number'.
93  *
94  * The CPU does not push the general registers, so we must do that, and we
95  * must restore them prior to calling 'iret'.  The CPU adjusts %cs and %ss
96  * but does not mess with %ds, %es, %gs or %fs.  We swap the %gs base for
97  * for the kernel mode operation shortly, without changes to the selector
98  * loaded.  Since superuser long mode works with any selectors loaded into
99  * segment registers other then %cs, which makes them mostly unused in long
100  * mode, and kernel does not reference %fs, leave them alone.  The segment
101  * registers are reloaded on return to the usermode.
102  */
103
104 MCOUNT_LABEL(user)
105 MCOUNT_LABEL(btrap)
106
107 /* Traps that we leave interrupts disabled for. */
108         .macro  TRAP_NOEN       l, trapno
109         PTI_ENTRY       \l,X\l
110         .globl  X\l
111         .type   X\l,@function
112 X\l:    subq $TF_RIP,%rsp
113         movl $\trapno,TF_TRAPNO(%rsp)
114         movq $0,TF_ADDR(%rsp)
115         movq $0,TF_ERR(%rsp)
116         jmp alltraps_noen
117         .endm
118
119         TRAP_NOEN       bpt, T_BPTFLT
120 #ifdef KDTRACE_HOOKS
121         TRAP_NOEN       dtrace_ret, T_DTRACE_RET
122 #endif
123
124 /* Regular traps; The cpu does not supply tf_err for these. */
125         .macro  TRAP    l, trapno
126         PTI_ENTRY       \l,X\l
127         .globl  X\l
128         .type   X\l,@function
129 X\l:
130         subq $TF_RIP,%rsp
131         movl $\trapno,TF_TRAPNO(%rsp)
132         movq $0,TF_ADDR(%rsp)
133         movq $0,TF_ERR(%rsp)
134         jmp alltraps
135         .endm
136
137         TRAP    div, T_DIVIDE
138         TRAP    ofl, T_OFLOW
139         TRAP    bnd, T_BOUND
140         TRAP    ill, T_PRIVINFLT
141         TRAP    dna, T_DNA
142         TRAP    fpusegm, T_FPOPFLT
143         TRAP    rsvd, T_RESERVED
144         TRAP    fpu, T_ARITHTRAP
145         TRAP    xmm, T_XMMFLT
146
147 /* This group of traps have tf_err already pushed by the cpu. */
148         .macro  TRAP_ERR        l, trapno
149         PTI_ENTRY       \l,X\l,has_err=1
150         .globl  X\l
151         .type   X\l,@function
152 X\l:
153         subq $TF_ERR,%rsp
154         movl $\trapno,TF_TRAPNO(%rsp)
155         movq $0,TF_ADDR(%rsp)
156         jmp alltraps
157         .endm
158
159         TRAP_ERR        tss, T_TSSFLT
160         TRAP_ERR        align, T_ALIGNFLT
161
162         /*
163          * alltraps entry point.  Use swapgs if this is the first time in the
164          * kernel from userland.  Reenable interrupts if they were enabled
165          * before the trap.  This approximates SDT_SYS386TGT on the i386 port.
166          */
167         SUPERALIGN_TEXT
168         .globl  alltraps
169         .type   alltraps,@function
170 alltraps:
171         movq    %rdi,TF_RDI(%rsp)
172         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
173         jz      1f              /* already running with kernel GS.base */
174         swapgs
175         movq    PCPU(CURPCB),%rdi
176         andl    $~PCB_FULL_IRET,PCB_FLAGS(%rdi)
177 1:      SAVE_SEGS
178         movq    %rdx,TF_RDX(%rsp)
179         movq    %rax,TF_RAX(%rsp)
180         movq    %rcx,TF_RCX(%rsp)
181         testb   $SEL_RPL_MASK,TF_CS(%rsp)
182         jz      2f
183         call    handle_ibrs_entry
184 2:      testl   $PSL_I,TF_RFLAGS(%rsp)
185         jz      alltraps_pushregs_no_rax
186         sti
187 alltraps_pushregs_no_rax:
188         movq    %rsi,TF_RSI(%rsp)
189         movq    %r8,TF_R8(%rsp)
190         movq    %r9,TF_R9(%rsp)
191         movq    %rbx,TF_RBX(%rsp)
192         movq    %rbp,TF_RBP(%rsp)
193         movq    %r10,TF_R10(%rsp)
194         movq    %r11,TF_R11(%rsp)
195         movq    %r12,TF_R12(%rsp)
196         movq    %r13,TF_R13(%rsp)
197         movq    %r14,TF_R14(%rsp)
198         movq    %r15,TF_R15(%rsp)
199         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
200         cld
201         FAKE_MCOUNT(TF_RIP(%rsp))
202 #ifdef KDTRACE_HOOKS
203         /*
204          * DTrace Function Boundary Trace (fbt) probes are triggered
205          * by int3 (0xcc) which causes the #BP (T_BPTFLT) breakpoint
206          * interrupt. For all other trap types, just handle them in
207          * the usual way.
208          */
209         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
210         jnz     calltrap                /* ignore userland traps */
211         cmpl    $T_BPTFLT,TF_TRAPNO(%rsp)
212         jne     calltrap
213
214         /* Check if there is no DTrace hook registered. */
215         cmpq    $0,dtrace_invop_jump_addr
216         je      calltrap
217
218         /*
219          * Set our jump address for the jump back in the event that
220          * the breakpoint wasn't caused by DTrace at all.
221          */
222         movq    $calltrap,dtrace_invop_calltrap_addr(%rip)
223
224         /* Jump to the code hooked in by DTrace. */
225         jmpq    *dtrace_invop_jump_addr
226 #endif
227         .globl  calltrap
228         .type   calltrap,@function
229 calltrap:
230         movq    %rsp,%rdi
231         call    trap_check
232         MEXITCOUNT
233         jmp     doreti                  /* Handle any pending ASTs */
234
235         /*
236          * alltraps_noen entry point.  Unlike alltraps above, we want to
237          * leave the interrupts disabled.  This corresponds to
238          * SDT_SYS386IGT on the i386 port.
239          */
240         SUPERALIGN_TEXT
241         .globl  alltraps_noen
242         .type   alltraps_noen,@function
243 alltraps_noen:
244         movq    %rdi,TF_RDI(%rsp)
245         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
246         jz      1f /* already running with kernel GS.base */
247         swapgs
248         movq    PCPU(CURPCB),%rdi
249         andl    $~PCB_FULL_IRET,PCB_FLAGS(%rdi)
250 1:      SAVE_SEGS
251         movq    %rdx,TF_RDX(%rsp)
252         movq    %rax,TF_RAX(%rsp)
253         movq    %rcx,TF_RCX(%rsp)
254         testb   $SEL_RPL_MASK,TF_CS(%rsp)
255         jz      alltraps_pushregs_no_rax
256         call    handle_ibrs_entry
257         jmp     alltraps_pushregs_no_rax
258
259 IDTVEC(dblfault)
260         subq    $TF_ERR,%rsp
261         movl    $T_DOUBLEFLT,TF_TRAPNO(%rsp)
262         movq    $0,TF_ADDR(%rsp)
263         movq    $0,TF_ERR(%rsp)
264         movq    %rdi,TF_RDI(%rsp)
265         movq    %rsi,TF_RSI(%rsp)
266         movq    %rdx,TF_RDX(%rsp)
267         movq    %rcx,TF_RCX(%rsp)
268         movq    %r8,TF_R8(%rsp)
269         movq    %r9,TF_R9(%rsp)
270         movq    %rax,TF_RAX(%rsp)
271         movq    %rbx,TF_RBX(%rsp)
272         movq    %rbp,TF_RBP(%rsp)
273         movq    %r10,TF_R10(%rsp)
274         movq    %r11,TF_R11(%rsp)
275         movq    %r12,TF_R12(%rsp)
276         movq    %r13,TF_R13(%rsp)
277         movq    %r14,TF_R14(%rsp)
278         movq    %r15,TF_R15(%rsp)
279         SAVE_SEGS
280         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
281         cld
282         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
283         jz      1f                      /* already running with kernel GS.base */
284         swapgs
285 1:
286         movq    PCPU(KCR3),%rax
287         cmpq    $~0,%rax
288         je      2f
289         movq    %rax,%cr3
290 2:      movq    %rsp,%rdi
291         call    dblfault_handler
292 3:      hlt
293         jmp     3b
294
295         ALIGN_TEXT
296 IDTVEC(page_pti)
297         testb   $SEL_RPL_MASK,PTI_CS-2*8(%rsp)
298         jz      Xpage
299         swapgs
300         pushq   %rax
301         movq    %cr3,%rax
302         movq    %rax,PCPU(SAVED_UCR3)
303         cmpq    $~0,PCPU(UCR3)
304         jne     1f
305         popq    %rax
306         jmp     2f
307 1:      pushq   %rdx
308         PTI_UUENTRY has_err=1
309 2:      subq    $TF_ERR,%rsp
310         movq    %rdi,TF_RDI(%rsp)
311         movq    %rax,TF_RAX(%rsp)
312         movq    %rdx,TF_RDX(%rsp)
313         movq    %rcx,TF_RCX(%rsp)
314         jmp     page_u
315 IDTVEC(page)
316         subq    $TF_ERR,%rsp
317         movq    %rdi,TF_RDI(%rsp)       /* free up GP registers */
318         movq    %rax,TF_RAX(%rsp)
319         movq    %rdx,TF_RDX(%rsp)
320         movq    %rcx,TF_RCX(%rsp)
321         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
322         jz      page_cr2                /* already running with kernel GS.base */
323         swapgs
324 page_u: movq    PCPU(CURPCB),%rdi
325         andl    $~PCB_FULL_IRET,PCB_FLAGS(%rdi)
326         movq    PCPU(SAVED_UCR3),%rax
327         movq    %rax,PCB_SAVED_UCR3(%rdi)
328         call    handle_ibrs_entry
329 page_cr2:
330         movq    %cr2,%rdi               /* preserve %cr2 before ..  */
331         movq    %rdi,TF_ADDR(%rsp)      /* enabling interrupts. */
332         SAVE_SEGS
333         movl    $T_PAGEFLT,TF_TRAPNO(%rsp)
334         testl   $PSL_I,TF_RFLAGS(%rsp)
335         jz      alltraps_pushregs_no_rax
336         sti
337         jmp     alltraps_pushregs_no_rax
338
339         /*
340          * We have to special-case this one.  If we get a trap in doreti() at
341          * the iretq stage, we'll reenter with the wrong gs state.  We'll have
342          * to do a special the swapgs in this case even coming from the kernel.
343          * XXX linux has a trap handler for their equivalent of load_gs().
344          *
345          * On the stack, we have the hardware interrupt frame to return
346          * to usermode (faulted) and another frame with error code, for
347          * fault.  For PTI, copy both frames to the main thread stack.
348          * Handle the potential 16-byte alignment adjustment incurred
349          * during the second fault by copying both frames independently
350          * while unwinding the stack in between.
351          */
352         .macro PROTF_ENTRY name,trapno
353 \name\()_pti_doreti:
354         swapgs
355         cmpq    $~0,PCPU(UCR3)
356         je      1f
357         pushq   %rax
358         pushq   %rdx
359         movq    PCPU(KCR3),%rax
360         movq    %rax,%cr3
361         movq    PCPU(RSP0),%rax
362         subq    $2*PTI_SIZE-3*8,%rax /* no err, %rax, %rdx in faulted frame */
363         MOVE_STACKS     (PTI_SIZE / 8)
364         addq    $PTI_SIZE,%rax
365         movq    PTI_RSP(%rsp),%rsp
366         MOVE_STACKS     (PTI_SIZE / 8 - 3)
367         subq    $PTI_SIZE,%rax
368         movq    %rax,%rsp
369         popq    %rdx
370         popq    %rax
371 1:      swapgs
372         jmp     X\name
373 IDTVEC(\name\()_pti)
374         cmpq    $doreti_iret,PTI_RIP-2*8(%rsp)
375         je      \name\()_pti_doreti
376         testb   $SEL_RPL_MASK,PTI_CS-2*8(%rsp) /* %rax, %rdx not yet pushed */
377         jz      X\name
378         PTI_UENTRY has_err=1
379         swapgs
380 IDTVEC(\name)
381         subq    $TF_ERR,%rsp
382         movl    $\trapno,TF_TRAPNO(%rsp)
383         jmp     prot_addrf
384         .endm
385
386         PROTF_ENTRY     missing, T_SEGNPFLT
387         PROTF_ENTRY     stk, T_STKFLT
388         PROTF_ENTRY     prot, T_PROTFLT
389
390 prot_addrf:
391         movq    $0,TF_ADDR(%rsp)
392         movq    %rdi,TF_RDI(%rsp)       /* free up a GP register */
393         movq    %rax,TF_RAX(%rsp)
394         movq    %rdx,TF_RDX(%rsp)
395         movq    %rcx,TF_RCX(%rsp)
396         movw    %fs,TF_FS(%rsp)
397         movw    %gs,TF_GS(%rsp)
398         leaq    doreti_iret(%rip),%rdi
399         cmpq    %rdi,TF_RIP(%rsp)
400         je      5f                      /* kernel but with user gsbase!! */
401         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
402         jz      6f                      /* already running with kernel GS.base */
403         testb   $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
404         jz      2f
405         cmpw    $KUF32SEL,TF_FS(%rsp)
406         jne     1f
407         rdfsbase %rax
408 1:      cmpw    $KUG32SEL,TF_GS(%rsp)
409         jne     2f
410         rdgsbase %rdx
411 2:      swapgs
412         movq    PCPU(CURPCB),%rdi
413         testb   $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
414         jz      4f
415         cmpw    $KUF32SEL,TF_FS(%rsp)
416         jne     3f
417         movq    %rax,PCB_FSBASE(%rdi)
418 3:      cmpw    $KUG32SEL,TF_GS(%rsp)
419         jne     4f
420         movq    %rdx,PCB_GSBASE(%rdi)
421 4:      call    handle_ibrs_entry
422         orl     $PCB_FULL_IRET,PCB_FLAGS(%rdi)  /* always full iret from GPF */
423         movw    %es,TF_ES(%rsp)
424         movw    %ds,TF_DS(%rsp)
425         testl   $PSL_I,TF_RFLAGS(%rsp)
426         jz      alltraps_pushregs_no_rax
427         sti
428         jmp     alltraps_pushregs_no_rax
429
430 5:      swapgs
431 6:      movq    PCPU(CURPCB),%rdi
432         jmp     4b
433
434 /*
435  * Fast syscall entry point.  We enter here with just our new %cs/%ss set,
436  * and the new privilige level.  We are still running on the old user stack
437  * pointer.  We have to juggle a few things around to find our stack etc.
438  * swapgs gives us access to our PCPU space only.
439  *
440  * We do not support invoking this from a custom segment registers,
441  * esp. %cs, %ss, %fs, %gs, e.g. using entries from an LDT.
442  */
443         SUPERALIGN_TEXT
444 IDTVEC(fast_syscall_pti)
445         swapgs
446         movq    %rax,PCPU(SCRATCH_RAX)
447         cmpq    $~0,PCPU(UCR3)
448         je      fast_syscall_common
449         movq    PCPU(KCR3),%rax
450         movq    %rax,%cr3
451         jmp     fast_syscall_common
452         SUPERALIGN_TEXT
453 IDTVEC(fast_syscall)
454         swapgs
455         movq    %rax,PCPU(SCRATCH_RAX)
456 fast_syscall_common:
457         movq    %rsp,PCPU(SCRATCH_RSP)
458         movq    PCPU(RSP0),%rsp
459         /* Now emulate a trapframe. Make the 8 byte alignment odd for call. */
460         subq    $TF_SIZE,%rsp
461         /* defer TF_RSP till we have a spare register */
462         movq    %r11,TF_RFLAGS(%rsp)
463         movq    %rcx,TF_RIP(%rsp)       /* %rcx original value is in %r10 */
464         movq    PCPU(SCRATCH_RSP),%r11  /* %r11 already saved */
465         movq    %r11,TF_RSP(%rsp)       /* user stack pointer */
466         movq    PCPU(SCRATCH_RAX),%rax
467         movq    %rax,TF_RAX(%rsp)       /* syscall number */
468         movq    %rdx,TF_RDX(%rsp)       /* arg 3 */
469         SAVE_SEGS
470         call    handle_ibrs_entry
471         movq    PCPU(CURPCB),%r11
472         andl    $~PCB_FULL_IRET,PCB_FLAGS(%r11)
473         sti
474         movq    $KUDSEL,TF_SS(%rsp)
475         movq    $KUCSEL,TF_CS(%rsp)
476         movq    $2,TF_ERR(%rsp)
477         movq    %rdi,TF_RDI(%rsp)       /* arg 1 */
478         movq    %rsi,TF_RSI(%rsp)       /* arg 2 */
479         movq    %r10,TF_RCX(%rsp)       /* arg 4 */
480         movq    %r8,TF_R8(%rsp)         /* arg 5 */
481         movq    %r9,TF_R9(%rsp)         /* arg 6 */
482         movq    %rbx,TF_RBX(%rsp)       /* C preserved */
483         movq    %rbp,TF_RBP(%rsp)       /* C preserved */
484         movq    %r12,TF_R12(%rsp)       /* C preserved */
485         movq    %r13,TF_R13(%rsp)       /* C preserved */
486         movq    %r14,TF_R14(%rsp)       /* C preserved */
487         movq    %r15,TF_R15(%rsp)       /* C preserved */
488         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
489         FAKE_MCOUNT(TF_RIP(%rsp))
490         movq    PCPU(CURTHREAD),%rdi
491         movq    %rsp,TD_FRAME(%rdi)
492         movl    TF_RFLAGS(%rsp),%esi
493         andl    $PSL_T,%esi
494         call    amd64_syscall
495 1:      movq    PCPU(CURPCB),%rax
496         /* Disable interrupts before testing PCB_FULL_IRET. */
497         cli
498         testl   $PCB_FULL_IRET,PCB_FLAGS(%rax)
499         jnz     4f
500         /* Check for and handle AST's on return to userland. */
501         movq    PCPU(CURTHREAD),%rax
502         testl   $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%rax)
503         jne     3f
504         call    handle_ibrs_exit
505         callq   *mds_handler
506         /* Restore preserved registers. */
507         MEXITCOUNT
508         movq    TF_RDI(%rsp),%rdi       /* bonus; preserve arg 1 */
509         movq    TF_RSI(%rsp),%rsi       /* bonus: preserve arg 2 */
510         movq    TF_RDX(%rsp),%rdx       /* return value 2 */
511         movq    TF_RAX(%rsp),%rax       /* return value 1 */
512         movq    TF_RFLAGS(%rsp),%r11    /* original %rflags */
513         movq    TF_RIP(%rsp),%rcx       /* original %rip */
514         movq    TF_RSP(%rsp),%rsp       /* user stack pointer */
515         xorl    %r8d,%r8d               /* zero the rest of GPRs */
516         xorl    %r10d,%r10d
517         cmpq    $~0,PCPU(UCR3)
518         je      2f
519         movq    PCPU(UCR3),%r9
520         movq    %r9,%cr3
521 2:      xorl    %r9d,%r9d
522         swapgs
523         sysretq
524
525 3:      /* AST scheduled. */
526         sti
527         movq    %rsp,%rdi
528         call    ast
529         jmp     1b
530
531 4:      /* Requested full context restore, use doreti for that. */
532         MEXITCOUNT
533         jmp     doreti
534
535 /*
536  * Here for CYA insurance, in case a "syscall" instruction gets
537  * issued from 32 bit compatibility mode. MSR_CSTAR has to point
538  * to *something* if EFER_SCE is enabled.
539  */
540 IDTVEC(fast_syscall32)
541         sysret
542
543 /*
544  * DB# handler is very similar to NM#, because 'mov/pop %ss' delay
545  * generation of exception until the next instruction is executed,
546  * which might be a kernel entry.  So we must execute the handler
547  * on IST stack and be ready for non-kernel GSBASE.
548  */
549 IDTVEC(dbg)
550         subq    $TF_RIP,%rsp
551         movl    $(T_TRCTRAP),TF_TRAPNO(%rsp)
552         movq    $0,TF_ADDR(%rsp)
553         movq    $0,TF_ERR(%rsp)
554         movq    %rdi,TF_RDI(%rsp)
555         movq    %rsi,TF_RSI(%rsp)
556         movq    %rdx,TF_RDX(%rsp)
557         movq    %rcx,TF_RCX(%rsp)
558         movq    %r8,TF_R8(%rsp)
559         movq    %r9,TF_R9(%rsp)
560         movq    %rax,TF_RAX(%rsp)
561         movq    %rbx,TF_RBX(%rsp)
562         movq    %rbp,TF_RBP(%rsp)
563         movq    %r10,TF_R10(%rsp)
564         movq    %r11,TF_R11(%rsp)
565         movq    %r12,TF_R12(%rsp)
566         movq    %r13,TF_R13(%rsp)
567         movq    %r14,TF_R14(%rsp)
568         movq    %r15,TF_R15(%rsp)
569         SAVE_SEGS
570         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
571         cld
572         testb   $SEL_RPL_MASK,TF_CS(%rsp)
573         jnz     dbg_fromuserspace
574         /*
575          * We've interrupted the kernel.  Preserve GS.base in %r12,
576          * %cr3 in %r13, and possibly lower half of MSR_IA32_SPEC_CTL in %r14d.
577          */
578         movl    $MSR_GSBASE,%ecx
579         rdmsr
580         movq    %rax,%r12
581         shlq    $32,%rdx
582         orq     %rdx,%r12
583         /* Retrieve and load the canonical value for GS.base. */
584         movq    TF_SIZE(%rsp),%rdx
585         movl    %edx,%eax
586         shrq    $32,%rdx
587         wrmsr
588         movq    %cr3,%r13
589         movq    PCPU(KCR3),%rax
590         cmpq    $~0,%rax
591         je      1f
592         movq    %rax,%cr3
593 1:      testl   $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
594         je      2f
595         movl    $MSR_IA32_SPEC_CTRL,%ecx
596         rdmsr
597         movl    %eax,%r14d
598         call    handle_ibrs_entry
599 2:      FAKE_MCOUNT(TF_RIP(%rsp))
600         movq    %rsp,%rdi
601         call    trap
602         MEXITCOUNT
603         testl   $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
604         je      3f
605         movl    %r14d,%eax
606         xorl    %edx,%edx
607         movl    $MSR_IA32_SPEC_CTRL,%ecx
608         wrmsr
609         /*
610          * Put back the preserved MSR_GSBASE value.
611          */
612 3:      movl    $MSR_GSBASE,%ecx
613         movq    %r12,%rdx
614         movl    %edx,%eax
615         shrq    $32,%rdx
616         wrmsr
617         movq    %r13,%cr3
618         RESTORE_REGS
619         addq    $TF_RIP,%rsp
620         jmp     doreti_iret
621 dbg_fromuserspace:
622         /*
623          * Switch to kernel GSBASE and kernel page table, and copy frame
624          * from the IST stack to the normal kernel stack, since trap()
625          * re-enables interrupts, and since we might trap on DB# while
626          * in trap().
627          */
628         swapgs
629         movq    PCPU(KCR3),%rax
630         cmpq    $~0,%rax
631         je      1f
632         movq    %rax,%cr3
633 1:      movq    PCPU(RSP0),%rax
634         movl    $TF_SIZE,%ecx
635         subq    %rcx,%rax
636         movq    %rax,%rdi
637         movq    %rsp,%rsi
638         rep;movsb
639         movq    %rax,%rsp
640         call    handle_ibrs_entry
641         movq    PCPU(CURPCB),%rdi
642         orl     $PCB_FULL_IRET,PCB_FLAGS(%rdi)
643         testb   $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
644         jz      3f
645         cmpw    $KUF32SEL,TF_FS(%rsp)
646         jne     2f
647         rdfsbase %rax
648         movq    %rax,PCB_FSBASE(%rdi)
649 2:      cmpw    $KUG32SEL,TF_GS(%rsp)
650         jne     3f
651         movl    $MSR_KGSBASE,%ecx
652         rdmsr
653         shlq    $32,%rdx
654         orq     %rdx,%rax
655         movq    %rax,PCB_GSBASE(%rdi)
656 3:      jmp     calltrap
657
658 /*
659  * NMI handling is special.
660  *
661  * First, NMIs do not respect the state of the processor's RFLAGS.IF
662  * bit.  The NMI handler may be entered at any time, including when
663  * the processor is in a critical section with RFLAGS.IF == 0.
664  * The processor's GS.base value could be invalid on entry to the
665  * handler.
666  *
667  * Second, the processor treats NMIs specially, blocking further NMIs
668  * until an 'iretq' instruction is executed.  We thus need to execute
669  * the NMI handler with interrupts disabled, to prevent a nested interrupt
670  * from executing an 'iretq' instruction and inadvertently taking the
671  * processor out of NMI mode.
672  *
673  * Third, the NMI handler runs on its own stack (tss_ist2). The canonical
674  * GS.base value for the processor is stored just above the bottom of its
675  * NMI stack.  For NMIs taken from kernel mode, the current value in
676  * the processor's GS.base is saved at entry to C-preserved register %r12,
677  * the canonical value for GS.base is then loaded into the processor, and
678  * the saved value is restored at exit time.  For NMIs taken from user mode,
679  * the cheaper 'SWAPGS' instructions are used for swapping GS.base.
680  */
681
682 IDTVEC(nmi)
683         subq    $TF_RIP,%rsp
684         movl    $(T_NMI),TF_TRAPNO(%rsp)
685         movq    $0,TF_ADDR(%rsp)
686         movq    $0,TF_ERR(%rsp)
687         movq    %rdi,TF_RDI(%rsp)
688         movq    %rsi,TF_RSI(%rsp)
689         movq    %rdx,TF_RDX(%rsp)
690         movq    %rcx,TF_RCX(%rsp)
691         movq    %r8,TF_R8(%rsp)
692         movq    %r9,TF_R9(%rsp)
693         movq    %rax,TF_RAX(%rsp)
694         movq    %rbx,TF_RBX(%rsp)
695         movq    %rbp,TF_RBP(%rsp)
696         movq    %r10,TF_R10(%rsp)
697         movq    %r11,TF_R11(%rsp)
698         movq    %r12,TF_R12(%rsp)
699         movq    %r13,TF_R13(%rsp)
700         movq    %r14,TF_R14(%rsp)
701         movq    %r15,TF_R15(%rsp)
702         SAVE_SEGS
703         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
704         cld
705         xorl    %ebx,%ebx
706         testb   $SEL_RPL_MASK,TF_CS(%rsp)
707         jnz     nmi_fromuserspace
708         /*
709          * We've interrupted the kernel.  Preserve GS.base in %r12,
710          * %cr3 in %r13, and possibly lower half of MSR_IA32_SPEC_CTL in %r14d.
711          */
712         movl    $MSR_GSBASE,%ecx
713         rdmsr
714         movq    %rax,%r12
715         shlq    $32,%rdx
716         orq     %rdx,%r12
717         /* Retrieve and load the canonical value for GS.base. */
718         movq    TF_SIZE(%rsp),%rdx
719         movl    %edx,%eax
720         shrq    $32,%rdx
721         wrmsr
722         movq    %cr3,%r13
723         movq    PCPU(KCR3),%rax
724         cmpq    $~0,%rax
725         je      1f
726         movq    %rax,%cr3
727 1:      testl   $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
728         je      nmi_calltrap
729         movl    $MSR_IA32_SPEC_CTRL,%ecx
730         rdmsr
731         movl    %eax,%r14d
732         call    handle_ibrs_entry
733         jmp     nmi_calltrap
734 nmi_fromuserspace:
735         incl    %ebx
736         swapgs
737         movq    %cr3,%r13
738         movq    PCPU(KCR3),%rax
739         cmpq    $~0,%rax
740         je      1f
741         movq    %rax,%cr3
742 1:      call    handle_ibrs_entry
743         movq    PCPU(CURPCB),%rdi
744         testq   %rdi,%rdi
745         jz      3f
746         orl     $PCB_FULL_IRET,PCB_FLAGS(%rdi)
747         testb   $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
748         jz      3f
749         cmpw    $KUF32SEL,TF_FS(%rsp)
750         jne     2f
751         rdfsbase %rax
752         movq    %rax,PCB_FSBASE(%rdi)
753 2:      cmpw    $KUG32SEL,TF_GS(%rsp)
754         jne     3f
755         movl    $MSR_KGSBASE,%ecx
756         rdmsr
757         shlq    $32,%rdx
758         orq     %rdx,%rax
759         movq    %rax,PCB_GSBASE(%rdi)
760 3:
761 /* Note: this label is also used by ddb and gdb: */
762 nmi_calltrap:
763         FAKE_MCOUNT(TF_RIP(%rsp))
764         movq    %rsp,%rdi
765         call    trap
766         MEXITCOUNT
767 #ifdef HWPMC_HOOKS
768         /*
769          * Capture a userspace callchain if needed.
770          *
771          * - Check if the current trap was from user mode.
772          * - Check if the current thread is valid.
773          * - Check if the thread requires a user call chain to be
774          *   captured.
775          *
776          * We are still in NMI mode at this point.
777          */
778         testl   %ebx,%ebx
779         jz      nocallchain     /* not from userspace */
780         movq    PCPU(CURTHREAD),%rax
781         orq     %rax,%rax       /* curthread present? */
782         jz      nocallchain
783         /*
784          * Move execution to the regular kernel stack, because we
785          * committed to return through doreti.
786          */
787         movq    %rsp,%rsi       /* source stack pointer */
788         movq    $TF_SIZE,%rcx
789         movq    PCPU(RSP0),%rdx
790         subq    %rcx,%rdx
791         movq    %rdx,%rdi       /* destination stack pointer */
792         shrq    $3,%rcx         /* trap frame size in long words */
793         cld
794         rep
795         movsq                   /* copy trapframe */
796         movq    %rdx,%rsp       /* we are on the regular kstack */
797
798         testl   $TDP_CALLCHAIN,TD_PFLAGS(%rax) /* flagged for capture? */
799         jz      nocallchain
800         /*
801          * A user callchain is to be captured, so:
802          * - Take the processor out of "NMI" mode by faking an "iret",
803          *   to allow for nested NMI interrupts.
804          * - Enable interrupts, so that copyin() can work.
805          */
806         movl    %ss,%eax
807         pushq   %rax            /* tf_ss */
808         pushq   %rdx            /* tf_rsp (on kernel stack) */
809         pushfq                  /* tf_rflags */
810         movl    %cs,%eax
811         pushq   %rax            /* tf_cs */
812         pushq   $outofnmi       /* tf_rip */
813         iretq
814 outofnmi:
815         /*
816          * At this point the processor has exited NMI mode and is running
817          * with interrupts turned off on the normal kernel stack.
818          *
819          * If a pending NMI gets recognized at or after this point, it
820          * will cause a kernel callchain to be traced.
821          *
822          * We turn interrupts back on, and call the user callchain capture hook.
823          */
824         movq    pmc_hook,%rax
825         orq     %rax,%rax
826         jz      nocallchain
827         movq    PCPU(CURTHREAD),%rdi            /* thread */
828         movq    $PMC_FN_USER_CALLCHAIN,%rsi     /* command */
829         movq    %rsp,%rdx                       /* frame */
830         sti
831         call    *%rax
832         cli
833 nocallchain:
834 #endif
835         testl   %ebx,%ebx       /* %ebx == 0 => return to userland */
836         jnz     doreti_exit
837         /*
838          * Restore speculation control MSR, if preserved.
839          */
840         testl   $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
841         je      1f
842         movl    %r14d,%eax
843         xorl    %edx,%edx
844         movl    $MSR_IA32_SPEC_CTRL,%ecx
845         wrmsr
846         /*
847          * Put back the preserved MSR_GSBASE value.
848          */
849 1:      movl    $MSR_GSBASE,%ecx
850         movq    %r12,%rdx
851         movl    %edx,%eax
852         shrq    $32,%rdx
853         wrmsr
854         cmpb    $0, nmi_flush_l1d_sw(%rip)
855         je      2f
856         call    flush_l1d_sw            /* bhyve L1TF assist */
857 2:      movq    %r13,%cr3
858         RESTORE_REGS
859         addq    $TF_RIP,%rsp
860         jmp     doreti_iret
861
862 /*
863  * MC# handling is similar to NMI.
864  *
865  * As with NMIs, machine check exceptions do not respect RFLAGS.IF and
866  * can occur at any time with a GS.base value that does not correspond
867  * to the privilege level in CS.
868  *
869  * Machine checks are not unblocked by iretq, but it is best to run
870  * the handler with interrupts disabled since the exception may have
871  * interrupted a critical section.
872  *
873  * The MC# handler runs on its own stack (tss_ist3).  The canonical
874  * GS.base value for the processor is stored just above the bottom of
875  * its MC# stack.  For exceptions taken from kernel mode, the current
876  * value in the processor's GS.base is saved at entry to C-preserved
877  * register %r12, the canonical value for GS.base is then loaded into
878  * the processor, and the saved value is restored at exit time.  For
879  * exceptions taken from user mode, the cheaper 'SWAPGS' instructions
880  * are used for swapping GS.base.
881  */
882
883 IDTVEC(mchk)
884         subq    $TF_RIP,%rsp
885         movl    $(T_MCHK),TF_TRAPNO(%rsp)
886         movq    $0,TF_ADDR(%rsp)
887         movq    $0,TF_ERR(%rsp)
888         movq    %rdi,TF_RDI(%rsp)
889         movq    %rsi,TF_RSI(%rsp)
890         movq    %rdx,TF_RDX(%rsp)
891         movq    %rcx,TF_RCX(%rsp)
892         movq    %r8,TF_R8(%rsp)
893         movq    %r9,TF_R9(%rsp)
894         movq    %rax,TF_RAX(%rsp)
895         movq    %rbx,TF_RBX(%rsp)
896         movq    %rbp,TF_RBP(%rsp)
897         movq    %r10,TF_R10(%rsp)
898         movq    %r11,TF_R11(%rsp)
899         movq    %r12,TF_R12(%rsp)
900         movq    %r13,TF_R13(%rsp)
901         movq    %r14,TF_R14(%rsp)
902         movq    %r15,TF_R15(%rsp)
903         SAVE_SEGS
904         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
905         cld
906         xorl    %ebx,%ebx
907         testb   $SEL_RPL_MASK,TF_CS(%rsp)
908         jnz     mchk_fromuserspace
909         /*
910          * We've interrupted the kernel.  Preserve GS.base in %r12,
911          * %cr3 in %r13, and possibly lower half of MSR_IA32_SPEC_CTL in %r14d.
912          */
913         movl    $MSR_GSBASE,%ecx
914         rdmsr
915         movq    %rax,%r12
916         shlq    $32,%rdx
917         orq     %rdx,%r12
918         /* Retrieve and load the canonical value for GS.base. */
919         movq    TF_SIZE(%rsp),%rdx
920         movl    %edx,%eax
921         shrq    $32,%rdx
922         wrmsr
923         movq    %cr3,%r13
924         movq    PCPU(KCR3),%rax
925         cmpq    $~0,%rax
926         je      1f
927         movq    %rax,%cr3
928 1:      testl   $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
929         je      mchk_calltrap
930         movl    $MSR_IA32_SPEC_CTRL,%ecx
931         rdmsr
932         movl    %eax,%r14d
933         call    handle_ibrs_entry
934         jmp     mchk_calltrap
935 mchk_fromuserspace:
936         incl    %ebx
937         swapgs
938         movq    %cr3,%r13
939         movq    PCPU(KCR3),%rax
940         cmpq    $~0,%rax
941         je      1f
942         movq    %rax,%cr3
943 1:      call    handle_ibrs_entry
944 /* Note: this label is also used by ddb and gdb: */
945 mchk_calltrap:
946         FAKE_MCOUNT(TF_RIP(%rsp))
947         movq    %rsp,%rdi
948         call    mca_intr
949         MEXITCOUNT
950         testl   %ebx,%ebx       /* %ebx == 0 => return to userland */
951         jnz     doreti_exit
952         /*
953          * Restore speculation control MSR, if preserved.
954          */
955         testl   $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
956         je      1f
957         movl    %r14d,%eax
958         xorl    %edx,%edx
959         movl    $MSR_IA32_SPEC_CTRL,%ecx
960         wrmsr
961         /*
962          * Put back the preserved MSR_GSBASE value.
963          */
964 1:      movl    $MSR_GSBASE,%ecx
965         movq    %r12,%rdx
966         movl    %edx,%eax
967         shrq    $32,%rdx
968         wrmsr
969         movq    %r13,%cr3
970         RESTORE_REGS
971         addq    $TF_RIP,%rsp
972         jmp     doreti_iret
973
974 ENTRY(fork_trampoline)
975         movq    %r12,%rdi               /* function */
976         movq    %rbx,%rsi               /* arg1 */
977         movq    %rsp,%rdx               /* trapframe pointer */
978         call    fork_exit
979         MEXITCOUNT
980         jmp     doreti                  /* Handle any ASTs */
981
982 /*
983  * To efficiently implement classification of trap and interrupt handlers
984  * for profiling, there must be only trap handlers between the labels btrap
985  * and bintr, and only interrupt handlers between the labels bintr and
986  * eintr.  This is implemented (partly) by including files that contain
987  * some of the handlers.  Before including the files, set up a normal asm
988  * environment so that the included files doen't need to know that they are
989  * included.
990  */
991
992 #ifdef COMPAT_FREEBSD32
993         .data
994         .p2align 4
995         .text
996         SUPERALIGN_TEXT
997
998 #include <amd64/ia32/ia32_exception.S>
999 #endif
1000
1001         .data
1002         .p2align 4
1003         .text
1004         SUPERALIGN_TEXT
1005 MCOUNT_LABEL(bintr)
1006
1007 #include <amd64/amd64/apic_vector.S>
1008
1009 #ifdef DEV_ATPIC
1010         .data
1011         .p2align 4
1012         .text
1013         SUPERALIGN_TEXT
1014
1015 #include <amd64/amd64/atpic_vector.S>
1016 #endif
1017
1018         .text
1019 MCOUNT_LABEL(eintr)
1020
1021 /*
1022  * void doreti(struct trapframe)
1023  *
1024  * Handle return from interrupts, traps and syscalls.
1025  */
1026         .text
1027         SUPERALIGN_TEXT
1028         .type   doreti,@function
1029         .globl  doreti
1030 doreti:
1031         FAKE_MCOUNT($bintr)             /* init "from" bintr -> doreti */
1032         /*
1033          * Check if ASTs can be handled now.
1034          */
1035         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* are we returning to user mode? */
1036         jz      doreti_exit             /* can't handle ASTs now if not */
1037
1038 doreti_ast:
1039         /*
1040          * Check for ASTs atomically with returning.  Disabling CPU
1041          * interrupts provides sufficient locking even in the SMP case,
1042          * since we will be informed of any new ASTs by an IPI.
1043          */
1044         cli
1045         movq    PCPU(CURTHREAD),%rax
1046         testl   $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%rax)
1047         je      doreti_exit
1048         sti
1049         movq    %rsp,%rdi       /* pass a pointer to the trapframe */
1050         call    ast
1051         jmp     doreti_ast
1052
1053         /*
1054          * doreti_exit: pop registers, iret.
1055          *
1056          *      The segment register pop is a special case, since it may
1057          *      fault if (for example) a sigreturn specifies bad segment
1058          *      registers.  The fault is handled in trap.c.
1059          */
1060 doreti_exit:
1061         MEXITCOUNT
1062         movq    PCPU(CURPCB),%r8
1063
1064         /*
1065          * Do not reload segment registers for kernel.
1066          * Since we do not reload segments registers with sane
1067          * values on kernel entry, descriptors referenced by
1068          * segments registers might be not valid.  This is fatal
1069          * for user mode, but is not a problem for the kernel.
1070          */
1071         testb   $SEL_RPL_MASK,TF_CS(%rsp)
1072         jz      ld_regs
1073         testl   $PCB_FULL_IRET,PCB_FLAGS(%r8)
1074         jz      ld_regs
1075         andl    $~PCB_FULL_IRET,PCB_FLAGS(%r8)
1076         testl   $TF_HASSEGS,TF_FLAGS(%rsp)
1077         je      set_segs
1078
1079 do_segs:
1080         /* Restore %fs and fsbase */
1081         movw    TF_FS(%rsp),%ax
1082         .globl  ld_fs
1083 ld_fs:
1084         movw    %ax,%fs
1085         cmpw    $KUF32SEL,%ax
1086         jne     1f
1087         movl    $MSR_FSBASE,%ecx
1088         movl    PCB_FSBASE(%r8),%eax
1089         movl    PCB_FSBASE+4(%r8),%edx
1090         .globl  ld_fsbase
1091 ld_fsbase:
1092         wrmsr
1093 1:
1094         /* Restore %gs and gsbase */
1095         movw    TF_GS(%rsp),%si
1096         pushfq
1097         cli
1098         movl    $MSR_GSBASE,%ecx
1099         /* Save current kernel %gs base into %r12d:%r13d */
1100         rdmsr
1101         movl    %eax,%r12d
1102         movl    %edx,%r13d
1103         .globl  ld_gs
1104 ld_gs:
1105         movw    %si,%gs
1106         /* Save user %gs base into %r14d:%r15d */
1107         rdmsr
1108         movl    %eax,%r14d
1109         movl    %edx,%r15d
1110         /* Restore kernel %gs base */
1111         movl    %r12d,%eax
1112         movl    %r13d,%edx
1113         wrmsr
1114         popfq
1115         /*
1116          * Restore user %gs base, either from PCB if used for TLS, or
1117          * from the previously saved msr read.
1118          */
1119         movl    $MSR_KGSBASE,%ecx
1120         cmpw    $KUG32SEL,%si
1121         jne     1f
1122         movl    PCB_GSBASE(%r8),%eax
1123         movl    PCB_GSBASE+4(%r8),%edx
1124         jmp     ld_gsbase
1125 1:
1126         movl    %r14d,%eax
1127         movl    %r15d,%edx
1128         .globl  ld_gsbase
1129 ld_gsbase:
1130         wrmsr   /* May trap if non-canonical, but only for TLS. */
1131         .globl  ld_es
1132 ld_es:
1133         movw    TF_ES(%rsp),%es
1134         .globl  ld_ds
1135 ld_ds:
1136         movw    TF_DS(%rsp),%ds
1137 ld_regs:
1138         RESTORE_REGS
1139         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
1140         jz      2f                      /* keep running with kernel GS.base */
1141         cli
1142         call    handle_ibrs_exit_rs
1143         callq   *mds_handler
1144         cmpq    $~0,PCPU(UCR3)
1145         je      1f
1146         pushq   %rdx
1147         movq    PCPU(PTI_RSP0),%rdx
1148         subq    $PTI_SIZE,%rdx
1149         movq    %rax,PTI_RAX(%rdx)
1150         popq    %rax
1151         movq    %rax,PTI_RDX(%rdx)
1152         movq    TF_RIP(%rsp),%rax
1153         movq    %rax,PTI_RIP(%rdx)
1154         movq    TF_CS(%rsp),%rax
1155         movq    %rax,PTI_CS(%rdx)
1156         movq    TF_RFLAGS(%rsp),%rax
1157         movq    %rax,PTI_RFLAGS(%rdx)
1158         movq    TF_RSP(%rsp),%rax
1159         movq    %rax,PTI_RSP(%rdx)
1160         movq    TF_SS(%rsp),%rax
1161         movq    %rax,PTI_SS(%rdx)
1162         movq    PCPU(UCR3),%rax
1163         swapgs
1164         movq    %rdx,%rsp
1165         movq    %rax,%cr3
1166         popq    %rdx
1167         popq    %rax
1168         addq    $8,%rsp
1169         jmp     doreti_iret
1170 1:      swapgs
1171 2:      addq    $TF_RIP,%rsp
1172         .globl  doreti_iret
1173 doreti_iret:
1174         iretq
1175
1176 set_segs:
1177         movw    $KUDSEL,%ax
1178         movw    %ax,TF_DS(%rsp)
1179         movw    %ax,TF_ES(%rsp)
1180         movw    $KUF32SEL,TF_FS(%rsp)
1181         movw    $KUG32SEL,TF_GS(%rsp)
1182         jmp     do_segs
1183
1184         /*
1185          * doreti_iret_fault.  Alternative return code for
1186          * the case where we get a fault in the doreti_exit code
1187          * above.  trap() (amd64/amd64/trap.c) catches this specific
1188          * case, sends the process a signal and continues in the
1189          * corresponding place in the code below.
1190          */
1191         ALIGN_TEXT
1192         .globl  doreti_iret_fault
1193 doreti_iret_fault:
1194         subq    $TF_RIP,%rsp            /* space including tf_err, tf_trapno */
1195         movq    %rax,TF_RAX(%rsp)
1196         movq    %rdx,TF_RDX(%rsp)
1197         movq    %rcx,TF_RCX(%rsp)
1198         call    handle_ibrs_entry
1199         testb   $SEL_RPL_MASK,TF_CS(%rsp)
1200         jz      1f
1201         sti
1202 1:
1203         SAVE_SEGS
1204         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
1205         movq    %rdi,TF_RDI(%rsp)
1206         movq    %rsi,TF_RSI(%rsp)
1207         movq    %r8,TF_R8(%rsp)
1208         movq    %r9,TF_R9(%rsp)
1209         movq    %rbx,TF_RBX(%rsp)
1210         movq    %rbp,TF_RBP(%rsp)
1211         movq    %r10,TF_R10(%rsp)
1212         movq    %r11,TF_R11(%rsp)
1213         movq    %r12,TF_R12(%rsp)
1214         movq    %r13,TF_R13(%rsp)
1215         movq    %r14,TF_R14(%rsp)
1216         movq    %r15,TF_R15(%rsp)
1217         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
1218         movq    $0,TF_ERR(%rsp) /* XXX should be the error code */
1219         movq    $0,TF_ADDR(%rsp)
1220         FAKE_MCOUNT(TF_RIP(%rsp))
1221         jmp     calltrap
1222
1223         ALIGN_TEXT
1224         .globl  ds_load_fault
1225 ds_load_fault:
1226         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
1227         testb   $SEL_RPL_MASK,TF_CS(%rsp)
1228         jz      1f
1229         sti
1230 1:
1231         movq    %rsp,%rdi
1232         call    trap
1233         movw    $KUDSEL,TF_DS(%rsp)
1234         jmp     doreti
1235
1236         ALIGN_TEXT
1237         .globl  es_load_fault
1238 es_load_fault:
1239         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
1240         testl   $PSL_I,TF_RFLAGS(%rsp)
1241         jz      1f
1242         sti
1243 1:
1244         movq    %rsp,%rdi
1245         call    trap
1246         movw    $KUDSEL,TF_ES(%rsp)
1247         jmp     doreti
1248
1249         ALIGN_TEXT
1250         .globl  fs_load_fault
1251 fs_load_fault:
1252         testl   $PSL_I,TF_RFLAGS(%rsp)
1253         jz      1f
1254         sti
1255 1:
1256         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
1257         movq    %rsp,%rdi
1258         call    trap
1259         movw    $KUF32SEL,TF_FS(%rsp)
1260         jmp     doreti
1261
1262         ALIGN_TEXT
1263         .globl  gs_load_fault
1264 gs_load_fault:
1265         popfq
1266         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
1267         testl   $PSL_I,TF_RFLAGS(%rsp)
1268         jz      1f
1269         sti
1270 1:
1271         movq    %rsp,%rdi
1272         call    trap
1273         movw    $KUG32SEL,TF_GS(%rsp)
1274         jmp     doreti
1275
1276         ALIGN_TEXT
1277         .globl  fsbase_load_fault
1278 fsbase_load_fault:
1279         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
1280         testl   $PSL_I,TF_RFLAGS(%rsp)
1281         jz      1f
1282         sti
1283 1:
1284         movq    %rsp,%rdi
1285         call    trap
1286         movq    PCPU(CURTHREAD),%r8
1287         movq    TD_PCB(%r8),%r8
1288         movq    $0,PCB_FSBASE(%r8)
1289         jmp     doreti
1290
1291         ALIGN_TEXT
1292         .globl  gsbase_load_fault
1293 gsbase_load_fault:
1294         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
1295         testl   $PSL_I,TF_RFLAGS(%rsp)
1296         jz      1f
1297         sti
1298 1:
1299         movq    %rsp,%rdi
1300         call    trap
1301         movq    PCPU(CURTHREAD),%r8
1302         movq    TD_PCB(%r8),%r8
1303         movq    $0,PCB_GSBASE(%r8)
1304         jmp     doreti
1305
1306 #ifdef HWPMC_HOOKS
1307         ENTRY(end_exceptions)
1308 #endif