]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/i386/i386/exception.s
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32
33 #include "opt_apic.h"
34 #include "opt_kdtrace.h"
35 #include "opt_npx.h"
36
37 #include <machine/asmacros.h>
38 #include <machine/psl.h>
39 #include <machine/trap.h>
40
41 #include "assym.s"
42
43 #define SEL_RPL_MASK    0x0003
44 #define GSEL_KPL        0x0020  /* GSEL(GCODE_SEL, SEL_KPL) */
45
46
47 #ifdef KDTRACE_HOOKS
48         .bss
49         .globl  dtrace_invop_jump_addr
50         .align  4
51         .type   dtrace_invop_jump_addr, @object
52         .size   dtrace_invop_jump_addr, 4
53 dtrace_invop_jump_addr:
54         .zero   4
55         .globl  dtrace_invop_calltrap_addr
56         .align  4
57         .type   dtrace_invop_calltrap_addr, @object
58         .size   dtrace_invop_calltrap_addr, 4
59 dtrace_invop_calltrap_addr:
60         .zero   8
61 #endif
62         .text
63
64 /*****************************************************************************/
65 /* Trap handling                                                             */
66 /*****************************************************************************/
67 /*
68  * Trap and fault vector routines.
69  *
70  * Most traps are 'trap gates', SDT_SYS386TGT.  A trap gate pushes state on
71  * the stack that mostly looks like an interrupt, but does not disable 
72  * interrupts.  A few of the traps we are use are interrupt gates, 
73  * SDT_SYS386IGT, which are nearly the same thing except interrupts are
74  * disabled on entry.
75  *
76  * The cpu will push a certain amount of state onto the kernel stack for
77  * the current process.  The amount of state depends on the type of trap 
78  * and whether the trap crossed rings or not.  See i386/include/frame.h.  
79  * At the very least the current EFLAGS (status register, which includes 
80  * the interrupt disable state prior to the trap), the code segment register,
81  * and the return instruction pointer are pushed by the cpu.  The cpu 
82  * will also push an 'error' code for certain traps.  We push a dummy 
83  * error code for those traps where the cpu doesn't in order to maintain 
84  * a consistent frame.  We also push a contrived 'trap number'.
85  *
86  * The cpu does not push the general registers, we must do that, and we 
87  * must restore them prior to calling 'iret'.  The cpu adjusts the %cs and
88  * %ss segment registers, but does not mess with %ds, %es, or %fs.  Thus we
89  * must load them with appropriate values for supervisor mode operation.
90  */
91
92 MCOUNT_LABEL(user)
93 MCOUNT_LABEL(btrap)
94
95 #define TRAP(a)         pushl $(a) ; jmp alltraps
96
97 IDTVEC(div)
98         pushl $0; TRAP(T_DIVIDE)
99 IDTVEC(dbg)
100         pushl $0; TRAP(T_TRCTRAP)
101 IDTVEC(nmi)
102         pushl $0; TRAP(T_NMI)
103 IDTVEC(bpt)
104         pushl $0; TRAP(T_BPTFLT)
105 IDTVEC(ofl)
106         pushl $0; TRAP(T_OFLOW)
107 IDTVEC(bnd)
108         pushl $0; TRAP(T_BOUND)
109 #ifndef KDTRACE_HOOKS
110 IDTVEC(ill)
111         pushl $0; TRAP(T_PRIVINFLT)
112 #endif
113 IDTVEC(dna)
114         pushl $0; TRAP(T_DNA)
115 IDTVEC(fpusegm)
116         pushl $0; TRAP(T_FPOPFLT)
117 IDTVEC(tss)
118         TRAP(T_TSSFLT)
119 IDTVEC(missing)
120         TRAP(T_SEGNPFLT)
121 IDTVEC(stk)
122         TRAP(T_STKFLT)
123 IDTVEC(prot)
124         TRAP(T_PROTFLT)
125 IDTVEC(page)
126         TRAP(T_PAGEFLT)
127 IDTVEC(mchk)
128         pushl $0; TRAP(T_MCHK)
129 IDTVEC(rsvd)
130         pushl $0; TRAP(T_RESERVED)
131 IDTVEC(fpu)
132         pushl $0; TRAP(T_ARITHTRAP)
133 IDTVEC(align)
134         TRAP(T_ALIGNFLT)
135 IDTVEC(xmm)
136         pushl $0; TRAP(T_XMMFLT)
137
138         /*
139          * alltraps entry point.  Interrupts are enabled if this was a trap
140          * gate (TGT), else disabled if this was an interrupt gate (IGT).
141          * Note that int0x80_syscall is a trap gate.   Interrupt gates are
142          * used by page faults, non-maskable interrupts, debug and breakpoint
143          * exceptions.
144          */
145
146         SUPERALIGN_TEXT
147         .globl  alltraps
148         .type   alltraps,@function
149 alltraps:
150         pushal
151         pushl   %ds
152         pushl   %es
153         pushl   %fs
154 alltraps_with_regs_pushed:
155         SET_KERNEL_SREGS
156         FAKE_MCOUNT(TF_EIP(%esp))
157 calltrap:
158         pushl   %esp
159         call    trap
160         add     $4, %esp
161         
162         /*
163          * Return via doreti to handle ASTs.
164          */
165         MEXITCOUNT
166         jmp     doreti
167
168 /*
169  * Privileged instruction fault.
170  */
171 #ifdef KDTRACE_HOOKS
172         SUPERALIGN_TEXT
173 IDTVEC(ill)
174         /* Check if there is no DTrace hook registered. */
175         cmpl    $0,dtrace_invop_jump_addr
176         je      norm_ill
177
178         /* Check if this is a user fault. */
179         cmpl    $GSEL_KPL, 4(%esp)      /* Check the code segment. */
180               
181         /* If so, just handle it as a normal trap. */
182         jne     norm_ill
183               
184         /*
185          * This is a kernel instruction fault that might have been caused
186          * by a DTrace provider.
187          */
188         pushal                          /* Push all registers onto the stack. */
189
190         /*
191          * Set our jump address for the jump back in the event that
192          * the exception wasn't caused by DTrace at all.
193          */
194         movl    $norm_ill, dtrace_invop_calltrap_addr
195
196         /* Jump to the code hooked in by DTrace. */
197         jmpl    *dtrace_invop_jump_addr
198
199         /*
200          * Process the instruction fault in the normal way.
201          */
202 norm_ill:
203         pushl $0
204         TRAP(T_PRIVINFLT)
205 #endif
206
207 /*
208  * SYSCALL CALL GATE (old entry point for a.out binaries)
209  *
210  * The intersegment call has been set up to specify one dummy parameter.
211  *
212  * This leaves a place to put eflags so that the call frame can be
213  * converted to a trap frame. Note that the eflags is (semi-)bogusly
214  * pushed into (what will be) tf_err and then copied later into the
215  * final spot. It has to be done this way because esp can't be just
216  * temporarily altered for the pushfl - an interrupt might come in
217  * and clobber the saved cs/eip.
218  */
219         SUPERALIGN_TEXT
220 IDTVEC(lcall_syscall)
221         pushfl                          /* save eflags */
222         popl    8(%esp)                 /* shuffle into tf_eflags */
223         pushl   $7                      /* sizeof "lcall 7,0" */
224         subl    $4,%esp                 /* skip over tf_trapno */
225         pushal
226         pushl   %ds
227         pushl   %es
228         pushl   %fs
229         SET_KERNEL_SREGS
230         FAKE_MCOUNT(TF_EIP(%esp))
231         pushl   %esp
232         call    syscall
233         add     $4, %esp
234         MEXITCOUNT
235         jmp     doreti
236
237 /*
238  * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
239  *
240  * Even though the name says 'int0x80', this is actually a TGT (trap gate)
241  * rather then an IGT (interrupt gate).  Thus interrupts are enabled on
242  * entry just as they are for a normal syscall.
243  */
244         SUPERALIGN_TEXT
245 IDTVEC(int0x80_syscall)
246         pushl   $2                      /* sizeof "int 0x80" */
247         subl    $4,%esp                 /* skip over tf_trapno */
248         pushal
249         pushl   %ds
250         pushl   %es
251         pushl   %fs
252         SET_KERNEL_SREGS
253         FAKE_MCOUNT(TF_EIP(%esp))
254         pushl   %esp
255         call    syscall
256         add     $4, %esp
257         MEXITCOUNT
258         jmp     doreti
259
260 ENTRY(fork_trampoline)
261         pushl   %esp                    /* trapframe pointer */
262         pushl   %ebx                    /* arg1 */
263         pushl   %esi                    /* function */
264         call    fork_exit
265         addl    $12,%esp
266         /* cut from syscall */
267
268         /*
269          * Return via doreti to handle ASTs.
270          */
271         MEXITCOUNT
272         jmp     doreti
273
274
275 /*
276  * To efficiently implement classification of trap and interrupt handlers
277  * for profiling, there must be only trap handlers between the labels btrap
278  * and bintr, and only interrupt handlers between the labels bintr and
279  * eintr.  This is implemented (partly) by including files that contain
280  * some of the handlers.  Before including the files, set up a normal asm
281  * environment so that the included files doen't need to know that they are
282  * included.
283  */
284
285         .data
286         .p2align 4
287         .text
288         SUPERALIGN_TEXT
289 MCOUNT_LABEL(bintr)
290
291 #include <i386/isa/atpic_vector.s>
292
293 #ifdef DEV_APIC
294         .data
295         .p2align 4
296         .text
297         SUPERALIGN_TEXT
298
299 #include <i386/i386/apic_vector.s>
300 #endif
301
302         .data
303         .p2align 4
304         .text
305         SUPERALIGN_TEXT
306 #include <i386/i386/vm86bios.s>
307
308         .text
309 MCOUNT_LABEL(eintr)
310
311 /*
312  * void doreti(struct trapframe)
313  *
314  * Handle return from interrupts, traps and syscalls.
315  */
316         .text
317         SUPERALIGN_TEXT
318         .type   doreti,@function
319 doreti:
320         FAKE_MCOUNT($bintr)             /* init "from" bintr -> doreti */
321 doreti_next:
322         /*
323          * Check if ASTs can be handled now.  PSL_VM must be checked first
324          * since segment registers only have an RPL in non-VM86 mode.
325          */
326         testl   $PSL_VM,TF_EFLAGS(%esp) /* are we in vm86 mode? */
327         jz      doreti_notvm86
328         movl    PCPU(CURPCB),%ecx
329         testl   $PCB_VM86CALL,PCB_FLAGS(%ecx)   /* are we in a vm86 call? */
330         jz      doreti_ast              /* can handle ASTS now if not */
331         jmp     doreti_exit
332
333 doreti_notvm86:
334         testb   $SEL_RPL_MASK,TF_CS(%esp) /* are we returning to user mode? */
335         jz      doreti_exit             /* can't handle ASTs now if not */
336
337 doreti_ast:
338         /*
339          * Check for ASTs atomically with returning.  Disabling CPU
340          * interrupts provides sufficient locking even in the SMP case,
341          * since we will be informed of any new ASTs by an IPI.
342          */
343         cli
344         movl    PCPU(CURTHREAD),%eax
345         testl   $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%eax)
346         je      doreti_exit
347         sti
348         pushl   %esp                    /* pass a pointer to the trapframe */
349         call    ast
350         add     $4,%esp
351         jmp     doreti_ast
352
353         /*
354          * doreti_exit: pop registers, iret.
355          *
356          *      The segment register pop is a special case, since it may
357          *      fault if (for example) a sigreturn specifies bad segment
358          *      registers.  The fault is handled in trap.c.
359          */
360 doreti_exit:
361         MEXITCOUNT
362
363         .globl  doreti_popl_fs
364 doreti_popl_fs:
365         popl    %fs
366         .globl  doreti_popl_es
367 doreti_popl_es:
368         popl    %es
369         .globl  doreti_popl_ds
370 doreti_popl_ds:
371         popl    %ds
372         popal
373         addl    $8,%esp
374         .globl  doreti_iret
375 doreti_iret:
376         iret
377
378         /*
379          * doreti_iret_fault and friends.  Alternative return code for
380          * the case where we get a fault in the doreti_exit code
381          * above.  trap() (i386/i386/trap.c) catches this specific
382          * case, sends the process a signal and continues in the
383          * corresponding place in the code below.
384          */
385         ALIGN_TEXT
386         .globl  doreti_iret_fault
387 doreti_iret_fault:
388         subl    $8,%esp
389         pushal
390         pushl   %ds
391         .globl  doreti_popl_ds_fault
392 doreti_popl_ds_fault:
393         pushl   %es
394         .globl  doreti_popl_es_fault
395 doreti_popl_es_fault:
396         pushl   %fs
397         .globl  doreti_popl_fs_fault
398 doreti_popl_fs_fault:
399         movl    $0,TF_ERR(%esp) /* XXX should be the error code */
400         movl    $T_PROTFLT,TF_TRAPNO(%esp)
401         jmp     alltraps_with_regs_pushed