Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The address in Kernel

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.

like image 529
Alex Avatar asked Jun 09 '12 07:06

Alex


1 Answers

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;
}
like image 180
Anthony Avatar answered Sep 19 '22 05:09

Anthony