title: How to access mmaped /dev/mem without crashing the Linux kernel?
body: <p>I have a simple program that tries to access the physical memory in user space,  where the kernel stores the 1st struct page. On a 64 bit machine this address is:</p>
  
  <ul>
  <li>kernel virtual address: ffffea0000000000</li>
  <li>physical address: 0000620000000000</li>
  </ul>
  
  <p>I am trying to access this physical address through mmap in user space. But the following code crashes the kernel.</p>
  
  <pre><code>int *addr;
  if ((fd = open("/dev/mem", O_RDWR|O_SYNC)) &lt; 0 ) {
      printf("Error opening file. \n");
      close(fd);
      return (-1);
  }
  /* mmap.  address of first struct page for 64 bit architectures 
   * is 0x0000620000000000.
   */
  addr = (int *)mmap(0, num*STRUCT_PAGE_SIZE, PROT_READ, MAP_PRIVATE,
              fd, 0x0000620000000000);
  printf("addr: %p \n",addr);
  printf("addr: %d \n",*addr); /* CRASH. */
  </code></pre>