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/i386/i386/Attic/exception.s,v 1.8 2003/06/30 19:50:30 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 jump to _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 #define IDTVEC(name) ALIGN_TEXT; .globl __CONCAT(_X,name); \
90 .type __CONCAT(_X,name),@function; __CONCAT(_X,name):
91 #define TRAP(a) pushl $(a) ; jmp _alltraps
94 #define BDBTRAP(name) \
96 cmpb $0,_bdb_exists ; \
98 testb $SEL_RPL_MASK,4(%esp) ; \
101 .globl __CONCAT(__CONCAT(bdb_,name),_ljmp); \
102 __CONCAT(__CONCAT(bdb_,name),_ljmp): \
106 #define BDBTRAP(name)
109 #define BPTTRAP(a) testl $PSL_I,4+8(%esp) ; je 1f ; sti ; 1: ; TRAP(a)
115 pushl $0; TRAP(T_DIVIDE)
118 pushl $0; BPTTRAP(T_TRCTRAP)
120 pushl $0; TRAP(T_NMI)
123 pushl $0; BPTTRAP(T_BPTFLT)
125 pushl $0; TRAP(T_OFLOW)
127 pushl $0; TRAP(T_BOUND)
129 pushl $0; TRAP(T_PRIVINFLT)
131 pushl $0; TRAP(T_DNA)
133 pushl $0; TRAP(T_FPOPFLT)
145 pushl $0; TRAP(T_MCHK)
147 pushl $0; TRAP(T_RESERVED)
152 * Handle like an interrupt (except for accounting) so that we can
153 * call npx_intr to clear the error. It would be better to handle
154 * npx interrupts as traps. Nested interrupts would probably have
155 * to be converted to ASTs.
157 pushl $0 /* dummy error code */
158 pushl $0 /* dummy trap type */
161 pushl %es /* now stack frame is a trap frame */
168 FAKE_MCOUNT(13*4(%esp))
170 movl _curthread,%ebx /* save original cpl */
171 movl TD_CPL(%ebx), %ebx
175 call _npx_intr /* note: call might mess w/ argument */
177 movl %ebx, (%esp) /* save cpl for doreti */
178 incb _intr_nesting_level
182 pushl $0; TRAP(T_ARITHTRAP)
183 #endif /* NNPX > 0 */
189 pushl $0; TRAP(T_XMMFLT)
192 * _alltraps entry point. Interrupts are enabled if this was a trap
193 * gate (TGT), else disabled if this was an interrupt gate (IGT).
194 * Note that int0x80_syscall is a trap gate. Only page faults
195 * use an interrupt gate.
197 * Note that all calls to MP_LOCK must occur with interrupts enabled
198 * in order to be able to take IPI's while waiting for the lock.
203 .type _alltraps,@function
209 alltraps_with_regs_pushed:
215 FAKE_MCOUNT(13*4(%esp))
217 FAKE_MCOUNT(_btrap) /* init "from" _btrap -> calltrap */
218 MPLOCKED incl _cnt+V_TRAP
220 movl _curthread,%eax /* keep orig cpl here during call */
221 movl TD_CPL(%eax),%ebx
225 * Return via _doreti to handle ASTs. Have to change trap frame
226 * to interrupt frame.
228 pushl %ebx /* cpl to restore */
229 incb _intr_nesting_level
234 * SYSCALL CALL GATE (old entry point for a.out binaries)
236 * The intersegment call has been set up to specify one dummy parameter.
238 * This leaves a place to put eflags so that the call frame can be
239 * converted to a trap frame. Note that the eflags is (semi-)bogusly
240 * pushed into (what will be) tf_err and then copied later into the
241 * final spot. It has to be done this way because esp can't be just
242 * temporarily altered for the pushfl - an interrupt might come in
243 * and clobber the saved cs/eip.
245 * We do not obtain the MP lock, but the call to syscall2 might. If it
246 * does it will release the lock prior to returning.
250 pushfl /* save eflags in tf_err for now */
251 subl $4,%esp /* skip over tf_trapno */
256 mov $KDSEL,%ax /* switch to kernel segments */
261 movl TF_ERR(%esp),%eax /* copy saved eflags to final spot */
262 movl %eax,TF_EFLAGS(%esp)
263 movl $7,TF_ERR(%esp) /* sizeof "lcall 7,0" */
264 FAKE_MCOUNT(13*4(%esp))
265 MPLOCKED incl _cnt+V_SYSCALL
268 cli /* atomic astpending access */
270 je doreti_syscall_ret
274 pushl $0 /* cpl to restore */
275 movl $1,_intr_nesting_level
279 * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
281 * Even though the name says 'int0x80', this is actually a TGT (trap gate)
282 * rather then an IGT (interrupt gate). Thus interrupts are enabled on
283 * entry just as they are for a normal syscall.
285 * We do not obtain the MP lock, but the call to syscall2 might. If it
286 * does it will release the lock prior to returning.
289 IDTVEC(int0x80_syscall)
290 subl $8,%esp /* skip over tf_trapno and tf_err */
295 mov $KDSEL,%ax /* switch to kernel segments */
300 movl $2,TF_ERR(%esp) /* sizeof "int 0x80" */
301 FAKE_MCOUNT(13*4(%esp))
302 MPLOCKED incl _cnt+V_SYSCALL
305 cli /* atomic astpending access */
307 je doreti_syscall_ret
311 pushl $0 /* cpl to restore */
312 movl $1,_intr_nesting_level
316 * This function is what cpu_heavy_restore jumps to after a new process
317 * is created. We are in a critical section in order to prevent
318 * cpu_heavy_restore from being interrupted (especially since it stores
319 * its context in a static place!), so the first thing we do is release
320 * the critical section.
322 ENTRY(fork_trampoline)
324 subl $TDPRI_CRIT,TD_PRI(%eax)
329 * cpu_set_fork_handler intercepts this function call to
330 * have this call a non-return function to stay in kernel mode.
331 * initproc has its own fork handler, but it does return.
333 pushl %ebx /* arg1 */
334 call *%esi /* function */
336 /* cut from syscall */
339 * Return via _doreti to handle ASTs.
341 pushl $0 /* cpl to restore */
342 movb $1,_intr_nesting_level
348 * Include vm86 call routines, which want to call _doreti.
350 #include "i386/i386/vm86bios.s"
353 * Include what was once config+isa-dependent code.
354 * XXX it should be in a stand-alone file. It's still icu-dependent and
355 * belongs in i386/isa.
357 #include "i386/isa/vector.s"
360 * Include what was once icu-dependent code.
361 * XXX it should be merged into this file (also move the definition of
362 * imen to vector.s or isa.c).
363 * Before including it, set up a normal asm environment so that vector.s
364 * doesn't have to know that stuff is included after it.
370 #include "i386/isa/ipl.s"