ioport.c 3.8 KB

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