interrupts_and_traps.c

来自「linux 内核源代码」· C语言 代码 · 共 509 行 · 第 1/2 页

C
509
字号
/*P:800 Interrupts (traps) are complicated enough to earn their own file. * There are three classes of interrupts: * * 1) Real hardware interrupts which occur while we're running the Guest, * 2) Interrupts for virtual devices attached to the Guest, and * 3) Traps and faults from the Guest. * * Real hardware interrupts must be delivered to the Host, not the Guest. * Virtual interrupts must be delivered to the Guest, but we make them look * just like real hardware would deliver them.  Traps from the Guest can be set * up to go directly back into the Guest, but sometimes the Host wants to see * them first, so we also have a way of "reflecting" them into the Guest as if * they had been delivered to it directly. :*/#include <linux/uaccess.h>#include <linux/interrupt.h>#include <linux/module.h>#include "lg.h"/* Allow Guests to use a non-128 (ie. non-Linux) syscall trap. */static unsigned int syscall_vector = SYSCALL_VECTOR;module_param(syscall_vector, uint, 0444);/* The address of the interrupt handler is split into two bits: */static unsigned long idt_address(u32 lo, u32 hi){	return (lo & 0x0000FFFF) | (hi & 0xFFFF0000);}/* The "type" of the interrupt handler is a 4 bit field: we only support a * couple of types. */static int idt_type(u32 lo, u32 hi){	return (hi >> 8) & 0xF;}/* An IDT entry can't be used unless the "present" bit is set. */static int idt_present(u32 lo, u32 hi){	return (hi & 0x8000);}/* We need a helper to "push" a value onto the Guest's stack, since that's a * big part of what delivering an interrupt does. */static void push_guest_stack(struct lguest *lg, unsigned long *gstack, u32 val){	/* Stack grows upwards: move stack then write value. */	*gstack -= 4;	lgwrite(lg, *gstack, u32, val);}/*H:210 The set_guest_interrupt() routine actually delivers the interrupt or * trap.  The mechanics of delivering traps and interrupts to the Guest are the * same, except some traps have an "error code" which gets pushed onto the * stack as well: the caller tells us if this is one. * * "lo" and "hi" are the two parts of the Interrupt Descriptor Table for this * interrupt or trap.  It's split into two parts for traditional reasons: gcc * on i386 used to be frightened by 64 bit numbers. * * We set up the stack just like the CPU does for a real interrupt, so it's * identical for the Guest (and the standard "iret" instruction will undo * it). */static void set_guest_interrupt(struct lguest *lg, u32 lo, u32 hi, int has_err){	unsigned long gstack, origstack;	u32 eflags, ss, irq_enable;	unsigned long virtstack;	/* There are two cases for interrupts: one where the Guest is already	 * in the kernel, and a more complex one where the Guest is in	 * userspace.  We check the privilege level to find out. */	if ((lg->regs->ss&0x3) != GUEST_PL) {		/* The Guest told us their kernel stack with the SET_STACK		 * hypercall: both the virtual address and the segment */		virtstack = lg->esp1;		ss = lg->ss1;		origstack = gstack = guest_pa(lg, virtstack);		/* We push the old stack segment and pointer onto the new		 * stack: when the Guest does an "iret" back from the interrupt		 * handler the CPU will notice they're dropping privilege		 * levels and expect these here. */		push_guest_stack(lg, &gstack, lg->regs->ss);		push_guest_stack(lg, &gstack, lg->regs->esp);	} else {		/* We're staying on the same Guest (kernel) stack. */		virtstack = lg->regs->esp;		ss = lg->regs->ss;		origstack = gstack = guest_pa(lg, virtstack);	}	/* Remember that we never let the Guest actually disable interrupts, so	 * the "Interrupt Flag" bit is always set.  We copy that bit from the	 * Guest's "irq_enabled" field into the eflags word: we saw the Guest	 * copy it back in "lguest_iret". */	eflags = lg->regs->eflags;	if (get_user(irq_enable, &lg->lguest_data->irq_enabled) == 0	    && !(irq_enable & X86_EFLAGS_IF))		eflags &= ~X86_EFLAGS_IF;	/* An interrupt is expected to push three things on the stack: the old	 * "eflags" word, the old code segment, and the old instruction	 * pointer. */	push_guest_stack(lg, &gstack, eflags);	push_guest_stack(lg, &gstack, lg->regs->cs);	push_guest_stack(lg, &gstack, lg->regs->eip);	/* For the six traps which supply an error code, we push that, too. */	if (has_err)		push_guest_stack(lg, &gstack, lg->regs->errcode);	/* Now we've pushed all the old state, we change the stack, the code	 * segment and the address to execute. */	lg->regs->ss = ss;	lg->regs->esp = virtstack + (gstack - origstack);	lg->regs->cs = (__KERNEL_CS|GUEST_PL);	lg->regs->eip = idt_address(lo, hi);	/* There are two kinds of interrupt handlers: 0xE is an "interrupt	 * gate" which expects interrupts to be disabled on entry. */	if (idt_type(lo, hi) == 0xE)		if (put_user(0, &lg->lguest_data->irq_enabled))			kill_guest(lg, "Disabling interrupts");}/*H:205 * Virtual Interrupts. * * maybe_do_interrupt() gets called before every entry to the Guest, to see if * we should divert the Guest to running an interrupt handler. */void maybe_do_interrupt(struct lguest *lg){	unsigned int irq;	DECLARE_BITMAP(blk, LGUEST_IRQS);	struct desc_struct *idt;	/* If the Guest hasn't even initialized yet, we can do nothing. */	if (!lg->lguest_data)		return;	/* Take our "irqs_pending" array and remove any interrupts the Guest	 * wants blocked: the result ends up in "blk". */	if (copy_from_user(&blk, lg->lguest_data->blocked_interrupts,			   sizeof(blk)))		return;	bitmap_andnot(blk, lg->irqs_pending, blk, LGUEST_IRQS);	/* Find the first interrupt. */	irq = find_first_bit(blk, LGUEST_IRQS);	/* None?  Nothing to do */	if (irq >= LGUEST_IRQS)		return;	/* They may be in the middle of an iret, where they asked us never to	 * deliver interrupts. */	if (lg->regs->eip >= lg->noirq_start && lg->regs->eip < lg->noirq_end)		return;	/* If they're halted, interrupts restart them. */	if (lg->halted) {		/* Re-enable interrupts. */		if (put_user(X86_EFLAGS_IF, &lg->lguest_data->irq_enabled))			kill_guest(lg, "Re-enabling interrupts");		lg->halted = 0;	} else {		/* Otherwise we check if they have interrupts disabled. */		u32 irq_enabled;		if (get_user(irq_enabled, &lg->lguest_data->irq_enabled))			irq_enabled = 0;		if (!irq_enabled)			return;	}	/* Look at the IDT entry the Guest gave us for this interrupt.  The	 * first 32 (FIRST_EXTERNAL_VECTOR) entries are for traps, so we skip	 * over them. */	idt = &lg->arch.idt[FIRST_EXTERNAL_VECTOR+irq];	/* If they don't have a handler (yet?), we just ignore it */	if (idt_present(idt->a, idt->b)) {		/* OK, mark it no longer pending and deliver it. */		clear_bit(irq, lg->irqs_pending);		/* set_guest_interrupt() takes the interrupt descriptor and a		 * flag to say whether this interrupt pushes an error code onto		 * the stack as well: virtual interrupts never do. */		set_guest_interrupt(lg, idt->a, idt->b, 0);	}	/* Every time we deliver an interrupt, we update the timestamp in the	 * Guest's lguest_data struct.  It would be better for the Guest if we	 * did this more often, but it can actually be quite slow: doing it	 * here is a compromise which means at least it gets updated every	 * timer interrupt. */	write_timestamp(lg);}/*:*//* Linux uses trap 128 for system calls.  Plan9 uses 64, and Ron Minnich sent * me a patch, so we support that too.  It'd be a big step for lguest if half * the Plan 9 user base were to start using it. * * Actually now I think of it, it's possible that Ron *is* half the Plan 9 * userbase.  Oh well. */static bool could_be_syscall(unsigned int num){	/* Normal Linux SYSCALL_VECTOR or reserved vector? */	return num == SYSCALL_VECTOR || num == syscall_vector;}/* The syscall vector it wants must be unused by Host. */bool check_syscall_vector(struct lguest *lg){	u32 vector;	if (get_user(vector, &lg->lguest_data->syscall_vec))		return false;	return could_be_syscall(vector);}int init_interrupts(void){	/* If they want some strange system call vector, reserve it now */	if (syscall_vector != SYSCALL_VECTOR	    && test_and_set_bit(syscall_vector, used_vectors)) {		printk("lg: couldn't reserve syscall %u\n", syscall_vector);		return -EBUSY;	}	return 0;}void free_interrupts(void){	if (syscall_vector != SYSCALL_VECTOR)		clear_bit(syscall_vector, used_vectors);}/*H:220 Now we've got the routines to deliver interrupts, delivering traps * like page fault is easy.  The only trick is that Intel decided that some * traps should have error codes: */static int has_err(unsigned int trap){	return (trap == 8 || (trap >= 10 && trap <= 14) || trap == 17);}/* deliver_trap() returns true if it could deliver the trap. */int deliver_trap(struct lguest *lg, unsigned int num){	/* Trap numbers are always 8 bit, but we set an impossible trap number	 * for traps inside the Switcher, so check that here. */	if (num >= ARRAY_SIZE(lg->arch.idt))		return 0;	/* Early on the Guest hasn't set the IDT entries (or maybe it put a

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?