1 /*-
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
5 * All rights reserved.
6 *
7 * Portions of this software were developed by A. Joseph Koshy under
8 * sponsorship from the FreeBSD Foundation and Google, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: releng/7.3/sys/i386/i386/exception.s 193634 2009-06-07 10:00:35Z fabient $
35 */
36
37 #include "opt_apic.h"
38 #include "opt_kdtrace.h"
39 #include "opt_hwpmc_hooks.h"
40 #include "opt_npx.h"
41
42 #include <machine/asmacros.h>
43 #include <machine/psl.h>
44 #include <machine/trap.h>
45
46 #include "assym.s"
47
48 #define SEL_RPL_MASK 0x0003
49 #define GSEL_KPL 0x0020 /* GSEL(GCODE_SEL, SEL_KPL) */
50
51
52 #ifdef KDTRACE_HOOKS
53 .bss
54 .globl dtrace_invop_jump_addr
55 .align 4
56 .type dtrace_invop_jump_addr, @object
57 .size dtrace_invop_jump_addr, 4
58 dtrace_invop_jump_addr:
59 .zero 4
60 .globl dtrace_invop_calltrap_addr
61 .align 4
62 .type dtrace_invop_calltrap_addr, @object
63 .size dtrace_invop_calltrap_addr, 4
64 dtrace_invop_calltrap_addr:
65 .zero 8
66 #endif
67 .text
68 #ifdef HWPMC_HOOKS
69 ENTRY(start_exceptions)
70 #endif
71 /*****************************************************************************/
72 /* Trap handling */
73 /*****************************************************************************/
74 /*
75 * Trap and fault vector routines.
76 *
77 * Most traps are 'trap gates', SDT_SYS386TGT. A trap gate pushes state on
78 * the stack that mostly looks like an interrupt, but does not disable
79 * interrupts. A few of the traps we are use are interrupt gates,
80 * SDT_SYS386IGT, which are nearly the same thing except interrupts are
81 * disabled on entry.
82 *
83 * The cpu will push a certain amount of state onto the kernel stack for
84 * the current process. The amount of state depends on the type of trap
85 * and whether the trap crossed rings or not. See i386/include/frame.h.
86 * At the very least the current EFLAGS (status register, which includes
87 * the interrupt disable state prior to the trap), the code segment register,
88 * and the return instruction pointer are pushed by the cpu. The cpu
89 * will also push an 'error' code for certain traps. We push a dummy
90 * error code for those traps where the cpu doesn't in order to maintain
91 * a consistent frame. We also push a contrived 'trap number'.
92 *
93 * The cpu does not push the general registers, we must do that, and we
94 * must restore them prior to calling 'iret'. The cpu adjusts the %cs and
95 * %ss segment registers, but does not mess with %ds, %es, or %fs. Thus we
96 * must load them with appropriate values for supervisor mode operation.
97 */
98
99 MCOUNT_LABEL(user)
100 MCOUNT_LABEL(btrap)
101
102 #define TRAP(a) pushl $(a) ; jmp alltraps
103
104 IDTVEC(div)
105 pushl $0; TRAP(T_DIVIDE)
106 IDTVEC(dbg)
107 pushl $0; TRAP(T_TRCTRAP)
108 IDTVEC(nmi)
109 pushl $0; TRAP(T_NMI)
110 IDTVEC(bpt)
111 pushl $0; TRAP(T_BPTFLT)
112 IDTVEC(ofl)
113 pushl $0; TRAP(T_OFLOW)
114 IDTVEC(bnd)
115 pushl $0; TRAP(T_BOUND)
116 #ifndef KDTRACE_HOOKS
117 IDTVEC(ill)
118 pushl $0; TRAP(T_PRIVINFLT)
119 #endif
120 IDTVEC(dna)
121 pushl $0; TRAP(T_DNA)
122 IDTVEC(fpusegm)
123 pushl $0; TRAP(T_FPOPFLT)
124 IDTVEC(tss)
125 TRAP(T_TSSFLT)
126 IDTVEC(missing)
127 TRAP(T_SEGNPFLT)
128 IDTVEC(stk)
129 TRAP(T_STKFLT)
130 IDTVEC(prot)
131 TRAP(T_PROTFLT)
132 IDTVEC(page)
133 TRAP(T_PAGEFLT)
134 IDTVEC(mchk)
135 pushl $0; TRAP(T_MCHK)
136 IDTVEC(rsvd)
137 pushl $0; TRAP(T_RESERVED)
138 IDTVEC(fpu)
139 pushl $0; TRAP(T_ARITHTRAP)
140 IDTVEC(align)
141 TRAP(T_ALIGNFLT)
142 IDTVEC(xmm)
143 pushl $0; TRAP(T_XMMFLT)
144
145 /*
146 * alltraps entry point. Interrupts are enabled if this was a trap
147 * gate (TGT), else disabled if this was an interrupt gate (IGT).
148 * Note that int0x80_syscall is a trap gate. Interrupt gates are
149 * used by page faults, non-maskable interrupts, debug and breakpoint
150 * exceptions.
151 */
152
153 SUPERALIGN_TEXT
154 .globl alltraps
155 .type alltraps,@function
156 alltraps:
157 pushal
158 pushl %ds
159 pushl %es
160 pushl %fs
161 alltraps_with_regs_pushed:
162 SET_KERNEL_SREGS
163 FAKE_MCOUNT(TF_EIP(%esp))
164 calltrap:
165 pushl %esp
166 call trap
167 add $4, %esp
168
169 /*
170 * Return via doreti to handle ASTs.
171 */
172 MEXITCOUNT
173 jmp doreti
174
175 /*
176 * Privileged instruction fault.
177 */
178 #ifdef KDTRACE_HOOKS
179 SUPERALIGN_TEXT
180 IDTVEC(ill)
181 /* Check if there is no DTrace hook registered. */
182 cmpl $0,dtrace_invop_jump_addr
183 je norm_ill
184
185 /* Check if this is a user fault. */
186 cmpl $GSEL_KPL, 4(%esp) /* Check the code segment. */
187
188 /* If so, just handle it as a normal trap. */
189 jne norm_ill
190
191 /*
192 * This is a kernel instruction fault that might have been caused
193 * by a DTrace provider.
194 */
195 pushal /* Push all registers onto the stack. */
196
197 /*
198 * Set our jump address for the jump back in the event that
199 * the exception wasn't caused by DTrace at all.
200 */
201 movl $norm_ill, dtrace_invop_calltrap_addr
202
203 /* Jump to the code hooked in by DTrace. */
204 jmpl *dtrace_invop_jump_addr
205
206 /*
207 * Process the instruction fault in the normal way.
208 */
209 norm_ill:
210 pushl $0
211 TRAP(T_PRIVINFLT)
212 #endif
213
214 /*
215 * SYSCALL CALL GATE (old entry point for a.out binaries)
216 *
217 * The intersegment call has been set up to specify one dummy parameter.
218 *
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.
225 */
226 SUPERALIGN_TEXT
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 */
232 pushal
233 pushl %ds
234 pushl %es
235 pushl %fs
236 SET_KERNEL_SREGS
237 FAKE_MCOUNT(TF_EIP(%esp))
238 pushl %esp
239 call syscall
240 add $4, %esp
241 MEXITCOUNT
242 jmp doreti
243
244 /*
245 * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
246 *
247 * Even though the name says 'int0x80', this is actually a TGT (trap gate)
248 * rather then an IGT (interrupt gate). Thus interrupts are enabled on
249 * entry just as they are for a normal syscall.
250 */
251 SUPERALIGN_TEXT
252 IDTVEC(int0x80_syscall)
253 pushl $2 /* sizeof "int 0x80" */
254 subl $4,%esp /* skip over tf_trapno */
255 pushal
256 pushl %ds
257 pushl %es
258 pushl %fs
259 SET_KERNEL_SREGS
260 FAKE_MCOUNT(TF_EIP(%esp))
261 pushl %esp
262 call syscall
263 add $4, %esp
264 MEXITCOUNT
265 jmp doreti
266
267 ENTRY(fork_trampoline)
268 pushl %esp /* trapframe pointer */
269 pushl %ebx /* arg1 */
270 pushl %esi /* function */
271 call fork_exit
272 addl $12,%esp
273 /* cut from syscall */
274
275 /*
276 * Return via doreti to handle ASTs.
277 */
278 MEXITCOUNT
279 jmp doreti
280
281
282 /*
283 * To efficiently implement classification of trap and interrupt handlers
284 * for profiling, there must be only trap handlers between the labels btrap
285 * and bintr, and only interrupt handlers between the labels bintr and
286 * eintr. This is implemented (partly) by including files that contain
287 * some of the handlers. Before including the files, set up a normal asm
288 * environment so that the included files doen't need to know that they are
289 * included.
290 */
291
292 .data
293 .p2align 4
294 .text
295 SUPERALIGN_TEXT
296 MCOUNT_LABEL(bintr)
297
298 #include <i386/isa/atpic_vector.s>
299
300 #ifdef DEV_APIC
301 .data
302 .p2align 4
303 .text
304 SUPERALIGN_TEXT
305
306 #include <i386/i386/apic_vector.s>
307 #endif
308
309 .data
310 .p2align 4
311 .text
312 SUPERALIGN_TEXT
313 #include <i386/i386/vm86bios.s>
314
315 .text
316 MCOUNT_LABEL(eintr)
317
318 /*
319 * void doreti(struct trapframe)
320 *
321 * Handle return from interrupts, traps and syscalls.
322 */
323 .text
324 SUPERALIGN_TEXT
325 .type doreti,@function
326 doreti:
327 FAKE_MCOUNT($bintr) /* init "from" bintr -> doreti */
328 doreti_next:
329 /*
330 * Check if ASTs can be handled now. ASTs cannot be safely
331 * processed when returning from an NMI.
332 */
333 cmpb $T_NMI,TF_TRAPNO(%esp)
334 #ifdef HWPMC_HOOKS
335 je doreti_nmi
336 #else
337 je doreti_exit
338 #endif
339 /*
340 * PSL_VM must be checked first since segment registers only
341 * have an RPL in non-VM86 mode.
342 */
343 testl $PSL_VM,TF_EFLAGS(%esp) /* are we in vm86 mode? */
344 jz doreti_notvm86
345 movl PCPU(CURPCB),%ecx
346 testl $PCB_VM86CALL,PCB_FLAGS(%ecx) /* are we in a vm86 call? */
347 jz doreti_ast /* can handle ASTS now if not */
348 jmp doreti_exit
349
350 doreti_notvm86:
351 testb $SEL_RPL_MASK,TF_CS(%esp) /* are we returning to user mode? */
352 jz doreti_exit /* can't handle ASTs now if not */
353
354 doreti_ast:
355 /*
356 * Check for ASTs atomically with returning. Disabling CPU
357 * interrupts provides sufficient locking even in the SMP case,
358 * since we will be informed of any new ASTs by an IPI.
359 */
360 cli
361 movl PCPU(CURTHREAD),%eax
362 testl $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%eax)
363 je doreti_exit
364 sti
365 pushl %esp /* pass a pointer to the trapframe */
366 call ast
367 add $4,%esp
368 jmp doreti_ast
369
370 /*
371 * doreti_exit: pop registers, iret.
372 *
373 * The segment register pop is a special case, since it may
374 * fault if (for example) a sigreturn specifies bad segment
375 * registers. The fault is handled in trap.c.
376 */
377 doreti_exit:
378 MEXITCOUNT
379
380 .globl doreti_popl_fs
381 doreti_popl_fs:
382 popl %fs
383 .globl doreti_popl_es
384 doreti_popl_es:
385 popl %es
386 .globl doreti_popl_ds
387 doreti_popl_ds:
388 popl %ds
389 popal
390 addl $8,%esp
391 .globl doreti_iret
392 doreti_iret:
393 iret
394
395 /*
396 * doreti_iret_fault and friends. Alternative return code for
397 * the case where we get a fault in the doreti_exit code
398 * above. trap() (i386/i386/trap.c) catches this specific
399 * case, sends the process a signal and continues in the
400 * corresponding place in the code below.
401 */
402 ALIGN_TEXT
403 .globl doreti_iret_fault
404 doreti_iret_fault:
405 subl $8,%esp
406 pushal
407 pushl %ds
408 .globl doreti_popl_ds_fault
409 doreti_popl_ds_fault:
410 pushl %es
411 .globl doreti_popl_es_fault
412 doreti_popl_es_fault:
413 pushl %fs
414 .globl doreti_popl_fs_fault
415 doreti_popl_fs_fault:
416 movl $0,TF_ERR(%esp) /* XXX should be the error code */
417 movl $T_PROTFLT,TF_TRAPNO(%esp)
418 jmp alltraps_with_regs_pushed
419 #ifdef HWPMC_HOOKS
420 doreti_nmi:
421 /*
422 * Since we are returning from an NMI, check if the current trap
423 * was from user mode and if so whether the current thread
424 * needs a user call chain capture.
425 */
426 testb $SEL_RPL_MASK,TF_CS(%esp)
427 jz doreti_exit
428 movl PCPU(CURTHREAD),%eax /* curthread present? */
429 orl %eax,%eax
430 jz doreti_exit
431 testl $TDP_CALLCHAIN,TD_PFLAGS(%eax) /* flagged for capture? */
432 jz doreti_exit
433 /*
434 * Take the processor out of NMI mode by executing a fake "iret".
435 */
436 pushfl
437 pushl %cs
438 pushl $outofnmi
439 iret
440 outofnmi:
441 /*
442 * Call the callchain capture hook after turning interrupts back on.
443 */
444 movl pmc_hook,%ecx
445 orl %ecx,%ecx
446 jz doreti_exit
447 pushl %esp /* frame pointer */
448 pushl $PMC_FN_USER_CALLCHAIN /* command */
449 movl PCPU(CURTHREAD),%eax
450 pushl %eax /* curthread */
451 sti
452 call *%ecx
453 addl $12,%esp
454 jmp doreti_ast
455 ENTRY(end_exceptions)
456 #endif
Cache object: 8b007c437209504c23d47a998b79c4ec
|