Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 12 Oct 2015 08:52:27 -0600
From:      Warner Losh <imp@bsdimp.com>
To:        Venkateswara Rao Dokku <dvrao.584@gmail.com>
Cc:        "freebsd-drivers@freebsd.org" <freebsd-drivers@freebsd.org>
Subject:   Re: Regarding User Space access of IO ports
Message-ID:  <CANCZdfrAHAa5cZXSwEgw9g6%2Bu6VfC48XD1v6_pK1kiE6UfM9cw@mail.gmail.com>
In-Reply-To: <CACiOAOCUP8KPSmzyGWJVKxiuDzKPkzNjk1wfz2W5Qi4z%2Bjp4EA@mail.gmail.com>

index | next in thread | previous in thread | raw e-mail

On Mon, Oct 12, 2015 at 5:45 AM, Venkateswara Rao Dokku <dvrao.584@gmail.com
> wrote:

> Hi,
>
> I am trying to access the IO ports from user space. This I am doing by
> having an ioctl that will return the address of the IO port & I am using
> this IO port address returned from kernel, in user space application to
> write to that port via outw();
>
> In short, I did this
>
> 1. Give IOCTL to kernel which will return the IO port address
> 2. FD = open("/dev/io",O_RDWR) in my user space app, this will give the
> privilege to user space to write to the IO address
> 3. outw( ioport_addr+ offset, value)
>
> The question here is, how the outw() work? Does it write to the address
> directly as in kernel  mode or will it give an ioctl on the FD which will
> result in kernel switch?
>

The bit in the CPU that allows I/O port access is set when the process
returns
from kernel mode.

/dev/io is implemented in sys/dev/io. ioopen() does some securitychecks and
then calls iodev_open, which lives in the i386 specific code. It does this
rather
simple looking thing:

        td->td_frame->tf_eflags |= PSL_IOPL;

which sets the IOPL bit in eflags. IOPL is the bit that controls access to
I/O

ports on the Intel architecture. The non-obvious bit here that you need to

know is that on return to usermode, the PSL is loaded with tf_eflags which

normally has this bit clear. td is the thread that opens /dev/io. Code for

amd64 is similar.


There's no ioctl to do this writing, so the /dev/io driver does do anything

other than have this side effect in the process' PSL. This is a bit odd,

but it's how it works.


Warner


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CANCZdfrAHAa5cZXSwEgw9g6%2Bu6VfC48XD1v6_pK1kiE6UfM9cw>