* ioperm() and iopl()
@ 1996-06-17 11:23 Martin Ostermann
1996-06-17 13:55 ` Erik Troan
0 siblings, 1 reply; 5+ messages in thread
From: Martin Ostermann @ 1996-06-17 11:23 UTC (permalink / raw)
Ioperm() and iopl() seem to be sytem calls on any Linux platform,
except for Alpha. What's the reason for this?
When I use iopl() in an app, I'll get an runtime error, somewhat like
"Couldn't determine system type -- Maybe /etc/alpha_io_system symlink
is missing". Well, it is missing, but where is it supposed to point
to? I don't have libc sources hanging around to look it up, so it
would be neat if somebody could help me.
Thanks, Martin
--
Martin Ostermann | mailto:ost@comnets.rwth-aachen.de
Communication Networks | http://www.comnets.rwth-aachen.de/~ost
Aachen University of Technology | phoneto:++49/241/807917
Germany | faxto:++49/241/8890378
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: ioperm() and iopl()
1996-06-17 11:23 Martin Ostermann
@ 1996-06-17 13:55 ` Erik Troan
0 siblings, 0 replies; 5+ messages in thread
From: Erik Troan @ 1996-06-17 13:55 UTC (permalink / raw)
On Mon, 17 Jun 1996, Martin Ostermann wrote:
> Ioperm() and iopl() seem to be sytem calls on any Linux platform,
> except for Alpha. What's the reason for this?
There isn't an IO bus on the Alpha - it's all done through memory mappings.
> When I use iopl() in an app, I'll get an runtime error, somewhat like
> "Couldn't determine system type -- Maybe /etc/alpha_io_system symlink
> is missing". Well, it is missing, but where is it supposed to point
> to? I don't have libc sources hanging around to look it up, so it
> would be neat if somebody could help me.
That's broken. By default, it looks in /proc/cpuinfo. What does yours
say?
Erik
-------------------------------------------------------------------------------
Always hoped that I'd be an apostle. Knew that I would make it if I tried.
Then when we retire we can write the gospels so they'll all talk about
us when we die. - "The Last Supper" from Jesus Christ Superstar
| Erik Troan = http://sunsite.unc.edu/ewt/ = ewt@sunsite.unc.edu |
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: ioperm() and iopl()
@ 1996-06-17 16:39 David Mosberger-Tang
0 siblings, 0 replies; 5+ messages in thread
From: David Mosberger-Tang @ 1996-06-17 16:39 UTC (permalink / raw)
>>>>> On Mon, 17 Jun 1996 13:23:48 +0200, Martin Ostermann <ost@comnets.rwth-aachen.de> said:
Martin> Ioperm() and iopl() seem to be sytem calls on any Linux
Martin> platform, except for Alpha. What's the reason for this?
Well, Alpha != x86 is the reason. In particular, the Alpha doesn't
have ioperm or iopl-equivalent instructions. On the Alpha, all I/O is
done through memory-mapped I/O (and there are good reasons for this).
It certainly would be possible to make them syscalls, but this would
mean that all port reads/writes (inb/outb etc) would _also_ have to be
syscalls. I don't think anybody would like that performance-wise.
For this reason, ioperm() and iopl() are emulation routines in the
library that simply mmap() the appropriate portion of the kernel
address space, so port I/O can be done in user-level space.
BTW: If you say that "all other architecture have iopl() as a
syscall"--- well, yes, sort of. Here is what the MIPS has:
asmlinkage int sys_iopl(long ebx,long ecx,long edx,
long esi, long edi, long ebp, long eax, long ds,
long es, long fs, long gs, long orig_eax,
long eip,long cs,long eflags,long esp,long ss)
{
return -ENOSYS;
}
Not particularly useful, is it? ;-) In other words: ioperm() and
iopl() are non-portable and should be avoided whenever possible. It
just so happens that the Alpha PCs have an I/O system that looks
identical to an x86 PC, which makes it easy to implement a meaningful
iopl() and ioperm() in user-level.
Martin> When I use iopl() in an app, I'll get an runtime error,
Martin> somewhat like "Couldn't determine system type -- Maybe
Martin> /etc/alpha_io_system symlink is missing". Well, it is
Martin> missing, but where is it supposed to point to? I don't have
Martin> libc sources hanging around to look it up, so it would be
Martin> neat if somebody could help me.
What does "cat /proc/cpuinfo" show? I think you have a Jensen, so
there should be a line like this:
system type : Jensen
If not, you can force the right system type with:
ln -s Jensen /etc/alpha_io_system
--david
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: ioperm() and iopl()
@ 1996-06-18 12:24 Martin Ostermann
0 siblings, 0 replies; 5+ messages in thread
From: Martin Ostermann @ 1996-06-18 12:24 UTC (permalink / raw)
>>>>> In article <Pine.LNX.3.91.960617095451.2573H-100000@elroy.redhat.com>, Erik Troan <ewt@redhat.com> writes:
>> When I use iopl() in an app, I'll get an runtime error, somewhat like
>> "Couldn't determine system type -- Maybe /etc/alpha_io_system symlink
>> is missing". Well, it is missing, but where is it supposed to point
>> to? I don't have libc sources hanging around to look it up, so it
>> would be neat if somebody could help me.
> That's broken. By default, it looks in /proc/cpuinfo. What does yours
> say?
Well, it contains a line like
system type : Jensen
as David suggested below.
>>>>> In article <199606171639.JAA27270@koala.azstarnet.com>, David Mosberger-Tang <davidm@AZStarNet.com> writes:
> Not particularly useful, is it? ;-) In other words: ioperm() and
> iopl() are non-portable and should be avoided whenever possible. It
> just so happens that the Alpha PCs have an I/O system that looks
> identical to an x86 PC, which makes it easy to implement a meaningful
> iopl() and ioperm() in user-level.
Ok, so I have to do some calculations were ports are located in sparse
memory by myself, but that shouldn't be a problem.
Martin> When I use iopl() in an app, I'll get an runtime error,
Martin> somewhat like "Couldn't determine system type -- Maybe
Martin> /etc/alpha_io_system symlink is missing". Well, it is
Martin> missing, but where is it supposed to point to? I don't have
Martin> libc sources hanging around to look it up, so it would be
Martin> neat if somebody could help me.
> What does "cat /proc/cpuinfo" show? I think you have a Jensen, so
> there should be a line like this:
> system type : Jensen
> If not, you can force the right system type with:
> ln -s Jensen /etc/alpha_io_system
Hmm, /proc/cpuinfo is available and contains such a line. I
nevertheless made the link (You really meant this: a link pointing to
a (possibly nonexisting) file name Jensen? -- not a file containing a
line "Jensen?") and the message disappeared. However, iopl() returns
non-zero, which indicates failure, and I still get segmentation faults
upon using the ports.
This was just for the record, I'll next try to allocate those ports
via normal memeory.
Thanks to both of you for your answers, Martin
--
Martin Ostermann | mailto:ost@comnets.rwth-aachen.de
Communication Networks | http://www.comnets.rwth-aachen.de/~ost
Aachen University of Technology | phoneto:++49/241/807917
Germany | faxto:++49/241/8890378
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: ioperm() and iopl()
@ 1996-06-18 15:20 David Mosberger-Tang
0 siblings, 0 replies; 5+ messages in thread
From: David Mosberger-Tang @ 1996-06-18 15:20 UTC (permalink / raw)
>>>>> On Tue, 18 Jun 1996 14:24:19 +0200, Martin Ostermann <ost@comnets.rwth-aachen.de> said:
Martin> Well, it contains a line like
Martin> system type : Jensen
Martin> as David suggested below.
Can you find find out why it doesn't work with /proc/cpuinfo? All you
should have to do is link with -lg and then set a breakpoint in
init_iosys.
Martin> Ok, so I have to do some calculations were ports are located
Martin> in sparse memory by myself, but that shouldn't be a problem.
I'm not sure what you mean here. The libc i/o emulation should give
you access to any port in the range 0..65535 whether it's a Jensen or
any other Alpha PC. The Jensen does have some devices on strange
addresses, but other than that, it should work.
Martin> Hmm, /proc/cpuinfo is available and contains such a line. I
Martin> nevertheless made the link (You really meant this: a link
Martin> pointing to a (possibly nonexisting) file name Jensen? --
Martin> not a file containing a line "Jensen?") and the message
Martin> disappeared. However, iopl() returns non-zero, which
Martin> indicates failure, and I still get segmentation faults upon
Martin> using the ports.
What exactly does your program do? Some caveats:
o the i/o privilege level has to be > 3 to gain access to all
ports
o only root can access the i/o ports
If it still fails, please tell us what "errno" is set to afterwards.
For your information, I appended the corresponding file from glibc.
Except for minor changes, the version in the Red Hat libc should be
identical.
--david
---
/* Copyright (C) 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by David Mosberger.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
/* I/O access is restricted to ISA port space (ports 0..65535).
Modern devices hopefully are sane enough not to put any performance
critical registers in i/o space.
On the first call to ioperm() or _sethae(), the entire (E)ISA port
space is mapped into the virtual address space at address io.base.
mprotect() calls are then used to enable/disable access to ports. Per
page, there are PAGE_SIZE>>IO_SHIFT I/O ports (e.g., 256 ports on a
Low Cost Alpha based system using 8KB pages).
Keep in mind that this code should be able to run in a 32bit address
space. It is therefore unreasonable to expect mmap'ing the entire
sparse address space would work (e.g., the Low Cost Alpha chip has an
I/O address space that's 512MB large!). */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <asm/page.h>
#include <asm/system.h>
#define PATH_ALPHA_SYSTYPE "/etc/alpha_systype"
#define PATH_CPUINFO "/proc/cpuinfo"
#define MAX_PORT 0x10000
#define vuip volatile unsigned int *
#define JENSEN_IO_BASE (0xfffffc0300000000UL)
#define JENSEN_MEM (0xfffffc0200000000UL) /* sparse!! */
/*
* With respect to the I/O architecture, APECS and LCA are identical,
* so the following defines apply to LCA as well.
*/
#define APECS_IO_BASE (0xfffffc01c0000000UL)
#define APECS_DENSE_MEM (0xfffffc0300000000UL)
#define ALCOR_IO_BASE (0xfffffc8580000000UL)
#define ALCOR_DENSE_MEM (0xfffffc8600000000UL)
enum {
IOSYS_JENSEN = 0, IOSYS_APECS = 1, IOSYS_ALCOR = 2
} iosys_t;
struct ioswtch {
void (*sethae)(unsigned long addr);
void (*outb)(unsigned char b, unsigned long port);
void (*outw)(unsigned short b, unsigned long port);
void (*outl)(unsigned int b, unsigned long port);
unsigned int (*inb)(unsigned long port);
unsigned int (*inw)(unsigned long port);
unsigned int (*inl)(unsigned long port);
};
static struct platform {
const char *name;
int io_sys;
unsigned long bus_memory_base;
} platform[] = {
{"Alcor", IOSYS_ALCOR, ALCOR_DENSE_MEM},
{"Avanti", IOSYS_APECS, APECS_DENSE_MEM},
{"Cabriolet", IOSYS_APECS, APECS_DENSE_MEM},
{"EB164", IOSYS_ALCOR, ALCOR_DENSE_MEM},
{"EB64+", IOSYS_APECS, APECS_DENSE_MEM},
{"EB66", IOSYS_APECS, APECS_DENSE_MEM}, /* LCA same as APECS */
{"EB66P", IOSYS_APECS, APECS_DENSE_MEM}, /* LCA same as APECS */
{"Jensen", IOSYS_JENSEN, JENSEN_MEM},
{"Mustang", IOSYS_APECS, APECS_DENSE_MEM},
{"Noname", IOSYS_APECS, APECS_DENSE_MEM}, /* LCA same as APECS */
};
static struct {
struct hae {
unsigned long cache;
unsigned long * reg;
} hae;
unsigned long base;
struct ioswtch * swp;
int sys;
} io;
static unsigned long bus_memory_base = -1;
extern void __sethae (unsigned long); /* we can't use asm/io.h */
static inline unsigned long
port_to_cpu_addr (unsigned long port, int iosys, int size)
{
if (iosys == IOSYS_JENSEN)
{
return (port << 7) + ((size - 1) << 4) + io.base;
}
else
{
return (port << 5) + ((size - 1) << 3) + io.base;
}
}
static inline void
inline_sethae (unsigned long addr, int iosys)
{
if (iosys == IOSYS_JENSEN)
{
/* hae on the Jensen is bits 31:25 shifted right */
addr >>= 25;
if (addr != io.hae.cache)
{
__sethae (addr);
io.hae.cache = addr;
}
}
else
{
unsigned long msb;
/* no need to set hae if msb is 0: */
msb = addr & 0xf8000000;
if (msb && msb != io.hae.cache)
{
__sethae (msb);
io.hae.cache = msb;
}
}
}
static inline void
inline_outb (unsigned char b, unsigned long port, int iosys)
{
unsigned int w;
unsigned long addr = port_to_cpu_addr (port, iosys, 1);
inline_sethae (0, iosys);
asm ("insbl %2,%1,%0" : "r=" (w) : "ri" (port & 0x3), "r" (b));
*(vuip)addr = w;
mb ();
}
static inline void
inline_outw (unsigned short b, unsigned long port, int iosys)
{
unsigned int w;
unsigned long addr = port_to_cpu_addr (port, iosys, 2);
inline_sethae (0, iosys);
asm ("inswl %2,%1,%0" : "r=" (w) : "ri" (port & 0x3), "r" (b));
*(vuip)addr = w;
mb ();
}
static inline void
inline_outl (unsigned int b, unsigned long port, int iosys)
{
unsigned long addr = port_to_cpu_addr (port, iosys, 4);
if (port >= MAX_PORT)
return;
inline_sethae (0, iosys);
*(vuip)addr = b;
mb ();
}
static inline unsigned int
inline_inb (unsigned long port, int iosys)
{
unsigned long result, addr = port_to_cpu_addr (port, iosys, 1);
inline_sethae (0, iosys);
result = *(vuip) addr;
result >>= (port & 3) * 8;
return 0xffUL & result;
}
static inline unsigned int
inline_inw (unsigned long port, int iosys)
{
unsigned long result, addr = port_to_cpu_addr (port, iosys, 2);
inline_sethae (0, iosys);
result = *(vuip) addr;
result >>= (port & 3) * 8;
return 0xffffUL & result;
}
static inline unsigned int
inline_inl (unsigned long port, int iosys)
{
unsigned long addr = port_to_cpu_addr (port, iosys, 4);
inline_sethae (0, iosys);
return *(vuip) addr;
}
#define DCL_SETHAE(name, iosys) \
static void \
name##_sethae (unsigned long addr) \
{ \
inline_sethae (addr, IOSYS_##iosys); \
}
#define DCL_OUT(name, func, type, iosys) \
static void \
name##_##func (unsigned type b, unsigned long addr) \
{ \
inline_##func (b, addr, IOSYS_##iosys); \
}
#define DCL_IN(name, func, iosys) \
static unsigned int \
name##_##func (unsigned long addr) \
{ \
return inline_##func (addr, IOSYS_##iosys); \
}
DCL_SETHAE(jensen, JENSEN)
DCL_OUT(jensen, outb, char, JENSEN)
DCL_OUT(jensen, outw, short, JENSEN)
DCL_OUT(jensen, outl, int, JENSEN)
DCL_IN(jensen, inb, JENSEN)
DCL_IN(jensen, inw, JENSEN)
DCL_IN(jensen, inl, JENSEN)
/* The APECS functions are also used for ALCOR since they are
identical. */
DCL_SETHAE(apecs, APECS)
DCL_OUT(apecs, outb, char, APECS)
DCL_OUT(apecs, outw, short, APECS)
DCL_OUT(apecs, outl, int, APECS)
DCL_IN(apecs, inb, APECS)
DCL_IN(apecs, inw, APECS)
DCL_IN(apecs, inl, APECS)
struct ioswtch ioswtch[] = {
{
jensen_sethae,
jensen_outb, jensen_outw, jensen_outl,
jensen_inb, jensen_inw, jensen_inl
},
{
apecs_sethae,
apecs_outb, apecs_outw, apecs_outl,
apecs_inb, apecs_inw, apecs_inl
}
};
/*
* Initialize I/O system. To determine what I/O system we're dealing
* with, we first try to read the value of symlink PATH_ALPHA_SYSTYPE,
* if that fails, we lookup the "system type" field in /proc/cpuinfo.
* If that fails as well, we give up.
*/
static int
init_iosys (void)
{
char systype[256];
int i, n;
n = readlink(PATH_ALPHA_SYSTYPE, systype, sizeof(systype) - 1);
if (n > 0)
{
systype[n] = '\0';
}
else
{
char name[256];
FILE * fp;
fp = fopen (PATH_CPUINFO, "r");
if (!fp)
return -1;
while ((n = fscanf (fp, "%256[^:]: %256[^\n]\n", name, systype)) != EOF)
{
if (n == 2 && strncmp (name, "system type", 11) == 0) {
break;
}
}
fclose(fp);
if (n == EOF)
{
/* this can happen if the format of /proc/cpuinfo changes... */
fprintf(stderr,
"ioperm.init_iosys(): Unable to determine system type.\n"
"\t(May need " PATH_ALPHA_SYSTYPE " symlink?)\n");
errno = ENODEV;
return -1;
}
}
/* translate systype name into i/o system: */
for (i = 0; i < sizeof (platform) / sizeof (platform[0]); ++i)
{
if (strcmp (platform[i].name, systype) == 0)
{
bus_memory_base = platform[i].bus_memory_base;
io.sys = platform[i].io_sys;
if (io.sys == IOSYS_JENSEN)
io.swp = &ioswtch[0];
else
io.swp = &ioswtch[1];
return 0;
}
}
/* systype is not a know platform name... */
errno = EINVAL;
return -1;
}
int
_ioperm (unsigned long from, unsigned long num, int turn_on)
{
unsigned long addr, len;
int prot;
if (!io.swp && init_iosys () < 0)
return -1;
/* this test isn't as silly as it may look like; consider overflows! */
if (from >= MAX_PORT || from + num > MAX_PORT)
{
errno = EINVAL;
return -1;
}
if (turn_on)
{
if (!io.base)
{
unsigned long base;
int fd;
io.hae.reg = 0; /* not used in user-level */
io.hae.cache = 0;
__sethae (io.hae.cache); /* synchronize with hw */
fd = open ("/dev/mem", O_RDWR);
if (fd < 0)
return fd;
switch (io.sys)
{
case IOSYS_JENSEN: base = JENSEN_IO_BASE; break;
case IOSYS_APECS: base = APECS_IO_BASE; break;
case IOSYS_ALCOR: base = ALCOR_IO_BASE; break;
default:
errno = ENODEV;
return -1;
}
addr = port_to_cpu_addr (from, io.sys, 1);
addr &= PAGE_MASK;
len = port_to_cpu_addr (MAX_PORT, io.sys, 1) - addr;
io.base =
(unsigned long) __mmap (0, len, PROT_NONE, MAP_SHARED, fd, base);
close (fd);
if ((long) io.base == -1)
return -1;
}
prot = PROT_READ | PROT_WRITE;
}
else
{
if (!io.base)
return 0; /* never was turned on... */
/* turnoff access to relevant pages: */
prot = PROT_NONE;
}
addr = port_to_cpu_addr (from, io.sys, 1);
addr &= PAGE_MASK;
len = port_to_cpu_addr (from + num, io.sys, 1) - addr;
return mprotect ((void *) addr, len, prot);
}
int
_iopl (unsigned int level)
{
if (level > 3)
{
errno = EINVAL;
return -1;
}
if (level)
{
return _ioperm (0, MAX_PORT, 1);
}
return 0;
}
void
_sethae (unsigned long addr)
{
if (!io.swp && init_iosys () < 0)
return;
io.swp->sethae (addr);
}
void
_outb (unsigned char b, unsigned long port)
{
if (port >= MAX_PORT)
return;
io.swp->outb (b, port);
}
void
_outw (unsigned short b, unsigned long port)
{
if (port >= MAX_PORT)
return;
io.swp->outw (b, port);
}
void
_outl (unsigned int b, unsigned long port)
{
if (port >= MAX_PORT)
return;
io.swp->outl (b, port);
}
unsigned int
_inb (unsigned long port)
{
return io.swp->inb (port);
}
unsigned int
_inw (unsigned long port)
{
return io.swp->inw (port);
}
unsigned int
_inl (unsigned long port)
{
return io.swp->inl (port);
}
unsigned long
_bus_base(void)
{
if (!io.swp && init_iosys () < 0)
return -1;
return bus_memory_base;
}
weak_alias (_sethae, sethae);
weak_alias (_ioperm, ioperm);
weak_alias (_iopl, iopl);
weak_alias (_inb, inb);
weak_alias (_inw, inw);
weak_alias (_inl, inl);
weak_alias (_outb, outb);
weak_alias (_outw, outw);
weak_alias (_outl, outl);
weak_alias (_bus_base, bus_base);
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~1996-06-18 15:20 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1996-06-18 12:24 ioperm() and iopl() Martin Ostermann
-- strict thread matches above, loose matches on Subject: below --
1996-06-18 15:20 David Mosberger-Tang
1996-06-17 16:39 David Mosberger-Tang
1996-06-17 11:23 Martin Ostermann
1996-06-17 13:55 ` Erik Troan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox