1

私はケンレルプログラミングに関するこのチュートリアル「 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