Technical article 100821

How can I access ports through inb, outb instructions from user space?

OpenServer 5.0.0

Written · Last revised

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"

From the Xinuos technical article database, record 100821. Reproduced with permission. The original is at www.sco.com/ta/100821, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.