]> Zhao Yanbai Git Server - kernel.git/commitdiff
改为用C实现初始分页
authoracevest <zhaoyanbai@126.com>
Sun, 28 Dec 2025 03:50:39 +0000 (11:50 +0800)
committeracevest <zhaoyanbai@126.com>
Sun, 28 Dec 2025 03:50:39 +0000 (11:50 +0800)
Makefile
boot/boot.c
boot/multiboot.S
boot/unpaged_boot.c [new file with mode: 0644]
gdbscript
include/page.h
include/tty.h
kernel/fork.c
scripts/link.ld

index 76626ec1fba0abe222bfa26a5e9b173af636a5e3..4349e0e2f5857d8dde04afeebc264abbf65a847f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,7 @@ endif
 
 CC             := $(CROSS_PREFIX)gcc
 LD             := $(CROSS_PREFIX)ld
+OBJCOPY        := $(CROSS_PREFIX)objcopy
 
 ifeq (,$(shell which $(CC) 2>/dev/null))
     $(error "编译器 $(CC) 未找到 请安装交叉编译工具链")
@@ -39,6 +40,7 @@ CFLAGS        += -fno-pic
 CFLAGS += -fno-omit-frame-pointer
 # 禁用控制流保护: Control-Flow Enforcement Technology (CET)
 CFLAGS += -fcf-protection=none
+CFLAGS += -nostdlib
 CFLAGS += -DNR_TTYS=3
 CFLAGS += -DFIXED_SYSENTER_ESP_MODE=1
 CFLAGS += -DENABLE_BOOT_WAIT=0
index e4d52145f0237baca9612e11aaea44f9aed5cf01..6480e14e86ec1f7983fdab1dac38f1ed57379a55 100644 (file)
@@ -246,42 +246,6 @@ extern void *kernel_begin;
 extern void *kernel_end;
 extern void *bootmem_bitmap_begin;
 
