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>
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>
19 /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
20 static void set_bitmap(unsigned long *bitmap, unsigned int base, unsigned int extent, int new_value)
24 for (i = base; i < base + extent; i++)
27 for (i = base; i < base + extent; i++)
33 * this changes the io permissions bitmap in the current task.
35 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
37 struct thread_struct * t = ¤t->thread;
38 struct tss_struct * tss;
39 unsigned long *bitmap;
41 if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
43 if (turn_on && !capable(CAP_SYS_RAWIO))
47 * If it's the first ioperm() call in this thread's lifetime, set the
48 * IO bitmap up. ioperm() is much less timing critical than clone(),
49 * this is why we delay this operation until now:
51 if (!t->io_bitmap_ptr) {
52 bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
56 memset(bitmap, 0xff, IO_BITMAP_BYTES);
57 t->io_bitmap_ptr = bitmap;
61 * do it in the per-thread copy and in the TSS ...
63 set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
64 tss = init_tss + get_cpu();
65 if (tss->io_bitmap_base == IO_BITMAP_OFFSET) { /* already active? */
66 set_bitmap(tss->io_bitmap, from, num, !turn_on);
68 memcpy(tss->io_bitmap, t->io_bitmap_ptr, IO_BITMAP_BYTES);
69 tss->io_bitmap_base = IO_BITMAP_OFFSET; /* Activate it in the TSS */
76 * sys_iopl has to be used when you want to access the IO ports
77 * beyond the 0x3ff range: to get the full 65536 ports bitmapped
78 * you'd need 8kB of bitmaps/process, which is a bit excessive.
80 * Here we just change the eflags value on the stack: we allow
81 * only the super-user to do it. This depends on the stack-layout
82 * on system-call entry - see also fork() and the signal handling
86 asmlinkage long sys_iopl(unsigned int level, struct pt_regs regs)
88 unsigned int old = (regs.eflags >> 12) & 3;
92 /* Trying to gain more privileges? */
94 if (!capable(CAP_SYS_RAWIO))
97 regs.eflags = (regs.eflags &~ 0x3000UL) | (level << 12);