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.2 2003/06/17 04:28:35 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 */
51 #define MOVL_KPSEL_EAX movl $KPSEL,%eax
53 #define MOVL_KPSEL_EAX
55 #define SEL_RPL_MASK 0x0003
59 /*****************************************************************************/
61 /*****************************************************************************/
63 * Trap and fault vector routines.
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
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'.
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.
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.
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.
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
103 #define BDBTRAP(name) \
105 cmpb $0,_bdb_exists ; \
107 testb $SEL_RPL_MASK,4(%esp) ; \
110 .globl __CONCAT(__CONCAT(bdb_,name),_ljmp); \
111 __CONCAT(__CONCAT(bdb_,name),_ljmp): \
115 #define BDBTRAP(name)
118 #define BPTTRAP(a) testl $PSL_I,4+8(%esp) ; je 1f ; sti ; 1: ; TRAP(a)
124 pushl $0; TRAP(T_DIVIDE)
127 pushl $0; BPTTRAP(T_TRCTRAP)
129 pushl $0; TRAP(T_NMI)
132 pushl $0; BPTTRAP(T_BPTFLT)
134 pushl $0; TRAP(T_OFLOW)
136 pushl $0; TRAP(T_BOUND)
138 pushl $0; TRAP(T_PRIVINFLT)
140 pushl $0; TRAP(T_DNA)
142 pushl $0; TRAP(T_FPOPFLT)
154 pushl $0; TRAP(T_MCHK)
156 pushl $0; TRAP(T_RESERVED)
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.
166 pushl $0 /* dummy error code */
167 pushl $0 /* dummy trap type */
170 pushl %es /* now stack frame is a trap frame */
177 FAKE_MCOUNT(13*4(%esp))
180 MPLOCKED incl _cnt+V_TRAP
183 pushl %eax /* save original cpl */
184 pushl $0 /* dummy unit to finish intr frame */
188 pushl $0 /* dummy unit to finish intr frame */
194 incb _intr_nesting_level
198 pushl $0; TRAP(T_ARITHTRAP)
199 #endif /* NNPX > 0 */
205 pushl $0; TRAP(T_XMMFLT)
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.
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.
219 .type _alltraps,@function
225 alltraps_with_regs_pushed:
231 FAKE_MCOUNT(13*4(%esp))
233 FAKE_MCOUNT(_btrap) /* init "from" _btrap -> calltrap */
234 MPLOCKED incl _cnt+V_TRAP
236 movl _cpl,%ebx /* keep orig. cpl here during trap() */
240 * Return via _doreti to handle ASTs. Have to change trap frame
241 * to interrupt frame.
243 pushl %ebx /* cpl to restore */
244 subl $4,%esp /* dummy unit to finish intr frame */
245 incb _intr_nesting_level
250 * SYSCALL CALL GATE (old entry point for a.out binaries)
252 * The intersegment call has been set up to specify one dummy parameter.
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.
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.
266 pushfl /* save eflags in tf_err for now */
267 subl $4,%esp /* skip over tf_trapno */
272 mov $KDSEL,%ax /* switch to kernel segments */
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
284 cli /* atomic astpending access */
286 je doreti_syscall_ret
290 pushl $0 /* cpl to restore */
291 subl $4,%esp /* dummy unit for interrupt frame */
292 movb $1,_intr_nesting_level
296 * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
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.
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.
306 IDTVEC(int0x80_syscall)
307 subl $8,%esp /* skip over tf_trapno and tf_err */
312 mov $KDSEL,%ax /* switch to kernel segments */
317 movl $2,TF_ERR(%esp) /* sizeof "int 0x80" */
318 FAKE_MCOUNT(13*4(%esp))
319 MPLOCKED incl _cnt+V_SYSCALL
322 cli /* atomic astpending access */
324 je doreti_syscall_ret
328 pushl $0 /* cpl to restore */
329 subl $4,%esp /* dummy unit for interrupt frame */
330 movb $1,_intr_nesting_level
333 ENTRY(fork_trampoline)
339 movl $gd_switchtime,%eax
345 movl %eax,_switchticks
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.
354 pushl %ebx /* arg1 */
355 call *%esi /* function */
357 /* cut from syscall */
360 * Return via _doreti to handle ASTs.
362 pushl $0 /* cpl to restore */
363 subl $4,%esp /* dummy unit to finish intr frame */
364 movb $1,_intr_nesting_level
370 * Include vm86 call routines, which want to call _doreti.
372 #include "i386/i386/vm86bios.s"
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.
379 #include "i386/isa/vector.s"
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.
392 #include "i386/isa/ipl.s"