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 * 3. 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_atpic.h"
39 #include "opt_hwpmc_hooks.h"
41 #include <machine/asmacros.h>
42 #include <machine/psl.h>
43 #include <machine/trap.h>
47 #define SEL_RPL_MASK 0x0003
48 #define GSEL_KPL 0x0020 /* GSEL(GCODE_SEL, SEL_KPL) */
52 .globl dtrace_invop_jump_addr
54 .type dtrace_invop_jump_addr, @object
55 .size dtrace_invop_jump_addr, 4
56 dtrace_invop_jump_addr:
58 .globl dtrace_invop_calltrap_addr
60 .type dtrace_invop_calltrap_addr, @object
61 .size dtrace_invop_calltrap_addr, 4
62 dtrace_invop_calltrap_addr:
67 ENTRY(start_exceptions)
69 /*****************************************************************************/
71 /*****************************************************************************/
73 * Trap and fault vector routines.
75 * Most traps are 'trap gates', SDT_SYS386TGT. A trap gate pushes state on
76 * the stack that mostly looks like an interrupt, but does not disable
77 * interrupts. A few of the traps we are use are interrupt gates,
78 * SDT_SYS386IGT, which are nearly the same thing except interrupts are
81 * The cpu will push a certain amount of state onto the kernel stack for
82 * the current process. The amount of state depends on the type of trap
83 * and whether the trap crossed rings or not. See i386/include/frame.h.
84 * At the very least the current EFLAGS (status register, which includes
85 * the interrupt disable state prior to the trap), the code segment register,
86 * and the return instruction pointer are pushed by the cpu. The cpu
87 * will also push an 'error' code for certain traps. We push a dummy
88 * error code for those traps where the cpu doesn't in order to maintain
89 * a consistent frame. We also push a contrived 'trap number'.
91 * The cpu does not push the general registers, we must do that, and we
92 * must restore them prior to calling 'iret'. The cpu adjusts the %cs and
93 * %ss segment registers, but does not mess with %ds, %es, or %fs. Thus we
94 * must load them with appropriate values for supervisor mode operation.
100 #define TRAP(a) pushl $(a) ; jmp alltraps
103 pushl $0; TRAP(T_DIVIDE)
105 pushl $0; TRAP(T_TRCTRAP)
107 pushl $0; TRAP(T_NMI)
109 pushl $0; TRAP(T_BPTFLT)
111 pushl $0; TRAP(T_DTRACE_RET)
113 pushl $0; TRAP(T_OFLOW)
115 pushl $0; TRAP(T_BOUND)
116 #ifndef KDTRACE_HOOKS
118 pushl $0; TRAP(T_PRIVINFLT)
121 pushl $0; TRAP(T_DNA)
123 pushl $0; TRAP(T_FPOPFLT)
135 pushl $0; TRAP(T_MCHK)
137 pushl $0; TRAP(T_RESERVED)
139 pushl $0; TRAP(T_ARITHTRAP)
143 pushl $0; TRAP(T_XMMFLT)
146 * All traps except ones for syscalls jump to alltraps. If
147 * interrupts were enabled when the trap occurred, then interrupts
148 * are enabled now if the trap was through a trap gate, else
149 * disabled if the trap was through an interrupt gate. Note that
150 * int0x80_syscall is a trap gate. Interrupt gates are used by
151 * page faults, non-maskable interrupts, debug and breakpoint
156 .type alltraps,@function
165 alltraps_with_regs_pushed:
168 FAKE_MCOUNT(TF_EIP(%esp))
175 * Return via doreti to handle ASTs.
181 * Privileged instruction fault.
186 /* Check if there is no DTrace hook registered. */
187 cmpl $0,dtrace_invop_jump_addr
190 /* Check if this is a user fault. */
191 cmpl $GSEL_KPL, 4(%esp) /* Check the code segment. */
193 /* If so, just handle it as a normal trap. */
197 * This is a kernel instruction fault that might have been caused
198 * by a DTrace provider.
200 pushal /* Push all registers onto the stack. */
203 * Set our jump address for the jump back in the event that
204 * the exception wasn't caused by DTrace at all.
206 movl $norm_ill, dtrace_invop_calltrap_addr
208 /* Jump to the code hooked in by DTrace. */
209 jmpl *dtrace_invop_jump_addr
212 * Process the instruction fault in the normal way.
220 * Call gate entry for syscalls (lcall 7,0).
221 * This is used by FreeBSD 1.x a.out executables and "old" NetBSD executables.
223 * The intersegment call has been set up to specify one dummy parameter.
224 * This leaves a place to put eflags so that the call frame can be
225 * converted to a trap frame. Note that the eflags is (semi-)bogusly
226 * pushed into (what will be) tf_err and then copied later into the
227 * final spot. It has to be done this way because esp can't be just
228 * temporarily altered for the pushfl - an interrupt might come in
229 * and clobber the saved cs/eip.
232 IDTVEC(lcall_syscall)
233 pushfl /* save eflags */
234 popl 8(%esp) /* shuffle into tf_eflags */
235 pushl $7 /* sizeof "lcall 7,0" */
236 pushl $0 /* tf_trapno */
246 FAKE_MCOUNT(TF_EIP(%esp))
254 * Trap gate entry for syscalls (int 0x80).
255 * This is used by FreeBSD ELF executables, "new" NetBSD executables, and all
258 * Even though the name says 'int0x80', this is actually a trap gate, not an
259 * interrupt gate. Thus interrupts are enabled on entry just as they are for
263 IDTVEC(int0x80_syscall)
264 pushl $2 /* sizeof "int 0x80" */
265 pushl $0 /* tf_trapno */
275 FAKE_MCOUNT(TF_EIP(%esp))
282 ENTRY(fork_trampoline)
283 pushl %esp /* trapframe pointer */
284 pushl %ebx /* arg1 */
285 pushl %esi /* function */
288 /* cut from syscall */
291 * Return via doreti to handle ASTs.
298 * To efficiently implement classification of trap and interrupt handlers
299 * for profiling, there must be only trap handlers between the labels btrap
300 * and bintr, and only interrupt handlers between the labels bintr and
301 * eintr. This is implemented (partly) by including files that contain
302 * some of the handlers. Before including the files, set up a normal asm
303 * environment so that the included files doen't need to know that they are
314 #include <i386/i386/atpic_vector.s>
317 #if defined(DEV_APIC) && defined(DEV_ATPIC)
325 #include <i386/i386/apic_vector.s>
332 #include <i386/i386/vm86bios.s>
338 * void doreti(struct trapframe)
340 * Handle return from interrupts, traps and syscalls.
344 .type doreti,@function
347 FAKE_MCOUNT($bintr) /* init "from" bintr -> doreti */
350 * Check if ASTs can be handled now. ASTs cannot be safely
351 * processed when returning from an NMI.
353 cmpb $T_NMI,TF_TRAPNO(%esp)
360 * PSL_VM must be checked first since segment registers only
361 * have an RPL in non-VM86 mode.
362 * ASTs can not be handled now if we are in a vm86 call.
364 testl $PSL_VM,TF_EFLAGS(%esp)
366 movl PCPU(CURPCB),%ecx
367 testl $PCB_VM86CALL,PCB_FLAGS(%ecx)
372 testb $SEL_RPL_MASK,TF_CS(%esp) /* are we returning to user mode? */
373 jz doreti_exit /* can't handle ASTs now if not */
377 * Check for ASTs atomically with returning. Disabling CPU
378 * interrupts provides sufficient locking even in the SMP case,
379 * since we will be informed of any new ASTs by an IPI.
382 movl PCPU(CURTHREAD),%eax
383 testl $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%eax)
386 pushl %esp /* pass a pointer to the trapframe */
392 * doreti_exit: pop registers, iret.
394 * The segment register pop is a special case, since it may
395 * fault if (for example) a sigreturn specifies bad segment
396 * registers. The fault is handled in trap.c.
401 .globl doreti_popl_fs
404 .globl doreti_popl_es
407 .globl doreti_popl_ds
417 * doreti_iret_fault and friends. Alternative return code for
418 * the case where we get a fault in the doreti_exit code
419 * above. trap() (i386/i386/trap.c) catches this specific
420 * case, sends the process a signal and continues in the
421 * corresponding place in the code below.
424 .globl doreti_iret_fault
430 .globl doreti_popl_ds_fault
431 doreti_popl_ds_fault:
434 .globl doreti_popl_es_fault
435 doreti_popl_es_fault:
438 .globl doreti_popl_fs_fault
439 doreti_popl_fs_fault:
441 movl $0,TF_ERR(%esp) /* XXX should be the error code */
442 movl $T_PROTFLT,TF_TRAPNO(%esp)
443 jmp alltraps_with_regs_pushed
447 * Since we are returning from an NMI, check if the current trap
448 * was from user mode and if so whether the current thread
449 * needs a user call chain capture.
451 testb $SEL_RPL_MASK,TF_CS(%esp)
453 movl PCPU(CURTHREAD),%eax /* curthread present? */
456 testl $TDP_CALLCHAIN,TD_PFLAGS(%eax) /* flagged for capture? */
459 * Take the processor out of NMI mode by executing a fake "iret".
467 * Call the callchain capture hook after turning interrupts back on.
472 pushl %esp /* frame pointer */
473 pushl $PMC_FN_USER_CALLCHAIN /* command */
474 movl PCPU(CURTHREAD),%eax
475 pushl %eax /* curthread */
480 ENTRY(end_exceptions)