ioport.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #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. int i;
  22. if (new_value)
  23. for (i = base; i < base + extent; i++)
  24. __set_bit(i, bitmap);
  25. else
  26. for (i = base; i < base + extent; i++)
  27. clear_bit(i, bitmap);
  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. unsigned int i, max_long, bytes, bytes_updated;
  35. struct thread_struct * t = &current->thread;
  36. struct tss_struct * tss;
  37. unsigned long *bitmap;
  38. if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
  39. return -EINVAL;
  40. if (turn_on && !capable(CAP_SYS_RAWIO))
  41. return -EPERM;
  42. /*
  43. * If it's the first ioperm() call in this thread's lifetime, set the
  44. * IO bitmap up. ioperm() is much less timing critical than clone(),
  45. * this is why we delay this operation until now:
  46. */
  47. if (!t->io_bitmap_ptr) {
  48. bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
  49. if (!bitmap)
  50. return -ENOMEM;
  51. memset(bitmap, 0xff, IO_BITMAP_BYTES);
  52. t->io_bitmap_ptr = bitmap;
  53. set_thread_flag(TIF_IO_BITMAP);
  54. }
  55. /*
  56. * do it in the per-thread copy and in the TSS ...
  57. *
  58. * Disable preemption via get_cpu() - we must not switch away
  59. * because the ->io_bitmap_max value must match the bitmap
  60. * contents:
  61. */
  62. tss = &per_cpu(init_tss, get_cpu());
  63. set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
  64. /*
  65. * Search for a (possibly new) maximum. This is simple and stupid,
  66. * to keep it obviously correct:
  67. */
  68. max_long = 0;
  69. for (i = 0; i < IO_BITMAP_LONGS; i++)
  70. if (t->io_bitmap_ptr[i] != ~0UL)
  71. max_long = i;
  72. bytes = (max_long + 1) * sizeof(long);
  73. bytes_updated = max(bytes, t->io_bitmap_max);
  74. t->io_bitmap_max = bytes;
  75. /* Update the TSS: */
  76. memcpy(tss->io_bitmap, t->io_bitmap_ptr, bytes_updated);
  77. put_cpu();
  78. return 0;
  79. }
  80. /*
  81. * sys_iopl has to be used when you want to access the IO ports
  82. * beyond the 0x3ff range: to get the full 65536 ports bitmapped
  83. * you'd need 8kB of bitmaps/process, which is a bit excessive.
  84. *
  85. * Here we just change the eflags value on the stack: we allow
  86. * only the super-user to do it. This depends on the stack-layout
  87. * on system-call entry - see also fork() and the signal handling
  88. * code.
  89. */
  90. asmlinkage long sys_iopl(unsigned int level, struct pt_regs *regs)
  91. {
  92. unsigned int old = (regs->eflags >> 12) & 3;
  93. if (level > 3)
  94. return -EINVAL;
  95. /* Trying to gain more privileges? */
  96. if (level > old) {
  97. if (!capable(CAP_SYS_RAWIO))
  98. return -EPERM;
  99. }
  100. regs->eflags = (regs->eflags &~ 0x3000UL) | (level << 12);
  101. return 0;
  102. }