]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/exception.s
Merge compiler-rt trunk r321017 to contrib/compiler-rt.
[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  * 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.
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
41 #include <machine/asmacros.h>
42 #include <machine/psl.h>
43 #include <machine/trap.h>
44
45 #include "assym.s"
46
47 #define SEL_RPL_MASK    0x0003
48 #define GSEL_KPL        0x0020  /* GSEL(GCODE_SEL, SEL_KPL) */
49
50 #ifdef KDTRACE_HOOKS
51         .bss
52         .globl  dtrace_invop_jump_addr
53         .align  4
54         .type   dtrace_invop_jump_addr, @object
55         .size   dtrace_invop_jump_addr, 4
56 dtrace_invop_jump_addr:
57         .zero   4
58         .globl  dtrace_invop_calltrap_addr
59         .align  4
60         .type   dtrace_invop_calltrap_addr, @object
61         .size   dtrace_invop_calltrap_addr, 4
62 dtrace_invop_calltrap_addr:
63         .zero   8
64 #endif
65         .text
66 #ifdef HWPMC_HOOKS
67         ENTRY(start_exceptions)
68 #endif
69 /*****************************************************************************/
70 /* Trap handling                                                             */
71 /*****************************************************************************/
72 /*
73  * Trap and fault vector routines.
74  *
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
79  * disabled on entry.
80  *
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'.
90  *
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.
95  */
96
97 MCOUNT_LABEL(user)
98 MCOUNT_LABEL(btrap)
99
100 #define TRAP(a)         pushl $(a) ; jmp alltraps
101
102 IDTVEC(div)
103         pushl $0; TRAP(T_DIVIDE)
104 IDTVEC(dbg)
105         pushl $0; TRAP(T_TRCTRAP)
106 IDTVEC(nmi)
107         pushl $0; TRAP(T_NMI)
108 IDTVEC(bpt)
109         pushl $0; TRAP(T_BPTFLT)
110 IDTVEC(dtrace_ret)
111         pushl $0; TRAP(T_DTRACE_RET)
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          * 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
152          * exceptions.
153          */
154         SUPERALIGN_TEXT
155         .globl  alltraps
156         .type   alltraps,@function
157 alltraps:
158         pushal
159         pushl   $0
160         movw    %ds,(%esp)
161         pushl   $0
162         movw    %es,(%esp)
163         pushl   $0
164         movw    %fs,(%esp)
165 alltraps_with_regs_pushed:
166         SET_KERNEL_SREGS
167         cld
168         FAKE_MCOUNT(TF_EIP(%esp))
169 calltrap:
170         pushl   %esp
171         call    trap
172         add     $4, %esp
173
174         /*
175          * Return via doreti to handle ASTs.
176          */
177         MEXITCOUNT
178         jmp     doreti
179
180 /*
181  * Privileged instruction fault.
182  */
183 #ifdef KDTRACE_HOOKS
184         SUPERALIGN_TEXT
185 IDTVEC(ill)
186         /*
187          * Check if a DTrace hook is registered.  The default (data) segment
188          * cannot be used for this since %ds is not known good until we
189          * verify that the entry was from kernel mode.
190          */
191         cmpl    $0,%ss:dtrace_invop_jump_addr
192         je      norm_ill
193
194         /*
195          * Check if this is a user fault.  If so, just handle it as a normal
196          * trap.
197          */
198         cmpl    $GSEL_KPL, 4(%esp)      /* Check the code segment */
199         jne     norm_ill
200         testl   $PSL_VM, 8(%esp)        /* and vm86 mode. */
201         jnz     norm_ill
202
203         /*
204          * This is a kernel instruction fault that might have been caused
205          * by a DTrace provider.
206          */
207         pushal
208         cld
209
210         /*
211          * Set our jump address for the jump back in the event that
212          * the exception wasn't caused by DTrace at all.
213          */
214         movl    $norm_ill, dtrace_invop_calltrap_addr
215
216         /* Jump to the code hooked in by DTrace. */
217         jmpl    *dtrace_invop_jump_addr
218
219         /*
220          * Process the instruction fault in the normal way.
221          */
222 norm_ill:
223         pushl $0
224         TRAP(T_PRIVINFLT)
225 #endif
226
227 /*
228  * Call gate entry for syscalls (lcall 7,0).
229  * This is used by FreeBSD 1.x a.out executables and "old" NetBSD executables.
230  *
231  * The intersegment call has been set up to specify one dummy parameter.
232  * This leaves a place to put eflags so that the call frame can be
233  * converted to a trap frame. Note that the eflags is (semi-)bogusly
234  * pushed into (what will be) tf_err and then copied later into the
235  * final spot. It has to be done this way because esp can't be just
236  * temporarily altered for the pushfl - an interrupt might come in
237  * and clobber the saved cs/eip.
238  */
239         SUPERALIGN_TEXT
240 IDTVEC(lcall_syscall)
241         pushfl                          /* save eflags */
242         popl    8(%esp)                 /* shuffle into tf_eflags */
243         pushl   $7                      /* sizeof "lcall 7,0" */
244         pushl   $0                      /* tf_trapno */
245         pushal
246         pushl   $0
247         movw    %ds,(%esp)
248         pushl   $0
249         movw    %es,(%esp)
250         pushl   $0
251         movw    %fs,(%esp)
252         SET_KERNEL_SREGS
253         cld
254         FAKE_MCOUNT(TF_EIP(%esp))
255         pushl   %esp
256         call    syscall
257         add     $4, %esp
258         MEXITCOUNT
259         jmp     doreti
260
261 /*
262  * Trap gate entry for syscalls (int 0x80).
263  * This is used by FreeBSD ELF executables, "new" NetBSD executables, and all
264  * Linux executables.
265  *
266  * Even though the name says 'int0x80', this is actually a trap gate, not an
267  * interrupt gate.  Thus interrupts are enabled on entry just as they are for
268  * a normal syscall.
269  */
270         SUPERALIGN_TEXT
271 IDTVEC(int0x80_syscall)
272         pushl   $2                      /* sizeof "int 0x80" */
273         pushl   $0                      /* tf_trapno */
274         pushal
275         pushl   $0
276         movw    %ds,(%esp)
277         pushl   $0
278         movw    %es,(%esp)
279         pushl   $0
280         movw    %fs,(%esp)
281         SET_KERNEL_SREGS
282         cld
283         FAKE_MCOUNT(TF_EIP(%esp))
284         pushl   %esp
285         call    syscall
286         add     $4, %esp
287         MEXITCOUNT
288         jmp     doreti
289
290 ENTRY(fork_trampoline)
291         pushl   %esp                    /* trapframe pointer */
292         pushl   %ebx                    /* arg1 */
293         pushl   %esi                    /* function */
294         call    fork_exit
295         addl    $12,%esp
296         /* cut from syscall */
297
298         /*
299          * Return via doreti to handle ASTs.
300          */
301         MEXITCOUNT
302         jmp     doreti
303
304
305 /*
306  * To efficiently implement classification of trap and interrupt handlers
307  * for profiling, there must be only trap handlers between the labels btrap
308  * and bintr, and only interrupt handlers between the labels bintr and
309  * eintr.  This is implemented (partly) by including files that contain
310  * some of the handlers.  Before including the files, set up a normal asm
311  * environment so that the included files doen't need to know that they are
312  * included.
313  */
314
315         .data
316         .p2align 4
317         .text
318         SUPERALIGN_TEXT
319 MCOUNT_LABEL(bintr)
320
321 #ifdef DEV_ATPIC
322 #include <i386/i386/atpic_vector.s>
323 #endif
324
325 #if defined(DEV_APIC) && defined(DEV_ATPIC)
326         .data
327         .p2align 4
328         .text
329         SUPERALIGN_TEXT
330 #endif
331
332 #ifdef DEV_APIC
333 #include <i386/i386/apic_vector.s>
334 #endif
335
336         .data
337         .p2align 4
338         .text
339         SUPERALIGN_TEXT
340 #include <i386/i386/vm86bios.s>
341
342         .text
343 MCOUNT_LABEL(eintr)
344
345 /*
346  * void doreti(struct trapframe)
347  *
348  * Handle return from interrupts, traps and syscalls.
349  */
350         .text
351         SUPERALIGN_TEXT
352         .type   doreti,@function
353         .globl  doreti
354 doreti:
355         FAKE_MCOUNT($bintr)             /* init "from" bintr -> doreti */
356 doreti_next:
357         /*
358          * Check if ASTs can be handled now.  ASTs cannot be safely
359          * processed when returning from an NMI.
360          */
361         cmpb    $T_NMI,TF_TRAPNO(%esp)
362 #ifdef HWPMC_HOOKS
363         je      doreti_nmi
364 #else
365         je      doreti_exit
366 #endif
367         /*
368          * PSL_VM must be checked first since segment registers only
369          * have an RPL in non-VM86 mode.
370          * ASTs can not be handled now if we are in a vm86 call.
371          */
372         testl   $PSL_VM,TF_EFLAGS(%esp)
373         jz      doreti_notvm86
374         movl    PCPU(CURPCB),%ecx
375         testl   $PCB_VM86CALL,PCB_FLAGS(%ecx)
376         jz      doreti_ast
377         jmp     doreti_exit
378
379 doreti_notvm86:
380         testb   $SEL_RPL_MASK,TF_CS(%esp) /* are we returning to user mode? */
381         jz      doreti_exit             /* can't handle ASTs now if not */
382
383 doreti_ast:
384         /*
385          * Check for ASTs atomically with returning.  Disabling CPU
386          * interrupts provides sufficient locking even in the SMP case,
387          * since we will be informed of any new ASTs by an IPI.
388          */
389         cli
390         movl    PCPU(CURTHREAD),%eax
391         testl   $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%eax)
392         je      doreti_exit
393         sti
394         pushl   %esp                    /* pass a pointer to the trapframe */
395         call    ast
396         add     $4,%esp
397         jmp     doreti_ast
398
399         /*
400          * doreti_exit: pop registers, iret.
401          *
402          *      The segment register pop is a special case, since it may
403          *      fault if (for example) a sigreturn specifies bad segment
404          *      registers.  The fault is handled in trap.c.
405          */
406 doreti_exit:
407         MEXITCOUNT
408
409         .globl  doreti_popl_fs
410 doreti_popl_fs:
411         popl    %fs
412         .globl  doreti_popl_es
413 doreti_popl_es:
414         popl    %es
415         .globl  doreti_popl_ds
416 doreti_popl_ds:
417         popl    %ds
418         popal
419         addl    $8,%esp
420         .globl  doreti_iret
421 doreti_iret:
422         iret
423
424         /*
425          * doreti_iret_fault and friends.  Alternative return code for
426          * the case where we get a fault in the doreti_exit code
427          * above.  trap() (i386/i386/trap.c) catches this specific
428          * case, and continues in the corresponding place in the code
429          * below.
430          *
431          * If the fault occured during return to usermode, we recreate
432          * the trap frame and call trap() to send a signal.  Otherwise
433          * the kernel was tricked into fault by attempt to restore invalid
434          * usermode segment selectors on return from nested fault or
435          * interrupt, where interrupted kernel entry code not yet loaded
436          * kernel selectors.  In the latter case, emulate iret and zero
437          * the invalid selector.
438          */
439         ALIGN_TEXT
440         .globl  doreti_iret_fault
441 doreti_iret_fault:
442         subl    $8,%esp
443         pushal
444         pushl   $0
445         movw    %ds,(%esp)
446         .globl  doreti_popl_ds_fault
447 doreti_popl_ds_fault:
448         testb   $SEL_RPL_MASK,TF_CS-TF_DS(%esp)
449         jz      doreti_popl_ds_kfault
450         pushl   $0
451         movw    %es,(%esp)
452         .globl  doreti_popl_es_fault
453 doreti_popl_es_fault:
454         testb   $SEL_RPL_MASK,TF_CS-TF_ES(%esp)
455         jz      doreti_popl_es_kfault
456         pushl   $0
457         movw    %fs,(%esp)
458         .globl  doreti_popl_fs_fault
459 doreti_popl_fs_fault:
460         testb   $SEL_RPL_MASK,TF_CS-TF_FS(%esp)
461         jz      doreti_popl_fs_kfault
462         sti
463         movl    $0,TF_ERR(%esp) /* XXX should be the error code */
464         movl    $T_PROTFLT,TF_TRAPNO(%esp)
465         jmp     alltraps_with_regs_pushed
466
467 doreti_popl_ds_kfault:
468         movl    $0,(%esp)
469         jmp     doreti_popl_ds
470 doreti_popl_es_kfault:
471         movl    $0,(%esp)
472         jmp     doreti_popl_es
473 doreti_popl_fs_kfault:
474         movl    $0,(%esp)
475         jmp     doreti_popl_fs
476         
477 #ifdef HWPMC_HOOKS
478 doreti_nmi:
479         /*
480          * Since we are returning from an NMI, check if the current trap
481          * was from user mode and if so whether the current thread
482          * needs a user call chain capture.
483          */
484         testb   $SEL_RPL_MASK,TF_CS(%esp)
485         jz      doreti_exit
486         movl    PCPU(CURTHREAD),%eax    /* curthread present? */
487         orl     %eax,%eax
488         jz      doreti_exit
489         testl   $TDP_CALLCHAIN,TD_PFLAGS(%eax) /* flagged for capture? */
490         jz      doreti_exit
491         /*
492          * Take the processor out of NMI mode by executing a fake "iret".
493          */
494         pushfl
495         pushl   %cs
496         pushl   $outofnmi
497         iret
498 outofnmi:
499         /*
500          * Call the callchain capture hook after turning interrupts back on.
501          */
502         movl    pmc_hook,%ecx
503         orl     %ecx,%ecx
504         jz      doreti_exit
505         pushl   %esp                    /* frame pointer */
506         pushl   $PMC_FN_USER_CALLCHAIN  /* command */
507         movl    PCPU(CURTHREAD),%eax
508         pushl   %eax                    /* curthread */
509         sti
510         call    *%ecx
511         addl    $12,%esp
512         jmp     doreti_ast
513         ENTRY(end_exceptions)
514 #endif