From 8dc53f662e01877122d447a22adb28bb3085a3e9 Mon Sep 17 00:00:00 2001 From: Katharina Heidenreich Date: Sat, 27 Jun 2026 09:48:09 +0200 Subject: [PATCH] fix: memory layout and startup --- kernel/Makefile | 6 ++++- kernel/include/init/multiboot2.h | 2 +- kernel/include/init/print.h | 3 ++- kernel/linker.ld | 42 +++++++++++++++++--------------- kernel/src/init/init.cpp | 3 ++- kernel/src/init/print.cpp | 18 ++++++++------ kernel/src/startup/entry.cpp | 6 +++++ 7 files changed, 48 insertions(+), 32 deletions(-) diff --git a/kernel/Makefile b/kernel/Makefile index b2f612d..eb468a8 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -6,7 +6,7 @@ output_dir := $(root_dir)/output LD := ld CXX := clang++ -CXXFLAGS := -std=c++20 -fpie -fno-strict-aliasing -fno-stack-protector -fno-asynchronous-unwind-tables -fno-exceptions -fno-rtti -fno-common -mno-red-zone -mgeneral-regs-only -ffreestanding -Og -g -Wall -Wextra -Wno-reserved-identifier -I$(root_dir)/include +CXXFLAGS := -std=c++20 -fpie -fno-strict-aliasing -fno-stack-protector -fno-asynchronous-unwind-tables -fno-exceptions -fno-rtti -fno-common -mno-red-zone -mgeneral-regs-only -ffreestanding -O2 -g -Wall -Wextra -Wno-reserved-identifier -I$(root_dir)/include source_files := $(shell find $(source_dir) -type f -name "*.cpp") object_files := $(patsubst $(source_dir)/%,$(object_dir)/%.o,$(source_files)) @@ -23,6 +23,10 @@ kernel.bin: $(object_files) @echo "Disassembling kernel" @objdump -dC $(output_dir)/kernel.bin > $(output_dir)/kernel.asm @objdump -DC $(output_dir)/kernel.bin > $(output_dir)/kernel_full.asm + @echo "Dumping strings" + @strings -t x $(output_dir)/kernel.bin > $(output_dir)/kernel.strings + @echo "Hexdumping sections" + @objdump -sC $(output_dir)/kernel.bin > $(output_dir)/kernel.hex iso: kernel.bin diff --git a/kernel/include/init/multiboot2.h b/kernel/include/init/multiboot2.h index ccb0744..15305c5 100644 --- a/kernel/include/init/multiboot2.h +++ b/kernel/include/init/multiboot2.h @@ -245,7 +245,7 @@ struct info { inline info* get_multiboot_info() { auto addr = read_symbol("__multiboot_info"); - uint32_t phys = *paddr_t{addr}.access(); + uint32_t phys = *paddr_t{addr}.access(); return paddr_t{phys}.access(); } diff --git a/kernel/include/init/print.h b/kernel/include/init/print.h index ee5fc81..efe85ab 100644 --- a/kernel/include/init/print.h +++ b/kernel/include/init/print.h @@ -32,7 +32,8 @@ struct TextAttr { } }; +void initFromLow(); void clear(); void print(const char* str, TextAttr attr = {}); void print_dec(uint64_t value, TextAttr attr = {}); -void print_hex(uint64_t value, TextAttr attr = {}); +void print_hex(uint64_t value, TextAttr attr = {}); \ No newline at end of file diff --git a/kernel/linker.ld b/kernel/linker.ld index 36995be..80535ae 100644 --- a/kernel/linker.ld +++ b/kernel/linker.ld @@ -1,6 +1,8 @@ ENTRY(__start) +high_base = 0xFFFF800000000000; + MEMORY { MULTIBOOT_HEADER_PV : ORIGIN = 16k, LENGTH = 4k @@ -9,7 +11,7 @@ MEMORY KERNEL_MEMORY_P : ORIGIN = 64k, LENGTH = (1024M - ORIGIN(KERNEL_MEMORY_P)) - HIGH_KERNEL_V : ORIGIN = 0xFFFF800000000000 + 64k, LENGTH = LENGTH(KERNEL_MEMORY_P) + HIGH_KERNEL_V : ORIGIN = high_base + ORIGIN(KERNEL_MEMORY_P), LENGTH = LENGTH(KERNEL_MEMORY_P) } SECTIONS @@ -29,31 +31,31 @@ SECTIONS KEEP(*(.startup_data)) __startup_data_end__ = .; } > START_MEMORY_PV - .text : { + .text : AT(ADDR(.text) - high_base) { __text_start__ = .; *(EXCLUDE_FILE(*trampoline*) .text) __text_end__ = .; - } > HIGH_KERNEL_V AT > KERNEL_MEMORY_P - .rodata : { + } > HIGH_KERNEL_V + .rodata : AT(ADDR(.rodata) - high_base) { __rodata_start__ = .; *(EXCLUDE_FILE(*trampoline*) .rodata) __rodata_end__ = .; - } > HIGH_KERNEL_V AT > KERNEL_MEMORY_P - .data : { + } > HIGH_KERNEL_V + .data : AT(ADDR(.data) - high_base) { __data_start__ = .; *(EXCLUDE_FILE(*trampoline*) .data) __data_end__ = .; - } > HIGH_KERNEL_V AT > KERNEL_MEMORY_P - .bss : { - __bss_start__ = .; - *(EXCLUDE_FILE(*trampoline*) .bss) - __bss_end__ = .; - } > HIGH_KERNEL_V AT > KERNEL_MEMORY_P - .reserved_ranges : { + } > HIGH_KERNEL_V + .reserved_ranges : AT(ADDR(.reserved_ranges) - high_base) { __reserved_ranges_start__ = .; KEEP(*(.reserved_ranges)) __reserved_ranges_end__ = .; - } > HIGH_KERNEL_V AT > KERNEL_MEMORY_P + } > HIGH_KERNEL_V + .bss (NOLOAD) : AT(ADDR(.bss) - high_base) { + __bss_start__ = .; + *(EXCLUDE_FILE(*trampoline*) .bss) + __bss_end__ = .; + } > HIGH_KERNEL_V .trampoline_text : { __trampoline_text_start__ = .; @@ -67,18 +69,18 @@ SECTIONS __trampoline_data_end__ = .; } > TRAMPOLINE_PV - .trampoline_bss : { - __trampoline_bss_start__ = .; - *trampoline*(.bss) - __trampoline_bss_end__ = .; - } > TRAMPOLINE_PV - .trampoline_rodata : { __trampoline_rodata_start__ = .; *trampoline*(.rodata) __trampoline_rodata_end__ = .; } > TRAMPOLINE_PV + .trampoline_bss (NOLOAD) : { + __trampoline_bss_start__ = .; + *trampoline*(.bss) + __trampoline_bss_end__ = .; + } > TRAMPOLINE_PV + /DISCARD/ : { *(.comment) *(.eh_frame) diff --git a/kernel/src/init/init.cpp b/kernel/src/init/init.cpp index 971980e..703752b 100644 --- a/kernel/src/init/init.cpp +++ b/kernel/src/init/init.cpp @@ -45,7 +45,8 @@ static void loadMultiboot() { } [[maybe_unused]] [[noreturn]] __attribute__((used)) void init() { + initFromLow(); print("Reached init\n", {}); - //step("multiboot", loadMultiboot); + step("multiboot", loadMultiboot); halt(); } diff --git a/kernel/src/init/print.cpp b/kernel/src/init/print.cpp index 62d2b3b..9c7e5b1 100644 --- a/kernel/src/init/print.cpp +++ b/kernel/src/init/print.cpp @@ -11,17 +11,14 @@ static constexpr int HEX_TOP_SHIFT = (sizeof(uint64_t) * 8) - HEX_BITS; static const char hex_chars[] = "0123456789ABCDEF"; -static volatile uint32_t* cursor() { - auto cursor_addr = read_symbol("__cursor"); - return paddr_t{cursor_addr}.access(); -} +static uint32_t cursor; static volatile uint16_t* vga_cell(uint32_t offset) { return paddr_t{VGA_PHYS_BASE + offset}.access(); } static void next_line() { - *cursor() = ((*cursor() + BYTES_PER_ROW - 1) / BYTES_PER_ROW) * BYTES_PER_ROW; + cursor = ((cursor + BYTES_PER_ROW - 1) / BYTES_PER_ROW) * BYTES_PER_ROW; } static void put_char(char ch, uint16_t attr_word) { @@ -29,12 +26,17 @@ static void put_char(char ch, uint16_t attr_word) { next_line(); return; } - *vga_cell(*cursor()) = attr_word | (uint8_t)ch; - *cursor() += BYTES_PER_CELL; + *vga_cell(cursor) = attr_word | (uint16_t)ch; + cursor += BYTES_PER_CELL; +} + +void initFromLow () { + auto __cursor = read_symbol("__cursor"); + cursor = *paddr_t{__cursor}.access(); } void clear() { - *cursor() = 0; + cursor = 0; for (int i = 0; i < VGA_COLS * VGA_ROWS; i++) { *vga_cell(i * BYTES_PER_CELL) = 0; } diff --git a/kernel/src/startup/entry.cpp b/kernel/src/startup/entry.cpp index 01a0cfe..e9a1ab8 100644 --- a/kernel/src/startup/entry.cpp +++ b/kernel/src/startup/entry.cpp @@ -141,6 +141,12 @@ __clear: mov $0xb8000, %edi mov $0x2000, %ecx rep stosw + mov $0x3D4, %dx + mov $0x0A, %al + out %al, %dx + inc %dx + mov $0x20, %al + out %al, %dx ret __print: