linux-old/arch/x86_64/kernel/ioport.c
<<
>>
Prefs
   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
   8#include <linux/sched.h>
   9#include <linux/kernel.h>
  10#include <linux/errno.h>
  11#include <linux/types.h>
  12#include <linux/ioport.h>
  13#include <linux/mm.h>
  14#include <linux/slab.h>
  15#include <linux/smp.h>
  16#include <linux/smp_lock.h>
  17#include <linux/stddef.h>
  18
  19/* Set EXTENT bits starting at BASE in BITMAP to value TURN_ON. */
  20static void set_bitmap(unsigned long *bitmap, short base, short extent, int new_value)
  21{
  22        int i;
  23                if (new_value)
  24                for (i = base; i < base + extent; i++) 
  25                        __set_bit(i, bitmap); 
  26                else
  27                for (i = base; i < base + extent; i++) 
  28                        clear_bit(i, bitmap); 
  29}
  30
  31/*
  32 * this changes the io permissions bitmap in the current task.
  33 */
  34asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
  35{
  36        struct thread_struct * t = &current->thread;
  37        struct tss_struct * tss = init_tss + smp_processor_id();
  38
  39        if ((from + num <= from) || (from + num > IO_BITMAP_SIZE*32))
  40                return -EINVAL;
  41        if (turn_on && !capable(CAP_SYS_RAWIO))
  42                return -EPERM;
  43        /*
  44         * If it's the first ioperm() call in this thread's lifetime, set the
  45         * IO bitmap up. ioperm() is much less timing critical than clone(),
  46         * this is why we delay this operation until now:
  47         */
  48        if (!t->io_bitmap_ptr) {
  49                t->io_bitmap_ptr = kmalloc((IO_BITMAP_SIZE+1)*4, GFP_KERNEL);
  50                if (!t->io_bitmap_ptr) 
  51                        return -ENOMEM; 
  52
  53                memset(t->io_bitmap_ptr,0xff,(IO_BITMAP_SIZE+1)*4);
  54                /*
  55                 * this activates it in the TSS
  56                 */
  57        }
  58
  59        /*
  60         * do it in the per-thread copy and in the TSS ...
  61         */
  62        set_bitmap((unsigned long *) t->io_bitmap_ptr, from, num, !turn_on);
  63        if (tss->io_map_base != IO_BITMAP_OFFSET) { 
  64                memcpy(tss->io_bitmap, t->io_bitmap_ptr, sizeof(tss->io_bitmap));
  65                tss->io_map_base = IO_BITMAP_OFFSET;
  66        } else { 
  67        set_bitmap((unsigned long *) tss->io_bitmap, from, num, !turn_on);
  68        }
  69
  70        return 0;
  71}
  72
  73/*
  74 * sys_iopl has to be used when you want to access the IO ports
  75 * beyond the 0x3ff range: to get the full 65536 ports bitmapped
  76 * you'd need 8kB of bitmaps/process, which is a bit excessive.
  77 *
  78 * Here we just change the eflags value on the stack: we allow
  79 * only the super-user to do it. This depends on the stack-layout
  80 * on system-call entry - see also fork() and the signal handling
  81 * code.
  82 */
  83
  84asmlinkage long sys_iopl(unsigned int level, struct pt_regs regs)
  85{
  86        unsigned int old = (regs.eflags >> 12) & 3;
  87
  88        if (level > 3)
  89                return -EINVAL;
  90        /* Trying to gain more privileges? */
  91        if (level > old) {
  92                if (!capable(CAP_SYS_RAWIO))
  93                        return -EPERM;
  94        }
  95        regs.eflags = (regs.eflags & 0xffffffffffffcfff) | (level << 12);
  96        return 0;
  97}
  98
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.