我试图在用户空间中使用mmap来读取’mem_map’开始的物理内存.它是一个包含所有物理页面的数组.这是运行3.0内核的i386机器.
代码是这样的:
....
//define page size
//
#define PAGE_SIZE 0x1000 //4096 bytes
#define PAGE_MASK (PAGE_SIZE - 1)
....
/* open /dev/mem file*/
if((fd = open("/dev/mem",O_RDWR | O_SYNC)) == -1) {
printf("/dev/mem could not be opened.\n");
perror("open");
exit(1);
} else {
printf("/dev/mem opened.\n");
}
/* Map one page */
printf(" mem_map is at physical addr: 0x%x\n",mem_map_phy_addr);
map_base = mmap(0,PAGE_SIZE,PROT_READ,MAP_SHARED,fd,(mem_map_phy_addr & ~PAGE_MASK)); //mem_map_phy_addr is at 0x356f2000
if(map_base == (void *) -1) {
printf("Memory map Failed. err num = %d\n",errno);
perror("mmap"); //Failed here
} else {
printf("Memory mapped at address %p.\n",map_base);
}
我把它作为根运行.输出是:
/dev/mem opened.
mem_map is at physical addr: 0x356f2000
Memory map Failed. err num = 1
mmap: Operation not permitted
可以肯定的是,我搜索了问题并将以下行添加到我的/etc/sysctl.conf文件中:
vm.mmap_min_addr = 0
但这也不起作用.
任何人都知道为什么不允许这样的mem_map操作以及我如何绕过它?
谢谢.
最佳答案
原文链接:https://www.f2er.com/linux/440259.html