lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* When trying to map /dev/mem with offset 0xFFFFF000 on the ARM platform, mmap |
| 2 | * returns -EOVERFLOW. |
| 3 | * |
| 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. |
| 8 | */ |
| 9 | |
| 10 | #include <unistd.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <sys/mman.h> |
| 17 | |
| 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) |
| 20 | |
| 21 | #define MAP_SIZE 4096UL |
| 22 | #define MAP_MASK (MAP_SIZE - 1) |
| 23 | |
| 24 | int main(int argc, char **argv) { |
| 25 | void* map_base = 0; |
| 26 | int fd; |
| 27 | off_t target = 0xfffff000; |
| 28 | if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) { |
| 29 | /* skip test for non-root users */ |
| 30 | if (errno == EACCES) |
| 31 | return 0; |
| 32 | FATAL; |
| 33 | } |
| 34 | printf("/dev/mem opened.\n"); |
| 35 | fflush(stdout); |
| 36 | |
| 37 | /* Map one page */ |
| 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); |
| 42 | fflush(stdout); |
| 43 | if(munmap(map_base, MAP_SIZE) == -1) FATAL; |
| 44 | close(fd); |
| 45 | return 0; |
| 46 | } |