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/errno.h>
11 #include <linux/types.h>
12 #include <linux/ioport.h>
14 #include <linux/slab.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/stddef.h>
19 /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
20 static void set_bitmap(unsigned long *bitmap, short base, short extent, int new_value)
24 for (i = base; i < base + extent; i++)
27 for (i = base; i < base + extent; i++)
32 * this changes the io permissions bitmap in the current task.
34 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
36 struct thread_struct * t = ¤t->thread;
37 struct tss_struct * tss = init_tss + smp_processor_id();
39 if ((from + num <= from) || (from + num > IO_BITMAP_SIZE*32))
41 if (turn_on && !capable(CAP_SYS_RAWIO))
44 * If it's the first ioperm() call in this thread's lifetime, set the
45 * IO bitmap up. ioperm() is much less timing critical than clone(),
46 * this is why we delay this operation until now:
48 if (!t->io_bitmap_ptr) {
49 t->io_bitmap_ptr = kmalloc((IO_BITMAP_SIZE+1)*4, GFP_KERNEL);
50 if (!t->io_bitmap_ptr)
53 memset(t->io_bitmap_ptr,0xff,(IO_BITMAP_SIZE+1)*4);
55 * this activates it in the TSS
60 * do it in the per-thread copy and in the TSS ...
62 set_bitmap((unsigned long *) t->io_bitmap_ptr, from, num, !turn_on);
63 if (tss->io_map_base != IO_BITMAP_OFFSET) {
64 memcpy(tss->io_bitmap, t->io_bitmap_ptr, sizeof(tss->io_bitmap));
65 tss->io_map_base = IO_BITMAP_OFFSET;
67 set_bitmap((unsigned long *) tss->io_bitmap, from, num, !turn_on);
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.
78 * Here we just change the eflags value on the stack: we allow
79 * only the super-user to do it. This depends on the stack-layout
80 * on system-call entry - see also fork() and the signal handling
84 asmlinkage long sys_iopl(unsigned int level, struct pt_regs *regs)
86 unsigned int old = (regs->eflags >> 12) & 3;
90 /* Trying to gain more privileges? */
92 if (!capable(CAP_SYS_RAWIO))
95 regs->eflags = (regs->eflags &~ 0x3000UL) | (level << 12);