当我在内核中定位地址时,我有一个问题。我在内核中插入一个 hello 模块,在这个模块中,我放了这些东西:
char mystring[]="this is my address";
printk("<1>The address of mystring is %p",virt_to_phys(mystring));
我想我可以得到 mystring 的物理地址,但我发现,在 syslog 中,它的打印地址是 0x38dd0000。但是,我转储了内存,发现它的真实地址是dcd2a000,和前者有很大的不同。怎么解释呢?我做错事情了?谢谢
PS:我用了一个工具把整个内存,物理地址都dump出来了。
最佳答案
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
首先尝试使用kmalloc
为mystring
分配内存;
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);
这是找到的 strcpy 的实现 here :
char *strcpy(char *dest, const char *src)
{
char *tmp = dest;
while ((*dest++ = *src++) != '\0')
/* nothing */;
return tmp;
}
关于c - 内核中的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10959225/