]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/exception.s
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / exception.s
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35
36 #include "npx.h"
37
38 #include <machine/asmacros.h>
39 #include <machine/ipl.h>
40 #include <machine/lock.h>
41 #include <machine/psl.h>
42 #include <machine/trap.h>
43 #ifdef SMP
44 #include <machine/smptests.h>           /** various SMP options */
45 #endif
46
47 #include "assym.s"
48
49 #ifdef SMP
50 #define MOVL_KPSEL_EAX  movl    $KPSEL,%eax
51 #else
52 #define MOVL_KPSEL_EAX
53 #endif
54 #define SEL_RPL_MASK    0x0003
55
56         .text
57
58 /*****************************************************************************/
59 /* Trap handling                                                             */
60 /*****************************************************************************/
61 /*
62  * Trap and fault vector routines.
63  *
64  * Most traps are 'trap gates', SDT_SYS386TGT.  A trap gate pushes state on
65  * the stack that mostly looks like an interrupt, but does not disable 
66  * interrupts.  A few of the traps we are use are interrupt gates, 
67  * SDT_SYS386IGT, which are nearly the same thing except interrupts are
68  * disabled on entry.
69  *
70  * The cpu will push a certain amount of state onto the kernel stack for
71  * the current process.  The amount of state depends on the type of trap 
72  * and whether the trap crossed rings or not.  See i386/include/frame.h.  
73  * At the very least the current EFLAGS (status register, which includes 
74  * the interrupt disable state prior to the trap), the code segment register,
75  * and the return instruction pointer are pushed by the cpu.  The cpu 
76  * will also push an 'error' code for certain traps.  We push a dummy 
77  * error code for those traps where the cpu doesn't in order to maintain 
78  * a consistent frame.  We also push a contrived 'trap number'.
79  *
80  * The cpu does not push the general registers, we must do that, and we 
81  * must restore them prior to calling 'iret'.  The cpu adjusts the %cs and
82  * %ss segment registers, but does not mess with %ds, %es, or %fs.  Thus we
83  * must load them with appropriate values for supervisor mode operation.
84  *
85  * On entry to a trap or interrupt WE DO NOT OWN THE MP LOCK.  This means
86  * that we must be careful in regards to accessing global variables.  We
87  * save (push) the current cpl (our software interrupt disable mask), call
88  * the trap function, then call _doreti to restore the cpl and deal with
89  * ASTs (software interrupts).  _doreti will determine if the restoration
90  * of the cpl unmasked any pending interrupts and will issue those interrupts
91  * synchronously prior to doing the iret.
92  *
93  * At the moment we must own the MP lock to do any cpl manipulation, which
94  * means we must own it prior to  calling _doreti.  The syscall case attempts
95  * to avoid this by handling a reduced set of cases itself and iret'ing.
96  */
97 #define IDTVEC(name)    ALIGN_TEXT; .globl __CONCAT(_X,name); \
98                         .type __CONCAT(_X,name),@function; __CONCAT(_X,name):
99 #define TRAP(a)         pushl $(a) ; jmp _alltraps
100
101 #ifdef BDE_DEBUGGER
102 #define BDBTRAP(name) \
103         ss ; \
104         cmpb    $0,_bdb_exists ; \
105         je      1f ; \
106         testb   $SEL_RPL_MASK,4(%esp) ; \
107         jne     1f ; \
108         ss ; \
109         .globl  __CONCAT(__CONCAT(bdb_,name),_ljmp); \
110 __CONCAT(__CONCAT(bdb_,name),_ljmp): \
111         ljmp    $0,$0 ; \
112 1:
113 #else
114 #define BDBTRAP(name)
115 #endif
116
117 #define BPTTRAP(a)      testl $PSL_I,4+8(%esp) ; je 1f ; sti ; 1: ; TRAP(a)
118
119 MCOUNT_LABEL(user)
120 MCOUNT_LABEL(btrap)
121
122 IDTVEC(div)
123         pushl $0; TRAP(T_DIVIDE)
124 IDTVEC(dbg)
125         BDBTRAP(dbg)
126         pushl $0; BPTTRAP(T_TRCTRAP)
127 IDTVEC(nmi)
128         pushl $0; TRAP(T_NMI)
129 IDTVEC(bpt)
130         BDBTRAP(bpt)
131         pushl $0; BPTTRAP(T_BPTFLT)
132 IDTVEC(ofl)
133         pushl $0; TRAP(T_OFLOW)
134 IDTVEC(bnd)
135         pushl $0; TRAP(T_BOUND)
136 IDTVEC(ill)
137         pushl $0; TRAP(T_PRIVINFLT)
138 IDTVEC(dna)
139         pushl $0; TRAP(T_DNA)
140 IDTVEC(fpusegm)
141         pushl $0; TRAP(T_FPOPFLT)
142 IDTVEC(tss)
143         TRAP(T_TSSFLT)
144 IDTVEC(missing)
145         TRAP(T_SEGNPFLT)
146 IDTVEC(stk)
147         TRAP(T_STKFLT)
148 IDTVEC(prot)
149         TRAP(T_PROTFLT)
150 IDTVEC(page)
151         TRAP(T_PAGEFLT)
152 IDTVEC(mchk)
153         pushl $0; TRAP(T_MCHK)
154 IDTVEC(rsvd)
155         pushl $0; TRAP(T_RESERVED)
156
157 IDTVEC(fpu)
158 #if NNPX > 0
159         /*
160          * Handle like an interrupt (except for accounting) so that we can
161          * call npx_intr to clear the error.  It would be better to handle
162          * npx interrupts as traps.  Nested interrupts would probably have
163          * to be converted to ASTs.
164          */
165         pushl   $0                      /* dummy error code */
166         pushl   $0                      /* dummy trap type */
167         pushal
168         pushl   %ds
169         pushl   %es                     /* now stack frame is a trap frame */
170         pushl   %fs
171         movl    $KDSEL,%eax
172         movl    %ax,%ds
173         movl    %ax,%es
174         MOVL_KPSEL_EAX
175         movl    %ax,%fs
176         FAKE_MCOUNT(13*4(%esp))
177
178 #ifdef SMP
179         MPLOCKED incl _cnt+V_TRAP
180         MP_LOCK
181         movl    _cpl,%eax
182         pushl   %eax                    /* save original cpl */
183         pushl   $0                      /* dummy unit to finish intr frame */
184 #else /* SMP */
185         movl    _cpl,%eax
186         pushl   %eax
187         pushl   $0                      /* dummy unit to finish intr frame */
188         incl    _cnt+V_TRAP
189 #endif /* SMP */
190
191         call    _npx_intr
192
193         incb    _intr_nesting_level
194         MEXITCOUNT
195         jmp     _doreti
196 #else   /* NNPX > 0 */
197         pushl $0; TRAP(T_ARITHTRAP)
198 #endif  /* NNPX > 0 */
199
200 IDTVEC(align)
201         TRAP(T_ALIGNFLT)
202
203         /*
204          * _alltraps entry point.  Interrupts are enabled if this was a trap
205          * gate (TGT), else disabled if this was an interrupt gate (IGT).
206          * Note that int0x80_syscall is a trap gate.  Only page faults
207          * use an interrupt gate.
208          *
209          * Note that all calls to MP_LOCK must occur with interrupts enabled
210          * in order to be able to take IPI's while waiting for the lock.
211          */
212
213         SUPERALIGN_TEXT
214         .globl  _alltraps
215         .type   _alltraps,@function
216 _alltraps:
217         pushal
218         pushl   %ds
219         pushl   %es
220         pushl   %fs
221 alltraps_with_regs_pushed:
222         movl    $KDSEL,%eax
223         movl    %ax,%ds
224         movl    %ax,%es
225         MOVL_KPSEL_EAX
226         movl    %ax,%fs
227         FAKE_MCOUNT(13*4(%esp))
228 calltrap:
229         FAKE_MCOUNT(_btrap)             /* init "from" _btrap -> calltrap */
230         MPLOCKED incl _cnt+V_TRAP
231         MP_LOCK
232         movl    _cpl,%ebx               /* keep orig. cpl here during trap() */
233         call    _trap
234
235         /*
236          * Return via _doreti to handle ASTs.  Have to change trap frame
237          * to interrupt frame.
238          */
239         pushl   %ebx                    /* cpl to restore */
240         subl    $4,%esp                 /* dummy unit to finish intr frame */
241         incb    _intr_nesting_level
242         MEXITCOUNT
243         jmp     _doreti
244
245 /*
246  * SYSCALL CALL GATE (old entry point for a.out binaries)
247  *
248  * The intersegment call has been set up to specify one dummy parameter.
249  *
250  * This leaves a place to put eflags so that the call frame can be
251  * converted to a trap frame. Note that the eflags is (semi-)bogusly
252  * pushed into (what will be) tf_err and then copied later into the
253  * final spot. It has to be done this way because esp can't be just
254  * temporarily altered for the pushfl - an interrupt might come in
255  * and clobber the saved cs/eip.
256  *
257  * We do not obtain the MP lock, but the call to syscall2 might.  If it
258  * does it will release the lock prior to returning.
259  */
260         SUPERALIGN_TEXT
261 IDTVEC(syscall)
262         pushfl                          /* save eflags in tf_err for now */
263         subl    $4,%esp                 /* skip over tf_trapno */
264         pushal
265         pushl   %ds
266         pushl   %es
267         pushl   %fs
268         movl    $KDSEL,%eax             /* switch to kernel segments */
269         movl    %ax,%ds
270         movl    %ax,%es
271         MOVL_KPSEL_EAX
272         movl    %ax,%fs
273         movl    TF_ERR(%esp),%eax       /* copy saved eflags to final spot */
274         movl    %eax,TF_EFLAGS(%esp)
275         movl    $7,TF_ERR(%esp)         /* sizeof "lcall 7,0" */
276         FAKE_MCOUNT(13*4(%esp))
277         MPLOCKED incl _cnt+V_SYSCALL
278         call    _syscall2
279         MEXITCOUNT
280         cli                             /* atomic astpending access */
281         cmpl    $0,_astpending
282         je      doreti_syscall_ret
283 #ifdef SMP
284         MP_LOCK
285 #endif
286         pushl   $0                      /* cpl to restore */
287         subl    $4,%esp                 /* dummy unit for interrupt frame */
288         movb    $1,_intr_nesting_level
289         jmp     _doreti
290
291 /*
292  * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
293  *
294  * Even though the name says 'int0x80', this is actually a TGT (trap gate)
295  * rather then an IGT (interrupt gate).  Thus interrupts are enabled on
296  * entry just as they are for a normal syscall.
297  *
298  * We do not obtain the MP lock, but the call to syscall2 might.  If it
299  * does it will release the lock prior to returning.
300  */
301         SUPERALIGN_TEXT
302 IDTVEC(int0x80_syscall)
303         subl    $8,%esp                 /* skip over tf_trapno and tf_err */
304         pushal
305         pushl   %ds
306         pushl   %es
307         pushl   %fs
308         movl    $KDSEL,%eax             /* switch to kernel segments */
309         movl    %ax,%ds
310         movl    %ax,%es
311         MOVL_KPSEL_EAX
312         movl    %ax,%fs
313         movl    $2,TF_ERR(%esp)         /* sizeof "int 0x80" */
314         FAKE_MCOUNT(13*4(%esp))
315         MPLOCKED incl _cnt+V_SYSCALL
316         call    _syscall2
317         MEXITCOUNT
318         cli                             /* atomic astpending access */
319         cmpl    $0,_astpending
320         je      doreti_syscall_ret
321 #ifdef SMP
322         MP_LOCK
323 #endif
324         pushl   $0                      /* cpl to restore */
325         subl    $4,%esp                 /* dummy unit for interrupt frame */
326         movb    $1,_intr_nesting_level
327         jmp     _doreti
328
329 ENTRY(fork_trampoline)
330         call    _spl0
331
332 #ifdef SMP
333         cmpl    $0,_switchtime
334         jne     1f
335         movl    $gd_switchtime,%eax
336         addl    %fs:0,%eax
337         pushl   %eax
338         call    _microuptime
339         popl    %edx
340         movl    _ticks,%eax
341         movl    %eax,_switchticks
342 1:
343 #endif
344
345         /*
346          * cpu_set_fork_handler intercepts this function call to
347          * have this call a non-return function to stay in kernel mode.
348          * initproc has its own fork handler, but it does return.
349          */
350         pushl   %ebx                    /* arg1 */
351         call    %esi                    /* function */
352         addl    $4,%esp
353         /* cut from syscall */
354
355         /*
356          * Return via _doreti to handle ASTs.
357          */
358         pushl   $0                      /* cpl to restore */
359         subl    $4,%esp                 /* dummy unit to finish intr frame */
360         movb    $1,_intr_nesting_level
361         MEXITCOUNT
362         jmp     _doreti
363
364
365 /*
366  * Include vm86 call routines, which want to call _doreti.
367  */
368 #include "i386/i386/vm86bios.s"
369
370 /*
371  * Include what was once config+isa-dependent code.
372  * XXX it should be in a stand-alone file.  It's still icu-dependent and
373  * belongs in i386/isa.
374  */
375 #include "i386/isa/vector.s"
376
377 /*
378  * Include what was once icu-dependent code.
379  * XXX it should be merged into this file (also move the definition of
380  * imen to vector.s or isa.c).
381  * Before including it, set up a normal asm environment so that vector.s
382  * doesn't have to know that stuff is included after it.
383  */
384         .data
385         ALIGN_DATA
386         .text
387         SUPERALIGN_TEXT
388 #include "i386/isa/ipl.s"