7

I have a question when I located the address in kernel. I insert a hello module in kernel, in this module, I put these things:

char mystring[]="this is my address";
printk("<1>The address of mystring is %p",virt_to_phys(mystring));

I think I can get the physical address of mystring, but what I found is, in syslog, the printed address of it is 0x38dd0000. However, I dumped the memory and found the real address of it is dcd2a000, which is quite different from the former one. How to explain this? I did something wrong? Thanks

PS: I used a tool to dump the whole memory, physical addresses.

Alex
  • 869
  • 3
  • 13
  • 20

1 Answers1

7

According to the Man page of VIRT_TO_PHYS

The returned physical address is the physical (CPU) mapping for the memory address given. It is only valid to use this function on addresses directly mapped or allocated via kmalloc.

This function does not give bus mappings for DMA transfers. In almost all conceivable cases a device driver should not be using this function

Try allocating the memory for mystring using kmalloc first;

char *mystring = kmalloc(19, GFP_KERNEL);
strcpy(mystring, "this is my address"); //use kernel implementation of strcpy
printk("<1>The address of mystring is %p", virt_to_phys(mystring));
kfree(mystring);

Here is an implementation of strcpy found here:

char *strcpy(char *dest, const char *src)
{
    char *tmp = dest;

    while ((*dest++ = *src++) != '\0')
            /* nothing */;
    return tmp;
}
Community
  • 1
  • 1
Anthony
  • 12,177
  • 9
  • 69
  • 105
  • I used like this but there are errors, first is kmalloc should have two arguments, so I added GEL_KERNEL, second is in the strcpy line, I think `strcpy` is not accepted...so how can I solve it, thanks – Alex Jun 12 '12 at 21:50
  • 1
    Haha, woops! Sorry, it's been about 6 months since I've touched the kernel. As far as strcpy goes, you should be able to `#include `, if your kernel source provides one. Or you can write your own. – Anthony Jun 12 '12 at 22:15
  • thanks !a lot! But when I make it, two errors also, one is `char *mystring = kmalloc(19, GFP_KERNEL);` error is `initializer element is not constant`, another is `strcpy(mystring, "this is my address");` command, the error is `expected ')' before string constant` I already added the strcpy function you wrote here, thanks!! – Alex Jun 13 '12 at 09:01