Subject: Re: netbsd i386 1.6.1 question: (user mode hw io)
To: der Mouse <mouse@Rodents.Montreal.QC.CA>
From: Peter L. Peres <plp@actcom.co.il>
List: port-i386
Date: 12/23/2004 15:01:17
On Thu, 23 Dec 2004, der Mouse wrote:

>> I have a small question.  I am trying to port an old usermode hw io
>> testing program that I wrote a long time ago from linux to netbsd.
>> My question is, how does one do user mode hw io using bus_space_...
>
> One doesn't.  The bus-space functions - and perhaps more importantly,
> the support behind them - are kernel-only.  Indeed, one of the kernel's
> jobs is to keep userland away from direct access to the hardware.
>
>> So, am I trying to do something impossible, or not ?  i.e. can I
>> compile a user mode program, to be run as root, which uses
>> bus_space_peek and _poke, or do I have to write a proper driver ?
>
> You'll need to write a driver (though for memory-mapped stuff, you can
> in principle use /dev/mem - depending on your device's interface, this
> may be good enough).
>
> Unless your device has requirements such as DMA or tight timing, you
> may well be able to get away with a very basic driver that just gives
> userland a more or less direct interface to the appropriate bus-space
> calls.  (It would be an immense security hole for normal use, or
> distribution, but it may nevertheless be suitable on an
> experimental-hackery system.)

Ok, thanks. So it has to be a proper device driver. Sigh. It would have 
to be /dev/io for i386 and I do not know what for something else ;-(.

The program in cause was written specifically for poking and peeking 
simple devices that do not yet have a driver. No dma or interrupts are 
involved. /dev/io will do fine except:

man io:
"NAME
      io - I/O privilege file

DESCRIPTION
      This device is obsolete and is provided for compatibility purposes only;
      use i386_iopl(2) instead.

"

hmm? This is implemented in libi386. Could I define my own bus_space_map 
using i386_iopl and i386_ioperm and link against libi386 ? Would that 
fix it to work now, and perhaps after /dev/io is obsoleted ? i.e. if I 
defined my own bus_space_map and _unmap so I can compile by linking 
against -li386, would the kernel prevent me from using that program ? 
Also, given the definition of bus_space_poke & _peek, would an existing 
device driver that occupies the relevant ports refuse to let me access 
them ? (the point is to access them anyway - this is for testing stuck 
hardware etc).

"
      I/O privilege on access to /dev/io is only granted if the kernel was
      built with the COMPAT_10 option.
"

And where can I determine whether it was from user mode, while running 
as root ?

Peter