From 717989bbe32f8a2fcacba01baff17fe62fd362c8 Mon Sep 17 00:00:00 2001 From: TunaCici <36923239+TunaCici@users.noreply.github.com> Date: Thu, 4 Apr 2024 15:24:00 +0300 Subject: [PATCH] westeros: Preperations for VMM. Housekeeping. Signed-off-by: TunaCici <36923239+TunaCici@users.noreply.github.com> --- Kernel/Arch/ARM64/Entry.S | 4 --- Kernel/Main.c | 12 ++++--- Kernel/Start.c | 17 --------- Kernel/kernel.ld | 73 ++++++++++++++++++++++----------------- 4 files changed, 48 insertions(+), 58 deletions(-) diff --git a/Kernel/Arch/ARM64/Entry.S b/Kernel/Arch/ARM64/Entry.S index a88a98b..455a784 100644 --- a/Kernel/Arch/ARM64/Entry.S +++ b/Kernel/Arch/ARM64/Entry.S @@ -25,10 +25,6 @@ _start: ldr x2, =end_entry mov x3, #0x00 - /* Register vector tables */ - adr x0, vector_table - msr vbar_el1, x0 - /* Enable access to FPU & Advanced SIMD (NEON) */ mrs x0, CPACR_EL1 orr x0, x0, #(0x3 << 20) diff --git a/Kernel/Main.c b/Kernel/Main.c index 9762b8c..93e692b 100644 --- a/Kernel/Main.c +++ b/Kernel/Main.c @@ -20,14 +20,12 @@ #include "Memory/Physical.h" #include "Memory/Virtual.h" -/* Kernel physical start & end addresses */ -extern uint64_t kstart; -extern uint64_t kend; +extern uint64_t kend; /* in Kernel/kerne.ld */ +extern uint64_t vector_table; /* in Kernel/Arch/ARM64/Vector.S */ void kmain(void) { - volatile const uint8_t *kernel_start = (uint8_t*) &kstart; - volatile const uint8_t *kernel_end = (uint8_t*) &kend; + const void *kernel_end = &kend; uint64_t mem_start = 0x0; uint64_t mem_end = 0x0; @@ -53,6 +51,10 @@ void kmain(void) wfi(); } + /* X. Setup vector tables */ + MSR("vbar_el1", vector_table); + isb(); + /* 1. Init BootMem */ klog("[kmain] Initializing early memory manager...\n"); diff --git a/Kernel/Start.c b/Kernel/Start.c index 60634cc..7f8db09 100644 --- a/Kernel/Start.c +++ b/Kernel/Start.c @@ -18,9 +18,6 @@ extern void kmain(void); -extern uint64_t kstart; -extern uint64_t kend; - void _halt(const char *s) { klog("Halting due to: %s", s); @@ -38,10 +35,6 @@ void start(void) const char *_cpuModel = "Cortex A-72"; const uint32_t _coreCount = 2u; - /* Symbols. Defined in Kernel/kernel.ld */ - const uint64_t *kernelBase = (uint64_t*) &kstart; - const uint64_t *kernelEnd = (uint64_t*) &kend; - klog("WesterOS early boot stage\n"); klog("Running sanity checks...\n"); @@ -129,9 +122,6 @@ void start(void) kprintf("Unmasked\n"); } - MRS("VBAR_EL1", val64); - klog("---- Vector Table: 0x%lx\n", val64); - /* -------- Machine Layout -------- */ klog("QEMU ARM Virt Machine memory layout:\n"); @@ -163,13 +153,6 @@ void start(void) DTB_START, DTB_END ); - klog ("---- Kernel: 0x%p - 0x%p (system)\n", - kernelBase, - kernelEnd - ); - - val64 = (kernelEnd - kernelBase); - klog("WesterOS kernel size: %lu bytes\n", val64); klog("Everything's OK. Calling the Kernel now...\n"); kmain(); diff --git a/Kernel/kernel.ld b/Kernel/kernel.ld index a5a28ad..09ce0cc 100644 --- a/Kernel/kernel.ld +++ b/Kernel/kernel.ld @@ -9,76 +9,85 @@ OUTPUT_ARCH(aarch64) ENTRY(_start) ENTRY_INIT_STACK_SIZE = 0x2000; /* 8 KiB */ +KERNEL_VMA_BASE = 0xFFFF000000000000; /* TTBR1_EL1 lower boundary */ SECTIONS { - /* the entry point, before enabling paging. The code to enable paing - * needs to have the same virtual/physical address. Entry.S and Start.c - * run in this initial setting. + /* + * The entry point, before enabling paging. + * The code to enable paging needs to have the same VA & PA. + * Only Entry.S and Start.c run in this initial setting. */ - /* QEMU loads DTB at 0x00000000_40000000. So we skip 1MiB */ + /* QEMU loads DTB at 0x00000000_40000000. So we skip over it (1MiB) */ . = 0x40100000; - PROVIDE (kstart = .); - - .start_sec : { + + .start_sec : + { + _start_sec = .; + Build/Entry.o(.text) - Build/Vector.o(.text) Build/Start.o(.text .text.*) Build/Entry.o(.rodata .rodata.*) - Build/Vector.o(.rodata .rodata.*) Build/Start.o(.rodata .rodata.*) Build/Entry.o(.data .data.*) - Build/Vector.o(.data .data.*) Build/Start.o(.data .data.*) PROVIDE(edata_entry = .); Build/Entry.o(.bss .bss.* COMMON) - Build/Vector.o(.bss .bss.* COMMON) Build/Start.o(.bss .bss.* COMMON) - /* Reserve space for stack */ + /* Reserve stack space for kernel */ . = ALIGN(0x1000); . += ENTRY_INIT_STACK_SIZE; PROVIDE (init_stktop = .); PROVIDE(end_entry = .); - . += 0x1000; + . = ALIGN(0x1000); } - /* the kernel executes at the higher address space, but loaded - at the lower memory (0x30000) */ + /* + * Kernel has two adresses: + * Load Memory Address (LMA): right after '.start_sec' + * Virtual Memory Address (VMA): right after 'KERNEL_VMA_BASE' + * + * https://sourceware.org/binutils/docs/ld/Basic-Script-Concepts.html + */ + + /* . = KERNEL_VMA_BASE; */ - .text : { /** here to make code also copied into phymem **/ + .text : AT(ADDR(.start_sec) + SIZEOF(.start_sec)) + { + _code = .; *(.text .text.* .gnu.linkonce.t.*) + *(.rodata*) + . = ALIGN(0x1000); } - PROVIDE(etext = .); /* Define the 'etext' symbol to this value */ - - .rodata : { - *(.rodata .rodata.* .gnu.linkonce.r.*) - } - - /* aligned the data to a (4K) page, so it can be assigned - different protection than the code*/ - . = ALIGN(0x1000); - - PROVIDE (data_start = .); - - .data : { + .data : AT(ADDR(.text) + SIZEOF(.text)) + { + _data = .; *(.data .data.*) + . = ALIGN(0x1000); } - PROVIDE (edata = .); + .eh_frame : AT(ADDR(.data) + SIZEOF(.data)) + { + _ehframe = .; + *(.eh_frame) + . = ALIGN(0x1000); + } - .bss : { + .bss : AT(ADDR(.eh_frame) + SIZEOF(.eh_frame)) + { + _bss = .; *(.bss .bss.* COMMON) + . = ALIGN(0x1000); } - . = ALIGN(0x1000); PROVIDE (kend = .); }