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>
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)
23 unsigned long *bitmap_base = bitmap + (base / BITS_PER_LONG);
24 unsigned int low_index = base & (BITS_PER_LONG-1);
25 int length = low_index + extent;
28 mask = (~0UL << low_index);
29 if (length < BITS_PER_LONG)
30 mask &= ~(~0UL << length);
32 *bitmap_base++ |= mask;
34 *bitmap_base++ &= ~mask;
35 length -= BITS_PER_LONG;
38 mask = (new_value ? ~0UL : 0UL);
39 while (length >= BITS_PER_LONG) {
40 *bitmap_base++ = mask;
41 length -= BITS_PER_LONG;
45 mask = ~(~0UL << length);
47 *bitmap_base++ |= mask;
49 *bitmap_base++ &= ~mask;
55 * this changes the io permissions bitmap in the current task.
57 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
59 struct thread_struct * t = ¤t->thread;
60 struct tss_struct * tss;
61 unsigned long *bitmap;
63 if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
65 if (turn_on && !capable(CAP_SYS_RAWIO))
69 * If it's the first ioperm() call in this thread's lifetime, set the
70 * IO bitmap up. ioperm() is much less timing critical than clone(),
71 * this is why we delay this operation until now:
73 if (!t->io_bitmap_ptr) {
74 bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
78 memset(bitmap, 0xff, IO_BITMAP_BYTES);
79 t->io_bitmap_ptr = bitmap;
83 * do it in the per-thread copy and in the TSS ...
85 set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
86 tss = init_tss + get_cpu();
87 if (tss->io_bitmap_base == IO_BITMAP_OFFSET) { /* already active? */
88 set_bitmap(tss->io_bitmap, from, num, !turn_on);
90 memcpy(tss->io_bitmap, t->io_bitmap_ptr, IO_BITMAP_BYTES);
91 tss->io_bitmap_base = IO_BITMAP_OFFSET; /* Activate it in the TSS */
98 * sys_iopl has to be used when you want to access the IO ports
99 * beyond the 0x3ff range: to get the full 65536 ports bitmapped
100 * you'd need 8kB of bitmaps/process, which is a bit excessive.
102 * Here we just change the eflags value on the stack: we allow
103 * only the super-user to do it. This depends on the stack-layout
104 * on system-call entry - see also fork() and the signal handling
108 asmlinkage long sys_iopl(unsigned long unused)
110 volatile struct pt_regs * regs = (struct pt_regs *) &unused;
111 unsigned int level = regs->ebx;
112 unsigned int old = (regs->eflags >> 12) & 3;
116 /* Trying to gain more privileges? */
118 if (!capable(CAP_SYS_RAWIO))
121 regs->eflags = (regs->eflags &~ 0x3000UL) | (level << 12);
122 /* Make sure we return the long way (not sysenter) */
123 set_thread_flag(TIF_IRET);