2 * Copyright (c) 1989, 1990 William F. Jolitz.
3 * Copyright (c) 1990 The Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "opt_kdtrace.h"
37 #include <machine/asmacros.h>
38 #include <machine/psl.h>
39 #include <machine/trap.h>
43 #define SEL_RPL_MASK 0x0003
44 #define GSEL_KPL 0x0020 /* GSEL(GCODE_SEL, SEL_KPL) */
49 .globl dtrace_invop_jump_addr
51 .type dtrace_invop_jump_addr, @object
52 .size dtrace_invop_jump_addr, 4
53 dtrace_invop_jump_addr:
55 .globl dtrace_invop_calltrap_addr
57 .type dtrace_invop_calltrap_addr, @object
58 .size dtrace_invop_calltrap_addr, 4
59 dtrace_invop_calltrap_addr:
64 /*****************************************************************************/
66 /*****************************************************************************/
68 * Trap and fault vector routines.
70 * Most traps are 'trap gates', SDT_SYS386TGT. A trap gate pushes state on
71 * the stack that mostly looks like an interrupt, but does not disable
72 * interrupts. A few of the traps we are use are interrupt gates,
73 * SDT_SYS386IGT, which are nearly the same thing except interrupts are
76 * The cpu will push a certain amount of state onto the kernel stack for
77 * the current process. The amount of state depends on the type of trap
78 * and whether the trap crossed rings or not. See i386/include/frame.h.
79 * At the very least the current EFLAGS (status register, which includes
80 * the interrupt disable state prior to the trap), the code segment register,
81 * and the return instruction pointer are pushed by the cpu. The cpu
82 * will also push an 'error' code for certain traps. We push a dummy
83 * error code for those traps where the cpu doesn't in order to maintain
84 * a consistent frame. We also push a contrived 'trap number'.
86 * The cpu does not push the general registers, we must do that, and we
87 * must restore them prior to calling 'iret'. The cpu adjusts the %cs and
88 * %ss segment registers, but does not mess with %ds, %es, or %fs. Thus we
89 * must load them with appropriate values for supervisor mode operation.
95 #define TRAP(a) pushl $(a) ; jmp alltraps
98 pushl $0; TRAP(T_DIVIDE)
100 pushl $0; TRAP(T_TRCTRAP)
102 pushl $0; TRAP(T_NMI)
104 pushl $0; TRAP(T_BPTFLT)
106 pushl $0; TRAP(T_OFLOW)
108 pushl $0; TRAP(T_BOUND)
109 #ifndef KDTRACE_HOOKS
111 pushl $0; TRAP(T_PRIVINFLT)
114 pushl $0; TRAP(T_DNA)
116 pushl $0; TRAP(T_FPOPFLT)
128 pushl $0; TRAP(T_MCHK)
130 pushl $0; TRAP(T_RESERVED)
132 pushl $0; TRAP(T_ARITHTRAP)
136 pushl $0; TRAP(T_XMMFLT)
139 * alltraps entry point. Interrupts are enabled if this was a trap
140 * gate (TGT), else disabled if this was an interrupt gate (IGT).
141 * Note that int0x80_syscall is a trap gate. Interrupt gates are
142 * used by page faults, non-maskable interrupts, debug and breakpoint
148 .type alltraps,@function
154 alltraps_with_regs_pushed:
156 FAKE_MCOUNT(TF_EIP(%esp))
163 * Return via doreti to handle ASTs.
169 * Privileged instruction fault.
174 /* Check if there is no DTrace hook registered. */
175 cmpl $0,dtrace_invop_jump_addr
178 /* Check if this is a user fault. */
179 cmpl $GSEL_KPL, 4(%esp) /* Check the code segment. */
181 /* If so, just handle it as a normal trap. */
185 * This is a kernel instruction fault that might have been caused
186 * by a DTrace provider.
188 pushal /* Push all registers onto the stack. */
191 * Set our jump address for the jump back in the event that
192 * the exception wasn't caused by DTrace at all.
194 movl $norm_ill, dtrace_invop_calltrap_addr
196 /* Jump to the code hooked in by DTrace. */
197 jmpl *dtrace_invop_jump_addr
200 * Process the instruction fault in the normal way.
208 * SYSCALL CALL GATE (old entry point for a.out binaries)
210 * The intersegment call has been set up to specify one dummy parameter.
212 * This leaves a place to put eflags so that the call frame can be
213 * converted to a trap frame. Note that the eflags is (semi-)bogusly
214 * pushed into (what will be) tf_err and then copied later into the
215 * final spot. It has to be done this way because esp can't be just
216 * temporarily altered for the pushfl - an interrupt might come in
217 * and clobber the saved cs/eip.
220 IDTVEC(lcall_syscall)
221 pushfl /* save eflags */
222 popl 8(%esp) /* shuffle into tf_eflags */
223 pushl $7 /* sizeof "lcall 7,0" */
224 subl $4,%esp /* skip over tf_trapno */
230 FAKE_MCOUNT(TF_EIP(%esp))
238 * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
240 * Even though the name says 'int0x80', this is actually a TGT (trap gate)
241 * rather then an IGT (interrupt gate). Thus interrupts are enabled on
242 * entry just as they are for a normal syscall.
245 IDTVEC(int0x80_syscall)
246 pushl $2 /* sizeof "int 0x80" */
247 subl $4,%esp /* skip over tf_trapno */
253 FAKE_MCOUNT(TF_EIP(%esp))
260 ENTRY(fork_trampoline)
261 pushl %esp /* trapframe pointer */
262 pushl %ebx /* arg1 */
263 pushl %esi /* function */
266 /* cut from syscall */
269 * Return via doreti to handle ASTs.
276 * To efficiently implement classification of trap and interrupt handlers
277 * for profiling, there must be only trap handlers between the labels btrap
278 * and bintr, and only interrupt handlers between the labels bintr and
279 * eintr. This is implemented (partly) by including files that contain
280 * some of the handlers. Before including the files, set up a normal asm
281 * environment so that the included files doen't need to know that they are
291 #include <i386/isa/atpic_vector.s>
299 #include <i386/i386/apic_vector.s>
306 #include <i386/i386/vm86bios.s>
312 * void doreti(struct trapframe)
314 * Handle return from interrupts, traps and syscalls.
318 .type doreti,@function
320 FAKE_MCOUNT($bintr) /* init "from" bintr -> doreti */
323 * Check if ASTs can be handled now. PSL_VM must be checked first
324 * since segment registers only have an RPL in non-VM86 mode.
326 testl $PSL_VM,TF_EFLAGS(%esp) /* are we in vm86 mode? */
328 movl PCPU(CURPCB),%ecx
329 testl $PCB_VM86CALL,PCB_FLAGS(%ecx) /* are we in a vm86 call? */
330 jz doreti_ast /* can handle ASTS now if not */
334 testb $SEL_RPL_MASK,TF_CS(%esp) /* are we returning to user mode? */
335 jz doreti_exit /* can't handle ASTs now if not */
339 * Check for ASTs atomically with returning. Disabling CPU
340 * interrupts provides sufficient locking even in the SMP case,
341 * since we will be informed of any new ASTs by an IPI.
344 movl PCPU(CURTHREAD),%eax
345 testl $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%eax)
348 pushl %esp /* pass a pointer to the trapframe */
354 * doreti_exit: pop registers, iret.
356 * The segment register pop is a special case, since it may
357 * fault if (for example) a sigreturn specifies bad segment
358 * registers. The fault is handled in trap.c.
363 .globl doreti_popl_fs
366 .globl doreti_popl_es
369 .globl doreti_popl_ds
379 * doreti_iret_fault and friends. Alternative return code for
380 * the case where we get a fault in the doreti_exit code
381 * above. trap() (i386/i386/trap.c) catches this specific
382 * case, sends the process a signal and continues in the
383 * corresponding place in the code below.
386 .globl doreti_iret_fault
391 .globl doreti_popl_ds_fault
392 doreti_popl_ds_fault:
394 .globl doreti_popl_es_fault
395 doreti_popl_es_fault:
397 .globl doreti_popl_fs_fault
398 doreti_popl_fs_fault:
399 movl $0,TF_ERR(%esp) /* XXX should be the error code */
400 movl $T_PROTFLT,TF_TRAPNO(%esp)
401 jmp alltraps_with_regs_pushed