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