当前位置: 技术问答>linux和unix
如果在linux中访问i/o端口?
来源: 互联网 发布时间:2016-03-04
本文导语: 以及如何访问物理内存,有没有现成的驱动以及应用程序接口,象windows中的winio驱动一样。急! | 从用户空间的 I/O 存取 可从用户空间使用, 至少在 PC-类 的计算机. GNU C 库在 中定义它...
以及如何访问物理内存,有没有现成的驱动以及应用程序接口,象windows中的winio驱动一样。急!
|
从用户空间的 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 安装它们; 它们是设计上的安全漏洞.
可从用户空间使用, 至少在 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
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!