【问题标题】:What causes a segmentation fault on a call to inb_p()?是什么导致调用 inb_p() 时出现分段错误?
【发布时间】:2012-12-03 14:23:46
【问题描述】:

我在尝试使用 inb_p( ) 读取端口时遇到分段错误。我在英特尔 D525 双核系统(Advantech PCM 9389 SBC)上运行 2.6.6 内核的 D​​ebian 系统上编译它。这是一个说明段错误的示例程序。

可能的原因是什么?我该如何解决这个问题?

目前,我没有连接任何设备。这会导致段错误吗?我本来希望得到一个零或一些随机字节,但不是段错误。

我尝试过的其他事情: 1) 将输入变量声明为 int 而不是 char。 2) 使用 iopl() 代替 ioperm()

/*
 * ioexample.c: very simple ioexample of port I/O
 * very simple port i/o
 * Compile with `gcc -O2 -o ioexample ioexample.c',
 * and run as root with `./ioexample'.
 */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/io.h>

#define BASEPORT 0x0100 /* iobase for sample system */
#define FLIPC 0x01
#define FLIPST 0x0
#define DIPSWITCH 0x25

int main()
{
char cinput;

  cinput = 0xff;
  setuid(0);
  printf("begin\n");
  /* Get access to the ports */
  if (ioperm(BASEPORT+DIPSWITCH, 10, 1)) 
  {
     perror("ioperm");
     exit(EXIT_FAILURE);
  }

  printf("read the dipswitch with pause\n");
  cinput = inb_p(BASEPORT+DIPSWITCH); // <=====SEGFAULT HERE

  /* We don't need the ports anymore */
  if (ioperm(BASEPORT+DIPSWITCH, 10, 0))
  {
     perror("ioperm");
     exit(EXIT_FAILURE);
  }

  printf("Dipswitch setting: 0x%X", cinput); 
  exit(EXIT_SUCCESS);
}

/* end of ioexample.c */

输出:

root@debian:/home/howard/sources# ./ioexample
begin
read the dipswitch with pause
Segmentation fault

编辑:/proc/ioports 没有列出地址 0x100 的任何内容,因此我尝试了列出的其他几个端口地址,结果相同。然后我决定尝试一个 输出 到一个已知的并行端口位置 (0x0378),并且 outb 没有导致段错误。但是,尝试读取 0x378 或 0x379 确实会导致段错误。我开始怀疑问题与硬件有关。

【问题讨论】:

  • 是的,我以 root 身份运行。

标签: c linux io segmentation-fault debian


【解决方案1】:

我发现了问题。除了要读取的端口之外,对 inb_p() 的调用还需要访问端口 0x80。

显然,当我尝试 iopl() 时,我没有正确调用它,因为它应该可以工作。

以下代码消除了段错误:

  /* Get access to the ports */
  if (ioperm(0x80, 1, 1)) 
  {
     perror("ioperm");
     exit(EXIT_FAILURE);
  }
  if (ioperm(BASEPORT+DIPSWITCH, 10, 1)) 
  {
     perror("ioperm");
     exit(EXIT_FAILURE);
  }

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2020-09-15
    • 2012-04-19
    相关资源
    最近更新 更多