Added patch headers.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / ioport-xen.c
1 /*
2  * This contains the io-permission bitmap code - written by obz, with changes
3  * by Linus. 32/64 bits code unification by Miguel Botón.
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/smp.h>
13 #include <linux/stddef.h>
14 #include <linux/slab.h>
15 #include <linux/thread_info.h>
16 #include <linux/syscalls.h>
17 #include <asm/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,
22                        unsigned int extent, int new_value)
23 {
24         unsigned int i;
25
26         for (i = base; i < base + extent; i++) {
27                 if (new_value)
28                         __set_bit(i, bitmap);
29                 else
30                         __clear_bit(i, bitmap);
31         }
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         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                 unsigned long *bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
54
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_xen_guest_handle(set_iobitmap.bitmap, (char *)bitmap);
63                 set_iobitmap.nr_ports = IO_BITMAP_BITS;
64                 WARN_ON(HYPERVISOR_physdev_op(PHYSDEVOP_set_iobitmap,
65                                               &set_iobitmap));
66         }
67
68         set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
69
70         return 0;
71 }
72
73 /*
74  * sys_iopl has to be used when you want to access the IO ports
75  * beyond the 0x3ff range: to get the full 65536 ports bitmapped
76  * you'd need 8kB of bitmaps/process, which is a bit excessive.
77  */
78 long sys_iopl(unsigned int level, struct pt_regs *regs)
79 {
80         struct thread_struct *t = &current->thread;
81         unsigned int old = t->iopl >> 12;
82
83         if (level > 3)
84                 return -EINVAL;
85         /* Trying to gain more privileges? */
86         if (level > old) {
87                 if (!capable(CAP_SYS_RAWIO))
88                         return -EPERM;
89         }
90         t->iopl = level << 12;
91         set_iopl_mask(t->iopl);
92
93         return 0;
94 }