2 * Copyright (c) 1989, 1990 William F. Jolitz.
3 * Copyright (c) 1990 The Regents of the University of California.
4 * Copyright (c) 2007 The FreeBSD Foundation
7 * Portions of this software were developed by A. Joseph Koshy under
8 * sponsorship from the FreeBSD Foundation and Google, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include "opt_hwpmc_hooks.h"
39 #include "opt_kdtrace.h"
42 #include <machine/asmacros.h>
43 #include <machine/psl.h>
44 #include <machine/trap.h>
48 #define SEL_RPL_MASK 0x0003
49 #define GSEL_KPL 0x0020 /* GSEL(GCODE_SEL, SEL_KPL) */
53 .globl dtrace_invop_jump_addr
55 .type dtrace_invop_jump_addr, @object
56 .size dtrace_invop_jump_addr, 4
57 dtrace_invop_jump_addr:
59 .globl dtrace_invop_calltrap_addr
61 .type dtrace_invop_calltrap_addr, @object
62 .size dtrace_invop_calltrap_addr, 4
63 dtrace_invop_calltrap_addr:
68 ENTRY(start_exceptions)
70 /*****************************************************************************/
72 /*****************************************************************************/
74 * Trap and fault vector routines.
76 * Most traps are 'trap gates', SDT_SYS386TGT. A trap gate pushes state on
77 * the stack that mostly looks like an interrupt, but does not disable
78 * interrupts. A few of the traps we are use are interrupt gates,
79 * SDT_SYS386IGT, which are nearly the same thing except interrupts are
82 * The cpu will push a certain amount of state onto the kernel stack for
83 * the current process. The amount of state depends on the type of trap
84 * and whether the trap crossed rings or not. See i386/include/frame.h.
85 * At the very least the current EFLAGS (status register, which includes
86 * the interrupt disable state prior to the trap), the code segment register,
87 * and the return instruction pointer are pushed by the cpu. The cpu
88 * will also push an 'error' code for certain traps. We push a dummy
89 * error code for those traps where the cpu doesn't in order to maintain
90 * a consistent frame. We also push a contrived 'trap number'.
92 * The cpu does not push the general registers, we must do that, and we
93 * must restore them prior to calling 'iret'. The cpu adjusts the %cs and
94 * %ss segment registers, but does not mess with %ds, %es, or %fs. Thus we
95 * must load them with appropriate values for supervisor mode operation.
101 #define TRAP(a) pushl $(a) ; jmp alltraps
104 pushl $0; TRAP(T_DIVIDE)
106 pushl $0; TRAP(T_TRCTRAP)
108 pushl $0; TRAP(T_NMI)
110 pushl $0; TRAP(T_BPTFLT)
112 pushl $0; TRAP(T_OFLOW)
114 pushl $0; TRAP(T_BOUND)
115 #ifndef KDTRACE_HOOKS
117 pushl $0; TRAP(T_PRIVINFLT)
120 pushl $0; TRAP(T_DNA)
122 pushl $0; TRAP(T_FPOPFLT)
134 pushl $0; TRAP(T_MCHK)
136 pushl $0; TRAP(T_RESERVED)
138 pushl $0; TRAP(T_ARITHTRAP)
142 pushl $0; TRAP(T_XMMFLT)
145 * alltraps entry point. Interrupts are enabled if this was a trap
146 * gate (TGT), else disabled if this was an interrupt gate (IGT).
147 * Note that int0x80_syscall is a trap gate. Interrupt gates are
148 * used by page faults, non-maskable interrupts, debug and breakpoint
154 .type alltraps,@function
160 alltraps_with_regs_pushed:
163 FAKE_MCOUNT(TF_EIP(%esp))
170 * Return via doreti to handle ASTs.
176 * Privileged instruction fault.
181 /* Check if there is no DTrace hook registered. */
182 cmpl $0,dtrace_invop_jump_addr
185 /* Check if this is a user fault. */
186 cmpl $GSEL_KPL, 4(%esp) /* Check the code segment. */
188 /* If so, just handle it as a normal trap. */
192 * This is a kernel instruction fault that might have been caused
193 * by a DTrace provider.
195 pushal /* Push all registers onto the stack. */
198 * Set our jump address for the jump back in the event that
199 * the exception wasn't caused by DTrace at all.
201 movl $norm_ill, dtrace_invop_calltrap_addr
203 /* Jump to the code hooked in by DTrace. */
204 jmpl *dtrace_invop_jump_addr
207 * Process the instruction fault in the normal way.
215 * SYSCALL CALL GATE (old entry point for a.out binaries)
217 * The intersegment call has been set up to specify one dummy parameter.
219 * This leaves a place to put eflags so that the call frame can be
220 * converted to a trap frame. Note that the eflags is (semi-)bogusly
221 * pushed into (what will be) tf_err and then copied later into the
222 * final spot. It has to be done this way because esp can't be just
223 * temporarily altered for the pushfl - an interrupt might come in
224 * and clobber the saved cs/eip.
227 IDTVEC(lcall_syscall)
228 pushfl /* save eflags */
229 popl 8(%esp) /* shuffle into tf_eflags */
230 pushl $7 /* sizeof "lcall 7,0" */
231 subl $4,%esp /* skip over tf_trapno */
238 FAKE_MCOUNT(TF_EIP(%esp))
246 * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
248 * Even though the name says 'int0x80', this is actually a TGT (trap gate)
249 * rather then an IGT (interrupt gate). Thus interrupts are enabled on
250 * entry just as they are for a normal syscall.
253 IDTVEC(int0x80_syscall)
254 pushl $2 /* sizeof "int 0x80" */
255 subl $4,%esp /* skip over tf_trapno */
262 FAKE_MCOUNT(TF_EIP(%esp))
269 ENTRY(fork_trampoline)
270 pushl %esp /* trapframe pointer */
271 pushl %ebx /* arg1 */
272 pushl %esi /* function */
275 /* cut from syscall */
278 * Return via doreti to handle ASTs.
285 * To efficiently implement classification of trap and interrupt handlers
286 * for profiling, there must be only trap handlers between the labels btrap
287 * and bintr, and only interrupt handlers between the labels bintr and
288 * eintr. This is implemented (partly) by including files that contain
289 * some of the handlers. Before including the files, set up a normal asm
290 * environment so that the included files doen't need to know that they are
300 #include <i386/i386/atpic_vector.s>
308 #include <i386/i386/apic_vector.s>
315 #include <i386/i386/vm86bios.s>
321 * void doreti(struct trapframe)
323 * Handle return from interrupts, traps and syscalls.
327 .type doreti,@function
329 FAKE_MCOUNT($bintr) /* init "from" bintr -> doreti */
332 * Check if ASTs can be handled now. ASTs cannot be safely
333 * processed when returning from an NMI.
335 cmpb $T_NMI,TF_TRAPNO(%esp)
342 * PSL_VM must be checked first since segment registers only
343 * have an RPL in non-VM86 mode.
345 testl $PSL_VM,TF_EFLAGS(%esp) /* are we in vm86 mode? */
347 movl PCPU(CURPCB),%ecx
348 testl $PCB_VM86CALL,PCB_FLAGS(%ecx) /* are we in a vm86 call? */
349 jz doreti_ast /* can handle ASTS now if not */
353 testb $SEL_RPL_MASK,TF_CS(%esp) /* are we returning to user mode? */
354 jz doreti_exit /* can't handle ASTs now if not */
358 * Check for ASTs atomically with returning. Disabling CPU
359 * interrupts provides sufficient locking even in the SMP case,
360 * since we will be informed of any new ASTs by an IPI.
363 movl PCPU(CURTHREAD),%eax
364 testl $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%eax)
367 pushl %esp /* pass a pointer to the trapframe */
373 * doreti_exit: pop registers, iret.
375 * The segment register pop is a special case, since it may
376 * fault if (for example) a sigreturn specifies bad segment
377 * registers. The fault is handled in trap.c.
382 .globl doreti_popl_fs
385 .globl doreti_popl_es
388 .globl doreti_popl_ds
398 * doreti_iret_fault and friends. Alternative return code for
399 * the case where we get a fault in the doreti_exit code
400 * above. trap() (i386/i386/trap.c) catches this specific
401 * case, sends the process a signal and continues in the
402 * corresponding place in the code below.
405 .globl doreti_iret_fault
410 .globl doreti_popl_ds_fault
411 doreti_popl_ds_fault:
413 .globl doreti_popl_es_fault
414 doreti_popl_es_fault:
416 .globl doreti_popl_fs_fault
417 doreti_popl_fs_fault:
418 movl $0,TF_ERR(%esp) /* XXX should be the error code */
419 movl $T_PROTFLT,TF_TRAPNO(%esp)
420 jmp alltraps_with_regs_pushed
424 * Since we are returning from an NMI, check if the current trap
425 * was from user mode and if so whether the current thread
426 * needs a user call chain capture.
428 testb $SEL_RPL_MASK,TF_CS(%esp)
430 movl PCPU(CURTHREAD),%eax /* curthread present? */
433 testl $TDP_CALLCHAIN,TD_PFLAGS(%eax) /* flagged for capture? */
436 * Take the processor out of NMI mode by executing a fake "iret".
444 * Call the callchain capture hook after turning interrupts back on.
449 pushl %esp /* frame pointer */
450 pushl $PMC_FN_USER_CALLCHAIN /* command */
451 movl PCPU(CURTHREAD),%eax
452 pushl %eax /* curthread */
457 ENTRY(end_exceptions)