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_vmbus_connection_id: StringId,
99 p_dma_coherent: StringId,
100 p_interrupt_parent: StringId,
101 p_interrupts: StringId,
102 interrupt_cell_value: Option<u32>,
103}
104
105fn write_vmbus<'a, T>(
107 parent: Builder<'a, T>,
108 name: &str,
109 vtl: Vtl,
110 vmbus: &VmbusInfo,
111 dt: VmbusDeviceTreeInfo,
112) -> Result<Builder<'a, T>, DtError> {
113 let VmbusDeviceTreeInfo {
114 p_address_cells,
115 p_size_cells,
116 p_compatible,
117 p_ranges,
118 p_vtl,
119 p_vmbus_connection_id,
120 p_dma_coherent,
121 p_interrupt_parent,
122 p_interrupts,
123 interrupt_cell_value,
124 } = dt;
125
126 let mut vmbus_builder = parent
127 .start_node(name)?
128 .add_u32(p_address_cells, 2)?
129 .add_u32(p_size_cells, 2)?
130 .add_null(p_dma_coherent)?
131 .add_str(p_compatible, "microsoft,vmbus")?
132 .add_u32(p_vtl, u8::from(vtl).into())?
133 .add_u32(p_vmbus_connection_id, vmbus.connection_id)?;
134
135 let mut mmio_ranges = ArrayVec::<u64, 6>::new();
136 for entry in vmbus.mmio.iter() {
137 mmio_ranges
138 .try_extend_from_slice(&[entry.start(), entry.start(), entry.len()])
139 .expect("should always fit");
140 }
141 vmbus_builder = vmbus_builder.add_u64_array(p_ranges, mmio_ranges.as_slice())?;
142
143 if cfg!(target_arch = "aarch64") {
144 vmbus_builder = vmbus_builder
145 .add_u32(p_interrupt_parent, aarch64::GIC_PHANDLE)?
146 .add_u32_array(
147 p_interrupts,
148 &[
151 aarch64::GIC_PPI,
152 aarch64::VMBUS_PPI_OFFSET,
153 interrupt_cell_value.expect("must be set on aarch64"),
154 ],
155 )?;
156 }
157
158 Ok(vmbus_builder.end_node()?)
159}
160
161pub fn write_dt(
163 buffer: &mut [u8],
164 partition_info: &PartitionInfo,
165 address_space: &AddressSpaceManager,
166 accepted_ranges: impl IntoIterator<Item = MemoryRange>,
167 initrd: Range<u64>,
168 cmdline: &ArrayString<COMMAND_LINE_SIZE>,
169 sidecar: Option<&SidecarConfig<'_>>,
170 boot_times: Option<BootTimes>,
171 #[cfg_attr(target_arch = "aarch64", expect(unused_variables))]
172 isolation_type: IsolationType,
174) -> Result<(), DtError> {
175 let mut memory_reservations =
181 off_stack!(ArrayVec<fdt::ReserveEntry, MAX_RESERVED_MEM_RANGES>, ArrayVec::new_const());
182
183 memory_reservations.extend(address_space.reserved_vtl2_ranges().map(|(r, _)| {
184 fdt::ReserveEntry {
185 address: r.start().into(),
186 size: r.len().into(),
187 }
188 }));
189
190 let builder_config = fdt::builder::BuilderConfig {
192 blob_buffer: buffer,
193 string_table_cap: 1024,
194 memory_reservations: &memory_reservations,
195 };
196 let mut builder = Builder::new(builder_config)?;
197
198 let p_address_cells = builder.add_string("#address-cells")?;
200 let p_size_cells = builder.add_string("#size-cells")?;
201 let p_reg = builder.add_string("reg")?;
202 let p_reg_names = builder.add_string("reg-names")?;
203 let p_device_type = builder.add_string("device_type")?;
204 let p_status = builder.add_string("status")?;
205 let p_compatible = builder.add_string("compatible")?;
206 let p_ranges = builder.add_string("ranges")?;
207 let p_numa_node_id = builder.add_string("numa-node-id")?;
208 let p_reftime_boot_start = builder.add_string("reftime_boot_start")?;
209 let p_reftime_boot_end = builder.add_string("reftime_boot_end")?;
210 let p_reftime_sidecar_start = builder.add_string("reftime_sidecar_start")?;
211 let p_reftime_sidecar_end = builder.add_string("reftime_sidecar_end")?;
212 let p_vtl = builder.add_string(igvm_defs::dt::IGVM_DT_VTL_PROPERTY)?;
213 let p_vmbus_connection_id = builder.add_string("microsoft,message-connection-id")?;
214 let p_dma_coherent = builder.add_string("dma-coherent")?;
215 let p_igvm_type = builder.add_string(IGVM_DT_IGVM_TYPE_PROPERTY)?;
216 let p_openhcl_memory = builder.add_string("openhcl,memory-type")?;
217
218 let p_interrupt_parent = builder.add_string("interrupt-parent")?;
225 let p_interrupts = builder.add_string("interrupts")?;
226 let p_enable_method = builder.add_string("enable-method")?;
227 let p_current_speed = builder.add_string("current-speed")?;
228
229 let num_cpus = partition_info.cpus.len();
230
231 let mut root_builder = builder
232 .start_node("")?
233 .add_u32(p_address_cells, 2)?
234 .add_u32(p_size_cells, 2)?
235 .add_str(p_compatible, "microsoft,openvmm")?;
236
237 if let Some(boot_times) = boot_times {
238 let BootTimes { start, end } = boot_times;
239 root_builder = root_builder
240 .add_u64(p_reftime_boot_start, start)?
241 .add_u64(p_reftime_boot_end, end)?;
242 }
243
244 if let Some(sidecar) = sidecar {
245 root_builder = root_builder
246 .add_u64(p_reftime_sidecar_start, sidecar.start_reftime)?
247 .add_u64(p_reftime_sidecar_end, sidecar.end_reftime)?;
248 }
249
250 let hypervisor_builder = root_builder
251 .start_node("hypervisor")?
252 .add_str(p_compatible, "microsoft,hyperv")?;
253 root_builder = hypervisor_builder.end_node()?;
254
255 #[cfg(target_arch = "x86_64")]
256 if isolation_type == IsolationType::Tdx {
257 let mut mailbox_builder = root_builder
258 .start_node("reserved-memory")?
259 .add_u32(p_address_cells, 2)?
260 .add_u32(p_size_cells, 1)?
261 .add_null(p_ranges)?;
262
263 let name = format_fixed!(32, "wakeup_table@{:x}", RESET_VECTOR_PAGE);
264 let mailbox_addr_builder = mailbox_builder
265 .start_node(name.as_ref())?
266 .add_str(p_compatible, "intel,wakeup-mailbox")?
267 .add_u32_array(p_reg, &[0x0, RESET_VECTOR_PAGE.try_into().unwrap(), 0x1000])?;
268
269 mailbox_builder = mailbox_addr_builder.end_node()?;
270
271 root_builder = mailbox_builder.end_node()?;
272 }
273
274 let address_cells = if cfg!(target_arch = "aarch64") { 2 } else { 1 };
277 let mut cpu_builder = root_builder
278 .start_node("cpus")?
279 .add_u32(p_address_cells, address_cells)?
280 .add_u32(p_size_cells, 0)?;
281
282 for (vp_index, cpu_entry) in partition_info.cpus.iter().enumerate() {
284 let name = format_fixed!(32, "cpu@{}", vp_index + 1);
285
286 let mut cpu = cpu_builder
287 .start_node(name.as_ref())?
288 .add_str(p_device_type, "cpu")?
289 .add_u32(p_numa_node_id, cpu_entry.vnode)?;
290
291 if cfg!(target_arch = "aarch64") {
292 cpu = cpu
293 .add_u64(p_reg, cpu_entry.reg)?
294 .add_str(p_compatible, "arm,arm-v8")?;
295
296 if num_cpus > 1 {
297 cpu = cpu.add_str(p_enable_method, "psci")?;
298 }
299
300 if vp_index == 0 {
301 cpu = cpu.add_str(p_status, "okay")?;
302 } else {
303 cpu = cpu.add_str(p_status, "disabled")?;
304 }
305 } else {
306 cpu = cpu
307 .add_u32(p_reg, cpu_entry.reg as u32)?
308 .add_str(p_status, "okay")?;
309 }
310
311 cpu_builder = cpu.end_node()?;
312 }
313 root_builder = cpu_builder.end_node()?;
314
315 if cfg!(target_arch = "aarch64") {
316 let p_method = root_builder.add_string("method")?;
317 let p_cpu_off = root_builder.add_string("cpu_off")?;
318 let p_cpu_on = root_builder.add_string("cpu_on")?;
319 let psci = root_builder
320 .start_node("psci")?
321 .add_str(p_compatible, "arm,psci-0.2")?
322 .add_str(p_method, "hvc")?
323 .add_u32(p_cpu_off, 1)?
324 .add_u32(p_cpu_on, 2)?;
325 root_builder = psci.end_node()?;
326 }
327
328 for mem_entry in partition_info.vtl2_ram.iter() {
330 let name = format_fixed!(32, "memory@{:x}", mem_entry.range.start());
331 let mut mem = root_builder.start_node(&name)?;
332 mem = mem.add_str(p_device_type, "memory")?;
333 mem = mem.add_u64_array(p_reg, &[mem_entry.range.start(), mem_entry.range.len()])?;
334 mem = mem.add_u32(p_numa_node_id, mem_entry.vnode)?;
335 root_builder = mem.end_node()?;
336 }
337
338 if cfg!(target_arch = "aarch64") {
339 let default = GicInfo {
343 gic_distributor_base: aarch64::DEFAULT_GIC_DISTRIBUTOR_BASE,
344 gic_distributor_size: aarch64defs::GIC_DISTRIBUTOR_SIZE,
345 gic_redistributors_base: aarch64::DEFAULT_GIC_REDISTRIBUTORS_BASE,
346 gic_redistributors_size: aarch64defs::GIC_REDISTRIBUTOR_SIZE * num_cpus as u64,
347 gic_redistributor_stride: aarch64defs::GIC_REDISTRIBUTOR_SIZE,
348 };
349 let gic = partition_info.gic.as_ref().unwrap_or(&default);
350
351 assert_eq!(gic.gic_distributor_size, default.gic_distributor_size);
353 assert_eq!(gic.gic_redistributors_size, default.gic_redistributors_size);
354 assert_eq!(
355 gic.gic_redistributor_stride,
356 default.gic_redistributor_stride
357 );
358
359 let p_interrupt_cells = root_builder.add_string("#interrupt-cells")?;
360 let p_redist_regions = root_builder.add_string("#redistributor-regions")?;
361 let p_redist_stride = root_builder.add_string("redistributor-stride")?;
362 let p_interrupt_controller = root_builder.add_string("interrupt-controller")?;
363 let p_phandle = root_builder.add_string("phandle")?;
364 let p_interrupt_names = root_builder.add_string("interrupt-names")?;
365 let p_always_on = root_builder.add_string("always-on")?;
366 let name = format_fixed!(32, "intc@{}", gic.gic_distributor_base);
367 let gicv3 = root_builder
368 .start_node(name.as_ref())?
369 .add_str(p_compatible, "arm,gic-v3")?
370 .add_u32(p_redist_regions, 1)?
371 .add_u64(p_redist_stride, gic.gic_redistributor_stride)?
372 .add_u64_array(
373 p_reg,
374 &[
375 gic.gic_distributor_base,
376 gic.gic_distributor_size,
377 gic.gic_redistributors_base,
378 gic.gic_redistributors_size,
379 ],
380 )?
381 .add_u32(p_address_cells, 2)?
382 .add_u32(p_size_cells, 2)?
383 .add_u32(p_interrupt_cells, 3)?
384 .add_null(p_interrupt_controller)?
385 .add_u32(p_phandle, aarch64::GIC_PHANDLE)?
386 .add_null(p_ranges)?;
387 root_builder = gicv3.end_node()?;
388
389 let timer = root_builder
391 .start_node("timer")?
392 .add_str(p_compatible, "arm,armv8-timer")?
393 .add_u32(p_interrupt_parent, aarch64::GIC_PHANDLE)?
394 .add_str(p_interrupt_names, "virt")?
395 .add_u32_array(
396 p_interrupts,
397 &[
402 aarch64::GIC_PPI,
403 aarch64::TIMER_INTID,
404 aarch64::IRQ_TYPE_LEVEL_LOW,
405 ],
406 )?
407 .add_null(p_always_on)?;
408 root_builder = timer.end_node()?;
409
410 let pmu_gsiv_index = partition_info
421 .pmu_gsiv
422 .map(|gsiv| {
423 assert!(
424 (16..32).contains(&gsiv),
425 "PMU GSIV must be a PPI in [16, 32) range"
426 );
427 gsiv - 16
428 })
429 .unwrap_or(aarch64::PMU_GSIV_INT_INDEX);
430 let pmu = root_builder
431 .start_node("pmu")?
432 .add_str(p_compatible, "arm,armv8-pmuv3")?
433 .add_u32_array(
434 p_interrupts,
435 &[
436 aarch64::GIC_PPI,
437 pmu_gsiv_index,
438 aarch64::IRQ_TYPE_LEVEL_HIGH,
439 ],
440 )?;
441 root_builder = pmu.end_node()?;
442
443 if let ComInfo::Pl011 {
445 base,
446 intid,
447 current_speed,
448 } = partition_info.com3_serial
449 {
450 let name = format_fixed!(32, "serial@{:x}", base);
451 let serial = root_builder
452 .start_node(&name)?
453 .add_str(p_compatible, "arm,sbsa-uart")?
454 .add_u32_array(p_reg, &[0, base, 0, 0x1000])?
455 .add_u32(p_interrupt_parent, aarch64::GIC_PHANDLE)?
456 .add_u32_array(p_interrupts, &[0, intid, 4])?
457 .add_u32(p_current_speed, current_speed)?
458 .add_str(p_status, "okay")?;
459 root_builder = serial.end_node()?;
460 }
461 }
462
463 let mut simple_bus_builder = root_builder
465 .start_node("bus")?
466 .add_str(p_compatible, "simple-bus")?
467 .add_u32(p_address_cells, 2)?
468 .add_u32(p_size_cells, 2)?;
469 simple_bus_builder = simple_bus_builder.add_prop_array(p_ranges, &[])?;
470
471 let vmbus_info = VmbusDeviceTreeInfo {
472 p_address_cells,
473 p_size_cells,
474 p_compatible,
475 p_ranges,
476 p_vtl,
477 p_vmbus_connection_id,
478 p_dma_coherent,
479 p_interrupt_parent,
480 p_interrupts,
481 interrupt_cell_value: if cfg!(target_arch = "aarch64") {
482 Some(aarch64::IRQ_TYPE_EDGE_RISING)
483 } else {
484 None
485 },
486 };
487
488 simple_bus_builder = write_vmbus(
489 simple_bus_builder,
490 "vmbus",
491 Vtl::Vtl2,
492 &partition_info.vmbus_vtl2,
493 vmbus_info,
494 )?;
495
496 if let Some(sidecar) = sidecar {
497 for node in sidecar.nodes {
498 let name = format_fixed!(64, "sidecar@{:x}", node.control_page);
499 simple_bus_builder = simple_bus_builder
500 .start_node(&name)?
501 .add_str(p_compatible, "microsoft,openhcl-sidecar")?
502 .add_u64_array(
503 p_reg,
504 &[
505 node.control_page,
506 sidecar_defs::PAGE_SIZE as u64,
507 node.shmem_pages_base,
508 node.shmem_pages_size,
509 ],
510 )?
511 .add_str_array(p_reg_names, &["ctrl", "shmem"])?
512 .end_node()?;
513 }
514 }
515
516 root_builder = simple_bus_builder.end_node()?;
517
518 if cfg!(target_arch = "aarch64") {
519 let p_bootargs = root_builder.add_string("bootargs")?;
520 let p_initrd_start = root_builder.add_string("linux,initrd-start")?;
521 let p_initrd_end = root_builder.add_string("linux,initrd-end")?;
522
523 let chosen = root_builder
524 .start_node("chosen")?
525 .add_str(p_bootargs, cmdline.as_str())?
526 .add_u64(p_initrd_start, initrd.start)?
527 .add_u64(p_initrd_end, initrd.end)?;
528 root_builder = chosen.end_node()?;
529 }
530
531 let mut openhcl_builder = root_builder.start_node("openhcl")?;
533
534 let p_isolation_type = openhcl_builder.add_string("isolation-type")?;
535 let isolation_type = match partition_info.isolation {
536 IsolationType::None => "none",
537 IsolationType::Vbs => "vbs",
538 IsolationType::Snp => "snp",
539 IsolationType::Tdx => "tdx",
540 IsolationType::Cca => "cca",
541 };
542 openhcl_builder = openhcl_builder.add_str(p_isolation_type, isolation_type)?;
543
544 let p_memory_allocation_mode = openhcl_builder.add_string("memory-allocation-mode")?;
547 match partition_info.memory_allocation_mode {
548 MemoryAllocationMode::Host => {
549 openhcl_builder = openhcl_builder.add_str(p_memory_allocation_mode, "host")?;
550 }
551 MemoryAllocationMode::Vtl2 {
552 memory_size,
553 mmio_size,
554 } => {
555 let p_memory_size = openhcl_builder.add_string("memory-size")?;
556 let p_mmio_size = openhcl_builder.add_string("mmio-size")?;
557 openhcl_builder = openhcl_builder.add_str(p_memory_allocation_mode, "vtl2")?;
558 if let Some(memory_size) = memory_size {
559 openhcl_builder = openhcl_builder.add_u64(p_memory_size, memory_size)?;
560 }
561 if let Some(mmio_size) = mmio_size {
562 openhcl_builder = openhcl_builder.add_u64(p_mmio_size, mmio_size)?;
563 }
564 }
565 }
566
567 if let Some(data) = partition_info.vtl0_alias_map {
568 let p_vtl0_alias_map = openhcl_builder.add_string("vtl0-alias-map")?;
569 openhcl_builder = openhcl_builder.add_u64(p_vtl0_alias_map, data)?;
570 }
571
572 let memory_openhcl_type = "memory-openhcl";
579 for (range, result) in walk_ranges(
580 partition_info.partition_ram.iter().map(|r| (r.range, r)),
581 address_space.vtl2_ranges(),
582 ) {
583 match result {
584 RangeWalkResult::Left(entry) => {
585 let name = format_fixed!(64, "memory@{:x}", range.start());
587 openhcl_builder = openhcl_builder
588 .start_node(&name)?
589 .add_str(p_device_type, memory_openhcl_type)?
590 .add_u64_array(p_reg, &[range.start(), range.len()])?
591 .add_u32(p_numa_node_id, entry.vnode)?
592 .add_u32(p_igvm_type, entry.mem_type.0.into())?
593 .add_u32(p_openhcl_memory, MemoryVtlType::VTL0.0)?
594 .end_node()?;
595 }
596 RangeWalkResult::Both(partition_entry, vtl2_type) => {
597 let name = format_fixed!(64, "memory@{:x}", range.start());
599 openhcl_builder = openhcl_builder
600 .start_node(&name)?
601 .add_str(p_device_type, memory_openhcl_type)?
602 .add_u64_array(p_reg, &[range.start(), range.len()])?
603 .add_u32(p_numa_node_id, partition_entry.vnode)?
604 .add_u32(p_igvm_type, partition_entry.mem_type.0.into())?
605 .add_u32(p_openhcl_memory, vtl2_type.0)?
606 .end_node()?;
607 }
608 RangeWalkResult::Right(..) => {
609 panic!("vtl2 range {:?} not contained in partition ram", range)
610 }
611 RangeWalkResult::Neither => {}
613 }
614 }
615
616 for entry in &partition_info.vmbus_vtl0.mmio {
618 let name = format_fixed!(64, "memory@{:x}", entry.start());
619 openhcl_builder = openhcl_builder
620 .start_node(&name)?
621 .add_str(p_device_type, memory_openhcl_type)?
622 .add_u64_array(p_reg, &[entry.start(), entry.len()])?
623 .add_u32(p_openhcl_memory, MemoryVtlType::VTL0_MMIO.0)?
624 .end_node()?;
625 }
626
627 for entry in &partition_info.vmbus_vtl2.mmio {
628 let name = format_fixed!(64, "memory@{:x}", entry.start());
629 openhcl_builder = openhcl_builder
630 .start_node(&name)?
631 .add_str(p_device_type, memory_openhcl_type)?
632 .add_u64_array(p_reg, &[entry.start(), entry.len()])?
633 .add_u32(p_openhcl_memory, MemoryVtlType::VTL2_MMIO.0)?
634 .end_node()?;
635 }
636
637 for range in accepted_ranges {
639 let name = format_fixed!(64, "accepted-memory@{:x}", range.start());
640 openhcl_builder = openhcl_builder
641 .start_node(&name)?
642 .add_u64_array(p_reg, &[range.start(), range.len()])?
643 .end_node()?;
644 }
645
646 if let Some(entropy) = &partition_info.entropy {
649 openhcl_builder = openhcl_builder
650 .start_node("entropy")?
651 .add_prop_array(p_reg, &[entropy])?
652 .end_node()?;
653 }
654
655 let root_builder = openhcl_builder.end_node()?;
656
657 root_builder.end_node()?.build(partition_info.bsp_reg)?;
658 Ok(())
659}