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

1// SPDX-License-Identifier: MPL-2.0
2
3//! The Linux 64-bit Boot Protocol supporting module.
4//!
5
6use linux_boot_params::{BootParams, E820Type, LINUX_BOOT_HEADER_MAGIC};
7
8#[cfg(feature = "cvm_guest")]
9use crate::arch::init_cvm_guest;
10use crate::{
11    arch::if_tdx_enabled,
12    boot::{
13        BootloaderAcpiArg, BootloaderFramebufferArg,
14        memory_region::{MemoryRegion, MemoryRegionArray, MemoryRegionType},
15    },
16    mm::kspace::paddr_to_vaddr,
17};
18
19fn parse_bootloader_name(boot_params: &BootParams) -> &str {
20    // The bootloaders have assigned IDs in Linux, see
21    // https://www.kernel.org/doc/Documentation/x86/boot.txt
22    // for details.
23    match boot_params.hdr.type_of_loader {
24        0x0 => "LILO", // (0x00 reserved for pre-2.00 bootloader)
25        0x1 => "Loadlin",
26        0x2 => "bootsect-loader", // (0x20, all other values reserved)
27        0x3 => "Syslinux",
28        0x4 => "Etherboot/gPXE/iPXE",
29        0x5 => "ELILO",
30        0x7 => "GRUB",
31        0x8 => "U-Boot",
32        0x9 => "Xen",
33        0xA => "Gujin",
34        0xB => "Qemu",
35        0xC => "Arcturus Networks uCbootloader",
36        0xD => "kexec-tools",
37        0xE => "Extended loader",
38        0xF => "Special", // (0xFF = undefined)
39        0x10 => "Reserved",
40        0x11 => "Minimal Linux Bootloader <http://sebastian-plotz.blogspot.de>",
41        0x12 => "OVMF UEFI virtualization stack",
42        _ => "Unknown Linux Loader",
43    }
44}
45
46fn parse_kernel_commandline(boot_params: &BootParams) -> Option<&str> {
47    if boot_params.ext_cmd_line_ptr != 0 {
48        // TODO: We can support the above 4GiB command line after setting up
49        // linear mappings. By far, we cannot log the error because the serial is
50        // not up. Proceed as if there was no command line.
51        return None;
52    }
53
54    if boot_params.hdr.cmd_line_ptr == 0 || boot_params.hdr.cmdline_size == 0 {
55        return None;
56    }
57
58    let cmdline_ptr = paddr_to_vaddr(boot_params.hdr.cmd_line_ptr as usize);
59    let cmdline_len = boot_params.hdr.cmdline_size as usize;
60    // SAFETY: The command line is safe to read because of the contract with the loader.
61    let cmdline = unsafe { core::slice::from_raw_parts(cmdline_ptr as *const u8, cmdline_len) };
62
63    // Now, unfortunately, there are silent errors because the serial is not up.
64    core::ffi::CStr::from_bytes_until_nul(cmdline)
65        .ok()?
66        .to_str()
67        .ok()
68}
69
70fn parse_initramfs(boot_params: &BootParams) -> Option<&[u8]> {
71    if boot_params.ext_ramdisk_image != 0 || boot_params.ext_ramdisk_size != 0 {
72        // See the explanation in `parse_kernel_commandline`.
73        return None;
74    }
75
76    if boot_params.hdr.ramdisk_image == 0 || boot_params.hdr.ramdisk_size == 0 {
77        return None;
78    }
79
80    let initramfs_ptr = paddr_to_vaddr(boot_params.hdr.ramdisk_image as usize);
81    let initramfs_len = boot_params.hdr.ramdisk_size as usize;
82    // SAFETY: The initramfs is safe to read because of the contract with the loader.
83    let initramfs =
84        unsafe { core::slice::from_raw_parts(initramfs_ptr as *const u8, initramfs_len) };
85
86    Some(initramfs)
87}
88
89fn parse_acpi_arg(boot_params: &BootParams) -> BootloaderAcpiArg {
90    let rsdp = boot_params.acpi_rsdp_addr;
91
92    if rsdp == 0 {
93        BootloaderAcpiArg::NotProvided
94    } else {
95        BootloaderAcpiArg::Rsdp(rsdp.try_into().expect("RSDP address overflowed!"))
96    }
97}
98
99fn parse_framebuffer_info(boot_params: &BootParams) -> Option<BootloaderFramebufferArg> {
100    let screen_info = boot_params.screen_info;
101
102    let address = screen_info.lfb_base as usize | ((screen_info.ext_lfb_base as usize) << 32);
103    if address == 0 {
104        return None;
105    }
106
107    Some(BootloaderFramebufferArg {
108        address,
109        width: screen_info.lfb_width as usize,
110        height: screen_info.lfb_height as usize,
111        bpp: screen_info.lfb_depth as usize,
112    })
113}
114
115impl From<E820Type> for MemoryRegionType {
116    fn from(value: E820Type) -> Self {
117        match value {
118            E820Type::Ram => Self::Usable,
119            E820Type::Reserved => Self::Reserved,
120            E820Type::Acpi => Self::Reclaimable,
121            E820Type::Nvs => Self::NonVolatileSleep,
122            E820Type::Unusable => Self::BadMemory,
123            // All other memory regions are reserved.
124            // FIXME: Using Rust enum in this way can be unsound if the bootloader passes an
125            // unknown memory type to the kernel (e.g., due to a newer protocol version).
126            _ => Self::Reserved,
127        }
128    }
129}
130
131fn parse_memory_regions(boot_params: &BootParams) -> MemoryRegionArray {
132    let mut regions = MemoryRegionArray::new();
133
134    // Add regions from E820.
135    let num_entries = boot_params.e820_entries as usize;
136    for e820_entry in &boot_params.e820_table[0..num_entries] {
137        regions
138            .push(MemoryRegion::new(
139                e820_entry.addr.try_into().unwrap(),
140                e820_entry.size.try_into().unwrap(),
141                e820_entry.typ.into(),
142            ))
143            .unwrap();
144    }
145
146    // Add the framebuffer region.
147    if let Some(fb) = parse_framebuffer_info(boot_params) {
148        regions.push(MemoryRegion::framebuffer(&fb)).unwrap();
149    }
150
151    // Add the kernel region.
152    regions.push(MemoryRegion::kernel()).unwrap();
153
154    // Add the initramfs region.
155    if let Some(initramfs) = parse_initramfs(boot_params) {
156        regions.push(MemoryRegion::module(initramfs)).unwrap();
157    }
158
159    // Add the AP boot code region that will be copied into by the BSP.
160    regions
161        .push(super::smp::reclaimable_memory_region())
162        .unwrap();
163
164    // Add the region of the kernel cmdline since some bootloaders do not provide it.
165    if let Some(kcmdline) = parse_kernel_commandline(boot_params) {
166        regions
167            .push(MemoryRegion::module(kcmdline.as_bytes()))
168            .unwrap();
169    }
170
171    // FIXME: Early versions of TDVF did not correctly report the location of AP's page tables as
172    // EfiACPIMemoryNVS. We need to manually reserve this memory region to prevent them from being
173    // corrupted. TDVF has now been upstreamed to OVMF, and this issue has been fixed in OVMF
174    // stable-202411 or later. See the commit for details:
175    // <https://github.com/tianocore/edk2/commit/383f729ac096b8deb279933fce86e83a5f7f5ec7>.
176    if_tdx_enabled!({
177        // The definition of these constants can be found in:
178        // <https://github.com/tianocore/edk2/blob/a7ab45ace25c4b987994158687d04de07ed20a96/OvmfPkg/IntelTdx/IntelTdxX64.fdf#L64-L71>
179        // <https://github.com/tianocore/edk2/blob/a7ab45ace25c4b987994158687d04de07ed20a96/OvmfPkg/Include/Fdf/OvmfPkgDefines.fdf.inc#L106>
180        regions
181            .push(MemoryRegion::new(
182                // PcdOvmfSecPageTablesBase = $(MEMFD_BASE_ADDRESS) + 0x000000 = 0x800000
183                0x800000,
184                // PcdOvmfSecPageTablesSize = 0x006000
185                0x006000,
186                // EfiACPIMemoryNVS
187                MemoryRegionType::NonVolatileSleep,
188            ))
189            .unwrap();
190    });
191
192    regions.into_non_overlapping()
193}
194
195/// The entry point of the Rust code portion of Asterinas (with Linux boot parameters).
196///
197/// # Safety
198///
199/// - This function must be called only once at a proper timing in the BSP's boot assembly code.
200/// - The caller must follow C calling conventions and put the right arguments in registers.
201// SAFETY: The name does not collide with other symbols.
202#[unsafe(no_mangle)]
203unsafe extern "sysv64" fn __linux_boot(params_ptr: *const BootParams) -> ! {
204    let params = unsafe { &*params_ptr };
205    assert_eq!({ params.hdr.header }, LINUX_BOOT_HEADER_MAGIC);
206
207    use crate::boot::{EARLY_INFO, EarlyBootInfo, call_ostd_main};
208
209    #[cfg(feature = "cvm_guest")]
210    init_cvm_guest();
211
212    EARLY_INFO.call_once(|| EarlyBootInfo {
213        bootloader_name: parse_bootloader_name(params),
214        kernel_cmdline: parse_kernel_commandline(params).unwrap_or(""),
215        initramfs: parse_initramfs(params),
216        acpi_arg: parse_acpi_arg(params),
217        framebuffer_arg: parse_framebuffer_info(params),
218        memory_regions: parse_memory_regions(params),
219    });
220
221    call_ostd_main();
222}