c - 内核中的地址

标签 c linux kernel kernel-module

当我在内核中定位地址时,我有一个问题。我在内核中插入一个 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出来了。

最佳答案

根据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

首先尝试使用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);

这是找到的 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/

相关文章:

c - 堆栈中缀到后缀

linux - glibc 检测到什么...httpd : double free or corruption mean?

linux - 为什么这个 Linux 内核页表代码将返回值用大括号括起来?

c - C 语言基数排序与 OpenMP 的并行化

c - 为什么我的代码会给我垃圾值,有时还会出现段错误?

linux - 如何使用 df 命令查找可用和已用内存总量

c - 内核模块中的 Stackoverflow

kernel - 有没有足够好的小内核来学习 osdev?

c - 在 C 中使用 Strcmp

linux - Debian Linux Raspbian- Raspberry Pi 时间偏移比 UTC 提前 65 秒