Merge remote branch 'crater/vendor/MDOCML' into HEAD
[dragonfly.git] / sys / platform / pc64 / x86_64 / 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  * Copyright (c) 2008 The DragonFly Project.
6  * Copyright (c) 2008 Jordan Gordeev.
7  * All rights reserved.
8  *
9  * Portions of this software were developed by A. Joseph Koshy under
10  * sponsorship from the FreeBSD Foundation and Google, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #if JG
38 #include "opt_atpic.h"
39 #endif
40 #include "opt_compat.h"
41
42 #include <machine/asmacros.h>
43 #include <machine/psl.h>
44 #include <machine/trap.h>
45 #include <machine/segments.h>
46
47 #include "assym.s"
48
49         .text
50
51 /*****************************************************************************/
52 /* Trap handling                                                             */
53 /*****************************************************************************/
54 /*
55  * Trap and fault vector routines.
56  *
57  * All traps are 'interrupt gates', SDT_SYSIGT.  An interrupt gate pushes
58  * state on the stack but also disables interrupts.  This is important for
59  * us for the use of the swapgs instruction.  We cannot be interrupted
60  * until the GS.base value is correct.  For most traps, we automatically
61  * then enable interrupts if the interrupted context had them enabled.
62  * This is equivalent to the i386 port's use of SDT_SYS386TGT.
63  *
64  * The cpu will push a certain amount of state onto the kernel stack for
65  * the current process.  See x86_64/include/frame.h.  
66  * This includes the current RFLAGS (status register, which includes 
67  * the interrupt disable state prior to the trap), the code segment register,
68  * and the return instruction pointer are pushed by the cpu.  The cpu 
69  * will also push an 'error' code for certain traps.  We push a dummy 
70  * error code for those traps where the cpu doesn't in order to maintain 
71  * a consistent frame.  We also push a contrived 'trap number'.
72  *
73  * The cpu does not push the general registers, we must do that, and we 
74  * must restore them prior to calling 'iret'.  The cpu adjusts the %cs and
75  * %ss segment registers, but does not mess with %ds, %es, or %fs.  Thus we
76  * must load them with appropriate values for supervisor mode operation.
77  */
78
79 MCOUNT_LABEL(user)
80 MCOUNT_LABEL(btrap)
81
82 /*
83  * Interrupts are enabled for all traps, otherwise horrible livelocks
84  * can occur with the smp_invltlb and cpusync ode.
85  */
86 #if 0
87 #define TRAP_NOEN(a)    \
88         subq $TF_RIP,%rsp; \
89         movq $0,TF_XFLAGS(%rsp) ; \
90         movq $(a),TF_TRAPNO(%rsp) ; \
91         movq $0,TF_ADDR(%rsp) ; \
92         movq $0,TF_ERR(%rsp) ; \
93         jmp alltraps_noen
94 #endif
95
96 /* Regular traps; The cpu does not supply tf_err for these. */
97 #define TRAP(a)  \
98         subq $TF_RIP,%rsp; \
99         movq $0,TF_XFLAGS(%rsp) ; \
100         movq $(a),TF_TRAPNO(%rsp) ; \
101         movq $0,TF_ADDR(%rsp) ; \
102         movq $0,TF_ERR(%rsp) ; \
103         jmp alltraps
104 IDTVEC(dbg)
105         TRAP(T_TRCTRAP)
106 IDTVEC(bpt)
107         TRAP(T_BPTFLT)
108 IDTVEC(div)
109         TRAP(T_DIVIDE)
110 IDTVEC(ofl)
111         TRAP(T_OFLOW)
112 IDTVEC(bnd)
113         TRAP(T_BOUND)
114 IDTVEC(ill)
115         TRAP(T_PRIVINFLT)
116 IDTVEC(dna)
117         TRAP(T_DNA)
118 IDTVEC(fpusegm)
119         TRAP(T_FPOPFLT)
120 IDTVEC(mchk)
121         TRAP(T_MCHK)
122 IDTVEC(rsvd)
123         TRAP(T_RESERVED)
124 IDTVEC(fpu)
125         TRAP(T_ARITHTRAP)
126 IDTVEC(xmm)
127         TRAP(T_XMMFLT)
128
129 /* This group of traps have tf_err already pushed by the cpu */
130 #define TRAP_ERR(a)     \
131         subq $TF_ERR,%rsp; \
132         movq $(a),TF_TRAPNO(%rsp) ; \
133         movq $0,TF_ADDR(%rsp) ; \
134         movq $0,TF_XFLAGS(%rsp) ; \
135         jmp alltraps
136 IDTVEC(tss)
137         TRAP_ERR(T_TSSFLT)
138 IDTVEC(missing)
139         TRAP_ERR(T_SEGNPFLT)
140 IDTVEC(stk)
141         TRAP_ERR(T_STKFLT)
142 IDTVEC(align)
143         TRAP_ERR(T_ALIGNFLT)
144
145         /*
146          * alltraps entry point.  Use swapgs if this is the first time in the
147          * kernel from userland.  Reenable interrupts if they were enabled
148          * before the trap.  This approximates SDT_SYS386TGT on the i386 port.
149          */
150
151         SUPERALIGN_TEXT
152         .globl  alltraps
153         .type   alltraps,@function
154 alltraps:
155         /* Fixup %gs if coming from userland */
156         testb   $SEL_RPL_MASK,TF_CS(%rsp)
157         jz      alltraps_testi
158         swapgs
159 alltraps_testi:
160         testq   $PSL_I,TF_RFLAGS(%rsp)
161         jz      alltraps_pushregs
162         sti
163 alltraps_pushregs:
164         movq    %rdi,TF_RDI(%rsp)
165 alltraps_pushregs_no_rdi:
166         movq    %rsi,TF_RSI(%rsp)
167         movq    %rdx,TF_RDX(%rsp)
168         movq    %rcx,TF_RCX(%rsp)
169         movq    %r8,TF_R8(%rsp)
170         movq    %r9,TF_R9(%rsp)
171         movq    %rax,TF_RAX(%rsp)
172         movq    %rbx,TF_RBX(%rsp)
173         movq    %rbp,TF_RBP(%rsp)
174         movq    %r10,TF_R10(%rsp)
175         movq    %r11,TF_R11(%rsp)
176         movq    %r12,TF_R12(%rsp)
177         movq    %r13,TF_R13(%rsp)
178         movq    %r14,TF_R14(%rsp)
179         movq    %r15,TF_R15(%rsp)
180         FAKE_MCOUNT(TF_RIP(%rsp))
181         .globl  calltrap
182         .type   calltrap,@function
183 calltrap:
184         movq    %rsp, %rdi
185         call    trap
186         MEXITCOUNT
187         jmp     doreti                  /* Handle any pending ASTs */
188
189         /*
190          * alltraps_noen entry point.  Unlike alltraps above, we want to
191          * leave the interrupts disabled.  This corresponds to
192          * SDT_SYS386IGT on the i386 port.
193          */
194         SUPERALIGN_TEXT
195         .globl  alltraps_noen
196         .type   alltraps_noen,@function
197 alltraps_noen:
198         /* Fixup %gs if coming from userland */
199         testb   $SEL_RPL_MASK,TF_CS(%rsp)
200         jz      alltraps_pushregs
201         swapgs
202         jmp     alltraps_pushregs
203
204 IDTVEC(dblfault)
205         subq    $TF_ERR,%rsp
206         movq    $T_DOUBLEFLT,TF_TRAPNO(%rsp)
207         movq    $0,TF_ADDR(%rsp)
208         movq    $0,TF_ERR(%rsp)
209         movq    $0,TF_XFLAGS(%rsp)
210         movq    %rdi,TF_RDI(%rsp)
211         movq    %rsi,TF_RSI(%rsp)
212         movq    %rdx,TF_RDX(%rsp)
213         movq    %rcx,TF_RCX(%rsp)
214         movq    %r8,TF_R8(%rsp)
215         movq    %r9,TF_R9(%rsp)
216         movq    %rax,TF_RAX(%rsp)
217         movq    %rbx,TF_RBX(%rsp)
218         movq    %rbp,TF_RBP(%rsp)
219         movq    %r10,TF_R10(%rsp)
220         movq    %r11,TF_R11(%rsp)
221         movq    %r12,TF_R12(%rsp)
222         movq    %r13,TF_R13(%rsp)
223         movq    %r14,TF_R14(%rsp)
224         movq    %r15,TF_R15(%rsp)
225         testb   $SEL_RPL_MASK,TF_CS(%rsp)
226         jz      1f
227         swapgs
228 1:      movq    %rsp, %rdi
229         call    dblfault_handler
230 2:      hlt
231         jmp     2b
232
233 IDTVEC(page)
234         subq    $TF_ERR,%rsp
235         movq    $T_PAGEFLT,TF_TRAPNO(%rsp)
236         /* Fixup %gs if coming from userland */
237         testb   $SEL_RPL_MASK,TF_CS(%rsp)
238         jz      1f
239         swapgs
240 1:
241         movq    %rdi,TF_RDI(%rsp)       /* free up a GP register */
242         movq    %cr2,%rdi               /* preserve %cr2 before ..  */
243         movq    %rdi,TF_ADDR(%rsp)      /* enabling interrupts. */
244         movq    $0,TF_XFLAGS(%rsp)
245         testq   $PSL_I,TF_RFLAGS(%rsp)
246         jz      alltraps_pushregs_no_rdi
247         sti
248         jmp     alltraps_pushregs_no_rdi
249
250         /*
251          * We have to special-case this one.  If we get a trap in doreti() at
252          * the iretq stage, we'll reenter with the wrong gs state.  We'll have
253          * to do a special the swapgs in this case even coming from the kernel.
254          * XXX linux has a trap handler for their equivalent of load_gs().
255          */
256 IDTVEC(prot)
257         subq    $TF_ERR,%rsp
258         movq    $T_PROTFLT,TF_TRAPNO(%rsp)
259         movq    $0,TF_ADDR(%rsp)
260         movq    $0,TF_XFLAGS(%rsp)
261         movq    %rdi,TF_RDI(%rsp)       /* free up a GP register */
262
263         /*
264          * Fixup %gs if coming from userland.  Handle the special case where
265          * %fs faults in doreti at the iretq instruction itself.
266          */
267         leaq    doreti_iret(%rip),%rdi
268         cmpq    %rdi,TF_RIP(%rsp)               /* special iretq fault case */
269         je      2f
270         testb   $SEL_RPL_MASK,TF_CS(%rsp)       /* check if from userland */
271         jz      1f
272 2:
273         swapgs
274 1:
275         testq   $PSL_I,TF_RFLAGS(%rsp)
276         jz      alltraps_pushregs_no_rdi
277         sti
278         jmp     alltraps_pushregs_no_rdi
279
280 /*
281  * Fast syscall entry point.  We enter here with just our new %cs/%ss set,
282  * and the new privilige level.  We are still running on the old user stack
283  * pointer.  We have to juggle a few things around to find our stack etc.
284  * swapgs gives us access to our PCPU space only.
285  */
286 IDTVEC(fast_syscall)
287         swapgs
288         movq    %rsp,PCPU(scratch_rsp)
289         movq    PCPU(common_tss) + TSS_RSP0, %rsp
290         /* Now emulate a trapframe. Make the 8 byte alignment odd for call. */
291         subq    $TF_SIZE,%rsp
292         /* defer TF_RSP till we have a spare register */
293         movq    %r11,TF_RFLAGS(%rsp)
294         movq    %rcx,TF_RIP(%rsp)       /* %rcx original value is in %r10 */
295         movq    PCPU(scratch_rsp),%r11  /* %r11 already saved */
296         movq    %r11,TF_RSP(%rsp)       /* user stack pointer */
297         sti
298         movq    $KUDSEL,TF_SS(%rsp)
299         movq    $KUCSEL,TF_CS(%rsp)
300         movq    $2,TF_ERR(%rsp)
301         movq    $T_FAST_SYSCALL,TF_TRAPNO(%rsp) /* for the vkernel */
302         movq    $0,TF_XFLAGS(%rsp)      /* note: used in signal frame */
303         movq    %rdi,TF_RDI(%rsp)       /* arg 1 */
304         movq    %rsi,TF_RSI(%rsp)       /* arg 2 */
305         movq    %rdx,TF_RDX(%rsp)       /* arg 3 */
306         movq    %r10,TF_RCX(%rsp)       /* arg 4 */
307         movq    %r8,TF_R8(%rsp)         /* arg 5 */
308         movq    %r9,TF_R9(%rsp)         /* arg 6 */
309         movq    %rax,TF_RAX(%rsp)       /* syscall number */
310         movq    %rbx,TF_RBX(%rsp)       /* C preserved */
311         movq    %rbp,TF_RBP(%rsp)       /* C preserved */
312         movq    %r12,TF_R12(%rsp)       /* C preserved */
313         movq    %r13,TF_R13(%rsp)       /* C preserved */
314         movq    %r14,TF_R14(%rsp)       /* C preserved */
315         movq    %r15,TF_R15(%rsp)       /* C preserved */
316         FAKE_MCOUNT(TF_RIP(%rsp))
317         movq    %rsp, %rdi
318         call    syscall2
319         MEXITCOUNT
320         jmp     doreti
321
322 /*
323  * Here for CYA insurance, in case a "syscall" instruction gets
324  * issued from 32 bit compatability mode. MSR_CSTAR has to point
325  * to *something* if EFER_SCE is enabled.
326  */
327 IDTVEC(fast_syscall32)
328         sysret
329
330 /*
331  * NMI handling is special.
332  *
333  * First, NMIs do not respect the state of the processor's RFLAGS.IF
334  * bit and the NMI handler may be invoked at any time, including when
335  * the processor is in a critical section with RFLAGS.IF == 0.  In
336  * particular, this means that the processor's GS.base values could be
337  * inconsistent on entry to the handler, and so we need to read
338  * MSR_GSBASE to determine if a 'swapgs' is needed.  We use '%ebx', a
339  * C-preserved register, to remember whether to swap GS back on the
340  * exit path.
341  *
342  * Second, the processor treats NMIs specially, blocking further NMIs
343  * until an 'iretq' instruction is executed.  We therefore need to
344  * execute the NMI handler with interrupts disabled to prevent a
345  * nested interrupt from executing an 'iretq' instruction and
346  * inadvertently taking the processor out of NMI mode.
347  *
348  * Third, the NMI handler runs on its own stack (tss_ist1), shared
349  * with the double fault handler.
350  */
351
352 IDTVEC(nmi)
353         subq    $TF_RIP,%rsp
354         movq    $(T_NMI),TF_TRAPNO(%rsp)
355         movq    $0,TF_ADDR(%rsp)
356         movq    $0,TF_ERR(%rsp)
357         movq    $0,TF_XFLAGS(%rsp)
358         movq    %rdi,TF_RDI(%rsp)
359         movq    %rsi,TF_RSI(%rsp)
360         movq    %rdx,TF_RDX(%rsp)
361         movq    %rcx,TF_RCX(%rsp)
362         movq    %r8,TF_R8(%rsp)
363         movq    %r9,TF_R9(%rsp)
364         movq    %rax,TF_RAX(%rsp)
365         movq    %rbx,TF_RBX(%rsp)
366         movq    %rbp,TF_RBP(%rsp)
367         movq    %r10,TF_R10(%rsp)
368         movq    %r11,TF_R11(%rsp)
369         movq    %r12,TF_R12(%rsp)
370         movq    %r13,TF_R13(%rsp)
371         movq    %r14,TF_R14(%rsp)
372         movq    %r15,TF_R15(%rsp)
373         xorl    %ebx,%ebx
374         testb   $SEL_RPL_MASK,TF_CS(%rsp)
375         jnz     nmi_needswapgs          /* we came from userland */
376         movl    $MSR_GSBASE,%ecx
377         rdmsr
378         cmpl    $VM_MAX_USER_ADDRESS >> 32,%edx
379         jae     nmi_calltrap            /* GS.base holds a kernel VA */
380 nmi_needswapgs:
381         incl    %ebx
382         swapgs
383 /* Note: this label is also used by ddb and gdb: */
384 nmi_calltrap:
385         FAKE_MCOUNT(TF_RIP(%rsp))
386         movq    %rsp, %rdi
387         call    trap
388         MEXITCOUNT
389         testl   %ebx,%ebx
390         jz      nmi_restoreregs
391         swapgs
392 nmi_restoreregs:
393         movq    TF_RDI(%rsp),%rdi
394         movq    TF_RSI(%rsp),%rsi
395         movq    TF_RDX(%rsp),%rdx
396         movq    TF_RCX(%rsp),%rcx
397         movq    TF_R8(%rsp),%r8
398         movq    TF_R9(%rsp),%r9
399         movq    TF_RAX(%rsp),%rax
400         movq    TF_RBX(%rsp),%rbx
401         movq    TF_RBP(%rsp),%rbp
402         movq    TF_R10(%rsp),%r10
403         movq    TF_R11(%rsp),%r11
404         movq    TF_R12(%rsp),%r12
405         movq    TF_R13(%rsp),%r13
406         movq    TF_R14(%rsp),%r14
407         movq    TF_R15(%rsp),%r15
408         addq    $TF_RIP,%rsp
409         iretq
410
411 /*
412  * This function is what cpu_heavy_restore jumps to after a new process
413  * is created.  The LWKT subsystem switches while holding a critical
414  * section and we maintain that abstraction here (e.g. because 
415  * cpu_heavy_restore needs it due to PCB_*() manipulation), then get out of
416  * it before calling the initial function (typically fork_return()) and/or
417  * returning to user mode.
418  *
419  * The MP lock is not held at any point but the critcount is bumped
420  * on entry to prevent interruption of the trampoline at a bad point.
421  */
422 ENTRY(fork_trampoline)
423         movq    PCPU(curthread),%rax
424         decl    TD_CRITCOUNT(%rax)
425
426         /*
427          * cpu_set_fork_handler intercepts this function call to
428          * have this call a non-return function to stay in kernel mode.
429          *
430          * initproc has its own fork handler, start_init(), which DOES
431          * return.
432          *
433          * %rbx - chaining function (typically fork_return)
434          * %r12 -> %rdi (argument)
435          * frame-> %rsi (trap frame)
436          *
437          *   void (func:rbx)(arg:rdi, trapframe:rsi)
438          */
439         movq    %rsp, %rsi              /* pass trapframe by reference */
440         movq    %r12, %rdi              /* arg1 */
441         call    *%rbx                   /* function */
442
443         /* cut from syscall */
444
445         sti
446         call    splz
447
448         /*
449          * Return via doreti to handle ASTs.
450          *
451          * trapframe is at the top of the stack.
452          */
453         MEXITCOUNT
454         jmp     doreti
455
456 /*
457  * To efficiently implement classification of trap and interrupt handlers
458  * for profiling, there must be only trap handlers between the labels btrap
459  * and bintr, and only interrupt handlers between the labels bintr and
460  * eintr.  This is implemented (partly) by including files that contain
461  * some of the handlers.  Before including the files, set up a normal asm
462  * environment so that the included files doen't need to know that they are
463  * included.
464  */
465
466 #ifdef COMPAT_IA32
467         .data
468         .p2align 4
469         .text
470         SUPERALIGN_TEXT
471
472 #include <x86_64/ia32/ia32_exception.S>
473 #endif
474
475         .data
476         .p2align 4
477         .text
478         SUPERALIGN_TEXT
479 MCOUNT_LABEL(bintr)
480
481 #if JG
482 #include <x86_64/x86_64/apic_vector.S>
483 #endif
484
485 #ifdef DEV_ATPIC
486         .data
487         .p2align 4
488         .text
489         SUPERALIGN_TEXT
490
491 #include <x86_64/isa/atpic_vector.S>
492 #endif
493
494         .text
495 MCOUNT_LABEL(eintr)
496