]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/exception.S
MFC r327819:
[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 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  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $FreeBSD$
35  */
36
37 #include "opt_atpic.h"
38 #include "opt_compat.h"
39 #include "opt_hwpmc_hooks.h"
40
41 #include <machine/asmacros.h>
42 #include <machine/psl.h>
43 #include <machine/trap.h>
44 #include <machine/specialreg.h>
45
46 #include "assym.s"
47
48 #ifdef KDTRACE_HOOKS
49         .bss
50         .globl  dtrace_invop_jump_addr
51         .align  8
52         .type   dtrace_invop_jump_addr,@object
53         .size   dtrace_invop_jump_addr,8
54 dtrace_invop_jump_addr:
55         .zero   8
56         .globl  dtrace_invop_calltrap_addr
57         .align  8
58         .type   dtrace_invop_calltrap_addr,@object
59         .size   dtrace_invop_calltrap_addr,8
60 dtrace_invop_calltrap_addr:
61         .zero   8
62 #endif
63         .text
64 #ifdef HWPMC_HOOKS
65         ENTRY(start_exceptions)
66 #endif
67
68 /*****************************************************************************/
69 /* Trap handling                                                             */
70 /*****************************************************************************/
71 /*
72  * Trap and fault vector routines.
73  *
74  * All traps are 'interrupt gates', SDT_SYSIGT.  An interrupt gate pushes
75  * state on the stack but also disables interrupts.  This is important for
76  * us for the use of the swapgs instruction.  We cannot be interrupted
77  * until the GS.base value is correct.  For most traps, we automatically
78  * then enable interrupts if the interrupted context had them enabled.
79  * This is equivalent to the i386 port's use of SDT_SYS386TGT.
80  *
81  * The cpu will push a certain amount of state onto the kernel stack for
82  * the current process.  See amd64/include/frame.h.
83  * This includes the current RFLAGS (status register, which includes
84  * the interrupt disable state prior to the trap), the code segment register,
85  * and the return instruction pointer are pushed by the cpu.  The cpu
86  * will also push an 'error' code for certain traps.  We push a dummy
87  * error code for those traps where the cpu doesn't in order to maintain
88  * a consistent frame.  We also push a contrived 'trap number'.
89  *
90  * The CPU does not push the general registers, so we must do that, and we
91  * must restore them prior to calling 'iret'.  The CPU adjusts %cs and %ss
92  * but does not mess with %ds, %es, %gs or %fs.  We swap the %gs base for
93  * for the kernel mode operation shortly, without changes to the selector
94  * loaded.  Since superuser long mode works with any selectors loaded into
95  * segment registers other then %cs, which makes them mostly unused in long
96  * mode, and kernel does not reference %fs, leave them alone.  The segment
97  * registers are reloaded on return to the usermode.
98  */
99
100 MCOUNT_LABEL(user)
101 MCOUNT_LABEL(btrap)
102
103 /* Traps that we leave interrupts disabled for.. */
104 #define TRAP_NOEN(a)    \
105         subq $TF_RIP,%rsp; \
106         movl $(a),TF_TRAPNO(%rsp) ; \
107         movq $0,TF_ADDR(%rsp) ; \
108         movq $0,TF_ERR(%rsp) ; \
109         jmp alltraps_noen
110 IDTVEC(dbg)
111         TRAP_NOEN(T_TRCTRAP)
112 IDTVEC(bpt)
113         TRAP_NOEN(T_BPTFLT)
114 #ifdef KDTRACE_HOOKS
115 IDTVEC(dtrace_ret)
116         TRAP_NOEN(T_DTRACE_RET)
117 #endif
118
119 /* Regular traps; The cpu does not supply tf_err for these. */
120 #define TRAP(a)  \
121         subq $TF_RIP,%rsp; \
122         movl $(a),TF_TRAPNO(%rsp) ; \
123         movq $0,TF_ADDR(%rsp) ; \
124         movq $0,TF_ERR(%rsp) ; \
125         jmp alltraps
126 IDTVEC(div)
127         TRAP(T_DIVIDE)
128 IDTVEC(ofl)
129         TRAP(T_OFLOW)
130 IDTVEC(bnd)
131         TRAP(T_BOUND)
132 IDTVEC(ill)
133         TRAP(T_PRIVINFLT)
134 IDTVEC(dna)
135         TRAP(T_DNA)
136 IDTVEC(fpusegm)
137         TRAP(T_FPOPFLT)
138 IDTVEC(mchk)
139         TRAP(T_MCHK)
140 IDTVEC(rsvd)
141         TRAP(T_RESERVED)
142 IDTVEC(fpu)
143         TRAP(T_ARITHTRAP)
144 IDTVEC(xmm)
145         TRAP(T_XMMFLT)
146
147 /* This group of traps have tf_err already pushed by the cpu */
148 #define TRAP_ERR(a)     \
149         subq $TF_ERR,%rsp; \
150         movl $(a),TF_TRAPNO(%rsp) ; \
151         movq $0,TF_ADDR(%rsp) ; \
152         jmp alltraps
153 IDTVEC(tss)
154         TRAP_ERR(T_TSSFLT)
155 IDTVEC(missing)
156         subq    $TF_ERR,%rsp
157         movl    $T_SEGNPFLT,TF_TRAPNO(%rsp)
158         jmp     prot_addrf
159 IDTVEC(stk)
160         subq    $TF_ERR,%rsp
161         movl    $T_STKFLT,TF_TRAPNO(%rsp)
162         jmp     prot_addrf
163 IDTVEC(align)
164         TRAP_ERR(T_ALIGNFLT)
165
166         /*
167          * alltraps entry point.  Use swapgs if this is the first time in the
168          * kernel from userland.  Reenable interrupts if they were enabled
169          * before the trap.  This approximates SDT_SYS386TGT on the i386 port.
170          */
171         SUPERALIGN_TEXT
172         .globl  alltraps
173         .type   alltraps,@function
174 alltraps:
175         movq    %rdi,TF_RDI(%rsp)
176         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
177         jz      alltraps_testi          /* already running with kernel GS.base */
178         swapgs
179         movq    PCPU(CURPCB),%rdi
180         andl    $~PCB_FULL_IRET,PCB_FLAGS(%rdi)
181         movw    %fs,TF_FS(%rsp)
182         movw    %gs,TF_GS(%rsp)
183         movw    %es,TF_ES(%rsp)
184         movw    %ds,TF_DS(%rsp)
185 alltraps_testi:
186         testl   $PSL_I,TF_RFLAGS(%rsp)
187         jz      alltraps_pushregs_no_rdi
188         sti
189 alltraps_pushregs_no_rdi:
190         movq    %rdx,TF_RDX(%rsp)
191         movq    %rax,TF_RAX(%rsp)
192 alltraps_pushregs_no_rax:
193         movq    %rsi,TF_RSI(%rsp)
194         movq    %rcx,TF_RCX(%rsp)
195         movq    %r8,TF_R8(%rsp)
196         movq    %r9,TF_R9(%rsp)
197         movq    %rbx,TF_RBX(%rsp)
198         movq    %rbp,TF_RBP(%rsp)
199         movq    %r10,TF_R10(%rsp)
200         movq    %r11,TF_R11(%rsp)
201         movq    %r12,TF_R12(%rsp)
202         movq    %r13,TF_R13(%rsp)
203         movq    %r14,TF_R14(%rsp)
204         movq    %r15,TF_R15(%rsp)
205         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
206         cld
207         FAKE_MCOUNT(TF_RIP(%rsp))
208 #ifdef KDTRACE_HOOKS
209         /*
210          * DTrace Function Boundary Trace (fbt) probes are triggered
211          * by int3 (0xcc) which causes the #BP (T_BPTFLT) breakpoint
212          * interrupt. For all other trap types, just handle them in
213          * the usual way.
214          */
215         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
216         jnz     calltrap                /* ignore userland traps */
217         cmpl    $T_BPTFLT,TF_TRAPNO(%rsp)
218         jne     calltrap
219
220         /* Check if there is no DTrace hook registered. */
221         cmpq    $0,dtrace_invop_jump_addr
222         je      calltrap
223
224         /*
225          * Set our jump address for the jump back in the event that
226          * the breakpoint wasn't caused by DTrace at all.
227          */
228         movq    $calltrap,dtrace_invop_calltrap_addr(%rip)
229
230         /* Jump to the code hooked in by DTrace. */
231         jmpq    *dtrace_invop_jump_addr
232 #endif
233         .globl  calltrap
234         .type   calltrap,@function
235 calltrap:
236         movq    %rsp,%rdi
237         call    trap_check
238         MEXITCOUNT
239         jmp     doreti                  /* Handle any pending ASTs */
240
241         /*
242          * alltraps_noen entry point.  Unlike alltraps above, we want to
243          * leave the interrupts disabled.  This corresponds to
244          * SDT_SYS386IGT on the i386 port.
245          */
246         SUPERALIGN_TEXT
247         .globl  alltraps_noen
248         .type   alltraps_noen,@function
249 alltraps_noen:
250         movq    %rdi,TF_RDI(%rsp)
251         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
252         jz      1f      /* already running with kernel GS.base */
253         swapgs
254         movq    PCPU(CURPCB),%rdi
255         andl    $~PCB_FULL_IRET,PCB_FLAGS(%rdi)
256 1:      movw    %fs,TF_FS(%rsp)
257         movw    %gs,TF_GS(%rsp)
258         movw    %es,TF_ES(%rsp)
259         movw    %ds,TF_DS(%rsp)
260         jmp     alltraps_pushregs_no_rdi
261
262 IDTVEC(dblfault)
263         subq    $TF_ERR,%rsp
264         movl    $T_DOUBLEFLT,TF_TRAPNO(%rsp)
265         movq    $0,TF_ADDR(%rsp)
266         movq    $0,TF_ERR(%rsp)
267         movq    %rdi,TF_RDI(%rsp)
268         movq    %rsi,TF_RSI(%rsp)
269         movq    %rdx,TF_RDX(%rsp)
270         movq    %rcx,TF_RCX(%rsp)
271         movq    %r8,TF_R8(%rsp)
272         movq    %r9,TF_R9(%rsp)
273         movq    %rax,TF_RAX(%rsp)
274         movq    %rbx,TF_RBX(%rsp)
275         movq    %rbp,TF_RBP(%rsp)
276         movq    %r10,TF_R10(%rsp)
277         movq    %r11,TF_R11(%rsp)
278         movq    %r12,TF_R12(%rsp)
279         movq    %r13,TF_R13(%rsp)
280         movq    %r14,TF_R14(%rsp)
281         movq    %r15,TF_R15(%rsp)
282         movw    %fs,TF_FS(%rsp)
283         movw    %gs,TF_GS(%rsp)
284         movw    %es,TF_ES(%rsp)
285         movw    %ds,TF_DS(%rsp)
286         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
287         cld
288         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
289         jz      1f                      /* already running with kernel GS.base */
290         swapgs
291 1:
292         movq    %rsp,%rdi
293         call    dblfault_handler
294 2:
295         hlt
296         jmp     2b
297
298 IDTVEC(page)
299         subq    $TF_ERR,%rsp
300         movl    $T_PAGEFLT,TF_TRAPNO(%rsp)
301         movq    %rdi,TF_RDI(%rsp)       /* free up a GP register */
302         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
303         jz      1f                      /* already running with kernel GS.base */
304         swapgs
305         movq    PCPU(CURPCB),%rdi
306         andl    $~PCB_FULL_IRET,PCB_FLAGS(%rdi)
307 1:      movq    %cr2,%rdi               /* preserve %cr2 before ..  */
308         movq    %rdi,TF_ADDR(%rsp)      /* enabling interrupts. */
309         movw    %fs,TF_FS(%rsp)
310         movw    %gs,TF_GS(%rsp)
311         movw    %es,TF_ES(%rsp)
312         movw    %ds,TF_DS(%rsp)
313         testl   $PSL_I,TF_RFLAGS(%rsp)
314         jz      alltraps_pushregs_no_rdi
315         sti
316         jmp     alltraps_pushregs_no_rdi
317
318         /*
319          * We have to special-case this one.  If we get a trap in doreti() at
320          * the iretq stage, we'll reenter with the wrong gs state.  We'll have
321          * to do a special the swapgs in this case even coming from the kernel.
322          * XXX linux has a trap handler for their equivalent of load_gs().
323          */
324 IDTVEC(prot)
325         subq    $TF_ERR,%rsp
326         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
327 prot_addrf:
328         movq    $0,TF_ADDR(%rsp)
329         movq    %rdi,TF_RDI(%rsp)       /* free up a GP register */
330         movq    %rax,TF_RAX(%rsp)
331         movq    %rdx,TF_RDX(%rsp)
332         movw    %fs,TF_FS(%rsp)
333         movw    %gs,TF_GS(%rsp)
334         leaq    doreti_iret(%rip),%rdi
335         cmpq    %rdi,TF_RIP(%rsp)
336         je      5f                      /* kernel but with user gsbase!! */
337         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
338         jz      6f                      /* already running with kernel GS.base */
339         testb   $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
340         jz      2f
341         cmpw    $KUF32SEL,TF_FS(%rsp)
342         jne     1f
343         rdfsbase %rax
344 1:      cmpw    $KUG32SEL,TF_GS(%rsp)
345         jne     2f
346         rdgsbase %rdx
347 2:      swapgs
348         movq    PCPU(CURPCB),%rdi
349         testb   $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
350         jz      4f
351         cmpw    $KUF32SEL,TF_FS(%rsp)
352         jne     3f
353         movq    %rax,PCB_FSBASE(%rdi)
354 3:      cmpw    $KUG32SEL,TF_GS(%rsp)
355         jne     4f
356         movq    %rdx,PCB_GSBASE(%rdi)
357 4:      orl     $PCB_FULL_IRET,PCB_FLAGS(%rdi)  /* always full iret from GPF */
358         movw    %es,TF_ES(%rsp)
359         movw    %ds,TF_DS(%rsp)
360         testl   $PSL_I,TF_RFLAGS(%rsp)
361         jz      alltraps_pushregs_no_rax
362         sti
363         jmp     alltraps_pushregs_no_rax
364
365 5:      swapgs
366 6:      movq    PCPU(CURPCB),%rdi
367         jmp     4b
368
369 /*
370  * Fast syscall entry point.  We enter here with just our new %cs/%ss set,
371  * and the new privilige level.  We are still running on the old user stack
372  * pointer.  We have to juggle a few things around to find our stack etc.
373  * swapgs gives us access to our PCPU space only.
374  *
375  * We do not support invoking this from a custom segment registers,
376  * esp. %cs, %ss, %fs, %gs, e.g. using entries from an LDT.
377  */
378 IDTVEC(fast_syscall)
379         swapgs
380         movq    %rsp,PCPU(SCRATCH_RSP)
381         movq    PCPU(RSP0),%rsp
382         /* Now emulate a trapframe. Make the 8 byte alignment odd for call. */
383         subq    $TF_SIZE,%rsp
384         /* defer TF_RSP till we have a spare register */
385         movq    %r11,TF_RFLAGS(%rsp)
386         movq    %rcx,TF_RIP(%rsp)       /* %rcx original value is in %r10 */
387         movq    PCPU(SCRATCH_RSP),%r11  /* %r11 already saved */
388         movq    %r11,TF_RSP(%rsp)       /* user stack pointer */
389         movw    %fs,TF_FS(%rsp)
390         movw    %gs,TF_GS(%rsp)
391         movw    %es,TF_ES(%rsp)
392         movw    %ds,TF_DS(%rsp)
393         movq    PCPU(CURPCB),%r11
394         andl    $~PCB_FULL_IRET,PCB_FLAGS(%r11)
395         sti
396         movq    $KUDSEL,TF_SS(%rsp)
397         movq    $KUCSEL,TF_CS(%rsp)
398         movq    $2,TF_ERR(%rsp)
399         movq    %rdi,TF_RDI(%rsp)       /* arg 1 */
400         movq    %rsi,TF_RSI(%rsp)       /* arg 2 */
401         movq    %rdx,TF_RDX(%rsp)       /* arg 3 */
402         movq    %r10,TF_RCX(%rsp)       /* arg 4 */
403         movq    %r8,TF_R8(%rsp)         /* arg 5 */
404         movq    %r9,TF_R9(%rsp)         /* arg 6 */
405         movq    %rax,TF_RAX(%rsp)       /* syscall number */
406         movq    %rbx,TF_RBX(%rsp)       /* C preserved */
407         movq    %rbp,TF_RBP(%rsp)       /* C preserved */
408         movq    %r12,TF_R12(%rsp)       /* C preserved */
409         movq    %r13,TF_R13(%rsp)       /* C preserved */
410         movq    %r14,TF_R14(%rsp)       /* C preserved */
411         movq    %r15,TF_R15(%rsp)       /* C preserved */
412         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
413         FAKE_MCOUNT(TF_RIP(%rsp))
414         movq    PCPU(CURTHREAD),%rdi
415         movq    %rsp,TD_FRAME(%rdi)
416         movl    TF_RFLAGS(%rsp),%esi
417         andl    $PSL_T,%esi
418         call    amd64_syscall
419 1:      movq    PCPU(CURPCB),%rax
420         /* Disable interrupts before testing PCB_FULL_IRET. */
421         cli
422         testl   $PCB_FULL_IRET,PCB_FLAGS(%rax)
423         jnz     3f
424         /* Check for and handle AST's on return to userland. */
425         movq    PCPU(CURTHREAD),%rax
426         testl   $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%rax)
427         jne     2f
428         /* Restore preserved registers. */
429         MEXITCOUNT
430         movq    TF_RDI(%rsp),%rdi       /* bonus; preserve arg 1 */
431         movq    TF_RSI(%rsp),%rsi       /* bonus: preserve arg 2 */
432         movq    TF_RDX(%rsp),%rdx       /* return value 2 */
433         movq    TF_RAX(%rsp),%rax       /* return value 1 */
434         movq    TF_RFLAGS(%rsp),%r11    /* original %rflags */
435         movq    TF_RIP(%rsp),%rcx       /* original %rip */
436         movq    TF_RSP(%rsp),%rsp       /* user stack pointer */
437         swapgs
438         sysretq
439
440 2:      /* AST scheduled. */
441         sti
442         movq    %rsp,%rdi
443         call    ast
444         jmp     1b
445
446 3:      /* Requested full context restore, use doreti for that. */
447         MEXITCOUNT
448         jmp     doreti
449
450 /*
451  * Here for CYA insurance, in case a "syscall" instruction gets
452  * issued from 32 bit compatibility mode. MSR_CSTAR has to point
453  * to *something* if EFER_SCE is enabled.
454  */
455 IDTVEC(fast_syscall32)
456         sysret
457
458 /*
459  * NMI handling is special.
460  *
461  * First, NMIs do not respect the state of the processor's RFLAGS.IF
462  * bit.  The NMI handler may be entered at any time, including when
463  * the processor is in a critical section with RFLAGS.IF == 0.
464  * The processor's GS.base value could be invalid on entry to the
465  * handler.
466  *
467  * Second, the processor treats NMIs specially, blocking further NMIs
468  * until an 'iretq' instruction is executed.  We thus need to execute
469  * the NMI handler with interrupts disabled, to prevent a nested interrupt
470  * from executing an 'iretq' instruction and inadvertently taking the
471  * processor out of NMI mode.
472  *
473  * Third, the NMI handler runs on its own stack (tss_ist2). The canonical
474  * GS.base value for the processor is stored just above the bottom of its
475  * NMI stack.  For NMIs taken from kernel mode, the current value in
476  * the processor's GS.base is saved at entry to C-preserved register %r12,
477  * the canonical value for GS.base is then loaded into the processor, and
478  * the saved value is restored at exit time.  For NMIs taken from user mode,
479  * the cheaper 'SWAPGS' instructions are used for swapping GS.base.
480  */
481
482 IDTVEC(nmi)
483         subq    $TF_RIP,%rsp
484         movl    $(T_NMI),TF_TRAPNO(%rsp)
485         movq    $0,TF_ADDR(%rsp)
486         movq    $0,TF_ERR(%rsp)
487         movq    %rdi,TF_RDI(%rsp)
488         movq    %rsi,TF_RSI(%rsp)
489         movq    %rdx,TF_RDX(%rsp)
490         movq    %rcx,TF_RCX(%rsp)
491         movq    %r8,TF_R8(%rsp)
492         movq    %r9,TF_R9(%rsp)
493         movq    %rax,TF_RAX(%rsp)
494         movq    %rbx,TF_RBX(%rsp)
495         movq    %rbp,TF_RBP(%rsp)
496         movq    %r10,TF_R10(%rsp)
497         movq    %r11,TF_R11(%rsp)
498         movq    %r12,TF_R12(%rsp)
499         movq    %r13,TF_R13(%rsp)
500         movq    %r14,TF_R14(%rsp)
501         movq    %r15,TF_R15(%rsp)
502         movw    %fs,TF_FS(%rsp)
503         movw    %gs,TF_GS(%rsp)
504         movw    %es,TF_ES(%rsp)
505         movw    %ds,TF_DS(%rsp)
506         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
507         cld
508         xorl    %ebx,%ebx
509         testb   $SEL_RPL_MASK,TF_CS(%rsp)
510         jnz     nmi_fromuserspace
511         /*
512          * We've interrupted the kernel.  Preserve GS.base in %r12.
513          */
514         movl    $MSR_GSBASE,%ecx
515         rdmsr
516         movq    %rax,%r12
517         shlq    $32,%rdx
518         orq     %rdx,%r12
519         /* Retrieve and load the canonical value for GS.base. */
520         movq    TF_SIZE(%rsp),%rdx
521         movl    %edx,%eax
522         shrq    $32,%rdx
523         wrmsr
524         jmp     nmi_calltrap
525 nmi_fromuserspace:
526         incl    %ebx
527         swapgs
528         testb   $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
529         jz      2f
530         movq    PCPU(CURPCB),%rdi
531         testq   %rdi,%rdi
532         jz      2f
533         cmpw    $KUF32SEL,TF_FS(%rsp)
534         jne     1f
535         rdfsbase %rax
536         movq    %rax,PCB_FSBASE(%rdi)
537 1:      cmpw    $KUG32SEL,TF_GS(%rsp)
538         jne     2f
539         movl    $MSR_KGSBASE,%ecx
540         rdmsr
541         shlq    $32,%rdx
542         orq     %rdx,%rax
543         movq    %rax,PCB_GSBASE(%rdi)
544 2:
545 /* Note: this label is also used by ddb and gdb: */
546 nmi_calltrap:
547         FAKE_MCOUNT(TF_RIP(%rsp))
548         movq    %rsp,%rdi
549         call    trap
550         MEXITCOUNT
551 #ifdef HWPMC_HOOKS
552         /*
553          * Capture a userspace callchain if needed.
554          *
555          * - Check if the current trap was from user mode.
556          * - Check if the current thread is valid.
557          * - Check if the thread requires a user call chain to be
558          *   captured.
559          *
560          * We are still in NMI mode at this point.
561          */
562         testl   %ebx,%ebx
563         jz      nocallchain     /* not from userspace */
564         movq    PCPU(CURTHREAD),%rax
565         orq     %rax,%rax       /* curthread present? */
566         jz      nocallchain
567         testl   $TDP_CALLCHAIN,TD_PFLAGS(%rax) /* flagged for capture? */
568         jz      nocallchain
569         /*
570          * A user callchain is to be captured, so:
571          * - Move execution to the regular kernel stack, to allow for
572          *   nested NMI interrupts.
573          * - Take the processor out of "NMI" mode by faking an "iret".
574          * - Enable interrupts, so that copyin() can work.
575          */
576         movq    %rsp,%rsi       /* source stack pointer */
577         movq    $TF_SIZE,%rcx
578         movq    PCPU(RSP0),%rdx
579         subq    %rcx,%rdx
580         movq    %rdx,%rdi       /* destination stack pointer */
581
582         shrq    $3,%rcx         /* trap frame size in long words */
583         cld
584         rep
585         movsq                   /* copy trapframe */
586
587         movl    %ss,%eax
588         pushq   %rax            /* tf_ss */
589         pushq   %rdx            /* tf_rsp (on kernel stack) */
590         pushfq                  /* tf_rflags */
591         movl    %cs,%eax
592         pushq   %rax            /* tf_cs */
593         pushq   $outofnmi       /* tf_rip */
594         iretq
595 outofnmi:
596         /*
597          * At this point the processor has exited NMI mode and is running
598          * with interrupts turned off on the normal kernel stack.
599          *
600          * If a pending NMI gets recognized at or after this point, it
601          * will cause a kernel callchain to be traced.
602          *
603          * We turn interrupts back on, and call the user callchain capture hook.
604          */
605         movq    pmc_hook,%rax
606         orq     %rax,%rax
607         jz      nocallchain
608         movq    PCPU(CURTHREAD),%rdi            /* thread */
609         movq    $PMC_FN_USER_CALLCHAIN,%rsi     /* command */
610         movq    %rsp,%rdx                       /* frame */
611         sti
612         call    *%rax
613         cli
614 nocallchain:
615 #endif
616         testl   %ebx,%ebx
617         jnz     doreti_exit
618 nmi_kernelexit:
619         /*
620          * Put back the preserved MSR_GSBASE value.
621          */
622         movl    $MSR_GSBASE,%ecx
623         movq    %r12,%rdx
624         movl    %edx,%eax
625         shrq    $32,%rdx
626         wrmsr
627 nmi_restoreregs:
628         movq    TF_RDI(%rsp),%rdi
629         movq    TF_RSI(%rsp),%rsi
630         movq    TF_RDX(%rsp),%rdx
631         movq    TF_RCX(%rsp),%rcx
632         movq    TF_R8(%rsp),%r8
633         movq    TF_R9(%rsp),%r9
634         movq    TF_RAX(%rsp),%rax
635         movq    TF_RBX(%rsp),%rbx
636         movq    TF_RBP(%rsp),%rbp
637         movq    TF_R10(%rsp),%r10
638         movq    TF_R11(%rsp),%r11
639         movq    TF_R12(%rsp),%r12
640         movq    TF_R13(%rsp),%r13
641         movq    TF_R14(%rsp),%r14
642         movq    TF_R15(%rsp),%r15
643         addq    $TF_RIP,%rsp
644         jmp     doreti_iret
645
646 ENTRY(fork_trampoline)
647         movq    %r12,%rdi               /* function */
648         movq    %rbx,%rsi               /* arg1 */
649         movq    %rsp,%rdx               /* trapframe pointer */
650         call    fork_exit
651         MEXITCOUNT
652         jmp     doreti                  /* Handle any ASTs */
653
654 /*
655  * To efficiently implement classification of trap and interrupt handlers
656  * for profiling, there must be only trap handlers between the labels btrap
657  * and bintr, and only interrupt handlers between the labels bintr and
658  * eintr.  This is implemented (partly) by including files that contain
659  * some of the handlers.  Before including the files, set up a normal asm
660  * environment so that the included files doen't need to know that they are
661  * included.
662  */
663
664 #ifdef COMPAT_FREEBSD32
665         .data
666         .p2align 4
667         .text
668         SUPERALIGN_TEXT
669
670 #include <amd64/ia32/ia32_exception.S>
671 #endif
672
673         .data
674         .p2align 4
675         .text
676         SUPERALIGN_TEXT
677 MCOUNT_LABEL(bintr)
678
679 #include <amd64/amd64/apic_vector.S>
680
681 #ifdef DEV_ATPIC
682         .data
683         .p2align 4
684         .text
685         SUPERALIGN_TEXT
686
687 #include <amd64/amd64/atpic_vector.S>
688 #endif
689
690         .text
691 MCOUNT_LABEL(eintr)
692
693 /*
694  * void doreti(struct trapframe)
695  *
696  * Handle return from interrupts, traps and syscalls.
697  */
698         .text
699         SUPERALIGN_TEXT
700         .type   doreti,@function
701         .globl  doreti
702 doreti:
703         FAKE_MCOUNT($bintr)             /* init "from" bintr -> doreti */
704         /*
705          * Check if ASTs can be handled now.
706          */
707         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* are we returning to user mode? */
708         jz      doreti_exit             /* can't handle ASTs now if not */
709
710 doreti_ast:
711         /*
712          * Check for ASTs atomically with returning.  Disabling CPU
713          * interrupts provides sufficient locking even in the SMP case,
714          * since we will be informed of any new ASTs by an IPI.
715          */
716         cli
717         movq    PCPU(CURTHREAD),%rax
718         testl   $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%rax)
719         je      doreti_exit
720         sti
721         movq    %rsp,%rdi       /* pass a pointer to the trapframe */
722         call    ast
723         jmp     doreti_ast
724
725         /*
726          * doreti_exit: pop registers, iret.
727          *
728          *      The segment register pop is a special case, since it may
729          *      fault if (for example) a sigreturn specifies bad segment
730          *      registers.  The fault is handled in trap.c.
731          */
732 doreti_exit:
733         MEXITCOUNT
734         movq    PCPU(CURPCB),%r8
735
736         /*
737          * Do not reload segment registers for kernel.
738          * Since we do not reload segments registers with sane
739          * values on kernel entry, descriptors referenced by
740          * segments registers might be not valid.  This is fatal
741          * for user mode, but is not a problem for the kernel.
742          */
743         testb   $SEL_RPL_MASK,TF_CS(%rsp)
744         jz      ld_regs
745         testl   $PCB_FULL_IRET,PCB_FLAGS(%r8)
746         jz      ld_regs
747         andl    $~PCB_FULL_IRET,PCB_FLAGS(%r8)
748         testl   $TF_HASSEGS,TF_FLAGS(%rsp)
749         je      set_segs
750
751 do_segs:
752         /* Restore %fs and fsbase */
753         movw    TF_FS(%rsp),%ax
754         .globl  ld_fs
755 ld_fs:
756         movw    %ax,%fs
757         cmpw    $KUF32SEL,%ax
758         jne     1f
759         movl    $MSR_FSBASE,%ecx
760         movl    PCB_FSBASE(%r8),%eax
761         movl    PCB_FSBASE+4(%r8),%edx
762         .globl  ld_fsbase
763 ld_fsbase:
764         wrmsr
765 1:
766         /* Restore %gs and gsbase */
767         movw    TF_GS(%rsp),%si
768         pushfq
769         cli
770         movl    $MSR_GSBASE,%ecx
771         /* Save current kernel %gs base into %r12d:%r13d */
772         rdmsr
773         movl    %eax,%r12d
774         movl    %edx,%r13d
775         .globl  ld_gs
776 ld_gs:
777         movw    %si,%gs
778         /* Save user %gs base into %r14d:%r15d */
779         rdmsr
780         movl    %eax,%r14d
781         movl    %edx,%r15d
782         /* Restore kernel %gs base */
783         movl    %r12d,%eax
784         movl    %r13d,%edx
785         wrmsr
786         popfq
787         /*
788          * Restore user %gs base, either from PCB if used for TLS, or
789          * from the previously saved msr read.
790          */
791         movl    $MSR_KGSBASE,%ecx
792         cmpw    $KUG32SEL,%si
793         jne     1f
794         movl    PCB_GSBASE(%r8),%eax
795         movl    PCB_GSBASE+4(%r8),%edx
796         jmp     ld_gsbase
797 1:
798         movl    %r14d,%eax
799         movl    %r15d,%edx
800         .globl  ld_gsbase
801 ld_gsbase:
802         wrmsr   /* May trap if non-canonical, but only for TLS. */
803         .globl  ld_es
804 ld_es:
805         movw    TF_ES(%rsp),%es
806         .globl  ld_ds
807 ld_ds:
808         movw    TF_DS(%rsp),%ds
809 ld_regs:
810         movq    TF_RDI(%rsp),%rdi
811         movq    TF_RSI(%rsp),%rsi
812         movq    TF_RDX(%rsp),%rdx
813         movq    TF_RCX(%rsp),%rcx
814         movq    TF_R8(%rsp),%r8
815         movq    TF_R9(%rsp),%r9
816         movq    TF_RAX(%rsp),%rax
817         movq    TF_RBX(%rsp),%rbx
818         movq    TF_RBP(%rsp),%rbp
819         movq    TF_R10(%rsp),%r10
820         movq    TF_R11(%rsp),%r11
821         movq    TF_R12(%rsp),%r12
822         movq    TF_R13(%rsp),%r13
823         movq    TF_R14(%rsp),%r14
824         movq    TF_R15(%rsp),%r15
825         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
826         jz      1f                      /* keep running with kernel GS.base */
827         cli
828         swapgs
829 1:
830         addq    $TF_RIP,%rsp            /* skip over tf_err, tf_trapno */
831         .globl  doreti_iret
832 doreti_iret:
833         iretq
834
835 set_segs:
836         movw    $KUDSEL,%ax
837         movw    %ax,TF_DS(%rsp)
838         movw    %ax,TF_ES(%rsp)
839         movw    $KUF32SEL,TF_FS(%rsp)
840         movw    $KUG32SEL,TF_GS(%rsp)
841         jmp     do_segs
842
843         /*
844          * doreti_iret_fault.  Alternative return code for
845          * the case where we get a fault in the doreti_exit code
846          * above.  trap() (amd64/amd64/trap.c) catches this specific
847          * case, sends the process a signal and continues in the
848          * corresponding place in the code below.
849          */
850         ALIGN_TEXT
851         .globl  doreti_iret_fault
852 doreti_iret_fault:
853         subq    $TF_RIP,%rsp            /* space including tf_err, tf_trapno */
854         testl   $PSL_I,TF_RFLAGS(%rsp)
855         jz      1f
856         sti
857 1:
858         movw    %fs,TF_FS(%rsp)
859         movw    %gs,TF_GS(%rsp)
860         movw    %es,TF_ES(%rsp)
861         movw    %ds,TF_DS(%rsp)
862         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
863         movq    %rdi,TF_RDI(%rsp)
864         movq    %rsi,TF_RSI(%rsp)
865         movq    %rdx,TF_RDX(%rsp)
866         movq    %rcx,TF_RCX(%rsp)
867         movq    %r8,TF_R8(%rsp)
868         movq    %r9,TF_R9(%rsp)
869         movq    %rax,TF_RAX(%rsp)
870         movq    %rbx,TF_RBX(%rsp)
871         movq    %rbp,TF_RBP(%rsp)
872         movq    %r10,TF_R10(%rsp)
873         movq    %r11,TF_R11(%rsp)
874         movq    %r12,TF_R12(%rsp)
875         movq    %r13,TF_R13(%rsp)
876         movq    %r14,TF_R14(%rsp)
877         movq    %r15,TF_R15(%rsp)
878         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
879         movq    $0,TF_ERR(%rsp) /* XXX should be the error code */
880         movq    $0,TF_ADDR(%rsp)
881         FAKE_MCOUNT(TF_RIP(%rsp))
882         jmp     calltrap
883
884         ALIGN_TEXT
885         .globl  ds_load_fault
886 ds_load_fault:
887         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
888         testl   $PSL_I,TF_RFLAGS(%rsp)
889         jz      1f
890         sti
891 1:
892         movq    %rsp,%rdi
893         call    trap
894         movw    $KUDSEL,TF_DS(%rsp)
895         jmp     doreti
896
897         ALIGN_TEXT
898         .globl  es_load_fault
899 es_load_fault:
900         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
901         testl   $PSL_I,TF_RFLAGS(%rsp)
902         jz      1f
903         sti
904 1:
905         movq    %rsp,%rdi
906         call    trap
907         movw    $KUDSEL,TF_ES(%rsp)
908         jmp     doreti
909
910         ALIGN_TEXT
911         .globl  fs_load_fault
912 fs_load_fault:
913         testl   $PSL_I,TF_RFLAGS(%rsp)
914         jz      1f
915         sti
916 1:
917         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
918         movq    %rsp,%rdi
919         call    trap
920         movw    $KUF32SEL,TF_FS(%rsp)
921         jmp     doreti
922
923         ALIGN_TEXT
924         .globl  gs_load_fault
925 gs_load_fault:
926         popfq
927         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
928         testl   $PSL_I,TF_RFLAGS(%rsp)
929         jz      1f
930         sti
931 1:
932         movq    %rsp,%rdi
933         call    trap
934         movw    $KUG32SEL,TF_GS(%rsp)
935         jmp     doreti
936
937         ALIGN_TEXT
938         .globl  fsbase_load_fault
939 fsbase_load_fault:
940         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
941         testl   $PSL_I,TF_RFLAGS(%rsp)
942         jz      1f
943         sti
944 1:
945         movq    %rsp,%rdi
946         call    trap
947         movq    PCPU(CURTHREAD),%r8
948         movq    TD_PCB(%r8),%r8
949         movq    $0,PCB_FSBASE(%r8)
950         jmp     doreti
951
952         ALIGN_TEXT
953         .globl  gsbase_load_fault
954 gsbase_load_fault:
955         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
956         testl   $PSL_I,TF_RFLAGS(%rsp)
957         jz      1f
958         sti
959 1:
960         movq    %rsp,%rdi
961         call    trap
962         movq    PCPU(CURTHREAD),%r8
963         movq    TD_PCB(%r8),%r8
964         movq    $0,PCB_GSBASE(%r8)
965         jmp     doreti
966
967 #ifdef HWPMC_HOOKS
968         ENTRY(end_exceptions)
969 #endif