Skip to main content

loader\linux/
mptable_boot.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Firmware-less x86-64 Linux direct boot that describes processors and
5//! interrupt routing with Intel MP 1.4 tables instead of ACPI.
6
7use super::CR3_BASE;
8use super::Error;
9use super::GDT_BASE;
10use super::InitrdConfig;
11use super::KERNEL_BASE;
12use super::LoadInfo;
13use super::ZERO_PAGE_BASE;
14use crate::common::import_default_gdt;
15use crate::importer::BootPageAcceptance;
16use crate::importer::ImageLoad;
17use crate::importer::X86Register;
18use crate::mptable;
19use hvdef::HV_PAGE_SIZE;
20use loader_defs::linux as defs;
21use memory_range::MemoryRange;
22use memory_range::subtract_ranges;
23use page_table::IdentityMapSize;
24use page_table::x64::IdentityMapBuilder;
25use page_table::x64::PageTable;
26use page_table::x64::align_up_to_page_size;
27use std::ffi::CString;
28use std::io::Read;
29use std::io::Seek;
30use vm_topology::memory::MemoryLayout;
31use zerocopy::FromZeros;
32use zerocopy::IntoBytes;
33
34const MPTABLE_CMDLINE_BASE: u64 = 0x2_0000;
35const MPTABLE_CMDLINE_END: u64 = 0x3_0000;
36const MPTABLE_ISA_HOLE_BASE: u64 = 0xa_0000;
37const MPTABLE_ISA_HOLE_END: u64 = 0x10_0000;
38const MPTABLE_MMIO_GAP_BASE: u64 = 0xc000_0000;
39const MPTABLE_MMIO_GAP_END: u64 = 0x1_0000_0000;
40const MPTABLE_PAGE_TABLE_COUNT: usize = 6;
41const MPTABLE_PAGE_TABLE_END: u64 = CR3_BASE + MPTABLE_PAGE_TABLE_COUNT as u64 * HV_PAGE_SIZE;
42const MPTABLE_FIXED_BOOT_DATA_RANGES: [(u64, u64); 3] = [
43    (0, ZERO_PAGE_BASE + HV_PAGE_SIZE),
44    (CR3_BASE, MPTABLE_PAGE_TABLE_END),
45    (MPTABLE_CMDLINE_BASE, MPTABLE_CMDLINE_END),
46];
47
48/// An e820 entry as a half-open range, before conversion to
49/// [`defs::e820entry`].
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51struct E820Range {
52    start: u64,
53    end: u64,
54    entry_type: u32,
55}
56
57/// Returns the zero page's setup header. It points to the command line in its
58/// fixed slot and holds the low 32 bits of the initrd's address and size.
59fn build_setup_header(cmdline: &CString, initrd_base: u64, initrd_size: u64) -> defs::setup_header {
60    const LOADER_TYPE_UNREGISTERED: u8 = 0xff;
61
62    let mut hdr = defs::setup_header {
63        boot_flag: 0xaa55.into(),
64        header: 0x53726448.into(),
65        kernel_alignment: 0x100000.into(),
66        ..FromZeros::new_zeroed()
67    };
68    hdr.type_of_loader = LOADER_TYPE_UNREGISTERED;
69    hdr.cmd_line_ptr = MPTABLE_CMDLINE_BASE.try_into().expect("must fit in u32");
70    hdr.cmdline_size = (cmdline.as_bytes().len() as u64)
71        .try_into()
72        .expect("must fit in u32");
73    hdr.ramdisk_image = (initrd_base as u32).into();
74    hdr.ramdisk_size = (initrd_size as u32).into();
75    hdr
76}
77
78/// Returns whether `range` is non-empty and RAM backs all of it, even across
79/// adjacent RAM ranges.
80fn range_is_in_ram(mem_layout: &MemoryLayout, range: MemoryRange) -> bool {
81    !range.is_empty()
82        && subtract_ranges([range], mem_layout.ram().iter().map(|ram| ram.range))
83            .next()
84            .is_none()
85}
86
87fn overlaps_range(range: &MemoryRange, start: u64, end: u64) -> bool {
88    range.start() < end && start < range.end()
89}
90
91/// Returns whether `range` overlaps the MP tables, GDT, zero page, page
92/// tables, or command line.
93fn overlaps_fixed_boot_data(range: &MemoryRange) -> bool {
94    MPTABLE_FIXED_BOOT_DATA_RANGES
95        .into_iter()
96        .any(|(start, end)| overlaps_range(range, start, end))
97}
98
99/// Fails with [`Error::MpTableImageOverlap`] if `image` overlaps `reserved`.
100fn validate_image_not_in_reserved_range(
101    image: &MemoryRange,
102    reserved: &MemoryRange,
103) -> Result<(), Error> {
104    if image.overlaps(reserved) {
105        return Err(Error::MpTableImageOverlap {
106            image_start: image.start(),
107            image_end: image.end(),
108            reserved_start: reserved.start(),
109            reserved_end: reserved.end(),
110        });
111    }
112    Ok(())
113}
114
115/// Returns the pages that hold `size` bytes starting at `gpa`, or `None` if
116/// the range is empty or not representable. Images, such as an initrd, need
117/// not end on a page boundary.
118fn covering_page_range(gpa: u64, size: u64) -> Option<MemoryRange> {
119    if size == 0 {
120        return None;
121    }
122    let end = gpa
123        .checked_add(size)?
124        .checked_next_multiple_of(HV_PAGE_SIZE)?;
125    MemoryRange::try_new(gpa & !(HV_PAGE_SIZE - 1)..end).ok()
126}
127
128/// Checks that the loaded kernel and initrd suit this boot layout. Returns the
129/// page ranges that cover the kernel and, if one was loaded, the initrd.
130///
131/// Each image must lie in RAM below 4 GiB, outside the fixed boot data, the
132/// ISA hole, and the other image, and the kernel entry point must lie in the
133/// kernel.
134fn validate_loaded_ranges(
135    load_info: &LoadInfo,
136    mem_layout: &MemoryLayout,
137) -> Result<(MemoryRange, Option<MemoryRange>), Error> {
138    let kernel = &load_info.kernel;
139    let kernel_range =
140        covering_page_range(kernel.gpa, kernel.size).ok_or(Error::InvalidMpTableMemoryLayout)?;
141    if !range_is_in_ram(mem_layout, kernel_range)
142        || overlaps_fixed_boot_data(&kernel_range)
143        || kernel_range.end() > MPTABLE_MMIO_GAP_END
144        || !(kernel.gpa..kernel.gpa + kernel.size).contains(&kernel.entrypoint)
145    {
146        return Err(Error::InvalidMpTableMemoryLayout);
147    }
148    let isa_hole = MemoryRange::new(MPTABLE_ISA_HOLE_BASE..MPTABLE_ISA_HOLE_END);
149    validate_image_not_in_reserved_range(&kernel_range, &isa_hole)?;
150
151    let initrd_range = load_info
152        .initrd
153        .as_ref()
154        .map(|info| {
155            let range = covering_page_range(info.gpa, info.size)
156                .ok_or(Error::InvalidMpTableMemoryLayout)?;
157            if !range_is_in_ram(mem_layout, range)
158                || overlaps_fixed_boot_data(&range)
159                || range.end() > MPTABLE_MMIO_GAP_END
160                || range.overlaps(&kernel_range)
161            {
162                return Err(Error::InvalidMpTableMemoryLayout);
163            }
164            validate_image_not_in_reserved_range(&range, &isa_hole)?;
165            Ok(range)
166        })
167        .transpose()?;
168
169    Ok((kernel_range, initrd_range))
170}
171
172/// Returns the e820 map: the RAM in `mem_layout`, with the ISA hole and
173/// `reserved_memory_ranges` marked reserved, sorted by address, and with
174/// adjacent entries of the same type merged.
175///
176/// Fails if RAM does not back the fixed boot data and the ISA hole, if any RAM
177/// lies in the MMIO gap from 3 GiB to 4 GiB, or if the reservations are not
178/// sorted, disjoint, page-aligned, non-empty ranges of RAM outside the boot
179/// data and the ISA hole.
180fn build_mptable_e820(
181    mem_layout: &MemoryLayout,
182    reserved_memory_ranges: &[MemoryRange],
183) -> Result<Vec<E820Range>, Error> {
184    if MPTABLE_FIXED_BOOT_DATA_RANGES
185        .into_iter()
186        .any(|(start, end)| !range_is_in_ram(mem_layout, MemoryRange::new(start..end)))
187    {
188        return Err(Error::InvalidMpTableMemoryLayout);
189    }
190
191    let mut previous_end = 0;
192    for range in reserved_memory_ranges {
193        let valid = !range.is_empty()
194            && range.start().is_multiple_of(HV_PAGE_SIZE)
195            && range.end().is_multiple_of(HV_PAGE_SIZE)
196            && range.start() >= previous_end
197            && range_is_in_ram(mem_layout, *range)
198            && !overlaps_fixed_boot_data(range);
199        if !valid {
200            return Err(Error::InvalidReservedMemoryRange {
201                start: range.start(),
202                end: range.end(),
203            });
204        }
205        previous_end = range.end();
206    }
207
208    let isa_hole = MemoryRange::new(MPTABLE_ISA_HOLE_BASE..MPTABLE_ISA_HOLE_END);
209    let mmio_gap = MemoryRange::new(MPTABLE_MMIO_GAP_BASE..MPTABLE_MMIO_GAP_END);
210    if !range_is_in_ram(mem_layout, isa_hole)
211        || mem_layout
212            .ram()
213            .iter()
214            .any(|ram| ram.range.overlaps(&mmio_gap))
215        || reserved_memory_ranges
216            .iter()
217            .any(|range| range.overlaps(&isa_hole))
218    {
219        return Err(Error::InvalidMpTableMemoryLayout);
220    }
221
222    let mut reservations = reserved_memory_ranges.to_vec();
223    reservations.push(isa_hole);
224    reservations.sort();
225
226    let mut entries = Vec::with_capacity(mem_layout.ram().len() + reservations.len() * 2 + 1);
227    for ram in mem_layout.ram() {
228        let mut next = ram.range.start();
229        for reserved in reservations
230            .iter()
231            .filter(|reserved| reserved.overlaps(&ram.range))
232        {
233            let reserved_start = reserved.start().max(ram.range.start());
234            let reserved_end = reserved.end().min(ram.range.end());
235            if next < reserved_start {
236                entries.push(E820Range {
237                    start: next,
238                    end: reserved_start,
239                    entry_type: defs::E820_RAM,
240                });
241            }
242            entries.push(E820Range {
243                start: reserved_start,
244                end: reserved_end,
245                entry_type: defs::E820_RESERVED,
246            });
247            next = reserved_end;
248        }
249        if next < ram.range.end() {
250            entries.push(E820Range {
251                start: next,
252                end: ram.range.end(),
253                entry_type: defs::E820_RAM,
254            });
255        }
256    }
257    entries.sort_by_key(|entry| (entry.start, entry.end));
258
259    let mut merged: Vec<E820Range> = Vec::with_capacity(entries.len());
260    for entry in entries {
261        if entry.start >= entry.end {
262            return Err(Error::InvalidMpTableMemoryLayout);
263        }
264        if let Some(previous) = merged.last_mut() {
265            if entry.start < previous.end {
266                return Err(Error::InvalidMpTableMemoryLayout);
267            }
268            if entry.start == previous.end && entry.entry_type == previous.entry_type {
269                previous.end = entry.end;
270                continue;
271            }
272        }
273        merged.push(entry);
274    }
275    Ok(merged)
276}
277
278/// Returns the zero page: the setup header, the high 32 bits of the initrd's
279/// address and size, and the e820 map from [`build_mptable_e820`].
280///
281/// Fails as [`build_mptable_e820`] does, or if the map has more entries than
282/// the zero page holds.
283fn build_mptable_zero_page(
284    mem_layout: &MemoryLayout,
285    reserved_memory_ranges: &[MemoryRange],
286    cmdline: &CString,
287    initrd_base: u64,
288    initrd_size: u64,
289) -> Result<defs::boot_params, Error> {
290    let hdr = build_setup_header(cmdline, initrd_base, initrd_size);
291    let mut boot_params = defs::boot_params {
292        hdr,
293        ..FromZeros::new_zeroed()
294    };
295    boot_params.ext_ramdisk_image = (initrd_base >> 32) as u32;
296    boot_params.ext_ramdisk_size = (initrd_size >> 32) as u32;
297    let entries = build_mptable_e820(mem_layout, reserved_memory_ranges)?;
298    let capacity = boot_params.e820_map.len();
299    if entries.len() > capacity {
300        return Err(Error::TooManyMemoryRanges(capacity));
301    }
302    for (output, entry) in boot_params.e820_map.iter_mut().zip(&entries) {
303        *output = defs::e820entry {
304            addr: entry.start.into(),
305            size: (entry.end - entry.start).into(),
306            typ: entry.entry_type.into(),
307        };
308    }
309    boot_params.e820_entries =
310        u8::try_from(entries.len()).map_err(|_| Error::TooManyMemoryRanges(capacity))?;
311    Ok(boot_params)
312}
313
314/// Imports the NUL-terminated command line into its fixed slot, or nothing if
315/// the command line is empty. Fails if it does not fit in the slot.
316fn import_command_line(
317    importer: &mut impl ImageLoad<X86Register>,
318    cmdline: &CString,
319) -> Result<(), Error> {
320    let raw_cmdline = cmdline.as_bytes_with_nul();
321    let slot_size = MPTABLE_CMDLINE_END - MPTABLE_CMDLINE_BASE;
322    if raw_cmdline.len() as u64 > slot_size {
323        return Err(Error::CommandLineTooLong(raw_cmdline.len(), slot_size));
324    }
325    if raw_cmdline.len() > 1 {
326        let cmdline_size_pages = align_up_to_page_size(raw_cmdline.len() as u64) / HV_PAGE_SIZE;
327        importer
328            .import_pages(
329                MPTABLE_CMDLINE_BASE / HV_PAGE_SIZE,
330                cmdline_size_pages,
331                "linux-commandline",
332                BootPageAcceptance::Exclusive,
333                raw_cmdline,
334            )
335            .map_err(Error::Importer)?;
336    }
337    Ok(())
338}
339
340/// Imports the default GDT and the page tables that identity-map the low
341/// 4 GiB.
342fn import_x86_boot_pages(importer: &mut impl ImageLoad<X86Register>) -> Result<(), Error> {
343    import_default_gdt(importer, GDT_BASE / HV_PAGE_SIZE).map_err(Error::Importer)?;
344    let mut page_table_work_buffer: Vec<PageTable> =
345        vec![PageTable::new_zeroed(); MPTABLE_PAGE_TABLE_COUNT];
346    let mut page_table: Vec<u8> = vec![0; MPTABLE_PAGE_TABLE_COUNT * HV_PAGE_SIZE as usize];
347    let page_table = IdentityMapBuilder::new(
348        CR3_BASE,
349        IdentityMapSize::Size4Gb,
350        page_table_work_buffer.as_mut_slice(),
351        page_table.as_mut_slice(),
352    )?
353    .build();
354    assert!((page_table.len() as u64).is_multiple_of(HV_PAGE_SIZE));
355    importer
356        .import_pages(
357            CR3_BASE / HV_PAGE_SIZE,
358            page_table.len() as u64 / HV_PAGE_SIZE,
359            "linux-pagetables",
360            BootPageAcceptance::Exclusive,
361            page_table,
362        )
363        .map_err(Error::Importer)?;
364    Ok(())
365}
366
367/// Sets the BSP's registers to start the kernel at its entry point in 64-bit
368/// mode, paging through the identity map, with `rsi` pointing to the zero
369/// page.
370fn import_x86_registers(
371    importer: &mut impl ImageLoad<X86Register>,
372    load_info: &LoadInfo,
373) -> Result<(), Error> {
374    let mut import_reg = |register| {
375        importer
376            .import_vp_register(register)
377            .map_err(Error::Importer)
378    };
379
380    import_reg(X86Register::Cr0(x86defs::X64_CR0_PG | x86defs::X64_CR0_PE))?;
381    import_reg(X86Register::Cr3(CR3_BASE))?;
382    import_reg(X86Register::Cr4(x86defs::X64_CR4_PAE))?;
383    import_reg(X86Register::Efer(
384        x86defs::X64_EFER_SCE
385            | x86defs::X64_EFER_LME
386            | x86defs::X64_EFER_LMA
387            | x86defs::X64_EFER_NXE,
388    ))?;
389    import_reg(X86Register::Rip(load_info.kernel.entrypoint))?;
390    import_reg(X86Register::Rsi(ZERO_PAGE_BASE))?;
391    Ok(())
392}
393
394/// Validates the command line, the loaded images, the memory layout, and the
395/// reservations, and then imports everything but the images: the MP tables,
396/// command line, GDT, page tables, zero page, and BSP registers. Imports
397/// nothing if validation fails.
398fn import_mptable_config(
399    importer: &mut impl ImageLoad<X86Register>,
400    load_info: &LoadInfo,
401    cmdline: &CString,
402    mem_layout: &MemoryLayout,
403    config: &mptable::MpTableConfig<'_>,
404    reserved_memory_ranges: &[MemoryRange],
405) -> Result<(), Error> {
406    let raw_cmdline = cmdline.as_bytes_with_nul();
407    let slot_size = MPTABLE_CMDLINE_END - MPTABLE_CMDLINE_BASE;
408    if raw_cmdline.len() as u64 > slot_size {
409        return Err(Error::CommandLineTooLong(raw_cmdline.len(), slot_size));
410    }
411    let (kernel_range, initrd_range) = validate_loaded_ranges(load_info, mem_layout)?;
412    if let Some(range) = reserved_memory_ranges.iter().find(|range| {
413        range.overlaps(&kernel_range)
414            || initrd_range.is_some_and(|initrd_range| range.overlaps(&initrd_range))
415    }) {
416        return Err(Error::InvalidReservedMemoryRange {
417            start: range.start(),
418            end: range.end(),
419        });
420    }
421    let tables = mptable::build(config)?;
422    let table_end = mptable::MP_CONFIG_TABLE_ADDR
423        .checked_add(tables.configuration_table.len())
424        .ok_or(Error::InvalidMpTableMemoryLayout)?;
425    if table_end > GDT_BASE as usize {
426        return Err(Error::MpTableOverlap {
427            table_end,
428            gdt_addr: GDT_BASE,
429        });
430    }
431    let boot_params = build_mptable_zero_page(
432        mem_layout,
433        reserved_memory_ranges,
434        cmdline,
435        load_info.initrd.as_ref().map(|info| info.gpa).unwrap_or(0),
436        load_info.initrd.as_ref().map(|info| info.size).unwrap_or(0),
437    )?;
438
439    let mut mp_page = [0u8; HV_PAGE_SIZE as usize];
440    mp_page[mptable::MP_FLOATING_POINTER_ADDR
441        ..mptable::MP_FLOATING_POINTER_ADDR + tables.floating_pointer.len()]
442        .copy_from_slice(&tables.floating_pointer);
443    mp_page[mptable::MP_CONFIG_TABLE_ADDR..table_end].copy_from_slice(&tables.configuration_table);
444    importer
445        .import_pages(
446            0,
447            1,
448            "linux-mptable",
449            BootPageAcceptance::Exclusive,
450            &mp_page,
451        )
452        .map_err(Error::Importer)?;
453
454    import_command_line(importer, cmdline)?;
455    import_x86_boot_pages(importer)?;
456    importer
457        .import_pages(
458            ZERO_PAGE_BASE / HV_PAGE_SIZE,
459            1,
460            "linux-zeropage",
461            BootPageAcceptance::Exclusive,
462            boot_params.as_bytes(),
463        )
464        .map_err(Error::Importer)?;
465    import_x86_registers(importer, load_info)?;
466    Ok(())
467}
468
469/// Loads an uncompressed x86-64 ELF kernel, and an optional initrd, for Linux
470/// direct boot that describes the processors and interrupt routing with Intel
471/// MP 1.4 tables instead of ACPI, and that has no SMBIOS or other firmware
472/// tables. Returns where the kernel and initrd were loaded.
473///
474/// It also imports the boot data at fixed addresses: the MP tables, GDT, and
475/// zero page in `0x0..0x3000`, the page tables that identity-map the low
476/// 4 GiB in `0x4000..0xa000`, and the NUL-terminated command line, of up to
477/// 64 KiB, in `0x20000..0x30000`. RAM must back these ranges and the ISA
478/// hole, `0xa0000..0x100000`, and no RAM may lie in the MMIO gap from 3 GiB to
479/// 4 GiB.
480///
481/// The kernel and initrd must lie in RAM below 4 GiB, outside the boot data,
482/// the ISA hole, `reserved_memory_ranges`, and each other. The reserved
483/// ranges must be sorted, disjoint, page-aligned, non-empty ranges of RAM
484/// outside the boot data and the ISA hole. The e820 map reports them and the
485/// ISA hole as reserved.
486pub fn load_x86_mptable<F>(
487    importer: &mut impl ImageLoad<X86Register>,
488    kernel_image: &mut F,
489    initrd: Option<InitrdConfig<'_>>,
490    cmdline: &CString,
491    mem_layout: &MemoryLayout,
492    config: &mptable::MpTableConfig<'_>,
493    reserved_memory_ranges: &[MemoryRange],
494) -> Result<LoadInfo, Error>
495where
496    F: Read + Seek,
497{
498    if crate::bzimage::is_bzimage(kernel_image).map_err(Error::BzImage)? {
499        return Err(Error::MpTableRequiresElf);
500    }
501    let load_info = super::load_kernel_and_initrd_x64(importer, kernel_image, KERNEL_BASE, initrd)?;
502    import_mptable_config(
503        importer,
504        &load_info,
505        cmdline,
506        mem_layout,
507        config,
508        reserved_memory_ranges,
509    )?;
510    Ok(load_info)
511}
512
513#[cfg(test)]
514mod tests {
515    use super::*;
516    use crate::importer::IgvmParameterType;
517    use crate::importer::IsolationConfig;
518    use crate::importer::ParameterAreaIndex;
519    use crate::importer::StartupMemoryType;
520    use crate::linux::InitrdInfo;
521    use crate::linux::KernelInfo;
522    use test_with_tracing::test;
523    use zerocopy::FromBytes;
524
525    const MB: u64 = 0x100000;
526    const GB: u64 = 0x4000_0000;
527
528    fn make_layout(ram_size: u64) -> MemoryLayout {
529        MemoryLayout::new(
530            ram_size,
531            &[MemoryRange::new(4 * GB - 128 * MB..4 * GB)],
532            &[],
533            &[],
534            None,
535        )
536        .unwrap()
537    }
538
539    fn make_mptable_layout(ram_size: u64) -> MemoryLayout {
540        MemoryLayout::new(
541            ram_size,
542            &[MemoryRange::new(3 * GB..4 * GB)],
543            &[],
544            &[],
545            None,
546        )
547        .unwrap()
548    }
549
550    #[derive(Default)]
551    struct RecordingImporter {
552        pages: Vec<(String, u64, u64)>,
553        imports: Vec<ImportRecord>,
554        registers: Vec<X86Register>,
555        vp_context_page: Option<u64>,
556    }
557
558    #[derive(Debug)]
559    struct ImportRecord {
560        page_base: u64,
561        page_count: u64,
562        tag: String,
563        data: Vec<u8>,
564    }
565
566    impl RecordingImporter {
567        fn page_base(&self, tag: &str) -> Option<u64> {
568            self.pages
569                .iter()
570                .find(|(t, ..)| t == tag)
571                .map(|(_, base, _)| *base)
572        }
573    }
574
575    impl ImageLoad<X86Register> for RecordingImporter {
576        fn isolation_config(&self) -> IsolationConfig {
577            IsolationConfig {
578                paravisor_present: false,
579                isolation_type: crate::importer::IsolationType::None,
580                shared_gpa_boundary_bits: None,
581            }
582        }
583
584        fn create_parameter_area(
585            &mut self,
586            _page_base: u64,
587            _page_count: u32,
588            _debug_tag: &str,
589        ) -> anyhow::Result<ParameterAreaIndex> {
590            unimplemented!()
591        }
592
593        fn create_parameter_area_with_data(
594            &mut self,
595            _page_base: u64,
596            _page_count: u32,
597            _debug_tag: &str,
598            _initial_data: &[u8],
599        ) -> anyhow::Result<ParameterAreaIndex> {
600            unimplemented!()
601        }
602
603        fn import_parameter(
604            &mut self,
605            _parameter_area: ParameterAreaIndex,
606            _byte_offset: u32,
607            _parameter_type: IgvmParameterType,
608        ) -> anyhow::Result<()> {
609            unimplemented!()
610        }
611
612        fn import_pages(
613            &mut self,
614            page_base: u64,
615            page_count: u64,
616            debug_tag: &'static str,
617            _acceptance: BootPageAcceptance,
618            data: &[u8],
619        ) -> anyhow::Result<()> {
620            self.pages
621                .push((debug_tag.to_string(), page_base, page_count));
622            self.imports.push(ImportRecord {
623                page_base,
624                page_count,
625                tag: debug_tag.to_string(),
626                data: data.to_vec(),
627            });
628            Ok(())
629        }
630
631        fn import_vp_register(&mut self, register: X86Register) -> anyhow::Result<()> {
632            self.registers.push(register);
633            Ok(())
634        }
635
636        fn verify_startup_memory_available(
637            &mut self,
638            _page_base: u64,
639            _page_count: u64,
640            _memory_type: StartupMemoryType,
641        ) -> anyhow::Result<()> {
642            Ok(())
643        }
644
645        fn set_vp_context_page(&mut self, page_base: u64) -> anyhow::Result<()> {
646            self.vp_context_page = Some(page_base);
647            Ok(())
648        }
649
650        fn relocation_region(
651            &mut self,
652            _gpa: u64,
653            _size_bytes: u64,
654            _relocation_alignment: u64,
655            _minimum_relocation_gpa: u64,
656            _maximum_relocation_gpa: u64,
657            _apply_rip_offset: bool,
658            _apply_gdtr_offset: bool,
659            _vp_index: u16,
660        ) -> anyhow::Result<()> {
661            unimplemented!()
662        }
663
664        fn page_table_relocation(
665            &mut self,
666            _page_table_gpa: u64,
667            _size_pages: u64,
668            _used_pages: u64,
669            _vp_index: u16,
670        ) -> anyhow::Result<()> {
671            unimplemented!()
672        }
673
674        fn set_imported_regions_config_page(&mut self, _page_base: u64) {
675            unimplemented!()
676        }
677    }
678
679    fn test_load_info() -> LoadInfo {
680        LoadInfo {
681            kernel: KernelInfo {
682                gpa: KERNEL_BASE,
683                size: 0x1000,
684                entrypoint: KERNEL_BASE,
685            },
686            initrd: None,
687            dtb: None,
688            bzimage_setup_header: None,
689        }
690    }
691
692    fn mptable_config(apic_ids: &[u32]) -> mptable::MpTableConfig<'_> {
693        mptable::MpTableConfig {
694            apic_ids,
695            level_triggered_irqs: &[4, 5, 6, 7],
696        }
697    }
698
699    fn imported_zero_page(importer: &RecordingImporter) -> defs::boot_params {
700        let data = &importer
701            .imports
702            .iter()
703            .find(|import| import.tag == "linux-zeropage")
704            .unwrap()
705            .data;
706        defs::boot_params::read_from_bytes(data).unwrap()
707    }
708
709    #[test]
710    fn mptable_config_places_boot_data_without_firmware_tables() {
711        let apic_ids = [0, 1, 2, 3];
712        let mut importer = RecordingImporter::default();
713        import_mptable_config(
714            &mut importer,
715            &test_load_info(),
716            &CString::new("console=hvc0").unwrap(),
717            &make_layout(256 * MB),
718            &mptable_config(&apic_ids),
719            &[MemoryRange::new(0x3_0000..0x3_1000)],
720        )
721        .unwrap();
722
723        let mp = importer
724            .imports
725            .iter()
726            .find(|import| import.tag == "linux-mptable")
727            .unwrap();
728        assert_eq!(mp.page_base, 0);
729        assert_eq!(mp.page_count, 1);
730        assert_eq!(
731            &mp.data[mptable::MP_FLOATING_POINTER_ADDR..mptable::MP_FLOATING_POINTER_ADDR + 4],
732            b"_MP_"
733        );
734        let table_length = u16::from_le_bytes(
735            mp.data[mptable::MP_CONFIG_TABLE_ADDR + 4..mptable::MP_CONFIG_TABLE_ADDR + 6]
736                .try_into()
737                .unwrap(),
738        ) as usize;
739        assert!(mptable::MP_CONFIG_TABLE_ADDR + table_length <= GDT_BASE as usize);
740        assert_eq!(
741            mp.data[mptable::MP_CONFIG_TABLE_ADDR..mptable::MP_CONFIG_TABLE_ADDR + table_length]
742                .iter()
743                .copied()
744                .fold(0u8, |sum, byte| sum.wrapping_add(byte)),
745            0
746        );
747        assert_eq!(
748            importer.page_base("linux-commandline"),
749            Some(MPTABLE_CMDLINE_BASE / HV_PAGE_SIZE)
750        );
751        assert!(importer.imports.iter().all(|import| {
752            !import.tag.contains("acpi")
753                && !import.tag.contains("rsdp")
754                && !import.tag.contains("smbios")
755        }));
756
757        let boot_params = imported_zero_page(&importer);
758        assert_eq!(boot_params.acpi_rsdp_addr, 0);
759        assert_eq!(
760            u32::from(boot_params.hdr.cmd_line_ptr),
761            MPTABLE_CMDLINE_BASE as u32
762        );
763        assert_eq!(u32::from(boot_params.hdr.cmdline_size), 12);
764        assert!(
765            importer
766                .registers
767                .contains(&X86Register::Rsi(ZERO_PAGE_BASE))
768        );
769        assert!(importer.registers.contains(&X86Register::Cr3(CR3_BASE)));
770        assert!(
771            importer
772                .registers
773                .contains(&X86Register::Cr0(x86defs::X64_CR0_PG | x86defs::X64_CR0_PE))
774        );
775        assert!(
776            importer
777                .registers
778                .contains(&X86Register::Cr4(x86defs::X64_CR4_PAE))
779        );
780        assert!(importer.registers.contains(&X86Register::Rip(KERNEL_BASE)));
781        assert!(
782            !importer
783                .registers
784                .contains(&X86Register::Pat(x86defs::X86X_MSR_DEFAULT_PAT))
785        );
786        assert!(
787            !importer
788                .registers
789                .contains(&X86Register::MtrrDefType(0xc00))
790        );
791    }
792
793    #[test]
794    fn mptable_config_supports_full_command_line_region() {
795        let cmdline = CString::new(vec![b'a'; 65_535]).unwrap();
796        let mut importer = RecordingImporter::default();
797        import_mptable_config(
798            &mut importer,
799            &test_load_info(),
800            &cmdline,
801            &make_layout(256 * MB),
802            &mptable_config(&[0]),
803            &[MemoryRange::new(0x3_0000..0x3_1000)],
804        )
805        .unwrap();
806        let imported = importer
807            .imports
808            .iter()
809            .find(|import| import.tag == "linux-commandline")
810            .unwrap();
811        assert_eq!(imported.page_base, MPTABLE_CMDLINE_BASE / HV_PAGE_SIZE);
812        assert_eq!(imported.page_count, 16);
813        assert_eq!(imported.data.len(), 65_536);
814        assert_eq!(imported.data[65_535], 0);
815
816        let mut importer = RecordingImporter::default();
817        let cmdline = CString::new(vec![b'a'; 65_536]).unwrap();
818        let error = import_mptable_config(
819            &mut importer,
820            &test_load_info(),
821            &cmdline,
822            &make_layout(256 * MB),
823            &mptable_config(&[0]),
824            &[MemoryRange::new(0x3_0000..0x3_1000)],
825        )
826        .unwrap_err();
827        assert!(matches!(error, Error::CommandLineTooLong(65_537, 65_536)));
828        assert!(importer.imports.is_empty());
829    }
830
831    #[test]
832    fn mptable_config_rejects_fixed_boot_data_outside_ram() {
833        let layout = MemoryLayout::new(
834            256 * MB,
835            &[MemoryRange::new(0..HV_PAGE_SIZE)],
836            &[],
837            &[],
838            None,
839        )
840        .unwrap();
841        let mut importer = RecordingImporter::default();
842        let error = import_mptable_config(
843            &mut importer,
844            &test_load_info(),
845            &CString::new("").unwrap(),
846            &layout,
847            &mptable_config(&[0]),
848            &[],
849        )
850        .unwrap_err();
851        assert!(matches!(error, Error::InvalidMpTableMemoryLayout));
852        assert!(importer.imports.is_empty());
853    }
854
855    #[test]
856    fn mptable_config_accepts_ranges_spanning_numa_nodes() {
857        let layout = MemoryLayout::new_with_numa(
858            &[KERNEL_BASE + HV_PAGE_SIZE, 3 * HV_PAGE_SIZE, 2 * MB],
859            &[],
860            &[],
861            &[],
862            None,
863        )
864        .unwrap();
865        let load_info = LoadInfo {
866            kernel: KernelInfo {
867                gpa: KERNEL_BASE,
868                size: 2 * HV_PAGE_SIZE,
869                entrypoint: KERNEL_BASE,
870            },
871            ..Default::default()
872        };
873        let reserved =
874            MemoryRange::new(KERNEL_BASE + 3 * HV_PAGE_SIZE..KERNEL_BASE + 5 * HV_PAGE_SIZE);
875        let mut importer = RecordingImporter::default();
876        import_mptable_config(
877            &mut importer,
878            &load_info,
879            &CString::new("").unwrap(),
880            &layout,
881            &mptable_config(&[0]),
882            &[reserved],
883        )
884        .unwrap();
885
886        assert_eq!(
887            build_mptable_e820(&layout, &[reserved]).unwrap(),
888            [
889                E820Range {
890                    start: 0,
891                    end: MPTABLE_ISA_HOLE_BASE,
892                    entry_type: defs::E820_RAM,
893                },
894                E820Range {
895                    start: MPTABLE_ISA_HOLE_BASE,
896                    end: MPTABLE_ISA_HOLE_END,
897                    entry_type: defs::E820_RESERVED,
898                },
899                E820Range {
900                    start: MPTABLE_ISA_HOLE_END,
901                    end: reserved.start(),
902                    entry_type: defs::E820_RAM,
903                },
904                E820Range {
905                    start: reserved.start(),
906                    end: reserved.end(),
907                    entry_type: defs::E820_RESERVED,
908                },
909                E820Range {
910                    start: reserved.end(),
911                    end: layout.end_of_ram(),
912                    entry_type: defs::E820_RAM,
913                },
914            ]
915        );
916    }
917
918    #[test]
919    fn mptable_e820_reserves_status_and_isa_hole_and_leaves_mmio_gap() {
920        let entries = build_mptable_e820(
921            &make_mptable_layout(8 * GB),
922            &[MemoryRange::new(0x3_0000..0x3_1000)],
923        )
924        .unwrap();
925        assert_eq!(
926            entries.as_slice(),
927            &[
928                E820Range {
929                    start: 0,
930                    end: 0x3_0000,
931                    entry_type: defs::E820_RAM,
932                },
933                E820Range {
934                    start: 0x3_0000,
935                    end: 0x3_1000,
936                    entry_type: defs::E820_RESERVED,
937                },
938                E820Range {
939                    start: 0x3_1000,
940                    end: MPTABLE_ISA_HOLE_BASE,
941                    entry_type: defs::E820_RAM,
942                },
943                E820Range {
944                    start: MPTABLE_ISA_HOLE_BASE,
945                    end: MPTABLE_ISA_HOLE_END,
946                    entry_type: defs::E820_RESERVED,
947                },
948                E820Range {
949                    start: MPTABLE_ISA_HOLE_END,
950                    end: MPTABLE_MMIO_GAP_BASE,
951                    entry_type: defs::E820_RAM,
952                },
953                E820Range {
954                    start: MPTABLE_MMIO_GAP_END,
955                    end: 9 * GB,
956                    entry_type: defs::E820_RAM,
957                },
958            ]
959        );
960    }
961
962    #[test]
963    fn mptable_e820_rejects_invalid_reservations_and_capacity() {
964        let layout = make_mptable_layout(256 * MB);
965        for ranges in [
966            vec![MemoryRange::new(256 * MB..256 * MB + HV_PAGE_SIZE)],
967            vec![MemoryRange::new(0..HV_PAGE_SIZE)],
968            vec![MemoryRange::new(
969                MPTABLE_CMDLINE_BASE..MPTABLE_CMDLINE_BASE + HV_PAGE_SIZE,
970            )],
971            vec![
972                MemoryRange::new(0x4_0000..0x4_2000),
973                MemoryRange::new(0x4_1000..0x4_3000),
974            ],
975            vec![
976                MemoryRange::new(0x5_0000..0x5_1000),
977                MemoryRange::new(0x4_0000..0x4_1000),
978            ],
979        ] {
980            assert!(matches!(
981                build_mptable_e820(&layout, &ranges),
982                Err(Error::InvalidReservedMemoryRange { .. })
983            ));
984        }
985
986        let reservations = (0..70)
987            .map(|index| {
988                let start = 0x10_0000 + index * 0x2000;
989                MemoryRange::new(start..start + HV_PAGE_SIZE)
990            })
991            .collect::<Vec<_>>();
992        assert!(matches!(
993            build_mptable_zero_page(&layout, &reservations, &CString::new("").unwrap(), 0, 0,),
994            Err(Error::TooManyMemoryRanges(128))
995        ));
996    }
997
998    #[test]
999    fn mptable_zero_page_preserves_high_initrd_bits() {
1000        let initrd_base = 0x1_2345_6789;
1001        let initrd_size = 0x2_3456_789a;
1002        let boot_params = build_mptable_zero_page(
1003            &make_mptable_layout(256 * MB),
1004            &[],
1005            &CString::new("").unwrap(),
1006            initrd_base,
1007            initrd_size,
1008        )
1009        .unwrap();
1010        assert_eq!(u32::from(boot_params.hdr.ramdisk_image), initrd_base as u32);
1011        assert_eq!(boot_params.ext_ramdisk_image, (initrd_base >> 32) as u32);
1012        assert_eq!(u32::from(boot_params.hdr.ramdisk_size), initrd_size as u32);
1013        assert_eq!(boot_params.ext_ramdisk_size, (initrd_size >> 32) as u32);
1014    }
1015
1016    #[test]
1017    fn mptable_config_rejects_reservation_overlapping_loaded_image() {
1018        let mut importer = RecordingImporter::default();
1019        let error = import_mptable_config(
1020            &mut importer,
1021            &test_load_info(),
1022            &CString::new("").unwrap(),
1023            &make_mptable_layout(256 * MB),
1024            &mptable_config(&[0]),
1025            &[MemoryRange::new(KERNEL_BASE..KERNEL_BASE + HV_PAGE_SIZE)],
1026        )
1027        .unwrap_err();
1028        assert!(matches!(error, Error::InvalidReservedMemoryRange { .. }));
1029        assert!(importer.imports.is_empty());
1030    }
1031
1032    #[test]
1033    fn mptable_config_accepts_unaligned_image_sizes() {
1034        let initrd_base = KERNEL_BASE + 2 * MB;
1035        let initrd_size = 0x1234;
1036        let load_info = LoadInfo {
1037            kernel: KernelInfo {
1038                gpa: KERNEL_BASE,
1039                size: 0x1801,
1040                entrypoint: KERNEL_BASE + 0x1800,
1041            },
1042            initrd: Some(InitrdInfo {
1043                gpa: initrd_base,
1044                size: initrd_size,
1045            }),
1046            ..Default::default()
1047        };
1048        let mut importer = RecordingImporter::default();
1049        import_mptable_config(
1050            &mut importer,
1051            &load_info,
1052            &CString::new("").unwrap(),
1053            &make_mptable_layout(256 * MB),
1054            &mptable_config(&[0]),
1055            &[],
1056        )
1057        .unwrap();
1058
1059        let boot_params = imported_zero_page(&importer);
1060        assert_eq!(u32::from(boot_params.hdr.ramdisk_image), initrd_base as u32);
1061        assert_eq!(u32::from(boot_params.hdr.ramdisk_size), initrd_size as u32);
1062    }
1063
1064    #[test]
1065    fn mptable_config_rejects_images_overlapping_isa_hole() {
1066        let kernel_above_hole = || KernelInfo {
1067            gpa: 2 * MB,
1068            size: HV_PAGE_SIZE,
1069            entrypoint: 2 * MB,
1070        };
1071        let invalid_load_info = [
1072            (
1073                LoadInfo {
1074                    kernel: KernelInfo {
1075                        gpa: MPTABLE_ISA_HOLE_BASE,
1076                        size: HV_PAGE_SIZE,
1077                        entrypoint: MPTABLE_ISA_HOLE_BASE,
1078                    },
1079                    ..Default::default()
1080                },
1081                MPTABLE_ISA_HOLE_BASE,
1082                MPTABLE_ISA_HOLE_BASE + HV_PAGE_SIZE,
1083            ),
1084            (
1085                LoadInfo {
1086                    kernel: kernel_above_hole(),
1087                    initrd: Some(InitrdInfo {
1088                        gpa: MPTABLE_ISA_HOLE_BASE,
1089                        size: HV_PAGE_SIZE,
1090                    }),
1091                    ..Default::default()
1092                },
1093                MPTABLE_ISA_HOLE_BASE,
1094                MPTABLE_ISA_HOLE_BASE + HV_PAGE_SIZE,
1095            ),
1096            (
1097                LoadInfo {
1098                    kernel: kernel_above_hole(),
1099                    initrd: Some(InitrdInfo {
1100                        gpa: MPTABLE_ISA_HOLE_BASE - HV_PAGE_SIZE,
1101                        size: 2 * HV_PAGE_SIZE,
1102                    }),
1103                    ..Default::default()
1104                },
1105                MPTABLE_ISA_HOLE_BASE - HV_PAGE_SIZE,
1106                MPTABLE_ISA_HOLE_BASE + HV_PAGE_SIZE,
1107            ),
1108            (
1109                LoadInfo {
1110                    kernel: kernel_above_hole(),
1111                    initrd: Some(InitrdInfo {
1112                        gpa: MPTABLE_ISA_HOLE_END - HV_PAGE_SIZE,
1113                        size: 2 * HV_PAGE_SIZE,
1114                    }),
1115                    ..Default::default()
1116                },
1117                MPTABLE_ISA_HOLE_END - HV_PAGE_SIZE,
1118                MPTABLE_ISA_HOLE_END + HV_PAGE_SIZE,
1119            ),
1120        ];
1121
1122        for (load_info, image_start, image_end) in invalid_load_info {
1123            let mut importer = RecordingImporter::default();
1124            let error = import_mptable_config(
1125                &mut importer,
1126                &load_info,
1127                &CString::new("").unwrap(),
1128                &make_mptable_layout(256 * MB),
1129                &mptable_config(&[0]),
1130                &[],
1131            )
1132            .unwrap_err();
1133            assert!(matches!(
1134                error,
1135                Error::MpTableImageOverlap {
1136                    image_start: actual_start,
1137                    image_end: actual_end,
1138                    reserved_start: MPTABLE_ISA_HOLE_BASE,
1139                    reserved_end: MPTABLE_ISA_HOLE_END,
1140                } if actual_start == image_start && actual_end == image_end
1141            ));
1142            assert!(importer.imports.is_empty());
1143        }
1144    }
1145
1146    #[test]
1147    fn mptable_config_accepts_initrd_just_above_isa_hole() {
1148        let load_info = LoadInfo {
1149            kernel: KernelInfo {
1150                gpa: 2 * MB,
1151                size: HV_PAGE_SIZE,
1152                entrypoint: 2 * MB,
1153            },
1154            initrd: Some(InitrdInfo {
1155                gpa: MPTABLE_ISA_HOLE_END,
1156                size: HV_PAGE_SIZE,
1157            }),
1158            ..Default::default()
1159        };
1160        let mut importer = RecordingImporter::default();
1161        import_mptable_config(
1162            &mut importer,
1163            &load_info,
1164            &CString::new("").unwrap(),
1165            &make_mptable_layout(256 * MB),
1166            &mptable_config(&[0]),
1167            &[],
1168        )
1169        .unwrap();
1170
1171        let boot_params = imported_zero_page(&importer);
1172        assert_eq!(
1173            u32::from(boot_params.hdr.ramdisk_image),
1174            MPTABLE_ISA_HOLE_END as u32
1175        );
1176        assert_eq!(u32::from(boot_params.hdr.ramdisk_size), HV_PAGE_SIZE as u32);
1177    }
1178
1179    #[test]
1180    fn mptable_config_rejects_initrd_above_identity_map() {
1181        let load_info = LoadInfo {
1182            kernel: KernelInfo {
1183                gpa: KERNEL_BASE,
1184                size: HV_PAGE_SIZE,
1185                entrypoint: KERNEL_BASE,
1186            },
1187            initrd: Some(InitrdInfo {
1188                gpa: MPTABLE_MMIO_GAP_END,
1189                size: HV_PAGE_SIZE,
1190            }),
1191            ..Default::default()
1192        };
1193        let mut importer = RecordingImporter::default();
1194        let error = import_mptable_config(
1195            &mut importer,
1196            &load_info,
1197            &CString::new("").unwrap(),
1198            &make_mptable_layout(8 * GB),
1199            &mptable_config(&[0]),
1200            &[],
1201        )
1202        .unwrap_err();
1203
1204        assert!(matches!(error, Error::InvalidMpTableMemoryLayout));
1205        assert!(importer.imports.is_empty());
1206    }
1207
1208    #[test]
1209    fn mptable_config_rejects_unbootable_loaded_ranges() {
1210        let invalid_load_info = [
1211            LoadInfo {
1212                kernel: KernelInfo {
1213                    gpa: KERNEL_BASE,
1214                    size: HV_PAGE_SIZE,
1215                    entrypoint: MPTABLE_MMIO_GAP_END,
1216                },
1217                ..Default::default()
1218            },
1219            LoadInfo {
1220                kernel: KernelInfo {
1221                    gpa: KERNEL_BASE,
1222                    size: HV_PAGE_SIZE,
1223                    entrypoint: KERNEL_BASE,
1224                },
1225                initrd: Some(InitrdInfo {
1226                    gpa: MPTABLE_CMDLINE_BASE,
1227                    size: HV_PAGE_SIZE,
1228                }),
1229                ..Default::default()
1230            },
1231            LoadInfo {
1232                kernel: KernelInfo {
1233                    gpa: KERNEL_BASE,
1234                    size: HV_PAGE_SIZE,
1235                    entrypoint: KERNEL_BASE,
1236                },
1237                initrd: Some(InitrdInfo {
1238                    gpa: MPTABLE_MMIO_GAP_BASE,
1239                    size: HV_PAGE_SIZE,
1240                }),
1241                ..Default::default()
1242            },
1243            LoadInfo {
1244                kernel: KernelInfo {
1245                    gpa: KERNEL_BASE,
1246                    size: HV_PAGE_SIZE,
1247                    entrypoint: KERNEL_BASE,
1248                },
1249                initrd: Some(InitrdInfo {
1250                    gpa: KERNEL_BASE,
1251                    size: HV_PAGE_SIZE,
1252                }),
1253                ..Default::default()
1254            },
1255        ];
1256
1257        for load_info in invalid_load_info {
1258            let mut importer = RecordingImporter::default();
1259            let error = import_mptable_config(
1260                &mut importer,
1261                &load_info,
1262                &CString::new("").unwrap(),
1263                &make_mptable_layout(256 * MB),
1264                &mptable_config(&[0]),
1265                &[MemoryRange::new(0x3_0000..0x3_1000)],
1266            )
1267            .unwrap_err();
1268            assert!(matches!(error, Error::InvalidMpTableMemoryLayout));
1269            assert!(importer.imports.is_empty());
1270        }
1271    }
1272
1273    #[test]
1274    fn mptable_config_rejects_unrepresentable_ioapic_id_before_import() {
1275        let apic_ids = (0..200).collect::<Vec<_>>();
1276        let mut importer = RecordingImporter::default();
1277        let error = import_mptable_config(
1278            &mut importer,
1279            &test_load_info(),
1280            &CString::new("").unwrap(),
1281            &make_mptable_layout(256 * MB),
1282            &mptable_config(&apic_ids),
1283            &[MemoryRange::new(0x3_0000..0x3_1000)],
1284        )
1285        .unwrap_err();
1286        assert!(matches!(
1287            error,
1288            Error::MpTable(mptable::Error::NoIoApicId { .. })
1289        ));
1290        assert!(importer.imports.is_empty());
1291    }
1292}