2 * Copyright (c) 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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/platform/pc32/i386/exception.s,v 1.6 2003/06/28 02:09:47 dillon Exp $
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>
45 #include <machine/smptests.h> /** various SMP options */
50 #define SEL_RPL_MASK 0x0003
54 /*****************************************************************************/
56 /*****************************************************************************/
58 * Trap and fault vector routines.
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
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'.
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.
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.
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.
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
98 #define BDBTRAP(name) \
100 cmpb $0,_bdb_exists ; \
102 testb $SEL_RPL_MASK,4(%esp) ; \
105 .globl __CONCAT(__CONCAT(bdb_,name),_ljmp); \
106 __CONCAT(__CONCAT(bdb_,name),_ljmp): \
110 #define BDBTRAP(name)
113 #define BPTTRAP(a) testl $PSL_I,4+8(%esp) ; je 1f ; sti ; 1: ; TRAP(a)
119 pushl $0; TRAP(T_DIVIDE)
122 pushl $0; BPTTRAP(T_TRCTRAP)
124 pushl $0; TRAP(T_NMI)
127 pushl $0; BPTTRAP(T_BPTFLT)
129 pushl $0; TRAP(T_OFLOW)
131 pushl $0; TRAP(T_BOUND)
133 pushl $0; TRAP(T_PRIVINFLT)
135 pushl $0; TRAP(T_DNA)
137 pushl $0; TRAP(T_FPOPFLT)
149 pushl $0; TRAP(T_MCHK)
151 pushl $0; TRAP(T_RESERVED)
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.
161 pushl $0 /* dummy error code */
162 pushl $0 /* dummy trap type */
165 pushl %es /* now stack frame is a trap frame */
172 FAKE_MCOUNT(13*4(%esp))
175 MPLOCKED incl _cnt+V_TRAP
177 movl _curthread,%eax /* save original cpl */
178 pushl TD_MACH+MTD_CPL(%eax)
179 pushl $0 /* dummy unit to finish intr frame */
181 movl _curthread,%eax /* save original cpl */
182 pushl TD_MACH+MTD_CPL(%eax)
183 pushl $0 /* dummy unit to finish intr frame */
189 incb _intr_nesting_level
193 pushl $0; TRAP(T_ARITHTRAP)
194 #endif /* NNPX > 0 */
200 pushl $0; TRAP(T_XMMFLT)
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.
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.
214 .type _alltraps,@function
220 alltraps_with_regs_pushed:
226 FAKE_MCOUNT(13*4(%esp))
228 FAKE_MCOUNT(_btrap) /* init "from" _btrap -> calltrap */
229 MPLOCKED incl _cnt+V_TRAP
231 movl _curthread,%eax /* keep orig cpl here during call */
232 movl TD_MACH+MTD_CPL(%eax),%ebx
236 * Return via _doreti to handle ASTs. Have to change trap frame
237 * to interrupt frame.
239 pushl %ebx /* cpl to restore */
240 subl $4,%esp /* dummy unit to finish intr frame */
241 incb _intr_nesting_level
246 * SYSCALL CALL GATE (old entry point for a.out binaries)
248 * The intersegment call has been set up to specify one dummy parameter.
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.
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.
262 pushfl /* save eflags in tf_err for now */
263 subl $4,%esp /* skip over tf_trapno */
268 mov $KDSEL,%ax /* switch to kernel segments */
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
280 cli /* atomic astpending access */
282 je doreti_syscall_ret
286 pushl $0 /* cpl to restore */
287 subl $4,%esp /* dummy unit for interrupt frame */
288 movb $1,_intr_nesting_level
292 * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
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.
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.
302 IDTVEC(int0x80_syscall)
303 subl $8,%esp /* skip over tf_trapno and tf_err */
308 mov $KDSEL,%ax /* switch to kernel segments */
313 movl $2,TF_ERR(%esp) /* sizeof "int 0x80" */
314 FAKE_MCOUNT(13*4(%esp))
315 MPLOCKED incl _cnt+V_SYSCALL
318 cli /* atomic astpending access */
320 je doreti_syscall_ret
324 pushl $0 /* cpl to restore */
325 subl $4,%esp /* dummy unit for interrupt frame */
326 movb $1,_intr_nesting_level
329 ENTRY(fork_trampoline)
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 */
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.
342 pushl %ebx /* arg1 */
343 call *%esi /* function */
345 /* cut from syscall */
348 * Return via _doreti to handle ASTs.
350 pushl $0 /* cpl to restore */
351 subl $4,%esp /* dummy unit to finish intr frame */
352 movb $1,_intr_nesting_level
358 * Include vm86 call routines, which want to call _doreti.
360 #include "i386/i386/vm86bios.s"
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.
367 #include "i386/isa/vector.s"
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.
380 #include "i386/isa/ipl.s"