如下图所示,Linux虚拟内存的高16位全0代表此地址为用户空间地址,全1代表此地址为内核空间地址
可以通过以下代码进行验证
int i = 5;
printf("address of i is %p\n", &i)
内核打印用printk
int i=5;
printk(KERN_INFO "address of i is 0x%p\n", &i);
打印用户段地址
[root@ecs-3370 ~]# cat address.c
#include <unistd.h> /*sleep()所在的头文件*/
#include <stdio.h>
int main(int argc, char const *argv[])
{
int i=5;
printf("address of i is %p\n", &i);
sleep(300);
return 0;
}
[root@ecs-3370 ~]# gcc address.c
[root@ecs-3370 ~]# ./a.out
address of i is 0x7ffcfd094ffc
printf()函数打印地址会自动添加0x符号标记该值为16进制,可以发现最高的16位全为0,所以忽略不写。
打印内核段地址
[root@ecs-3370 tmp]# cat test.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
MODULE_AUTHOR("<insert your name here>");
MODULE_DESCRIPTION("LLKD book:ch4/helloworld_lkm: hello, world, our first LKM");
MODULE_LICENSE("Dual MIT/GPL");
MODULE_VERSION("0.1");
static int __init helloworld_lkm_init(void)
{
int i=5;
printk(KERN_INFO "address of i is 0x%p\n", &i);
return 0; /* success */
}
static void __exit helloworld_lkm_exit(void)
{
printk(KERN_INFO "Goodbye, world\n");
}
module_init(helloworld_lkm_init);
module_exit(helloworld_lkm_exit);
[root@ecs-3370 tmp]# make
make -C /lib/modules/3.10.0-1062.el7.x86_64/build/ M=/tmp modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-1062.el7.x86_64'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/kernels/3.10.0-1062.el7.x86_64'
[root@ecs-3370 tmp]# insmod test.ko
[root@ecs-3370 tmp]# dmesg
[150741.097975] address of i is 0xffff9fe35a313d24
可以发现高16位都为1(0xffff ....),由于内核printk函数不会自动添加0x注释符,因此为了好看,打印的时候写为0x%p。