问题描述
我正在尝试创建一个简单的程序,我将内存映射到无线网络 pci 设备的资源 0。问题是我收到了 mmap(..) 调用返回的 0xffffffff
地址。它看起来没有正确映射,因为 0xffffffff
没有意义。这个页面上的 errno 是 22 它说它与偏移量 android mmap failed: invalid argument (errno 22)
这是我的代码
void main(){
void *map_base,*virt_addr;
off_t target,target_base;
int fd;
if((fd = open("/sys/bus/pci/devices/0000:02:00.0/resource0",O_RDWR | O_SYNC)) == -1) {
printf("open error\n");
}
target=0xc0700000;//register location of resource. But not sure what to have here
target_base = target & ~(sysconf(_SC_PAGE_SIZE)-1);
int map_size = 4096UL;
struct ethhdr *eth;
map_base = mmap(0,map_size,PROT_READ | PROT_WRITE,MAP_SHARED,fd,target_base);
printf("%x\n",map_base);
printf("hello\n");
for (int i = 0; i < 4; i++) {
virt_addr = map_base + target + i*8 - target_base;
printf("hello\n");
eth=((struct ethhdr *) virt_addr);
char a=eth->h_dest[0];
printf("hello\n");
//printf( "%.2X-%.2X-%.2X-%.2X-%.2X-%.2X \n",eth->h_dest[0],eth->h_dest[1],eth->h_dest[2],eth->h_dest[3],eth->h_dest[4],eth->h_dest[5] );
}
return;
}
然后我试图读取映射的位置并尝试从循环中的映射区域提取以太网标头(它可能是错误的,但错误的映射是无用的)。考虑到 github 上的代码使用不同的架构,我认为这不是做这件事的正确方法。所以我想问一下,当我映射到 IO 资源时,我从 mmap 返回奇怪的地址可能是什么原因。我将进入数据表部分,然后我可以正确地从映射位置提取标题。但我的主要问题是关于错误的映射。我怎样才能正确地映射它。那么我应该使用什么(在整个 for 循环中)所以我在正确的 IO 映射之后有一些有用的打印(不要求英特尔设备架构和注册信息。只是需要很容易获得以太网标头/ip 标头/和 tcp 标头的数据结构)。我不知道这是否有意义,但我启用了系统虚拟化。我所知道的可能是将设备映射到内存位置区域是必要的,如我正在使用的 intel x86-64 中的内存映射 I/O。
无论如何,此页面上都有类似的技术。我从这个页面获得了想法https://github.com/billfarrow/pcimem/blob/master/pcimem.c
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)