2 * Copyright (c) 1989, 1990 William F. Jolitz.
3 * Copyright (c) 1990 The Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
36 #include <machine/asmacros.h>
37 #include <machine/psl.h>
38 #include <machine/trap.h>
43 #define SEL_RPL_MASK 0x0002
44 #define __HYPERVISOR_iret 23
46 /* Offsets into shared_info_t. */
47 #define evtchn_upcall_pending /* 0 */
48 #define evtchn_upcall_mask 1
49 #define XEN_BLOCK_EVENTS(reg) movb $1,evtchn_upcall_mask(reg)
50 #define XEN_UNBLOCK_EVENTS(reg) movb $0,evtchn_upcall_mask(reg)
51 #define XEN_TEST_PENDING(reg) testb $0x1,evtchn_upcall_pending(reg)
65 /*****************************************************************************/
67 /*****************************************************************************/
69 * Trap and fault vector routines.
71 * Most traps are 'trap gates', SDT_SYS386TGT. A trap gate pushes state on
72 * the stack that mostly looks like an interrupt, but does not disable
73 * interrupts. A few of the traps we are use are interrupt gates,
74 * SDT_SYS386IGT, which are nearly the same thing except interrupts are
77 * The cpu will push a certain amount of state onto the kernel stack for
78 * the current process. The amount of state depends on the type of trap
79 * and whether the trap crossed rings or not. See i386/include/frame.h.
80 * At the very least the current EFLAGS (status register, which includes
81 * the interrupt disable state prior to the trap), the code segment register,
82 * and the return instruction pointer are pushed by the cpu. The cpu
83 * will also push an 'error' code for certain traps. We push a dummy
84 * error code for those traps where the cpu doesn't in order to maintain
85 * a consistent frame. We also push a contrived 'trap number'.
87 * The cpu does not push the general registers, we must do that, and we
88 * must restore them prior to calling 'iret'. The cpu adjusts the %cs and
89 * %ss segment registers, but does not mess with %ds, %es, or %fs. Thus we
90 * must load them with appropriate values for supervisor mode operation.
96 #define TRAP(a) pushl $(a) ; jmp alltraps
99 pushl $0; TRAP(T_DIVIDE)
101 pushl $0; TRAP(T_TRCTRAP)
103 pushl $0; TRAP(T_NMI)
105 pushl $0; TRAP(T_BPTFLT)
107 pushl $0; TRAP(T_OFLOW)
109 pushl $0; TRAP(T_BOUND)
111 pushl $0; TRAP(T_PRIVINFLT)
113 pushl $0; TRAP(T_DNA)
115 pushl $0; TRAP(T_FPOPFLT)
127 pushl $0; TRAP(T_MCHK)
129 pushl $0; TRAP(T_RESERVED)
131 pushl $0; TRAP(T_ARITHTRAP)
135 pushl $0; TRAP(T_XMMFLT)
137 IDTVEC(hypervisor_callback)
144 upcall_with_regs_pushed:
146 FAKE_MCOUNT(TF_EIP(%esp))
148 movl TF_EIP(%esp),%eax
152 jb critical_region_fixup
155 call evtchn_do_upcall
159 * Return via doreti to handle ASTs.
165 hypervisor_callback_pending:
166 movl HYPERVISOR_shared_info,%esi
167 XEN_BLOCK_EVENTS(%esi) /* cli */
171 * alltraps entry point. Interrupts are enabled if this was a trap
172 * gate (TGT), else disabled if this was an interrupt gate (IGT).
173 * Note that int0x80_syscall is a trap gate. Only page faults
174 * use an interrupt gate.
179 .type alltraps,@function
186 alltraps_with_regs_pushed:
188 FAKE_MCOUNT(TF_EIP(%esp))
196 * Return via doreti to handle ASTs.
202 * SYSCALL CALL GATE (old entry point for a.out binaries)
204 * The intersegment call has been set up to specify one dummy parameter.
206 * This leaves a place to put eflags so that the call frame can be
207 * converted to a trap frame. Note that the eflags is (semi-)bogusly
208 * pushed into (what will be) tf_err and then copied later into the
209 * final spot. It has to be done this way because esp can't be just
210 * temporarily altered for the pushfl - an interrupt might come in
211 * and clobber the saved cs/eip.
214 IDTVEC(lcall_syscall)
215 pushfl /* save eflags */
216 popl 8(%esp) /* shuffle into tf_eflags */
217 pushl $7 /* sizeof "lcall 7,0" */
218 subl $4,%esp /* skip over tf_trapno */
224 FAKE_MCOUNT(TF_EIP(%esp))
232 * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
234 * Even though the name says 'int0x80', this is actually a TGT (trap gate)
235 * rather then an IGT (interrupt gate). Thus interrupts are enabled on
236 * entry just as they are for a normal syscall.
239 IDTVEC(int0x80_syscall)
240 pushl $2 /* sizeof "int 0x80" */
241 pushl $0xBEEF /* for debug */
247 FAKE_MCOUNT(TF_EIP(%esp))
254 ENTRY(fork_trampoline)
255 pushl %esp /* trapframe pointer */
256 pushl %ebx /* arg1 */
257 pushl %esi /* function */
260 /* cut from syscall */
263 * Return via doreti to handle ASTs.
270 * To efficiently implement classification of trap and interrupt handlers
271 * for profiling, there must be only trap handlers between the labels btrap
272 * and bintr, and only interrupt handlers between the labels bintr and
273 * eintr. This is implemented (partly) by including files that contain
274 * some of the handlers. Before including the files, set up a normal asm
275 * environment so that the included files doen't need to know that they are
286 #include <i386/isa/atpic_vector.s>
295 #include <i386/i386/apic_vector.s>
302 #include <i386/i386/vm86bios.s>
308 * void doreti(struct trapframe)
310 * Handle return from interrupts, traps and syscalls.
314 .type doreti,@function
316 FAKE_MCOUNT($bintr) /* init "from" bintr -> doreti */
320 * Check if ASTs can be handled now. PSL_VM must be checked first
321 * since segment registers only have an RPL in non-VM86 mode.
323 testl $PSL_VM,TF_EFLAGS(%esp) /* are we in vm86 mode? */
325 movl PCPU(CURPCB),%ecx
326 testl $PCB_VM86CALL,PCB_FLAGS(%ecx) /* are we in a vm86 call? */
327 jz doreti_ast /* can handle ASTS now if not */
332 testb $SEL_RPL_MASK,TF_CS(%esp) /* are we returning to user mode? */
333 jz doreti_exit /* can't handle ASTs now if not */
337 * Check for ASTs atomically with returning. Disabling CPU
338 * interrupts provides sufficient locking even in the SMP case,
339 * since we will be informed of any new ASTs by an IPI.
341 movl HYPERVISOR_shared_info,%esi
342 XEN_BLOCK_EVENTS(%esi) /* cli */
343 movl PCPU(CURTHREAD),%eax
344 testl $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%eax)
346 XEN_UNBLOCK_EVENTS(%esi) /* sti */
347 pushl %esp /* pass a pointer to the trapframe */
353 * doreti_exit: pop registers, iret.
355 * The segment register pop is a special case, since it may
356 * fault if (for example) a sigreturn specifies bad segment
357 * registers. The fault is handled in trap.c.
360 movl HYPERVISOR_shared_info,%esi
361 XEN_UNBLOCK_EVENTS(%esi) # reenable event callbacks (sti)
365 XEN_TEST_PENDING(%esi)
366 jnz hypervisor_callback_pending /* More to go */
370 .globl doreti_popl_fs
373 .globl doreti_popl_es
376 .globl doreti_popl_ds
381 * This is important: as nothing is atomic over here (we can get
382 * interrupted any time), we use the critical_region_fixup() in
383 * order to figure out where out stack is. Therefore, do NOT use
384 * 'popal' here without fixing up the table!
390 jmp hypercall_page + (__HYPERVISOR_iret * 32)
394 * doreti_iret_fault and friends. Alternative return code for
395 * the case where we get a fault in the doreti_exit code
396 * above. trap() (i386/i386/trap.c) catches this specific
397 * case, sends the process a signal and continues in the
398 * corresponding place in the code below.
401 .globl doreti_iret_fault
406 .globl doreti_popl_ds_fault
407 doreti_popl_ds_fault:
409 .globl doreti_popl_es_fault
410 doreti_popl_es_fault:
412 .globl doreti_popl_fs_fault
413 doreti_popl_fs_fault:
414 movl $0,TF_ERR(%esp) /* XXX should be the error code */
415 movl $T_PROTFLT,TF_TRAPNO(%esp)
416 jmp alltraps_with_regs_pushed
419 # [How we do the fixup]. We want to merge the current stack frame with the
420 # just-interrupted frame. How we do this depends on where in the critical
421 # region the interrupted handler was executing, and so how many saved
422 # registers are in each frame. We do this quickly using the lookup table
423 # 'critical_fixup_table'. For each byte offset in the critical region, it
424 # provides the number of bytes which have already been popped from the
425 # interrupted stack frame.
428 .globl critical_region_fixup
429 critical_region_fixup:
430 addl $critical_fixup_table-scrit,%eax
431 movzbl (%eax),%eax # %eax contains num bytes popped
433 add %eax,%esi # %esi points at end of src region
435 add $0x40,%edi # %edi points at end of dst region
437 shr $2,%ecx # convert bytes to words
438 je 16f # skip loop if nothing to copy
439 15: subl $4,%esi # pre-decrementing copy loop
444 16: movl %edi,%esp # final %edi is top of merged stack
445 jmp hypervisor_callback_pending
448 critical_fixup_table:
449 .byte 0x0,0x0,0x0 #testb $0x1,(%esi)
450 .byte 0x0,0x0,0x0,0x0,0x0,0x0 #jne ea
451 .byte 0x0,0x0 #pop %fs
462 .byte 0x2c,0x2c,0x2c #add $0x8,%esp
466 .byte 0x34,0x34,0x34,0x34,0x34 #HYPERVISOR_iret
469 /* # Hypervisor uses this for application faults while it executes.*/
470 ENTRY(failsafe_callback)
472 call xen_failsafe_handler
473 /*# call install_safe_pf_handler */
482 /*# call install_normal_pf_handler */