-
-#include <cpuid.h>
-
-__attribute__((section(".unpaged_text")))
-void test_unpaged_break() {
-    // printk("test unpaged break\n");
-}
-
-
-void lapic_init() {
-    cpuid_regs_t r;
-    r = cpuid(1);
-    if(r.edx & (1 << 9)) {
-        printk("local apic supported\n");
-        if(r.ecx & (1 << 21)) {
-            printk("x2apic supported\n");
-        } else {
-            panic("x2apic not supported\n");
-        }
-    } else {
-        panic("local apic not supported\n");
-    }
-
-    uint64_t apic_base = read_msr(MSR_IA32_APIC_BASE);
-    printk("apic base: %016lx\n", apic_base);
-
-    // 开启2xapic
-    apic_base |= (1 << 10);
-    write_msr(MSR_IA32_APIC_BASE, apic_base);
-
-    apic_base = read_msr(MSR_IA32_APIC_BASE);
-    printk("after 2xapic enable apic base: %016lx\n", apic_base);
-
-    test_unpaged_break();
-}
-
 void init_system_info() {
     system.kernel_begin = &kernel_begin;
     system.kernel_end = &kernel_end;
@@ -296,5 +260,4 @@ void init_system_info() {
 
     boot_delay(DEFAULT_BOOT_DELAY_TICKS);
 
-    lapic_init();
 }
index 2e9f7e601edc925cafc39f1e1df818dd3379cd1e..e071a1771bc3d4fd02197d4869859aed8277a1e9 100644 (file)
@@ -22,9 +22,6 @@
 .extern check_kernel
 .extern init_system_info
 .extern setup_kernel
-.extern init_pgd
-.extern init_pgt
-.extern kernel_virtual_addr_start
 .extern root_task
 .extern root_task_entry
 
@@ -54,82 +51,23 @@ real_kernel_entry:
     movw    %dx,%fs
     movw    %dx,%gs
 
-
-    nop
-    nop
-    nop
-    nop
     movl    $(stack + MULTIBOOT_STACK_SIZE), %esp
-    #leal stack, %esp
-    #addl MULTIBOOT_STACK_SIZE, %esp
 
     # Reset EFLAGS
     pushl   $0
     popf
 
-    call test_unpaged_break
-
     # Save Multiboot Infomation...
     pushl   %eax
     addl    $KRNLADDR,%ebx
     pushl   %ebx
 
-    # Setup Paging
-    # Clear Page Directory
-    movl    $init_pgd-KRNLADDR,%ebx
-    movl    %ebx,%edi
-    movl    $1024,%ecx
-    xorl    %eax,%eax
-    cld
-    rep
-    stosl
-
-
-    # Length = BOOT_INIT_PAGETBL_CNT*4M
-    # [0x00000000, 0x00000000 + Length)
-    # [0xC0000000, 0xC0000000 + Length)
-    # 这两个线性地址空间都映射到同一片物理内存空间: [0x00000000, 0x00000000 + Length)
-
-
-    # 初始化页目录
-    # 设置低端线性地址空间的页表项
-    movl    %ebx,%edi
-    movl    $init_pgt-KRNLADDR,%eax
-    addl    $3,%eax
-    movl    $BOOT_INIT_PAGETBL_CNT,%ecx
-1:
-    stosl
-    addl    $0x1000,%eax
-    loop    1b
-
-    # 设置内核线性地址空间的页表项
-    movl    $kernel_virtual_addr_start,%eax
-    shrl    $20,%eax
-    addl    %ebx,%eax
-    movl    %eax,%edi
-
-    movl    $init_pgt-KRNLADDR,%eax
-    addl    $3,%eax
-    movl    $BOOT_INIT_PAGETBL_CNT,%ecx
-2:
-    stosl
-    addl    $0x1000,%eax
-    loop    2b
-
-    # 初始化页表
-    # 直接线性映射BOOT_INIT_PAGETBL_CNT*4M物理内存
-    movl    $init_pgt-KRNLADDR,%ebx
-    movl    %ebx,%edi
-    movl    $(BOOT_INIT_PAGETBL_CNT*1024),%ecx
-    movl    $3,%eax
-    cld
-3:
-    stosl
-    addl    $0x1000,%eax
-    loop    3b
-
-    movl    $init_pgd-KRNLADDR,%ebx
-    movl    %ebx,%cr3
+    call lapic_init
+
+    call boot_paging
+
+    #movl    $init_pgd-KRNLADDR,%ebx
+    #movl    %ebx,%cr3
 
     # enable PG WP
     movl    %cr0,%eax
diff --git a/boot/unpaged_boot.c b/boot/unpaged_boot.c
new file mode 100644 (file)
index 0000000..b22049e
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: unpaged_boot.c
+ *      Author: Zhao Yanbai
+ *              2025-12-27 21:21:24 Saturday CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+
+#include <types.h>
+#include <msr.h>
+#include <page.h>
+#include <linkage.h>
+#include <boot.h>
+
+
+
+extern unsigned long kernel_virtual_addr_start;
+extern pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE)));
+extern pte_t __initdata init_pgt[PTECNT_PER_PAGE * BOOT_INIT_PAGETBL_CNT] __attribute__((__aligned__(PAGE_SIZE)));
+
+// Length = BOOT_INIT_PAGETBL_CNT*4M
+// [0x00000000, 0x00000000 + Length - 1]
+// [0xC0000000, 0xC0000000 + Length - 1]
+// 这两个线性地址空间都映射到同一片物理内存空间: [0x00000000, 0x00000000 + Length -1]
+// 总共 BOOT_INIT_PAGETBL_CNT * 4M 物理内存
+void boot_paging() {
+    unsigned long init_pgd_paddr = va2pa(init_pgd);
+    unsigned long init_pgt_paddr = va2pa(init_pgt);
+
+    // 清空页目录
+    pde_t *dir =(pde_t *)init_pgd_paddr;
+    for(int i=0; i<PDECNT_PER_PAGE; i++) {
+        dir[i] = 0;
+    }
+
+    // 初始化页目录
+    unsigned long kpde_base = get_npde((unsigned long)(&kernel_virtual_addr_start));
+    pde_t pd_entry = init_pgt_paddr | PAGE_US | PAGE_WR | PAGE_P;
+    for(int i=0; i<BOOT_INIT_PAGETBL_CNT; i++) {
+        dir[i] = pd_entry; // 设置低端线性地址空间的页表项
+        dir[kpde_base+i] = pd_entry; // 设置内核线性地址空间的页表项
+        pd_entry += PAGE_SIZE;
+    }
+
+
+    pte_t *table = (pte_t *)init_pgt_paddr;
+    pte_t pt_entry = PAGE_US | PAGE_WR | PAGE_P;
+    for(int i=0; i<BOOT_INIT_PAGETBL_CNT*PTECNT_PER_PAGE; i++) {
+        table[i] = pt_entry;
+        pt_entry += PAGE_SIZE;
+    }
+
+
+    // 设置页目录
+    asm volatile("mov %0, %%cr3"::"r"(init_pgd_paddr));
+}
+
+
+void lapic_init() {
+    // cpuid_regs_t r;
+    // r = cpuid(1);
+    // if(r.edx & (1 << 9)) {
+    //     printk("local apic supported\n");
+    //     if(r.ecx & (1 << 21)) {
+    //         printk("x2apic supported\n");
+    //     } else {
+    //         panic("x2apic not supported\n");
+    //     }
+    // } else {
+    //     panic("local apic not supported\n");
+    // }
+
+    uint64_t apic_base = read_msr(MSR_IA32_APIC_BASE);
+    // printk("apic base: %016lx\n", apic_base);
+
+    // 开启2xapic
+    apic_base |= (1 << 10);
+    write_msr(MSR_IA32_APIC_BASE, apic_base);
+
+    apic_base = read_msr(MSR_IA32_APIC_BASE);
+    // printk("after 2xapic enable apic base: %016lx\n", apic_base);
+}
index e36ea99bda9883959f9931ea2a684c11d42ef5d1..d31b702ce0c2e8e8f4342b27c73990524b441d3a 100644 (file)
--- a/gdbscript
+++ b/gdbscript
@@ -4,8 +4,6 @@ set confirm off
 set architecture i386
 
 #b *0x100000
