Linux mmap:不允许操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8213671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mmap: Operation not permitted
提问by user899159
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.
我试图在用户空间中使用 mmap 来读取 'mem_map' 开始的物理内存。它是一个包含所有物理页面的数组。这是一台运行 3.0 内核的 i386 机器。
The code is like this:
代码是这样的:
....
//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);
}
I ran this as a root. The output is:
我以root身份运行它。输出是:
/dev/mem opened.
mem_map is at physical addr: 0x356f2000
Memory map failed. err num = 1
mmap: Operation not permitted
To be sure, I googled the problem and added the following line to my /etc/sysctl.conf file:
可以肯定的是,我在 google 上搜索了这个问题并将以下行添加到我的 /etc/sysctl.conf 文件中:
vm.mmap_min_addr = 0
But this doesn't work either.
但这也行不通。
Anyone knows why mem_map operation like this is not permitted and how I can get around it?
任何人都知道为什么不允许这样的 mem_map 操作以及我如何解决它?
Thanks.
谢谢。
采纳答案by Brett Hale
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.
听起来内核已被编译为CONFIG_STRICT_DEVMEM
启用。这是一项安全功能,可防止用户空间访问大于 1MB (IIRC) 的(可能是敏感的)物理内存。您可以使用 sysctl dev.mem.restricted 禁用此功能。
回答by Benibr
I had a similar problem which occured when I was trying use flashrom on an APU2c4 Board with Arch Linux.
当我尝试在带有 Arch Linux 的 APU2c4 板上使用 flashrom 时,我遇到了类似的问题。
The sysctl option dev.mem.restricted
wasn't available in my system and using a self compiled kernel was no option for me.
sysctl 选项dev.mem.restricted
在我的系统中不可用,并且使用自编译内核对我来说是没有选择的。
I worked around the problem by setting the iomem
Kernelparameter to relaxed
via Grub:
我通过将iomem
Kernelparameter设置为relaxed
via Grub来解决这个问题:
# /boot/grub/grub.cfg
linux /boot/vmlinuz-linux iomem=relaxed
Of course a reboot is nessesary for this solution.
当然,此解决方案需要重新启动。
Reference:
https://www.reddit.com/r/libreboot/comments/6wvyry/flashrom_failures_to_access/
https://www.flashrom.org/FAQ
https://www.kernel.org/doc/Documentation/admin-guide/kernel-parameters.txt
参考:
https: //www.reddit.com/r/libreboot/comments/6wvyry/flashrom_failures_to_access/
https://www.flashrom.org/FAQ
https://www.kernel.org/doc/Documentation/admin-guide /内核参数.txt