File: /usr/src/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/mm.h>
14     #include <linux/smp.h>
15     #include <linux/smp_lock.h>
16     #include <linux/stddef.h>
17     
18     /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
19     static void set_bitmap(unsigned long *bitmap, short base, short extent, int new_value)
20     {
21     	int mask;
22     	unsigned long *bitmap_base = bitmap + (base >> 5);
23     	unsigned short low_index = base & 0x1f;
24     	int length = low_index + extent;
25     
26     	if (low_index != 0) {
27     		mask = (~0 << low_index);
28     		if (length < 32)
29     				mask &= ~(~0 << length);
30     		if (new_value)
31     			*bitmap_base++ |= mask;
32     		else
33     			*bitmap_base++ &= ~mask;
34     		length -= 32;
35     	}
36     
37     	mask = (new_value ? ~0 : 0);
38     	while (length >= 32) {
39     		*bitmap_base++ = mask;
40     		length -= 32;
41     	}
42     
43     	if (length > 0) {
44     		mask = ~(~0 << length);
45     		if (new_value)
46     			*bitmap_base++ |= mask;
47     		else
48     			*bitmap_base++ &= ~mask;
49     	}
50     }
51     
52     /*
53      * this changes the io permissions bitmap in the current task.
54      */
55     asmlinkage int sys_ioperm(unsigned long from, unsigned long num, int turn_on)
56     {
57     	struct thread_struct * t = &current->thread;
58     	struct tss_struct * tss = init_tss + smp_processor_id();
59     
60     	if ((from + num <= from) || (from + num > IO_BITMAP_SIZE*32))
61     		return -EINVAL;
62     	if (turn_on && !capable(CAP_SYS_RAWIO))
63     		return -EPERM;
64     	/*
65     	 * If it's the first ioperm() call in this thread's lifetime, set the
66     	 * IO bitmap up. ioperm() is much less timing critical than clone(),
67     	 * this is why we delay this operation until now:
68     	 */
69     	if (!t->ioperm) {
70     		/*
71     		 * just in case ...
72     		 */
73     		memset(t->io_bitmap,0xff,(IO_BITMAP_SIZE+1)*4);
74     		t->ioperm = 1;
75     		/*
76     		 * this activates it in the TSS
77     		 */
78     		tss->bitmap = IO_BITMAP_OFFSET;
79     	}
80     
81     	/*
82     	 * do it in the per-thread copy and in the TSS ...
83     	 */
84     	set_bitmap(t->io_bitmap, from, num, !turn_on);
85     	set_bitmap(tss->io_bitmap, from, num, !turn_on);
86     
87     	return 0;
88     }
89     
90     /*
91      * sys_iopl has to be used when you want to access the IO ports
92      * beyond the 0x3ff range: to get the full 65536 ports bitmapped
93      * you'd need 8kB of bitmaps/process, which is a bit excessive.
94      *
95      * Here we just change the eflags value on the stack: we allow
96      * only the super-user to do it. This depends on the stack-layout
97      * on system-call entry - see also fork() and the signal handling
98      * code.
99      */
100     
101     asmlinkage int sys_iopl(unsigned long unused)
102     {
103     	struct pt_regs * regs = (struct pt_regs *) &unused;
104     	unsigned int level = regs->ebx;
105     	unsigned int old = (regs->eflags >> 12) & 3;
106     
107     	if (level > 3)
108     		return -EINVAL;
109     	/* Trying to gain more privileges? */
110     	if (level > old) {
111     		if (!capable(CAP_SYS_RAWIO))
112     			return -EPERM;
113     	}
114     	regs->eflags = (regs->eflags & 0xffffcfff) | (level << 12);
115     	return 0;
116     }
117