【发布时间】:2020-03-20 10:41:24
【问题描述】:
我正在尝试从用户空间访问 /dev/mem。为此目的使用 qemu-system-arm。
UART0 被映射:0x101f1000 并且 UARTDR 被放置在偏移量 0x0
$ devmem 0x101f1000 8 0x61
上面在控制台上写了'a'。
当我尝试从 C 代码中实现相同的逻辑时,它失败了
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd;
char ch = 'a';
fd = open("/dev/mem", O_RDWR | O_SYNC);
if (fd < 0) {
perror("open failed");
return -1;
}
if (lseek(fd, 0x101f1000, SEEK_SET) == -1) {
perror("lseek");
}
if (write(fd, &ch, sizeof(ch)) == -1) {
perror("write");
}
close(fd);
return 0;
}
失败并出现错误: 写:错误的地址
【问题讨论】:
-
您对问题的更改使我的回答难以理解。请不要在给出答案/评论后更改问题,而仅添加内容。
-
@md.jamal:实际代码(如果函数失败仅调用
perror)是否显示lseek: Success?如果是这样lseek失败。 -
Lseek 成功写入失败 我已经更新了代码和输出
-
@md.jamal 看看
mmap(),UART 寄存器不在进程虚拟地址空间 -
或许你可以用
strace看看devmem是如何读/写内存的……