http://www.hht-eu.com/pls/hht/docs/F3140/bcm963xx_Speedport500V.0.09.04L.300L01.V27_c...
[bcm963xx.git] / kernel / linux / arch / i386 / kernel / ioport.c
1 /*
2  *      linux/arch/i386/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         unsigned long mask;
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;
26
27         if (low_index != 0) {
28                 mask = (~0UL << low_index);
29                 if (length < BITS_PER_LONG)
30                         mask &= ~(~0UL << length);
31                 if (new_value)
32                         *bitmap_base++ |= mask;
33                 else
34                         *bitmap_base++ &= ~mask;
35                 length -= BITS_PER_LONG;
36         }
37
38         mask = (new_value ? ~0UL : 0UL);
39         while (length >= BITS_PER_LONG) {
40                 *bitmap_base++ = mask;
41                 length -= BITS_PER_LONG;
42         }
43
44         if (length > 0) {
45                 mask = ~(~0UL << length);
46                 if (new_value)
47                         *bitmap_base++ |= mask;
48                 else
49                         *bitmap_base++ &= ~mask;
50         }
51 }
52
53
54 /*
55  * this changes the io permissions bitmap in the current task.
56  */
57 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
58 {
59         struct thread_struct * t = &current->thread;
60         struct tss_struct * tss;
61         unsigned long *bitmap;
62
63         if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
64                 return -EINVAL;
65         if (turn_on && !capable(CAP_SYS_RAWIO))
66                 return -EPERM;
67
68         /*
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:
72          */
73         if (!t->io_bitmap_ptr) {
74                 bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
75                 if (!bitmap)
76                         return -ENOMEM;
77
78                 memset(bitmap, 0xff, IO_BITMAP_BYTES);
79                 t->io_bitmap_ptr = bitmap;
80         }
81
82         /*
83          * do it in the per-thread copy and in the TSS ...
84          */
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);
89         } else {
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 */
92         }
93         put_cpu();
94         return 0;
95 }
96
97 /*
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.
101  *
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
105  * code.
106  */
107
108 asmlinkage long sys_iopl(unsigned long unused)
109 {
110         volatile struct pt_regs * regs = (struct pt_regs *) &unused;
111         unsigned int level = regs->ebx;
112         unsigned int old = (regs->eflags >> 12) & 3;
113
114         if (level > 3)
115                 return -EINVAL;
116         /* Trying to gain more privileges? */
117         if (level > old) {
118                 if (!capable(CAP_SYS_RAWIO))
119                         return -EPERM;
120         }
121         regs->eflags = (regs->eflags &~ 0x3000UL) | (level << 12);
122         /* Make sure we return the long way (not sysenter) */
123         set_thread_flag(TIF_IRET);
124         return 0;
125 }