1use 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
33mod aarch64 {
35 pub const DEFAULT_GIC_DISTRIBUTOR_BASE: u64 = 0xFFFF_0000;
38 pub const DEFAULT_GIC_REDISTRIBUTORS_BASE: u64 = 0xEFFE_E000;
39
40 pub const VMBUS_PPI_OFFSET: u32 = 2;
50 pub const TIMER_INTID: u32 = 4; 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 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#[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
104fn 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 &[
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
158pub 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: IsolationType,
171) -> Result<(), DtError> {
172 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 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 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 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 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 let address_cells = if cfg!(target_arch = "aarch64") { 2 } else { 1 };
279 let mut cpu_builder = root_builder
280 .start_node("cpus")?
281 .add_u32(p_address_cells, address_cells)?
282 .add_u32(p_size_cells, 0)?;
283
284 for (vp_index, cpu_entry) in partition_info.cpus.iter().enumerate() {
286 let name = format_fixed!(32, "cpu@{}", vp_index + 1);
287
288 let mut cpu = cpu_builder
289 .start_node(name.as_ref())?
290 .add_str(p_device_type, "cpu")?
291 .add_u32(p_numa_node_id, cpu_entry.vnode)?;
292
293 if cfg!(target_arch = "aarch64") {
294 cpu = cpu
295 .add_u64(p_reg, cpu_entry.reg)?
296 .add_str(p_compatible, "arm,arm-v8")?;
297
298 if num_cpus > 1 {
299 cpu = cpu.add_str(p_enable_method, "psci")?;
300 }
301
302 if vp_index == 0 {
303 cpu = cpu.add_str(p_status, "okay")?;
304 } else {
305 cpu = cpu.add_str(p_status, "disabled")?;
306 }
307 } else {
308 cpu = cpu
309 .add_u32(p_reg, cpu_entry.reg as u32)?
310 .add_str(p_status, "okay")?;
311 }
312
313 cpu_builder = cpu.end_node()?;
314 }
315 root_builder = cpu_builder.end_node()?;
316
317 if cfg!(target_arch = "aarch64") {
318 let p_method = root_builder.add_string("method")?;
319 let p_cpu_off = root_builder.add_string("cpu_off")?;
320 let p_cpu_on = root_builder.add_string("cpu_on")?;
321 let psci = root_builder
322 .start_node("psci")?
323 .add_str(p_compatible, "arm,psci-0.2")?
324 .add_str(p_method, "hvc")?
325 .add_u32(p_cpu_off, 1)?
326 .add_u32(p_cpu_on, 2)?;
327 root_builder = psci.end_node()?;
328 }
329
330 for mem_entry in partition_info.vtl2_ram.iter() {
332 let name = format_fixed!(32, "memory@{:x}", mem_entry.range.start());
333 let mut mem = root_builder.start_node(&name)?;
334 mem = mem.add_str(p_device_type, "memory")?;
335 mem = mem.add_u64_array(p_reg, &[mem_entry.range.start(), mem_entry.range.len()])?;
336 mem = mem.add_u32(p_numa_node_id, mem_entry.vnode)?;
337 root_builder = mem.end_node()?;
338 }
339
340 if cfg!(target_arch = "aarch64") {
341 let default = GicInfo {
345 gic_distributor_base: aarch64::DEFAULT_GIC_DISTRIBUTOR_BASE,
346 gic_distributor_size: aarch64defs::GIC_DISTRIBUTOR_SIZE,
347 gic_redistributors_base: aarch64::DEFAULT_GIC_REDISTRIBUTORS_BASE,
348 gic_redistributors_size: aarch64defs::GIC_REDISTRIBUTOR_SIZE * num_cpus as u64,
349 gic_redistributor_stride: aarch64defs::GIC_REDISTRIBUTOR_SIZE,
350 };
351 let gic = partition_info.gic.as_ref().unwrap_or(&default);
352
353 assert_eq!(gic.gic_distributor_size, default.gic_distributor_size);
355 assert_eq!(gic.gic_redistributors_size, default.gic_redistributors_size);
356 assert_eq!(
357 gic.gic_redistributor_stride,
358 default.gic_redistributor_stride
359 );
360
361 let p_interrupt_cells = root_builder.add_string("#interrupt-cells")?;
362 let p_redist_regions = root_builder.add_string("#redistributor-regions")?;
363 let p_redist_stride = root_builder.add_string("redistributor-stride")?;
364 let p_interrupt_controller = root_builder.add_string("interrupt-controller")?;
365 let p_phandle = root_builder.add_string("phandle")?;
366 let p_interrupt_names = root_builder.add_string("interrupt-names")?;
367 let p_always_on = root_builder.add_string("always-on")?;
368 let name = format_fixed!(32, "intc@{}", gic.gic_distributor_base);
369 let gicv3 = root_builder
370 .start_node(name.as_ref())?
371 .add_str(p_compatible, "arm,gic-v3")?
372 .add_u32(p_redist_regions, 1)?
373 .add_u64(p_redist_stride, gic.gic_redistributor_stride)?
374 .add_u64_array(
375 p_reg,
376 &[
377 gic.gic_distributor_base,
378 gic.gic_distributor_size,
379 gic.gic_redistributors_base,
380 gic.gic_redistributors_size,
381 ],
382 )?
383 .add_u32(p_address_cells, 2)?
384 .add_u32(p_size_cells, 2)?
385 .add_u32(p_interrupt_cells, 3)?
386 .add_null(p_interrupt_controller)?
387 .add_u32(p_phandle, aarch64::GIC_PHANDLE)?
388 .add_null(p_ranges)?;
389 root_builder = gicv3.end_node()?;
390
391 let timer = root_builder
393 .start_node("timer")?
394 .add_str(p_compatible, "arm,armv8-timer")?
395 .add_u32(p_interrupt_parent, aarch64::GIC_PHANDLE)?
396 .add_str(p_interrupt_names, "virt")?
397 .add_u32_array(
398 p_interrupts,
399 &[
404 aarch64::GIC_PPI,
405 aarch64::TIMER_INTID,
406 aarch64::IRQ_TYPE_LEVEL_LOW,
407 ],
408 )?
409 .add_null(p_always_on)?;
410 root_builder = timer.end_node()?;
411
412 let pmu_gsiv_index = partition_info
423 .pmu_gsiv
424 .map(|gsiv| {
425 assert!(
426 (16..32).contains(&gsiv),
427 "PMU GSIV must be a PPI in [16, 32) range"
428 );
429 gsiv - 16
430 })
431 .unwrap_or(aarch64::PMU_GSIV_INT_INDEX);
432 let pmu = root_builder
433 .start_node("pmu")?
434 .add_str(p_compatible, "arm,armv8-pmuv3")?
435 .add_u32_array(
436 p_interrupts,
437 &[
438 aarch64::GIC_PPI,
439 pmu_gsiv_index,
440 aarch64::IRQ_TYPE_LEVEL_HIGH,
441 ],
442 )?;
443 root_builder = pmu.end_node()?;
444
445 if let ComInfo::Pl011 {
447 base,
448 intid,
449 current_speed,
450 } = partition_info.com3_serial
451 {
452 let name = format_fixed!(32, "serial@{:x}", base);
453 let serial = root_builder
454 .start_node(&name)?
455 .add_str(p_compatible, "arm,sbsa-uart")?
456 .add_u32_array(p_reg, &[0, base, 0, 0x1000])?
457 .add_u32(p_interrupt_parent, aarch64::GIC_PHANDLE)?
458 .add_u32_array(p_interrupts, &[0, intid, 4])?
459 .add_u32(p_current_speed, current_speed)?
460 .add_str(p_status, "okay")?;
461 root_builder = serial.end_node()?;
462 }
463 }
464
465 let mut simple_bus_builder = root_builder
467 .start_node("bus")?
468 .add_str(p_compatible, "simple-bus")?
469 .add_u32(p_address_cells, 2)?
470 .add_u32(p_size_cells, 2)?;
471 simple_bus_builder = simple_bus_builder.add_prop_array(p_ranges, &[])?;
472
473 let vmbus_info = VmbusDeviceTreeInfo {
474 p_address_cells,
475 p_size_cells,
476 p_compatible,
477 p_ranges,
478 p_vtl,
479 p_dma_coherent,
480 p_interrupt_parent,
481 p_interrupts,
482 interrupt_cell_value: if cfg!(target_arch = "aarch64") {
483 Some(aarch64::IRQ_TYPE_EDGE_RISING)
484 } else {
485 None
486 },
487 };
488
489 simple_bus_builder = write_vmbus(
490 simple_bus_builder,
491 "vmbus",
492 Vtl::Vtl2,
493 &partition_info.vmbus_vtl2,
494 vmbus_info,
495 )?;
496
497 if let Some(sidecar) = sidecar {
498 for node in sidecar.nodes {
499 let name = format_fixed!(64, "sidecar@{:x}", node.control_page);
500 simple_bus_builder = simple_bus_builder
501 .start_node(&name)?
502 .add_str(p_compatible, "microsoft,openhcl-sidecar")?
503 .add_u64_array(
504 p_reg,
505 &[
506 node.control_page,
507 sidecar_defs::PAGE_SIZE as u64,
508 node.shmem_pages_base,
509 node.shmem_pages_size,
510 ],
511 )?
512 .add_str_array(p_reg_names, &["ctrl", "shmem"])?
513 .end_node()?;
514 }
515 }
516
517 root_builder = simple_bus_builder.end_node()?;
518
519 if cfg!(target_arch = "aarch64") {
520 let p_bootargs = root_builder.add_string("bootargs")?;
521 let p_initrd_start = root_builder.add_string("linux,initrd-start")?;
522 let p_initrd_end = root_builder.add_string("linux,initrd-end")?;
523
524 let chosen = root_builder
525 .start_node("chosen")?
526 .add_str(p_bootargs, cmdline.as_str())?
527 .add_u64(p_initrd_start, initrd.start)?
528 .add_u64(p_initrd_end, initrd.end)?;
529 root_builder = chosen.end_node()?;
530 }
531
532 let mut openhcl_builder = root_builder.start_node("openhcl")?;
534
535 let p_isolation_type = openhcl_builder.add_string("isolation-type")?;
536 let isolation_type = match partition_info.isolation {
537 IsolationType::None => "none",
538 IsolationType::Vbs => "vbs",
539 IsolationType::Snp => "snp",
540 IsolationType::Tdx => "tdx",
541 IsolationType::Cca => "cca",
542 };
543 openhcl_builder = openhcl_builder.add_str(p_isolation_type, isolation_type)?;
544
545 let p_memory_allocation_mode = openhcl_builder.add_string("memory-allocation-mode")?;
548 match partition_info.memory_allocation_mode {
549 MemoryAllocationMode::Host => {
550 openhcl_builder = openhcl_builder.add_str(p_memory_allocation_mode, "host")?;
551 }
552 MemoryAllocationMode::Vtl2 {
553 memory_size,
554 mmio_size,
555 } => {
556 let p_memory_size = openhcl_builder.add_string("memory-size")?;
557 let p_mmio_size = openhcl_builder.add_string("mmio-size")?;
558 openhcl_builder = openhcl_builder.add_str(p_memory_allocation_mode, "vtl2")?;
559 if let Some(memory_size) = memory_size {
560 openhcl_builder = openhcl_builder.add_u64(p_memory_size, memory_size)?;
561 }
562 if let Some(mmio_size) = mmio_size {
563 openhcl_builder = openhcl_builder.add_u64(p_mmio_size, mmio_size)?;
564 }
565 }
566 }
567
568 if let Some(data) = partition_info.vtl0_alias_map {
569 let p_vtl0_alias_map = openhcl_builder.add_string("vtl0-alias-map")?;
570 openhcl_builder = openhcl_builder.add_u64(p_vtl0_alias_map, data)?;
571 }
572
573 let memory_openhcl_type = "memory-openhcl";
580 for (range, result) in walk_ranges(
581 partition_info.partition_ram.iter().map(|r| (r.range, r)),
582 address_space.vtl2_ranges(),
583 ) {
584 match result {
585 RangeWalkResult::Left(entry) => {
586 let name = format_fixed!(64, "memory@{:x}", range.start());
588 openhcl_builder = openhcl_builder
589 .start_node(&name)?
590 .add_str(p_device_type, memory_openhcl_type)?
591 .add_u64_array(p_reg, &[range.start(), range.len()])?
592 .add_u32(p_numa_node_id, entry.vnode)?
593 .add_u32(p_igvm_type, entry.mem_type.0.into())?
594 .add_u32(p_openhcl_memory, MemoryVtlType::VTL0.0)?
595 .end_node()?;
596 }
597 RangeWalkResult::Both(partition_entry, vtl2_type) => {
598 let name = format_fixed!(64, "memory@{:x}", range.start());
600 openhcl_builder = openhcl_builder
601 .start_node(&name)?
602 .add_str(p_device_type, memory_openhcl_type)?
603 .add_u64_array(p_reg, &[range.start(), range.len()])?
604 .add_u32(p_numa_node_id, partition_entry.vnode)?
605 .add_u32(p_igvm_type, partition_entry.mem_type.0.into())?
606 .add_u32(p_openhcl_memory, vtl2_type.0)?
607 .end_node()?;
608 }
609 RangeWalkResult::Right(..) => {
610 panic!("vtl2 range {:?} not contained in partition ram", range)
611 }
612 RangeWalkResult::Neither => {}
614 }
615 }
616
617 for entry in &partition_info.vmbus_vtl0.mmio {
619 let name = format_fixed!(64, "memory@{:x}", entry.start());
620 openhcl_builder = openhcl_builder
621 .start_node(&name)?
622 .add_str(p_device_type, memory_openhcl_type)?
623 .add_u64_array(p_reg, &[entry.start(), entry.len()])?
624 .add_u32(p_openhcl_memory, MemoryVtlType::VTL0_MMIO.0)?
625 .end_node()?;
626 }
627
628 for entry in &partition_info.vmbus_vtl2.mmio {
629 let name = format_fixed!(64, "memory@{:x}", entry.start());
630 openhcl_builder = openhcl_builder
631 .start_node(&name)?
632 .add_str(p_device_type, memory_openhcl_type)?
633 .add_u64_array(p_reg, &[entry.start(), entry.len()])?
634 .add_u32(p_openhcl_memory, MemoryVtlType::VTL2_MMIO.0)?
635 .end_node()?;
636 }
637
638 for range in accepted_ranges {
640 let name = format_fixed!(64, "accepted-memory@{:x}", range.start());
641 openhcl_builder = openhcl_builder
642 .start_node(&name)?
643 .add_u64_array(p_reg, &[range.start(), range.len()])?
644 .end_node()?;
645 }
646
647 if let Some(entropy) = &partition_info.entropy {
650 openhcl_builder = openhcl_builder
651 .start_node("entropy")?
652 .add_prop_array(p_reg, &[entropy])?
653 .end_node()?;
654 }
655
656 let root_builder = openhcl_builder.end_node()?;
657
658 root_builder.end_node()?.build(partition_info.bsp_reg)?;
659 Ok(())
660}