使用mmap进行虚拟到物理寻址

时间:2014-07-10 10:15:48

标签: memory

我想写一个物理地址来改变使用ARM板的引脚电压 - 但是为了写入物理地址,我需要获取一个虚拟地址,并使用mmap将其映射到物理地址。 所以我这样做了:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>

#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)

int main(void) {
int fd;
int *map_base_c,*map_base_d, *map_base_p, *virt_addr;
off_t target,control,data,pullup;

control=0x56000050;

if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;

map_base_d = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd,data & ~MAP_MASK);

printf("Memory mapped at address %p.\n", map_base_d);

virt_addr = map_base_d; //+ (data & MAP_MASK)
*virt_addr = 0x00;  //This is where it goes off. find out why!!!
printf("Value at address 0x%X (%p): 0x%X\n", data, virt_addr,(*virt_addr));

close(fd);
return 0;

}

但是,按照我的预期,该引脚没有达到高电压。我改变地址的方式有问题吗? 还有,有办法看到映射到虚拟地址的物理地址吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

在致电mmap时,offset参数应该是您要访问的最低实际地址。在您的代码中,您传递data & ~MAP_MASK,数据尚未初始化(或已默认初始化为0)。

我相信你想要类似的以下内容:

uintptr_t control = 0x56000050;
uintptr_t base = control & ~MAP_MASK;
int fd;
void *map_base_d;
int *virt_addr;

if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;

map_base_d = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base);
//now *map_base_d corresponds to the physical address of 0x56000000

virt_addr = map_base_d + (control - base);
*virt_addr = 0x00; //Make sure virt_addr is a pointer of the right width (int*, char*, etc), so that you don't accidentally write a dword when you really only want to write a single word.