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/smp.h>
15 #include <linux/smp_lock.h>
16 #include <linux/stddef.h>
17 #include <linux/slab.h>
20 /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
21 static void set_bitmap(unsigned long *bitmap, short base, short extent, int new_value)
24 unsigned long *bitmap_base = bitmap + (base / sizeof(unsigned long));
25 unsigned short low_index = base & 0x3f;
26 int length = low_index + extent;
29 mask = (~0UL << low_index);
31 mask &= ~(~0UL << length);
33 *bitmap_base++ |= mask;
35 *bitmap_base++ &= ~mask;
39 mask = (new_value ? ~0UL : 0UL);
40 while (length >= 64) {
41 *bitmap_base++ = mask;
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;
63 if ((from + num <= from) || (from + num > IO_BITMAP_SIZE*32))
65 if (turn_on && !capable(CAP_SYS_RAWIO))
68 struct tss_struct * tss = init_tss + cpu;
71 * If it's the first ioperm() call in this thread's lifetime, set the
72 * IO bitmap up. ioperm() is much less timing critical than clone(),
73 * this is why we delay this operation until now:
75 if (!t->io_bitmap_ptr) {
76 t->io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
77 if (!t->io_bitmap_ptr) {
82 memset(t->io_bitmap_ptr,0xff,IO_BITMAP_BYTES);
86 * do it in the per-thread copy and in the TSS ...
88 set_bitmap((unsigned long *) t->io_bitmap_ptr, from, num, !turn_on);
89 if (tss->io_map_base != IO_BITMAP_OFFSET) {
90 memcpy(tss->io_bitmap, t->io_bitmap_ptr, sizeof(tss->io_bitmap));
91 tss->io_map_base = IO_BITMAP_OFFSET;
93 set_bitmap((unsigned long *) tss->io_bitmap, from, num, !turn_on);
101 * sys_iopl has to be used when you want to access the IO ports
102 * beyond the 0x3ff range: to get the full 65536 ports bitmapped
103 * you'd need 8kB of bitmaps/process, which is a bit excessive.
105 * Here we just change the eflags value on the stack: we allow
106 * only the super-user to do it. This depends on the stack-layout
107 * on system-call entry - see also fork() and the signal handling
111 asmlinkage long sys_iopl(unsigned int level, struct pt_regs regs)
113 unsigned int old = (regs.eflags >> 12) & 3;
117 /* Trying to gain more privileges? */
119 if (!capable(CAP_SYS_RAWIO))
122 regs.eflags = (regs.eflags & 0xffffffffffffcfff) | (level << 12);