This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / arch / i386 / kernel / ioport.c
index 3346657..79026f0 100644 (file)
@@ -7,6 +7,7 @@
 
 #include <linux/sched.h>
 #include <linux/kernel.h>
+#include <linux/capability.h>
 #include <linux/errno.h>
 #include <linux/types.h>
 #include <linux/ioport.h>
@@ -56,6 +57,7 @@ static void set_bitmap(unsigned long *bitmap, unsigned int base, unsigned int ex
  */
 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
 {
+       unsigned long i, max_long, bytes, bytes_updated;
        struct thread_struct * t = &current->thread;
        struct tss_struct * tss;
        unsigned long *bitmap;
@@ -81,16 +83,40 @@ asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
 
        /*
         * do it in the per-thread copy and in the TSS ...
+        *
+        * Disable preemption via get_cpu() - we must not switch away
+        * because the ->io_bitmap_max value must match the bitmap
+        * contents:
         */
+       tss = &per_cpu(init_tss, get_cpu());
+
        set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
-       tss = init_tss + get_cpu();
-       if (tss->io_bitmap_base == IO_BITMAP_OFFSET) { /* already active? */
-               set_bitmap(tss->io_bitmap, from, num, !turn_on);
-       } else {
-               memcpy(tss->io_bitmap, t->io_bitmap_ptr, IO_BITMAP_BYTES);
-               tss->io_bitmap_base = IO_BITMAP_OFFSET; /* Activate it in the TSS */
-       }
+
+       /*
+        * Search for a (possibly new) maximum. This is simple and stupid,
+        * to keep it obviously correct:
+        */
+       max_long = 0;
+       for (i = 0; i < IO_BITMAP_LONGS; i++)
+               if (t->io_bitmap_ptr[i] != ~0UL)
+                       max_long = i;
+
+       bytes = (max_long + 1) * sizeof(long);
+       bytes_updated = max(bytes, t->io_bitmap_max);
+
+       t->io_bitmap_max = bytes;
+
+       /*
+        * Sets the lazy trigger so that the next I/O operation will
+        * reload the correct bitmap.
+        * Reset the owner so that a process switch will not set
+        * tss->io_bitmap_base to IO_BITMAP_OFFSET.
+        */
+       tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
+       tss->io_bitmap_owner = NULL;
+
        put_cpu();
+
        return 0;
 }
 
@@ -110,6 +136,7 @@ asmlinkage long sys_iopl(unsigned long unused)
        volatile struct pt_regs * regs = (struct pt_regs *) &unused;
        unsigned int level = regs->ebx;
        unsigned int old = (regs->eflags >> 12) & 3;
+       struct thread_struct *t = &current->thread;
 
        if (level > 3)
                return -EINVAL;
@@ -118,8 +145,8 @@ asmlinkage long sys_iopl(unsigned long unused)
                if (!capable(CAP_SYS_RAWIO))
                        return -EPERM;
        }
-       regs->eflags = (regs->eflags &~ 0x3000UL) | (level << 12);
-       /* Make sure we return the long way (not sysenter) */
-       set_thread_flag(TIF_IRET);
+       t->iopl = level << 12;
+       regs->eflags = (regs->eflags & ~X86_EFLAGS_IOPL) | t->iopl;
+       set_iopl_mask(t->iopl);
        return 0;
 }