Linux如何从用户空间访问物理地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12040303/
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
How to access physical addresses from user space in Linux?
提问by lang2
On a ARM based system running Linux, I have a device that's memory mapped to a physical address. From a user space program where all addresses are virtual, how can I read content from this address?
在运行 Linux 的基于 ARM 的系统上,我有一个内存映射到物理地址的设备。从所有地址都是虚拟的用户空间程序中,如何从该地址读取内容?
采纳答案by Dmytro Sirenko
You can map a device file to a user process memory using mmap(2)
system call. Usually, device files are mappings of physical memory to the file system.
Otherwise, you have to write a kernel module which creates such a file or provides a way to map the needed memory to a user process.
您可以使用mmap(2)
系统调用将设备文件映射到用户进程内存。通常,设备文件是物理内存到文件系统的映射。否则,您必须编写一个内核模块来创建这样的文件或提供一种将所需内存映射到用户进程的方法。
Another way is remapping parts of /dev/mem to a user memory.
另一种方法是将 /dev/mem 的部分重新映射到用户内存。
Edit: Example of mmaping /dev/mem (this program must have access to /dev/mem, e.g. have root rights):
编辑: mmaping /dev/mem 示例(此程序必须有权访问 /dev/mem,例如具有 root 权限):
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s <phys_addr> <offset>\n", argv[0]);
return 0;
}
off_t offset = strtoul(argv[1], NULL, 0);
size_t len = strtoul(argv[2], NULL, 0);
// Truncate offset to a multiple of the page size, or mmap will fail.
size_t pagesize = sysconf(_SC_PAGE_SIZE);
off_t page_base = (offset / pagesize) * pagesize;
off_t page_offset = offset - page_base;
int fd = open("/dev/mem", O_SYNC);
unsigned char *mem = mmap(NULL, page_offset + len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, page_base);
if (mem == MAP_FAILED) {
perror("Can't map memory");
return -1;
}
size_t i;
for (i = 0; i < len; ++i)
printf("%02x ", (int)mem[page_offset + i]);
return 0;
}