From: acevest Date: Wed, 3 Nov 2021 10:48:35 +0000 (+0800) Subject: 准备ring3的地址映射 X-Git-Url: http://zhaoyanbai.com/repos/?a=commitdiff_plain;h=6909ffdad3ee9e424d8ecef346679b55ee13d910;p=kernel.git 准备ring3的地址映射 --- diff --git a/include/page.h b/include/page.h index 28bb569..3148212 100644 --- a/include/page.h +++ b/include/page.h @@ -17,9 +17,9 @@ #ifndef _PAGE_H #define _PAGE_H -#define PAGE_P 0x1 -#define PAGE_WR 0x2 -#define PAGE_US 0x4 +#define PAGE_P 0x1 // 在内存中 +#define PAGE_WR 0x2 // 表示可读写 +#define PAGE_US 0x4 // 用户级 #define PAGE_SHIFT (12) #define PAGE_SIZE (1UL << PAGE_SHIFT) diff --git a/kernel/init.c b/kernel/init.c index cb9f1e1..b75e2dc 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -13,31 +13,66 @@ void root_task_entry(); -TSS tss; System system; -Desc idt[NIDT]; -Desc gdt[NGDT]; +TSS tss; +Desc idt[NIDT] __attribute__((__aligned__(8))); +Desc gdt[NGDT] __attribute__((__aligned__(8))); +char gdtr[6] __attribute__((__aligned__(4))); +char idtr[6] __attribute__((__aligned__(4))); -char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__((__aligned__(PAGE_SIZE))); +// char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__((__aligned__(PAGE_SIZE))); -int debug_wait_queue_get(); +// int debug_wait_queue_get(); -// #define __ring3section__ __attribute__((__section__(".ring3"))) +#define __ring3text__ __attribute__((__section__(".ring3.text"), __aligned__(PAGE_SIZE))) +#define __ring3data__ __attribute__((__section__(".ring3.data"), __aligned__(PAGE_SIZE))) +#define __ring3bss__ __attribute__((__section__(".ring3.bss"), __aligned__(PAGE_SIZE))) -// char __attribute__((__section__(".ring3.data"))) ring3_stack[PAGE_SIZE] = {0}; -// void __ring3section__ ring3_entry() { -// while (1) { -// systest(); -// } -// } +char __ring3data__ ring3_stack[PAGE_SIZE] = {0}; +char __ring3bss__ ring3_stack[PAGE_SIZE]; +void __ring3text__ ring3_entry() { + while (1) { + systest(); + } +} 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); + // 现在要准备返回用户态 // eip --> edx // esp --> ecx - // asm("sysexit;" ::"d"(ring3_entry), "c"(ring3_stack + PAGE_SIZE)); + asm("xchg %bx, %bx"); + // asm("sysexit;" ::"d"(0x08000000), "c"(0x30000000 + PAGE_SIZE)); while (1) { asm("hlt;"); } diff --git a/kernel/sched.c b/kernel/sched.c index 8e210f1..325234d 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -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 = (unsigned long)(init_pgd); tss.esp0 = root_task.esp0; diff --git a/kernel/system.c b/kernel/system.c index a4bc4e4..0604df1 100644 --- a/kernel/system.c +++ b/kernel/system.c @@ -175,5 +175,3 @@ void system_delay() { } irq_restore(flags); } - -char gdtr[6], idtr[6]; diff --git a/scripts/link.ld b/scripts/link.ld index 98f31e9..ce2afbd 100644 --- a/scripts/link.ld +++ b/scripts/link.ld @@ -49,6 +49,25 @@ SECTIONS ebss = .; + .ring3.text : AT(phys_addr) ALIGN(0x1000) + { + phys_addr = . - kernel_virtual_addr_start; + *(.ring3.text); + } + + .ring3.data : AT(phys_addr) ALIGN(0x1000) + { + phys_addr = . - kernel_virtual_addr_start; + *(.ring3.data); + } + + .ring3.bss : AT(phys_addr) ALIGN(0x1000) + { + phys_addr = . - kernel_virtual_addr_start; + *(.ring3.bss); + } + + .init.data : AT(phys_addr) ALIGN(0x1000) { initdata = .;