]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/lguest/interrupts_and_traps.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/net-2.6
[linux-2.6-omap-h63xx.git] / drivers / lguest / interrupts_and_traps.c
index bd0091bf79ec485edacedcdaf47194e429a35cb3..2b66f79c208b519d19937af625ae64a8b8fc70a5 100644 (file)
  * 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)
 {
@@ -39,7 +45,7 @@ static void push_guest_stack(struct lguest *lg, unsigned long *gstack, u32 val)
 {
        /* Stack grows upwards: move stack then write value. */
        *gstack -= 4;
-       lgwrite_u32(lg, *gstack, val);
+       lgwrite(lg, *gstack, u32, val);
 }
 
 /*H:210 The set_guest_interrupt() routine actually delivers the interrupt or
@@ -56,8 +62,9 @@ static void push_guest_stack(struct lguest *lg, unsigned long *gstack, u32 val)
  * it). */
 static void set_guest_interrupt(struct lguest *lg, u32 lo, u32 hi, int has_err)
 {
-       unsigned long gstack;
+       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
@@ -65,8 +72,10 @@ static void set_guest_interrupt(struct lguest *lg, u32 lo, u32 hi, int has_err)
        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 */
-               gstack = guest_pa(lg, lg->esp1);
+               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
@@ -75,14 +84,16 @@ static void set_guest_interrupt(struct lguest *lg, u32 lo, u32 hi, int has_err)
                push_guest_stack(lg, &gstack, lg->regs->esp);
        } else {
                /* We're staying on the same Guest (kernel) stack. */
-               gstack = guest_pa(lg, lg->regs->esp);
+               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: the Guest copies
-        * it back in "lguest_iret". */
+        * 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))
@@ -102,7 +113,7 @@ static void set_guest_interrupt(struct lguest *lg, u32 lo, u32 hi, int has_err)
        /* 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 = gstack + lg->page_offset;
+       lg->regs->esp = virtstack + (gstack - origstack);
        lg->regs->cs = (__KERNEL_CS|GUEST_PL);
        lg->regs->eip = idt_address(lo, hi);
 
@@ -113,7 +124,7 @@ static void set_guest_interrupt(struct lguest *lg, u32 lo, u32 hi, int has_err)
                        kill_guest(lg, "Disabling interrupts");
 }
 
-/*H:200
+/*H:205
  * Virtual Interrupts.
  *
  * maybe_do_interrupt() gets called before every entry to the Guest, to see if
@@ -165,7 +176,7 @@ void maybe_do_interrupt(struct lguest *lg)
        /* 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->idt[FIRST_EXTERNAL_VECTOR+irq];
+       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. */
@@ -175,6 +186,54 @@ void maybe_do_interrupt(struct lguest *lg)
                 * 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
@@ -188,48 +247,45 @@ static int has_err(unsigned int trap)
 /* deliver_trap() returns true if it could deliver the trap. */
 int deliver_trap(struct lguest *lg, unsigned int num)
 {
-       u32 lo = lg->idt[num].a, hi = lg->idt[num].b;
+       /* 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
         * bogus one in): if we fail here, the Guest will be killed. */
-       if (!idt_present(lo, hi))
+       if (!idt_present(lg->arch.idt[num].a, lg->arch.idt[num].b))
                return 0;
-       set_guest_interrupt(lg, lo, hi, has_err(num));
+       set_guest_interrupt(lg, lg->arch.idt[num].a, lg->arch.idt[num].b,
+                           has_err(num));
        return 1;
 }
 
 /*H:250 Here's the hard part: returning to the Host every time a trap happens
  * and then calling deliver_trap() and re-entering the Guest is slow.
- * Particularly because Guest userspace system calls are traps (trap 128).
+ * Particularly because Guest userspace system calls are traps (usually trap
+ * 128).
  *
  * So we'd like to set up the IDT to tell the CPU to deliver traps directly
  * into the Guest.  This is possible, but the complexities cause the size of
  * this file to double!  However, 150 lines of code is worth writing for taking
  * system calls down from 1750ns to 270ns.  Plus, if lguest didn't do it, all
- * the other hypervisors would tease it.
+ * the other hypervisors would beat it up at lunchtime.
  *
- * This routine determines if a trap can be delivered directly. */
-static int direct_trap(const struct lguest *lg,
-                      const struct desc_struct *trap,
-                      unsigned int num)
+ * This routine indicates if a particular trap number could be delivered
+ * directly. */
+static int direct_trap(unsigned int num)
 {
        /* Hardware interrupts don't go to the Guest at all (except system
         * call). */
-       if (num >= FIRST_EXTERNAL_VECTOR && num != SYSCALL_VECTOR)
+       if (num >= FIRST_EXTERNAL_VECTOR && !could_be_syscall(num))
                return 0;
 
        /* The Host needs to see page faults (for shadow paging and to save the
         * fault address), general protection faults (in/out emulation) and
         * device not available (TS handling), and of course, the hypercall
         * trap. */
-       if (num == 14 || num == 13 || num == 7 || num == LGUEST_TRAP_ENTRY)
-               return 0;
-
-       /* Only trap gates (type 15) can go direct to the Guest.  Interrupt
-        * gates (type 14) disable interrupts as they are entered, which we
-        * never let the Guest do.  Not present entries (type 0x0) also can't
-        * go direct, of course 8) */
-       return idt_type(trap->a, trap->b) == 0xF;
+       return num != 14 && num != 13 && num != 7 && num != LGUEST_TRAP_ENTRY;
 }
 /*:*/
 
@@ -260,8 +316,11 @@ void pin_stack_pages(struct lguest *lg)
        /* Depending on the CONFIG_4KSTACKS option, the Guest can have one or
         * two pages of stack space. */
        for (i = 0; i < lg->stack_pages; i++)
