Skip to main content

openhcl_boot/
main.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! The openhcl boot loader, which loads before the kernel to set up the
5//! kernel's boot parameters.
6
7// See build.rs.
8#![cfg_attr(minimal_rt, no_std, no_main)]
9// UNSAFETY: Interacting with low level hardware and bootloader primitives.
10#![expect(unsafe_code)]
11// Allow the allocator api when compiling with `RUSTFLAGS="--cfg nightly"`. This
12// is used for some miri tests for testing the bump allocator.
13//
14// Do not use a normal feature, as that shows errors with rust-analyzer since
15// most people are using stable and enable all features. We could remove this
16// once the allocator_api feature is stable.
17#![cfg_attr(nightly, feature(allocator_api))]
18
19mod arch;
20mod boot_logger;
21mod cmdline;
22mod dt;
23mod host_params;
24mod hypercall;
25mod memory;
26mod rt;
27mod sidecar;
28mod single_threaded;
29
30use crate::arch::setup_vtl2_memory;
31use crate::arch::setup_vtl2_vp;
32#[cfg(target_arch = "x86_64")]
33use crate::arch::tdx::get_tdx_tsc_reftime;
34use crate::arch::verify_imported_regions_hash;
35use crate::boot_logger::boot_logger_memory_init;
36use crate::boot_logger::boot_logger_runtime_init;
37use crate::hypercall::hvcall;
38use crate::memory::AddressSpaceManager;
39use crate::single_threaded::OffStackRef;
40use crate::single_threaded::off_stack;
41use arrayvec::ArrayString;
42use arrayvec::ArrayVec;
43use cmdline::BootCommandLineOptions;
44use core::fmt::Write;
45use dt::BootTimes;
46use dt::write_dt;
47use host_fdt_parser::ComInfo;
48use host_params::COMMAND_LINE_SIZE;
49use host_params::PartitionInfo;
50use host_params::shim_params::IsolationType;
51use host_params::shim_params::ShimParams;
52use hvdef::Vtl;
53use loader_defs::linux::SETUP_DTB;
54use loader_defs::linux::setup_data;
55use loader_defs::shim::ShimParamsRaw;
56use memory_range::RangeWalkResult;
57use memory_range::walk_ranges;
58use minimal_rt::enlightened_panic::enable_enlightened_panic;
59use sidecar::SidecarConfig;
60use sidecar_defs::SidecarOutput;
61use sidecar_defs::SidecarParams;
62use zerocopy::FromBytes;
63use zerocopy::FromZeros;
64use zerocopy::Immutable;
65use zerocopy::IntoBytes;
66use zerocopy::KnownLayout;
67
68#[derive(Debug)]
69struct CommandLineTooLong;
70
71impl From<core::fmt::Error> for CommandLineTooLong {
72    fn from(_: core::fmt::Error) -> Self {
73        Self
74    }
75}
76
77struct BuildKernelCommandLineParams<'a> {
78    params: &'a ShimParams,
79    cmdline: &'a mut ArrayString<COMMAND_LINE_SIZE>,
80    partition_info: &'a PartitionInfo,
81    can_trust_host: bool,
82    is_confidential_debug: bool,
83    sidecar: Option<&'a SidecarConfig<'a>>,
84    vtl2_pool_supported: bool,
85}
86
87/// Read and setup the underhill kernel command line into the specified buffer.
88fn build_kernel_command_line(
89    fn_params: BuildKernelCommandLineParams<'_>,
90) -> Result<(), CommandLineTooLong> {
91    let BuildKernelCommandLineParams {
92        params,
93        cmdline,
94        partition_info,
95        can_trust_host,
96        is_confidential_debug,
97        sidecar,
98        vtl2_pool_supported,
99    } = fn_params;
100
101    // For reference:
102    // https://www.kernel.org/doc/html/v5.15/admin-guide/kernel-parameters.html
103    const KERNEL_PARAMETERS: &[&str] = &[
104        // If a console is specified, then write everything to it.
105        "loglevel=8",
106        // Use a fixed 128KB log buffer by default.
107        "log_buf_len=128K",
108        // Enable time output on console for ohcldiag-dev.
109        "printk.time=1",
110        // Enable facility and level output on console for ohcldiag-dev.
111        "console_msg_format=syslog",
112        // Set uio parameter to configure vmbus ring buffer behavior.
113        "uio_hv_generic.no_mask=1",
114        // RELIABILITY: Dump anonymous pages and ELF headers only. Skip over
115        // huge pages and the shared pages.
116        "coredump_filter=0x33",
117        // PERF: No processor frequency governing.
118        "cpufreq.off=1",
119        // PERF: Disable the CPU idle time management entirely. It does not
120        // prevent the idle loop from running on idle CPUs, but it prevents
121        // the CPU idle time governors and drivers from being invoked.
122        "cpuidle.off=1",
123        // PERF: No perf checks for crypto algorithms to boot faster.
124        // Would have to evaluate the perf wins on the crypto manager vs
125        // delaying the boot up.
126        "cryptomgr.notests",
127        // PERF: Idle threads use HLT on x64 if there is no work.
128        // Believed to be a compromise between waking up the processor
129        // and the power consumption.
130        "idle=halt",
131        // WORKAROUND: Avoid init calls that assume presence of CMOS (Simple
132        // Boot Flag) or allocate the real-mode trampoline for APs.
133        "initcall_blacklist=init_real_mode,sbf_init",
134        // CONFIG-STATIC, PERF: Static loops-per-jiffy value to save time on boot.
135        "lpj=3000000",
136        // PERF: No broken timer check to boot faster.
137        "no_timer_check",
138        // CONFIG-STATIC, PERF: Using xsave makes VTL transitions being
139        // much slower. The xsave state is shared between VTLs, and we don't
140        // context switch it in the kernel when leaving/entering VTL2.
141        // Removing this will lead to corrupting register state and the
142        // undefined behaviour.
143        "noxsave",
144        // RELIABILITY: Panic on MCEs and faults in the kernel.
145        "oops=panic",
146        // RELIABILITY: Don't panic on kernel warnings.
147        "panic_on_warn=0",
148        // PERF, RELIABILITY: Don't print detailed information about the failing
149        // processes (memory maps, threads).
150        "panic_print=0",
151        // RELIABILITY: Reboot immediately on panic, no timeout.
152        "panic=-1",
153        // RELIABILITY: Don't print processor context information on a fatal
154        // signal. Our crash dump collection infrastructure seems reliable, and
155        // this information doesn't seem useful without a dump anyways.
156        // Additionally it may push important logs off the end of the kmsg
157        // page logged by the host.
158        //"print_fatal_signals=0",
159        // RELIABILITY: Unlimited logging to /dev/kmsg from userspace.
160        "printk.devkmsg=on",
161        // RELIABILITY: Reboot using a triple fault as the fastest method.
162        // That is also the method used for compatibility with earlier versions
163        // of the Microsoft HCL.
164        "reboot=t",
165        // CONFIG-STATIC: Type of the root file system.
166        "rootfstype=tmpfs",
167        // PERF: Deactivate kcompactd kernel thread, otherwise it will queue a
168        // scheduler timer periodically, which introduces jitters for VTL0.
169        "sysctl.vm.compaction_proactiveness=0",
170        // PERF: No TSC stability check when booting up to boot faster,
171        // also no validation during runtime.
172        "tsc=reliable",
173        // RELIABILITY: Panic on receiving an NMI.
174        "unknown_nmi_panic=1",
175        // Use vfio for MANA devices.
176        "vfio_pci.ids=1414:00ba",
177        // WORKAROUND: Enable no-IOMMU mode. This mode provides no device isolation,
178        // and no DMA translation.
179        "vfio.enable_unsafe_noiommu_mode=1",
180        // Specify the init path.
181        "rdinit=/underhill-init",
182        // Default to user-mode NVMe driver.
183        "OPENHCL_NVME_VFIO=1",
184        // The next three items reduce the memory overhead of the storvsc driver.
185        // Since it is only used for DVD, performance is not critical.
186        "hv_storvsc.storvsc_vcpus_per_sub_channel=2048",
187        // Fix number of hardware queues at 2.
188        "hv_storvsc.storvsc_max_hw_queues=2",
189        // Reduce the ring buffer size to 32K.
190        "hv_storvsc.storvsc_ringbuffer_size=0x8000",
191        // Disable eager mimalloc commit to prevent core dumps from being overly large
192        "MIMALLOC_ARENA_EAGER_COMMIT=0",
193        // Disable acpi runtime support. Unused in underhill, but some support
194        // is compiled in for the kernel (ie TDX mailbox protocol).
195        "acpi=off",
196    ];
197
198    const X86_KERNEL_PARAMETERS: &[&str] = &[
199        // Disable all attempts to use an IOMMU, including swiotlb.
200        "iommu=off",
201        // Don't probe for a PCI bus. PCI devices currently come from VPCI. When
202        // this changes, we will explicitly enumerate a PCI bus via devicetree.
203        "pci=off",
204    ];
205
206    const AARCH64_KERNEL_PARAMETERS: &[&str] = &[];
207
208    for p in KERNEL_PARAMETERS {
209        write!(cmdline, "{p} ")?;
210    }
211
212    let arch_parameters = if cfg!(target_arch = "x86_64") {
213        X86_KERNEL_PARAMETERS
214    } else {
215        AARCH64_KERNEL_PARAMETERS
216    };
217    for p in arch_parameters {
218        write!(cmdline, "{p} ")?;
219    }
220
221    const HARDWARE_ISOLATED_KERNEL_PARAMETERS: &[&str] = &[
222        // Even with iommu=off, the SWIOTLB is still allocated on AARCH64
223        // (iommu=off ignored entirely), and CVMs (memory encryption forces it
224        // on). Set it to a single area in 8MB. The first parameter controls the
225        // area size in slabs (2KB per slab), the second controls the number of
226        // areas (default is # of CPUs).
227        //
228        // This is set to 8MB on hardware isolated VMs since there are some
229        // scenarios, such as provisioning over DVD, which require a larger size
230        // since the buffer is being used.
231        "swiotlb=4096,1",
232    ];
233
234    const NON_HARDWARE_ISOLATED_KERNEL_PARAMETERS: &[&str] = &[
235        // Even with iommu=off, the SWIOTLB is still allocated on AARCH64
236        // (iommu=off ignored entirely). Set it to the minimum, saving ~63 MiB.
237        // The first parameter controls the area size, the second controls the
238        // number of areas (default is # of CPUs). Set them both to the minimum.
239        "swiotlb=1,1",
240    ];
241
242    if params.isolation_type.is_hardware_isolated() {
243        for p in HARDWARE_ISOLATED_KERNEL_PARAMETERS {
244            write!(cmdline, "{p} ")?;
245        }
246    } else {
247        for p in NON_HARDWARE_ISOLATED_KERNEL_PARAMETERS {
248            write!(cmdline, "{p} ")?;
249        }
250    }
251
252    // Enable the com3 console by default if it's available and we're not
253    // isolated, or if we are isolated but also have debugging enabled.
254    //
255    // Otherwise, set the console to ttynull so the kernel does not default to
256    // com1. This is overridden by any user customizations in the static or
257    // dynamic command line, as this console argument provided by the bootloader
258    // comes first.
259    write!(cmdline, "console=")?;
260    match (&partition_info.com3_serial, can_trust_host) {
261        (ComInfo::Ns16550 { current_speed, .. }, true) => {
262            write!(cmdline, "ttyS2,{current_speed} ")?
263        }
264        (ComInfo::Pl011 { current_speed, .. }, true) => {
265            write!(cmdline, "ttyAMA0,{current_speed} ")?
266        }
267        _ => write!(cmdline, "ttynull ")?,
268    }
269
270    if params.isolation_type != IsolationType::None {
271        write!(
272            cmdline,
273            "{}=1 ",
274            underhill_confidentiality::OPENHCL_CONFIDENTIAL_ENV_VAR_NAME
275        )?;
276    }
277
278    if is_confidential_debug {
279        write!(
280            cmdline,
281            "{}=1 ",
282            underhill_confidentiality::OPENHCL_CONFIDENTIAL_DEBUG_ENV_VAR_NAME
283        )?;
284    }
285
286    // Generate the NVMe keep alive command line which should look something
287    // like: OPENHCL_NVME_KEEP_ALIVE=disabled,host,privatepool
288    // TODO: Move from command line to device tree when stabilized.
289    write!(cmdline, "OPENHCL_NVME_KEEP_ALIVE=")?;
290
291    if partition_info.boot_options.disable_nvme_keep_alive {
292        write!(cmdline, "disabled,")?;
293    }
294
295    if partition_info.nvme_keepalive {
296        write!(cmdline, "host,")?;
297    } else {
298        write!(cmdline, "nohost,")?;
299    }
300
301    if vtl2_pool_supported {
302        write!(cmdline, "privatepool ")?;
303    } else {
304        write!(cmdline, "noprivatepool ")?;
305    }
306
307    if let Some(sidecar) = sidecar {
308        write!(cmdline, "{} ", sidecar.kernel_command_line())?;
309    }
310
311    if !cmdline.contains("hv_vmbus.message_connection_id") {
312        // HACK: Set the vmbus connection id via kernel commandline if we haven't
313        // gotten one from elsewhere.
314        //
315        // This code will be removed when the kernel supports setting connection id
316        // via device tree.
317        write!(
318            cmdline,
319            "hv_vmbus.message_connection_id=0x{:x} ",
320            partition_info.vmbus_vtl2.connection_id
321        )?;
322    }
323
324    // Prepend the computed parameters to the original command line.
325    cmdline.write_str(&partition_info.cmdline)?;
326
327    Ok(())
328}
329
330// The Linux kernel requires that the FDT fit within a single 256KB mapping, as
331// that is the maximum size the kernel can use during its early boot processes.
332// We also want our FDT to be as large as possible to support as many vCPUs as
333// possible. We set it to 256KB, but it must also be page-aligned, as leaving it
334// unaligned runs the possibility of it taking up 1 too many pages, resulting in
335// a 260KB mapping, which will fail.
336const FDT_SIZE: usize = 256 * 1024;
337
338#[repr(C, align(4096))]
339#[derive(FromBytes, IntoBytes, Immutable, KnownLayout)]
340struct Fdt {
341    header: setup_data,
342    data: [u8; FDT_SIZE - size_of::<setup_data>()],
343}
344
345/// Raw shim parameters are provided via a relative offset from the base of
346/// where the shim is loaded. Return a ShimParams structure based on the raw
347/// offset based RawShimParams.
348fn shim_parameters(shim_params_raw_offset: isize) -> ShimParams {
349    unsafe extern "C" {
350        static __ehdr_start: u8;
351    }
352
353    let shim_base = core::ptr::addr_of!(__ehdr_start) as usize;
354
355    // SAFETY: The host is required to relocate everything by the same bias, so
356    //         the shim parameters should be at the build time specified offset
357    //         from the base address of the image.
358    let raw_shim_params = unsafe {
359        &*(shim_base.wrapping_add_signed(shim_params_raw_offset) as *const ShimParamsRaw)
360    };
361
362    ShimParams::new(shim_base as u64, raw_shim_params)
363}
364
365#[cfg_attr(not(target_arch = "x86_64"), expect(dead_code))]
366mod x86_boot {
367    use crate::PageAlign;
368    use crate::memory::AddressSpaceManager;
369    use crate::single_threaded::OffStackRef;
370    use crate::single_threaded::off_stack;
371    use crate::zeroed;
372    use core::mem::size_of;
373    use core::ops::Range;
374    use core::ptr;
375    use loader_defs::linux::E820_RAM;
376    use loader_defs::linux::E820_RESERVED;
377    use loader_defs::linux::SETUP_E820_EXT;
378    use loader_defs::linux::boot_params;
379    use loader_defs::linux::e820entry;
380    use loader_defs::linux::setup_data;
381    use loader_defs::shim::MemoryVtlType;
382    use memory_range::MemoryRange;
383    use zerocopy::FromZeros;
384    use zerocopy::Immutable;
385    use zerocopy::KnownLayout;
386
387    #[repr(C)]
388    #[derive(FromZeros, Immutable, KnownLayout)]
389    pub struct E820Ext {
390        pub header: setup_data,
391        pub entries: [e820entry; 512],
392    }
393
394    fn add_e820_entry(
395        entry: Option<&mut e820entry>,
396        range: MemoryRange,
397        typ: u32,
398    ) -> Result<(), BuildE820MapError> {
399        *entry.ok_or(BuildE820MapError::OutOfE820Entries)? = e820entry {
400            addr: range.start().into(),
401            size: range.len().into(),
402            typ: typ.into(),
403        };
404        Ok(())
405    }
406
407    #[derive(Debug)]
408    pub enum BuildE820MapError {
409        /// Out of e820 entries.
410        OutOfE820Entries,
411    }
412
413    /// Build the e820 map for the kernel representing usable VTL2 ram.
414    pub fn build_e820_map(
415        boot_params: &mut boot_params,
416        ext: &mut E820Ext,
417        address_space: &AddressSpaceManager,
418    ) -> Result<bool, BuildE820MapError> {
419        boot_params.e820_entries = 0;
420        let mut entries = boot_params
421            .e820_map
422            .iter_mut()
423            .chain(ext.entries.iter_mut());
424
425        let mut n = 0;
426        for (range, typ) in address_space.vtl2_ranges() {
427            match typ {
428                MemoryVtlType::VTL2_RAM => {
429                    add_e820_entry(entries.next(), range, E820_RAM)?;
430                    n += 1;
431                }
432                MemoryVtlType::VTL2_CONFIG
433                | MemoryVtlType::VTL2_SIDECAR_IMAGE
434                | MemoryVtlType::VTL2_SIDECAR_NODE
435                | MemoryVtlType::VTL2_RESERVED
436                | MemoryVtlType::VTL2_GPA_POOL
437                | MemoryVtlType::VTL2_TDX_PAGE_TABLES
438                | MemoryVtlType::VTL2_BOOTSHIM_LOG_BUFFER
439                | MemoryVtlType::VTL2_PERSISTED_STATE_HEADER
440                | MemoryVtlType::VTL2_PERSISTED_STATE_PROTOBUF => {
441                    add_e820_entry(entries.next(), range, E820_RESERVED)?;
442                    n += 1;
443                }
444
445                _ => {
446                    panic!("unexpected vtl2 ram type {typ:?} for range {range:#?}");
447                }
448            }
449        }
450
451        let base = n.min(boot_params.e820_map.len());
452        boot_params.e820_entries = base as u8;
453
454        if base < n {
455            ext.header.len = ((n - base) * size_of::<e820entry>()) as u32;
456            Ok(true)
457        } else {
458            Ok(false)
459        }
460    }
461
462    pub fn build_boot_params(
463        address_space: &AddressSpaceManager,
464        initrd: Range<u64>,
465        cmdline: &str,
466        setup_data_head: *const setup_data,
467        setup_data_tail: &mut &mut setup_data,
468    ) -> OffStackRef<'static, PageAlign<boot_params>> {
469        let mut boot_params_storage = off_stack!(PageAlign<boot_params>, zeroed());
470        let boot_params = &mut boot_params_storage.0;
471        boot_params.hdr.type_of_loader = 0xff; // Unknown loader type
472
473        // HACK: A kernel change just in the Underhill kernel tree has a workaround
474        // to disable probe_roms and reserve_bios_regions when X86_SUBARCH_LGUEST
475        // (1) is set by the bootloader. This stops the kernel from reading VTL0
476        // memory during kernel boot, which can have catastrophic consequences
477        // during a servicing operation when VTL0 has written values to memory, or
478        // unaccepted page accesses in an isolated partition.
479        //
480        // This is only intended as a stopgap until a suitable upstreamable kernel
481        // patch is made.
482        boot_params.hdr.hardware_subarch = 1.into();
483
484        boot_params.hdr.ramdisk_image = (initrd.start as u32).into();
485        boot_params.ext_ramdisk_image = (initrd.start >> 32) as u32;
486        let initrd_len = initrd.end - initrd.start;
487        boot_params.hdr.ramdisk_size = (initrd_len as u32).into();
488        boot_params.ext_ramdisk_size = (initrd_len >> 32) as u32;
489
490        let e820_ext = OffStackRef::leak(off_stack!(E820Ext, zeroed()));
491
492        let used_ext = build_e820_map(boot_params, e820_ext, address_space)
493            .expect("building e820 map must succeed");
494
495        if used_ext {
496            e820_ext.header.ty = SETUP_E820_EXT;
497            setup_data_tail.next = ptr::from_ref(&e820_ext.header) as u64;
498            *setup_data_tail = &mut e820_ext.header;
499        }
500
501        let cmd_line_addr = cmdline.as_ptr() as u64;
502        boot_params.hdr.cmd_line_ptr = (cmd_line_addr as u32).into();
503        boot_params.ext_cmd_line_ptr = (cmd_line_addr >> 32) as u32;
504
505        boot_params.hdr.setup_data = (setup_data_head as u64).into();
506
507        boot_params_storage
508    }
509}
510
511/// Build the cc_blob containing the location of different parameters associated with SEV.
512#[cfg(target_arch = "x86_64")]
513fn build_cc_blob_sev_info(
514    cc_blob: &mut loader_defs::linux::cc_blob_sev_info,
515    shim_params: &ShimParams,
516) {
517    // TODO SNP: Currently only the first CPUID page is passed through.
518    // Consider changing this.
519    cc_blob.magic = loader_defs::linux::CC_BLOB_SEV_INFO_MAGIC;
520    cc_blob.version = 0;
521    cc_blob._reserved = 0;
522    cc_blob.secrets_phys = shim_params.secrets_start();
523    cc_blob.secrets_len = hvdef::HV_PAGE_SIZE as u32;
524    cc_blob._rsvd1 = 0;
525    cc_blob.cpuid_phys = shim_params.cpuid_start();
526    cc_blob.cpuid_len = hvdef::HV_PAGE_SIZE as u32;
527    cc_blob._rsvd2 = 0;
528}
529
530#[repr(C, align(4096))]
531#[derive(FromZeros, Immutable, KnownLayout)]
532struct PageAlign<T>(T);
533
534const fn zeroed<T: FromZeros>() -> T {
535    // SAFETY: `T` implements `FromZeros`, so this is a safe initialization of `T`.
536    unsafe { core::mem::MaybeUninit::<T>::zeroed().assume_init() }
537}
538
539fn get_ref_time(isolation: IsolationType) -> Option<u64> {
540    match isolation {
541        #[cfg(target_arch = "x86_64")]
542        IsolationType::Tdx => get_tdx_tsc_reftime(),
543        #[cfg(target_arch = "x86_64")]
544        IsolationType::Snp => None,
545        _ => Some(minimal_rt::reftime::reference_time()),
546    }
547}
548
549/// Dump diagnostics when initrd CRC32 does not match the build-time value.
550///
551/// Contents:
552///
553/// - `base` / `size` / `expected` (build-time) / `got` (first read)
554///   CRCs.
555/// - `got2` — a second CRC re-computed immediately from the same virtual
556///   range. If `got2 != got`, initrd memory is not being read consistently
557///   (typical symptom of stale/mismatched cache lines after the SNP shared
558///   -> private transition, rather than data being wrong in memory).
559/// - `head` / `tail` — first and last 16 bytes as hex, to fingerprint what
560///   is actually in memory.
561/// - `eighths` — CRC32 of eight roughly-equal slices of the initrd. This
562///   is a coarse "which region diverges" locator that is cheap to compute
563///   and stable across boots, so it can be compared with a known-good
564///   build without needing a per-page dump (which would blow the log
565///   budget for real-sized initrds).
566//
567// SNP TODO: temporary diagnostic; remove once the SNP initrd CRC mismatch
568// is root-caused.
569fn build_initrd_crc_diagnostic(p: &ShimParams, first_computed_crc: u32) -> ArrayString<384> {
570    let initrd_bytes = p.initrd();
571
572    // A second read from the same VA. If this differs from the first read,
573    // the initrd memory is not being read consistently, which typically
574    // indicates stale/mismatched cache lines rather than actual data
575    // corruption.
576    let second_computed_crc = crc32fast::hash(initrd_bytes);
577
578    // First 16 and last 16 bytes, as fixed-size arrays so we can rely on
579    // Debug's `{:02x?}` slice formatting.
580    let mut head = [0u8; 16];
581    let head_len = head.len().min(initrd_bytes.len());
582    head[..head_len].copy_from_slice(&initrd_bytes[..head_len]);
583
584    let mut tail = [0u8; 16];
585    let tail_len = tail.len().min(initrd_bytes.len());
586    if tail_len > 0 {
587        let start = initrd_bytes.len() - tail_len;
588        tail[..tail_len].copy_from_slice(&initrd_bytes[start..]);
589    }
590
591    // Split the initrd into (up to) 8 roughly-equal slices and CRC each.
592    // Bytes past the aligned slices go into the last chunk.
593    let mut eighths = [0u32; 8];
594    let n = initrd_bytes.len();
595    if n > 0 {
596        let step = n.div_ceil(8);
597        for (i, e) in eighths.iter_mut().enumerate() {
598            let start = i * step;
599            if start >= n {
600                break;
601            }
602            let end = ((i + 1) * step).min(n);
603            *e = crc32fast::hash(&initrd_bytes[start..end]);
604        }
605    }
606
607    let mut buf = ArrayString::<384>::new();
608    let _ = write!(
609        &mut buf,
610        "initrd crc mismatch: iso={:?} base={:#x} size={:#x} \
611         exp={:#x} got={:#x} got2={:#x} head={:02x?} tail={:02x?} \
612         eighths=[{:#x},{:#x},{:#x},{:#x},{:#x},{:#x},{:#x},{:#x}]",
613        p.isolation_type,
614        p.initrd_base,
615        p.initrd_size,
616        p.initrd_crc,
617        first_computed_crc,
618        second_computed_crc,
619        &head[..head_len],
620        &tail[..tail_len],
621        eighths[0],
622        eighths[1],
623        eighths[2],
624        eighths[3],
625        eighths[4],
626        eighths[5],
627        eighths[6],
628        eighths[7],
629    );
630    buf
631}
632
633fn shim_main(shim_params_raw_offset: isize) -> ! {
634    let p = shim_parameters(shim_params_raw_offset);
635    if p.isolation_type == IsolationType::None {
636        enable_enlightened_panic();
637    }
638
639    #[cfg(feature = "cvm_boot_log")]
640    arch::initialize_serial_io(&p);
641
642    // Enable the in-memory log.
643    boot_logger_memory_init(p.log_buffer);
644
645    // Enable global log crate.
646    log::set_logger(&boot_logger::BOOT_LOGGER).unwrap();
647    // TODO: allow overriding filter at runtime
648    log::set_max_level(log::LevelFilter::Info);
649
650    let boot_reftime = get_ref_time(p.isolation_type);
651
652    // The support code for the fast hypercalls does not set
653    // the Guest ID if it is not set yet as opposed to the slow
654    // hypercall code path where that is done automatically.
655    // Thus the fast hypercalls will fail as the the Guest ID has
656    // to be set first hence initialize hypercall support
657    // explicitly.
658    if !p.isolation_type.is_hardware_isolated() {
659        hvcall().initialize();
660    }
661
662    let mut static_options = BootCommandLineOptions::new();
663    if let Some(cmdline) = p.command_line().command_line() {
664        static_options.parse(cmdline);
665    }
666
667    let static_confidential_debug = static_options.confidential_debug;
668    let can_trust_host = p.isolation_type == IsolationType::None || static_confidential_debug;
669
670    let mut dt_storage = off_stack!(PartitionInfo, PartitionInfo::new());
671    let address_space = OffStackRef::leak(off_stack!(
672        AddressSpaceManager,
673        AddressSpaceManager::new_const()
674    ));
675    let partition_info = match PartitionInfo::read_from_dt(
676        &p,
677        &mut dt_storage,
678        address_space,
679        static_options,
680        can_trust_host,
681    ) {
682        Ok(val) => val,
683        Err(e) => panic!("unable to read device tree params {:?}", e),
684    };
685
686    // Enable logging ASAP. This is fine even when isolated, as we don't have
687    // any access to secrets in the boot shim.
688    boot_logger_runtime_init(p.isolation_type, partition_info.com3_serial.clone());
689    log::info!("openhcl_boot: logging enabled");
690    log::info!("serial configuration: {:#x?}", partition_info.com3_serial);
691
692    // Confidential debug will show up in boot_options only if included in the
693    // static command line, or if can_trust_host is true (so the dynamic command
694    // line has been parsed).
695    let is_confidential_debug =
696        static_confidential_debug || partition_info.boot_options.confidential_debug;
697
698    // Fill out the non-devicetree derived parts of PartitionInfo.
699    if !p.isolation_type.is_hardware_isolated()
700        && hvcall().vtl() == Vtl::Vtl2
701        && hvdef::HvRegisterVsmCapabilities::from(
702            hvcall()
703                .get_register(hvdef::HvAllArchRegisterName::VsmCapabilities.into())
704                .expect("failed to query vsm capabilities")
705                .as_u64(),
706        )
707        .vtl0_alias_map_available()
708    {
709        // If the vtl0 alias map was not provided in the devicetree, attempt to
710        // derive it from the architectural physical address bits.
711        //
712        // The value in the ID_AA64MMFR0_EL1 register used to determine the
713        // physical address bits can only represent multiples of 4. As a result,
714        // the Surface Pro X (and systems with similar CPUs) cannot properly
715        // report their address width of 39 bits. This causes the calculated
716        // alias map to be incorrect, which results in panics when trying to
717        // read memory and getting invalid data.
718        if partition_info.vtl0_alias_map.is_none() {
719            partition_info.vtl0_alias_map =
720                Some(1 << (arch::physical_address_bits(p.isolation_type) - 1));
721        }
722    } else {
723        // Ignore any devicetree-provided alias map if the conditions above
724        // aren't met.
725        partition_info.vtl0_alias_map = None;
726    }
727
728    // Rebind partition_info as no longer mutable.
729    let partition_info: &PartitionInfo = partition_info;
730
731    if partition_info.cpus.is_empty() {
732        panic!("no cpus");
733    }
734
735    validate_vp_hw_ids(partition_info);
736
737    setup_vtl2_memory(&p, partition_info, address_space);
738    setup_vtl2_vp(partition_info);
739
740    verify_imported_regions_hash(&p);
741
742    let mut sidecar_params = off_stack!(PageAlign<SidecarParams>, zeroed());
743    let mut sidecar_output = off_stack!(PageAlign<SidecarOutput>, zeroed());
744    let sidecar = sidecar::start_sidecar(
745        &p,
746        partition_info,
747        address_space,
748        &mut sidecar_params.0,
749        &mut sidecar_output.0,
750    );
751
752    // Rebind address_space as no longer mutable.
753    let address_space: &AddressSpaceManager = address_space;
754
755    let mut cmdline = off_stack!(ArrayString<COMMAND_LINE_SIZE>, ArrayString::new_const());
756    build_kernel_command_line(BuildKernelCommandLineParams {
757        params: &p,
758        cmdline: &mut cmdline,
759        partition_info,
760        can_trust_host,
761        is_confidential_debug,
762        sidecar: sidecar.as_ref(),
763        vtl2_pool_supported: address_space.has_vtl2_pool(),
764    })
765    .unwrap();
766
767    let mut fdt = off_stack!(Fdt, zeroed());
768    fdt.header.len = fdt.data.len() as u32;
769    fdt.header.ty = SETUP_DTB;
770
771    #[cfg(target_arch = "x86_64")]
772    let mut setup_data_tail = &mut fdt.header;
773    #[cfg(target_arch = "x86_64")]
774    let setup_data_head = core::ptr::from_ref(setup_data_tail);
775
776    #[cfg(target_arch = "x86_64")]
777    if p.isolation_type == IsolationType::Snp {
778        let cc_blob = OffStackRef::leak(off_stack!(loader_defs::linux::cc_blob_sev_info, zeroed()));
779        build_cc_blob_sev_info(cc_blob, &p);
780
781        let cc_data = OffStackRef::leak(off_stack!(loader_defs::linux::cc_setup_data, zeroed()));
782        cc_data.header.len = size_of::<loader_defs::linux::cc_setup_data>() as u32;
783        cc_data.header.ty = loader_defs::linux::SETUP_CC_BLOB;
784        cc_data.cc_blob_address = core::ptr::from_ref(&*cc_blob) as u32;
785
786        // Chain in the setup data.
787        setup_data_tail.next = core::ptr::from_ref(&*cc_data) as u64;
788        setup_data_tail = &mut cc_data.header;
789    }
790
791    let initrd = p.initrd_base..p.initrd_base + p.initrd_size;
792
793    // Validate the initrd crc matches what was put at file generation time.
794    let computed_crc = crc32fast::hash(p.initrd());
795    if computed_crc != p.initrd_crc && is_confidential_debug {
796        let diag = build_initrd_crc_diagnostic(&p, computed_crc);
797        log::error!("{}", diag.as_str());
798        panic!("{}", diag.as_str());
799    }
800    assert_eq!(
801        computed_crc, p.initrd_crc,
802        "computed initrd crc does not match build time calculated crc"
803    );
804
805    #[cfg(target_arch = "x86_64")]
806    let boot_params = x86_boot::build_boot_params(
807        address_space,
808        initrd.clone(),
809        &cmdline,
810        setup_data_head,
811        &mut setup_data_tail,
812    );
813
814    // Compute the ending boot time. This has to be before writing to device
815    // tree, so this is as late as we can do it.
816
817    let boot_times = boot_reftime.map(|start| BootTimes {
818        start,
819        end: get_ref_time(p.isolation_type).unwrap_or(0),
820    });
821
822    // Validate that no imported regions that are pending are not part of vtl2
823    // ram.
824    for (range, result) in walk_ranges(
825        partition_info.vtl2_ram.iter().map(|r| (r.range, ())),
826        p.imported_regions(),
827    ) {
828        match result {
829            RangeWalkResult::Neither | RangeWalkResult::Left(_) | RangeWalkResult::Both(_, _) => {}
830            RangeWalkResult::Right(accepted) => {
831                // Ranges that are not a part of VTL2 ram must have been
832                // preaccepted, as usermode expect that to be the case.
833                assert!(
834                    accepted,
835                    "range {:#x?} not in vtl2 ram was not preaccepted at launch",
836                    range
837                );
838            }
839        }
840    }
841
842    write_dt(
843        &mut fdt.data,
844        partition_info,
845        address_space,
846        p.imported_regions().map(|r| {
847            // Discard if the range was previously pending - the bootloader has
848            // accepted all pending ranges.
849            //
850            // NOTE: No VTL0 memory today is marked as pending. The check above
851            // validates that, and this code may need to change if this becomes
852            // no longer true.
853            r.0
854        }),
855        initrd,
856        &cmdline,
857        sidecar.as_ref(),
858        boot_times,
859        p.isolation_type,
860    )
861    .unwrap();
862
863    rt::verify_stack_cookie();
864
865    log::info!("uninitializing hypercalls");
866    #[cfg(not(feature = "cvm_boot_log"))]
867    log::info!("about to jump to kernel");
868
869    hvcall().uninitialize();
870
871    #[cfg(feature = "cvm_boot_log")]
872    {
873        log::info!("uninitializing serial io");
874        log::info!("about to jump to kernel");
875        arch::uninitialize_serial_io(&p);
876    }
877
878    cfg_if::cfg_if! {
879        if #[cfg(target_arch = "x86_64")] {
880            // SAFETY: the parameter blob is trusted.
881            let kernel_entry: extern "C" fn(u64, &loader_defs::linux::boot_params) -> ! =
882                unsafe { core::mem::transmute(p.kernel_entry_address) };
883            kernel_entry(0, &boot_params.0)
884        } else if #[cfg(target_arch = "aarch64")] {
885            // SAFETY: the parameter blob is trusted.
886            let kernel_entry: extern "C" fn(fdt_data: *const u8, mbz0: u64, mbz1: u64, mbz2: u64) -> ! =
887                unsafe { core::mem::transmute(p.kernel_entry_address) };
888            // Disable MMU for kernel boot without EFI, as required by the boot protocol.
889            // Flush (and invalidate) the caches, as that is required for disabling MMU.
890            // SAFETY: Just changing a bit in the register and then jumping to the kernel.
891            unsafe {
892                core::arch::asm!(
893                    "
894                    mrs     {0}, sctlr_el1
895                    bic     {0}, {0}, #0x1
896                    msr     sctlr_el1, {0}
897                    tlbi    vmalle1
898                    dsb     sy
899                    isb     sy",
900                    lateout(reg) _,
901                );
902            }
903            kernel_entry(fdt.data.as_ptr(), 0, 0, 0)
904        } else {
905            panic!("unsupported arch")
906        }
907    }
908}
909
910/// Ensure that mshv VP indexes for the CPUs listed in the partition info
911/// correspond to the N in the cpu@N devicetree node name. OpenVMM assumes that
912/// this will be the case.
913fn validate_vp_hw_ids(partition_info: &PartitionInfo) {
914    use host_params::MAX_CPU_COUNT;
915    use hypercall::HwId;
916
917    if partition_info.isolation.is_hardware_isolated() {
918        // TODO TDX SNP: we don't have a GHCB/GHCI page set up to communicate
919        // with the hypervisor here, so we can't easily perform the check. Since
920        // there is no security impact to this check, we can skip it for now; if
921        // the VM fails to boot, then this is due to a host contract violation.
922        //
923        // For TDX, we could use ENUM TOPOLOGY to validate that the TD VCPU
924        // indexes correspond to the APIC IDs in the right order. I am not
925        // certain if there are places where we depend on this mapping today.
926        return;
927    }
928
929    if hvcall().vtl() != Vtl::Vtl2 {
930        // If we're not using guest VSM, then the guest won't communicate
931        // directly with the hypervisor, so we can choose the VP indexes
932        // ourselves.
933        return;
934    }
935
936    // Ensure the host and hypervisor agree on VP index ordering.
937
938    let mut hw_ids = off_stack!(ArrayVec<HwId, MAX_CPU_COUNT>, ArrayVec::new_const());
939    hw_ids.clear();
940    hw_ids.extend(partition_info.cpus.iter().map(|c| c.reg as _));
941    let mut vp_indexes = off_stack!(ArrayVec<u32, MAX_CPU_COUNT>, ArrayVec::new_const());
942    vp_indexes.clear();
943    if let Err(err) = hvcall().get_vp_index_from_hw_id(&hw_ids, &mut vp_indexes) {
944        panic!(
945            "failed to get VP index for hardware ID {:#x}: {}",
946            hw_ids[vp_indexes.len().min(hw_ids.len() - 1)],
947            err
948        );
949    }
950    if let Some((i, &vp_index)) = vp_indexes
951        .iter()
952        .enumerate()
953        .find(|&(i, vp_index)| i as u32 != *vp_index)
954    {
955        panic!(
956            "CPU hardware ID {:#x} does not correspond to VP index {}",
957            hw_ids[i], vp_index
958        );
959    }
960}
961
962// See build.rs. See `mod rt` for the actual bootstrap code required to invoke
963// shim_main.
964#[cfg(not(minimal_rt))]
965fn main() {
966    unimplemented!("build with MINIMAL_RT_BUILD to produce a working boot loader");
967}
968
969#[cfg(test)]
970mod test {
971    use super::x86_boot::E820Ext;
972    use super::x86_boot::build_e820_map;
973    use crate::cmdline::BootCommandLineOptions;
974    use crate::dt::write_dt;
975    use crate::host_params::MAX_CPU_COUNT;
976    use crate::host_params::PartitionInfo;
977    use crate::host_params::shim_params::IsolationType;
978    use crate::memory::AddressSpaceManager;
979    use crate::memory::AddressSpaceManagerBuilder;
980    use arrayvec::ArrayString;
981    use arrayvec::ArrayVec;
982    use core::ops::Range;
983    use host_fdt_parser::ComInfo;
984    use host_fdt_parser::CpuEntry;
985    use host_fdt_parser::MemoryEntry;
986    use host_fdt_parser::VmbusInfo;
987    use igvm_defs::MemoryMapEntryType;
988    use loader_defs::linux::E820_RAM;
989    use loader_defs::linux::E820_RESERVED;
990    use loader_defs::linux::boot_params;
991    use loader_defs::linux::e820entry;
992    use memory_range::MemoryRange;
993    use memory_range::subtract_ranges;
994    use sidecar_defs::PerCpuState;
995    use zerocopy::FromZeros;
996
997    const HIGH_MMIO_GAP_END: u64 = 0x1000000000; //  64 GiB
998    const VMBUS_MMIO_GAP_SIZE: u64 = 0x10000000; // 256 MiB
999    const HIGH_MMIO_GAP_START: u64 = HIGH_MMIO_GAP_END - VMBUS_MMIO_GAP_SIZE;
1000
1001    /// Create partition info with given cpu count enabled and sequential
1002    /// apic_ids.
1003    fn new_partition_info(cpu_count: usize) -> PartitionInfo {
1004        let mut cpus: ArrayVec<CpuEntry, MAX_CPU_COUNT> = ArrayVec::new();
1005
1006        for id in 0..(cpu_count as u64) {
1007            cpus.push(CpuEntry { reg: id, vnode: 0 });
1008        }
1009
1010        let mut mmio = ArrayVec::new();
1011        mmio.push(
1012            MemoryRange::try_new(HIGH_MMIO_GAP_START..HIGH_MMIO_GAP_END).expect("valid range"),
1013        );
1014
1015        PartitionInfo {
1016            vtl2_ram: ArrayVec::new(),
1017            partition_ram: ArrayVec::new(),
1018            isolation: IsolationType::None,
1019            bsp_reg: cpus[0].reg as u32,
1020            cpus,
1021            sidecar_cpu_overrides: PerCpuState {
1022                per_cpu_state_specified: false,
1023                sidecar_starts_cpu: [true; sidecar_defs::NUM_CPUS_SUPPORTED_FOR_PER_CPU_STATE],
1024            },
1025            cmdline: ArrayString::new(),
1026            vmbus_vtl2: VmbusInfo {
1027                mmio,
1028                connection_id: 0,
1029            },
1030            vmbus_vtl0: VmbusInfo {
1031                mmio: ArrayVec::new(),
1032                connection_id: 0,
1033            },
1034            com3_serial: ComInfo::None,
1035            gic: None,
1036            pmu_gsiv: None,
1037            memory_allocation_mode: host_fdt_parser::MemoryAllocationMode::Host,
1038            entropy: None,
1039            vtl0_alias_map: None,
1040            nvme_keepalive: false,
1041            boot_options: BootCommandLineOptions::new(),
1042        }
1043    }
1044
1045    // ensure we can boot with a _lot_ of vcpus
1046    #[test]
1047    #[cfg_attr(
1048        target_arch = "aarch64",
1049        ignore = "TODO: investigate why this doesn't always work on ARM"
1050    )]
1051    fn fdt_cpu_scaling() {
1052        const MAX_CPUS: usize = 2048;
1053
1054        let mut buf = [0; 0x40000];
1055        write_dt(
1056            &mut buf,
1057            &new_partition_info(MAX_CPUS),
1058            &AddressSpaceManager::new_const(),
1059            [],
1060            0..0,
1061            &ArrayString::from("test").unwrap_or_default(),
1062            None,
1063            None,
1064            IsolationType::None,
1065        )
1066        .unwrap();
1067    }
1068
1069    // Must match the DeviceTree blob generated with the standard tooling
1070    // to ensure being compliant to the standards (or, at least, compatibility
1071    // with a widely used implementation).
1072    // For details on regenerating the test content, see `fdt_dtc_decompile`
1073    // below.
1074    #[test]
1075    #[ignore = "TODO: temporarily broken"]
1076    fn fdt_dtc_check_content() {
1077        const MAX_CPUS: usize = 2;
1078        const BUF_SIZE: usize = 0x1000;
1079
1080        // Rust cannot infer the type.
1081        let dtb_data_spans: [(usize, &[u8]); 2] = [
1082            (
1083                /* Span starts at offset */ 0,
1084                b"\xd0\x0d\xfe\xed\x00\x00\x10\x00\x00\x00\x04\x38\x00\x00\x00\x38\
1085                \x00\x00\x00\x28\x00\x00\x00\x11\x00\x00\x00\x10\x00\x00\x00\x00\
1086                \x00\x00\x00\x4a\x00\x00\x01\x6c\x00\x00\x00\x00\x00\x00\x00\x00\
1087                \x00\x00\x00\x00\x00\x00\x00\x00\x23\x61\x64\x64\x72\x65\x73\x73\
1088                \x2d\x63\x65\x6c\x6c\x73\x00\x23\x73\x69\x7a\x65\x2d\x63\x65\x6c\
1089                \x6c\x73\x00\x6d\x6f\x64\x65\x6c\x00\x72\x65\x67\x00\x64\x65\x76\
1090                \x69\x63\x65\x5f\x74\x79\x70\x65\x00\x73\x74\x61\x74\x75\x73\x00\
1091                \x63\x6f\x6d\x70\x61\x74\x69\x62\x6c\x65\x00\x72\x61\x6e\x67\x65\
1092                \x73",
1093            ),
1094            (
1095                /* Span starts at offset */ 0x430,
1096                b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
1097                \x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x02\
1098                \x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x0f\x00\x00\x00\x00\
1099                \x00\x00\x00\x03\x00\x00\x00\x0f\x00\x00\x00\x1b\x6d\x73\x66\x74\
1100                \x2c\x75\x6e\x64\x65\x72\x68\x69\x6c\x6c\x00\x00\x00\x00\x00\x01\
1101                \x63\x70\x75\x73\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x04\
1102                \x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00\x04\
1103                \x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x01\x63\x70\x75\x40\
1104                \x30\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x25\
1105                \x63\x70\x75\x00\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x21\
1106                \x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x05\x00\x00\x00\x31\
1107                \x6f\x6b\x61\x79\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\
1108                \x63\x70\x75\x40\x31\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x04\
1109                \x00\x00\x00\x25\x63\x70\x75\x00\x00\x00\x00\x03\x00\x00\x00\x04\
1110                \x00\x00\x00\x21\x00\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00\x05\
1111                \x00\x00\x00\x31\x6f\x6b\x61\x79\x00\x00\x00\x00\x00\x00\x00\x02\
1112                \x00\x00\x00\x02\x00\x00\x00\x01\x76\x6d\x62\x75\x73\x00\x00\x00\
1113                \x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x02\
1114                \x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x0f\x00\x00\x00\x01\
1115                \x00\x00\x00\x03\x00\x00\x00\x0b\x00\x00\x00\x38\x6d\x73\x66\x74\
1116                \x2c\x76\x6d\x62\x75\x73\x00\x00\x00\x00\x00\x03\x00\x00\x00\x14\
1117                \x00\x00\x00\x43\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\
1118                \xf0\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x02\
1119                \x00\x00\x00\x09",
1120            ),
1121        ];
1122
1123        let mut sample_buf = [0u8; BUF_SIZE];
1124        for (span_start, bytes) in dtb_data_spans {
1125            sample_buf[span_start..span_start + bytes.len()].copy_from_slice(bytes);
1126        }
1127
1128        let mut buf = [0u8; BUF_SIZE];
1129        write_dt(
1130            &mut buf,
1131            &new_partition_info(MAX_CPUS),
1132            &AddressSpaceManager::new_const(),
1133            [],
1134            0..0,
1135            &ArrayString::from("test").unwrap_or_default(),
1136            None,
1137            None,
1138            IsolationType::None,
1139        )
1140        .unwrap();
1141
1142        assert!(sample_buf == buf);
1143    }
1144
1145    // This test should be manually enabled when need to regenerate
1146    // the sample content above and validate spec compliance with `dtc`.
1147    // Before running the test, please install the DeviceTree compiler:
1148    // ```shell
1149    // sudo apt-get update && sudo apt-get install device-tree-compiler
1150    // ```
1151    #[test]
1152    #[ignore = "enabling the test requires installing additional software, \
1153                and developers will experience a break."]
1154    fn fdt_dtc_decompile() {
1155        const MAX_CPUS: usize = 2048;
1156
1157        let mut buf = [0; 0x40000];
1158        write_dt(
1159            &mut buf,
1160            &new_partition_info(MAX_CPUS),
1161            &AddressSpaceManager::new_const(),
1162            [],
1163            0..0,
1164            &ArrayString::from("test").unwrap_or_default(),
1165            None,
1166            None,
1167            IsolationType::None,
1168        )
1169        .unwrap();
1170
1171        let input_dtb_file_name = "openhcl_boot.dtb";
1172        let output_dts_file_name = "openhcl_boot.dts";
1173        std::fs::write(input_dtb_file_name, buf).unwrap();
1174        let success = std::process::Command::new("dtc")
1175            .args([input_dtb_file_name, "-I", "dtb", "-o", output_dts_file_name])
1176            .status()
1177            .unwrap()
1178            .success();
1179        assert!(success);
1180    }
1181
1182    fn new_address_space_manager(
1183        ram: &[MemoryRange],
1184        bootshim_used: MemoryRange,
1185        persisted_range: MemoryRange,
1186        parameter_range: MemoryRange,
1187        reclaim: Option<MemoryRange>,
1188    ) -> AddressSpaceManager {
1189        let ram = ram
1190            .iter()
1191            .cloned()
1192            .map(|range| MemoryEntry {
1193                range,
1194                mem_type: MemoryMapEntryType::VTL2_PROTECTABLE,
1195                vnode: 0,
1196            })
1197            .collect::<Vec<_>>();
1198        let mut address_space = AddressSpaceManager::new_const();
1199        AddressSpaceManagerBuilder::new(
1200            &mut address_space,
1201            &ram,
1202            bootshim_used,
1203            persisted_range,
1204            subtract_ranges([parameter_range], reclaim),
1205        )
1206        .init()
1207        .unwrap();
1208        address_space
1209    }
1210
1211    fn check_e820(boot_params: &boot_params, ext: &E820Ext, expected: &[(Range<u64>, u32)]) {
1212        let actual = boot_params.e820_map[..boot_params.e820_entries as usize]
1213            .iter()
1214            .chain(
1215                ext.entries
1216                    .iter()
1217                    .take((ext.header.len as usize) / size_of::<e820entry>()),
1218            );
1219
1220        assert_eq!(actual.clone().count(), expected.len());
1221
1222        for (actual, (expected_range, expected_type)) in actual.zip(expected.iter()) {
1223            let addr: u64 = actual.addr.into();
1224            let size: u64 = actual.size.into();
1225            let typ: u32 = actual.typ.into();
1226            assert_eq!(addr, expected_range.start);
1227            assert_eq!(size, expected_range.end - expected_range.start);
1228            assert_eq!(typ, *expected_type);
1229        }
1230    }
1231
1232    const PAGE_SIZE: u64 = 0x1000;
1233    const ONE_MB: u64 = 0x10_0000;
1234
1235    #[test]
1236    fn test_e820_basic() {
1237        // memmap with no param reclaim
1238        let mut boot_params: boot_params = FromZeros::new_zeroed();
1239        let mut ext = FromZeros::new_zeroed();
1240        let bootshim_used = MemoryRange::try_new(ONE_MB..3 * ONE_MB).unwrap();
1241        let persisted_header_end = ONE_MB + PAGE_SIZE;
1242        let persisted_end = ONE_MB + 4 * PAGE_SIZE;
1243        let persisted_state = MemoryRange::try_new(ONE_MB..persisted_end).unwrap();
1244        let parameter_range = MemoryRange::try_new(2 * ONE_MB..3 * ONE_MB).unwrap();
1245        let address_space = new_address_space_manager(
1246            &[MemoryRange::new(ONE_MB..4 * ONE_MB)],
1247            bootshim_used,
1248            persisted_state,
1249            parameter_range,
1250            None,
1251        );
1252
1253        assert!(build_e820_map(&mut boot_params, &mut ext, &address_space).is_ok());
1254
1255        check_e820(
1256            &boot_params,
1257            &ext,
1258            &[
1259                (ONE_MB..(persisted_header_end), E820_RESERVED),
1260                (persisted_header_end..persisted_end, E820_RESERVED),
1261                (persisted_end..2 * ONE_MB, E820_RAM),
1262                (2 * ONE_MB..3 * ONE_MB, E820_RESERVED),
1263                (3 * ONE_MB..4 * ONE_MB, E820_RAM),
1264            ],
1265        );
1266
1267        // memmap with reclaim
1268        let mut boot_params: boot_params = FromZeros::new_zeroed();
1269        let mut ext = FromZeros::new_zeroed();
1270        let bootshim_used = MemoryRange::try_new(ONE_MB..5 * ONE_MB).unwrap();
1271        let persisted_header_end = ONE_MB + PAGE_SIZE;
1272        let persisted_end = ONE_MB + 4 * PAGE_SIZE;
1273        let persisted_state = MemoryRange::try_new(ONE_MB..persisted_end).unwrap();
1274        let parameter_range = MemoryRange::try_new(2 * ONE_MB..5 * ONE_MB).unwrap();
1275        let reclaim = MemoryRange::try_new(3 * ONE_MB..4 * ONE_MB).unwrap();
1276        let address_space = new_address_space_manager(
1277            &[MemoryRange::new(ONE_MB..6 * ONE_MB)],
1278            bootshim_used,
1279            persisted_state,
1280            parameter_range,
1281            Some(reclaim),
1282        );
1283
1284        assert!(build_e820_map(&mut boot_params, &mut ext, &address_space).is_ok());
1285
1286        check_e820(
1287            &boot_params,
1288            &ext,
1289            &[
1290                (ONE_MB..(persisted_header_end), E820_RESERVED),
1291                (persisted_header_end..persisted_end, E820_RESERVED),
1292                (persisted_end..2 * ONE_MB, E820_RAM),
1293                (2 * ONE_MB..3 * ONE_MB, E820_RESERVED),
1294                (3 * ONE_MB..4 * ONE_MB, E820_RAM),
1295                (4 * ONE_MB..5 * ONE_MB, E820_RESERVED),
1296                (5 * ONE_MB..6 * ONE_MB, E820_RAM),
1297            ],
1298        );
1299
1300        // two mem ranges
1301        let mut boot_params: boot_params = FromZeros::new_zeroed();
1302        let mut ext = FromZeros::new_zeroed();
1303        let bootshim_used = MemoryRange::try_new(ONE_MB..5 * ONE_MB).unwrap();
1304        let persisted_header_end = ONE_MB + PAGE_SIZE;
1305        let persisted_end = ONE_MB + 4 * PAGE_SIZE;
1306        let persisted_state = MemoryRange::try_new(ONE_MB..persisted_end).unwrap();
1307        let parameter_range = MemoryRange::try_new(2 * ONE_MB..5 * ONE_MB).unwrap();
1308        let reclaim = MemoryRange::try_new(3 * ONE_MB..4 * ONE_MB).unwrap();
1309        let address_space = new_address_space_manager(
1310            &[
1311                MemoryRange::new(ONE_MB..4 * ONE_MB),
1312                MemoryRange::new(4 * ONE_MB..10 * ONE_MB),
1313            ],
1314            bootshim_used,
1315            persisted_state,
1316            parameter_range,
1317            Some(reclaim),
1318        );
1319
1320        assert!(build_e820_map(&mut boot_params, &mut ext, &address_space).is_ok());
1321
1322        check_e820(
1323            &boot_params,
1324            &ext,
1325            &[
1326                (ONE_MB..(persisted_header_end), E820_RESERVED),
1327                (persisted_header_end..persisted_end, E820_RESERVED),
1328                (persisted_end..2 * ONE_MB, E820_RAM),
1329                (2 * ONE_MB..3 * ONE_MB, E820_RESERVED),
1330                (3 * ONE_MB..4 * ONE_MB, E820_RAM),
1331                (4 * ONE_MB..5 * ONE_MB, E820_RESERVED),
1332                (5 * ONE_MB..10 * ONE_MB, E820_RAM),
1333            ],
1334        );
1335
1336        // memmap in 1 mb chunks
1337        let mut boot_params: boot_params = FromZeros::new_zeroed();
1338        let mut ext = FromZeros::new_zeroed();
1339        let bootshim_used = MemoryRange::try_new(ONE_MB..5 * ONE_MB).unwrap();
1340        let persisted_header_end = ONE_MB + PAGE_SIZE;
1341        let persisted_end = ONE_MB + 4 * PAGE_SIZE;
1342        let persisted_state = MemoryRange::try_new(ONE_MB..persisted_end).unwrap();
1343        let parameter_range = MemoryRange::try_new(2 * ONE_MB..5 * ONE_MB).unwrap();
1344        let reclaim = MemoryRange::try_new(3 * ONE_MB..4 * ONE_MB).unwrap();
1345        let address_space = new_address_space_manager(
1346            &[
1347                MemoryRange::new(ONE_MB..2 * ONE_MB),
1348                MemoryRange::new(2 * ONE_MB..3 * ONE_MB),
1349                MemoryRange::new(3 * ONE_MB..4 * ONE_MB),
1350                MemoryRange::new(4 * ONE_MB..5 * ONE_MB),
1351                MemoryRange::new(5 * ONE_MB..6 * ONE_MB),
1352                MemoryRange::new(6 * ONE_MB..7 * ONE_MB),
1353                MemoryRange::new(7 * ONE_MB..8 * ONE_MB),
1354            ],
1355            bootshim_used,
1356            persisted_state,
1357            parameter_range,
1358            Some(reclaim),
1359        );
1360
1361        assert!(build_e820_map(&mut boot_params, &mut ext, &address_space).is_ok());
1362
1363        check_e820(
1364            &boot_params,
1365            &ext,
1366            &[
1367                (ONE_MB..(persisted_header_end), E820_RESERVED),
1368                (persisted_header_end..persisted_end, E820_RESERVED),
1369                (persisted_end..2 * ONE_MB, E820_RAM),
1370                (2 * ONE_MB..3 * ONE_MB, E820_RESERVED),
1371                (3 * ONE_MB..4 * ONE_MB, E820_RAM),
1372                (4 * ONE_MB..5 * ONE_MB, E820_RESERVED),
1373                (5 * ONE_MB..8 * ONE_MB, E820_RAM),
1374            ],
1375        );
1376    }
1377
1378    // test e820 with spillover into ext
1379    #[test]
1380    fn test_e820_huge() {
1381        use crate::memory::AllocationPolicy;
1382        use crate::memory::AllocationType;
1383
1384        // Create 64 RAM ranges, then allocate 256 ranges to test spillover
1385        // boot_params.e820_map has E820_MAX_ENTRIES_ZEROPAGE (128) entries
1386        const E820_MAX_ENTRIES_ZEROPAGE: usize = 128;
1387        const RAM_RANGES: usize = 64;
1388        const TOTAL_ALLOCATIONS: usize = 256;
1389
1390        // Create 64 large RAM ranges (64MB each = 64 * 1MB pages per range)
1391        let mut ranges = Vec::new();
1392        for i in 0..RAM_RANGES {
1393            let start = (i as u64) * 64 * ONE_MB;
1394            let end = start + 64 * ONE_MB;
1395            ranges.push(MemoryRange::new(start..end));
1396        }
1397
1398        let bootshim_used = MemoryRange::try_new(0..ONE_MB * 2).unwrap();
1399        let persisted_range = MemoryRange::try_new(0..ONE_MB).unwrap();
1400        let parameter_range = MemoryRange::try_new(ONE_MB..2 * ONE_MB).unwrap();
1401
1402        let mut address_space = {
1403            let ram = ranges
1404                .iter()
1405                .cloned()
1406                .map(|range| MemoryEntry {
1407                    range,
1408                    mem_type: MemoryMapEntryType::VTL2_PROTECTABLE,
1409                    vnode: 0,
1410                })
1411                .collect::<Vec<_>>();
1412            let mut address_space = AddressSpaceManager::new_const();
1413            AddressSpaceManagerBuilder::new(
1414                &mut address_space,
1415                &ram,
1416                bootshim_used,
1417                persisted_range,
1418                core::iter::once(parameter_range),
1419            )
1420            .init()
1421            .unwrap();
1422            address_space
1423        };
1424
1425        for i in 0..TOTAL_ALLOCATIONS {
1426            // Intersperse sidecar node allocations with gpa pool allocations,
1427            // as otherwise the address space manager will collapse adjacent
1428            // ranges of the same type.
1429            let _allocated = address_space
1430                .allocate(
1431                    None,
1432                    ONE_MB,
1433                    if i % 2 == 0 {
1434                        AllocationType::GpaPool
1435                    } else {
1436                        AllocationType::SidecarNode
1437                    },
1438                    AllocationPolicy::LowMemory,
1439                )
1440                .expect("should be able to allocate sidecar node");
1441        }
1442
1443        let mut boot_params: boot_params = FromZeros::new_zeroed();
1444        let mut ext = FromZeros::new_zeroed();
1445        let total_ranges = address_space.vtl2_ranges().count();
1446
1447        let used_ext = build_e820_map(&mut boot_params, &mut ext, &address_space).unwrap();
1448
1449        // Verify that we used the extension
1450        assert!(used_ext, "should use extension when there are many ranges");
1451
1452        // Verify the standard e820_map is full
1453        assert_eq!(boot_params.e820_entries, E820_MAX_ENTRIES_ZEROPAGE as u8);
1454
1455        // Verify the extension has the overflow entries
1456        let ext_entries = (ext.header.len as usize) / size_of::<e820entry>();
1457        assert_eq!(ext_entries, total_ranges - E820_MAX_ENTRIES_ZEROPAGE);
1458
1459        // Verify we have the expected number of total ranges
1460        let total_e820_entries = boot_params.e820_entries as usize + ext_entries;
1461        assert_eq!(total_e820_entries, total_ranges);
1462    }
1463}