fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / x86_64 / kernel / ioport-xen.c
1 /*
2  *      linux/arch/x86_64/kernel/ioport.c
3  *
4  * This contains the io-permission bitmap code - written by obz, with changes
5  * by Linus.
6  */
7
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/capability.h>
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/ioport.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/stddef.h>
18 #include <linux/slab.h>
19 #include <linux/thread_info.h>
20 #include <xen/interface/physdev.h>
21
22 /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
23 static void set_bitmap(unsigned long *bitmap, unsigned int base, unsigned int extent, int new_value)
24 {
25         int i;
26                 if (new_value)
27                 for (i = base; i < base + extent; i++) 
28                         __set_bit(i, bitmap); 
29                 else
30                 for (i = base; i < base + extent; i++) 
31                         clear_bit(i, bitmap); 
32 }
33
34 /*
35  * this changes the io permissions bitmap in the current task.
36  */
37 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
38 {
39         struct thread_struct * t = &current->thread;
40         unsigned long *bitmap;
41         struct physdev_set_iobitmap set_iobitmap;
42
43         if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
44                 return -EINVAL;
45         if (turn_on && !capable(CAP_SYS_RAWIO))
46                 return -EPERM;
47
48         /*
49          * If it's the first ioperm() call in this thread's lifetime, set the
50          * IO bitmap up. ioperm() is much less timing critical than clone(),
51          * this is why we delay this operation until now:
52          */
53         if (!t->io_bitmap_ptr) {
54                 bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
55                 if (!bitmap)
56                         return -ENOMEM;
57
58                 memset(bitmap, 0xff, IO_BITMAP_BYTES);
59                 t->io_bitmap_ptr = bitmap;
60                 set_thread_flag(TIF_IO_BITMAP);
61
62                 set_iobitmap.bitmap   = (char *)bitmap;
63                 set_iobitmap.nr_ports = IO_BITMAP_BITS;
64                 HYPERVISOR_physdev_op(PHYSDEVOP_set_iobitmap, &set_iobitmap);
65         }
66
67         set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
68
69         return 0;
70 }
71
72 /*
73  * sys_iopl has to be used when you want to access the IO ports
74  * beyond the 0x3ff range: to get the full 65536 ports bitmapped
75  * you'd need 8kB of bitmaps/process, which is a bit excessive.
76  *
77  */
78
79 asmlinkage long sys_iopl(unsigned int level, struct pt_regs *regs)
80 {
81         unsigned int old = current->thread.iopl;
82         struct physdev_set_iopl set_iopl;
83
84         if (level > 3)
85                 return -EINVAL;
86         /* Trying to gain more privileges? */
87         if (level > old) {
88                 if (!capable(CAP_SYS_RAWIO))
89                         return -EPERM;
90         }
91         /* Change our version of the privilege levels. */
92         current->thread.iopl = level;
93
94         /* Force the change at ring 0. */
95         set_iopl.iopl = (level == 0) ? 1 : level;
96         HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
97
98         return 0;
99 }