Re: what is wrong with this?

Philip Blundell (pjb27@cam.ac.uk)
Tue, 7 Jan 1997 12:25:56 +0000 (GMT)


On Mon, 6 Jan 1997, Andrew E. Mileski wrote:

> > #include <asm/io.h>
> > void main()
> > {
> > long port;
> > short data;
> >
> > port = 0x8370;
> > data = 0;
> > iopl(3);
> > (*(volatile unsigned short *)(port))=0;
> > }
> >
> > running this as root should not segv. However, it does. Why?
> > tried linux 2.1.14 and 2.1.20...
>
> Try reading the man pages :-)
>
> You cannot access ports > 0x3ff without calling ioperm().
> The iopl() man pages refer you to ioperm() for a reason.
>
> Explanation: A bit map is used to specify what ports a process
> can access. By default, only the first 1024 are mapped since they
> are commonly used. This mapping requires 128 bytes! Now it is a waste
> to lug around any more if they are not being used - it takes 8k to
> map the entire I/O space.

Bzzt. Sorry, but thanks for playing.

Firstly, Todd was doing _memory_ access, not io-port access. Secondly,
you've got iopl() and ioperm() backwards - it's ioperm() that only works
for ports below 0x400, for the reasons you gave. iopl(3) should give you
access to any ports.

The reason the code faults, I imagine, is that it's trying to access
memory that plain doesn't exist. All the privilege in the world won't
protect you from a fault when you try to write to an unmapped page.

P.