fix: memory layout and startup

This commit is contained in:
Katharina 2026-06-27 09:48:09 +02:00
parent 92d8a91be4
commit 8dc53f662e
7 changed files with 48 additions and 32 deletions

View file

@ -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

View file

@ -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>();
uint32_t phys = *paddr_t{addr}.access<volatile uint32_t>();
return paddr_t{phys}.access<info>();
}

View file

@ -32,6 +32,7 @@ struct TextAttr {
}
};
void initFromLow();
void clear();
void print(const char* str, TextAttr attr = {});
void print_dec(uint64_t value, TextAttr attr = {});

View file

@ -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)

View file

@ -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();
}

View file

@ -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<volatile uint32_t>();
}
static uint32_t cursor;
static volatile uint16_t* vga_cell(uint32_t offset) {
return paddr_t{VGA_PHYS_BASE + offset}.access<volatile uint16_t>();
}
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<volatile uint32_t>();
}
void clear() {
*cursor() = 0;
cursor = 0;
for (int i = 0; i < VGA_COLS * VGA_ROWS; i++) {
*vga_cell(i * BYTES_PER_CELL) = 0;
}

View file

@ -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: