C uio0设备的mmap参数无效
我配置了我的主机系统,以便PCI设备(这里是ethernet controller)可供在用户空间中运行的驱动程序使用。 我已将以太网控制器绑定到UIO_PCI_GENERIC,并尝试使用mmap()在用户空间中映射设备内存。 我正在虚拟机上运行此代码C uio0设备的mmap参数无效,c,mmap,drivers,C,Mmap,Drivers,我配置了我的主机系统,以便PCI设备(这里是ethernet controller)可供在用户空间中运行的驱动程序使用。 我已将以太网控制器绑定到UIO_PCI_GENERIC,并尝试使用mmap()在用户空间中映射设备内存。 我正在虚拟机上运行此代码 #define MAPPED_SIZE (4*1024) //place the size here #define DDR_RAM_PHYS (0) //place the physical address here
#define MAPPED_SIZE (4*1024) //place the size here
#define DDR_RAM_PHYS (0) //place the physical address here
int main(int argc, char const *argv[])
{
int fduio;
int *map = NULL;
const char uioDevice[] = "/dev/uio0";
/* open /dev/uio and error checking */
fduio = open( uioDevice, O_RDWR | O_SYNC );
if(fduio < 0)
{
printf("Failed to open the /dev/uio0 !\n");
return 0;
}
else{
printf("open /dev/uio0 successfully !\n");
}
/* mmap() the opened /dev/uio */
map=(int*)mmap(0,MAPPED_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,fduio,DDR_RAM_PHYS);
if (map == MAP_FAILED)
{
perror("mmap");
}
/* use 'map' pointer to access the mapped area! */
for(int i=0;i<100;i++)
printf("content: 0x%x\n",*(map+i));
/* unmap the area & error checking */
if(munmap(map,MAPPED_SIZE)==-1)
{
perror("Error un-mmapping the file");
}
/* close the character device */
close(fduio);
return 0;
}
我做错了什么?是因为偏移吗?
当我为/dev/mem运行相同的代码时,它运行得很好吗?
对于UIO设备,是否需要进行任何特殊配置?尝试查看
/sys/class/UIO/ui0/maps/map0/size
,以查看区域大小。可能(4*1024)
太多了。当我将设备绑定到UIO时,没有生成映射文件夹。知道为什么会这样吗?4096是它的默认页面大小…例如,getpagesize()返回的值可能是DTB中的一个问题。
open /dev/uio0 successfully !
mmap : invalid argument
Segmentation fault (core dumped)