ioport.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <asm/syscalls.h>
  17. /* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
  18. static void set_bitmap(unsigned long *bitmap, unsigned int base,
  19. unsigned int extent, int new_value)
  20. {
  21. unsigned int i;
  22. for (i = base; i < base + extent; i++) {
  23. if (new_value)
  24. __set_bit(i, bitmap);
  25. else
  26. __clear_bit(i, bitmap);
  27. }
  28. }
  29. /*
  30. * this changes the io permissions bitmap in the current task.
  31. */
  32. asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
  33. {
  34. struct thread_struct *t = &current->thread;
  35. struct tss_struct *tss;
  36. unsigned int i, max_long, bytes, bytes_updated;
  37. if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
  38. return -EINVAL;
  39. if (turn_on && !capable(CAP_SYS_RAWIO))
  40. return -EPERM;
  41. /*
  42. * If it's the first ioperm() call in this thread's lifetime, set the
  43. * IO bitmap up. ioperm() is much less timing critical than clone(),
  44. * this is why we delay this operation until now:
  45. */
  46. if (!t->io_bitmap_ptr) {
  47. unsigned long *bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
  48. if (!bitmap)
  49. return -ENOMEM;
  50. memset(bitmap, 0xff, IO_BITMAP_BYTES);
  51. t->io_bitmap_ptr = bitmap;
  52. set_thread_flag(TIF_IO_BITMAP);
  53. }
  54. /*
  55. * do it in the per-thread copy and in the TSS ...
  56. *
  57. * Disable preemption via get_cpu() - we must not switch away
  58. * because the ->io_bitmap_max value must match the bitmap
  59. * contents:
  60. */
  61. tss = &per_cpu(init_tss, get_cpu());
  62. set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
  63. /*
  64. * Search for a (possibly new) maximum. This is simple and stupid,
  65. * to keep it obviously correct:
  66. */
  67. max_long = 0;
  68. for (i = 0; i < IO_BITMAP_LONGS; i++)
  69. if (t->io_bitmap_ptr[i] != ~0UL)
  70. max_long = i;
  71. bytes = (max_long + 1) * sizeof(unsigned long);
  72. bytes_updated = max(bytes, t->io_bitmap_max);
  73. t->io_bitmap_max = bytes;
  74. /* Update the TSS: */
  75. memcpy(tss->io_bitmap, t->io_bitmap_ptr, bytes_updated);
  76. put_cpu();
  77. return 0;
  78. }
  79. /*
  80. * sys_iopl has to be used when you want to access the IO ports
  81. * beyond the 0x3ff range: to get the full 65536 ports bitmapped
  82. * you'd need 8kB of bitmaps/process, which is a bit excessive.
  83. *
  84. * Here we just change the flags value on the stack: we allow
  85. * only the super-user to do it. This depends on the stack-layout
  86. * on system-call entry - see also fork() and the signal handling
  87. * code.
  88. */
  89. static int do_iopl(unsigned int level, struct pt_regs *regs)
  90. {
  91. unsigned int old = (regs->flags >> 12) & 3;
  92. if (level > 3)
  93. return -EINVAL;
  94. /* Trying to gain more privileges? */
  95. if (level > old) {
  96. if (!capable(CAP_SYS_RAWIO))
  97. return -EPERM;
  98. }
  99. regs->flags = (regs->flags & ~X86_EFLAGS_IOPL) | (level << 12);
  100. return 0;
  101. }
  102. #ifdef CONFIG_X86_32
  103. long sys_iopl(struct pt_regs *regs)
  104. {
  105. unsigned int level = regs->bx;
  106. struct thread_struct *t = &current->thread;
  107. int rc;
  108. rc = do_iopl(level, regs);
  109. if (rc < 0)
  110. goto out;
  111. t->iopl = level << 12;
  112. set_iopl_mask(t->iopl);
  113. out:
  114. return rc;
  115. }
  116. #else
  117. asmlinkage long sys_iopl(unsigned int level, struct pt_regs *regs)
  118. {
  119. return do_iopl(level, regs);
  120. }
  121. #endif