在调用inb

编程入门 行业动态 更新时间:2024-10-28 10:22:37
本文介绍了在调用inb_p()时导致分段错误的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am getting a segmentation fault when trying to read a port with inb_p( ). I'm compiling this on a Debian system running 2.6.6 kernel on an Intel D525 dual-core system (Advantech PCM 9389 SBC). Here is a sample program which illustrates the segfault.

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

What is the probable cause? How do I fix this?

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

Currently, I don't have any devices hooked up. Could this cause the segfault? I would have expected to get either a zero or some random byte, but not a segfault.

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

Other things I tried: 1) Declared the input variable as int instead of char. 2) Used iopl() instead of 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 确实会导致段错误。我开始怀疑问题与硬件有关。

/proc/ioports did not list anything at address 0x100, so I tried several other port addresses that were listed, with the same result. Then I decided to try an output to a known parallel port location (0x0378), and outb did not cause a segfault. However, trying to read either 0x378 or 0x379 did cause a segfault. I am beginning to suspect that the problem is hardware related.

推荐答案

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

I found the problem. The call to inb_p() requires access to port 0x80 in addition to the port to be read.

显然,当我尝试使用iopl()时,我没有调用它正确的,因为它应该有效。

Apparently, when I tried iopl(), I didn't call it correctly, because that should have worked.

以下代码消除了段错误:

The following code eliminated the segfault:

/* 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); }

更多推荐

在调用inb

本文发布于:2023-10-08 14:06:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472853.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:inb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!