mmap:不允许操作 [英] mmap: Operation not permitted
问题描述
我正在尝试在用户空间中使用mmap读取"mem_map"开始的物理内存.它是一个包含所有物理页面的数组.这是一台运行3.0内核的i386计算机.
I am trying to use mmap in user space to read the physical memory where 'mem_map' starts. It's an array that contains all the physical pages. This is a i386 machine running 3.0 kernel.
代码如下:
....
//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文件中:
To be sure, I googled the problem and added the following line to my /etc/sysctl.conf file:
vm.mmap_min_addr = 0
但这也不起作用.
任何人都知道为什么不允许这样的mem_map操作以及如何解决该问题?
Anyone knows why mem_map operation like this is not permitted and how I can get around it?
谢谢.
推荐答案
听起来好像内核已启用CONFIG_STRICT_DEVMEM
进行编译.这是一项安全功能,可防止用户空间访问(可能是敏感的)1MB(IIRC)以上的物理内存.您可以使用sysctl dev.mem.restricted禁用它.
It sounds like the kernel has been compiled with CONFIG_STRICT_DEVMEM
enabled. This is a security feature to prevent user space access to (possibly sensitive) physical memory above 1MB (IIRC). You might be able to disable this with sysctl dev.mem.restricted.
这篇关于mmap:不允许操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!