Skip to main content

openhcl_boot/
dt.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Module used to write the device tree used by the OpenHCL kernel and
5//! usermode.
6
7use crate::host_params::COMMAND_LINE_SIZE;
8use crate::host_params::PartitionInfo;
9use crate::host_params::shim_params::IsolationType;
10use crate::memory::AddressSpaceManager;
11use crate::memory::MAX_RESERVED_MEM_RANGES;
12use crate::sidecar::SidecarConfig;
13use crate::single_threaded::off_stack;
14use arrayvec::ArrayString;
15use arrayvec::ArrayVec;
16use core::fmt;
17use core::ops::Range;
18use fdt::builder::Builder;
19use fdt::builder::StringId;
20use host_fdt_parser::ComInfo;
21use host_fdt_parser::GicInfo;
22use host_fdt_parser::MemoryAllocationMode;
23use host_fdt_parser::VmbusInfo;
24use hvdef::Vtl;
25use igvm_defs::dt::IGVM_DT_IGVM_TYPE_PROPERTY;
26use loader_defs::shim::MemoryVtlType;
27use memory_range::MemoryRange;
28use memory_range::RangeWalkResult;
29use memory_range::walk_ranges;
30#[cfg(target_arch = "x86_64")]
31use x86defs::tdx::RESET_VECTOR_PAGE;
32
33/// AArch64 defines
34mod aarch64 {
35    // For compatibility with older hosts, use these legacy Hyper-V defaults if
36    // GIC addresses aren't passed in via the host device tree
37    pub const DEFAULT_GIC_DISTRIBUTOR_BASE: u64 = 0xFFFF_0000;
38    pub const DEFAULT_GIC_REDISTRIBUTORS_BASE: u64 = 0xEFFE_E000;
39
40    // The interrupt IDs (INTID's) in the ARM64 DeviceTree must be _relative_
41    // to its base. See `gic_irq_domain_translate` in the Linux kernel, could not
42    // find a specification for that.
43    //
44    // Architecturally, PPIs occupy INTID's in the [16..32) range. In DeviceTree,
45    // the type of the interrupt is specified first (PPI) and then the _relative_ INTID:
46    // for PPI INTID `27` `[GIC_PPI, 27-16, flags]` goes into the DT description.
47    /// VMBus PPI offset for the DT `interrupts` property.
48    /// Canonical INTID is DEFAULT_VMBUS_PPI (18) in openvmm_defs.
49    pub const VMBUS_PPI_OFFSET: u32 = 2;
50    pub const TIMER_INTID: u32 = 4; // Note: the hardware INTID will be 16 + 4
51
52    /// The Hyper-V default PMU_GSIV value.
53    pub const PMU_GSIV: u32 = 0x17;
54    pub const PMU_GSIV_INT_INDEX: u32 = PMU_GSIV - 16;
55
56    pub const GIC_PHANDLE: u32 = 1;
57    pub const GIC_PPI: u32 = 1;
58    pub const IRQ_TYPE_EDGE_RISING: u32 = 1;
59    pub const IRQ_TYPE_LEVEL_LOW: u32 = 8;
60    pub const IRQ_TYPE_LEVEL_HIGH: u32 = 4;
61}
62
63#[derive(Debug)]
64pub enum DtError {
65    // Field is stored solely for logging via debug, not actually dead.
66    Fdt(#[expect(dead_code)] fdt::builder::Error),
67}
68
69impl From<fdt::builder::Error> for DtError {
70    fn from(err: fdt::builder::Error) -> Self {
71        DtError::Fdt(err)
72    }
73}
74
75macro_rules! format_fixed {
76    ($n:expr, $($arg:tt)*) => {
77        {
78            let mut buf = ArrayString::<$n>::new();
79            fmt::write(&mut buf, format_args!($($arg)*)).unwrap();
80            buf
81        }
82    };
83}
84
85pub struct BootTimes {
86    pub start: u64,
87    pub end: u64,
88}
89
90/// Info needed about the current device tree being built to add the vmbus node.
91#[derive(Clone, Copy)]
92pub struct VmbusDeviceTreeInfo {
93    p_address_cells: StringId,
94    p_size_cells: StringId,
95    p_compatible: StringId,
96    p_ranges: StringId,
97    p_vtl: StringId,
98    p_dma_coherent: StringId,
99    p_interrupt_parent: StringId,
100    p_interrupts: StringId,
101    interrupt_cell_value: Option<u32>,
102}
103
104/// Write a vmbus node to the device tree.
105fn write_vmbus<'a, T>(
106    parent: Builder<'a, T>,
107    name: &str,
108    vtl: Vtl,
109    vmbus: &VmbusInfo,
110    dt: VmbusDeviceTreeInfo,
111) -> Result<Builder<'a, T>, DtError> {
112    let VmbusDeviceTreeInfo {
113        p_address_cells,
114        p_size_cells,
115        p_compatible,
116        p_ranges,
117        p_vtl,
118        p_dma_coherent,
119        p_interrupt_parent,
120        p_interrupts,
121        interrupt_cell_value,
122    } = dt;
123
124    let mut vmbus_builder = parent
125        .start_node(name)?
126        .add_u32(p_address_cells, 2)?
127        .add_u32(p_size_cells, 2)?
128        .add_null(p_dma_coherent)?
129        .add_str(p_compatible, "microsoft,vmbus")?
130        .add_u32(p_vtl, u8::from(vtl).into())?;
131
132    let mut mmio_ranges = ArrayVec::<u64, 6>::new();
133    for entry in vmbus.mmio.iter() {
134        mmio_ranges
135            .try_extend_from_slice(&[entry.start(), entry.start(), entry.len()])
136            .expect("should always fit");
137    }
138    vmbus_builder = vmbus_builder.add_u64_array(p_ranges, mmio_ranges.as_slice())?;
139
140    if cfg!(target_arch = "aarch64") {
141        vmbus_builder = vmbus_builder
142            .add_u32(p_interrupt_parent, aarch64::GIC_PHANDLE)?
143            .add_u32_array(
144                p_interrupts,
145                // Here 3 parameters are used as the "#interrupt-cells"
146                // above specifies.
147                &[
148                    aarch64::GIC_PPI,
149                    aarch64::VMBUS_PPI_OFFSET,
150                    interrupt_cell_value.expect("must be set on aarch64"),
151                ],
152            )?;
153    }
154
155    Ok(vmbus_builder.end_node()?)
156}
157
158/// Writes the device tree blob into `buffer`.
159pub fn write_dt(
160    buffer: &mut [u8],
161    partition_info: &PartitionInfo,
162    address_space: &AddressSpaceManager,
163    accepted_ranges: impl IntoIterator<Item = MemoryRange>,
164    initrd: Range<u64>,
165    cmdline: &ArrayString<COMMAND_LINE_SIZE>,
166    sidecar: Option<&SidecarConfig<'_>>,
167    boot_times: Option<BootTimes>,
168    #[cfg_attr(target_arch = "aarch64", expect(unused_variables))]
169    // isolation_type is unused on aarch64
170    isolation_type: IsolationType,
171) -> Result<(), DtError> {
172    // First, the reservation map is built. That keyes off of the x86 E820 memory map.
173    // The `/memreserve/` is used to tell the kernel that the reserved memory is RAM
174    // but it is reserved. That way the kernel allows mapping it via `/dev/mem` without
175    // inhibiting the cache thus disabling the unaligned access on some architectures.
176
177    let mut memory_reservations =
178        off_stack!(ArrayVec<fdt::ReserveEntry, MAX_RESERVED_MEM_RANGES>, ArrayVec::new_const());
179
180    memory_reservations.extend(address_space.reserved_vtl2_ranges().map(|(r, _)| {
181        fdt::ReserveEntry {
182            address: r.start().into(),
183            size: r.len().into(),
184        }
185    }));
186
187    // Build the actual device tree.
188    let builder_config = fdt::builder::BuilderConfig {
189        blob_buffer: buffer,
190        string_table_cap: 1024,
191        memory_reservations: &memory_reservations,
192    };
193    let mut builder = Builder::new(builder_config)?;
194
195    // These StringIds are common across many nodes.
196    let p_address_cells = builder.add_string("#address-cells")?;
197    let p_size_cells = builder.add_string("#size-cells")?;
198    let p_reg = builder.add_string("reg")?;
199    let p_reg_names = builder.add_string("reg-names")?;
200    let p_device_type = builder.add_string("device_type")?;
201    let p_status = builder.add_string("status")?;
202    let p_compatible = builder.add_string("compatible")?;
203    let p_ranges = builder.add_string("ranges")?;
204    let p_numa_node_id = builder.add_string("numa-node-id")?;
205    let p_reftime_boot_start = builder.add_string("reftime_boot_start")?;
206    let p_reftime_boot_end = builder.add_string("reftime_boot_end")?;
207    let p_reftime_sidecar_start = builder.add_string("reftime_sidecar_start")?;
208    let p_reftime_sidecar_end = builder.add_string("reftime_sidecar_end")?;
209    let p_vtl = builder.add_string(igvm_defs::dt::IGVM_DT_VTL_PROPERTY)?;
210    let p_dma_coherent = builder.add_string("dma-coherent")?;
211    let p_igvm_type = builder.add_string(IGVM_DT_IGVM_TYPE_PROPERTY)?;
212    let p_openhcl_memory = builder.add_string("openhcl,memory-type")?;
213
214    // These StringIds are used across multiple AArch64 nodes.
215    //
216    // TODO: If we add support for an associative map based add_string/add_prop
217    // interface to the fdt builder, these explicit definitions would go away.
218    // That would require either alloc support, or an alloc-free associative
219    // datastructure.
220    let p_interrupt_parent = builder.add_string("interrupt-parent")?;
221    let p_interrupts = builder.add_string("interrupts")?;
222    let p_enable_method = builder.add_string("enable-method")?;
223    let p_current_speed = builder.add_string("current-speed")?;
224
225    let num_cpus = partition_info.cpus.len();
226
227    let mut root_builder = builder
228        .start_node("")?
229        .add_u32(p_address_cells, 2)?
230        .add_u32(p_size_cells, 2)?
231        .add_str(p_compatible, "microsoft,openvmm")?;
232
233    if let Some(boot_times) = boot_times {
234        let BootTimes { start, end } = boot_times;
235        root_builder = root_builder
236            .add_u64(p_reftime_boot_start, start)?
237            .add_u64(p_reftime_boot_end, end)?;
238    }
239
240    if let Some(sidecar) = sidecar {
241        root_builder = root_builder
242            .add_u64(p_reftime_sidecar_start, sidecar.start_reftime)?
243            .add_u64(p_reftime_sidecar_end, sidecar.end_reftime)?;
244    }
245
246    let hypervisor_builder = root_builder
247        .start_node("hypervisor")?
248        .add_str(p_compatible, "microsoft,hyperv")?;
249    root_builder = hypervisor_builder.end_node()?;
250
251    #[cfg(target_arch = "x86_64")]
252    if isolation_type == IsolationType::Tdx {
253        // Linux ignores the whole `/reserved-memory` subtree unless its cell
254        // counts match the root node's, so these must stay in sync with the
255        // root node above.
256        let mut mailbox_builder = root_builder
257            .start_node("reserved-memory")?
258            .add_u32(p_address_cells, 2)?
259            .add_u32(p_size_cells, 2)?
260            .add_null(p_ranges)?;
261
262        let name = format_fixed!(32, "wakeup_table@{:x}", RESET_VECTOR_PAGE);
263        let mailbox_addr_builder = mailbox_builder
264            .start_node(name.as_ref())?
265            .add_str(p_compatible, "intel,wakeup-mailbox")?
266            .add_u32_array(
267                p_reg,
268                &[0x0, RESET_VECTOR_PAGE.try_into().unwrap(), 0x0, 0x1000],
269            )?;
270
271        mailbox_builder = mailbox_addr_builder.end_node()?;
272
273        root_builder = mailbox_builder.end_node()?;
274    }
275
276    // For ARM v8, always specify two register cells, which can accommodate
277    // higher number of VPs.
278    let address_cells = cfg_select! {
279        target_arch = "x86_64" => 1,
280        target_arch = "aarch64" => 2,
281    };
282    let mut cpu_builder = root_builder
283        .start_node("cpus")?
284        .add_u32(p_address_cells, address_cells)?
285        .add_u32(p_size_cells, 0)?;
286
287    // Add a CPU node for each cpu.
288    for (vp_index, cpu_entry) in partition_info.cpus.iter().enumerate() {
289        let name = format_fixed!(32, "cpu@{}", vp_index + 1);
290
291        let mut cpu = cpu_builder
292            .start_node(name.as_ref())?
293            .add_str(p_device_type, "cpu")?
294            .add_u32(p_numa_node_id, cpu_entry.vnode)?;
295
296        if cfg!(target_arch = "aarch64") {
297            cpu = cpu
298                .add_u64(p_reg, cpu_entry.reg)?
299                .add_str(p_compatible, "arm,arm-v8")?;
300
301            if num_cpus > 1 {
302                cpu = cpu.add_str(p_enable_method, "psci")?;
303            }
304
305            if vp_index == 0 {
306                cpu = cpu.add_str(p_status, "okay")?;
307            } else {
308                cpu = cpu.add_str(p_status, "disabled")?;
309            }
310        } else {
311            cpu = cpu
312                .add_u32(p_reg, cpu_entry.reg as u32)?
313                .add_str(p_status, "okay")?;
314        }
315
316        cpu_builder = cpu.end_node()?;
317    }
318    root_builder = cpu_builder.end_node()?;
319
320    if cfg!(target_arch = "aarch64") {
321        let p_method = root_builder.add_string("method")?;
322        let p_cpu_off = root_builder.add_string("cpu_off")?;
323        let p_cpu_on = root_builder.add_string("cpu_on")?;
324        let psci = root_builder
325            .start_node("psci")?
326            .add_str(p_compatible, "arm,psci-0.2")?
327            .add_str(p_method, "hvc")?
328            .add_u32(p_cpu_off, 1)?
329            .add_u32(p_cpu_on, 2)?;
330        root_builder = psci.end_node()?;
331    }
332
333    // Add a memory node for each VTL2 range.
334    for mem_entry in partition_info.vtl2_ram.iter() {
335        let name = format_fixed!(32, "memory@{:x}", mem_entry.range.start());
336        let mut mem = root_builder.start_node(&name)?;
337        mem = mem.add_str(p_device_type, "memory")?;
338        mem = mem.add_u64_array(p_reg, &[mem_entry.range.start(), mem_entry.range.len()])?;
339        mem = mem.add_u32(p_numa_node_id, mem_entry.vnode)?;
340        root_builder = mem.end_node()?;
341    }
342
343    if cfg!(target_arch = "aarch64") {
344        // ARM64 Generic Interrupt Controller aka GIC, v3.
345
346        // Use legacy Hyper-V defaults if not specified in the host device tree.
347        let default = GicInfo {
348            gic_distributor_base: aarch64::DEFAULT_GIC_DISTRIBUTOR_BASE,
349            gic_distributor_size: aarch64defs::GIC_DISTRIBUTOR_SIZE,
350            gic_redistributors_base: aarch64::DEFAULT_GIC_REDISTRIBUTORS_BASE,
351            gic_redistributors_size: aarch64defs::GIC_REDISTRIBUTOR_SIZE * num_cpus as u64,
352            gic_redistributor_stride: aarch64defs::GIC_REDISTRIBUTOR_SIZE,
353        };
354        let gic = partition_info.gic.as_ref().unwrap_or(&default);
355
356        // Validate sizes
357        assert_eq!(gic.gic_distributor_size, default.gic_distributor_size);
358        assert_eq!(gic.gic_redistributors_size, default.gic_redistributors_size);
359        assert_eq!(
360            gic.gic_redistributor_stride,
361            default.gic_redistributor_stride
362        );
363
364        let p_interrupt_cells = root_builder.add_string("#interrupt-cells")?;
365        let p_redist_regions = root_builder.add_string("#redistributor-regions")?;
366        let p_redist_stride = root_builder.add_string("redistributor-stride")?;
367        let p_interrupt_controller = root_builder.add_string("interrupt-controller")?;
368        let p_phandle = root_builder.add_string("phandle")?;
369        let p_interrupt_names = root_builder.add_string("interrupt-names")?;
370        let p_always_on = root_builder.add_string("always-on")?;
371        let name = format_fixed!(32, "intc@{}", gic.gic_distributor_base);
372        let gicv3 = root_builder
373            .start_node(name.as_ref())?
374            .add_str(p_compatible, "arm,gic-v3")?
375            .add_u32(p_redist_regions, 1)?
376            .add_u64(p_redist_stride, gic.gic_redistributor_stride)?
377            .add_u64_array(
378                p_reg,
379                &[
380                    gic.gic_distributor_base,
381                    gic.gic_distributor_size,
382                    gic.gic_redistributors_base,
383                    gic.gic_redistributors_size,
384                ],
385            )?
386            .add_u32(p_address_cells, 2)?
387            .add_u32(p_size_cells, 2)?
388            .add_u32(p_interrupt_cells, 3)?
389            .add_null(p_interrupt_controller)?
390            .add_u32(p_phandle, aarch64::GIC_PHANDLE)?
391            .add_null(p_ranges)?;
392        root_builder = gicv3.end_node()?;
393
394        // ARM64 Architectural Timer.
395        let timer = root_builder
396            .start_node("timer")?
397            .add_str(p_compatible, "arm,armv8-timer")?
398            .add_u32(p_interrupt_parent, aarch64::GIC_PHANDLE)?
399            .add_str(p_interrupt_names, "virt")?
400            .add_u32_array(
401                p_interrupts,
402                // Here 3 parameters are used as the "#interrupt-cells"
403                // above specifies. The only interrupt employed is
404                // the one for the virtualized environment, it is a
405                // Private Peripheral Interrupt.
406                &[
407                    aarch64::GIC_PPI,
408                    aarch64::TIMER_INTID,
409                    aarch64::IRQ_TYPE_LEVEL_LOW,
410                ],
411            )?
412            .add_null(p_always_on)?;
413        root_builder = timer.end_node()?;
414
415        // Add PMU.
416        //
417        // TODO: This may be insufficient to get perf to work as the kernel
418        // prints:
419        // `armv8-pmu pmu: hw perfevents: no irqs for PMU, sampling events not supported`
420        //
421        // Tracked by issue 1808.
422        //
423        // NOTE: The host may not provide this value in device tree, so use the
424        // Hyper-V platform default if not provided.
425        let pmu_gsiv_index = partition_info
426            .pmu_gsiv
427            .map(|gsiv| {
428                assert!(
429                    (16..32).contains(&gsiv),
430                    "PMU GSIV must be a PPI in [16, 32) range"
431                );
432                gsiv - 16
433            })
434            .unwrap_or(aarch64::PMU_GSIV_INT_INDEX);
435        let pmu = root_builder
436            .start_node("pmu")?
437            .add_str(p_compatible, "arm,armv8-pmuv3")?
438            .add_u32_array(
439                p_interrupts,
440                &[
441                    aarch64::GIC_PPI,
442                    pmu_gsiv_index,
443                    aarch64::IRQ_TYPE_LEVEL_HIGH,
444                ],
445            )?;
446        root_builder = pmu.end_node()?;
447
448        // ARM64 PL011 Serial device for COM3 OpenHCL logs
449        if let ComInfo::Pl011 {
450            base,
451            intid,
452            current_speed,
453        } = partition_info.com3_serial
454        {
455            let name = format_fixed!(32, "serial@{:x}", base);
456            let serial = root_builder
457                .start_node(&name)?
458                .add_str(p_compatible, "arm,sbsa-uart")?
459                .add_u32_array(p_reg, &[0, base, 0, 0x1000])?
460                .add_u32(p_interrupt_parent, aarch64::GIC_PHANDLE)?
461                .add_u32_array(p_interrupts, &[0, intid, 4])?
462                .add_u32(p_current_speed, current_speed)?
463                .add_str(p_status, "okay")?;
464            root_builder = serial.end_node()?;
465        }
466    }
467
468    // Linux requires vmbus to be under a simple-bus node.
469    let mut simple_bus_builder = root_builder
470        .start_node("bus")?
471        .add_str(p_compatible, "simple-bus")?
472        .add_u32(p_address_cells, 2)?
473        .add_u32(p_size_cells, 2)?;
474    simple_bus_builder = simple_bus_builder.add_prop_array(p_ranges, &[])?;
475
476    let vmbus_info = VmbusDeviceTreeInfo {
477        p_address_cells,
478        p_size_cells,
479        p_compatible,
480        p_ranges,
481        p_vtl,
482        p_dma_coherent,
483        p_interrupt_parent,
484        p_interrupts,
485        interrupt_cell_value: if cfg!(target_arch = "aarch64") {
486            Some(aarch64::IRQ_TYPE_EDGE_RISING)
487        } else {
488            None
489        },
490    };
491
492    simple_bus_builder = write_vmbus(
493        simple_bus_builder,
494        "vmbus",
495        Vtl::Vtl2,
496        &partition_info.vmbus_vtl2,
497        vmbus_info,
498    )?;
499
500    if let Some(sidecar) = sidecar {
501        for node in sidecar.nodes {
502            let name = format_fixed!(64, "sidecar@{:x}", node.control_page);
503            simple_bus_builder = simple_bus_builder
504                .start_node(&name)?
505                .add_str(p_compatible, "microsoft,openhcl-sidecar")?
506                .add_u64_array(
507                    p_reg,
508                    &[
509                        node.control_page,
510                        sidecar_defs::PAGE_SIZE as u64,
511                        node.shmem_pages_base,
512                        node.shmem_pages_size,
513                    ],
514                )?
515                .add_str_array(p_reg_names, &["ctrl", "shmem"])?
516                .end_node()?;
517        }
518    }
519
520    root_builder = simple_bus_builder.end_node()?;
521
522    if cfg!(target_arch = "aarch64") {
523        let p_bootargs = root_builder.add_string("bootargs")?;
524        let p_initrd_start = root_builder.add_string("linux,initrd-start")?;
525        let p_initrd_end = root_builder.add_string("linux,initrd-end")?;
526
527        let chosen = root_builder
528            .start_node("chosen")?
529            .add_str(p_bootargs, cmdline.as_str())?
530            .add_u64(p_initrd_start, initrd.start)?
531            .add_u64(p_initrd_end, initrd.end)?;
532        root_builder = chosen.end_node()?;
533    }
534
535    // Add information used by openhcl usermode.
536    let mut openhcl_builder = root_builder.start_node("openhcl")?;
537
538    let p_isolation_type = openhcl_builder.add_string("isolation-type")?;
539    let isolation_type = match partition_info.isolation {
540        IsolationType::None => "none",
541        IsolationType::Vbs => "vbs",
542        IsolationType::Snp => "snp",
543        IsolationType::Tdx => "tdx",
544        IsolationType::Cca => "cca",
545    };
546    openhcl_builder = openhcl_builder.add_str(p_isolation_type, isolation_type)?;
547
548    // Indicate what kind of memory allocation mode was done by the bootloader
549    // to usermode.
550    let p_memory_allocation_mode = openhcl_builder.add_string("memory-allocation-mode")?;
551    match partition_info.memory_allocation_mode {
552        MemoryAllocationMode::Host => {
553            openhcl_builder = openhcl_builder.add_str(p_memory_allocation_mode, "host")?;
554        }
555        MemoryAllocationMode::Vtl2 {
556            memory_size,
557            mmio_size,
558        } => {
559            let p_memory_size = openhcl_builder.add_string("memory-size")?;
560            let p_mmio_size = openhcl_builder.add_string("mmio-size")?;
561            openhcl_builder = openhcl_builder.add_str(p_memory_allocation_mode, "vtl2")?;
562            if let Some(memory_size) = memory_size {
563                openhcl_builder = openhcl_builder.add_u64(p_memory_size, memory_size)?;
564            }
565            if let Some(mmio_size) = mmio_size {
566                openhcl_builder = openhcl_builder.add_u64(p_mmio_size, mmio_size)?;
567            }
568        }
569    }
570
571    if let Some(data) = partition_info.vtl0_alias_map {
572        let p_vtl0_alias_map = openhcl_builder.add_string("vtl0-alias-map")?;
573        openhcl_builder = openhcl_builder.add_u64(p_vtl0_alias_map, data)?;
574    }
575
576    // Report the unified memory map to usermode describing which memory is
577    // used by what.
578    //
579    // NOTE: Use a different device type for memory ranges, as the Linux kernel
580    // will treat every device tree node with device type as memory, and attempt
581    // to parse numa information from it.
582    let memory_openhcl_type = "memory-openhcl";
583    for (range, result) in walk_ranges(
584        partition_info.partition_ram.iter().map(|r| (r.range, r)),
585        address_space.vtl2_ranges(),
586    ) {
587        match result {
588            RangeWalkResult::Left(entry) => {
589                // This range is usable by VTL0.
590                let name = format_fixed!(64, "memory@{:x}", range.start());
591                openhcl_builder = openhcl_builder
592                    .start_node(&name)?
593                    .add_str(p_device_type, memory_openhcl_type)?
594                    .add_u64_array(p_reg, &[range.start(), range.len()])?
595                    .add_u32(p_numa_node_id, entry.vnode)?
596                    .add_u32(p_igvm_type, entry.mem_type.0.into())?
597                    .add_u32(p_openhcl_memory, MemoryVtlType::VTL0.0)?
598                    .end_node()?;
599            }
600            RangeWalkResult::Both(partition_entry, vtl2_type) => {
601                // This range is in use by VTL2. Indicate that.
602                let name = format_fixed!(64, "memory@{:x}", range.start());
603                openhcl_builder = openhcl_builder
604                    .start_node(&name)?
605                    .add_str(p_device_type, memory_openhcl_type)?
606                    .add_u64_array(p_reg, &[range.start(), range.len()])?
607                    .add_u32(p_numa_node_id, partition_entry.vnode)?
608                    .add_u32(p_igvm_type, partition_entry.mem_type.0.into())?
609                    .add_u32(p_openhcl_memory, vtl2_type.0)?
610                    .end_node()?;
611            }
612            RangeWalkResult::Right(..) => {
613                panic!("vtl2 range {:?} not contained in partition ram", range)
614            }
615            // Ignore ranges not described in either.
616            RangeWalkResult::Neither => {}
617        }
618    }
619
620    // Add mmio ranges for both VTL0 and VTL2.
621    for entry in &partition_info.vmbus_vtl0.mmio {
622        let name = format_fixed!(64, "memory@{:x}", entry.start());
623        openhcl_builder = openhcl_builder
624            .start_node(&name)?
625            .add_str(p_device_type, memory_openhcl_type)?
626            .add_u64_array(p_reg, &[entry.start(), entry.len()])?
627            .add_u32(p_openhcl_memory, MemoryVtlType::VTL0_MMIO.0)?
628            .end_node()?;
629    }
630
631    for entry in &partition_info.vmbus_vtl2.mmio {
632        let name = format_fixed!(64, "memory@{:x}", entry.start());
633        openhcl_builder = openhcl_builder
634            .start_node(&name)?
635            .add_str(p_device_type, memory_openhcl_type)?
636            .add_u64_array(p_reg, &[entry.start(), entry.len()])?
637            .add_u32(p_openhcl_memory, MemoryVtlType::VTL2_MMIO.0)?
638            .end_node()?;
639    }
640
641    // Report accepted ranges underhil openhcl node.
642    for range in accepted_ranges {
643        let name = format_fixed!(64, "accepted-memory@{:x}", range.start());
644        openhcl_builder = openhcl_builder
645            .start_node(&name)?
646            .add_u64_array(p_reg, &[range.start(), range.len()])?
647            .end_node()?;
648    }
649
650    // Pass through host-provided entropy to the init process for seeding
651    // the OpenHCL kernel random number generator
652    if let Some(entropy) = &partition_info.entropy {
653        openhcl_builder = openhcl_builder
654            .start_node("entropy")?
655            .add_prop_array(p_reg, &[entropy])?
656            .end_node()?;
657    }
658
659    let root_builder = openhcl_builder.end_node()?;
660
661    root_builder.end_node()?.build(partition_info.bsp_reg)?;
662    Ok(())
663}