linux 访问i o地址,如果在linux中访问i/o端口?

从用户空间的 I/O 存取

可从用户空间使用, 至少在 PC-类 的计算机. GNU C 库在  中定义它们.

下列条件应当应用来对于 inb 及其友在用户空间代码中使用:

程序必须使用 -O 选项编译来强制扩展内联函数.

ioperm 和 iopl 系统调用必须用来获得权限来进行对端口的 I/O 操作. ioperm 为单独端口获取许可, 而 iopl 为整个 I/O 空间获取许可.这 2 个函数都是 x86 特有的.

程序必须作为 root 来调用 ioperm 或者 iopl.[34] 可选地, 一个它的祖先必须已赢得作为 root 运行的端口权限.

如果主机平台没有 ioperm 和 iopl 系统调用, 用户空间仍然可以存取 I/O 端口, 通过使用 /dev/prot 设备文件. 注意, 但是, 这个文件的含义是非常平台特定的, 并且对任何东西除了 PC 不可能有用.

例子源码 misc-progs/inp.c 和 misc-progs/outp.c 是一个从命令行读写端口的小工具, 在用户空间. 它们希望被安装在多个名子下

(例如, inb, inw, 和 inl 并且操作字节, 字, 或者长端口依赖于用户调用哪个名子).

它们使用 ioperm 或者 iopl 在 x86下, 在其他平台是 /dev/port.

程序可以做成 setuid root, 如果你想过危险生活并且在不要求明确的权限的情况下使用你的硬件.

但是, 请不要在产品系统上以 set-uid 安装它们; 它们是设计上的安全漏洞.

/*

* inp.c -- read all the ports specified in hex on the command line.

*     The program uses the faster ioperm/iopl calls on x86, /dev/port

*     on other platforms. The program acts as inb/inw/inl according

*     to its own name

*

* Copyright (C) 1998,2000,2001 Alessandro Rubini

*

*   This program is free software; you can redistribute it and/or modify

*   it under the terms of the GNU General Public License as published by

*   the Free Software Foundation; either version 2 of the License, or

*   (at your option) any later version.

*

*   This program 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 General Public License for more details.

*

*   You should have received a copy of the GNU General Public License

*   along with this program; if not, write to the Free Software

*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

*/

#include

#include

#include

#include

#include

#include

#include

#include

#include  /* linux-specific */

#ifdef __GLIBC__

#  include

#endif

#define PORT_FILE "/dev/port"

char *prgname;

#ifdef __i386__

static int read_and_print_one(unsigned int port,int size)

{

static int iopldone = 0;

if (port > 1024) {

if (!iopldone && iopl(3)) {

fprintf(stderr, "%s: iopl(): %sn", prgname, strerror(errno));

return 1;

}

iopldone++;

} else if (ioperm(port,size,1)) {

fprintf(stderr, "%s: ioperm(%x): %sn", prgname,

port, strerror(errno));

return 1;

}

if (size == 4)

printf("%04x: %08xn", port, inl(port));

else if (size == 2)

printf("%04x: %04xn", port, inw(port));

else

printf("%04x: %02xn", port, inb(port));

return 0;

}

#else /* not i386 */

static int read_and_print_one(unsigned int port,int size)

{

static int fd = -1;

unsigned char b; unsigned short w; unsigned int l;

if (fd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值