How to map physical memory using mmap ()

I am trying to access a physical memory address 0x30000000and I am trying to accomplish this with mmap(). When I map this address to a virtual address pointer, I cannot read the correct value from memory. When I look at memory using a debugger (TI Code Composer Studio w / JTAG), can I see the values ​​that are in memory, but I do not get the same values ​​in my code? Am I using it correctly mmap()?

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);
+4
source share
1 answer

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

mmap man.

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

0

Source: https://habr.com/ru/post/1536000/


All Articles