在内核编程中激活分页

时间:2013-10-09 22:52:05

标签: c assembly kernel paging

我正在阅读本教程“http://www.jamesmolloy.co.uk/tutorial_html/6.-Paging.html”关于kenrel编程,作者正在使用以下结构来构建页面目录

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的地址总是一样的?

1 个答案:

答案 0 :(得分:0)

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

那就是说,我会说文字不清楚,但我读过多任务部分,它涵盖了最终用于当前目录的内容。