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