1 /* When trying to map /dev/mem with offset 0xFFFFF000 on the ARM platform, mmap
4 * Since off_t is defined as a long int and the sign bit is set in the address,
5 * the shift operation shifts in ones instead of zeroes
6 * from the left. This results the offset sent to the kernel function becomes
7 * 0xFFFFFFFF instead of 0x000FFFFF with MMAP2_PAGE_SHIFT set to 12.
18 #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
19 __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
21 #define MAP_SIZE 4096UL
22 #define MAP_MASK (MAP_SIZE - 1)
24 int main(int argc, char **argv) {
27 off_t target = 0xfffff000;
28 if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) {
29 /* skip test for non-root users */
34 printf("/dev/mem opened.\n");
38 map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
39 fd, target & ~MAP_MASK);
40 if(map_base == (void *) -1) FATAL;
41 printf("Memory mapped at address %p.\n", map_base);
43 if(munmap(map_base, MAP_SIZE) == -1) FATAL;