]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/exception.S
Add VHD support to mkimg(1). VHD is used by Xen and Microsoft's Hyper-V
[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         TRAP_ERR(T_SEGNPFLT)
157 IDTVEC(stk)
158         TRAP_ERR(T_STKFLT)
159 IDTVEC(align)
160         TRAP_ERR(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      alltraps_testi          /* already running with kernel GS.base */
174         swapgs
175         movq    PCPU(CURPCB),%rdi
176         andl    $~PCB_FULL_IRET,PCB_FLAGS(%rdi)
177         movw    %fs,TF_FS(%rsp)
178         movw    %gs,TF_GS(%rsp)
179         movw    %es,TF_ES(%rsp)
180         movw    %ds,TF_DS(%rsp)
181 alltraps_testi:
182         testl   $PSL_I,TF_RFLAGS(%rsp)
183         jz      alltraps_pushregs_no_rdi
184         sti
185 alltraps_pushregs_no_rdi:
186         movq    %rsi,TF_RSI(%rsp)
187         movq    %rdx,TF_RDX(%rsp)
188         movq    %rcx,TF_RCX(%rsp)
189         movq    %r8,TF_R8(%rsp)
190         movq    %r9,TF_R9(%rsp)
191         movq    %rax,TF_RAX(%rsp)
192         movq    %rbx,TF_RBX(%rsp)
193         movq    %rbp,TF_RBP(%rsp)
194         movq    %r10,TF_R10(%rsp)
195         movq    %r11,TF_R11(%rsp)
196         movq    %r12,TF_R12(%rsp)
197         movq    %r13,TF_R13(%rsp)
198         movq    %r14,TF_R14(%rsp)
199         movq    %r15,TF_R15(%rsp)
200         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
201         cld
202         FAKE_MCOUNT(TF_RIP(%rsp))
203 #ifdef KDTRACE_HOOKS
204         /*
205          * DTrace Function Boundary Trace (fbt) probes are triggered
206          * by int3 (0xcc) which causes the #BP (T_BPTFLT) breakpoint
207          * interrupt. For all other trap types, just handle them in
208          * the usual way.
209          */
210         cmpl    $T_BPTFLT,TF_TRAPNO(%rsp)
211         jne     calltrap
212
213         /* Check if there is no DTrace hook registered. */
214         cmpq    $0,dtrace_invop_jump_addr
215         je      calltrap
216
217         /*
218          * Set our jump address for the jump back in the event that
219          * the breakpoint wasn't caused by DTrace at all.
220          */
221         movq    $calltrap,dtrace_invop_calltrap_addr(%rip)
222
223         /* Jump to the code hooked in by DTrace. */
224         movq    dtrace_invop_jump_addr,%rax
225         jmpq    *dtrace_invop_jump_addr
226 #endif
227         .globl  calltrap
228         .type   calltrap,@function
229 calltrap:
230         movq    %rsp,%rdi
231         call    trap
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:      movw    %fs,TF_FS(%rsp)
251         movw    %gs,TF_GS(%rsp)
252         movw    %es,TF_ES(%rsp)
253         movw    %ds,TF_DS(%rsp)
254         jmp     alltraps_pushregs_no_rdi
255
256 IDTVEC(dblfault)
257         subq    $TF_ERR,%rsp
258         movl    $T_DOUBLEFLT,TF_TRAPNO(%rsp)
259         movq    $0,TF_ADDR(%rsp)
260         movq    $0,TF_ERR(%rsp)
261         movq    %rdi,TF_RDI(%rsp)
262         movq    %rsi,TF_RSI(%rsp)
263         movq    %rdx,TF_RDX(%rsp)
264         movq    %rcx,TF_RCX(%rsp)
265         movq    %r8,TF_R8(%rsp)
266         movq    %r9,TF_R9(%rsp)
267         movq    %rax,TF_RAX(%rsp)
268         movq    %rbx,TF_RBX(%rsp)
269         movq    %rbp,TF_RBP(%rsp)
270         movq    %r10,TF_R10(%rsp)
271         movq    %r11,TF_R11(%rsp)
272         movq    %r12,TF_R12(%rsp)
273         movq    %r13,TF_R13(%rsp)
274         movq    %r14,TF_R14(%rsp)
275         movq    %r15,TF_R15(%rsp)
276         movw    %fs,TF_FS(%rsp)
277         movw    %gs,TF_GS(%rsp)
278         movw    %es,TF_ES(%rsp)
279         movw    %ds,TF_DS(%rsp)
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    %rsp,%rdi
287         call    dblfault_handler
288 2:
289         hlt
290         jmp     2b
291
292 IDTVEC(page)
293         subq    $TF_ERR,%rsp
294         movl    $T_PAGEFLT,TF_TRAPNO(%rsp)
295         movq    %rdi,TF_RDI(%rsp)       /* free up a GP register */
296         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
297         jz      1f                      /* already running with kernel GS.base */
298         swapgs
299         movq    PCPU(CURPCB),%rdi
300         andl    $~PCB_FULL_IRET,PCB_FLAGS(%rdi)
301 1:      movq    %cr2,%rdi               /* preserve %cr2 before ..  */
302         movq    %rdi,TF_ADDR(%rsp)      /* enabling interrupts. */
303         movw    %fs,TF_FS(%rsp)
304         movw    %gs,TF_GS(%rsp)
305         movw    %es,TF_ES(%rsp)
306         movw    %ds,TF_DS(%rsp)
307         testl   $PSL_I,TF_RFLAGS(%rsp)
308         jz      alltraps_pushregs_no_rdi
309         sti
310         jmp     alltraps_pushregs_no_rdi
311
312         /*
313          * We have to special-case this one.  If we get a trap in doreti() at
314          * the iretq stage, we'll reenter with the wrong gs state.  We'll have
315          * to do a special the swapgs in this case even coming from the kernel.
316          * XXX linux has a trap handler for their equivalent of load_gs().
317          */
318 IDTVEC(prot)
319         subq    $TF_ERR,%rsp
320         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
321         movq    $0,TF_ADDR(%rsp)
322         movq    %rdi,TF_RDI(%rsp)       /* free up a GP register */
323         leaq    doreti_iret(%rip),%rdi
324         cmpq    %rdi,TF_RIP(%rsp)
325         je      1f                      /* kernel but with user gsbase!! */
326         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
327         jz      2f                      /* already running with kernel GS.base */
328 1:      swapgs
329 2:      movq    PCPU(CURPCB),%rdi
330         orl     $PCB_FULL_IRET,PCB_FLAGS(%rdi)  /* always full iret from GPF */
331         movw    %fs,TF_FS(%rsp)
332         movw    %gs,TF_GS(%rsp)
333         movw    %es,TF_ES(%rsp)
334         movw    %ds,TF_DS(%rsp)
335         testl   $PSL_I,TF_RFLAGS(%rsp)
336         jz      alltraps_pushregs_no_rdi
337         sti
338         jmp     alltraps_pushregs_no_rdi
339
340 /*
341  * Fast syscall entry point.  We enter here with just our new %cs/%ss set,
342  * and the new privilige level.  We are still running on the old user stack
343  * pointer.  We have to juggle a few things around to find our stack etc.
344  * swapgs gives us access to our PCPU space only.
345  *
346  * We do not support invoking this from a custom %cs or %ss (e.g. using
347  * entries from an LDT).
348  */
349 IDTVEC(fast_syscall)
350         swapgs
351         movq    %rsp,PCPU(SCRATCH_RSP)
352         movq    PCPU(RSP0),%rsp
353         /* Now emulate a trapframe. Make the 8 byte alignment odd for call. */
354         subq    $TF_SIZE,%rsp
355         /* defer TF_RSP till we have a spare register */
356         movq    %r11,TF_RFLAGS(%rsp)
357         movq    %rcx,TF_RIP(%rsp)       /* %rcx original value is in %r10 */
358         movq    PCPU(SCRATCH_RSP),%r11  /* %r11 already saved */
359         movq    %r11,TF_RSP(%rsp)       /* user stack pointer */
360         movw    %fs,TF_FS(%rsp)
361         movw    %gs,TF_GS(%rsp)
362         movw    %es,TF_ES(%rsp)
363         movw    %ds,TF_DS(%rsp)
364         movq    PCPU(CURPCB),%r11
365         andl    $~PCB_FULL_IRET,PCB_FLAGS(%r11)
366         sti
367         movq    $KUDSEL,TF_SS(%rsp)
368         movq    $KUCSEL,TF_CS(%rsp)
369         movq    $2,TF_ERR(%rsp)
370         movq    %rdi,TF_RDI(%rsp)       /* arg 1 */
371         movq    %rsi,TF_RSI(%rsp)       /* arg 2 */
372         movq    %rdx,TF_RDX(%rsp)       /* arg 3 */
373         movq    %r10,TF_RCX(%rsp)       /* arg 4 */
374         movq    %r8,TF_R8(%rsp)         /* arg 5 */
375         movq    %r9,TF_R9(%rsp)         /* arg 6 */
376         movq    %rax,TF_RAX(%rsp)       /* syscall number */
377         movq    %rbx,TF_RBX(%rsp)       /* C preserved */
378         movq    %rbp,TF_RBP(%rsp)       /* C preserved */
379         movq    %r12,TF_R12(%rsp)       /* C preserved */
380         movq    %r13,TF_R13(%rsp)       /* C preserved */
381         movq    %r14,TF_R14(%rsp)       /* C preserved */
382         movq    %r15,TF_R15(%rsp)       /* C preserved */
383         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
384         cld
385         FAKE_MCOUNT(TF_RIP(%rsp))
386         movq    PCPU(CURTHREAD),%rdi
387         movq    %rsp,TD_FRAME(%rdi)
388         movl    TF_RFLAGS(%rsp),%esi
389         andl    $PSL_T,%esi
390         call    amd64_syscall
391 1:      movq    PCPU(CURPCB),%rax
392         /* Disable interrupts before testing PCB_FULL_IRET. */
393         cli
394         testl   $PCB_FULL_IRET,PCB_FLAGS(%rax)
395         jnz     3f
396         /* Check for and handle AST's on return to userland. */
397         movq    PCPU(CURTHREAD),%rax
398         testl   $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%rax)
399         jne     2f
400         /* Restore preserved registers. */
401         MEXITCOUNT
402         movq    TF_RDI(%rsp),%rdi       /* bonus; preserve arg 1 */
403         movq    TF_RSI(%rsp),%rsi       /* bonus: preserve arg 2 */
404         movq    TF_RDX(%rsp),%rdx       /* return value 2 */
405         movq    TF_RAX(%rsp),%rax       /* return value 1 */
406         movq    TF_RFLAGS(%rsp),%r11    /* original %rflags */
407         movq    TF_RIP(%rsp),%rcx       /* original %rip */
408         movq    TF_RSP(%rsp),%rsp       /* user stack pointer */
409         swapgs
410         sysretq
411
412 2:      /* AST scheduled. */
413         sti
414         movq    %rsp,%rdi
415         call    ast
416         jmp     1b
417
418 3:      /* Requested full context restore, use doreti for that. */
419         MEXITCOUNT
420         jmp     doreti
421
422 /*
423  * Here for CYA insurance, in case a "syscall" instruction gets
424  * issued from 32 bit compatability mode. MSR_CSTAR has to point
425  * to *something* if EFER_SCE is enabled.
426  */
427 IDTVEC(fast_syscall32)
428         sysret
429
430 /*
431  * NMI handling is special.
432  *
433  * First, NMIs do not respect the state of the processor's RFLAGS.IF
434  * bit.  The NMI handler may be entered at any time, including when
435  * the processor is in a critical section with RFLAGS.IF == 0.
436  * The processor's GS.base value could be invalid on entry to the
437  * handler.
438  *
439  * Second, the processor treats NMIs specially, blocking further NMIs
440  * until an 'iretq' instruction is executed.  We thus need to execute
441  * the NMI handler with interrupts disabled, to prevent a nested interrupt
442  * from executing an 'iretq' instruction and inadvertently taking the
443  * processor out of NMI mode.
444  *
445  * Third, the NMI handler runs on its own stack (tss_ist2). The canonical
446  * GS.base value for the processor is stored just above the bottom of its
447  * NMI stack.  For NMIs taken from kernel mode, the current value in
448  * the processor's GS.base is saved at entry to C-preserved register %r12,
449  * the canonical value for GS.base is then loaded into the processor, and
450  * the saved value is restored at exit time.  For NMIs taken from user mode,
451  * the cheaper 'SWAPGS' instructions are used for swapping GS.base.
452  */
453
454 IDTVEC(nmi)
455         subq    $TF_RIP,%rsp
456         movl    $(T_NMI),TF_TRAPNO(%rsp)
457         movq    $0,TF_ADDR(%rsp)
458         movq    $0,TF_ERR(%rsp)
459         movq    %rdi,TF_RDI(%rsp)
460         movq    %rsi,TF_RSI(%rsp)
461         movq    %rdx,TF_RDX(%rsp)
462         movq    %rcx,TF_RCX(%rsp)
463         movq    %r8,TF_R8(%rsp)
464         movq    %r9,TF_R9(%rsp)
465         movq    %rax,TF_RAX(%rsp)
466         movq    %rbx,TF_RBX(%rsp)
467         movq    %rbp,TF_RBP(%rsp)
468         movq    %r10,TF_R10(%rsp)
469         movq    %r11,TF_R11(%rsp)
470         movq    %r12,TF_R12(%rsp)
471         movq    %r13,TF_R13(%rsp)
472         movq    %r14,TF_R14(%rsp)
473         movq    %r15,TF_R15(%rsp)
474         movw    %fs,TF_FS(%rsp)
475         movw    %gs,TF_GS(%rsp)
476         movw    %es,TF_ES(%rsp)
477         movw    %ds,TF_DS(%rsp)
478         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
479         cld
480         xorl    %ebx,%ebx
481         testb   $SEL_RPL_MASK,TF_CS(%rsp)
482         jnz     nmi_fromuserspace
483         /*
484          * We've interrupted the kernel.  Preserve GS.base in %r12.
485          */
486         movl    $MSR_GSBASE,%ecx
487         rdmsr
488         movq    %rax,%r12
489         shlq    $32,%rdx
490         orq     %rdx,%r12
491         /* Retrieve and load the canonical value for GS.base. */
492         movq    TF_SIZE(%rsp),%rdx
493         movl    %edx,%eax
494         shrq    $32,%rdx
495         wrmsr
496         jmp     nmi_calltrap
497 nmi_fromuserspace:
498         incl    %ebx
499         swapgs
500 /* Note: this label is also used by ddb and gdb: */
501 nmi_calltrap:
502         FAKE_MCOUNT(TF_RIP(%rsp))
503         movq    %rsp,%rdi
504         call    trap
505         MEXITCOUNT
506 #ifdef HWPMC_HOOKS
507         /*
508          * Capture a userspace callchain if needed.
509          *
510          * - Check if the current trap was from user mode.
511          * - Check if the current thread is valid.
512          * - Check if the thread requires a user call chain to be
513          *   captured.
514          *
515          * We are still in NMI mode at this point.
516          */
517         testl   %ebx,%ebx
518         jz      nocallchain     /* not from userspace */
519         movq    PCPU(CURTHREAD),%rax
520         orq     %rax,%rax       /* curthread present? */
521         jz      nocallchain
522         testl   $TDP_CALLCHAIN,TD_PFLAGS(%rax) /* flagged for capture? */
523         jz      nocallchain
524         /*
525          * A user callchain is to be captured, so:
526          * - Move execution to the regular kernel stack, to allow for
527          *   nested NMI interrupts.
528          * - Take the processor out of "NMI" mode by faking an "iret".
529          * - Enable interrupts, so that copyin() can work.
530          */
531         movq    %rsp,%rsi       /* source stack pointer */
532         movq    $TF_SIZE,%rcx
533         movq    PCPU(RSP0),%rdx
534         subq    %rcx,%rdx
535         movq    %rdx,%rdi       /* destination stack pointer */
536
537         shrq    $3,%rcx         /* trap frame size in long words */
538         cld
539         rep
540         movsq                   /* copy trapframe */
541
542         movl    %ss,%eax
543         pushq   %rax            /* tf_ss */
544         pushq   %rdx            /* tf_rsp (on kernel stack) */
545         pushfq                  /* tf_rflags */
546         movl    %cs,%eax
547         pushq   %rax            /* tf_cs */
548         pushq   $outofnmi       /* tf_rip */
549         iretq
550 outofnmi:
551         /*
552          * At this point the processor has exited NMI mode and is running
553          * with interrupts turned off on the normal kernel stack.
554          *
555          * If a pending NMI gets recognized at or after this point, it
556          * will cause a kernel callchain to be traced.
557          *
558          * We turn interrupts back on, and call the user callchain capture hook.
559          */
560         movq    pmc_hook,%rax
561         orq     %rax,%rax
562         jz      nocallchain
563         movq    PCPU(CURTHREAD),%rdi            /* thread */
564         movq    $PMC_FN_USER_CALLCHAIN,%rsi     /* command */
565         movq    %rsp,%rdx                       /* frame */
566         sti
567         call    *%rax
568         cli
569 nocallchain:
570 #endif
571         testl   %ebx,%ebx
572         jnz     doreti_exit
573 nmi_kernelexit:
574         /*
575          * Put back the preserved MSR_GSBASE value.
576          */
577         movl    $MSR_GSBASE,%ecx
578         movq    %r12,%rdx
579         movl    %edx,%eax
580         shrq    $32,%rdx
581         wrmsr
582 nmi_restoreregs:
583         movq    TF_RDI(%rsp),%rdi
584         movq    TF_RSI(%rsp),%rsi
585         movq    TF_RDX(%rsp),%rdx
586         movq    TF_RCX(%rsp),%rcx
587         movq    TF_R8(%rsp),%r8
588         movq    TF_R9(%rsp),%r9
589         movq    TF_RAX(%rsp),%rax
590         movq    TF_RBX(%rsp),%rbx
591         movq    TF_RBP(%rsp),%rbp
592         movq    TF_R10(%rsp),%r10
593         movq    TF_R11(%rsp),%r11
594         movq    TF_R12(%rsp),%r12
595         movq    TF_R13(%rsp),%r13
596         movq    TF_R14(%rsp),%r14
597         movq    TF_R15(%rsp),%r15
598         addq    $TF_RIP,%rsp
599         jmp     doreti_iret
600
601 ENTRY(fork_trampoline)
602         movq    %r12,%rdi               /* function */
603         movq    %rbx,%rsi               /* arg1 */
604         movq    %rsp,%rdx               /* trapframe pointer */
605         call    fork_exit
606         MEXITCOUNT
607         jmp     doreti                  /* Handle any ASTs */
608
609 /*
610  * To efficiently implement classification of trap and interrupt handlers
611  * for profiling, there must be only trap handlers between the labels btrap
612  * and bintr, and only interrupt handlers between the labels bintr and
613  * eintr.  This is implemented (partly) by including files that contain
614  * some of the handlers.  Before including the files, set up a normal asm
615  * environment so that the included files doen't need to know that they are
616  * included.
617  */
618
619 #ifdef COMPAT_FREEBSD32
620         .data
621         .p2align 4
622         .text
623         SUPERALIGN_TEXT
624
625 #include <amd64/ia32/ia32_exception.S>
626 #endif
627
628         .data
629         .p2align 4
630         .text
631         SUPERALIGN_TEXT
632 MCOUNT_LABEL(bintr)
633
634 #include <amd64/amd64/apic_vector.S>
635
636 #ifdef DEV_ATPIC
637         .data
638         .p2align 4
639         .text
640         SUPERALIGN_TEXT
641
642 #include <amd64/amd64/atpic_vector.S>
643 #endif
644
645         .text
646 MCOUNT_LABEL(eintr)
647
648 /*
649  * void doreti(struct trapframe)
650  *
651  * Handle return from interrupts, traps and syscalls.
652  */
653         .text
654         SUPERALIGN_TEXT
655         .type   doreti,@function
656 doreti:
657         FAKE_MCOUNT($bintr)             /* init "from" bintr -> doreti */
658         /*
659          * Check if ASTs can be handled now.
660          */
661         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* are we returning to user mode? */
662         jz      doreti_exit             /* can't handle ASTs now if not */
663
664 doreti_ast:
665         /*
666          * Check for ASTs atomically with returning.  Disabling CPU
667          * interrupts provides sufficient locking even in the SMP case,
668          * since we will be informed of any new ASTs by an IPI.
669          */
670         cli
671         movq    PCPU(CURTHREAD),%rax
672         testl   $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%rax)
673         je      doreti_exit
674         sti
675         movq    %rsp,%rdi       /* pass a pointer to the trapframe */
676         call    ast
677         jmp     doreti_ast
678
679         /*
680          * doreti_exit: pop registers, iret.
681          *
682          *      The segment register pop is a special case, since it may
683          *      fault if (for example) a sigreturn specifies bad segment
684          *      registers.  The fault is handled in trap.c.
685          */
686 doreti_exit:
687         MEXITCOUNT
688         movq    PCPU(CURPCB),%r8
689
690         /*
691          * Do not reload segment registers for kernel.
692          * Since we do not reload segments registers with sane
693          * values on kernel entry, descriptors referenced by
694          * segments registers might be not valid.  This is fatal
695          * for user mode, but is not a problem for the kernel.
696          */
697         testb   $SEL_RPL_MASK,TF_CS(%rsp)
698         jz      ld_regs
699         testl   $PCB_FULL_IRET,PCB_FLAGS(%r8)
700         jz      ld_regs
701         testl   $TF_HASSEGS,TF_FLAGS(%rsp)
702         je      set_segs
703
704 do_segs:
705         /* Restore %fs and fsbase */
706         movw    TF_FS(%rsp),%ax
707         .globl  ld_fs
708 ld_fs:
709         movw    %ax,%fs
710         cmpw    $KUF32SEL,%ax
711         jne     1f
712         movl    $MSR_FSBASE,%ecx
713         movl    PCB_FSBASE(%r8),%eax
714         movl    PCB_FSBASE+4(%r8),%edx
715         .globl  ld_fsbase
716 ld_fsbase:
717         wrmsr
718 1:
719         /* Restore %gs and gsbase */
720         movw    TF_GS(%rsp),%si
721         pushfq
722         cli
723         movl    $MSR_GSBASE,%ecx
724         /* Save current kernel %gs base into %r12d:%r13d */
725         rdmsr
726         movl    %eax,%r12d
727         movl    %edx,%r13d
728         .globl  ld_gs
729 ld_gs:
730         movw    %si,%gs
731         /* Save user %gs base into %r14d:%r15d */
732         rdmsr
733         movl    %eax,%r14d
734         movl    %edx,%r15d
735         /* Restore kernel %gs base */
736         movl    %r12d,%eax
737         movl    %r13d,%edx
738         wrmsr
739         popfq
740         /*
741          * Restore user %gs base, either from PCB if used for TLS, or
742          * from the previously saved msr read.
743          */
744         movl    $MSR_KGSBASE,%ecx
745         cmpw    $KUG32SEL,%si
746         jne     1f
747         movl    PCB_GSBASE(%r8),%eax
748         movl    PCB_GSBASE+4(%r8),%edx
749         jmp     ld_gsbase
750 1:
751         movl    %r14d,%eax
752         movl    %r15d,%edx
753         .globl  ld_gsbase
754 ld_gsbase:
755         wrmsr   /* May trap if non-canonical, but only for TLS. */
756         .globl  ld_es
757 ld_es:
758         movw    TF_ES(%rsp),%es
759         .globl  ld_ds
760 ld_ds:
761         movw    TF_DS(%rsp),%ds
762 ld_regs:
763         movq    TF_RDI(%rsp),%rdi
764         movq    TF_RSI(%rsp),%rsi
765         movq    TF_RDX(%rsp),%rdx
766         movq    TF_RCX(%rsp),%rcx
767         movq    TF_R8(%rsp),%r8
768         movq    TF_R9(%rsp),%r9
769         movq    TF_RAX(%rsp),%rax
770         movq    TF_RBX(%rsp),%rbx
771         movq    TF_RBP(%rsp),%rbp
772         movq    TF_R10(%rsp),%r10
773         movq    TF_R11(%rsp),%r11
774         movq    TF_R12(%rsp),%r12
775         movq    TF_R13(%rsp),%r13
776         movq    TF_R14(%rsp),%r14
777         movq    TF_R15(%rsp),%r15
778         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
779         jz      1f                      /* keep running with kernel GS.base */
780         cli
781         swapgs
782 1:
783         addq    $TF_RIP,%rsp            /* skip over tf_err, tf_trapno */
784         .globl  doreti_iret
785 doreti_iret:
786         iretq
787
788 set_segs:
789         movw    $KUDSEL,%ax
790         movw    %ax,TF_DS(%rsp)
791         movw    %ax,TF_ES(%rsp)
792         movw    $KUF32SEL,TF_FS(%rsp)
793         movw    $KUG32SEL,TF_GS(%rsp)
794         jmp     do_segs
795
796         /*
797          * doreti_iret_fault.  Alternative return code for
798          * the case where we get a fault in the doreti_exit code
799          * above.  trap() (amd64/amd64/trap.c) catches this specific
800          * case, sends the process a signal and continues in the
801          * corresponding place in the code below.
802          */
803         ALIGN_TEXT
804         .globl  doreti_iret_fault
805 doreti_iret_fault:
806         subq    $TF_RIP,%rsp            /* space including tf_err, tf_trapno */
807         testl   $PSL_I,TF_RFLAGS(%rsp)
808         jz      1f
809         sti
810 1:
811         movw    %fs,TF_FS(%rsp)
812         movw    %gs,TF_GS(%rsp)
813         movw    %es,TF_ES(%rsp)
814         movw    %ds,TF_DS(%rsp)
815         movl    $TF_HASSEGS,TF_FLAGS(%rsp)
816         movq    %rdi,TF_RDI(%rsp)
817         movq    %rsi,TF_RSI(%rsp)
818         movq    %rdx,TF_RDX(%rsp)
819         movq    %rcx,TF_RCX(%rsp)
820         movq    %r8,TF_R8(%rsp)
821         movq    %r9,TF_R9(%rsp)
822         movq    %rax,TF_RAX(%rsp)
823         movq    %rbx,TF_RBX(%rsp)
824         movq    %rbp,TF_RBP(%rsp)
825         movq    %r10,TF_R10(%rsp)
826         movq    %r11,TF_R11(%rsp)
827         movq    %r12,TF_R12(%rsp)
828         movq    %r13,TF_R13(%rsp)
829         movq    %r14,TF_R14(%rsp)
830         movq    %r15,TF_R15(%rsp)
831         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
832         movq    $0,TF_ERR(%rsp) /* XXX should be the error code */
833         movq    $0,TF_ADDR(%rsp)
834         FAKE_MCOUNT(TF_RIP(%rsp))
835         jmp     calltrap
836
837         ALIGN_TEXT
838         .globl  ds_load_fault
839 ds_load_fault:
840         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
841         testl   $PSL_I,TF_RFLAGS(%rsp)
842         jz      1f
843         sti
844 1:
845         movq    %rsp,%rdi
846         call    trap
847         movw    $KUDSEL,TF_DS(%rsp)
848         jmp     doreti
849
850         ALIGN_TEXT
851         .globl  es_load_fault
852 es_load_fault:
853         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
854         testl   $PSL_I,TF_RFLAGS(%rsp)
855         jz      1f
856         sti
857 1:
858         movq    %rsp,%rdi
859         call    trap
860         movw    $KUDSEL,TF_ES(%rsp)
861         jmp     doreti
862
863         ALIGN_TEXT
864         .globl  fs_load_fault
865 fs_load_fault:
866         testl   $PSL_I,TF_RFLAGS(%rsp)
867         jz      1f
868         sti
869 1:
870         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
871         movq    %rsp,%rdi
872         call    trap
873         movw    $KUF32SEL,TF_FS(%rsp)
874         jmp     doreti
875
876         ALIGN_TEXT
877         .globl  gs_load_fault
878 gs_load_fault:
879         popfq
880         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
881         testl   $PSL_I,TF_RFLAGS(%rsp)
882         jz      1f
883         sti
884 1:
885         movq    %rsp,%rdi
886         call    trap
887         movw    $KUG32SEL,TF_GS(%rsp)
888         jmp     doreti
889
890         ALIGN_TEXT
891         .globl  fsbase_load_fault
892 fsbase_load_fault:
893         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
894         testl   $PSL_I,TF_RFLAGS(%rsp)
895         jz      1f
896         sti
897 1:
898         movq    %rsp,%rdi
899         call    trap
900         movq    PCPU(CURTHREAD),%r8
901         movq    TD_PCB(%r8),%r8
902         movq    $0,PCB_FSBASE(%r8)
903         jmp     doreti
904
905         ALIGN_TEXT
906         .globl  gsbase_load_fault
907 gsbase_load_fault:
908         movl    $T_PROTFLT,TF_TRAPNO(%rsp)
909         testl   $PSL_I,TF_RFLAGS(%rsp)
910         jz      1f
911         sti
912 1:
913         movq    %rsp,%rdi
914         call    trap
915         movq    PCPU(CURTHREAD),%r8
916         movq    TD_PCB(%r8),%r8
917         movq    $0,PCB_GSBASE(%r8)
918         jmp     doreti
919
920 #ifdef HWPMC_HOOKS
921         ENTRY(end_exceptions)
922 #endif