title: Accessing mmaped /dev/mem
body: <p>I have simple program that tries to access the physical memory in user space,  where the kernel stores the first struct page. On a 64-bit machine this address is:</p>
  
  <blockquote>
    <p>kernel virtual address: ffffea0000000000 physical address: 0000620000000000</p>
  </blockquote>
  
  <p>I am trying to access this physical address through <code>mmap</code> 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>
  
  <p>What am I doing wrong?</p>