mmap2.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <fcntl.h>
  15. #include <sys/mman.h>
  16. #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
  17. __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
  18. #define MAP_SIZE sysconf(_SC_PAGESIZE)
  19. #define MAP_MASK (MAP_SIZE - 1)
  20. int main(int argc, char **argv) {
  21. void* map_base = 0;
  22. int fd;
  23. off_t target = 0xfffff000;
  24. if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) {
  25. /* skip test for non-root users */
  26. if (errno == EACCES)
  27. return 0;
  28. FATAL;
  29. }
  30. printf("/dev/mem opened.\n");
  31. fflush(stdout);
  32. /* Map one page */
  33. map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
  34. fd, target & ~MAP_MASK);
  35. if(map_base == (void *) -1) FATAL;
  36. printf("Memory mapped at address %p.\n", map_base);
  37. fflush(stdout);
  38. if(munmap(map_base, MAP_SIZE) == -1) FATAL;
  39. close(fd);
  40. return 0;
  41. }