Subject: Re: hw addressing
To: Peter L. Peres <plp@actcom.co.il>
From: Frank van der Linden <fvdl@netbsd.org>
List: port-i386
Date: 01/19/2005 23:48:02
On Wed, Jan 19, 2005 at 10:46:05PM +0200, Peter L. Peres wrote:
> It does not work from a user application, it has to be done in kernel 
> space, even if the kernel security level is -1. I just went through this 
> recently.

Sure it does. Provided that you're root, of course, this works:


#include <sys/types.h>
#include <machine/sysarch.h>
#include <machine/pio.h>

#include <err.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
	int port;
	uint8_t val;

	if (argc != 2)
		errx(1, "usage: inb <port>");

	port = atoi(argv[1]);
	if (port < 0 || port > 0xffff)
		errx(1, "port out of range");

	if (i386_iopl(2) < 0)
		err(2, "i386_iopl(2)");

	val = inb(port);

	printf("inb(%04x) -> %02x\n", port, val);

	return 0;
}

====

gate# cc -o inb inb.c -li386
gate# ./inb 1016
inb(03f8) -> c7