/dev/mem 的 mmap 因 virt_to_phys 地址的无效参数而失败,但地址是页面对齐的 [英] mmap of /dev/mem fails with invalid argument for virt_to_phys address, but address is page aligned
本文介绍了/dev/mem 的 mmap 因 virt_to_phys 地址的无效参数而失败,但地址是页面对齐的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某种原因,我的 mmap
失败并显示 Invalid argument
消息,即使我的偏移量是页面对齐的.页大小为 4096 字节.另外 CONFIG_STRICT_DEVMEM
被禁用,即我可以访问 1MB 以上的内存.
For some reason my mmap
failed with an Invalid argument
message even though my offset is page aligned. Page size is 4096 bytes. Also CONFIG_STRICT_DEVMEM
is disabled, i.e. I can access memory above 1MB.
这是我的代码:
void *mmap64;
off_t offset = 0x000000d9fcc000;
int memFd = open("/dev/mem", O_RDWR);
if (-1 == memFd)
perror("Error ");
mmap64 = mmap(0, getpagesize(), PROT_WRITE | PROT_READ, MAP_SHARED, memFd, offset);
if (MAP_FAILED == mmap64) {
perror("Error ");
return -1;
}
有人可以解释为什么会这样吗?
Can someone explain why this is happening?
编辑
这是我代码的strace
execve("./to_phys_test", ["./to_phys_test", "-r"], [/* 18 vars */]) = 0
brk(0) = 0x2012000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fe240a2c000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=162063, ...}) = 0
mmap(NULL, 162063, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fe240a04000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "177ELF211 3 >