From: acevest Date: Wed, 3 Nov 2021 11:59:58 +0000 (+0800) Subject: cr3字段统一处理成物理地址 X-Git-Url: http://zhaoyanbai.com/repos/named.html?a=commitdiff_plain;h=59068dc23141c07fba1b2e1289e9ae44a9267263;p=kernel.git cr3字段统一处理成物理地址 --- diff --git a/include/page.h b/include/page.h index 3148212..0b0068d 100644 --- a/include/page.h +++ b/include/page.h @@ -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; diff --git a/kernel/exec.c b/kernel/exec.c index 4f33469..b2afffa 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -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) { diff --git a/kernel/fork.c b/kernel/fork.c index 08fea39..f064622 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -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]; diff --git a/kernel/init.c b/kernel/init.c index b75e2dc..9ce0257 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -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 diff --git a/kernel/sched.c b/kernel/sched.c index 325234d..8b193b2 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -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 9652e1e..06bdad7 100644 --- 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() { diff --git a/mm/page.c b/mm/page.c index 31c14cb..4a9f54e 100644 --- a/mm/page.c +++ b/mm/page.c @@ -18,7 +18,7 @@ #include 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]);