我正在尝试从用户空间访问/ 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;
}
它失败并显示错误: 写:错误的地址