简体   繁体   中英

How to map physical memory with mmap()

I am trying to access physical memory address 0x30000000 and I am trying to accomplish this using mmap() . When I map this address to a virtual address pointer I am unable to read the correct value from memory. When I look at the memory using a debugger (TI Code Composer Studio w/ JTAG) I am able to see the values that are in memory but am not getting the same values in my code? Am I using mmap() correctly?

off_t          dev_base = 0x30000000;
size_t         ldev = 0x3FFFFFF;
int offset = 0x00;


memfd = open("/dev/mem", O_RDWR | O_SYNC);
mapped_base = (int*)mmap(0, ldev, PROT_READ|PROT_WRITE, MAP_SHARED, memfd, dev_base);

if (mapped_base == MAP_FAILED)
{
    errx(1, "mmap failure");
}

printf("mapped_base = %08p\n", mapped_base);


printf("The value at address [%08p] = %08p\n", offset + ((int)mapped_base), mapped_base[offset/4]);

munmap(mapped_base, ldev);
close(memfd);

offset passed to mmap call should be in page units , this is the difference in new mmap2 system call.

mmap man page.

http://man7.org/linux/man-pages/man2/mmap.2.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM