Cosmin Aprodu
Cosmin Aprodu

Reputation: 13

Why does address read return nothing on second read? (using mmap)

I am building a program on a RPi3 using Buildroot as linux distro and I need to write to a given physical address. I've read that the best approach is to first use mmap, then obtain a virtual address at which you normally write. In my case, the test program must write a single character at the given address.

The strange thing is that, when I read the value from the virtual address after the mapping, the first time works, but the second time, the same address points to nothing. This is a snippet from my program:

    ...

    int mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
    if (0 > mem_fd) {
        fprintf(stderr, "Could not open memory!\n");
        return -1;
    }

    map_base = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, ATTEST_START & ~map_mask);
    map_target = map_base + (ATTEST_START & map_mask);

    *((unsigned char *) map_target) = (char) ((rand() % 25) + 65);
    printf("After assignment %p, val is %c\n", map_target, *((unsigned char *) map_target)); // here I can read the value
    printf("After assignment %p, val is %c\n", map_target, *((unsigned char *) map_target)); // here it is empty!!!

    ...

ATTEST_START is the registered physical address (using register_phys_mem, in OP-TEE trusted OS). I would very much appreciate if you could tell me why the second read doesn't work and what can I do differently to make it work. Thanks!

Upvotes: 0

Views: 95

Answers (0)

Related Questions