Skip to main content

kernel/arch/x86_64/boot/
mod.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! Arch-Specific Boot Code including linker symbols, early kernel init and limine requests
3//!
4//! Authors: MarioS271
5
6mod entry;
7pub(crate) mod mm;
8
9// TODO: add CR4 init
10
11/// Initialize x86_64 specific stuff
12///
13/// # Safety
14/// The caller must ensure the following:
15/// - This function is called exactly once
16/// - No SMP/threading is currently active
17/// - No jump to userspace has been made yet
18pub unsafe fn init(boot_info: &crate::lib::types::boot_info::BootInfo) {
19    use crate::state::kstate::KSTATE;
20
21    let cpu = &KSTATE.cpu;
22    let gdt_setup_info;
23
24    // Safety:
25    // - init_tss is called before init_gdt
26    // - We're on the BSP and TSS, GDT and GS Info are all for the BSP CPU
27    // The rest is guaranteed by this function's safety contract
28    unsafe {
29        cpu.bsp_cpu_state().init_tss();
30        gdt_setup_info = cpu.bsp_cpu_state().init_gdt();
31        cpu.global_cpu_state().init_idt();
32        cpu.bsp_cpu_state().init_gs_info();
33    }
34    cpu.global_cpu_state().set_user_selectors(gdt_setup_info.user_code.0, gdt_setup_info.user_data.0);
35
36    super::interrupts::pic::init();
37
38    mm::mm_init(&boot_info.kernel_sections);
39
40    // Safety: GDT was correctly initialized above, the rest is guaranteed by this function's
41    // safety contract
42    unsafe {
43        super::syscall::init(&gdt_setup_info);
44    }
45}