From c8cb4346064c69ab8431aa0d3c287e2fad60ce80 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 22 Jul 2025 21:23:23 +0000 Subject: x86_64: split bootstrap code along mode lines --- arch/x86_64/src/boot/entry64.s | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 arch/x86_64/src/boot/entry64.s (limited to 'arch/x86_64/src/boot/entry64.s') diff --git a/arch/x86_64/src/boot/entry64.s b/arch/x86_64/src/boot/entry64.s new file mode 100644 index 0000000..f575c50 --- /dev/null +++ b/arch/x86_64/src/boot/entry64.s @@ -0,0 +1,21 @@ +.section .boot_text, "ax", @progbits +.code64 + +.global _entry64 +_entry64: + mov $global_descriptor_table_data, %rax + mov %rax, %ss + mov %rax, %ds + mov %rax, %es + mov %rax, %fs + mov %rax, %gs + + xor %rax, %rax + + call _init + + call main + +1: + hlt + jmp 1b -- cgit v1.2.3 From f62b05c93c6c539d899d2656c0638d404a036f1a Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 24 Jul 2025 12:28:23 +0000 Subject: x86_64: implement robust C++ global initialization Implement a comprehensive mechanism to ensure correct C++ runtime initialization before the kernel main function is called. This replaces the previous, incomplete reliance on an `_init` function. The new design robustly handles both legacy `.ctors` and modern `.init_array` initialization schemes used by the GNU toolchain. A single C++ function, `invoke_global_constructors`, now iterates through both arrays of function pointers to ensure all types of global initializers are executed. --- arch/x86_64/src/boot/entry64.s | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/boot/entry64.s') diff --git a/arch/x86_64/src/boot/entry64.s b/arch/x86_64/src/boot/entry64.s index f575c50..110ced8 100644 --- a/arch/x86_64/src/boot/entry64.s +++ b/arch/x86_64/src/boot/entry64.s @@ -12,8 +12,8 @@ _entry64: xor %rax, %rax - call _init - + call invoke_global_constructors + call main 1: -- cgit v1.2.3 From ef907825e861b63726952bb34b425a98f34ed412 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 24 Jul 2025 13:52:42 +0000 Subject: x86_64: provide a clean slate on entry to long mode --- arch/x86_64/src/boot/entry64.s | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/boot/entry64.s') diff --git a/arch/x86_64/src/boot/entry64.s b/arch/x86_64/src/boot/entry64.s index 110ced8..c5df5db 100644 --- a/arch/x86_64/src/boot/entry64.s +++ b/arch/x86_64/src/boot/entry64.s @@ -10,10 +10,20 @@ _entry64: mov %rax, %fs mov %rax, %gs - xor %rax, %rax - call invoke_global_constructors + xor %rax, %rax + mov %rax, %rbp + mov %rax, %rdx + mov %rax, %rsi + + mov $stack_size, %rcx + shr $3, %rcx + lea (stack_bottom), %rdi + rep stosq + + mov %rax, %rdi + call main 1: -- cgit v1.2.3 From 1a3c20cc9ea191a862eb7e8ac55b3a69ac74ad5e Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 24 Nov 2025 16:59:24 +0100 Subject: x86_64/vga: rely less on magic state --- arch/x86_64/src/boot/entry64.s | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'arch/x86_64/src/boot/entry64.s') diff --git a/arch/x86_64/src/boot/entry64.s b/arch/x86_64/src/boot/entry64.s index c5df5db..636e4cd 100644 --- a/arch/x86_64/src/boot/entry64.s +++ b/arch/x86_64/src/boot/entry64.s @@ -1,3 +1,16 @@ +.section .bss, "aw", @nobits + +//! A structure containing information gathered during the bootstrap process. +//! Expected layout (as described by teachos::boot::information): +//! +//! struct +//! { +//! multiboot2::information_view const * mbi; +//! std::size_t vga_buffer_index; +//! } +.global bootstrap_information +bootstrap_information: .skip 16 + .section .boot_text, "ax", @progbits .code64 @@ -10,6 +23,12 @@ _entry64: mov %rax, %fs mov %rax, %gs + mov multiboot_information_pointer, %rax + mov vga_buffer_pointer, %rdx + sub $0xb8000, %rdx + mov %rax, (bootstrap_information) + mov %rdx, (bootstrap_information + 8) + call invoke_global_constructors xor %rax, %rax -- cgit v1.2.3 From a08847ded5fba25859e7a3ad06ae3fed342d4d6a Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 3 Dec 2025 14:02:07 +0100 Subject: x86_64/boot: move stack to higher half --- arch/x86_64/src/boot/entry64.s | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'arch/x86_64/src/boot/entry64.s') diff --git a/arch/x86_64/src/boot/entry64.s b/arch/x86_64/src/boot/entry64.s index 636e4cd..2932354 100644 --- a/arch/x86_64/src/boot/entry64.s +++ b/arch/x86_64/src/boot/entry64.s @@ -11,6 +11,12 @@ .global bootstrap_information bootstrap_information: .skip 16 +.align 16 +.global stack_top +stack_bottom: .skip 1 << 20 +stack_top: +stack_size = stack_top - stack_bottom + .section .boot_text, "ax", @progbits .code64 @@ -23,6 +29,9 @@ _entry64: mov %rax, %fs mov %rax, %gs + mov $stack_top, %rsp + mov %rsp, %rbp + mov multiboot_information_pointer, %rax mov vga_buffer_pointer, %rdx sub $0xb8000, %rdx -- cgit v1.2.3 From 40804526a58ddf2cc0df0750550c8dcfa7b7c57c Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 15 Dec 2025 11:34:30 +0100 Subject: x86_64/boot: use high-mem address of MBI --- arch/x86_64/src/boot/entry64.s | 1 + 1 file changed, 1 insertion(+) (limited to 'arch/x86_64/src/boot/entry64.s') diff --git a/arch/x86_64/src/boot/entry64.s b/arch/x86_64/src/boot/entry64.s index 2932354..657b0a8 100644 --- a/arch/x86_64/src/boot/entry64.s +++ b/arch/x86_64/src/boot/entry64.s @@ -33,6 +33,7 @@ _entry64: mov %rsp, %rbp mov multiboot_information_pointer, %rax + or $TEACHOS_VMA, %rax mov vga_buffer_pointer, %rdx sub $0xb8000, %rdx mov %rax, (bootstrap_information) -- cgit v1.2.3