1

我正在阅读关于 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;

我的问题是为什么他在函数 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 的地址总是相同的?

4

1 回答 1

0

您的函数编写不正确,并且您没有在问题中包含整个相关代码。

也就是说,我会说文本不清楚,但我会阅读 Multitasking 部分,其中涵盖了 current_directory 的最终用途。

于 2013-10-09T23:12:43.160 回答