2 * linux/arch/i386/kernel/ioport.c
4 * This contains the io-permission bitmap code - written by obz, with changes
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>
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)
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;
29 mask = (~0UL << low_index);
30 if (length < BITS_PER_LONG)
31 mask &= ~(~0UL << length);
33 *bitmap_base++ |= mask;
35 *bitmap_base++ &= ~mask;
36 length -= BITS_PER_LONG;
39 mask = (new_value ? ~0UL : 0UL);
40 while (length >= BITS_PER_LONG) {
41 *bitmap_base++ = mask;
42 length -= BITS_PER_LONG;
46 mask = ~(~0UL << length);
48 *bitmap_base++ |= mask;
50 *bitmap_base++ &= ~mask;
56 * this changes the io permissions bitmap in the current task.
58 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
60 struct thread_struct * t = ¤t->thread;
61 unsigned long *bitmap;
64 if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
66 if (turn_on && !capable(CAP_SYS_RAWIO))
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:
74 if (!t->io_bitmap_ptr) {
75 bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
79 memset(bitmap, 0xff, IO_BITMAP_BYTES);
80 t->io_bitmap_ptr = bitmap;
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);
88 set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
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.
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
104 asmlinkage long sys_iopl(unsigned int new_io_pl)
106 unsigned int old_io_pl = current->thread.io_pl;
112 /* Need "raw I/O" privileges for direct port access. */
113 if ((new_io_pl > old_io_pl) && !capable(CAP_SYS_RAWIO))
116 /* Maintain OS privileges even if user attempts to relinquish them. */
120 /* Change our version of the privilege levels. */
121 current->thread.io_pl = new_io_pl;
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);