您好,
我正在尝试编写一个用户程序来打印'mem_map'数组中所有页面的'flags'字段的值。然后我想看看是否设置了保留位。
对于初学者,我编写了一个内核模块来打印'mem_map'数组中前20页的值,它们各自的'flags'字段和PageReserved宏的输出。
for (;i < 20; ++i) {
unsigned long *value = &(mem_map[i].flags);
printk("i = %u page->flags value = %u phy address = %#x isReserved = %d\n", i,mem_map[i].flags, virt_to_phys(&(mem_map[i].flags)),PageReserved(&mem_map[i]));
}
当我运行“dmesg | tail -50”时,我看到以下值:
[ 2858.387520] i = 0 page->flags value = 0 phy address = 0x36840000 isReserved = 0 [ 2858.387520] i = 1 page->flags value = 1024 phy address = 0x36840020 isReserved = 1 [ 2858.387520] i = 2 page->flags value = 1024 phy address = 0x36840040 isReserved = 1 [ 2858.387520] i = 3 page->flags value = 1024 phy address = 0x36840060 isReserved = 1
请注意,我正在打印'flags'字段的物理地址以及值。
我想通过指针查看地址,在用户程序中重现上述结果。
我打开'/ dev / mem',第一页'flag'字段的偏移量为:0x36840000。我确保在关闭STRICT_DEVMEM的情况下编译内核,并在'devmem_is_allowed()'函数中添加了默认的'return 1'。
/* create the offset for mmap() using the physical address of the
mem_map[] array*/
off_t offset = 0x36840000;
/* /dev/mem contains the physical memory map */
if ((fd = open("/dev/mem", O_RDONLY)) == -1) {
printf("/dev/mem could not be opened.\n");
perror("open");
exit(1);
else {
printf("/dev/mem opened.\n");
}
starting_page = (char*)mmap(0, MAP_SIZE, PROT_READ,MAP_SHARED, fd,offset);
if (starting_page == MAP_FAILED) {
perror("mmap");
return -1;
}
else {
printf("Memory mapped at address %p.\n", starting_page);
}
mapped_page = starting_page;
for (i=0;i<10;++i,mapped_page+=32) {
printf("\npage i = %u flags value %ul ", i, *(mapped_page));
}
但是,当我只打印出“标志值”的值时,我得到零。关于我得到什么零以及如何调试它的任何建议?
谢谢! -RR