Repos / kernel.git / commitdiff
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 53140a3)
raw | patch | inline | side by side (parent: 53140a3)
| author | zhaoyanbai <zhaoyanbai@126.com> | |
| Wed, 3 Nov 2021 10:48:35 +0000 (18:48 +0800) | ||
| committer | zhaoyanbai <zhaoyanbai@126.com> | |
| Wed, 3 Nov 2021 10:48:35 +0000 (18:48 +0800) |
| include/page.h | patch | blob | history | |
| kernel/init.c | patch | blob | history | |
| kernel/sched.c | patch | blob | history | |
| kernel/system.c | patch | blob | history | |
| scripts/link.ld | patch | blob | history |
diff --git a/include/page.h b/include/page.h
--- a/include/page.h
+++ b/include/page.h
#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
--- a/kernel/init.c
+++ b/kernel/init.c
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
--- a/kernel/sched.c
+++ b/kernel/sched.c
// 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
--- a/kernel/system.c
+++ b/kernel/system.c
}
irq_restore(flags);
}
-
-char gdtr[6], idtr[6];
diff --git a/scripts/link.ld b/scripts/link.ld
--- a/scripts/link.ld
+++ b/scripts/link.ld
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 = .;