]> Zhao Yanbai Git Server - kernel.git/commitdiff
准备ring3的地址映射
authoracevest <zhaoyanbai@126.com>
Wed, 3 Nov 2021 10:48:35 +0000 (18:48 +0800)
committeracevest <zhaoyanbai@126.com>
Wed, 3 Nov 2021 10:48:35 +0000 (18:48 +0800)
include/page.h
kernel/init.c
kernel/sched.c
kernel/system.c
scripts/link.ld

index 28bb56936cd812a2181dd3abd8f4a968bf0a6faf..3148212cbb81d2845322e7675c28145e0aa1c886 100644 (file)
@@ -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)
index cb9f1e1e3c24060f53480e013aca09a087640d10..b75e2dcc58e9910c043228d84f18bd668d24a806 100644 (file)
 
 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;");
     }
index 8e210f1d898f537c36bc3b4808245a77c5f5a8c2..325234deca5c4e4d8ec1c387588aac4ae1c4fc6a 100644 (file)
@@ -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;
 
index a4bc4e4311cbc714d0fdb33ddd07653b7e8d1c9e..0604df130e13436b110f2aadf0d9c077b2814d57 100644 (file)
@@ -175,5 +175,3 @@ void system_delay() {
     }
     irq_restore(flags);
 }
-
-char gdtr[6], idtr[6];
index 98f31e9623e47324beb440f4f2b9910582615367..ce2afbd86182cdd946953d8189889ac726ac3d55 100644 (file)
@@ -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 = .;