你好,
我正在尝试编写一个用户程序来打印“mem_map”数组中所有页面的“标志”字段的值。然后我想看看是否设置了保留位。
对于初学者,我编写了一个内核模块来打印“mem_map”数组中前 20 页的值、它们各自的“标志”字段和 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 页->标志值 = 1024 phy 地址 = 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
请注意,我正在打印“标志”字段的物理地址以及值。
我想通过指针查看地址来在用户程序中重现上述结果。
我打开了“/dev/mem”,第一页的“标志”字段的偏移量为: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