Problem
I want to access ports through the inb, outb family of i386 instructions from a user space program.
Solution
You first need to create the privilege to use the inb, outb type instructions through a sysi86(S) call. It is then highly recommended to use the below assembler wrappers for inb(), outb(), etc. functions.
The following code can be used as an example:
-----------cut here for setiopl.c file ------------------------------------#include <sys/types.h>
#include <sys/tss.h>
#include <sys/immu.h>
#include <sys/region.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/v86.h>
#include <sys/sysi86.h>
#include "inline.h"set_iopl() { if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) < 0) perror("V86SC_IOPL"); }
-----------cut here for inline.h file --------------------------------------asm void
outl(int port, ulong_t val)
{
%treg port, val;
pushl port
movl val, %eax
popl %edx
outl (%dx)
%treg port; mem val;
movl port, %edx
movl val, %eax
outl (%dx)
%mem port, val;
movl val, %eax
movl port, %edx
outl (%dx)
}asm void
outw(int port, ushort_t val)
{
%treg port, val;
pushl port
movw val, %ax
popl %edx
data16
outl (%dx)
%treg port; mem val;
movl port, %edx
movw val, %ax
data16
outl (%dx)
%mem port, val;
movw val, %ax
movl port, %edx
data16
outl (%dx)
}asm void
outb(int port, uchar_t val)
{
%treg port, val;
pushl port
movb val, %al
popl %edx
outb (%dx)
%treg port; mem val;
movl port, %edx
movb val, %al
outb (%dx)
%mem port, val;
movb val, %al
movl port, %edx
outb (%dx)
}asm ulong_t
inl(int port)
{
%mem port;
movl port, %edx
inl (%dx)
}asm ushort_t
inw(int port)
{
%mem port;
movl port, %edx
xorl %eax, %eax
data16
inl (%dx)
}asm uchar_t
inb(int port)
{
%mem port;
movl port, %edx
xorl %eax, %eax
inb (%dx)
}
----------------------------------------------------------------------------NOTE: The Microsoft C compiler of the Cross development system does
not support the asm routines in the inline.h file. For an
alternative method see the article mentioned in the next
section. See also
inb(K), outb(K), sysi86(S)TA100410, "How to make the special devices /dev/inb and /dev/outb"