Initial import from FreeBSD RELENG_4:
[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  */
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         mov     $KDSEL,%ax
172         mov     %ax,%ds
173         mov     %ax,%es
174         MOVL_KPSEL_EAX
175         mov     %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 IDTVEC(xmm)
204         pushl $0; TRAP(T_XMMFLT)
205         
206         /*
207          * _alltraps entry point.  Interrupts are enabled if this was a trap
208          * gate (TGT), else disabled if this was an interrupt gate (IGT).
209          * Note that int0x80_syscall is a trap gate.  Only page faults
210          * use an interrupt gate.
211          *
212          * Note that all calls to MP_LOCK must occur with interrupts enabled
213          * in order to be able to take IPI's while waiting for the lock.
214          */
215
216         SUPERALIGN_TEXT
217         .globl  _alltraps
218         .type   _alltraps,@function
219 _alltraps:
220         pushal
221         pushl   %ds
222         pushl   %es
223         pushl   %fs
224 alltraps_with_regs_pushed:
225         mov     $KDSEL,%ax
226         mov     %ax,%ds
227         mov     %ax,%es
228         MOVL_KPSEL_EAX
229         mov     %ax,%fs
230         FAKE_MCOUNT(13*4(%esp))
231 calltrap:
232         FAKE_MCOUNT(_btrap)             /* init "from" _btrap -> calltrap */
233         MPLOCKED incl _cnt+V_TRAP
234         MP_LOCK
235         movl    _cpl,%ebx               /* keep orig. cpl here during trap() */
236         call    _trap
237
238         /*
239          * Return via _doreti to handle ASTs.  Have to change trap frame
240          * to interrupt frame.
241          */
242         pushl   %ebx                    /* cpl to restore */
243         subl    $4,%esp                 /* dummy unit to finish intr frame */
244         incb    _intr_nesting_level
245         MEXITCOUNT
246         jmp     _doreti
247
248 /*
249  * SYSCALL CALL GATE (old entry point for a.out binaries)
250  *
251  * The intersegment call has been set up to specify one dummy parameter.
252  *
253  * This leaves a place to put eflags so that the call frame can be
254  * converted to a trap frame. Note that the eflags is (semi-)bogusly
255  * pushed into (what will be) tf_err and then copied later into the
256  * final spot. It has to be done this way because esp can't be just
257  * temporarily altered for the pushfl - an interrupt might come in
258  * and clobber the saved cs/eip.
259  *
260  * We do not obtain the MP lock, but the call to syscall2 might.  If it
261  * does it will release the lock prior to returning.
262  */
263         SUPERALIGN_TEXT
264 IDTVEC(syscall)
265         pushfl                          /* save eflags in tf_err for now */
266         subl    $4,%esp                 /* skip over tf_trapno */
267         pushal
268         pushl   %ds
269         pushl   %es
270         pushl   %fs
271         mov     $KDSEL,%ax              /* switch to kernel segments */
272         mov     %ax,%ds
273         mov     %ax,%es
274         MOVL_KPSEL_EAX
275         mov     %ax,%fs
276         movl    TF_ERR(%esp),%eax       /* copy saved eflags to final spot */
277         movl    %eax,TF_EFLAGS(%esp)
278         movl    $7,TF_ERR(%esp)         /* sizeof "lcall 7,0" */
279         FAKE_MCOUNT(13*4(%esp))
280         MPLOCKED incl _cnt+V_SYSCALL
281         call    _syscall2
282         MEXITCOUNT
283         cli                             /* atomic astpending access */
284         cmpl    $0,_astpending
285         je      doreti_syscall_ret
286 #ifdef SMP
287         MP_LOCK
288 #endif
289         pushl   $0                      /* cpl to restore */
290         subl    $4,%esp                 /* dummy unit for interrupt frame */
291         movb    $1,_intr_nesting_level
292         jmp     _doreti
293
294 /*
295  * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
296  *
297  * Even though the name says 'int0x80', this is actually a TGT (trap gate)
298  * rather then an IGT (interrupt gate).  Thus interrupts are enabled on
299  * entry just as they are for a normal syscall.
300  *
301  * We do not obtain the MP lock, but the call to syscall2 might.  If it
302  * does it will release the lock prior to returning.
303  */
304         SUPERALIGN_TEXT
305 IDTVEC(int0x80_syscall)
306         subl    $8,%esp                 /* skip over tf_trapno and tf_err */
307         pushal
308         pushl   %ds
309         pushl   %es
310         pushl   %fs
311         mov     $KDSEL,%ax              /* switch to kernel segments */
312         mov     %ax,%ds
313         mov     %ax,%es
314         MOVL_KPSEL_EAX
315         mov     %ax,%fs
316         movl    $2,TF_ERR(%esp)         /* sizeof "int 0x80" */
317         FAKE_MCOUNT(13*4(%esp))
318         MPLOCKED incl _cnt+V_SYSCALL
319         call    _syscall2
320         MEXITCOUNT
321         cli                             /* atomic astpending access */
322         cmpl    $0,_astpending
323         je      doreti_syscall_ret
324 #ifdef SMP
325         MP_LOCK
326 #endif
327         pushl   $0                      /* cpl to restore */
328         subl    $4,%esp                 /* dummy unit for interrupt frame */
329         movb    $1,_intr_nesting_level
330         jmp     _doreti
331
332 ENTRY(fork_trampoline)
333         call    _spl0
334
335 #ifdef SMP
336         cmpl    $0,_switchtime
337         jne     1f
338         movl    $gd_switchtime,%eax
339         addl    %fs:0,%eax
340         pushl   %eax
341         call    _microuptime
342         popl    %edx
343         movl    _ticks,%eax
344         movl    %eax,_switchticks
345 1:
346 #endif
347
348         /*
349          * cpu_set_fork_handler intercepts this function call to
350          * have this call a non-return function to stay in kernel mode.
351          * initproc has its own fork handler, but it does return.
352          */
353         pushl   %ebx                    /* arg1 */
354         call    *%esi                   /* function */
355         addl    $4,%esp
356         /* cut from syscall */
357
358         /*
359          * Return via _doreti to handle ASTs.
360          */
361         pushl   $0                      /* cpl to restore */
362         subl    $4,%esp                 /* dummy unit to finish intr frame */
363         movb    $1,_intr_nesting_level
364         MEXITCOUNT
365         jmp     _doreti
366
367
368 /*
369  * Include vm86 call routines, which want to call _doreti.
370  */
371 #include "i386/i386/vm86bios.s"
372
373 /*
374  * Include what was once config+isa-dependent code.
375  * XXX it should be in a stand-alone file.  It's still icu-dependent and
376  * belongs in i386/isa.
377  */
378 #include "i386/isa/vector.s"
379
380 /*
381  * Include what was once icu-dependent code.
382  * XXX it should be merged into this file (also move the definition of
383  * imen to vector.s or isa.c).
384  * Before including it, set up a normal asm environment so that vector.s
385  * doesn't have to know that stuff is included after it.
386  */
387         .data
388         ALIGN_DATA
389         .text
390         SUPERALIGN_TEXT
391 #include "i386/isa/ipl.s"