ioport.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * This contains the io-permission bitmap code - written by obz, with changes
  3. * by Linus. 32/64 bits code unification by Miguel Botón.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/kernel.h>
  7. #include <linux/capability.h>
  8. #include <linux/errno.h>
  9. #include <linux/types.h>
  10. #include <linux/ioport.h>
  11. #include <linux/smp.h>
  12. #include <linux/stddef.h>
  13. #include <linux/slab.h>
  14. #include <linux/thread_info.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/bitmap.h>
  17. #include <asm/syscalls.h>
  18. /*
  19. * this changes the io permissions bitmap in the current task.
  20. */
  21. asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
  22. {
  23. struct thread_struct *t = &current->thread;
  24. struct tss_struct *tss;
  25. unsigned int i, max_long, bytes, bytes_updated;
  26. if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
  27. return -EINVAL;
  28. if (turn_on && !capable(CAP_SYS_RAWIO))
  29. return -EPERM;
  30. /*
  31. * If it's the first ioperm() call in this thread's lifetime, set the
  32. * IO bitmap up. ioperm() is much less timing critical than clone(),
  33. * this is why we delay this operation until now:
  34. */
  35. if (!t->io_bitmap_ptr) {
  36. unsigned long *bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
  37. if (!bitmap)
  38. return -ENOMEM;
  39. memset(bitmap, 0xff, IO_BITMAP_BYTES);
  40. t->io_bitmap_ptr = bitmap;
  41. set_thread_flag(TIF_IO_BITMAP);
  42. }
  43. /*
  44. * do it in the per-thread copy and in the TSS ...
  45. *
  46. * Disable preemption via get_cpu() - we must not switch away
  47. * because the ->io_bitmap_max value must match the bitmap
  48. * contents:
  49. */
  50. tss = &per_cpu(cpu_tss, get_cpu());
  51. if (turn_on)
  52. bitmap_clear(t->io_bitmap_ptr, from, num);
  53. else
  54. bitmap_set(t->io_bitmap_ptr, from, num);
  55. /*
  56. * Search for a (possibly new) maximum. This is simple and stupid,
  57. * to keep it obviously correct:
  58. */
  59. max_long = 0;
  60. for (i = 0; i < IO_BITMAP_LONGS; i++)
  61. if (t->io_bitmap_ptr[i] != ~0UL)
  62. max_long = i;
  63. bytes = (max_long + 1) * sizeof(unsigned long);
  64. bytes_updated = max(bytes, t->io_bitmap_max);
  65. t->io_bitmap_max = bytes;
  66. /* Update the TSS: */
  67. memcpy(tss->io_bitmap, t->io_bitmap_ptr, bytes_updated);
  68. put_cpu();
  69. return 0;
  70. }
  71. /*
  72. * sys_iopl has to be used when you want to access the IO ports
  73. * beyond the 0x3ff range: to get the full 65536 ports bitmapped
  74. * you'd need 8kB of bitmaps/process, which is a bit excessive.
  75. *
  76. * Here we just change the flags value on the stack: we allow
  77. * only the super-user to do it. This depends on the stack-layout
  78. * on system-call entry - see also fork() and the signal handling
  79. * code.
  80. */
  81. SYSCALL_DEFINE1(iopl, unsigned int, level)
  82. {
  83. struct pt_regs *regs = current_pt_regs();
  84. struct thread_struct *t = &current->thread;
  85. /*
  86. * Careful: the IOPL bits in regs->flags are undefined under Xen PV
  87. * and changing them has no effect.
  88. */
  89. unsigned int old = t->iopl >> X86_EFLAGS_IOPL_BIT;
  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->flags = (regs->flags & ~X86_EFLAGS_IOPL) |
  98. (level << X86_EFLAGS_IOPL_BIT);
  99. t->iopl = level << X86_EFLAGS_IOPL_BIT;
  100. set_iopl_mask(t->iopl);
  101. return 0;
  102. }