]> Zhao Yanbai Git Server - kernel.git/commitdiff
cr3字段统一处理成物理地址
authoracevest <zhaoyanbai@126.com>
Wed, 3 Nov 2021 11:59:58 +0000 (19:59 +0800)
committeracevest <zhaoyanbai@126.com>
Wed, 3 Nov 2021 11:59:58 +0000 (19:59 +0800)
include/page.h
kernel/exec.c
kernel/fork.c
kernel/init.c
kernel/sched.c
mm/mm.c
mm/page.c

index 3148212cbb81d2845322e7675c28145e0aa1c886..0b0068d5763b782c077ac9cd82c76fbbe585f63c 100644 (file)
@@ -66,7 +66,7 @@ typedef unsigned long pte_t;
 
 #define MAX_ORDER (11)
 
-#define LOAD_CR3(pde) asm("movl %%edx, %%cr3" ::"d"(va2pa(pde)))
+#define LoadCR3(cr3) asm volatile ("movl %%edx, %%cr3"::"d"(cr3))
 
 typedef unsigned int gfp_t;
 
index 4f33469f2799b50b991a87a81893f6fcef523635..b2afffa39be16d48b7359a757987eb3698148c9e 100644 (file)
@@ -31,7 +31,7 @@ void put_paging(unsigned long vaddr, unsigned long paddr, unsigned long flags) {
     unsigned int npde = get_npd(vaddr);
     unsigned int npte = get_npt(vaddr);
 
-    pde_t *page_dir = (pde_t *)current->cr3;
+    pde_t *page_dir = (pde_t *)pa2va(current->cr3);
     pte_t *page_table = (pte_t *)PAGE_ALIGN(page_dir[npde]);
 
     if (page_table == 0) {
index 08fea395f2653db23ea2191d7fdbe29bda78ebb8..f0646223ee9b54a690a9d5f247e4b96b9c7909d9 100644 (file)
@@ -31,14 +31,14 @@ int do_fork(pt_regs_t *regs, unsigned long flags) {
 
     memcpy(tsk, current, sizeof(task_union));
 
-    tsk->cr3 = (unsigned long)alloc_one_page(0);
+    tsk->cr3 = va2pa((unsigned long)alloc_one_page(0));
     assert(tsk->cr3 != 0);
 
     unsigned int i, j;
-    pde_t *pde_src = (pde_t *)current->cr3;
-    pde_t *pde_dst = (pde_t *)tsk->cr3;
+    pde_t *pde_src = (pde_t *)pa2va(current->cr3);
+    pde_t *pde_dst = (pde_t *)pa2va(tsk->cr3);
 
-    memcpy((void *)tsk->cr3, (void *)current->cr3, PAGE_SIZE);
+    memcpy((void *)pa2va(tsk->cr3), (void *)pa2va(current->cr3), PAGE_SIZE);
 
     for (i = 0; i < PAGE_PDE_CNT; ++i) {
         unsigned long spde = (unsigned long)pde_src[i];
index b75e2dcc58e9910c043228d84f18bd668d24a806..9ce02577c68f4cdfa634734e56ed4d2ae04fb7ae 100644 (file)
@@ -37,36 +37,40 @@ void __ring3text__ ring3_entry() {
 }
 
 void user_task_entry() {
-    // printk("user_task_entry: %08x\n", ring3_entry);
-
-    unsigned long ring3_text_page = va2pa(alloc_one_page(0));
-    unsigned long ring3_data_page = va2pa(alloc_one_page(0));
-    unsigned long ring3_bss_page = va2pa(alloc_one_page(0));
-    unsigned long *pt_text_page = (unsigned long *)va2pa(alloc_one_page(0));
-    unsigned long *pt_data_page = (unsigned long *)va2pa(alloc_one_page(0));
-    unsigned long *pt_bss_page = (unsigned long *)va2pa(alloc_one_page(0));
-    unsigned long *p = (unsigned long *)current->cr3;
-
-    // text: 0x0800_0000
-    // data: 0x2000_0000
-    //  bss: 0x3000_0000
-    unsigned long text_at = 0x08000000;
-    unsigned long data_at = 0x20000000;
-    unsigned long bbs_at = 0x30000000;
-
-    unsigned long flag = 0;
-
-    flag |= PAGE_P;
-    flag |= PAGE_US;
-
-    p[text_at >> 22] = (unsigned long)pt_text_page | PAGE_P | PAGE_US;
-    pt_text_page[0] = ring3_text_page;
-    p[data_at >> 22] = (unsigned long)pt_data_page | PAGE_P | PAGE_WR | PAGE_US;
-    pt_data_page[0] = ring3_data_page;
-    p[bbs_at >> 22] = (unsigned long)pt_bss_page | PAGE_P | PAGE_WR | PAGE_US;
-    pt_bss_page[0] = ring3_bss_page;
-
-    LOAD_CR3(current->cr3);
+    // // printk("user_task_entry: %08x\n", ring3_entry);
+
+    // unsigned long ring3_text_page = va2pa(alloc_one_page(0));
+    // unsigned long ring3_data_page = va2pa(alloc_one_page(0));
+    // unsigned long ring3_bss_page = va2pa(alloc_one_page(0));
+    // unsigned long *pt_text_page = (unsigned long *)va2pa(alloc_one_page(0));
+    // unsigned long *pt_data_page = (unsigned long *)va2pa(alloc_one_page(0));
+    // unsigned long *pt_bss_page = (unsigned long *)va2pa(alloc_one_page(0));
+    // unsigned long *p = (unsigned long *)((current->cr3 - 0xC0000000));
+
+    //  asm volatile("xchg %%bx, %%bx;mov %%eax, %%ebx;xchg %%bx, %%bx;"::"a"(p));
+
+    // // text: 0x0800_0000
+    // // data: 0x2000_0000
+    // //  bss: 0x3000_0000
+    // unsigned long text_at = 0x08000000;
+    // unsigned long data_at = 0x20000000;
+    // unsigned long bbs_at = 0x30000000;
+
+    // unsigned long flag = 0;
+
+    // flag |= PAGE_P;
+    // flag |= PAGE_US;
+
+    // p[text_at >> 22] = (unsigned long)pt_text_page | PAGE_P | PAGE_US;
+    // pt_text_page[0] = ring3_text_page;
+    // p[data_at >> 22] = (unsigned long)pt_data_page | PAGE_P | PAGE_WR | PAGE_US;
+    // pt_data_page[0] = ring3_data_page;
+    // p[bbs_at >> 22] = (unsigned long)pt_bss_page | PAGE_P | PAGE_WR | PAGE_US;
+    // pt_bss_page[0] = ring3_bss_page;
+
+    // // 
+    // asm("xchg %bx, %bx");
+    // LOAD_CR3((unsigned long)p);
 
     // 现在要准备返回用户态
     // eip --> edx
index 325234deca5c4e4d8ec1c387588aac4ae1c4fc6a..8b193b2c1b5a57363a3ffa8ffbe57367177a04c3 100644 (file)
@@ -39,7 +39,7 @@ pid_t get_next_pid() {
     return pid;
 }
 
-void load_cr3(task_union *tsk) { LOAD_CR3(tsk->cr3); }
+void load_cr3(task_union *tsk) { LoadCR3(tsk->cr3); }
 
 extern pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE)));
 
@@ -64,7 +64,7 @@ void init_root_tsk() {
     //    root_task.fps[i] = 0;
 
     root_task.esp0 = ((unsigned long)&root_task) + sizeof(root_task);
-    root_task.cr3 = (unsigned long)(init_pgd);
+    root_task.cr3 = va2pa((unsigned long)(init_pgd));
 
     tss.esp0 = root_task.esp0;
 
@@ -87,7 +87,7 @@ task_union *alloc_task_union() { return (task_union *)kmem_cache_alloc(task_unio
 inline task_union *get_next_tsk() { return 0; }
 
 void switch_to() {
-    LOAD_CR3(current->cr3);
+    LoadCR3(current->cr3);
     tss.esp0 = current->esp0;
     wrmsr(MSR_SYSENTER_ESP, current->esp0, 0);
 }
diff --git a/mm/mm.c b/mm/mm.c
index 9652e1e128df90ff6f621ef3add8de2cd09909c3..06bdad72ffd62da4cd4e2e2f80914d8e215edeb7 100644 (file)
--- a/mm/mm.c
+++ b/mm/mm.c
@@ -76,7 +76,7 @@ void init_paging() {
     // paging for user space
     set_page_shared(sysexit);
 
-    LOAD_CR3(init_pgd);
+    LoadCR3(va2pa(init_pgd));
 }
 
 void init_mm() {
index 31c14cbe35ac7aa452db97c789c03d6347efea6b..4a9f54e7fe478b174540af8baa44e66683b20c92 100644 (file)
--- a/mm/page.c
+++ b/mm/page.c
@@ -18,7 +18,7 @@
 #include <types.h>
 
 void do_no_page(void *addr) {
-    pde_t *page_dir = (pde_t *)current->cr3;
+    pde_t *page_dir = (pde_t *)pa2va(current->cr3);
     pte_t *page_tbl = 0;
 
     unsigned long page = alloc_one_page(0);
@@ -51,7 +51,7 @@ void do_wp_page(void *addr) {
     int npde = get_npd(addr);
     int npte = get_npt(addr);
 
-    pde_t *page_dir = (pde_t *)current->cr3;
+    pde_t *page_dir = (pde_t *)pa2va(current->cr3);
     pte_t *page_tbl = pa2va(PAGE_ALIGN(page_dir[npde]));
 
     unsigned long wp_pa_addr = PAGE_ALIGN(page_tbl[npte]);