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>
42 #define SEL_RPL_MASK 0x0002
43 #define __HYPERVISOR_iret 23
45 /* Offsets into shared_info_t. */
47 #define evtchn_upcall_pending /* 0 */
48 #define evtchn_upcall_mask 1
50 #define sizeof_vcpu_shift 6
54 #define GET_VCPU_INFO(reg) movl PCPU(CPUID),reg ; \
55 shl $sizeof_vcpu_shift,reg ; \
56 addl HYPERVISOR_shared_info,reg
58 #define GET_VCPU_INFO(reg) movl HYPERVISOR_shared_info,reg
61 #define __DISABLE_INTERRUPTS(reg) movb $1,evtchn_upcall_mask(reg)
62 #define __ENABLE_INTERRUPTS(reg) movb $0,evtchn_upcall_mask(reg)
63 #define DISABLE_INTERRUPTS(reg) GET_VCPU_INFO(reg) ; \
64 __DISABLE_INTERRUPTS(reg)
65 #define ENABLE_INTERRUPTS(reg) GET_VCPU_INFO(reg) ; \
66 __ENABLE_INTERRUPTS(reg)
67 #define __TEST_PENDING(reg) testb $0xFF,evtchn_upcall_pending(reg)
81 /*****************************************************************************/
83 /*****************************************************************************/
85 * Trap and fault vector routines.
87 * Most traps are 'trap gates', SDT_SYS386TGT. A trap gate pushes state on
88 * the stack that mostly looks like an interrupt, but does not disable
89 * interrupts. A few of the traps we are use are interrupt gates,
90 * SDT_SYS386IGT, which are nearly the same thing except interrupts are
93 * The cpu will push a certain amount of state onto the kernel stack for
94 * the current process. The amount of state depends on the type of trap
95 * and whether the trap crossed rings or not. See i386/include/frame.h.
96 * At the very least the current EFLAGS (status register, which includes
97 * the interrupt disable state prior to the trap), the code segment register,
98 * and the return instruction pointer are pushed by the cpu. The cpu
99 * will also push an 'error' code for certain traps. We push a dummy
100 * error code for those traps where the cpu doesn't in order to maintain
101 * a consistent frame. We also push a contrived 'trap number'.
103 * The cpu does not push the general registers, we must do that, and we
104 * must restore them prior to calling 'iret'. The cpu adjusts the %cs and
105 * %ss segment registers, but does not mess with %ds, %es, or %fs. Thus we
106 * must load them with appropriate values for supervisor mode operation.
112 #define TRAP(a) pushl $(a) ; jmp alltraps
115 pushl $0; TRAP(T_DIVIDE)
117 pushl $0; TRAP(T_TRCTRAP)
119 pushl $0; TRAP(T_NMI)
121 pushl $0; TRAP(T_BPTFLT)
123 pushl $0; TRAP(T_OFLOW)
125 pushl $0; TRAP(T_BOUND)
127 pushl $0; TRAP(T_PRIVINFLT)
129 pushl $0; TRAP(T_DNA)
131 pushl $0; TRAP(T_FPOPFLT)
143 pushl $0; TRAP(T_MCHK)
145 pushl $0; TRAP(T_RESERVED)
147 pushl $0; TRAP(T_ARITHTRAP)
151 pushl $0; TRAP(T_XMMFLT)
153 IDTVEC(hypervisor_callback)
160 upcall_with_regs_pushed:
162 FAKE_MCOUNT(TF_EIP(%esp))
164 movl TF_EIP(%esp),%eax
168 jb critical_region_fixup
171 call xen_intr_handle_upcall
175 * Return via doreti to handle ASTs.
181 hypervisor_callback_pending:
182 DISABLE_INTERRUPTS(%esi) /* cli */
185 * alltraps entry point. Interrupts are enabled if this was a trap
186 * gate (TGT), else disabled if this was an interrupt gate (IGT).
187 * Note that int0x80_syscall is a trap gate. Only page faults
188 * use an interrupt gate.
192 .type alltraps,@function
199 alltraps_with_regs_pushed:
201 FAKE_MCOUNT(TF_EIP(%esp))
209 * Return via doreti to handle ASTs.
215 * SYSCALL CALL GATE (old entry point for a.out binaries)
217 * The intersegment call has been set up to specify one dummy parameter.
219 * This leaves a place to put eflags so that the call frame can be
220 * converted to a trap frame. Note that the eflags is (semi-)bogusly
221 * pushed into (what will be) tf_err and then copied later into the
222 * final spot. It has to be done this way because esp can't be just
223 * temporarily altered for the pushfl - an interrupt might come in
224 * and clobber the saved cs/eip.
227 IDTVEC(lcall_syscall)
228 pushfl /* save eflags */
229 popl 8(%esp) /* shuffle into tf_eflags */
230 pushl $7 /* sizeof "lcall 7,0" */
231 subl $4,%esp /* skip over tf_trapno */
237 FAKE_MCOUNT(TF_EIP(%esp))
245 * Call gate entry for FreeBSD ELF and Linux/NetBSD syscall (int 0x80)
247 * Even though the name says 'int0x80', this is actually a TGT (trap gate)
248 * rather then an IGT (interrupt gate). Thus interrupts are enabled on
249 * entry just as they are for a normal syscall.
252 IDTVEC(int0x80_syscall)
253 pushl $2 /* sizeof "int 0x80" */
254 pushl $0xBEEF /* for debug */
260 FAKE_MCOUNT(TF_EIP(%esp))
267 ENTRY(fork_trampoline)
268 pushl %esp /* trapframe pointer */
269 pushl %ebx /* arg1 */
270 pushl %esi /* function */
273 /* cut from syscall */
276 * Return via doreti to handle ASTs.
283 * To efficiently implement classification of trap and interrupt handlers
284 * for profiling, there must be only trap handlers between the labels btrap
285 * and bintr, and only interrupt handlers between the labels bintr and
286 * eintr. This is implemented (partly) by including files that contain
287 * some of the handlers. Before including the files, set up a normal asm
288 * environment so that the included files doen't need to know that they are
304 #include <i386/i386/apic_vector.s>
311 #include <i386/i386/vm86bios.s>
317 * void doreti(struct trapframe)
319 * Handle return from interrupts, traps and syscalls.
323 .type doreti,@function
325 FAKE_MCOUNT($bintr) /* init "from" bintr -> doreti */
329 * Check if ASTs can be handled now. PSL_VM must be checked first
330 * since segment registers only have an RPL in non-VM86 mode.
332 testl $PSL_VM,TF_EFLAGS(%esp) /* are we in vm86 mode? */
334 movl PCPU(CURPCB),%ecx
335 testl $PCB_VM86CALL,PCB_FLAGS(%ecx) /* are we in a vm86 call? */
336 jz doreti_ast /* can handle ASTS now if not */
341 testb $SEL_RPL_MASK,TF_CS(%esp) /* are we returning to user mode? */
342 jz doreti_exit /* can't handle ASTs now if not */
346 * Check for ASTs atomically with returning. Disabling CPU
347 * interrupts provides sufficient locking even in the SMP case,
348 * since we will be informed of any new ASTs by an IPI.
350 DISABLE_INTERRUPTS(%esi) /* cli */
351 movl PCPU(CURTHREAD),%eax
352 testl $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%eax)
354 ENABLE_INTERRUPTS(%esi) /* sti */
355 pushl %esp /* pass a pointer to the trapframe */
361 * doreti_exit: pop registers, iret.
363 * The segment register pop is a special case, since it may
364 * fault if (for example) a sigreturn specifies bad segment
365 * registers. The fault is handled in trap.c.
368 ENABLE_INTERRUPTS(%esi) # reenable event callbacks (sti)
373 jnz hypervisor_callback_pending /* More to go */
377 .globl doreti_popl_fs
380 .globl doreti_popl_es
383 .globl doreti_popl_ds
388 * This is important: as nothing is atomic over here (we can get
389 * interrupted any time), we use the critical_region_fixup() in
390 * order to figure out where out stack is. Therefore, do NOT use
391 * 'popal' here without fixing up the table!
397 jmp hypercall_page + (__HYPERVISOR_iret * 32)
401 * doreti_iret_fault and friends. Alternative return code for
402 * the case where we get a fault in the doreti_exit code
403 * above. trap() (i386/i386/trap.c) catches this specific
404 * case, sends the process a signal and continues in the
405 * corresponding place in the code below.
408 .globl doreti_iret_fault
413 .globl doreti_popl_ds_fault
414 doreti_popl_ds_fault:
416 .globl doreti_popl_es_fault
417 doreti_popl_es_fault:
419 .globl doreti_popl_fs_fault
420 doreti_popl_fs_fault:
421 movl $0,TF_ERR(%esp) /* XXX should be the error code */
422 movl $T_PROTFLT,TF_TRAPNO(%esp)
423 jmp alltraps_with_regs_pushed
426 # [How we do the fixup]. We want to merge the current stack frame with the
427 # just-interrupted frame. How we do this depends on where in the critical
428 # region the interrupted handler was executing, and so how many saved
429 # registers are in each frame. We do this quickly using the lookup table
430 # 'critical_fixup_table'. For each byte offset in the critical region, it
431 # provides the number of bytes which have already been popped from the
432 # interrupted stack frame.
435 .globl critical_region_fixup
436 critical_region_fixup:
437 addl $critical_fixup_table-scrit,%eax
438 movzbl (%eax),%eax # %eax contains num bytes popped
440 add %eax,%esi # %esi points at end of src region
442 add $0x40,%edi # %edi points at end of dst region
444 shr $2,%ecx # convert bytes to words
445 je 16f # skip loop if nothing to copy
446 15: subl $4,%esi # pre-decrementing copy loop
451 16: movl %edi,%esp # final %edi is top of merged stack
452 jmp hypervisor_callback_pending
455 critical_fixup_table:
456 .byte 0x0,0x0,0x0 #testb $0x1,(%esi)
457 .byte 0x0,0x0,0x0,0x0,0x0,0x0 #jne ea
458 .byte 0x0,0x0 #pop %fs
469 .byte 0x2c,0x2c,0x2c #add $0x8,%esp
473 .byte 0x34,0x34,0x34,0x34,0x34 #HYPERVISOR_iret
476 /* # Hypervisor uses this for application faults while it executes.*/
477 ENTRY(failsafe_callback)
479 call xen_failsafe_handler
480 /*# call install_safe_pf_handler */
489 /*# call install_normal_pf_handler */