c - 在内核编程中激活分页

标签 c assembly kernel paging

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

相关文章:

c - 如何在 Linux 内核中将 char[] 字符串转换为 int?

linux - 我新编译的内核在 qemu 中丢失了网络

c - C 中的外部类型定义

c - 字符串不是从 C 函数的返回值中捕获的

c - "-1"在unsigned int和signed int的取值范围内代表什么?

gcc - 为什么x86-64上的GCC会在函数内部插入NOP?

assembly - 汇编中的异或语句

c - 访问冲突读取位置 C 数组

assembly - TLB 是否包含在内?

c++ - 基于 IOKit 的 kext 驱动程序中的单例类