linux下在用户空间访问I/O端口的ioperm和iopl函数

本文介绍了Linux系统下,用户空间访问I/O端口的两个关键函数:ioperm和iopl。ioperm用于设置低端I/O端口的访问权限,而iopl则允许访问所有65536个端口。通过示例程序展示了如何使用这两个函数读写端口,并讨论了不同内核版本下的行为差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux下在用户空间访问I/O端口的ioperm和iopl函数
    Linux下设置端口权限的系统调用有两个:ioperm和iopl函数。
1.ioperm函数
     功能描述:
为调用进程设置I/O端口访问权能。ioperm的使用需要具有超级用户的权限,只有低端的[0-0x3ff] I/O端口可被设置,要想指定更多端口的权能,可使用iopl函数。这一调用只可用于i386平台。

用法:
#include  /* for libc5 */
#include  /* for glibc */
int ioperm(unsigned long from, unsigned long num, int turn_on);
      
参数:
from:起始端口地址。
num:需要修改权能的端口数。
turn_on:端口的新权能位。1为开启,0为关闭。
返回说明: 
成功执行时,返回0。失败返回-1,errno被设为以下的某个值 
EINVAL:参数无效
EIO:这一调用不被支持
EPERM:调用进程权能不足。
   
2.iopl函数
功能描述:该调用用于修改当前进程的操作端口的权限。可以用于所有65536个端口的权限。因此,ioperm相当于该调用的子集。和ioperm一样,这一调用仅适用于i386平台。

用法:
#include 
   int iopl(int level);
参数:
level: 端口的权限级别。为3时可以读写端口。默认权能级别为0,用户空间不可读写。
返回说明:成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EINVAL:level值大于3
ENOSYS:未实现该调用
EPERM:调用进程权能不足。

二、程序示例
1. ioperm.c
操作低于0x3FF的端口
该程序首先设置0x3FF端口的读写权限,然后读出原先的值,然后将原值的LSB翻转并写回端口,并在此读取端口值。
/*Godbach. Dec 18, 2008
Description:This function is used to test ioperm()*/
#include 
#include  
#include  
#define PORT_ADDR 0x3FF
int main(void)
{
      int ret;
      char port_val;

      /*set r/w permission of port_addr on, only one port*/
      ret =ioperm(PORT_ADDR, 1, 1);
      if(ret) 
     
 	{
    		perror("iopermset error");
           	return 0;
     	 }
      port_val =inb(PORT_ADDR);
      printf("Originalvalue of port 0x%x is : %.2x\n", PORT_ADDR, port_val);
      /*reverse the least significant bit */
      outb(port_val^0x01, PORT_ADDR);
      port_val = inb(PORT_ADDR);
      printf("Current value of port 0x%x is : %.2x\n", PORT_ADDR, port_val);      
      /*set r/w permission of PORT_ADDR off, only one port*/
      ret = ioperm(PORT_ADDR, 1, 0);
      if(ret)
      { 
           perror("iopermset error");
           return 0;
      }
      return 0;
}

程序执行的结果是:
[root@localhost misc-progs]# ./a.out 
Original value of port 0x3ff is : 00
Current value of port 0x3ff is : 01
[root@localhost misc-progs]# ./a.out 
Original value of port 0x3ff is : 01
Current value of port 0x3ff is : 00
该程序执行几次,将进行几次的LSB翻转。
这里有一个问题值得注意:在2.4(RH9)的内核上,当端口值大于0x3FF时,执行该程序则会报错:ioperm set error: Invalid
argument。即IO端口的值设置有问题,超出了限制。但是在2.6内核下并没有报错,而且执行结果也符合程序既定的结果。但是man ioperm中仍然说明了0x3FF的限制。暂且存疑。

2. iopl.c
该程序可以操作所有65536个端口。
该程序首先设置0x3FF端口的读写权限,然后读出原先的值,然后将原值的LSB翻转并写回端口,并在此读取端口值。
代码如下:
/*Godbach. Dec 18, 2008
Description:This function is used to test iopl()*/
#include 
#include  
#include  

#define PORT_ADDR 0x3FF

int main(void)
{
      int ret;
      char port_val;

      /*set r/w permission of all 65536 ports*/
      ret = iopl(3);
      if(ret)
      { 
           perror("iopl set error");
           return 0;
      }
      port_val = inb(PORT_ADDR);
      printf("Original value of port 0x%x is : %.2x\n", PORT_ADDR, port_val);
      /*reverse the least significant bit */
      outb(port_val^0x01, PORT_ADDR);
      port_val = inb(PORT_ADDR);
      printf("Current value of port 0x%x is : %.2x\n", PORT_ADDR, port_val);     
      /*set r/w permission of  all 65536 ports*/
      ret = iopl(0);
      if(ret)
      { 
           perror("iopl set error");
           return 0;
      }
      return 0;
}
程序执行结果:
[root@linux misc-progs]# ./a.out 
Original value of port 0x3ff is : 01
Current value of port 0x3ff is : 00
[root@linux misc-progs]# ./a.out 
Original value of port 0x3ff is : 00
Current value of port 0x3ff is : 01
该程序执行几次,将进行几次的LSB翻转。
注:这里再次使用0x3FF端口,主要个人对端口的理解还不很深入,其他高于0x3FF的端口进行测试的时候,没有得到既定的结果。这里权且还使用这个端口,借此对iopl的用法熟悉一下即可。至于在真正使用中,如果系统的某个端口是可以进行配置的,那么执行这个程序应该是可以得到既定结果的。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值