- patches.rt/0001-sched-count-of-queued-RT-tasks.patch: Delete.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / ioport_64-xen.c
1 /*
2  * This contains the io-permission bitmap code - written by obz, with changes
3  * by Linus.
4  */
5
6 #include <linux/sched.h>
7 #include <linux/kernel.h>
8 #include <linux/capability.h>
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/ioport.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/stddef.h>
15 #include <linux/slab.h>
16 #include <linux/thread_info.h>
17 #include <linux/syscalls.h>
18 #include <xen/interface/physdev.h>
19
20 /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
21 static void set_bitmap(unsigned long *bitmap, unsigned int base, unsigned int extent, int new_value)
22 {
23         int i;
24
25         if (new_value)
26                 for (i = base; i < base + extent; i++)
27                         __set_bit(i, bitmap);
28         else
29                 for (i = base; i < base + extent; i++)
30                         clear_bit(i, bitmap);
31 }
32
33 /*
34  * this changes the io permissions bitmap in the current task.
35  */
36 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
37 {
38         struct thread_struct * t = &current->thread;
39         unsigned long *bitmap;
40         struct physdev_set_iobitmap set_iobitmap;
41
42         if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
43                 return -EINVAL;
44         if (turn_on && !capable(CAP_SYS_RAWIO))
45                 return -EPERM;
46
47         /*
48          * If it's the first ioperm() call in this thread's lifetime, set the
49          * IO bitmap up. ioperm() is much less timing critical than clone(),
50          * this is why we delay this operation until now:
51          */
52         if (!t->io_bitmap_ptr) {
53                 bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
54                 if (!bitmap)
55                         return -ENOMEM;
56
57                 memset(bitmap, 0xff, IO_BITMAP_BYTES);
58                 t->io_bitmap_ptr = bitmap;
59                 set_thread_flag(TIF_IO_BITMAP);
60
61                 set_xen_guest_handle(set_iobitmap.bitmap, (char *)bitmap);
62                 set_iobitmap.nr_ports = IO_BITMAP_BITS;
63                 WARN_ON(HYPERVISOR_physdev_op(PHYSDEVOP_set_iobitmap,
64                                               &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 new_iopl, struct pt_regs *regs)
80 {
81         unsigned int old_iopl = current->thread.iopl;
82         struct physdev_set_iopl set_iopl;
83
84         if (new_iopl > 3)
85                 return -EINVAL;
86
87         /* Need "raw I/O" privileges for direct port access. */
88         if ((new_iopl > old_iopl) && !capable(CAP_SYS_RAWIO))
89                 return -EPERM;
90
91         /* Change our version of the privilege levels. */
92         current->thread.iopl = new_iopl;
93
94         /* Force the change at ring 0. */
95         set_iopl.iopl = (new_iopl == 0) ? 1 : new_iopl;
96         WARN_ON(HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl));
97
98         return 0;
99 }