2 * linux/arch/x86_64/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/capability.h>
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/ioport.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/stddef.h>
18 #include <linux/slab.h>
19 #include <linux/thread_info.h>
20 #include <xen/interface/physdev.h>
22 /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
23 static void set_bitmap(unsigned long *bitmap, unsigned int base, unsigned int extent, int new_value)
27 for (i = base; i < base + extent; i++)
30 for (i = base; i < base + extent; i++)
35 * this changes the io permissions bitmap in the current task.
37 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
39 struct thread_struct * t = ¤t->thread;
40 unsigned long *bitmap;
41 struct physdev_set_iobitmap set_iobitmap;
43 if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
45 if (turn_on && !capable(CAP_SYS_RAWIO))
49 * If it's the first ioperm() call in this thread's lifetime, set the
50 * IO bitmap up. ioperm() is much less timing critical than clone(),
51 * this is why we delay this operation until now:
53 if (!t->io_bitmap_ptr) {
54 bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
58 memset(bitmap, 0xff, IO_BITMAP_BYTES);
59 t->io_bitmap_ptr = bitmap;
60 set_thread_flag(TIF_IO_BITMAP);
62 set_iobitmap.bitmap = (char *)bitmap;
63 set_iobitmap.nr_ports = IO_BITMAP_BITS;
64 HYPERVISOR_physdev_op(PHYSDEVOP_set_iobitmap, &set_iobitmap);
67 set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
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.
79 asmlinkage long sys_iopl(unsigned int level, struct pt_regs *regs)
81 unsigned int old = current->thread.iopl;
82 struct physdev_set_iopl set_iopl;
86 /* Trying to gain more privileges? */
88 if (!capable(CAP_SYS_RAWIO))
91 /* Change our version of the privilege levels. */
92 current->thread.iopl = level;
94 /* Force the change at ring 0. */
95 set_iopl.iopl = (level == 0) ? 1 : level;
96 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);