This commit was generated by cvs2svn to compensate for changes in r925,
[linux-2.6.git] / arch / xen / i386 / kernel / ioport.c
1 /*
2  *      linux/arch/i386/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/errno.h>
11 #include <linux/types.h>
12 #include <linux/ioport.h>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
15 #include <linux/stddef.h>
16 #include <linux/slab.h>
17 #include <linux/thread_info.h>
18 #include <asm-xen/xen-public/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         unsigned long mask;
24         unsigned long *bitmap_base = bitmap + (base / BITS_PER_LONG);
25         unsigned int low_index = base & (BITS_PER_LONG-1);
26         int length = low_index + extent;
27
28         if (low_index != 0) {
29                 mask = (~0UL << low_index);
30                 if (length < BITS_PER_LONG)
31                         mask &= ~(~0UL << length);
32                 if (new_value)
33                         *bitmap_base++ |= mask;
34                 else
35                         *bitmap_base++ &= ~mask;
36                 length -= BITS_PER_LONG;
37         }
38
39         mask = (new_value ? ~0UL : 0UL);
40         while (length >= BITS_PER_LONG) {
41                 *bitmap_base++ = mask;
42                 length -= BITS_PER_LONG;
43         }
44
45         if (length > 0) {
46                 mask = ~(~0UL << length);
47                 if (new_value)
48                         *bitmap_base++ |= mask;
49                 else
50                         *bitmap_base++ &= ~mask;
51         }
52 }
53
54
55 /*
56  * this changes the io permissions bitmap in the current task.
57  */
58 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
59 {
60         struct thread_struct * t = &current->thread;
61         unsigned long *bitmap;
62         physdev_op_t op;
63
64         if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
65                 return -EINVAL;
66         if (turn_on && !capable(CAP_SYS_RAWIO))
67                 return -EPERM;
68
69         /*
70          * If it's the first ioperm() call in this thread's lifetime, set the
71          * IO bitmap up. ioperm() is much less timing critical than clone(),
72          * this is why we delay this operation until now:
73          */
74         if (!t->io_bitmap_ptr) {
75                 bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
76                 if (!bitmap)
77                         return -ENOMEM;
78
79                 memset(bitmap, 0xff, IO_BITMAP_BYTES);
80                 t->io_bitmap_ptr = bitmap;
81
82                 op.cmd = PHYSDEVOP_SET_IOBITMAP;
83                 op.u.set_iobitmap.bitmap   = (unsigned long)bitmap;
84                 op.u.set_iobitmap.nr_ports = IO_BITMAP_BITS;
85                 HYPERVISOR_physdev_op(&op);
86         }
87
88         set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
89
90         return 0;
91 }
92
93 /*
94  * sys_iopl has to be used when you want to access the IO ports
95  * beyond the 0x3ff range: to get the full 65536 ports bitmapped
96  * you'd need 8kB of bitmaps/process, which is a bit excessive.
97  *
98  * Here we just change the eflags value on the stack: we allow
99  * only the super-user to do it. This depends on the stack-layout
100  * on system-call entry - see also fork() and the signal handling
101  * code.
102  */
103
104 asmlinkage long sys_iopl(unsigned int new_io_pl)
105 {
106         unsigned int old_io_pl = current->thread.io_pl;
107         physdev_op_t op;
108
109         if (new_io_pl > 3)
110                 return -EINVAL;
111
112         /* Need "raw I/O" privileges for direct port access. */
113         if ((new_io_pl > old_io_pl) && !capable(CAP_SYS_RAWIO))
114                 return -EPERM;
115
116         /* Maintain OS privileges even if user attempts to relinquish them. */
117         if (new_io_pl == 0)
118                 new_io_pl = 1;
119
120         /* Change our version of the privilege levels. */
121         current->thread.io_pl = new_io_pl;
122
123         /* Force the change at ring 0. */
124         op.cmd             = PHYSDEVOP_SET_IOPL;
125         op.u.set_iopl.iopl = new_io_pl;
126         HYPERVISOR_physdev_op(&op);
127
128         return 0;
129 }