ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / x86_64 / kernel / ioport.c
1 /*
2  *      linux/arch/x86_64/kernel/ioport.c
3  *
4  * This contains the io-permission bitmap code - written by obz, with changes
5  * by Linus.
6  */
7
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>
18
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)
21 {
22         int i;
23                 if (new_value)
24                 for (i = base; i < base + extent; i++) 
25                         __set_bit(i, bitmap); 
26                 else
27                 for (i = base; i < base + extent; i++) 
28                         clear_bit(i, bitmap); 
29 }
30
31
32 /*
33  * this changes the io permissions bitmap in the current task.
34  */
35 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
36 {
37         struct thread_struct * t = &current->thread;
38         struct tss_struct * tss;
39         unsigned long *bitmap;
40
41         if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
42                 return -EINVAL;
43         if (turn_on && !capable(CAP_SYS_RAWIO))
44                 return -EPERM;
45
46         /*
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:
50          */
51         if (!t->io_bitmap_ptr) {
52                 bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
53                 if (!bitmap)
54                         return -ENOMEM;
55
56                 memset(bitmap, 0xff, IO_BITMAP_BYTES);
57                 t->io_bitmap_ptr = bitmap;
58         }
59
60         /*
61          * do it in the per-thread copy and in the TSS ...
62          */
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);
67         } else {
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 */
70         }
71         put_cpu();
72         return 0;
73 }
74
75 /*
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.
79  *
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
83  * code.
84  */
85
86 asmlinkage long sys_iopl(unsigned int level, struct pt_regs regs)
87 {
88         unsigned int old = (regs.eflags >> 12) & 3;
89
90         if (level > 3)
91                 return -EINVAL;
92         /* Trying to gain more privileges? */
93         if (level > old) {
94                 if (!capable(CAP_SYS_RAWIO))
95                         return -EPERM;
96         }
97         regs.eflags = (regs.eflags &~ 0x3000UL) | (level << 12);
98         return 0;
99 }