简体   繁体   English

内核中的地址

[英]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: 我在内核中插入了一个hello模块,在这个模块中,我把这些东西:

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. 我想我可以得到mystring的物理地址,但我发现,在syslog中,它的打印地址是0x38dd0000。 However, I dumped the memory and found the real address of it is dcd2a000, which is quite different from the former one. 但是,我倾倒了内存并发现它的真实地址是dcd2a000,这与前一个完全不同。 How to explain this? 怎么解释这个? I did something wrong? 我做错事情了? Thanks 谢谢

PS: I used a tool to dump the whole memory, physical addresses. PS:我用一个工具来转储整个内存,物理地址。

According to the Man page of VIRT_TO_PHYS 根据VIRT_TO_PHYSMan页面

The returned physical address is the physical (CPU) mapping for the memory address given. 返回的物理地址是给定内存地址的物理(CPU)映射。 It is only valid to use this function on addresses directly mapped or allocated via kmalloc. 仅在通过kmalloc直接映射或分配的地址上使用此功能才有效。

This function does not give bus mappings for DMA transfers. 此函数不提供DMA传输的总线映射。 In almost all conceivable cases a device driver should not be using this function 在几乎所有可能的情况下,设备驱动程序都不应该使用此功能

Try allocating the memory for mystring using kmalloc first; 首先尝试使用kmallocmystring分配内存;

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 : 这是strcpy的一个实现,在这里找到:

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

    while ((*dest++ = *src++) != '\0')
            /* nothing */;
    return tmp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM