Skip to content
Advertisement

mmap does not work as expected (return random 0xdeadbeef)

I am writing c++ on linux7arm and the physical address is referring to a memory block (that can be accessed/changed by other peripherals) when I map a page of physical address space to the local address space and try to read it, it works fine and show the expected data BUT not all the time. if I write a script to run my compiled c program in a loop and just print the data out it prints 0xdeadbeef randomly. can this be related to physical memory or there is a problem with my code!

int page_size = sysconf(_SC_PAGE_SIZE);

if ((mem_file = open("/dev/mem", O_RDWR|O_SYNC)) < 0) {
    close(mem_file);
    printf("Error : Unable to open /dev/memn");
}
local_address = mmap(
                 NULL,
                 page_size,  // length of the mapped mem
                 PROT_READ,
                 MAP_SHARED,
                 mem_file,
                 (uint32_t)page_address   // starting physical address
                        );

volatile char * start_address=(char *)local_address;
printf("n");
for(i=0;i<page_size;i++) {
    printf("%x",*(start_address+i)); 
{
printf("n");
munmap . . . 

Advertisement

Answer

Your code has two possible issues:

  1. You need to check the return value of mmap in case it fails.
  2. You are assuming the peripherals sharing the memory with the CPU are data cache coherent with it. It is possible, but not likely. If they aren’t you are bound to see stale data.

Having said that, I doubt what you are seeing is related to either issues and suspect a memory value of 0xdeadbeef is the poor RTL designer way to indicate some error in accessing the memory.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement