]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/exception.s
Update mandoc to cvs snaphot from 20150302
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / exception.s
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$
35  */
36
37 #include "opt_apic.h"
38 #include "opt_atpic.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 #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          * All traps except ones for syscalls jump to alltraps.  If
148          * interrupts were enabled when the trap occurred, then interrupts
149          * are enabled now if the trap was through a trap gate, else
150          * disabled if the trap was through an interrupt gate.  Note that
151          * int0x80_syscall is a trap gate.   Interrupt gates are used by
152          * page faults, non-maskable interrupts, debug and breakpoint
153          * exceptions.
154          */
155         SUPERALIGN_TEXT
156         .globl  alltraps
157         .type   alltraps,@function
158 alltraps:
159         pushal
160         pushl   %ds
161         pushl   %es
162         pushl   %fs
163 alltraps_with_regs_pushed:
164         SET_KERNEL_SREGS
165         cld
166         FAKE_MCOUNT(TF_EIP(%esp))
167 calltrap:
168         pushl   %esp
169         call    trap
170         add     $4, %esp
171
172         /*
173          * Return via doreti to handle ASTs.
174          */
175         MEXITCOUNT
176         jmp     doreti
177
178 /*
179  * Privileged instruction fault.
180  */
181 #ifdef KDTRACE_HOOKS
182         SUPERALIGN_TEXT
183 IDTVEC(ill)
184         /* Check if there is no DTrace hook registered. */
185         cmpl    $0,dtrace_invop_jump_addr
186         je      norm_ill
187
188         /* Check if this is a user fault. */
189         cmpl    $GSEL_KPL, 4(%esp)      /* Check the code segment. */
190
191         /* If so, just handle it as a normal trap. */
192         jne     norm_ill
193
194         /*
195          * This is a kernel instruction fault that might have been caused
196          * by a DTrace provider.
197          */
198         pushal                          /* Push all registers onto the stack. */
199
200         /*
201          * Set our jump address for the jump back in the event that
202          * the exception wasn't caused by DTrace at all.
203          */
204         movl    $norm_ill, dtrace_invop_calltrap_addr
205
206         /* Jump to the code hooked in by DTrace. */
207         jmpl    *dtrace_invop_jump_addr
208
209         /*
210          * Process the instruction fault in the normal way.
211          */
212 norm_ill:
213         pushl $0
214         TRAP(T_PRIVINFLT)
215 #endif
216
217 /*
218  * Call gate entry for syscalls (lcall 7,0).
219  * This is used by FreeBSD 1.x a.out executables and "old" NetBSD executables.
220  *
221  * The intersegment call has been set up to specify one dummy parameter.
222  * This leaves a place to put eflags so that the call frame can be
223  * converted to a trap frame. Note that the eflags is (semi-)bogusly
224  * pushed into (what will be) tf_err and then copied later into the
225  * final spot. It has to be done this way because esp can't be just
226  * temporarily altered for the pushfl - an interrupt might come in
227  * and clobber the saved cs/eip.
228  */
229         SUPERALIGN_TEXT
230 IDTVEC(lcall_syscall)
231         pushfl                          /* save eflags */
232         popl    8(%esp)                 /* shuffle into tf_eflags */
233         pushl   $7                      /* sizeof "lcall 7,0" */
234         subl    $4,%esp                 /* skip over tf_trapno */
235         pushal
236         pushl   %ds
237         pushl   %es
238         pushl   %fs
239         SET_KERNEL_SREGS
240         cld
241         FAKE_MCOUNT(TF_EIP(%esp))
242         pushl   %esp
243         call    syscall
244         add     $4, %esp
245         MEXITCOUNT
246         jmp     doreti
247
248 /*
249  * Trap gate entry for syscalls (int 0x80).
250  * This is used by FreeBSD ELF executables, "new" NetBSD executables, and all
251  * Linux executables.
252  *
253  * Even though the name says 'int0x80', this is actually a trap gate, not an
254  * interrupt gate.  Thus interrupts are enabled on entry just as they are for
255  * a normal syscall.
256  */
257         SUPERALIGN_TEXT
258 IDTVEC(int0x80_syscall)
259         pushl   $2                      /* sizeof "int 0x80" */
260         subl    $4,%esp                 /* skip over tf_trapno */
261         pushal
262         pushl   %ds
263         pushl   %es
264         pushl   %fs
265         SET_KERNEL_SREGS
266         cld
267         FAKE_MCOUNT(TF_EIP(%esp))
268         pushl   %esp
269         call    syscall
270         add     $4, %esp
271         MEXITCOUNT
272         jmp     doreti
273
274 ENTRY(fork_trampoline)
275         pushl   %esp                    /* trapframe pointer */
276         pushl   %ebx                    /* arg1 */
277         pushl   %esi                    /* function */
278         call    fork_exit
279         addl    $12,%esp
280         /* cut from syscall */
281
282         /*
283          * Return via doreti to handle ASTs.
284          */
285         MEXITCOUNT
286         jmp     doreti
287
288
289 /*
290  * To efficiently implement classification of trap and interrupt handlers
291  * for profiling, there must be only trap handlers between the labels btrap
292  * and bintr, and only interrupt handlers between the labels bintr and
293  * eintr.  This is implemented (partly) by including files that contain
294  * some of the handlers.  Before including the files, set up a normal asm
295  * environment so that the included files doen't need to know that they are
296  * included.
297  */
298
299         .data
300         .p2align 4
301         .text
302         SUPERALIGN_TEXT
303 MCOUNT_LABEL(bintr)
304
305 #ifdef DEV_ATPIC
306 #include <i386/i386/atpic_vector.s>
307 #endif
308
309 #if defined(DEV_APIC) && defined(DEV_ATPIC)
310         .data
311         .p2align 4
312         .text
313         SUPERALIGN_TEXT
314 #endif
315
316 #ifdef DEV_APIC
317 #include <i386/i386/apic_vector.s>
318 #endif
319
320         .data
321         .p2align 4
322         .text
323         SUPERALIGN_TEXT
324 #include <i386/i386/vm86bios.s>
325
326         .text
327 MCOUNT_LABEL(eintr)
328
329 /*
330  * void doreti(struct trapframe)
331  *
332  * Handle return from interrupts, traps and syscalls.
333  */
334         .text
335         SUPERALIGN_TEXT
336         .type   doreti,@function
337 doreti:
338         FAKE_MCOUNT($bintr)             /* init "from" bintr -> doreti */
339 doreti_next:
340         /*
341          * Check if ASTs can be handled now.  ASTs cannot be safely
342          * processed when returning from an NMI.
343          */
344         cmpb    $T_NMI,TF_TRAPNO(%esp)
345 #ifdef HWPMC_HOOKS
346         je      doreti_nmi
347 #else
348         je      doreti_exit
349 #endif
350         /*
351          * PSL_VM must be checked first since segment registers only
352          * have an RPL in non-VM86 mode.
353          * ASTs can not be handled now if we are in a vm86 call.
354          */
355         testl   $PSL_VM,TF_EFLAGS(%esp)
356         jz      doreti_notvm86
357         movl    PCPU(CURPCB),%ecx
358         testl   $PCB_VM86CALL,PCB_FLAGS(%ecx)
359         jz      doreti_ast
360         jmp     doreti_exit
361
362 doreti_notvm86:
363         testb   $SEL_RPL_MASK,TF_CS(%esp) /* are we returning to user mode? */
364         jz      doreti_exit             /* can't handle ASTs now if not */
365
366 doreti_ast:
367         /*
368          * Check for ASTs atomically with returning.  Disabling CPU
369          * interrupts provides sufficient locking even in the SMP case,
370          * since we will be informed of any new ASTs by an IPI.
371          */
372         cli
373         movl    PCPU(CURTHREAD),%eax
374         testl   $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%eax)
375         je      doreti_exit
376         sti
377         pushl   %esp                    /* pass a pointer to the trapframe */
378         call    ast
379         add     $4,%esp
380         jmp     doreti_ast
381
382         /*
383          * doreti_exit: pop registers, iret.
384          *
385          *      The segment register pop is a special case, since it may
386          *      fault if (for example) a sigreturn specifies bad segment
387          *      registers.  The fault is handled in trap.c.
388          */
389 doreti_exit:
390         MEXITCOUNT
391
392         .globl  doreti_popl_fs
393 doreti_popl_fs:
394         popl    %fs
395         .globl  doreti_popl_es
396 doreti_popl_es:
397         popl    %es
398         .globl  doreti_popl_ds
399 doreti_popl_ds:
400         popl    %ds
401         popal
402         addl    $8,%esp
403         .globl  doreti_iret
404 doreti_iret:
405         iret
406
407         /*
408          * doreti_iret_fault and friends.  Alternative return code for
409          * the case where we get a fault in the doreti_exit code
410          * above.  trap() (i386/i386/trap.c) catches this specific
411          * case, sends the process a signal and continues in the
412          * corresponding place in the code below.
413          */
414         ALIGN_TEXT
415         .globl  doreti_iret_fault
416 doreti_iret_fault:
417         subl    $8,%esp
418         pushal
419         pushl   %ds
420         .globl  doreti_popl_ds_fault
421 doreti_popl_ds_fault:
422         pushl   %es
423         .globl  doreti_popl_es_fault
424 doreti_popl_es_fault:
425         pushl   %fs
426         .globl  doreti_popl_fs_fault
427 doreti_popl_fs_fault:
428         sti
429         movl    $0,TF_ERR(%esp) /* XXX should be the error code */
430         movl    $T_PROTFLT,TF_TRAPNO(%esp)
431         jmp     alltraps_with_regs_pushed
432 #ifdef HWPMC_HOOKS
433 doreti_nmi:
434         /*
435          * Since we are returning from an NMI, check if the current trap
436          * was from user mode and if so whether the current thread
437          * needs a user call chain capture.
438          */
439         testb   $SEL_RPL_MASK,TF_CS(%esp)
440         jz      doreti_exit
441         movl    PCPU(CURTHREAD),%eax    /* curthread present? */
442         orl     %eax,%eax
443         jz      doreti_exit
444         testl   $TDP_CALLCHAIN,TD_PFLAGS(%eax) /* flagged for capture? */
445         jz      doreti_exit
446         /*
447          * Take the processor out of NMI mode by executing a fake "iret".
448          */
449         pushfl
450         pushl   %cs
451         pushl   $outofnmi
452         iret
453 outofnmi:
454         /*
455          * Call the callchain capture hook after turning interrupts back on.
456          */
457         movl    pmc_hook,%ecx
458         orl     %ecx,%ecx
459         jz      doreti_exit
460         pushl   %esp                    /* frame pointer */
461         pushl   $PMC_FN_USER_CALLCHAIN  /* command */
462         movl    PCPU(CURTHREAD),%eax
463         pushl   %eax                    /* curthread */
464         sti
465         call    *%ecx
466         addl    $12,%esp
467         jmp     doreti_ast
468         ENTRY(end_exceptions)
469 #endif