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 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
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/5.1/sys/i386/i386/exception.s 111032 2003-02-17 09:55:10Z julian $
35 */
36
37 #include "opt_npx.h"
38
39 #include <machine/asmacros.h>
40 #include <sys/mutex.h>
41 #include <machine/psl.h>
42 #include <machine/trap.h>
43 #ifdef SMP
44 #include <machine/smptests.h> /** various SMP options */
45 #endif
46
47 #include "assym.s"
48
49 #define SEL_RPL_MASK 0x0003
50
51 .text
52
53 /*****************************************************************************/
54 /* Trap handling */
55 /*****************************************************************************/
56 /*
57 * Trap and fault vector routines.
58 *
59 * Most traps are 'trap gates', SDT_SYS386TGT. A trap gate pushes state on
60 * the stack that mostly looks like an interrupt, but does not disable
61 * interrupts. A few of the traps we are use are interrupt gates,
62 * SDT_SYS386IGT, which are nearly the same thing except interrupts are
63 * disabled on entry.
64 *
65 * The cpu will push a certain amount of state onto the kernel stack for
66 * the current process. The amount of state depends on the type of trap
67 * and whether the trap crossed rings or not. See i386/include/frame.h.
68 * At the very least the current EFLAGS (status register, which includes
69 * the interrupt disable state prior to the trap), the code segment register,
70 * and the return instruction pointer are pushed by the cpu. The cpu
71 * will also push an 'error' code for certain traps. We push a dummy
72 * error code for those traps where the cpu doesn't in order to maintain
73 * a consistent frame. We also push a contrived 'trap number'.
74 *
75 * The cpu does not push the general registers, we must do that, and we
76 * must restore them prior to calling 'iret'. The cpu adjusts the %cs and
77 * %ss segment registers, but does not mess with %ds, %es, or %fs. Thus we
78 * must load them with appropriate values for supervisor mode operation.
79 */
80 #define IDTVEC(name) ALIGN_TEXT; .globl __CONCAT(X,name); \
81 .type __CONCAT(X,name),@function; __CONCAT(X,name):
82 #define TRAP(a) pushl $(a) ; jmp alltraps
83
84 MCOUNT_LABEL(user)
85 MCOUNT_LABEL(btrap)
86
87 IDTVEC(div)
88 pushl $0; TRAP(T_DIVIDE)
89 IDTVEC(dbg)
90 pushl $0; TRAP(T_TRCTRAP)
91 IDTVEC(nmi)
92 pushl $0; TRAP(T_NMI)
93 IDTVEC(bpt)
94 pushl $0; TRAP(T_BPTFLT)
95 IDTVEC(ofl)
96 pushl $0; TRAP(T_OFLOW)
97 IDTVEC(bnd)
98 pushl $0; TRAP(T_BOUND)
99 IDTVEC(ill)
100 pushl $0; TRAP(T_PRIVINFLT)
101 IDTVEC(dna)
102 pushl $0; TRAP(T_DNA)
103 IDTVEC(fpusegm)
104 pushl $0; TRAP(T_FPOPFLT)
105 IDTVEC(tss)
106 TRAP(T_TSSFLT)
107 IDTVEC(missing)
108 TRAP(T_SEGNPFLT)
109 IDTVEC(stk)
110 TRAP(T_STKFLT)
111 IDTVEC(prot)
112 TRAP(T_PROTFLT)
113 IDTVEC(page)
114 TRAP(T_PAGEFLT)
115 IDTVEC(mchk)
116 pushl $0; TRAP(T_MCHK)
117 IDTVEC(rsvd)
118 pushl $0; TRAP(T_RESERVED)
119 IDTVEC(fpu)
120 pushl $0; TRAP(T_ARITHTRAP)
121 IDTVEC(align)
122 TRAP(T_ALIGNFLT)
123
124 IDTVEC(xmm)
125 pushl $0; TRAP(T_XMMFLT)
126
127 /*
128 * alltraps entry point. Interrupts are enabled if this was a trap
129 * gate (TGT), else disabled if this was an interrupt gate (IGT).
130 * Note that int0x80_syscall is a trap gate. Only page faults
131 * use an interrupt gate.
132 */
133
134 SUPERALIGN_TEXT
135 .globl alltraps
136 .type alltraps,@function
137 alltraps:
138 pushal
139 pushl %ds
140 pushl %es
141 pushl %fs
142 alltraps_with_regs_pushed:
143 mov $KDSEL,%ax
144 mov %ax,%ds
145 mov %ax,%es
146 mov $KPSEL,%ax
147 mov %ax,%fs
148 FAKE_MCOUNT(13*4(%esp))
149 calltrap:
150 FAKE_MCOUNT(btrap) /* init "from" btrap -> calltrap */
151 call trap
152
153 /*
154 * Return via doreti to handle ASTs.
155 */
156 MEXITCOUNT
157 jmp doreti
158
159 /*
160 * SYSCALL CALL GATE (old entry point for a.out binaries)
161 *
162 * The intersegment call has been set up to specify one dummy parameter.
163 *
164 * This leaves a place to put eflags so that the call frame can be
165 * converted to a trap frame. Note that the eflags is (semi-)bogusly
166 * pushed into (what will be) tf_err and then copied later into the
167 * final spot. It has to be done this way because esp can't be just
168 * temporarily altered for the pushfl - an interrupt might come in
169 * and clobber the saved cs/eip.
170 */
171 SUPERALIGN_TEXT
172 IDTVEC(lcall_syscall)
173 pushfl /* save eflags */
174 popl 8(%esp) /* shuffle into tf_eflags */
175 pushl $7 /* sizeof "lcall 7,0" */
176 subl $4,%esp /* skip over tf_trapno */
177 pushal
178 pushl %ds
179 pushl %es
180 pushl %fs
181 mov $KDSEL,%ax /* switch to kernel segments */
182 mov %ax,%ds
183 mov %ax,%es
184 mov $KPSEL,%ax
185 mov %ax,%fs
186 FAKE_MCOUNT(13*4(%esp))
187 call syscall
188 MEXITCOUNT
189 jmp doreti
190
191 /*
192 * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
193 *
194 * Even though the name says 'int0x80', this is actually a TGT (trap gate)
195 * rather then an IGT (interrupt gate). Thus interrupts are enabled on
196 * entry just as they are for a normal syscall.
197 */
198 SUPERALIGN_TEXT
199 IDTVEC(int0x80_syscall)
200 pushl $2 /* sizeof "int 0x80" */
201 subl $4,%esp /* skip over tf_trapno */
202 pushal
203 pushl %ds
204 pushl %es
205 pushl %fs
206 mov $KDSEL,%ax /* switch to kernel segments */
207 mov %ax,%ds
208 mov %ax,%es
209 mov $KPSEL,%ax
210 mov %ax,%fs
211 FAKE_MCOUNT(13*4(%esp))
212 call syscall
213 MEXITCOUNT
214 jmp doreti
215
216 ENTRY(fork_trampoline)
217 pushl %esp /* trapframe pointer */
218 pushl %ebx /* arg1 */
219 pushl %esi /* function */
220 movl PCPU(CURTHREAD),%ebx /* setup critnest */
221 movl $1,TD_CRITNEST(%ebx)
222 sti /* enable interrupts */
223 call fork_exit
224 addl $12,%esp
225 /* cut from syscall */
226
227 /*
228 * Return via doreti to handle ASTs.
229 */
230 MEXITCOUNT
231 jmp doreti
232
233
234 /*
235 * Include vm86 call routines, which want to call doreti.
236 */
237 #include "i386/i386/vm86bios.s"
238
239 /*
240 * Include what was once config+isa-dependent code.
241 * XXX it should be in a stand-alone file. It's still icu-dependent and
242 * belongs in i386/isa.
243 */
244 #include "i386/isa/vector.s"
245
246 .data
247 ALIGN_DATA
248
249 /*
250 * void doreti(struct trapframe)
251 *
252 * Handle return from interrupts, traps and syscalls.
253 */
254 .text
255 SUPERALIGN_TEXT
256 .type doreti,@function
257 doreti:
258 FAKE_MCOUNT(bintr) /* init "from" bintr -> doreti */
259 doreti_next:
260 /*
261 * Check if ASTs can be handled now. PSL_VM must be checked first
262 * since segment registers only have an RPL in non-VM86 mode.
263 */
264 testl $PSL_VM,TF_EFLAGS(%esp) /* are we in vm86 mode? */
265 jz doreti_notvm86
266 movl PCPU(CURPCB),%ecx
267 testl $PCB_VM86CALL,PCB_FLAGS(%ecx) /* are we in a vm86 call? */
268 jz doreti_ast /* can handle ASTS now if not */
269 jmp doreti_exit
270
271 doreti_notvm86:
272 testb $SEL_RPL_MASK,TF_CS(%esp) /* are we returning to user mode? */
273 jz doreti_exit /* can't handle ASTs now if not */
274
275 doreti_ast:
276 /*
277 * Check for ASTs atomically with returning. Disabling CPU
278 * interrupts provides sufficient locking evein the SMP case,
279 * since we will be informed of any new ASTs by an IPI.
280 */
281 cli
282 movl PCPU(CURTHREAD),%eax
283 testl $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%eax)
284 je doreti_exit
285 sti
286 pushl %esp /* pass a pointer to the trapframe */
287 call ast
288 add $4,%esp
289 jmp doreti_ast
290
291 /*
292 * doreti_exit: pop registers, iret.
293 *
294 * The segment register pop is a special case, since it may
295 * fault if (for example) a sigreturn specifies bad segment
296 * registers. The fault is handled in trap.c.
297 */
298 doreti_exit:
299 MEXITCOUNT
300
301 .globl doreti_popl_fs
302 doreti_popl_fs:
303 popl %fs
304 .globl doreti_popl_es
305 doreti_popl_es:
306 popl %es
307 .globl doreti_popl_ds
308 doreti_popl_ds:
309 popl %ds
310 popal
311 addl $8,%esp
312 .globl doreti_iret
313 doreti_iret:
314 iret
315
316 /*
317 * doreti_iret_fault and friends. Alternative return code for
318 * the case where we get a fault in the doreti_exit code
319 * above. trap() (i386/i386/trap.c) catches this specific
320 * case, sends the process a signal and continues in the
321 * corresponding place in the code below.
322 */
323 ALIGN_TEXT
324 .globl doreti_iret_fault
325 doreti_iret_fault:
326 subl $8,%esp
327 pushal
328 pushl %ds
329 .globl doreti_popl_ds_fault
330 doreti_popl_ds_fault:
331 pushl %es
332 .globl doreti_popl_es_fault
333 doreti_popl_es_fault:
334 pushl %fs
335 .globl doreti_popl_fs_fault
336 doreti_popl_fs_fault:
337 movl $0,TF_ERR(%esp) /* XXX should be the error code */
338 movl $T_PROTFLT,TF_TRAPNO(%esp)
339 jmp alltraps_with_regs_pushed
340
341 #ifdef APIC_IO
342 #include "i386/isa/apic_ipl.s"
343 #else
344 #include "i386/isa/icu_ipl.s"
345 #endif /* APIC_IO */
Cache object: 79ec5453c643ea22bdc963e3a091e139
|