内核中的地址

Ale*_*lex 7 c linux kernel kernel-module

当我在内核中找到地址时,我有一个问题.我在内核中插入了一个hello模块,在这个模块中,我把这些东西:

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

我想我可以得到mystring的物理地址,但我发现,在syslog中,它的打印地址是0x38dd0000.但是,我倾倒了内存并发现它的真实地址是dcd2a000,这与前一个完全不同.怎么解释这个?我做错事情了?谢谢

PS:我用一个工具来转储整个内存,物理地址.

Ant*_*ony 7

根据VIRT_TO_PHYSMan页面

返回的物理地址是给定内存地址的物理(CPU)映射.仅在通过kmalloc直接映射或分配的地址上使用此函数才有效.

此函数不提供DMA传输的总线映射.在几乎所有可能的情况下,设备驱动程序都不应该使用此功能

尝试分配内存以便先mystring使用kmalloc;

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);
Run Code Online (Sandbox Code Playgroud)

这是strcpy的一个实现,在这里找到:

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

    while ((*dest++ = *src++) != '\0')
            /* nothing */;
    return tmp;
}
Run Code Online (Sandbox Code Playgroud)