smp/up collapse stage 1 of 2: Make UP use the globaldata structure the same
[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.6 2003/06/28 02:09:47 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 #define SEL_RPL_MASK    0x0003
51
52         .text
53
54 /*****************************************************************************/
55 /* Trap handling                                                             */
56 /*****************************************************************************/
57 /*
58  * Trap and fault vector routines.
59  *
60  * Most traps are 'trap gates', SDT_SYS386TGT.  A trap gate pushes state on
61  * the stack that mostly looks like an interrupt, but does not disable 
62  * interrupts.  A few of the traps we are use are interrupt gates, 
63  * SDT_SYS386IGT, which are nearly the same thing except interrupts are
64  * disabled on entry.
65  *
66  * The cpu will push a certain amount of state onto the kernel stack for
67  * the current process.  The amount of state depends on the type of trap 
68  * and whether the trap crossed rings or not.  See i386/include/frame.h.  
69  * At the very least the current EFLAGS (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  * On entry to a trap or interrupt WE DO NOT OWN THE MP LOCK.  This means
82  * that we must be careful in regards to accessing global variables.  We
83  * save (push) the current cpl (our software interrupt disable mask), call
84  * the trap function, then call _doreti to restore the cpl and deal with
85  * ASTs (software interrupts).  _doreti will determine if the restoration
86  * of the cpl unmasked any pending interrupts and will issue those interrupts
87  * synchronously prior to doing the iret.
88  *
89  * At the moment we must own the MP lock to do any cpl manipulation, which
90  * means we must own it prior to  calling _doreti.  The syscall case attempts
91  * to avoid this by handling a reduced set of cases itself and iret'ing.
92  */
93 #define IDTVEC(name)    ALIGN_TEXT; .globl __CONCAT(_X,name); \
94                         .type __CONCAT(_X,name),@function; __CONCAT(_X,name):
95 #define TRAP(a)         pushl $(a) ; jmp _alltraps
96
97 #ifdef BDE_DEBUGGER
98 #define BDBTRAP(name) \
99         ss ; \
100         cmpb    $0,_bdb_exists ; \
101         je      1f ; \
102         testb   $SEL_RPL_MASK,4(%esp) ; \
103         jne     1f ; \
104         ss ; \
105         .globl  __CONCAT(__CONCAT(bdb_,name),_ljmp); \
106 __CONCAT(__CONCAT(bdb_,name),_ljmp): \
107         ljmp    $0,$0 ; \
108 1:
109 #else
110 #define BDBTRAP(name)
111 #endif
112
113 #define BPTTRAP(a)      testl $PSL_I,4+8(%esp) ; je 1f ; sti ; 1: ; TRAP(a)
114
115 MCOUNT_LABEL(user)
116 MCOUNT_LABEL(btrap)
117
118 IDTVEC(div)
119         pushl $0; TRAP(T_DIVIDE)
120 IDTVEC(dbg)
121         BDBTRAP(dbg)
122         pushl $0; BPTTRAP(T_TRCTRAP)
123 IDTVEC(nmi)
124         pushl $0; TRAP(T_NMI)
125 IDTVEC(bpt)
126         BDBTRAP(bpt)
127         pushl $0; BPTTRAP(T_BPTFLT)
128 IDTVEC(ofl)
129         pushl $0; TRAP(T_OFLOW)
130 IDTVEC(bnd)
131         pushl $0; TRAP(T_BOUND)
132 IDTVEC(ill)
133         pushl $0; TRAP(T_PRIVINFLT)
134 IDTVEC(dna)
135         pushl $0; TRAP(T_DNA)
136 IDTVEC(fpusegm)
137         pushl $0; TRAP(T_FPOPFLT)
138 IDTVEC(tss)
139         TRAP(T_TSSFLT)
140 IDTVEC(missing)
141         TRAP(T_SEGNPFLT)
142 IDTVEC(stk)
143         TRAP(T_STKFLT)
144 IDTVEC(prot)
145         TRAP(T_PROTFLT)
146 IDTVEC(page)
147         TRAP(T_PAGEFLT)
148 IDTVEC(mchk)
149         pushl $0; TRAP(T_MCHK)
150 IDTVEC(rsvd)
151         pushl $0; TRAP(T_RESERVED)
152
153 IDTVEC(fpu)
154 #if NNPX > 0
155         /*
156          * Handle like an interrupt (except for accounting) so that we can
157          * call npx_intr to clear the error.  It would be better to handle
158          * npx interrupts as traps.  Nested interrupts would probably have
159          * to be converted to ASTs.
160          */
161         pushl   $0                      /* dummy error code */
162         pushl   $0                      /* dummy trap type */
163         pushal
164         pushl   %ds
165         pushl   %es                     /* now stack frame is a trap frame */
166         pushl   %fs
167         mov     $KDSEL,%ax
168         mov     %ax,%ds
169         mov     %ax,%es
170         movl    $KPSEL,%eax
171         mov     %ax,%fs
172         FAKE_MCOUNT(13*4(%esp))
173
174 #ifdef SMP
175         MPLOCKED incl _cnt+V_TRAP
176         MP_LOCK
177         movl    _curthread,%eax         /* save original cpl */
178         pushl   TD_MACH+MTD_CPL(%eax)
179         pushl   $0                      /* dummy unit to finish intr frame */
180 #else /* SMP */
181         movl    _curthread,%eax         /* save original cpl */
182         pushl   TD_MACH+MTD_CPL(%eax)
183         pushl   $0                      /* dummy unit to finish intr frame */
184         incl    _cnt+V_TRAP
185 #endif /* SMP */
186
187         call    _npx_intr
188
189         incb    _intr_nesting_level
190         MEXITCOUNT
191         jmp     _doreti
192 #else   /* NNPX > 0 */
193         pushl $0; TRAP(T_ARITHTRAP)
194 #endif  /* NNPX > 0 */
195
196 IDTVEC(align)
197         TRAP(T_ALIGNFLT)
198
199 IDTVEC(xmm)
200         pushl $0; TRAP(T_XMMFLT)
201         
202         /*
203          * _alltraps entry point.  Interrupts are enabled if this was a trap
204          * gate (TGT), else disabled if this was an interrupt gate (IGT).
205          * Note that int0x80_syscall is a trap gate.  Only page faults
206          * use an interrupt gate.
207          *
208          * Note that all calls to MP_LOCK must occur with interrupts enabled
209          * in order to be able to take IPI's while waiting for the lock.
210          */
211
212         SUPERALIGN_TEXT
213         .globl  _alltraps
214         .type   _alltraps,@function
215 _alltraps:
216         pushal
217         pushl   %ds
218         pushl   %es
219         pushl   %fs
220 alltraps_with_regs_pushed:
221         mov     $KDSEL,%ax
222         mov     %ax,%ds
223         mov     %ax,%es
224         movl    $KPSEL,%eax
225         mov     %ax,%fs
226         FAKE_MCOUNT(13*4(%esp))
227 calltrap:
228         FAKE_MCOUNT(_btrap)             /* init "from" _btrap -> calltrap */
229         MPLOCKED incl _cnt+V_TRAP
230         MP_LOCK
231         movl    _curthread,%eax         /* keep orig cpl here during call */
232         movl    TD_MACH+MTD_CPL(%eax),%ebx
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         mov     $KDSEL,%ax              /* switch to kernel segments */
269         mov     %ax,%ds
270         mov     %ax,%es
271         movl    $KPSEL,%eax
272         mov     %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         mov     $KDSEL,%ax              /* switch to kernel segments */
309         mov     %ax,%ds
310         mov     %ax,%es
311         movl    $KPSEL,%eax
312         mov     %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         movl    _curthread,%eax         /* YYY heavy weight process must */
333         pushl   TD_PROC(%eax)           /* YYY remove itself from runq because */
334         call    remrunqueue             /* LWKT restore func doesn't do that */
335         addl    $4,%esp
336
337         /*
338          * cpu_set_fork_handler intercepts this function call to
339          * have this call a non-return function to stay in kernel mode.
340          * initproc has its own fork handler, but it does return.
341          */
342         pushl   %ebx                    /* arg1 */
343         call    *%esi                   /* function */
344         addl    $4,%esp
345         /* cut from syscall */
346
347         /*
348          * Return via _doreti to handle ASTs.
349          */
350         pushl   $0                      /* cpl to restore */
351         subl    $4,%esp                 /* dummy unit to finish intr frame */
352         movb    $1,_intr_nesting_level
353         MEXITCOUNT
354         jmp     _doreti
355
356
357 /*
358  * Include vm86 call routines, which want to call _doreti.
359  */
360 #include "i386/i386/vm86bios.s"
361
362 /*
363  * Include what was once config+isa-dependent code.
364  * XXX it should be in a stand-alone file.  It's still icu-dependent and
365  * belongs in i386/isa.
366  */
367 #include "i386/isa/vector.s"
368
369 /*
370  * Include what was once icu-dependent code.
371  * XXX it should be merged into this file (also move the definition of
372  * imen to vector.s or isa.c).
373  * Before including it, set up a normal asm environment so that vector.s
374  * doesn't have to know that stuff is included after it.
375  */
376         .data
377         ALIGN_DATA
378         .text
379         SUPERALIGN_TEXT
380 #include "i386/isa/ipl.s"