-b multiboot.S:60
-b multiboot.S:136
 
 #b *0x7c00
 
index ad11224a9869bb466cf384962a2d676dff9c947d..12b465d7a368a56877da02ea2aa8b8526a9670fe 100644 (file)
@@ -62,7 +62,6 @@
 #define PDE_PWT 0x008
 #define PDE_PCD 0X010
 #define PDE_A 0x020
-// #define PDE_D 0x040
 #define PDE_PS 0x080
 #define PDE_G 0x100
 #define PDE_AVL 0xE00
index 3d81ec0655b0fd28512b288c88cfc87861285565..f526b89338661d5deb415d7c2714dbae94c2c6b0 100644 (file)
@@ -50,4 +50,6 @@ void tty_switch(tty_t *tty);
 
 void tty_switch_to_next();
 
+void tty_clear(tty_t *tty);
+
 extern tty_t *current_tty;
index 81309af2c059021cb10e24ae5b779762517f2540..dc99843812a4b5be974bbc0c0de89e0493544c0f 100644 (file)
@@ -52,7 +52,7 @@ int do_fork(pt_regs_t *regs, unsigned long flags) {
         unsigned long spde = (unsigned long)pde_src[i];
         unsigned long dpde = 0;
 
-        if (i >= 768) {
+        if (i >= get_npde(PAGE_OFFSET)) {
             pde_dst[i] = pde_src[i];
             continue;
         }
index 36e87a816da7f5dd271eba8505f1515e00736a05..e7049e03a1aa014ad20f9aa45dcd19656b119b26 100644 (file)
@@ -33,16 +33,23 @@ SECTIONS
         *(.kernel_entry_text)
         *(.multiboot2_header)
         boot/multiboot.S.o(.text)
-        *(.unpaged_text)
+        *unpaged_*.o(.text)
     }
     eunpagged_text = .;
+
     . = ALIGN(0x1000);
     .unpaged_data :
     {
-        boot/multiboot.S.o(COMMON)
-       *(.unpaged_data)
+       *unpaged_*.o(.data .rodata*)
     }
     eunpagged_data = .;
+    . = ALIGN(0x1000);
+    .unpaged_bss : ALIGN(0x1000)
+    {
+        boot/multiboot.S.o(.bss COMMON)
+        *unpaged_*.o(.bss COMMON)
+    }
+    eunpaged_rodata = .;
 
     /* . = kernel_begin; */
     . += kernel_virtual_addr_start;