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/capability.h> |
---|
11 | #include <linux/errno.h> |
---|
12 | #include <linux/types.h> |
---|
13 | #include <linux/ioport.h> |
---|
14 | #include <linux/mm.h> |
---|
15 | #include <linux/smp.h> |
---|
16 | #include <linux/smp_lock.h> |
---|
17 | #include <linux/stddef.h> |
---|
18 | #include <linux/slab.h> |
---|
19 | #include <linux/thread_info.h> |
---|
20 | #include <xen/interface/physdev.h> |
---|
21 | |
---|
22 | /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */ |
---|
23 | static void set_bitmap(unsigned long *bitmap, unsigned int base, unsigned int extent, int new_value) |
---|
24 | { |
---|
25 | int i; |
---|
26 | |
---|
27 | if (new_value) |
---|
28 | for (i = base; i < base + extent; i++) |
---|
29 | __set_bit(i, bitmap); |
---|
30 | else |
---|
31 | for (i = base; i < base + extent; i++) |
---|
32 | clear_bit(i, bitmap); |
---|
33 | } |
---|
34 | |
---|
35 | /* |
---|
36 | * this changes the io permissions bitmap in the current task. |
---|
37 | */ |
---|
38 | asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on) |
---|
39 | { |
---|
40 | struct thread_struct * t = ¤t->thread; |
---|
41 | unsigned long *bitmap; |
---|
42 | struct physdev_set_iobitmap set_iobitmap; |
---|
43 | |
---|
44 | if ((from + num <= from) || (from + num > IO_BITMAP_BITS)) |
---|
45 | return -EINVAL; |
---|
46 | if (turn_on && !capable(CAP_SYS_RAWIO)) |
---|
47 | return -EPERM; |
---|
48 | |
---|
49 | /* |
---|
50 | * If it's the first ioperm() call in this thread's lifetime, set the |
---|
51 | * IO bitmap up. ioperm() is much less timing critical than clone(), |
---|
52 | * this is why we delay this operation until now: |
---|
53 | */ |
---|
54 | if (!t->io_bitmap_ptr) { |
---|
55 | bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL); |
---|
56 | if (!bitmap) |
---|
57 | return -ENOMEM; |
---|
58 | |
---|
59 | memset(bitmap, 0xff, IO_BITMAP_BYTES); |
---|
60 | t->io_bitmap_ptr = bitmap; |
---|
61 | |
---|
62 | set_xen_guest_handle(set_iobitmap.bitmap, (char *)bitmap); |
---|
63 | set_iobitmap.nr_ports = IO_BITMAP_BITS; |
---|
64 | HYPERVISOR_physdev_op(PHYSDEVOP_set_iobitmap, &set_iobitmap); |
---|
65 | } |
---|
66 | |
---|
67 | set_bitmap(t->io_bitmap_ptr, from, num, !turn_on); |
---|
68 | |
---|
69 | return 0; |
---|
70 | } |
---|
71 | |
---|
72 | /* |
---|
73 | * sys_iopl has to be used when you want to access the IO ports |
---|
74 | * beyond the 0x3ff range: to get the full 65536 ports bitmapped |
---|
75 | * you'd need 8kB of bitmaps/process, which is a bit excessive. |
---|
76 | * |
---|
77 | */ |
---|
78 | |
---|
79 | asmlinkage long sys_iopl(unsigned int new_iopl, struct pt_regs *regs) |
---|
80 | { |
---|
81 | unsigned int old_iopl = current->thread.iopl; |
---|
82 | struct physdev_set_iopl set_iopl; |
---|
83 | |
---|
84 | if (new_iopl > 3) |
---|
85 | return -EINVAL; |
---|
86 | |
---|
87 | /* Need "raw I/O" privileges for direct port access. */ |
---|
88 | if ((new_iopl > old_iopl) && !capable(CAP_SYS_RAWIO)) |
---|
89 | return -EPERM; |
---|
90 | |
---|
91 | /* Change our version of the privilege levels. */ |
---|
92 | current->thread.iopl = new_iopl; |
---|
93 | |
---|
94 | /* Force the change at ring 0. */ |
---|
95 | set_iopl.iopl = (new_iopl == 0) ? 1 : new_iopl; |
---|
96 | HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl); |
---|
97 | |
---|
98 | return 0; |
---|
99 | } |
---|