1

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:

kernel virtual address: ffffea0000000000 physical address: 0000620000000000

I am trying to access this physical address through mmap in user space, but the following code crashes the kernel:

int *addr;
if ((fd = open("/dev/mem", O_RDWR|O_SYNC)) < 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. */

What am I doing wrong?

Mat
  • 202,337
  • 40
  • 393
  • 406
Vinay
  • 433
  • 1
  • 5
  • 11
  • I'm curious where you got the number 0x0000620000000000 from. It doesn't look right to me. – Alan Curry Aug 12 '12 at 06:16
  • i wrote a kernel module to get the address of the first struct page in kernel. This is the address of the 1st struct page on 64bit x86 architecture. – Vinay Aug 12 '12 at 06:56
  • I believe that 0xffffea0000000000 is correct for the virtual address, but the physical address looks bad. It looks like it came from virt_to_phys, which doesn't handle all addresses. – Alan Curry Aug 12 '12 at 07:28
  • Yes, it came from virt_to_phys.. – Vinay Aug 12 '12 at 23:24
  • I can't find any recently updated documentation for `virt_to_phys` but http://dev.man-online.org/man9/virt_to_phys/ says "It is only valid to use this function on addresses directly mapped or allocated via kmalloc." Your guess is as good as mine what they mean by "directly mapped". – Alan Curry Aug 12 '12 at 23:30
  • I don't know how to find the physical address, but if your goal is just to read the memory, it would be easy enough to just use the virtual address and `/dev/kmem` or `/proc/kcore`. – Alan Curry Aug 12 '12 at 23:39
  • This question is answered here : Answered here http://stackoverflow.com/questions/11891979/accessing-mmaped-dev-mem – Vinay Dec 12 '13 at 08:01

0 Answers0