Merge from vendor branch OPENSSH:
[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 $(a),TF_TRAPNO(%rsp) ; \
88         movq $0,TF_ADDR(%rsp) ; \
89         movq $0,TF_ERR(%rsp) ; \
90         jmp alltraps_noen
91 IDTVEC(dbg)
92         TRAP_NOEN(T_TRCTRAP)
93 IDTVEC(bpt)
94         TRAP_NOEN(T_BPTFLT)
95
96 /* Regular traps; The cpu does not supply tf_err for these. */
97 #define TRAP(a)  \
98         subq $TF_RIP,%rsp; \
99         movq $(a),TF_TRAPNO(%rsp) ; \
100         movq $0,TF_ADDR(%rsp) ; \
101         movq $0,TF_ERR(%rsp) ; \
102         jmp alltraps
103 IDTVEC(div)
104         TRAP(T_DIVIDE)
105 IDTVEC(ofl)
106         TRAP(T_OFLOW)
107 IDTVEC(bnd)
108         TRAP(T_BOUND)
109 IDTVEC(ill)
110         TRAP(T_PRIVINFLT)
111 IDTVEC(dna)
112         TRAP(T_DNA)
113 IDTVEC(fpusegm)
114         TRAP(T_FPOPFLT)
115 IDTVEC(mchk)
116         TRAP(T_MCHK)
117 IDTVEC(rsvd)
118         TRAP(T_RESERVED)
119 IDTVEC(fpu)
120         TRAP(T_ARITHTRAP)
121 IDTVEC(xmm)
122         TRAP(T_XMMFLT)
123
124 /* This group of traps have tf_err already pushed by the cpu */
125 #define TRAP_ERR(a)     \
126         subq $TF_ERR,%rsp; \
127         movq $(a),TF_TRAPNO(%rsp) ; \
128         movq $0,TF_ADDR(%rsp) ; \
129         jmp alltraps
130 IDTVEC(tss)
131         TRAP_ERR(T_TSSFLT)
132 IDTVEC(missing)
133         TRAP_ERR(T_SEGNPFLT)
134 IDTVEC(stk)
135         TRAP_ERR(T_STKFLT)
136 IDTVEC(align)
137         TRAP_ERR(T_ALIGNFLT)
138
139         /*
140          * alltraps entry point.  Use swapgs if this is the first time in the
141          * kernel from userland.  Reenable interrupts if they were enabled
142          * before the trap.  This approximates SDT_SYS386TGT on the i386 port.
143          */
144
145         SUPERALIGN_TEXT
146         .globl  alltraps
147         .type   alltraps,@function
148 alltraps:
149         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
150         jz      alltraps_testi          /* already running with kernel GS.base */
151         swapgs
152 alltraps_testi:
153         testq   $PSL_I,TF_RFLAGS(%rsp)
154         jz      alltraps_pushregs
155         sti
156 alltraps_pushregs:
157         movq    %rdi,TF_RDI(%rsp)
158 alltraps_pushregs_no_rdi:
159         movq    %rsi,TF_RSI(%rsp)
160         movq    %rdx,TF_RDX(%rsp)
161         movq    %rcx,TF_RCX(%rsp)
162         movq    %r8,TF_R8(%rsp)
163         movq    %r9,TF_R9(%rsp)
164         movq    %rax,TF_RAX(%rsp)
165         movq    %rbx,TF_RBX(%rsp)
166         movq    %rbp,TF_RBP(%rsp)
167         movq    %r10,TF_R10(%rsp)
168         movq    %r11,TF_R11(%rsp)
169         movq    %r12,TF_R12(%rsp)
170         movq    %r13,TF_R13(%rsp)
171         movq    %r14,TF_R14(%rsp)
172         movq    %r15,TF_R15(%rsp)
173         FAKE_MCOUNT(TF_RIP(%rsp))
174         .globl  calltrap
175         .type   calltrap,@function
176 calltrap:
177         movq    %rsp, %rdi
178         call    trap
179         MEXITCOUNT
180         jmp     doreti                  /* Handle any pending ASTs */
181
182         /*
183          * alltraps_noen entry point.  Unlike alltraps above, we want to
184          * leave the interrupts disabled.  This corresponds to
185          * SDT_SYS386IGT on the i386 port.
186          */
187         SUPERALIGN_TEXT
188         .globl  alltraps_noen
189         .type   alltraps_noen,@function
190 alltraps_noen:
191         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
192         jz      alltraps_pushregs       /* already running with kernel GS.base */
193         swapgs
194         jmp     alltraps_pushregs
195
196 IDTVEC(dblfault)
197         subq    $TF_ERR,%rsp
198         movq    $T_DOUBLEFLT,TF_TRAPNO(%rsp)
199         movq    $0,TF_ADDR(%rsp)
200         movq    $0,TF_ERR(%rsp)
201         movq    %rdi,TF_RDI(%rsp)
202         movq    %rsi,TF_RSI(%rsp)
203         movq    %rdx,TF_RDX(%rsp)
204         movq    %rcx,TF_RCX(%rsp)
205         movq    %r8,TF_R8(%rsp)
206         movq    %r9,TF_R9(%rsp)
207         movq    %rax,TF_RAX(%rsp)
208         movq    %rbx,TF_RBX(%rsp)
209         movq    %rbp,TF_RBP(%rsp)
210         movq    %r10,TF_R10(%rsp)
211         movq    %r11,TF_R11(%rsp)
212         movq    %r12,TF_R12(%rsp)
213         movq    %r13,TF_R13(%rsp)
214         movq    %r14,TF_R14(%rsp)
215         movq    %r15,TF_R15(%rsp)
216         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
217         jz      1f                      /* already running with kernel GS.base */
218         swapgs
219 1:      movq    %rsp, %rdi
220         call    dblfault_handler
221 2:      hlt
222         jmp     2b
223
224 IDTVEC(page)
225         subq    $TF_ERR,%rsp
226         movq    $T_PAGEFLT,TF_TRAPNO(%rsp)
227         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
228         jz      1f                      /* already running with kernel GS.base */
229         swapgs
230 1:
231         movq    %rdi,TF_RDI(%rsp)       /* free up a GP register */
232         movq    %cr2,%rdi               /* preserve %cr2 before ..  */
233         movq    %rdi,TF_ADDR(%rsp)      /* enabling interrupts. */
234         testq   $PSL_I,TF_RFLAGS(%rsp)
235         jz      alltraps_pushregs_no_rdi
236         sti
237         jmp     alltraps_pushregs_no_rdi
238
239         /*
240          * We have to special-case this one.  If we get a trap in doreti() at
241          * the iretq stage, we'll reenter with the wrong gs state.  We'll have
242          * to do a special the swapgs in this case even coming from the kernel.
243          * XXX linux has a trap handler for their equivalent of load_gs().
244          */
245 IDTVEC(prot)
246         subq    $TF_ERR,%rsp
247         movq    $T_PROTFLT,TF_TRAPNO(%rsp)
248         movq    $0,TF_ADDR(%rsp)
249         movq    %rdi,TF_RDI(%rsp)       /* free up a GP register */
250         leaq    doreti_iret(%rip),%rdi
251         cmpq    %rdi,TF_RIP(%rsp)
252         je      2f                      /* kernel but with user gsbase!! */
253         testb   $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
254         jz      1f                      /* already running with kernel GS.base */
255 2:
256         swapgs
257 1:
258         testq   $PSL_I,TF_RFLAGS(%rsp)
259         jz      alltraps_pushregs_no_rdi
260         sti
261         jmp     alltraps_pushregs_no_rdi
262
263 /*
264  * Fast syscall entry point.  We enter here with just our new %cs/%ss set,
265  * and the new privilige level.  We are still running on the old user stack
266  * pointer.  We have to juggle a few things around to find our stack etc.
267  * swapgs gives us access to our PCPU space only.
268  */
269 IDTVEC(fast_syscall)
270         swapgs
271         movq    %rsp,PCPU(scratch_rsp)
272         movq    PCPU(rsp0),%rsp
273         /* Now emulate a trapframe. Make the 8 byte alignment odd for call. */
274         subq    $TF_SIZE,%rsp
275         /* defer TF_RSP till we have a spare register */
276         movq    %r11,TF_RFLAGS(%rsp)
277         movq    %rcx,TF_RIP(%rsp)       /* %rcx original value is in %r10 */
278         movq    PCPU(scratch_rsp),%r11  /* %r11 already saved */
279         movq    %r11,TF_RSP(%rsp)       /* user stack pointer */
280         sti
281         movq    $KUDSEL,TF_SS(%rsp)
282         movq    $KUCSEL,TF_CS(%rsp)
283         movq    $2,TF_ERR(%rsp)
284         movq    %rdi,TF_RDI(%rsp)       /* arg 1 */
285         movq    %rsi,TF_RSI(%rsp)       /* arg 2 */
286         movq    %rdx,TF_RDX(%rsp)       /* arg 3 */
287         movq    %r10,TF_RCX(%rsp)       /* arg 4 */
288         movq    %r8,TF_R8(%rsp)         /* arg 5 */
289         movq    %r9,TF_R9(%rsp)         /* arg 6 */
290         movq    %rax,TF_RAX(%rsp)       /* syscall number */
291         movq    %rbx,TF_RBX(%rsp)       /* C preserved */
292         movq    %rbp,TF_RBP(%rsp)       /* C preserved */
293         movq    %r12,TF_R12(%rsp)       /* C preserved */
294         movq    %r13,TF_R13(%rsp)       /* C preserved */
295         movq    %r14,TF_R14(%rsp)       /* C preserved */
296         movq    %r15,TF_R15(%rsp)       /* C preserved */
297         FAKE_MCOUNT(TF_RIP(%rsp))
298         movq    %rsp, %rdi
299         call    syscall2
300         /* JGXXX handle AST's? */
301         /* restore preserved registers */
302         MEXITCOUNT
303         movq    TF_RDI(%rsp),%rdi       /* bonus; preserve arg 1 */
304         movq    TF_RSI(%rsp),%rsi       /* bonus: preserve arg 2 */
305         movq    TF_RDX(%rsp),%rdx       /* return value 2 */
306         movq    TF_RAX(%rsp),%rax       /* return value 1 */
307         movq    TF_RBX(%rsp),%rbx       /* C preserved */
308         movq    TF_RBP(%rsp),%rbp       /* C preserved */
309         movq    TF_R12(%rsp),%r12       /* C preserved */
310         movq    TF_R13(%rsp),%r13       /* C preserved */
311         movq    TF_R14(%rsp),%r14       /* C preserved */
312         movq    TF_R15(%rsp),%r15       /* C preserved */
313         movq    TF_RFLAGS(%rsp),%r11    /* original %rflags */
314         movq    TF_RIP(%rsp),%rcx       /* original %rip */
315         movq    TF_RSP(%rsp),%r9        /* user stack pointer */
316         movq    %r9,%rsp                /* original %rsp */
317         swapgs
318         sysretq
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    %rdi,TF_RDI(%rsp)
358         movq    %rsi,TF_RSI(%rsp)
359         movq    %rdx,TF_RDX(%rsp)
360         movq    %rcx,TF_RCX(%rsp)
361         movq    %r8,TF_R8(%rsp)
362         movq    %r9,TF_R9(%rsp)
363         movq    %rax,TF_RAX(%rsp)
364         movq    %rbx,TF_RBX(%rsp)
365         movq    %rbp,TF_RBP(%rsp)
366         movq    %r10,TF_R10(%rsp)
367         movq    %r11,TF_R11(%rsp)
368         movq    %r12,TF_R12(%rsp)
369         movq    %r13,TF_R13(%rsp)
370         movq    %r14,TF_R14(%rsp)
371         movq    %r15,TF_R15(%rsp)
372         xorl    %ebx,%ebx
373         testb   $SEL_RPL_MASK,TF_CS(%rsp)
374         jnz     nmi_needswapgs          /* we came from userland */
375         movl    $MSR_GSBASE,%ecx
376         rdmsr
377         cmpl    $VM_MAXUSER_ADDRESS >> 32,%edx
378         jae     nmi_calltrap            /* GS.base holds a kernel VA */
379 nmi_needswapgs:
380         incl    %ebx
381         swapgs
382 /* Note: this label is also used by ddb and gdb: */
383 nmi_calltrap:
384         FAKE_MCOUNT(TF_RIP(%rsp))
385         movq    %rsp, %rdi
386         call    trap
387         MEXITCOUNT
388         testl   %ebx,%ebx
389         jz      nmi_restoreregs
390         swapgs
391 nmi_restoreregs:
392         movq    TF_RDI(%rsp),%rdi
393         movq    TF_RSI(%rsp),%rsi
394         movq    TF_RDX(%rsp),%rdx
395         movq    TF_RCX(%rsp),%rcx
396         movq    TF_R8(%rsp),%r8
397         movq    TF_R9(%rsp),%r9
398         movq    TF_RAX(%rsp),%rax
399         movq    TF_RBX(%rsp),%rbx
400         movq    TF_RBP(%rsp),%rbp
401         movq    TF_R10(%rsp),%r10
402         movq    TF_R11(%rsp),%r11
403         movq    TF_R12(%rsp),%r12
404         movq    TF_R13(%rsp),%r13
405         movq    TF_R14(%rsp),%r14
406         movq    TF_R15(%rsp),%r15
407         addq    $TF_RIP,%rsp
408         iretq
409
410 /*
411  * This function is what cpu_heavy_restore jumps to after a new process
412  * is created.  The LWKT subsystem switches while holding a critical
413  * section and we maintain that abstraction here (e.g. because 
414  * cpu_heavy_restore needs it due to PCB_*() manipulation), then get out of
415  * it before calling the initial function (typically fork_return()) and/or
416  * returning to user mode.
417  *
418  * The MP lock is held on entry, but for processes fork_return(esi)
419  * releases it.  'doreti' always runs without the MP lock.
420  */
421 ENTRY(fork_trampoline)
422         movq    PCPU(curthread),%rax
423         subl    $TDPRI_CRIT,TD_PRI(%rax)
424
425         /*
426          * cpu_set_fork_handler intercepts this function call to
427          * have this call a non-return function to stay in kernel mode.
428          *
429          * initproc has its own fork handler, start_init(), which DOES
430          * return.
431          *
432          * %rbx - chaining function (typically fork_return)
433          * %r12 -> %rdi (argument)
434          * frame-> %rsi (trap frame)
435          *
436          *   void (func:rbx)(arg:rdi, trapframe:rsi)
437          */
438         movq    %rsp, %rsi              /* pass trapframe by reference */
439         movq    %r12, %rdi              /* arg1 */
440         call    *%rbx                   /* function */
441
442         /* cut from syscall */
443
444         sti
445         call    splz
446
447 #if defined(INVARIANTS) && defined(SMP)
448         movq    PCPU(curthread),%rax
449         cmpl    $0,TD_MPCOUNT(%rax)
450         je      1f
451         movq    $pmsg4, %rdi
452         movl    TD_MPCOUNT(%rax), %rsi
453         movq    %rbx, %rdx
454         xorl    %eax, %eax
455         call    panic
456 pmsg4:  .asciz  "fork_trampoline mpcount %d after calling %p"
457         /* JG what's the purpose of this alignment and is it enough on amd64? */
458         .p2align 2
459 1:
460 #endif
461         /*
462          * Return via doreti to handle ASTs.
463          *
464          * trapframe is at the top of the stack.
465          */
466 #if JG
467         pushl   $0                      /* cpl to restore */
468 #endif
469         MEXITCOUNT
470         jmp     doreti
471
472 /*
473  * To efficiently implement classification of trap and interrupt handlers
474  * for profiling, there must be only trap handlers between the labels btrap
475  * and bintr, and only interrupt handlers between the labels bintr and
476  * eintr.  This is implemented (partly) by including files that contain
477  * some of the handlers.  Before including the files, set up a normal asm
478  * environment so that the included files doen't need to know that they are
479  * included.
480  */
481
482 #ifdef COMPAT_IA32
483         .data
484         .p2align 4
485         .text
486         SUPERALIGN_TEXT
487
488 #include <amd64/ia32/ia32_exception.S>
489 #endif
490
491         .data
492         .p2align 4
493         .text
494         SUPERALIGN_TEXT
495 MCOUNT_LABEL(bintr)
496
497 #if JG
498 #include <amd64/amd64/apic_vector.S>
499 #endif
500
501 #ifdef DEV_ATPIC
502         .data
503         .p2align 4
504         .text
505         SUPERALIGN_TEXT
506
507 #include <amd64/isa/atpic_vector.S>
508 #endif
509
510         .text
511 MCOUNT_LABEL(eintr)
512