gpt4 book ai didi

c++ - 为什么0xCF8端口无法访问?

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:43 25 4
gpt4 key购买 nike

我使用ubuntu作为Virtual Box(虚拟机)中的操作系统。我的目标是使用 C++ 代码列出系统中可以找到的所有 pci 设备。这是其中的一部分:

#include <stdio.h>
#include <string.h>
#include <sys/io.h>
#include <stdlib.h>
#define NOT_SPECIFIED "Not specified"

int main()
{
int i, busid, devid;
FILE * file = fopen("Pcibits.txt", "w+");
for (busid = 0; busid < 256; busid++)
{
for (devid = 0; devid < 32; devid++)
{
unsigned int recvp, ven_id, dev_id, clid;
unsigned int sendp = (busid << 16) | (devid << 11) | ((unsigned int)0x80000000);
outl(sendp, 0x0CF8);
recvp = inl(0x0CFC);
char* str = new char[32];
sprintf(str, "%d", recvp);
fwrite(str, sizeof(char), sizeof(str)/sizeof(char), file);
fclose(file);
}
}
return 0;
}`

但是使用 gdb 我遇到了这样的错误:

Traceback (most recent call last): File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in <module>
from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named 'libstdcxx'

Program received signal SIGSEGV, Segmentation fault.
0x000000000040075a in outl (__value=2256549650, __port=3320)
at /usr/include/x86_64-linux-gnu/sys/io.h:125
125 __asm__ __volatile__ ("outl %0,%w1": :"a" (__value), "Nd" (__port));

这有什么问题吗?当我意识到该程序在输出步骤失败时。请帮忙!)

最佳答案

来自Ubuntu reference for these types of functions .

You use ioperm(2) or alternatively iopl(2) to tell the kernel to allow the user space application to access the I/O ports in question. Failure to do this will cause the application to receive a segmentation fault.

(强调我的)

因此,您必须为您的程序设置适当的权限,以防止段错误。

查看有关如何设置低级端口访问权限的更多信息 here

关于c++ - 为什么0xCF8端口无法访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33647800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com