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