-               /* The stack grows *upwards*, hence the subtraction */
-               pin_page(lg, lg->esp1 - i * PAGE_SIZE);
+               /* The stack grows *upwards*, so the address we're given is the
+                * start of the page after the kernel stack.  Subtract one to
+                * get back onto the first stack page, and keep subtracting to
+                * get to the rest of the stack pages. */
+               pin_page(lg, lg->esp1 - 1 - i * PAGE_SIZE);
 }
 
 /* Direct traps also mean that we need to know whenever the Guest wants to use
@@ -274,7 +333,7 @@ void pin_stack_pages(struct lguest *lg)
  * change stacks on each context switch. */
 void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages)
 {
-       /* You are not allowd have a stack segment with privilege level 0: bad
+       /* You are not allowed have a stack segment with privilege level 0: bad
         * Guest! */
        if ((seg & 0x3) != GUEST_PL)
                kill_guest(lg, "bad stack segment %i", seg);
@@ -293,7 +352,7 @@ void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages)
  * part of the Host: page table handling. */
 
 /*H:235 This is the routine which actually checks the Guest's IDT entry and
- * transfers it into our entry in "struct lguest": */
+ * transfers it into the entry in "struct lguest": */
 static void set_trap(struct lguest *lg, struct desc_struct *trap,
                     unsigned int num, u32 lo, u32 hi)
 {
@@ -335,15 +394,11 @@ void load_guest_idt_entry(struct lguest *lg, unsigned int num, u32 lo, u32 hi)
         * to copy this again. */
        lg->changed |= CHANGED_IDT;
 
-       /* The IDT which we keep in "struct lguest" only contains 32 entries
-        * for the traps and LGUEST_IRQS (32) entries for interrupts.  We
-        * ignore attempts to set handlers for higher interrupt numbers, except
-        * for the system call "interrupt" at 128: we have a special IDT entry
-        * for that. */
-       if (num < ARRAY_SIZE(lg->idt))
-               set_trap(lg, &lg->idt[num], num, lo, hi);
-       else if (num == SYSCALL_VECTOR)
-               set_trap(lg, &lg->syscall_idt, num, lo, hi);
+       /* Check that the Guest doesn't try to step outside the bounds. */
+       if (num >= ARRAY_SIZE(lg->arch.idt))
+               kill_guest(lg, "Setting idt entry %u", num);
+       else
+               set_trap(lg, &lg->arch.idt[num], num, lo, hi);
 }
 
 /* The default entry for each interrupt points into the Switcher routines which
@@ -386,22 +441,35 @@ void copy_traps(const struct lguest *lg, struct desc_struct *idt,
 
        /* We can simply copy the direct traps, otherwise we use the default
         * ones in the Switcher: they will return to the Host. */
-       for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++) {
-               if (direct_trap(lg, &lg->idt[i], i))
-                       idt[i] = lg->idt[i];
+       for (i = 0; i < ARRAY_SIZE(lg->arch.idt); i++) {
+               /* If no Guest can ever override this trap, leave it alone. */
+               if (!direct_trap(i))
+                       continue;
+
+               /* Only trap gates (type 15) can go direct to the Guest.
+                * Interrupt gates (type 14) disable interrupts as they are
+                * entered, which we never let the Guest do.  Not present
+                * entries (type 0x0) also can't go direct, of course. */
+               if (idt_type(lg->arch.idt[i].a, lg->arch.idt[i].b) == 0xF)
+                       idt[i] = lg->arch.idt[i];
                else
+                       /* Reset it to the default. */
                        default_idt_entry(&idt[i], i, def[i]);
        }
-
-       /* Don't forget the system call trap!  The IDT entries for other
-        * interupts never change, so no need to copy them. */
-       i = SYSCALL_VECTOR;
-       if (direct_trap(lg, &lg->syscall_idt, i))
-               idt[i] = lg->syscall_idt;
-       else
-               default_idt_entry(&idt[i], i, def[i]);
 }
 
+/*H:200
+ * The Guest Clock.
+ *
+ * There are two sources of virtual interrupts.  We saw one in lguest_user.c:
+ * the Launcher sending interrupts for virtual devices.  The other is the Guest
+ * timer interrupt.
+ *
+ * The Guest uses the LHCALL_SET_CLOCKEVENT hypercall to tell us how long to
+ * the next timer interrupt (in nanoseconds).  We use the high-resolution timer
+ * infrastructure to set a callback at that time.
+ *
+ * 0 means "turn off the clock". */
 void guest_set_clockevent(struct lguest *lg, unsigned long delta)
 {
        ktime_t expires;
@@ -412,20 +480,27 @@ void guest_set_clockevent(struct lguest *lg, unsigned long delta)
                return;
        }
 
+       /* We use wallclock time here, so the Guest might not be running for
+        * all the time between now and the timer interrupt it asked for.  This
+        * is almost always the right thing to do. */
        expires = ktime_add_ns(ktime_get_real(), delta);
        hrtimer_start(&lg->hrt, expires, HRTIMER_MODE_ABS);
 }
 
+/* This is the function called when the Guest's timer expires. */
 static enum hrtimer_restart clockdev_fn(struct hrtimer *timer)
 {
        struct lguest *lg = container_of(timer, struct lguest, hrt);
 
+       /* Remember the first interrupt is the timer interrupt. */
        set_bit(0, lg->irqs_pending);
+       /* If the Guest is actually stopped, we need to wake it up. */
        if (lg->halted)
                wake_up_process(lg->tsk);
        return HRTIMER_NORESTART;
 }
 
+/* This sets up the timer for this Guest. */
 void init_clockdev(struct lguest *lg)
 {
        hrtimer_init(&lg->hrt, CLOCK_REALTIME, HRTIMER_MODE_ABS);