ioport.c 3.8 KB

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