From: acevest Date: Sun, 28 Dec 2025 03:50:39 +0000 (+0800) Subject: 改为用C实现初始分页 X-Git-Url: http://repos.zhaoyanbai.com/?a=commitdiff_plain;h=c4a89281a4a59a25d0d3f753df7d7cd65033fc92;p=kernel.git 改为用C实现初始分页 --- diff --git a/Makefile b/Makefile index 76626ec..4349e0e 100644 --- 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 diff --git a/boot/boot.c b/boot/boot.c index e4d5214..6480e14 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -246,42 +246,6 @@ extern void *kernel_begin; extern void *kernel_end; extern void *bootmem_bitmap_begin; - -#include - -__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(); } diff --git a/boot/multiboot.S b/boot/multiboot.S index 2e9f7e6..e071a17 100644 --- a/boot/multiboot.S +++ b/boot/multiboot.S @@ -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 index 0000000..b22049e --- /dev/null +++ b/boot/unpaged_boot.c @@ -0,0 +1,83 @@ +/* + * ------------------------------------------------------------------------ + * File Name: unpaged_boot.c + * Author: Zhao Yanbai + * 2025-12-27 21:21:24 Saturday CST + * Description: none + * ------------------------------------------------------------------------ + */ + +#include +#include +#include +#include +#include + + + +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= 768) { + if (i >= get_npde(PAGE_OFFSET)) { pde_dst[i] = pde_src[i]; continue; } diff --git a/scripts/link.ld b/scripts/link.ld index 36e87a8..e7049e0 100644 --- a/scripts/link.ld +++ b/scripts/link.ld @@ -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;