我正在阅读关于 kenrel 编程的教程“http://www.jamesmolloy.co.uk/tutorial_html/6.-Paging.html”,作者使用以下结构构建页面目录
typedef struct page_directory
{
/**
Array of pointers to pagetables.
**/
page_table_t *tables[1024];
/**
Array of pointers to the pagetables above, but gives their *physical*
location, for loading into the CR3 register.
**/
u32int tablesPhysical[1024];
/**
The physical address of tablesPhysical. This comes into play
when we get our kernel heap allocated and the directory
may be in a different location in virtual memory.
**/
u32int physicalAddr;
} page_directory_t;
我的问题是为什么他在function void switch_page_directory(page_directory_t *new); 中这样加载页面目录的地址
asm volatile("mov %0, %%cr3":: "r"(&dir->tablesPhysical));
不是这样的
asm volatile("mov %0, %%cr3":: "r"(current_directory ));
我一直在测试,如下面的代码所示
#include<stdio.h>
#include<stdlib.h>
typedef struct page
{
unsigned int present : 1; // Page present in memory
unsigned int rw : 1; // Read-only if clear, readwrite if set
unsigned int user : 1; // Supervisor level only if clear
unsigned int accessed : 1; // Has the page been accessed since last refresh?
unsigned int dirty : 1; // Has the page been written to since last refresh?
unsigned int unused : 7; // Amalgamation of unused and reserved bits
unsigned int frame : 20; // Frame address (shifted right 12 bits)
} page_t;
typedef struct page_table
{
page_t pages[1024];
} page_table_t;
typedef struct page_directory
{
page_table_t *tables[1024];
unsigned int tablesPhysical[1024];
unsigned int physicalAddr;
} page_directory_t;
int main()
{
page_directory_t *n;
n = malloc(sizeof(page_directory_t));
printf("n=%p i=%p y=%p\n", n,&n->tablesPhysical, &n->tables);
}
结果如下
n=0x833b008 i=0x833c008 y=0x833b008
我不确定为什么 printf 的地址总是相同的?
最佳答案
你的函数写得不正确,你没有在问题中包含整个相关代码。
也就是说,我会说文本不清楚,但我读过多任务处理部分,其中涵盖了 current_directory 最终用于什么。
关于c - 在内核编程中激活分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19284166/