Update to 3.4-final.
[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 <linux/bitmap.h>
18 #include <asm/syscalls.h>
19 #include <xen/interface/physdev.h>
20
21 /*
22  * this changes the io permissions bitmap in the current task.
23  */
24 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
25 {
26         struct thread_struct *t = &current->thread;
27         struct physdev_set_iobitmap set_iobitmap;
28
29         if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
30                 return -EINVAL;
31         if (turn_on && !capable(CAP_SYS_RAWIO))
32                 return -EPERM;
33
34         /*
35          * If it's the first ioperm() call in this thread's lifetime, set the
36          * IO bitmap up. ioperm() is much less timing critical than clone(),
37          * this is why we delay this operation until now:
38          */
39         if (!t->io_bitmap_ptr) {
40                 unsigned long *bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
41
42                 if (!bitmap)
43                         return -ENOMEM;
44
45                 memset(bitmap, 0xff, IO_BITMAP_BYTES);
46                 t->io_bitmap_ptr = bitmap;
47                 set_thread_flag(TIF_IO_BITMAP);
48
49                 set_xen_guest_handle(set_iobitmap.bitmap, (char *)bitmap);
50                 set_iobitmap.nr_ports = IO_BITMAP_BITS;
51                 WARN_ON(HYPERVISOR_physdev_op(PHYSDEVOP_set_iobitmap,
52                                               &set_iobitmap));
53         }
54
55         if (turn_on)
56                 bitmap_clear(t->io_bitmap_ptr, from, num);
57         else
58                 bitmap_set(t->io_bitmap_ptr, from, num);
59
60         return 0;
61 }
62
63 /*
64  * sys_iopl has to be used when you want to access the IO ports
65  * beyond the 0x3ff range: to get the full 65536 ports bitmapped
66  * you'd need 8kB of bitmaps/process, which is a bit excessive.
67  */
68 long sys_iopl(unsigned int level, struct pt_regs *regs)
69 {
70         struct thread_struct *t = &current->thread;
71         unsigned int old = t->iopl >> 12;
72
73         if (level > 3)
74                 return -EINVAL;
75         /* Trying to gain more privileges? */
76         if (level > old) {
77                 if (!capable(CAP_SYS_RAWIO))
78                         return -EPERM;
79         }
80         t->iopl = level << 12;
81         set_iopl_mask(t->iopl);
82
83         return 0;
84 }