ostd/arch/x86/boot/multiboot2/
mod.rs

1// SPDX-License-Identifier: MPL-2.0
2
3use core::arch::global_asm;
4
5use multiboot2::{BootInformation, BootInformationHeader, MemoryAreaType};
6
7use crate::{
8    boot::{
9        BootloaderAcpiArg, BootloaderFramebufferArg,
10        memory_region::{MemoryRegion, MemoryRegionArray, MemoryRegionType},
11    },
12    mm::{Paddr, kspace::paddr_to_vaddr},
13};
14
15global_asm!(include_str!("header.S"));
16
17fn parse_bootloader_name(mb2_info: &BootInformation) -> Option<&'static str> {
18    let name = mb2_info.boot_loader_name_tag()?.name().ok()?;
19
20    // SAFETY: The address of `name` is physical and the bootloader name will live for `'static`.
21    Some(unsafe { make_str_vaddr_static(name) })
22}
23
24fn parse_kernel_commandline(mb2_info: &BootInformation) -> Option<&'static str> {
25    let cmdline = mb2_info.command_line_tag()?.cmdline().ok()?;
26
27    // SAFETY: The address of `cmdline` is physical and the command line will live for `'static`.
28    Some(unsafe { make_str_vaddr_static(cmdline) })
29}
30
31unsafe fn make_str_vaddr_static(str: &str) -> &'static str {
32    let vaddr = paddr_to_vaddr(str.as_ptr() as Paddr);
33
34    // SAFETY: The safety is upheld by the caller.
35    let bytes = unsafe { core::slice::from_raw_parts(vaddr as *const u8, str.len()) };
36
37    core::str::from_utf8(bytes).unwrap()
38}
39
40fn parse_initramfs(mb2_info: &BootInformation) -> Option<&'static [u8]> {
41    let module_tag = mb2_info.module_tags().next()?;
42
43    let initramfs_ptr = paddr_to_vaddr(module_tag.start_address() as usize);
44    let initramfs_len = module_tag.module_size() as usize;
45    // SAFETY: The initramfs is safe to read because of the contract with the loader.
46    let initramfs =
47        unsafe { core::slice::from_raw_parts(initramfs_ptr as *const u8, initramfs_len) };
48
49    Some(initramfs)
50}
51
52fn parse_acpi_arg(mb2_info: &BootInformation) -> BootloaderAcpiArg {
53    if let Some(v2_tag) = mb2_info.rsdp_v2_tag() {
54        // Check for RSDP v2
55        BootloaderAcpiArg::Xsdt(v2_tag.xsdt_address())
56    } else if let Some(v1_tag) = mb2_info.rsdp_v1_tag() {
57        // Fall back to RSDP v1
58        BootloaderAcpiArg::Rsdt(v1_tag.rsdt_address())
59    } else {
60        BootloaderAcpiArg::NotProvided
61    }
62}
63
64fn parse_framebuffer_info(mb2_info: &BootInformation) -> Option<BootloaderFramebufferArg> {
65    let fb_tag = mb2_info.framebuffer_tag()?.ok()?;
66
67    Some(BootloaderFramebufferArg {
68        address: fb_tag.address() as usize,
69        width: fb_tag.width() as usize,
70        height: fb_tag.height() as usize,
71        bpp: fb_tag.bpp() as usize,
72    })
73}
74
75impl From<MemoryAreaType> for MemoryRegionType {
76    fn from(value: MemoryAreaType) -> Self {
77        match value {
78            MemoryAreaType::Available => Self::Usable,
79            MemoryAreaType::Reserved => Self::Reserved,
80            MemoryAreaType::AcpiAvailable => Self::Reclaimable,
81            MemoryAreaType::ReservedHibernate => Self::NonVolatileSleep,
82            MemoryAreaType::Defective => Self::BadMemory,
83            MemoryAreaType::Custom(_) => Self::Reserved,
84        }
85    }
86}
87
88fn parse_memory_regions(mb2_info: &BootInformation) -> MemoryRegionArray {
89    let mut regions = MemoryRegionArray::new();
90
91    // Add the regions returned by Grub.
92    let memory_regions_tag = mb2_info
93        .memory_map_tag()
94        .expect("No memory regions are found in the Multiboot2 header!");
95    for region in memory_regions_tag.memory_areas() {
96        let start = region.start_address();
97        let end = region.end_address();
98        let area_typ: MemoryRegionType = MemoryAreaType::from(region.typ()).into();
99        let region = MemoryRegion::new(
100            start.try_into().unwrap(),
101            (end - start).try_into().unwrap(),
102            area_typ,
103        );
104        regions.push(region).unwrap();
105    }
106
107    // Add the framebuffer region since Grub does not specify it.
108    if let Some(fb) = parse_framebuffer_info(mb2_info) {
109        regions.push(MemoryRegion::framebuffer(&fb)).unwrap();
110    }
111
112    // Add the kernel region since Grub does not specify it.
113    regions.push(MemoryRegion::kernel()).unwrap();
114
115    // Add the initramfs region.
116    if let Some(initramfs) = parse_initramfs(mb2_info) {
117        regions.push(MemoryRegion::module(initramfs)).unwrap();
118    }
119
120    // Add the AP boot code region that will be copied into by the BSP.
121    regions
122        .push(super::smp::reclaimable_memory_region())
123        .unwrap();
124
125    // Add the kernel cmdline and boot loader name region since Grub does not specify it.
126    if let Some(kcmdline) = parse_kernel_commandline(mb2_info) {
127        regions
128            .push(MemoryRegion::module(kcmdline.as_bytes()))
129            .unwrap();
130    }
131    if let Some(bootloader_name) = parse_bootloader_name(mb2_info) {
132        regions
133            .push(MemoryRegion::module(bootloader_name.as_bytes()))
134            .unwrap();
135    }
136
137    regions.into_non_overlapping()
138}
139
140/// The entry point of the Rust code portion of Asterinas (with multiboot2 parameters).
141///
142/// # Safety
143///
144/// - This function must be called only once at a proper timing in the BSP's boot assembly code.
145/// - The caller must follow C calling conventions and put the right arguments in registers.
146// SAFETY: The name does not collide with other symbols.
147#[unsafe(no_mangle)]
148unsafe extern "sysv64" fn __multiboot2_entry(boot_magic: u32, boot_params: u64) -> ! {
149    assert_eq!(boot_magic, multiboot2::MAGIC);
150    let mb2_info =
151        unsafe { BootInformation::load(boot_params as *const BootInformationHeader).unwrap() };
152
153    use crate::boot::{EARLY_INFO, EarlyBootInfo, call_ostd_main};
154
155    EARLY_INFO.call_once(|| EarlyBootInfo {
156        bootloader_name: parse_bootloader_name(&mb2_info).unwrap_or("Unknown Multiboot2 Loader"),
157        kernel_cmdline: parse_kernel_commandline(&mb2_info).unwrap_or(""),
158        initramfs: parse_initramfs(&mb2_info),
159        acpi_arg: parse_acpi_arg(&mb2_info),
160        framebuffer_arg: parse_framebuffer_info(&mb2_info),
161        memory_regions: parse_memory_regions(&mb2_info),
162    });
163
164    call_ostd_main();
165}