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