ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/i386/kernel/ioport.c @ 6316:f7dfaa2af90c

merge?
author cl349@firebug.cl.cam.ac.uk
date Sun Aug 21 11:02:00 2005 +0000 (2005-08-21)
parents 1872e09bfba3
children 6721abf6b16d
line source
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 */
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 #include <asm-xen/xen-public/physdev.h>
20 /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
21 static void set_bitmap(unsigned long *bitmap, unsigned int base, unsigned int extent, int new_value)
22 {
23 unsigned long mask;
24 unsigned long *bitmap_base = bitmap + (base / BITS_PER_LONG);
25 unsigned int low_index = base & (BITS_PER_LONG-1);
26 int length = low_index + extent;
28 if (low_index != 0) {
29 mask = (~0UL << low_index);
30 if (length < BITS_PER_LONG)
31 mask &= ~(~0UL << length);
32 if (new_value)
33 *bitmap_base++ |= mask;
34 else
35 *bitmap_base++ &= ~mask;
36 length -= BITS_PER_LONG;
37 }
39 mask = (new_value ? ~0UL : 0UL);
40 while (length >= BITS_PER_LONG) {
41 *bitmap_base++ = mask;
42 length -= BITS_PER_LONG;
43 }
45 if (length > 0) {
46 mask = ~(~0UL << length);
47 if (new_value)
48 *bitmap_base++ |= mask;
49 else
50 *bitmap_base++ &= ~mask;
51 }
52 }
55 /*
56 * this changes the io permissions bitmap in the current task.
57 */
58 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
59 {
60 struct thread_struct * t = &current->thread;
61 unsigned long *bitmap;
62 physdev_op_t op;
64 if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
65 return -EINVAL;
66 if (turn_on && !capable(CAP_SYS_RAWIO))
67 return -EPERM;
69 /*
70 * If it's the first ioperm() call in this thread's lifetime, set the
71 * IO bitmap up. ioperm() is much less timing critical than clone(),
72 * this is why we delay this operation until now:
73 */
74 if (!t->io_bitmap_ptr) {
75 bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
76 if (!bitmap)
77 return -ENOMEM;
79 memset(bitmap, 0xff, IO_BITMAP_BYTES);
80 t->io_bitmap_ptr = bitmap;
82 op.cmd = PHYSDEVOP_SET_IOBITMAP;
83 op.u.set_iobitmap.bitmap = (char *)bitmap;
84 op.u.set_iobitmap.nr_ports = IO_BITMAP_BITS;
85 HYPERVISOR_physdev_op(&op);
86 }
88 set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
90 return 0;
91 }
93 /*
94 * sys_iopl has to be used when you want to access the IO ports
95 * beyond the 0x3ff range: to get the full 65536 ports bitmapped
96 * you'd need 8kB of bitmaps/process, which is a bit excessive.
97 *
98 * Here we just change the eflags value on the stack: we allow
99 * only the super-user to do it. This depends on the stack-layout
100 * on system-call entry - see also fork() and the signal handling
101 * code.
102 */
104 asmlinkage long sys_iopl(unsigned int new_io_pl)
105 {
106 unsigned int old_io_pl = current->thread.io_pl;
107 physdev_op_t op;
109 if (new_io_pl > 3)
110 return -EINVAL;
112 /* Need "raw I/O" privileges for direct port access. */
113 if ((new_io_pl > old_io_pl) && !capable(CAP_SYS_RAWIO))
114 return -EPERM;
116 /* Change our version of the privilege levels. */
117 current->thread.io_pl = new_io_pl;
119 /* Force the change at ring 0. */
120 op.cmd = PHYSDEVOP_SET_IOPL;
121 op.u.set_iopl.iopl = (new_io_pl == 0) ? 1 : new_io_pl;
122 HYPERVISOR_physdev_op(&op);
124 return 0;
125 }