diff --git a/src/arch/x86_64/mod.rs b/src/arch/x86_64/mod.rs index a1fb1ee0..41d227ab 100644 --- a/src/arch/x86_64/mod.rs +++ b/src/arch/x86_64/mod.rs @@ -6,6 +6,8 @@ mod paging; #[cfg(target_os = "none")] mod physicalmem; mod platform; +#[cfg(target_os = "none")] +mod stack; pub use console::Console; diff --git a/src/arch/x86_64/platform/linux/entry.s b/src/arch/x86_64/platform/linux/entry.s index 7b6f6775..7e2c74e6 100644 --- a/src/arch/x86_64/platform/linux/entry.s +++ b/src/arch/x86_64/platform/linux/entry.s @@ -3,8 +3,6 @@ .code64 -.set BOOT_STACK_SIZE, 4096 - .extern loader_start # defined in linker script .extern loader_end @@ -16,8 +14,8 @@ _start: cli # avoid any interrupt # Initialize stack pointer - movabs rsp, OFFSET boot_stack - add rsp, BOOT_STACK_SIZE - 16 + movabs rsp, OFFSET {stack} + add rsp, {stack_top_offset} mov [boot_params], rsi @@ -67,8 +65,8 @@ start64: mov gs, ax cld # set default stack pointer - movabs rsp, OFFSET boot_stack - add rsp, BOOT_STACK_SIZE-16 + movabs rsp, OFFSET {stack} + add rsp, {stack_top_offset} # jump to the boot processors's C code jmp {loader_main} @@ -81,11 +79,6 @@ invalid: boot_params: .8byte 0 -.align 4096 -.global boot_stack -boot_stack: - .fill BOOT_STACK_SIZE, 1, 0xcd - # Bootstrap page tables are used during the initialization. .align 4096 boot_pml4: diff --git a/src/arch/x86_64/platform/linux/mod.rs b/src/arch/x86_64/platform/linux/mod.rs index ea805240..9f910c7c 100644 --- a/src/arch/x86_64/platform/linux/mod.rs +++ b/src/arch/x86_64/platform/linux/mod.rs @@ -26,6 +26,8 @@ mod entry { core::arch::global_asm!( include_str!("entry.s"), loader_main = sym crate::os::loader_main, + stack = sym crate::arch::x86_64::stack::STACK, + stack_top_offset = const crate::arch::x86_64::stack::Stack::top_offset(), gdt_ptr = sym crate::arch::x86_64::gdt::GDT_PTR, kernel_code_selector = const crate::arch::x86_64::gdt::Gdt::kernel_code_selector().0, kernel_data_selector = const crate::arch::x86_64::gdt::Gdt::kernel_data_selector().0, diff --git a/src/arch/x86_64/platform/multiboot/entry.s b/src/arch/x86_64/platform/multiboot/entry.s index 8935c8b6..c9d7f59e 100644 --- a/src/arch/x86_64/platform/multiboot/entry.s +++ b/src/arch/x86_64/platform/multiboot/entry.s @@ -5,8 +5,6 @@ .code32 -.set BOOT_STACK_SIZE, 4096 - .extern loader_start # defined in linker script .extern loader_end @@ -37,8 +35,8 @@ _start: cli # avoid any interrupt # Initialize stack pointer - mov esp, OFFSET boot_stack - add esp, BOOT_STACK_SIZE - 16 + mov esp, OFFSET {stack} + add esp, {stack_top_offset} # Interpret multiboot information mov [mb_info], ebx @@ -155,8 +153,8 @@ start64: mov gs, eax cld # set default stack pointer - movabs rsp, OFFSET boot_stack - add rsp, BOOT_STACK_SIZE-16 + movabs rsp, OFFSET {stack} + add rsp, {stack_top_offset} # jump to the boot processors's C code jmp {loader_main} @@ -168,11 +166,6 @@ start64: mb_info: .8byte 0 -.align 4096 -.global boot_stack -boot_stack: - .fill BOOT_STACK_SIZE, 1, 0xcd - # Bootstrap page tables are used during the initialization. .align 4096 boot_pml4: diff --git a/src/arch/x86_64/platform/multiboot/mod.rs b/src/arch/x86_64/platform/multiboot/mod.rs index d0262552..4fa8237d 100644 --- a/src/arch/x86_64/platform/multiboot/mod.rs +++ b/src/arch/x86_64/platform/multiboot/mod.rs @@ -27,6 +27,8 @@ mod entry { core::arch::global_asm!( include_str!("entry.s"), loader_main = sym crate::os::loader_main, + stack = sym crate::arch::x86_64::stack::STACK, + stack_top_offset = const crate::arch::x86_64::stack::Stack::top_offset(), gdt_ptr = sym crate::arch::x86_64::gdt::GDT_PTR, kernel_code_selector = const crate::arch::x86_64::gdt::Gdt::kernel_code_selector().0, kernel_data_selector = const crate::arch::x86_64::gdt::Gdt::kernel_data_selector().0, diff --git a/src/arch/x86_64/stack.rs b/src/arch/x86_64/stack.rs new file mode 100644 index 00000000..06481498 --- /dev/null +++ b/src/arch/x86_64/stack.rs @@ -0,0 +1,24 @@ +use core::cell::UnsafeCell; +use core::mem::{self, MaybeUninit}; + +#[repr(C, align(0x1000))] +pub struct Stack(UnsafeCell<[MaybeUninit; 0x1000]>); + +unsafe impl Sync for Stack {} + +impl Stack { + const fn new() -> Self { + let fill = if cfg!(debug_assertions) { + MaybeUninit::new(0xcd) + } else { + MaybeUninit::uninit() + }; + Self(UnsafeCell::new([fill; _])) + } + + pub const fn top_offset() -> u16 { + mem::size_of::() as u16 - 0x10 + } +} + +pub static STACK: Stack = Stack::new();