Skip to main content

openvmm_entry/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! This module implements the interactive control process and the entry point
5//! for the worker process.
6
7#![expect(missing_docs)]
8#![forbid(unsafe_code)]
9
10mod cli_args;
11mod crash_dump;
12mod kvp;
13mod meshworker;
14mod pidfile;
15mod repl;
16mod serial_io;
17mod storage_builder;
18mod tracing_init;
19mod ttrpc;
20mod usb;
21mod vm_controller;
22
23// `pub` so that the missing_docs warning fires for options without
24// documentation.
25pub use cli_args::Options;
26use console_relay::ConsoleLaunchOptions;
27
28use crate::cli_args::SecureBootTemplateCli;
29use anyhow::Context;
30use anyhow::bail;
31use chipset_resources::acpi_power_button::PowerButtonRpc;
32use chipset_resources::battery::HostBatteryUpdate;
33use cli_args::DiskCliKind;
34use cli_args::EfiDiagnosticsLogLevelCli;
35use cli_args::EndpointConfigCli;
36use cli_args::IgvmPersonalityCli;
37use cli_args::NicConfigCli;
38use cli_args::ProvisionCondition;
39use cli_args::SerialConfigCli;
40use cli_args::TpmVersionCli;
41use cli_args::UefiConsoleModeCli;
42use cli_args::VirtioBusCli;
43use crash_dump::spawn_dump_handler;
44use cxl::test::CxlTestDeviceHandle;
45use disk_backend_resources::DelayDiskHandle;
46use disk_backend_resources::DiskLayerDescription;
47use disk_backend_resources::layer::DiskLayerHandle;
48use disk_backend_resources::layer::RamDiskLayerHandle;
49use disk_backend_resources::layer::SqliteAutoCacheDiskLayerHandle;
50use disk_backend_resources::layer::SqliteDiskLayerHandle;
51use floppy_resources::FloppyDiskConfig;
52use framebuffer::FRAMEBUFFER_SIZE;
53use framebuffer::FramebufferAccess;
54use futures::AsyncReadExt;
55use futures::AsyncWrite;
56use futures::StreamExt;
57use futures::executor::block_on;
58use futures::io::AllowStdIo;
59use gdma_resources::GdmaDeviceHandle;
60use gdma_resources::VportDefinition;
61use guid::Guid;
62use input_core::MultiplexedInputHandle;
63use inspect::InspectMut;
64use mesh::CancelContext;
65use mesh::CellUpdater;
66use mesh::rpc::RpcSend;
67use meshworker::VmmMesh;
68use net_backend_resources::mac_address::MacAddress;
69use nvme_resources::NvmeControllerRequest;
70use openvmm_defs::config::Config;
71use openvmm_defs::config::DEFAULT_ACPI_POWER_BUTTON_IRQ_X64;
72use openvmm_defs::config::DEFAULT_ACPI_POWER_BUTTON_SPI_AARCH64;
73use openvmm_defs::config::DEFAULT_PCAT_BOOT_ORDER;
74use openvmm_defs::config::DeviceVtl;
75use openvmm_defs::config::HypervisorConfig;
76use openvmm_defs::config::IgvmPersonality;
77use openvmm_defs::config::LateMapVtl0MemoryPolicy;
78use openvmm_defs::config::LoadMode;
79use openvmm_defs::config::MemoryConfig;
80use openvmm_defs::config::NumaDistance;
81use openvmm_defs::config::NumaNode;
82use openvmm_defs::config::NumaTopology;
83use openvmm_defs::config::PcieDeviceConfig;
84use openvmm_defs::config::PcieMmioRangeConfig;
85use openvmm_defs::config::PciePortConfig;
86use openvmm_defs::config::PcieRootComplexConfig;
87use openvmm_defs::config::PcieSwitchConfig;
88use openvmm_defs::config::ProcessorTopologyConfig;
89use openvmm_defs::config::RootComplexCxlConfig;
90use openvmm_defs::config::UartId;
91use openvmm_defs::config::VirtioBus;
92use openvmm_defs::config::VmbusConfig;
93use openvmm_defs::config::VpAssignment;
94use openvmm_defs::config::VpciDeviceConfig;
95use openvmm_defs::config::Vtl2BaseAddressType;
96use openvmm_defs::config::Vtl2Config;
97use openvmm_defs::rpc::VmRpc;
98use openvmm_defs::worker::VM_WORKER;
99use openvmm_defs::worker::VmWorkerParameters;
100use openvmm_helpers::config::supports_acpi_power_button;
101use openvmm_helpers::disk::OpenDiskOptions;
102use openvmm_helpers::disk::create_disk_type;
103use openvmm_helpers::disk::open_disk_type;
104use pal_async::DefaultDriver;
105use pal_async::DefaultPool;
106use pal_async::socket::PolledSocket;
107use pal_async::task::Spawn;
108use pal_async::task::Task;
109use serial_16550_resources::ComPort;
110use serial_core::resources::DisconnectedSerialBackendHandle;
111use serial_core::resources::NullSerialBackendHandle;
112use sparse_mmap::alloc_shared_memory;
113use std::cell::RefCell;
114use std::collections::BTreeMap;
115use std::collections::HashSet;
116use std::fmt::Write as _;
117use std::io;
118#[cfg(unix)]
119use std::io::IsTerminal;
120use std::io::Write;
121use std::net::TcpListener;
122use std::path::Path;
123use std::path::PathBuf;
124use std::sync::Arc;
125use std::thread;
126use std::time::Duration;
127use storvsp_resources::ScsiControllerRequest;
128use tpm_resources::TpmDeviceHandle;
129use tpm_resources::TpmRegisterLayout;
130use tpm_resources::TpmVersion;
131use uidevices_resources::SynthKeyboardHandle;
132use uidevices_resources::SynthMouseHandle;
133use uidevices_resources::SynthVideoHandle;
134use video_core::SharedFramebufferHandle;
135use virtio_resources::VirtioPciDeviceHandle;
136use vm_manifest_builder::BaseChipsetType;
137use vm_manifest_builder::MachineArch;
138use vm_manifest_builder::VmChipsetResult;
139use vm_manifest_builder::VmManifestBuilder;
140use vm_resource::IntoResource;
141use vm_resource::Resource;
142use vm_resource::kind::DiskHandleKind;
143use vm_resource::kind::DiskLayerHandleKind;
144use vm_resource::kind::NetEndpointHandleKind;
145use vm_resource::kind::NonVolatileStoreKind;
146use vm_resource::kind::VirtioDeviceHandle;
147use vm_resource::kind::VmbusDeviceHandleKind;
148use vmbus_serial_resources::VmbusSerialDeviceHandle;
149use vmbus_serial_resources::VmbusSerialPort;
150use vmcore::non_volatile_store::resources::EphemeralNonVolatileStoreHandle;
151use vmgs_resources::GuestStateEncryptionPolicy;
152use vmgs_resources::VmgsDisk;
153use vmgs_resources::VmgsFileHandle;
154use vmgs_resources::VmgsResource;
155use vmotherboard::ChipsetDeviceHandle;
156use vnc_worker_defs::VncParameters;
157
158pub fn openvmm_main() {
159    // Save the current state of the terminal so we can restore it back to
160    // normal before exiting.
161    #[cfg(unix)]
162    let orig_termios = io::stderr().is_terminal().then(term::get_termios);
163
164    let mut pidfile_guard: Option<pidfile::Pidfile> = None;
165    let exit_code = match do_main(&mut pidfile_guard) {
166        Ok(code) => code,
167        Err(err) => {
168            eprintln!("fatal error: {:?}", err);
169            1
170        }
171    };
172
173    // Restore the terminal to its initial state.
174    #[cfg(unix)]
175    if let Some(orig_termios) = orig_termios {
176        term::set_termios(orig_termios);
177    }
178
179    // Clean up the pidfile before terminating, since
180    // pal::process::terminate skips destructors.
181    drop(pidfile_guard);
182
183    // Terminate the process immediately without graceful shutdown of DLLs or
184    // C++ destructors or anything like that. This is all unnecessary and saves
185    // time on Windows.
186    //
187    // Do flush stdout, though, since there may be buffered data.
188    let _ = io::stdout().flush();
189    pal::process::terminate(exit_code);
190}
191
192#[derive(Default)]
193struct VmResources {
194    console_in: Option<Box<dyn AsyncWrite + Send + Unpin>>,
195    /// Keeps the dedicated serial reactor alive while serial I/O objects exist.
196    serial_driver: Option<DefaultDriver>,
197    framebuffer_access: Option<FramebufferAccess>,
198    shutdown_ic: Option<mesh::Sender<hyperv_ic_resources::shutdown::ShutdownRpc>>,
199    acpi_power_button: Option<mesh::Sender<PowerButtonRpc>>,
200    kvp_ic: Option<mesh::Sender<hyperv_ic_resources::kvp::KvpConnectRpc>>,
201    scsi_rpc: Option<mesh::Sender<ScsiControllerRequest>>,
202    nvme_vtl2_rpc: Option<mesh::Sender<NvmeControllerRequest>>,
203    consomme_rpc: Option<mesh::Sender<net_backend_resources::consomme::ConsommeRequest>>,
204    ged_rpc: Option<mesh::Sender<get_resources::ged::GuestEmulationRequest>>,
205    vtl2_settings: Option<vtl2_settings_proto::Vtl2Settings>,
206    /// Receives dirty rectangles from the synthetic video device for the VNC worker.
207    dirty_rect_recv: Option<mesh::Receiver<Vec<video_core::DirtyRect>>>,
208    #[cfg(windows)]
209    switch_ports: Vec<vmswitch::kernel::SwitchPort>,
210}
211
212struct ConsoleState<'a> {
213    device: &'a str,
214    input: Box<dyn AsyncWrite + Unpin + Send>,
215}
216
217/// Builds controller configurations from the USB command-line options. The
218/// controller resolver assigns omitted connectors and validates connectors.
219fn usb_controller_configs(
220    pcie_ports: &[cli_args::UsbXhciCli],
221    devices: &[cli_args::UsbDeviceCli],
222) -> anyhow::Result<Vec<(String, usb_config_resources::UsbControllerConfig)>> {
223    let mut controllers: Vec<_> = pcie_ports
224        .iter()
225        .map(|pcie_port| usb::UsbControllerSpec {
226            port_name: pcie_port.port_name.clone(),
227            devices: Vec::new(),
228            winusb_all: false,
229        })
230        .collect();
231    let default_pcie_port = if let [pcie_port] = pcie_ports {
232        Some(pcie_port.port_name.as_str())
233    } else {
234        None
235    };
236    for device in devices {
237        let pcie_port_name = device
238            .controller_pcie_port()
239            .or(default_pcie_port)
240            .context("on=PCIE_PORT is required unless exactly one xHCI controller is declared")?;
241        let controller = controllers
242            .iter_mut()
243            .find(|controller| controller.port_name == pcie_port_name)
244            .with_context(|| {
245                format!(
246                    "USB device references undeclared controller on PCIe port {pcie_port_name:?}"
247                )
248            })?;
249        match device {
250            cli_args::UsbDeviceCli::Device { location, backend } => controller.devices.push((
251                location.connector.map(cli_args::UsbConnectorCli::number),
252                backend.clone(),
253            )),
254            cli_args::UsbDeviceCli::WinUsbAll { .. } => {
255                anyhow::ensure!(
256                    !controller.winusb_all,
257                    "winusb-all may be selected only once per VM"
258                );
259                controller.winusb_all = true;
260            }
261        }
262    }
263    usb::controller_configs(controllers)
264}
265
266/// Build a flat list of switches with their parent port assignments.
267///
268/// This function converts hierarchical CLI switch definitions into a flat list
269/// where each switch specifies its parent port directly.
270fn build_switch_list(all_switches: &[cli_args::GenericPcieSwitchCli]) -> Vec<PcieSwitchConfig> {
271    all_switches
272        .iter()
273        .map(|switch_cli| PcieSwitchConfig {
274            name: switch_cli.name.clone(),
275            parent_port: switch_cli.port_name.clone(),
276            ports: (0..switch_cli.num_downstream_ports)
277                .map(|i| PciePortConfig {
278                    name: format!("{}-downstream-{}", switch_cli.name, i),
279                    devfn: None,
280                    hotplug: switch_cli.hotplug,
281                    acs_capabilities_supported: switch_cli.acs_capabilities_supported,
282                    cxl: false,
283                    pasid: switch_cli.pasid,
284                })
285                .collect(),
286        })
287        .collect()
288}
289
290fn base_chipset_type(opt: &Options) -> BaseChipsetType {
291    if let Some(igvm) = &opt.igvm {
292        match igvm.personality {
293            IgvmPersonalityCli::Openhcl => BaseChipsetType::HclHost,
294            IgvmPersonalityCli::Uefi => BaseChipsetType::HypervGen2Uefi,
295            IgvmPersonalityCli::LinuxDirect if opt.isolation.is_some() => {
296                BaseChipsetType::EnlightenedLinuxDirect
297            }
298            IgvmPersonalityCli::LinuxDirect if opt.hv => BaseChipsetType::HyperVGen2LinuxDirect,
299            IgvmPersonalityCli::LinuxDirect => BaseChipsetType::UnenlightenedLinuxDirect,
300        }
301    } else if matches!(opt.isolation, Some(cli_args::IsolationCli::Snp)) {
302        BaseChipsetType::EnlightenedLinuxDirect
303    } else if opt.pcat {
304        BaseChipsetType::HypervGen1
305    } else if opt.uefi.is_some() {
306        BaseChipsetType::HypervGen2Uefi
307    } else if opt.hv {
308        BaseChipsetType::HyperVGen2LinuxDirect
309    } else {
310        BaseChipsetType::UnenlightenedLinuxDirect
311    }
312}
313
314/// Build the loader's [`SmbiosConfig`](openvmm_defs::config::SmbiosConfig) from
315/// the parsed `--smbios` arguments.
316///
317/// Multiple `--smbios` arguments are merged (erroring on a field set twice).
318/// String overrides left unset fall through to the loader's default identity.
319/// The system UUID defaults to the all-zero GUID unless overridden with
320/// `uuid=GUID`; `uuid=random` requests a freshly generated per-VM GUID.
321fn smbios_config_from_cli(
322    args: &[cli_args::SmbiosCli],
323) -> anyhow::Result<openvmm_defs::config::SmbiosConfig> {
324    let mut merged = cli_args::SmbiosCli::default();
325    for arg in args {
326        merged.merge(arg.clone())?;
327    }
328    let cli_args::SmbiosCli {
329        bios:
330            cli_args::SmbiosBiosCli {
331                vendor: bios_vendor,
332                version: bios_version,
333                release_date: bios_release_date,
334                release: bios_release,
335            },
336        system:
337            cli_args::SmbiosSystemCli {
338                manufacturer: system_manufacturer,
339                product_name: system_product,
340                version: system_version,
341                serial_number: system_serial,
342                sku_number: system_sku,
343                family: system_family,
344                uuid: system_uuid,
345            },
346    } = merged;
347    Ok(openvmm_defs::config::SmbiosConfig {
348        bios: openvmm_defs::config::SmbiosBiosOverrides {
349            vendor: bios_vendor,
350            version: bios_version,
351            release_date: bios_release_date,
352            release: bios_release.map(|r| (r.0, r.1)),
353        },
354        system: openvmm_defs::config::SmbiosSystemOverrides {
355            manufacturer: system_manufacturer,
356            product_name: system_product,
357            version: system_version,
358            serial_number: system_serial,
359            sku_number: system_sku,
360            family: system_family,
361            uuid: match system_uuid {
362                None => Guid::ZERO,
363                Some(cli_args::SmbiosUuid::Random) => Guid::new_random(),
364                Some(cli_args::SmbiosUuid::Fixed(guid)) => guid,
365            },
366        },
367    })
368}
369
370async fn vm_config_from_command_line(
371    spawner: impl Spawn,
372    mesh: &VmmMesh,
373    opt: &Options,
374) -> anyhow::Result<(Config, VmResources)> {
375    opt.validate_isolation_options()?;
376    opt.validate_igvm_options()?;
377
378    let (_, serial_driver) = DefaultPool::spawn_on_thread("serial");
379    let uefi = opt.effective_uefi()?;
380    let default_uefi = cli_args::UefiCli::default();
381    let uefi_options = uefi.as_ref().unwrap_or(&default_uefi);
382
383    let openhcl_vtl = if opt.vtl2 {
384        DeviceVtl::Vtl2
385    } else {
386        DeviceVtl::Vtl0
387    };
388
389    let console_state: RefCell<Option<ConsoleState<'_>>> = RefCell::new(None);
390    let setup_serial = |name: &str, cli_cfg, device| -> anyhow::Result<_> {
391        Ok(match cli_cfg {
392            SerialConfigCli::Console => {
393                if let Some(console_state) = console_state.borrow().as_ref() {
394                    bail!("console already set by {}", console_state.device);
395                }
396                let (config, serial) = serial_io::anonymous_serial_pair(&serial_driver)?;
397                let (serial_read, serial_write) = AsyncReadExt::split(serial);
398                *console_state.borrow_mut() = Some(ConsoleState {
399                    device,
400                    input: Box::new(serial_write),
401                });
402                thread::Builder::new()
403                    .name(name.to_owned())
404                    .spawn(move || {
405                        let _ = block_on(futures::io::copy(
406                            serial_read,
407                            &mut AllowStdIo::new(term::raw_stdout()),
408                        ));
409                    })
410                    .unwrap();
411                Some(config)
412            }
413            SerialConfigCli::Stderr => {
414                let (config, serial) = serial_io::anonymous_serial_pair(&serial_driver)?;
415                thread::Builder::new()
416                    .name(name.to_owned())
417                    .spawn(move || {
418                        let _ = block_on(futures::io::copy(
419                            serial,
420                            &mut AllowStdIo::new(term::raw_stderr()),
421                        ));
422                    })
423                    .unwrap();
424                Some(config)
425            }
426            SerialConfigCli::File(path) => Some(serial_io::file_serial(&path, false)?),
427            SerialConfigCli::None => None,
428            SerialConfigCli::Null => Some(NullSerialBackendHandle.into_resource()),
429            SerialConfigCli::Pipe(path) => {
430                Some(serial_io::bind_serial(&path).context("failed to bind serial")?)
431            }
432            SerialConfigCli::Tcp(addr) => {
433                Some(serial_io::bind_tcp_serial(&addr).context("failed to bind serial")?)
434            }
435            SerialConfigCli::NewConsole(app, window_title) => {
436                let path = console_relay::random_console_path();
437                let config =
438                    serial_io::bind_serial(&path).context("failed to bind console serial")?;
439                let window_title =
440                    window_title.unwrap_or_else(|| name.to_uppercase() + " [OpenVMM]");
441
442                console_relay::launch_console(
443                    app.or_else(openvmm_terminal_app).as_deref(),
444                    &path,
445                    ConsoleLaunchOptions {
446                        window_title: Some(window_title),
447                    },
448                )
449                .context("failed to launch console")?;
450
451                Some(config)
452            }
453        })
454    };
455
456    let mut vmbus_devices = Vec::new();
457
458    let com_debugger_mode = [
459        opt.com1.as_ref().is_some_and(|c| c.debugger_mode),
460        opt.com2.as_ref().is_some_and(|c| c.debugger_mode),
461        opt.com3.as_ref().is_some_and(|c| c.debugger_mode),
462        opt.com4.as_ref().is_some_and(|c| c.debugger_mode),
463    ];
464
465    let [serial0_name, serial1_name, serial2_name, serial3_name] = cfg_select! {
466        guest_arch = "x86_64" => ["ttyS0", "ttyS1", "ttyS2", "ttyS3"],
467        guest_arch = "aarch64" => ["ttyAMA0", "ttyAMA1", "ttyAMA2", "ttyAMA3"],
468    };
469    let serial0_cfg = setup_serial(
470        "com1",
471        opt.com1
472            .clone()
473            .map_or(SerialConfigCli::Console, |c| c.backend),
474        serial0_name,
475    )?;
476    let serial1_cfg = setup_serial(
477        "com2",
478        opt.com2
479            .clone()
480            .map_or(SerialConfigCli::None, |c| c.backend),
481        serial1_name,
482    )?;
483    let serial2_cfg = setup_serial(
484        "com3",
485        opt.com3
486            .clone()
487            .map_or(SerialConfigCli::None, |c| c.backend),
488        serial2_name,
489    )?;
490    let serial3_cfg = setup_serial(
491        "com4",
492        opt.com4
493            .clone()
494            .map_or(SerialConfigCli::None, |c| c.backend),
495        serial3_name,
496    )?;
497    let with_vmbus_com1_serial = if let Some(vmbus_com1_cfg) = setup_serial(
498        "vmbus_com1",
499        opt.vmbus_com1_serial
500            .clone()
501            .unwrap_or(SerialConfigCli::None),
502        "vmbus_com1",
503    )? {
504        vmbus_devices.push((
505            openhcl_vtl,
506            VmbusSerialDeviceHandle {
507                port: VmbusSerialPort::Com1,
508                backend: vmbus_com1_cfg,
509            }
510            .into_resource(),
511        ));
512        true
513    } else {
514        false
515    };
516    let with_vmbus_com2_serial = if let Some(vmbus_com2_cfg) = setup_serial(
517        "vmbus_com2",
518        opt.vmbus_com2_serial
519            .clone()
520            .unwrap_or(SerialConfigCli::None),
521        "vmbus_com2",
522    )? {
523        vmbus_devices.push((
524            openhcl_vtl,
525            VmbusSerialDeviceHandle {
526                port: VmbusSerialPort::Com2,
527                backend: vmbus_com2_cfg,
528            }
529            .into_resource(),
530        ));
531        true
532    } else {
533        false
534    };
535    let debugcon_cfg = setup_serial(
536        "debugcon",
537        opt.debugcon
538            .clone()
539            .map(|cfg| cfg.serial)
540            .unwrap_or(SerialConfigCli::None),
541        "debugcon",
542    )?;
543
544    let virtio_console_backend = if let Some(serial_cfg) = opt.virtio_console.clone() {
545        setup_serial("virtio-console", serial_cfg, "hvc0")?
546    } else {
547        None
548    };
549
550    let mut resources = VmResources::default();
551    let mut console_str = "";
552    if let Some(ConsoleState { device, input }) = console_state.into_inner() {
553        resources.console_in = Some(input);
554        console_str = device;
555    }
556
557    if opt.shared_memory {
558        tracing::warn!("--shared-memory/-M flag has no effect and will be removed");
559    }
560    if opt.deprecated_prefetch {
561        tracing::warn!("--prefetch is deprecated; use --memory prefetch=on");
562    }
563    if opt.deprecated_private_memory {
564        tracing::warn!("--private-memory is deprecated; use --memory shared=off");
565    }
566    if opt.deprecated_thp {
567        tracing::warn!("--thp is deprecated; use --memory shared=off,thp=on");
568    }
569    if opt.deprecated_memory_backing_file.is_some() {
570        tracing::warn!("--memory-backing-file is deprecated; use --memory file=<path>");
571    }
572
573    opt.validate_memory_options()?;
574
575    const MAX_PROCESSOR_COUNT: u32 = 1024;
576
577    if opt.processors == 0 || opt.processors > MAX_PROCESSOR_COUNT {
578        bail!("invalid proc count: {}", opt.processors);
579    }
580
581    // Total SCSI channel count should not exceed the processor count
582    // (at most, one channel per VP).
583    if opt.scsi_sub_channels > (MAX_PROCESSOR_COUNT - 1) as u16 {
584        bail!(
585            "invalid SCSI sub-channel count: requested {}, max {}",
586            opt.scsi_sub_channels,
587            MAX_PROCESSOR_COUNT - 1
588        );
589    }
590
591    let with_get = opt.get || (opt.vtl2 && !opt.no_get);
592
593    let mut storage = storage_builder::StorageBuilder::new(with_get.then_some(openhcl_vtl));
594
595    // Register named controllers first, so that --disk on=<name>
596    // references can be resolved.
597    for ctrl in &opt.nvme_pci {
598        let transport = match &ctrl.transport {
599            cli_args::NvmeControllerTransport::Pcie(port) => {
600                storage_builder::NvmeControllerTransport::Pcie(port.clone())
601            }
602            cli_args::NvmeControllerTransport::Vpci(guid) => {
603                let guid = guid.unwrap_or_else(|| storage_builder::deterministic_guid(&ctrl.id));
604                storage_builder::NvmeControllerTransport::Vpci(guid)
605            }
606        };
607        storage.add_nvme_controller(ctrl.id.clone(), ctrl.vtl, transport, None)?;
608    }
609
610    for ctrl in &opt.vmbus_scsi {
611        let instance_id = storage_builder::deterministic_guid(&ctrl.id);
612        storage.add_scsi_controller(ctrl.id.clone(), ctrl.vtl, instance_id, ctrl.sub_channels)?;
613    }
614
615    for ctrl in &opt.openhcl_controller {
616        let controller_type = match ctrl.controller_type {
617            cli_args::OpenhclControllerType::Scsi => storage_builder::OpenhclControllerType::Scsi,
618            cli_args::OpenhclControllerType::Nvme => storage_builder::OpenhclControllerType::Nvme,
619        };
620        let instance_id = ctrl
621            .guid
622            .unwrap_or_else(|| storage_builder::deterministic_guid(&ctrl.id));
623        storage.add_openhcl_controller(ctrl.id.clone(), controller_type, instance_id)?;
624    }
625
626    for &cli_args::DiskCli {
627        vtl,
628        ref kind,
629        read_only,
630        is_dvd,
631        underhill,
632        ref pcie_port,
633        ref serial,
634        num_queues,
635        ref controller,
636        nsid,
637        lun,
638        ref relay,
639    } in &opt.disk
640    {
641        if serial.is_some() {
642            anyhow::bail!("`serial` is only supported by `--virtio-blk`");
643        }
644        if num_queues.is_some() {
645            anyhow::bail!("`num_queues` is only supported by `--virtio-blk`");
646        }
647        if controller.is_none() && underhill.is_none() && relay.is_none() {
648            tracing::warn!(
649                "--disk without `on` is deprecated; \
650                 use --vmbus-scsi and --disk on=<name> instead"
651            );
652        }
653
654        let relay_target = relay
655            .as_ref()
656            .map(|(name, loc)| storage_builder::RelayTarget {
657                controller: name.clone(),
658                location: *loc,
659            });
660
661        let target = if let Some(name) = controller {
662            if pcie_port.is_some() {
663                anyhow::bail!("`on` is incompatible with `pcie_port` on `--disk`");
664            }
665            storage_builder::DiskLocation::Named {
666                controller: name.clone(),
667                nsid,
668                lun,
669            }
670        } else if pcie_port.is_some() {
671            anyhow::bail!("`--disk` is incompatible with `pcie_port` without `controller`");
672        } else {
673            if opt.no_vmbus {
674                anyhow::bail!(
675                    "`--disk` without `on=` attaches to the default VMBus SCSI controller and \
676                     cannot be used with `--no-vmbus`; use `on=<name>` to attach to a named controller"
677                );
678            }
679            storage_builder::DiskLocation::Scsi(None)
680        };
681
682        storage
683            .add(
684                vtl,
685                underhill,
686                relay_target,
687                target,
688                kind,
689                is_dvd,
690                read_only,
691            )
692            .await?;
693    }
694
695    for &cli_args::IdeDiskCli {
696        ref kind,
697        read_only,
698        channel,
699        device,
700        is_dvd,
701    } in &opt.ide
702    {
703        storage
704            .add(
705                DeviceVtl::Vtl0,
706                None,
707                None,
708                storage_builder::DiskLocation::Ide(channel, device),
709                kind,
710                is_dvd,
711                read_only,
712            )
713            .await?;
714    }
715
716    if !opt.nvme.is_empty() {
717        tracing::warn!("--nvme is deprecated; use --nvme-pci and --disk on=<name> instead");
718
719        // Pre-register implicit PCIe controllers for unique port names.
720        let mut registered_ports = std::collections::BTreeSet::new();
721        for disk in &opt.nvme {
722            if let Some(port) = &disk.pcie_port {
723                if registered_ports.insert(port.clone()) {
724                    storage.add_nvme_controller(
725                        port.clone(),
726                        DeviceVtl::Vtl0,
727                        storage_builder::NvmeControllerTransport::Pcie(port.clone()),
728                        None,
729                    ).with_context(|| format!(
730                        "legacy --nvme flag conflicts with an explicit controller named '{port}'; \
731                         use --nvme-pci and --disk on=<name> instead"
732                    ))?;
733                }
734            }
735        }
736    }
737
738    for &cli_args::DiskCli {
739        vtl,
740        ref kind,
741        read_only,
742        is_dvd,
743        underhill,
744        ref pcie_port,
745        ref serial,
746        num_queues,
747        controller: _,
748        nsid: _,
749        lun: _,
750        relay: _,
751    } in &opt.nvme
752    {
753        if serial.is_some() {
754            anyhow::bail!("`serial` is only supported by `--virtio-blk`");
755        }
756        if num_queues.is_some() {
757            anyhow::bail!("`num_queues` is only supported by `--virtio-blk`");
758        }
759        let target = if let Some(port) = pcie_port {
760            storage_builder::DiskLocation::Named {
761                controller: port.clone(),
762                nsid: None,
763                lun: None,
764            }
765        } else {
766            storage_builder::DiskLocation::Nvme(None)
767        };
768        storage
769            .add(vtl, underhill, None, target, kind, is_dvd, read_only)
770            .await?;
771    }
772
773    for &cli_args::DiskCli {
774        vtl,
775        ref kind,
776        read_only,
777        is_dvd,
778        ref underhill,
779        ref pcie_port,
780        ref serial,
781        num_queues,
782        controller: _,
783        nsid: _,
784        lun: _,
785        relay: _,
786    } in &opt.virtio_blk
787    {
788        if underhill.is_some() {
789            anyhow::bail!("underhill not supported with virtio-blk");
790        }
791        storage
792            .add(
793                vtl,
794                None,
795                None,
796                storage_builder::DiskLocation::VirtioBlk {
797                    pcie_port: pcie_port.clone(),
798                    serial: serial.clone(),
799                    options: virtio_resources::blk::VirtioBlkOptions {
800                        queue_count: num_queues.unwrap_or(opt.virtio_blk_queues),
801                        io_depth: opt.virtio_blk_io_depth,
802                    },
803                },
804                kind,
805                is_dvd,
806                read_only,
807            )
808            .await?;
809    }
810
811    let mut floppy_disks = Vec::new();
812    for disk in &opt.floppy {
813        let &cli_args::FloppyDiskCli {
814            ref kind,
815            read_only,
816        } = disk;
817        floppy_disks.push(FloppyDiskConfig {
818            disk_type: disk_open(kind, read_only).await?,
819            read_only,
820        });
821    }
822
823    let mut vpci_mana_nics = [(); 3].map(|()| None);
824    let mut pcie_mana_nics = BTreeMap::<String, GdmaDeviceHandle>::new();
825    let mut underhill_nics = Vec::new();
826    let mut vpci_devices = Vec::new();
827
828    let mut nic_index = 0;
829    for cli_cfg in &opt.net {
830        if cli_cfg.pcie_port.is_some() {
831            anyhow::bail!("`--net` does not support PCIe");
832        }
833        let vport = parse_endpoint(cli_cfg, &mut nic_index, &mut resources)?;
834        if cli_cfg.underhill {
835            if !opt.no_alias_map {
836                anyhow::bail!("must specify --no-alias-map to offer NICs to VTL2");
837            }
838            let mana = vpci_mana_nics[openhcl_vtl as usize].get_or_insert_with(|| {
839                let vpci_instance_id = Guid::new_random();
840                underhill_nics.push(vtl2_settings_proto::NicDeviceLegacy {
841                    instance_id: vpci_instance_id.to_string(),
842                    subordinate_instance_id: None,
843                    max_sub_channels: None,
844                });
845                (vpci_instance_id, GdmaDeviceHandle { vports: Vec::new() })
846            });
847            mana.1.vports.push(VportDefinition {
848                mac_address: vport.mac_address,
849                endpoint: vport.endpoint,
850            });
851        } else {
852            vmbus_devices.push(vport.into_netvsp_handle());
853        }
854    }
855
856    if opt.nic {
857        let nic_config = parse_endpoint(
858            &NicConfigCli {
859                vtl: DeviceVtl::Vtl0,
860                endpoint: EndpointConfigCli::Consomme {
861                    cidr: None,
862                    host_fwd: Vec::new(),
863                },
864                mac_address: None,
865                max_queues: None,
866                enable_rx_gso: false,
867                underhill: false,
868                pcie_port: None,
869            },
870            &mut nic_index,
871            &mut resources,
872        )?;
873        vmbus_devices.push(nic_config.into_netvsp_handle());
874    }
875
876    // Build initial PCIe devices list from CLI options. Storage devices
877    // (e.g., NVMe controllers on PCIe ports) are added later by storage_builder.
878    let mut pcie_devices = Vec::new();
879    for (pcie_port_name, config) in usb_controller_configs(&opt.usb_xhci, &opt.usb_device)? {
880        pcie_devices.push(PcieDeviceConfig {
881            port_name: pcie_port_name,
882            resource: config.into_resource(),
883        });
884    }
885    for (index, cli_cfg) in opt.pcie_remote.iter().enumerate() {
886        tracing::info!(
887            port_name = %cli_cfg.port_name,
888            socket_addr = ?cli_cfg.socket_addr,
889            "instantiating PCIe remote device"
890        );
891
892        // Generate a deterministic instance ID based on index
893        const PCIE_REMOTE_BASE_INSTANCE_ID: Guid =
894            guid::guid!("28ed784d-c059-429f-9d9a-46bea02562c0");
895        let instance_id = Guid {
896            data1: index as u32,
897            ..PCIE_REMOTE_BASE_INSTANCE_ID
898        };
899
900        pcie_devices.push(PcieDeviceConfig {
901            port_name: cli_cfg.port_name.clone(),
902            resource: pcie_remote_resources::PcieRemoteHandle {
903                instance_id,
904                socket_addr: cli_cfg.socket_addr.clone(),
905                hu: cli_cfg.hu,
906                controller: cli_cfg.controller,
907            }
908            .into_resource(),
909        });
910    }
911
912    #[cfg(windows)]
913    let mut kernel_vmnics = Vec::new();
914    #[cfg(windows)]
915    for (index, switch_id) in opt.kernel_vmnic.iter().enumerate() {
916        // Pick a random MAC address.
917        let mut mac_address = [0x00, 0x15, 0x5D, 0, 0, 0];
918        getrandom::fill(&mut mac_address[3..]).expect("rng failure");
919
920        // Pick a fixed instance ID based on the index.
921        const BASE_INSTANCE_ID: Guid = guid::guid!("00000000-435d-11ee-9f59-00155d5016fc");
922        let instance_id = Guid {
923            data1: index as u32,
924            ..BASE_INSTANCE_ID
925        };
926
927        let switch_id = if switch_id == "default" {
928            None
929        } else {
930            Some(switch_id.as_str())
931        };
932        let (port_id, port) = new_switch_port(switch_id)?;
933        resources.switch_ports.push(port);
934
935        kernel_vmnics.push(openvmm_defs::config::KernelVmNicConfig {
936            instance_id,
937            mac_address: mac_address.into(),
938            switch_port_id: port_id,
939        });
940    }
941
942    for vport in &opt.mana {
943        let vport = parse_endpoint(vport, &mut nic_index, &mut resources)?;
944        let vport_array = match (vport.vtl as usize, vport.pcie_port) {
945            (vtl, None) => {
946                &mut vpci_mana_nics[vtl]
947                    .get_or_insert_with(|| {
948                        (Guid::new_random(), GdmaDeviceHandle { vports: Vec::new() })
949                    })
950                    .1
951                    .vports
952            }
953            (0, Some(pcie_port)) => {
954                &mut pcie_mana_nics
955                    .entry(pcie_port)
956                    .or_insert(GdmaDeviceHandle { vports: Vec::new() })
957                    .vports
958            }
959            _ => anyhow::bail!("PCIe NICs only supported to VTL0"),
960        };
961        vport_array.push(VportDefinition {
962            mac_address: vport.mac_address,
963            endpoint: vport.endpoint,
964        });
965    }
966
967    vpci_devices.extend(
968        vpci_mana_nics
969            .into_iter()
970            .enumerate()
971            .filter_map(|(vtl, nic)| {
972                nic.map(|(instance_id, handle)| VpciDeviceConfig {
973                    vtl: match vtl {
974                        0 => DeviceVtl::Vtl0,
975                        1 => DeviceVtl::Vtl1,
976                        2 => DeviceVtl::Vtl2,
977                        _ => unreachable!(),
978                    },
979                    instance_id,
980                    resource: handle.into_resource(),
981                    vnode: None,
982                })
983            }),
984    );
985
986    pcie_devices.extend(
987        pcie_mana_nics
988            .into_iter()
989            .map(|(pcie_port, handle)| PcieDeviceConfig {
990                port_name: pcie_port,
991                resource: handle.into_resource(),
992            }),
993    );
994
995    for cxl_test in &opt.cxl_test {
996        pcie_devices.push(PcieDeviceConfig {
997            port_name: cxl_test.pcie_port.clone(),
998            resource: CxlTestDeviceHandle {
999                hdm_size_bytes: cxl_test.hdm_size,
1000            }
1001            .into_resource(),
1002        });
1003    }
1004
1005    let arch = cfg_select! {
1006        guest_arch = "x86_64" => MachineArch::X86_64,
1007        guest_arch = "aarch64" => MachineArch::Aarch64,
1008    };
1009
1010    cfg_select! {
1011        guest_arch = "x86_64" => {
1012            anyhow::ensure!(
1013                opt.amd_iommu.is_empty() || opt.intel_vtd.is_empty(),
1014                "--amd-iommu and --intel-vtd cannot both be used in the same VM"
1015            );
1016
1017            let mut amd_iommu_names: HashSet<&str> =
1018                opt.amd_iommu.iter().map(|s| s.as_str()).collect();
1019            let mut vtd_names: HashSet<&str> = opt.intel_vtd.iter().map(|s| s.as_str()).collect();
1020        }
1021        guest_arch = "aarch64" => {
1022            // Map each `--smmu` entry to its root complex, rejecting duplicate `rc=`
1023            // entries up front. Entries are removed as they are matched to a root
1024            // complex below; any left over refer to unknown root complexes.
1025            let mut smmu_names: std::collections::HashMap<&str, &cli_args::SmmuCli> = {
1026                let mut map = std::collections::HashMap::new();
1027                for s in &opt.smmu {
1028                    if map.insert(s.rc_name.as_str(), s).is_some() {
1029                        anyhow::bail!(
1030                            "--smmu specified multiple times for root complex '{}'",
1031                            s.rc_name
1032                        );
1033                    }
1034                }
1035                map
1036            };
1037        }
1038    }
1039    let mut pcie_root_complexes = Vec::new();
1040    let mut legacy_pci_config_io_for_pcie = false;
1041    for (i, rc_cli) in opt.pcie_root_complex.iter().enumerate() {
1042        let ports: Vec<PciePortConfig> = opt
1043            .pcie_root_port
1044            .iter()
1045            .filter(|port_cli| port_cli.root_complex_name == rc_cli.name)
1046            .map(|port_cli| PciePortConfig {
1047                name: port_cli.name.clone(),
1048                devfn: port_cli.devfn,
1049                hotplug: port_cli.hotplug,
1050                acs_capabilities_supported: port_cli.acs_capabilities_supported,
1051                cxl: port_cli.cxl,
1052                pasid: port_cli.pasid,
1053            })
1054            .collect();
1055
1056        const ONE_MB: u64 = 1024 * 1024;
1057        // Keep all PCI windows 1MB-granular to match layout and downstream placement rules.
1058        let low_mmio_size = (rc_cli.low_mmio as u64).next_multiple_of(ONE_MB);
1059        let high_mmio_size = rc_cli
1060            .high_mmio
1061            .checked_next_multiple_of(ONE_MB)
1062            .context("high mmio rounding error")?;
1063
1064        // Count CXL-capable ports under the root bus. If the root bus has CXL root ports, it needs CHBCR.
1065        let cxl_port_count = ports.iter().filter(|port| port.cxl).count() as u64;
1066
1067        let cxl = if cxl_port_count != 0 {
1068            Some(RootComplexCxlConfig {
1069                hdm_size: rc_cli.hdm,
1070                hdm_window_restrictions: rc_cli.hdm_window_restrictions.bits(),
1071            })
1072        } else {
1073            None
1074        };
1075        let legacy_pci_config_io = arch == MachineArch::X86_64
1076            && !opt.pcat
1077            && rc_cli.segment == 0
1078            && rc_cli.start_bus == 0
1079            && rc_cli.end_bus == u8::MAX;
1080        legacy_pci_config_io_for_pcie |= legacy_pci_config_io;
1081        pcie_root_complexes.push(PcieRootComplexConfig {
1082            index: i as u32,
1083            name: rc_cli.name.clone(),
1084            segment: rc_cli.segment,
1085            start_bus: rc_cli.start_bus,
1086            end_bus: rc_cli.end_bus,
1087            legacy_pci_config_io,
1088            low_mmio: if let Some(base) = rc_cli.low_mmio_base {
1089                PcieMmioRangeConfig::Fixed(
1090                    memory_range::MemoryRange::try_new(base..base.wrapping_add(low_mmio_size))
1091                        .context("invalid low MMIO range")?,
1092                )
1093            } else {
1094                PcieMmioRangeConfig::Dynamic {
1095                    size: low_mmio_size,
1096                }
1097            },
1098            high_mmio: if let Some(base) = rc_cli.high_mmio_base {
1099                PcieMmioRangeConfig::Fixed(
1100                    memory_range::MemoryRange::try_new(base..base.wrapping_add(high_mmio_size))
1101                        .context("invalid high MMIO range")?,
1102                )
1103            } else {
1104                PcieMmioRangeConfig::Dynamic {
1105                    size: high_mmio_size,
1106                }
1107            },
1108            cxl,
1109            ports,
1110            #[cfg(guest_arch = "aarch64")]
1111            iommu: smmu_names.remove(rc_cli.name.as_str()).map(|s| {
1112                openvmm_defs::config::PcieIommuConfig::Smmu {
1113                    accel: s.accel,
1114                    oas: match s.oas {
1115                        cli_args::SmmuOasCli::Auto => openvmm_defs::config::SmmuOas::Auto,
1116                        cli_args::SmmuOasCli::Fixed(bits) => {
1117                            openvmm_defs::config::SmmuOas::Fixed(bits)
1118                        }
1119                    },
1120                    ssidsize: match s.ssidsize {
1121                        cli_args::SmmuSsidSizeCli::Auto => openvmm_defs::config::SmmuSsidSize::Auto,
1122                        cli_args::SmmuSsidSizeCli::Fixed(bits) => {
1123                            openvmm_defs::config::SmmuSsidSize::Fixed(bits)
1124                        }
1125                    },
1126                }
1127            }),
1128            #[cfg(guest_arch = "x86_64")]
1129            iommu: if amd_iommu_names.remove(rc_cli.name.as_str()) {
1130                Some(openvmm_defs::config::PcieIommuConfig::AmdVi)
1131            } else if vtd_names.remove(rc_cli.name.as_str()) {
1132                Some(openvmm_defs::config::PcieIommuConfig::IntelVtd)
1133            } else {
1134                None
1135            },
1136            vnode: rc_cli.vnode,
1137            preserve_bars: rc_cli.preserve_bars,
1138        });
1139    }
1140
1141    cfg_select! {
1142        guest_arch = "x86_64" => {
1143            if let Some(name) = amd_iommu_names.into_iter().next() {
1144                anyhow::bail!("--amd-iommu refers to unknown root complex '{name}'");
1145            }
1146            if let Some(name) = vtd_names.into_iter().next() {
1147                anyhow::bail!("--intel-vtd refers to unknown root complex '{name}'");
1148            }
1149        }
1150        guest_arch = "aarch64" => {
1151            if let Some(name) = smmu_names.into_keys().next() {
1152                anyhow::bail!("--smmu refers to unknown root complex '{name}'");
1153            }
1154        }
1155    }
1156
1157    let pcie_switches = build_switch_list(&opt.pcie_switch);
1158    let pcie_generic_initiators = opt
1159        .pcie_generic_initiator
1160        .iter()
1161        .map(|gi| openvmm_defs::config::PcieGenericInitiatorConfig {
1162            port_name: gi.port_name.clone(),
1163            node: gi.node,
1164        })
1165        .collect();
1166    #[cfg(target_os = "linux")]
1167    let vfio_pcie_devices: Vec<PcieDeviceConfig> = {
1168        use std::collections::HashMap;
1169        use vm_resource::IntoResource;
1170
1171        // Process --iommu flags: open /dev/iommu for each declared context.
1172        let mut iommu_map: HashMap<String, std::fs::File> = HashMap::new();
1173        for iommu_cli in &opt.iommu {
1174            anyhow::ensure!(
1175                !iommu_map.contains_key(&iommu_cli.id),
1176                "duplicate --iommu id={}",
1177                iommu_cli.id
1178            );
1179            let file = std::fs::OpenOptions::new()
1180                .read(true)
1181                .write(true)
1182                .open("/dev/iommu")
1183                .context("failed to open /dev/iommu (is iommufd available?)")?;
1184            iommu_map.insert(iommu_cli.id.clone(), file);
1185        }
1186
1187        opt.vfio
1188            .iter()
1189            .map(|cli_cfg| {
1190                let sysfs_path = Path::new("/sys/bus/pci/devices").join(&cli_cfg.pci_id);
1191
1192                if let Some(iommu_id) = &cli_cfg.iommu {
1193                    // cdev + iommufd path
1194                    let iommufd = iommu_map.get(iommu_id).with_context(|| {
1195                        format!(
1196                            "--vfio device {} references iommu={iommu_id}, \
1197                             but no --iommu id={iommu_id} was specified",
1198                            cli_cfg.pci_id
1199                        )
1200                    })?;
1201                    // Clone the iommufd fd so the per-iommu manager can own it.
1202                    // The first device for a given iommu ID uses the cloned fd
1203                    // to create the IoasManager; subsequent devices reuse the
1204                    // existing manager and the cloned fd is dropped.
1205                    let iommufd = iommufd.try_clone().with_context(|| {
1206                        format!("failed to dup iommufd fd for iommu={iommu_id}")
1207                    })?;
1208
1209                    // Open the cdev device node.
1210                    let vfio_dev_dir = sysfs_path.join("vfio-dev");
1211                    let entry = std::fs::read_dir(&vfio_dev_dir)
1212                        .with_context(|| {
1213                            format!(
1214                                "failed to read {}: is {} bound to vfio-pci?",
1215                                vfio_dev_dir.display(),
1216                                cli_cfg.pci_id
1217                            )
1218                        })?
1219                        .next()
1220                        .context("no vfio-dev entry found")?
1221                        .context("failed to read vfio-dev entry")?;
1222                    let dev_path = Path::new("/dev/vfio/devices").join(entry.file_name());
1223                    let cdev = std::fs::OpenOptions::new()
1224                        .read(true)
1225                        .write(true)
1226                        .open(&dev_path)
1227                        .with_context(|| format!("failed to open {}", dev_path.display()))?;
1228
1229                    Ok(PcieDeviceConfig {
1230                        port_name: cli_cfg.port_name.clone(),
1231                        resource: vfio_assigned_device_resources::VfioCdevDeviceHandle {
1232                            pci_id: cli_cfg.pci_id.clone(),
1233                            cdev,
1234                            iommufd,
1235                            iommu_id: iommu_id.clone(),
1236                            bar_addresses: cli_cfg.bar_addresses,
1237                        }
1238                        .into_resource(),
1239                    })
1240                } else {
1241                    // Legacy group/container path
1242                    let iommu_group_link = std::fs::read_link(sysfs_path.join("iommu_group"))
1243                        .with_context(|| {
1244                            format!("failed to read IOMMU group for {}", cli_cfg.pci_id)
1245                        })?;
1246                    let group_id: u64 = iommu_group_link
1247                        .file_name()
1248                        .and_then(|s| s.to_str())
1249                        .context("invalid iommu_group symlink")?
1250                        .parse()
1251                        .context("failed to parse IOMMU group ID")?;
1252                    let group = std::fs::OpenOptions::new()
1253                        .read(true)
1254                        .write(true)
1255                        .open(format!("/dev/vfio/{group_id}"))
1256                        .with_context(|| format!("failed to open /dev/vfio/{group_id}"))?;
1257
1258                    Ok(PcieDeviceConfig {
1259                        port_name: cli_cfg.port_name.clone(),
1260                        resource: vfio_assigned_device_resources::VfioDeviceHandle {
1261                            pci_id: cli_cfg.pci_id.clone(),
1262                            group,
1263                            bar_addresses: cli_cfg.bar_addresses,
1264                        }
1265                        .into_resource(),
1266                    })
1267                }
1268            })
1269            .collect::<anyhow::Result<Vec<_>>>()?
1270    };
1271
1272    #[cfg(windows)]
1273    let vpci_resources: Vec<_> = opt
1274        .device
1275        .iter()
1276        .map(|path| -> anyhow::Result<_> {
1277            Ok(virt_whp::device::DeviceHandle(
1278                whp::VpciResource::new(
1279                    None,
1280                    Default::default(),
1281                    &whp::VpciResourceDescriptor::Sriov(path, 0, 0),
1282                )
1283                .with_context(|| format!("opening PCI device {}", path))?,
1284            ))
1285        })
1286        .collect::<Result<_, _>>()?;
1287
1288    // Create a vmbusproxy handle if needed by any devices.
1289    #[cfg(windows)]
1290    let vmbusproxy_handle = if !kernel_vmnics.is_empty() {
1291        Some(vmbus_proxy::ProxyHandle::new().context("failed to open vmbusproxy handle")?)
1292    } else {
1293        None
1294    };
1295
1296    let framebuffer = if opt.gfx || opt.vtl2_gfx || opt.vnc.vnc || opt.pcat {
1297        let vram = alloc_shared_memory(FRAMEBUFFER_SIZE, "vram")?;
1298        let (fb, fba) =
1299            framebuffer::framebuffer(vram, FRAMEBUFFER_SIZE, 0).context("creating framebuffer")?;
1300        resources.framebuffer_access = Some(fba);
1301        Some(fb)
1302    } else {
1303        None
1304    };
1305
1306    let load_mode;
1307    let with_hv;
1308
1309    let any_serial_configured = serial0_cfg.is_some()
1310        || serial1_cfg.is_some()
1311        || serial2_cfg.is_some()
1312        || serial3_cfg.is_some();
1313
1314    let has_com3 = serial2_cfg.is_some();
1315
1316    let mut chipset = VmManifestBuilder::new(base_chipset_type(opt), arch);
1317
1318    if legacy_pci_config_io_for_pcie {
1319        chipset = chipset.with_legacy_pci_config_io_for_pcie();
1320    }
1321
1322    if framebuffer.is_some() {
1323        chipset = chipset.with_framebuffer();
1324    }
1325    if opt.guest_watchdog {
1326        chipset = chipset.with_guest_watchdog();
1327    }
1328    if any_serial_configured {
1329        chipset = chipset.with_serial([serial0_cfg, serial1_cfg, serial2_cfg, serial3_cfg]);
1330    }
1331    chipset = chipset.with_serial_debugger_mode(com_debugger_mode);
1332    if opt.battery {
1333        let (tx, rx) = mesh::channel();
1334        tx.send(HostBatteryUpdate::default_present());
1335        chipset = chipset.with_battery(rx);
1336    }
1337    if opt.no_vmbus {
1338        chipset = chipset.without_vmbus();
1339    }
1340    if let Some(cfg) = &opt.debugcon {
1341        chipset = chipset.with_debugcon(
1342            debugcon_cfg.unwrap_or_else(|| DisconnectedSerialBackendHandle.into_resource()),
1343            cfg.port,
1344        );
1345    }
1346
1347    let (base_template, custom_uefi_json) = {
1348        cfg_select! {
1349            guest_arch = "x86_64" => {
1350                use firmware_uefi_resources::x64_secure_boot_templates as secure_boot_templates;
1351            }
1352            guest_arch = "aarch64" => {
1353                use firmware_uefi_resources::aarch64_secure_boot_templates as secure_boot_templates;
1354            }
1355        }
1356        let base_template = opt.secure_boot_template.map(|template| match template {
1357            SecureBootTemplateCli::Windows => secure_boot_templates::microsoft_windows(),
1358            SecureBootTemplateCli::UefiCa => secure_boot_templates::microsoft_uefi_ca(),
1359        });
1360
1361        // TODO: fallback to VMGS read if no command line flag was given
1362
1363        let custom_uefi_json = match &opt.custom_uefi_json {
1364            Some(file) => Some(
1365                fs_err::read(file)
1366                    .context("opening custom uefi json file")?
1367                    .into(),
1368            ),
1369            None => None,
1370        };
1371
1372        (base_template, custom_uefi_json)
1373    };
1374
1375    if (uefi.is_some() && opt.igvm.is_none())
1376        || opt
1377            .igvm
1378            .as_ref()
1379            .is_some_and(|igvm| igvm.personality == IgvmPersonalityCli::Uefi)
1380    {
1381        let log_level = match uefi_options.diagnostics.unwrap_or_default() {
1382            EfiDiagnosticsLogLevelCli::Default => firmware_uefi_resources::LogLevel::make_default(),
1383            EfiDiagnosticsLogLevelCli::Info => firmware_uefi_resources::LogLevel::make_info(),
1384            EfiDiagnosticsLogLevelCli::Full => firmware_uefi_resources::LogLevel::make_full(),
1385        };
1386        let nvram_storage =
1387            non_volatile_store_resource(opt.vmgs.is_some(), vmgs_format::FileId::BIOS_NVRAM);
1388        chipset = chipset.with_uefi(vm_manifest_builder::UefiManifest::new(
1389            arch,
1390            base_template,
1391            custom_uefi_json,
1392            opt.secure_boot,
1393            log_level,
1394            None,
1395            nvram_storage,
1396            None,
1397        ));
1398    }
1399
1400    // Build the SMBIOS config once, up front, so that UEFI and Linux direct
1401    // boot share a single source for the VM's BIOS GUID / system UUID. The TPM
1402    // also keys off this GUID.
1403    let smbios = Box::new(smbios_config_from_cli(&opt.smbios)?);
1404    let bios_guid = smbios.system.uuid;
1405
1406    // Capture the SMBIOS config for the OpenHCL/GED path before `smbios` is
1407    // potentially moved into a non-VTL2 LoadMode below. The GED forwards only
1408    // the system identity to the paravisor and fails closed on BIOS overrides
1409    // it cannot honor, so it is delivered as the shared `SmbiosConfig`.
1410    let ged_smbios = (*smbios).clone();
1411
1412    let tpm_version = opt.tpm.map(|cli_ver| match cli_ver {
1413        TpmVersionCli::V138 => TpmVersion::V138,
1414        TpmVersionCli::V185 => TpmVersion::V185,
1415    });
1416
1417    if let Some(igvm) = &opt.igvm {
1418        let cli_args::UefiCli {
1419            firmware,
1420            debug: _,
1421            enable_memory_protections: _,
1422            force_dma_bounce: _,
1423            force_firmware_version,
1424            disable_frontpage: _,
1425            console: _,
1426            diagnostics: _,
1427            default_boot_always_attempt: _,
1428        } = uefi_options;
1429
1430        anyhow::ensure!(
1431            firmware.is_none(),
1432            "--uefi firmware is not supported with --igvm"
1433        );
1434        anyhow::ensure!(
1435            !force_firmware_version,
1436            "--uefi force_firmware_version is not supported with --igvm"
1437        );
1438        let file = fs_err::File::open(&igvm.firmware)
1439            .context("failed to open igvm file")?
1440            .into();
1441        let cmdline = opt.cmdline.join(" ");
1442        with_hv = match igvm.personality {
1443            IgvmPersonalityCli::Openhcl | IgvmPersonalityCli::Uefi => true,
1444            IgvmPersonalityCli::LinuxDirect => opt.hv,
1445        };
1446
1447        load_mode = LoadMode::Igvm {
1448            file,
1449            cmdline,
1450            vtl2_base_address: if opt.vtl2 {
1451                opt.igvm_vtl2_relocation_type
1452            } else {
1453                Vtl2BaseAddressType::File
1454            },
1455            com_serial: has_com3.then_some(UartId::Com(ComPort::Com3)),
1456            personality: match igvm.personality {
1457                IgvmPersonalityCli::Openhcl => IgvmPersonality::Openhcl,
1458                IgvmPersonalityCli::Uefi => IgvmPersonality::Uefi,
1459                IgvmPersonalityCli::LinuxDirect => IgvmPersonality::LinuxDirect,
1460            },
1461        };
1462
1463        // An IGVM launch carries no SMBIOS field of its own; the identity is
1464        // only delivered over the GET/GED channel, which is absent here. Reject
1465        // overrides that would otherwise be silently dropped.
1466        let smbios_requested = !opt.smbios.is_empty();
1467        let smbios_delivered_via_get = with_get && with_hv;
1468        if smbios_requested && !smbios_delivered_via_get {
1469            anyhow::bail!(
1470                "--smbios is not supported for IGVM launches without an OpenHCL GET channel"
1471            );
1472        }
1473    } else if opt.pcat {
1474        // Emit a nice error early instead of complaining about missing firmware.
1475        if arch != MachineArch::X86_64 {
1476            anyhow::bail!("pcat not supported on this architecture");
1477        }
1478        with_hv = true;
1479
1480        let firmware = openvmm_pcat_locator::find_pcat_bios(opt.pcat_firmware.as_deref())?;
1481        load_mode = LoadMode::Pcat {
1482            firmware,
1483            boot_order: opt
1484                .pcat_boot_order
1485                .map(|x| x.0)
1486                .unwrap_or(DEFAULT_PCAT_BOOT_ORDER),
1487            hibernation_enabled: opt.hibernation,
1488            smbios,
1489        };
1490    } else if let Some(uefi_options) = &uefi {
1491        use openvmm_defs::config::UefiConsoleMode;
1492
1493        let cli_args::UefiCli {
1494            firmware,
1495            debug,
1496            enable_memory_protections,
1497            force_dma_bounce,
1498            force_firmware_version,
1499            disable_frontpage,
1500            console,
1501            diagnostics: _,
1502            default_boot_always_attempt,
1503        } = uefi_options;
1504
1505        if opt.no_hv && cfg!(guest_arch = "x86_64") {
1506            anyhow::bail!("--no-hv is not supported on x86_64");
1507        }
1508
1509        with_hv = !opt.no_hv;
1510
1511        let default_firmware = cli_args::default_uefi_firmware();
1512        let firmware = fs_err::File::open(
1513            firmware
1514                .as_ref()
1515                .or(default_firmware.as_ref())
1516                .context("must provide uefi firmware when booting with uefi")?,
1517        )
1518        .context("failed to open uefi firmware")?;
1519
1520        // TODO: It would be better to default memory protections to on, but currently Linux does not boot via UEFI due to what
1521        //       appears to be a GRUB memory protection fault. Memory protections are therefore only enabled if configured.
1522        load_mode = LoadMode::Uefi {
1523            firmware: firmware.into(),
1524            enable_debugging: *debug,
1525            enable_memory_protections: *enable_memory_protections,
1526            disable_frontpage: *disable_frontpage,
1527            tpm_version,
1528            enable_battery: opt.battery,
1529            enable_serial: any_serial_configured,
1530            enable_vpci_boot: false,
1531            uefi_console_mode: console.map(|m| match m {
1532                UefiConsoleModeCli::Default => UefiConsoleMode::Default,
1533                UefiConsoleModeCli::Com1 => UefiConsoleMode::Com1,
1534                UefiConsoleModeCli::Com2 => UefiConsoleMode::Com2,
1535                UefiConsoleModeCli::None => UefiConsoleMode::None,
1536            }),
1537            default_boot_always_attempt: *default_boot_always_attempt,
1538            smbios,
1539            enable_vmbus: !opt.no_vmbus,
1540            force_dma_bounce: *force_dma_bounce,
1541            enable_hv: !opt.no_hv,
1542            hibernation_enabled: opt.hibernation,
1543            force_firmware_version: *force_firmware_version,
1544        };
1545    } else {
1546        // Linux Direct
1547        let mut cmdline = "panic=-1 debug".to_string();
1548
1549        with_hv = opt.hv;
1550        if with_hv && opt.pcie_root_complex.is_empty() {
1551            cmdline += " pci=off";
1552        }
1553
1554        if !console_str.is_empty() {
1555            let _ = write!(&mut cmdline, " console={}", console_str);
1556        }
1557
1558        if opt.gfx {
1559            cmdline += " console=tty";
1560        }
1561        for extra in &opt.cmdline {
1562            let _ = write!(&mut cmdline, " {}", extra);
1563        }
1564
1565        let kernel = fs_err::File::open(
1566            (opt.kernel.0)
1567                .as_ref()
1568                .context("must provide kernel when booting with linux direct")?,
1569        )
1570        .context("failed to open kernel")?;
1571        let initrd = (opt.initrd.0)
1572            .as_ref()
1573            .map(fs_err::File::open)
1574            .transpose()
1575            .context("failed to open initrd")?;
1576
1577        load_mode = LoadMode::Linux {
1578            kernel: kernel.into(),
1579            initrd: initrd.map(Into::into),
1580            cmdline,
1581            enable_serial: any_serial_configured,
1582            isolation: if matches!(opt.isolation, Some(cli_args::IsolationCli::Snp)) {
1583                openvmm_defs::config::LinuxIsolationConfig::Snp {
1584                    restricted_injection: opt.snp_restricted_injection,
1585                }
1586            } else {
1587                openvmm_defs::config::LinuxIsolationConfig::None
1588            },
1589            boot_mode: if opt.device_tree {
1590                openvmm_defs::config::LinuxDirectBootMode::DeviceTree
1591            } else {
1592                openvmm_defs::config::LinuxDirectBootMode::Acpi
1593            },
1594            smbios,
1595        };
1596    }
1597
1598    if supports_acpi_power_button(&load_mode) {
1599        let (send, recv) = mesh::channel();
1600        let irq = match arch {
1601            MachineArch::X86_64 => DEFAULT_ACPI_POWER_BUTTON_IRQ_X64,
1602            MachineArch::Aarch64 => DEFAULT_ACPI_POWER_BUTTON_SPI_AARCH64,
1603        };
1604        chipset = chipset.with_acpi_power_button(irq, recv);
1605        resources.acpi_power_button = Some(send);
1606    }
1607
1608    let layout_config = chipset.layout_config();
1609    let VmChipsetResult {
1610        chipset,
1611        mut chipset_devices,
1612        pci_chipset_devices,
1613        isa_dma_controller,
1614        capabilities,
1615    } = chipset
1616        .build()
1617        .context("failed to build chipset configuration")?;
1618
1619    let mut vmgs = Some(if let Some(vmgs) = &opt.vmgs {
1620        let encryption_policy = if opt.test_gsp_by_id {
1621            GuestStateEncryptionPolicy::GspById(true)
1622        } else {
1623            GuestStateEncryptionPolicy::None(true)
1624        };
1625        open_vmgs(&vmgs.kind, vmgs.provision, encryption_policy)
1626            .await
1627            .context("failed to open vmgs disk")?
1628    } else {
1629        VmgsResource::Ephemeral
1630    });
1631
1632    if with_get && with_hv {
1633        let has_vtl0_nvme = storage.has_vtl0_nvme();
1634        let vtl2_settings = vtl2_settings_proto::Vtl2Settings {
1635            version: vtl2_settings_proto::vtl2_settings_base::Version::V1.into(),
1636            fixed: Some(Default::default()),
1637            dynamic: Some(vtl2_settings_proto::Vtl2SettingsDynamic {
1638                storage_controllers: storage.build_openhcl_settings(opt.vmbus_redirect),
1639                nic_devices: underhill_nics,
1640            }),
1641            namespace_settings: Vec::default(),
1642        };
1643
1644        // Cache the VTL2 settings for later modification via the interactive console.
1645        resources.vtl2_settings = Some(vtl2_settings.clone());
1646
1647        let (send, guest_request_recv) = mesh::channel();
1648        resources.ged_rpc = Some(send);
1649
1650        let vmgs = vmgs.take().unwrap();
1651
1652        vmbus_devices.extend([
1653            (
1654                openhcl_vtl,
1655                get_resources::gel::GuestEmulationLogHandle.into_resource(),
1656            ),
1657            (
1658                openhcl_vtl,
1659                get_resources::ged::GuestEmulationDeviceHandle {
1660                    firmware: if opt.pcat {
1661                        get_resources::ged::GuestFirmwareConfig::Pcat {
1662                            boot_order: opt
1663                                .pcat_boot_order
1664                                .map_or(DEFAULT_PCAT_BOOT_ORDER, |x| x.0)
1665                                .map(|x| match x {
1666                                    openvmm_defs::config::PcatBootDevice::Floppy => {
1667                                        get_resources::ged::PcatBootDevice::Floppy
1668                                    }
1669                                    openvmm_defs::config::PcatBootDevice::HardDrive => {
1670                                        get_resources::ged::PcatBootDevice::HardDrive
1671                                    }
1672                                    openvmm_defs::config::PcatBootDevice::Optical => {
1673                                        get_resources::ged::PcatBootDevice::Optical
1674                                    }
1675                                    openvmm_defs::config::PcatBootDevice::Network => {
1676                                        get_resources::ged::PcatBootDevice::Network
1677                                    }
1678                                }),
1679                        }
1680                    } else {
1681                        use get_resources::ged::UefiConsoleMode;
1682
1683                        get_resources::ged::GuestFirmwareConfig::Uefi {
1684                            enable_vpci_boot: has_vtl0_nvme,
1685                            firmware_debug: uefi_options.debug,
1686                            enable_memory_protections: uefi_options.enable_memory_protections,
1687                            disable_frontpage: uefi_options.disable_frontpage,
1688                            console_mode: match uefi_options.console.unwrap_or(UefiConsoleModeCli::Default) {
1689                                UefiConsoleModeCli::Default => UefiConsoleMode::Default,
1690                                UefiConsoleModeCli::Com1 => UefiConsoleMode::COM1,
1691                                UefiConsoleModeCli::Com2 => UefiConsoleMode::COM2,
1692                                UefiConsoleModeCli::None => UefiConsoleMode::None,
1693                            },
1694                            default_boot_always_attempt: uefi_options.default_boot_always_attempt,
1695                        }
1696                    },
1697                    com1: with_vmbus_com1_serial,
1698                    com2: with_vmbus_com2_serial,
1699                    serial_tx_only: opt.serial_tx_only,
1700                    vtl2_settings: Some(prost::Message::encode_to_vec(&vtl2_settings)),
1701                    vmbus_redirection: opt.vmbus_redirect,
1702                    vmgs,
1703                    framebuffer: opt
1704                        .vtl2_gfx
1705                        .then(|| SharedFramebufferHandle.into_resource()),
1706                    guest_request_recv,
1707                    tpm_version: tpm_version.map(|v| match v {
1708                        TpmVersion::V138 => get_resources::ged::GedTpmVersion::V138,
1709                        TpmVersion::V185 => get_resources::ged::GedTpmVersion::V185,
1710                    }),
1711                    firmware_event_send: None,
1712                    ipmi_sel_event_send: None,
1713                    secure_boot_enabled: opt.secure_boot,
1714                    secure_boot_template: match opt.secure_boot_template {
1715                        Some(SecureBootTemplateCli::Windows) => {
1716                            get_resources::ged::GuestSecureBootTemplateType::MicrosoftWindows
1717                        },
1718                        Some(SecureBootTemplateCli::UefiCa) => {
1719                            get_resources::ged::GuestSecureBootTemplateType::MicrosoftUefiCertificateAuthority
1720                        }
1721                        None => {
1722                            get_resources::ged::GuestSecureBootTemplateType::None
1723                        },
1724                    },
1725                    enable_battery: opt.battery,
1726                    enable_ipmi: false,
1727                    enable_hibernation: opt.hibernation,
1728                    no_persistent_secrets: true,
1729                    igvm_attest_test_config: None,
1730                    test_gsp_by_id: opt.test_gsp_by_id,
1731                    efi_diagnostics_log_level: {
1732                        match uefi_options.diagnostics.unwrap_or_default() {
1733                            EfiDiagnosticsLogLevelCli::Default => get_resources::ged::EfiDiagnosticsLogLevelType::Default,
1734                            EfiDiagnosticsLogLevelCli::Info => get_resources::ged::EfiDiagnosticsLogLevelType::Info,
1735                            EfiDiagnosticsLogLevelCli::Full => get_resources::ged::EfiDiagnosticsLogLevelType::Full,
1736                        }
1737                    },
1738                    force_dma_bounce_enabled: uefi_options.force_dma_bounce,
1739                    smbios: ged_smbios,
1740                }
1741                .into_resource(),
1742            ),
1743        ]);
1744    }
1745
1746    if let Some(tpm_version) = tpm_version
1747        && !opt.vtl2
1748    {
1749        let register_layout = cfg_select! {
1750            guest_arch = "x86_64" => TpmRegisterLayout::IoPort,
1751            guest_arch = "aarch64" => TpmRegisterLayout::Mmio,
1752        };
1753
1754        let ppi_store =
1755            non_volatile_store_resource(opt.vmgs.is_some(), vmgs_format::FileId::TPM_PPI);
1756        let nvram_store = non_volatile_store_resource(
1757            opt.vmgs.is_some(),
1758            tpm_vmgs::tpm_nvram_file_id(tpm_version),
1759        );
1760
1761        chipset_devices.push(ChipsetDeviceHandle {
1762            name: "tpm".to_string(),
1763            resource: chipset_device_worker_defs::RemoteChipsetDeviceHandle {
1764                device: TpmDeviceHandle {
1765                    version: tpm_version,
1766                    ppi_store,
1767                    nvram_store,
1768                    nvram_size: None,
1769                    refresh_tpm_seeds: false,
1770                    ak_cert_type: tpm_resources::TpmAkCertTypeResource::None,
1771                    register_layout,
1772                    guest_secret_key: None,
1773                    logger: None,
1774                    is_confidential_vm: false,
1775                    bios_guid,
1776                }
1777                .into_resource(),
1778                worker_host: mesh.make_host("tpm", None).await?,
1779            }
1780            .into_resource(),
1781        });
1782    }
1783
1784    let vga_firmware = if opt.pcat {
1785        Some(openvmm_pcat_locator::find_svga_bios(
1786            opt.vga_firmware.as_deref(),
1787        )?)
1788    } else {
1789        None
1790    };
1791
1792    if opt.gfx {
1793        // Channel for the video device to report dirty rectangles to the VNC worker.
1794        let (dirt_send, dirt_recv) = mesh::channel();
1795        resources.dirty_rect_recv = Some(dirt_recv);
1796
1797        vmbus_devices.extend([
1798            (
1799                DeviceVtl::Vtl0,
1800                SynthVideoHandle {
1801                    framebuffer: SharedFramebufferHandle.into_resource(),
1802                    dirt_send: Some(dirt_send),
1803                }
1804                .into_resource(),
1805            ),
1806            (
1807                DeviceVtl::Vtl0,
1808                SynthKeyboardHandle {
1809                    source: MultiplexedInputHandle {
1810                        // Save 0 for PS/2
1811                        elevation: 1,
1812                    }
1813                    .into_resource(),
1814                }
1815                .into_resource(),
1816            ),
1817            (
1818                DeviceVtl::Vtl0,
1819                SynthMouseHandle {
1820                    source: MultiplexedInputHandle {
1821                        // Save 0 for PS/2
1822                        elevation: 1,
1823                    }
1824                    .into_resource(),
1825                }
1826                .into_resource(),
1827            ),
1828        ]);
1829    }
1830
1831    let vsock_listener = |path: Option<&str>| -> anyhow::Result<_> {
1832        if let Some(path) = path {
1833            cleanup_socket(path.as_ref());
1834            let listener = unix_socket::UnixListener::bind(path)
1835                .with_context(|| format!("failed to bind to hybrid vsock path: {}", path))?;
1836            Ok(Some(listener))
1837        } else {
1838            Ok(None)
1839        }
1840    };
1841
1842    let vtl0_vsock_listener = vsock_listener(opt.vmbus_vsock_path.as_deref())?;
1843    let vtl2_vsock_listener = vsock_listener(opt.vmbus_vtl2_vsock_path.as_deref())?;
1844
1845    if let Some(path) = &opt.openhcl_dump_path {
1846        let (resource, task) = spawn_dump_handler(&spawner, path.clone(), None);
1847        task.detach();
1848        vmbus_devices.push((openhcl_vtl, resource));
1849    }
1850
1851    let topology_arch = cfg_select! {
1852        guest_arch = "aarch64" => openvmm_defs::config::ArchTopologyConfig::Aarch64(
1853            openvmm_defs::config::Aarch64TopologyConfig {
1854                // TODO: allow this to be configured from the command line
1855                gic_config: None,
1856                pmu_gsiv: openvmm_defs::config::PmuGsivConfig::Platform,
1857                gic_msi: match opt.gic_msi {
1858                    cli_args::GicMsiCli::Auto => openvmm_defs::config::GicMsiConfig::Auto,
1859                    cli_args::GicMsiCli::Its => openvmm_defs::config::GicMsiConfig::Its,
1860                    cli_args::GicMsiCli::V2m => {
1861                        openvmm_defs::config::GicMsiConfig::V2m { spi_count: None }
1862                    }
1863                },
1864            },
1865        ),
1866        guest_arch = "x86_64" => {
1867            openvmm_defs::config::ArchTopologyConfig::X86(openvmm_defs::config::X86TopologyConfig {
1868                apic_id_offset: opt.apic_id_offset,
1869                x2apic: opt.x2apic,
1870            })
1871        }
1872    };
1873
1874    let with_isolation = if let Some(isolation) = &opt.isolation {
1875        match isolation {
1876            cli_args::IsolationCli::Vbs => {
1877                // TODO: For now, VBS isolation is only supported with VTL2.
1878                if !opt.vtl2 {
1879                    anyhow::bail!("VBS isolation is only currently supported with vtl2");
1880                }
1881
1882                // TODO: Alias map support is not yet implemented with isolation.
1883                if !opt.no_alias_map {
1884                    anyhow::bail!("alias map not supported with isolation");
1885                }
1886
1887                Some(openvmm_defs::config::IsolationType::Vbs)
1888            }
1889            cli_args::IsolationCli::Snp => Some(openvmm_defs::config::IsolationType::Snp {
1890                // SNP host data is currently only configurable via TTRPC.
1891                host_data: None,
1892            }),
1893        }
1894    } else {
1895        None
1896    };
1897
1898    if with_hv && !opt.no_vmbus {
1899        let (shutdown_send, shutdown_recv) = mesh::channel();
1900        resources.shutdown_ic = Some(shutdown_send);
1901        let (kvp_send, kvp_recv) = mesh::channel();
1902        resources.kvp_ic = Some(kvp_send);
1903        vmbus_devices.extend(
1904            [
1905                hyperv_ic_resources::shutdown::ShutdownIcHandle {
1906                    recv: shutdown_recv,
1907                }
1908                .into_resource(),
1909                hyperv_ic_resources::kvp::KvpIcHandle { recv: kvp_recv }.into_resource(),
1910                hyperv_ic_resources::timesync::TimesyncIcHandle.into_resource(),
1911            ]
1912            .map(|r| (DeviceVtl::Vtl0, r)),
1913        );
1914    }
1915
1916    if let Some(hive_path) = &opt.imc {
1917        let file = fs_err::File::open(hive_path).context("failed to open imc hive")?;
1918        vmbus_devices.push((
1919            DeviceVtl::Vtl0,
1920            vmbfs_resources::VmbfsImcDeviceHandle { file: file.into() }.into_resource(),
1921        ));
1922    }
1923
1924    let mut virtio_devices = Vec::new();
1925    let mut add_virtio_device =
1926        |bus, resource: Resource<VirtioDeviceHandle>, pcie_devices: &mut Vec<_>| match bus {
1927            VirtioBusCli::Auto => {
1928                // Use VPCI when possible (currently only on Windows and macOS due
1929                // to KVM backend limitations).
1930                if with_hv && (cfg!(windows) || cfg!(target_os = "macos")) {
1931                    vpci_devices.push(VpciDeviceConfig {
1932                        vtl: DeviceVtl::Vtl0,
1933                        instance_id: Guid::new_random(),
1934                        resource: VirtioPciDeviceHandle(resource).into_resource(),
1935                        vnode: None,
1936                    });
1937                } else {
1938                    virtio_devices.push((VirtioBus::Pci, resource));
1939                }
1940            }
1941            VirtioBusCli::Mmio => virtio_devices.push((VirtioBus::Mmio, resource)),
1942            VirtioBusCli::Pci => virtio_devices.push((VirtioBus::Pci, resource)),
1943            VirtioBusCli::Pcie(port_name) => pcie_devices.push(PcieDeviceConfig {
1944                port_name,
1945                resource: VirtioPciDeviceHandle(resource).into_resource(),
1946            }),
1947            VirtioBusCli::Vpci => vpci_devices.push(VpciDeviceConfig {
1948                vtl: DeviceVtl::Vtl0,
1949                instance_id: Guid::new_random(),
1950                resource: VirtioPciDeviceHandle(resource).into_resource(),
1951                vnode: None,
1952            }),
1953        };
1954
1955    for cli_cfg in &opt.virtio_net {
1956        if cli_cfg.underhill {
1957            anyhow::bail!("use --net uh:[...] to add underhill NICs")
1958        }
1959        let vport = parse_endpoint(cli_cfg, &mut nic_index, &mut resources)?;
1960        let resource = virtio_resources::net::VirtioNetHandle {
1961            max_queues: vport.max_queues,
1962            enable_rx_gso: vport.enable_rx_gso,
1963            mac_address: vport.mac_address,
1964            endpoint: vport.endpoint,
1965        }
1966        .into_resource();
1967        if let Some(pcie_port) = &cli_cfg.pcie_port {
1968            pcie_devices.push(PcieDeviceConfig {
1969                port_name: pcie_port.clone(),
1970                resource: VirtioPciDeviceHandle(resource).into_resource(),
1971            });
1972        } else {
1973            add_virtio_device(VirtioBusCli::Auto, resource, &mut pcie_devices);
1974        }
1975    }
1976
1977    for args in &opt.virtio_fs {
1978        let resource: Resource<VirtioDeviceHandle> = virtio_resources::fs::VirtioFsHandle {
1979            tag: args.tag.clone(),
1980            fs: virtio_resources::fs::VirtioFsBackend::HostFs {
1981                root_path: args.path.clone(),
1982                mount_options: args.options.clone(),
1983                windows_options: None,
1984            },
1985        }
1986        .into_resource();
1987        if let Some(pcie_port) = &args.pcie_port {
1988            pcie_devices.push(PcieDeviceConfig {
1989                port_name: pcie_port.clone(),
1990                resource: VirtioPciDeviceHandle(resource).into_resource(),
1991            });
1992        } else {
1993            add_virtio_device(opt.virtio_fs_bus.clone(), resource, &mut pcie_devices);
1994        }
1995    }
1996
1997    for args in &opt.virtio_fs_shmem {
1998        let resource: Resource<VirtioDeviceHandle> = virtio_resources::fs::VirtioFsHandle {
1999            tag: args.tag.clone(),
2000            fs: virtio_resources::fs::VirtioFsBackend::SectionFs {
2001                root_path: args.path.clone(),
2002            },
2003        }
2004        .into_resource();
2005        if let Some(pcie_port) = &args.pcie_port {
2006            pcie_devices.push(PcieDeviceConfig {
2007                port_name: pcie_port.clone(),
2008                resource: VirtioPciDeviceHandle(resource).into_resource(),
2009            });
2010        } else {
2011            add_virtio_device(opt.virtio_fs_bus.clone(), resource, &mut pcie_devices);
2012        }
2013    }
2014
2015    for args in &opt.virtio_9p {
2016        let resource: Resource<VirtioDeviceHandle> = virtio_resources::p9::VirtioPlan9Handle {
2017            tag: args.tag.clone(),
2018            root_path: args.path.clone(),
2019            debug: opt.virtio_9p_debug,
2020        }
2021        .into_resource();
2022        if let Some(pcie_port) = &args.pcie_port {
2023            pcie_devices.push(PcieDeviceConfig {
2024                port_name: pcie_port.clone(),
2025                resource: VirtioPciDeviceHandle(resource).into_resource(),
2026            });
2027        } else {
2028            add_virtio_device(VirtioBusCli::Auto, resource, &mut pcie_devices);
2029        }
2030    }
2031
2032    if let Some(pmem_args) = &opt.virtio_pmem {
2033        let resource: Resource<VirtioDeviceHandle> = virtio_resources::pmem::VirtioPmemHandle {
2034            path: pmem_args.path.clone(),
2035            read_only: false,
2036        }
2037        .into_resource();
2038        if let Some(pcie_port) = &pmem_args.pcie_port {
2039            pcie_devices.push(PcieDeviceConfig {
2040                port_name: pcie_port.clone(),
2041                resource: VirtioPciDeviceHandle(resource).into_resource(),
2042            });
2043        } else {
2044            add_virtio_device(VirtioBusCli::Auto, resource, &mut pcie_devices);
2045        }
2046    }
2047
2048    if opt.virtio_rng {
2049        let resource: Resource<VirtioDeviceHandle> =
2050            virtio_resources::rng::VirtioRngHandle.into_resource();
2051        if let Some(pcie_port) = &opt.virtio_rng_pcie_port {
2052            pcie_devices.push(PcieDeviceConfig {
2053                port_name: pcie_port.clone(),
2054                resource: VirtioPciDeviceHandle(resource).into_resource(),
2055            });
2056        } else {
2057            add_virtio_device(opt.virtio_rng_bus.clone(), resource, &mut pcie_devices);
2058        }
2059    }
2060
2061    if let Some(rtc) = &opt.virtio_rtc {
2062        let resource: Resource<VirtioDeviceHandle> =
2063            virtio_resources::rtc::VirtioRtcHandle.into_resource();
2064        add_virtio_device(rtc.bus.clone(), resource, &mut pcie_devices);
2065    }
2066
2067    if let Some(backend) = virtio_console_backend {
2068        let resource: Resource<VirtioDeviceHandle> =
2069            virtio_resources::console::VirtioConsoleHandle { backend }.into_resource();
2070        if let Some(pcie_port) = &opt.virtio_console_pcie_port {
2071            pcie_devices.push(PcieDeviceConfig {
2072                port_name: pcie_port.clone(),
2073                resource: VirtioPciDeviceHandle(resource).into_resource(),
2074            });
2075        } else {
2076            add_virtio_device(VirtioBusCli::Auto, resource, &mut pcie_devices);
2077        }
2078    }
2079
2080    // Handle --vhost-user arguments.
2081    #[cfg(target_os = "linux")]
2082    for vhost_cli in &opt.vhost_user {
2083        let stream =
2084            unix_socket::UnixStream::connect(&vhost_cli.socket_path).with_context(|| {
2085                format!(
2086                    "failed to connect to vhost-user socket: {}",
2087                    vhost_cli.socket_path
2088                )
2089            })?;
2090
2091        use crate::cli_args::VhostUserDeviceTypeCli;
2092        let resource: Resource<VirtioDeviceHandle> = match vhost_cli.device_type {
2093            VhostUserDeviceTypeCli::Fs {
2094                ref tag,
2095                num_queues,
2096                queue_size,
2097            } => virtio_resources::vhost_user::VhostUserFsHandle {
2098                socket: stream.into(),
2099                tag: tag.clone(),
2100                num_queues,
2101                queue_size,
2102            }
2103            .into_resource(),
2104            VhostUserDeviceTypeCli::Blk {
2105                num_queues,
2106                queue_size,
2107            } => virtio_resources::vhost_user::VhostUserBlkHandle {
2108                socket: stream.into(),
2109                num_queues,
2110                queue_size,
2111            }
2112            .into_resource(),
2113            VhostUserDeviceTypeCli::Other {
2114                device_id,
2115                ref queue_sizes,
2116            } => virtio_resources::vhost_user::VhostUserGenericHandle {
2117                socket: stream.into(),
2118                device_id,
2119                queue_sizes: queue_sizes.clone(),
2120            }
2121            .into_resource(),
2122        };
2123        if let Some(pcie_port) = &vhost_cli.pcie_port {
2124            pcie_devices.push(PcieDeviceConfig {
2125                port_name: pcie_port.clone(),
2126                resource: VirtioPciDeviceHandle(resource).into_resource(),
2127            });
2128        } else {
2129            add_virtio_device(VirtioBusCli::Auto, resource, &mut pcie_devices);
2130        }
2131    }
2132
2133    let virtio_vsock_bus = opt.virtio_vsock_bus.clone().unwrap_or(VirtioBusCli::Auto);
2134
2135    if let Some(vsock_path) = &opt.virtio_vsock_path {
2136        let listener = vsock_listener(Some(vsock_path))?.unwrap();
2137        let resource: Resource<VirtioDeviceHandle> = virtio_resources::vsock::VirtioVsockHandle {
2138            // The guest CID does not matter since the UDS relay does not use it. It just needs
2139            // to be some non-reserved value for the guest to use.
2140            guest_cid: 0x3,
2141            base_path: vsock_path.clone(),
2142            listener,
2143        }
2144        .into_resource();
2145        add_virtio_device(virtio_vsock_bus.clone(), resource, &mut pcie_devices);
2146    }
2147
2148    #[cfg(target_os = "linux")]
2149    if let Some(guest_cid) = opt.virtio_vsock_vhost_cid {
2150        let vhost = std::fs::OpenOptions::new()
2151            .read(true)
2152            .write(true)
2153            .open("/dev/vhost-vsock")
2154            .context("failed to open /dev/vhost-vsock")?
2155            .into();
2156        let resource =
2157            virtio_resources::vsock::VirtioVsockVhostHandle { vhost, guest_cid }.into_resource();
2158        add_virtio_device(virtio_vsock_bus, resource, &mut pcie_devices);
2159    }
2160
2161    #[cfg(target_os = "linux")]
2162    pcie_devices.extend(vfio_pcie_devices);
2163
2164    let mut cfg = Config {
2165        chipset,
2166        load_mode,
2167        floppy_disks,
2168        pcie_root_complexes,
2169        pcie_ecam_below_4gb: opt.pcie_ecam_below_4gb,
2170        #[cfg(target_os = "linux")]
2171        pcie_devices,
2172        #[cfg(not(target_os = "linux"))]
2173        pcie_devices,
2174        pcie_switches,
2175        pcie_generic_initiators,
2176        vpci_devices,
2177        ide_disks: Vec::new(),
2178        numa: {
2179            if let Some(ref nodes) = opt.numa {
2180                // --numa mode: each --numa flag defines a node.
2181                NumaTopology {
2182                    nodes: nodes
2183                        .iter()
2184                        .map(|n| {
2185                            let vps = match &n.vps {
2186                                Some(vps) if vps.0.is_empty() => VpAssignment::Empty,
2187                                Some(vps) => {
2188                                    VpAssignment::Explicit(vps.expand_below(opt.processors)?)
2189                                }
2190                                None => VpAssignment::FromTopology,
2191                            };
2192                            Ok(NumaNode {
2193                                mem: Some(MemoryConfig {
2194                                    mem_size: n
2195                                        .memory
2196                                        .size
2197                                        .expect("NUMA memory size was validated")
2198                                        .0,
2199                                    prefetch_memory: n.memory.prefetch,
2200                                    private_memory: n.memory.shared == Some(false),
2201                                    transparent_hugepages: n
2202                                        .memory
2203                                        .transparent_hugepages
2204                                        .unwrap_or(!n.memory.hugepages),
2205                                    hugepages: n.memory.hugepages,
2206                                    hugepage_size: n.memory.hugepage_size.map(|m| m.0),
2207                                    host_numa_node: n.host_numa_node,
2208                                }),
2209                                vps,
2210                            })
2211                        })
2212                        .collect::<anyhow::Result<Vec<_>>>()?,
2213                    distances: opt
2214                        .numa_distance
2215                        .as_deref()
2216                        .unwrap_or(&[])
2217                        .iter()
2218                        .map(|d| NumaDistance {
2219                            src: d.src,
2220                            dst: d.dst,
2221                            distance: d.distance,
2222                        })
2223                        .collect(),
2224                }
2225            } else {
2226                // Single-node default from --memory.
2227                NumaTopology {
2228                    nodes: vec![NumaNode {
2229                        mem: Some(MemoryConfig {
2230                            mem_size: opt.memory_size(),
2231                            prefetch_memory: opt.prefetch_memory(),
2232                            private_memory: opt.private_memory(),
2233                            transparent_hugepages: opt.transparent_hugepages(),
2234                            hugepages: opt.memory.hugepages,
2235                            hugepage_size: opt.memory.hugepage_size.map(|m| m.0),
2236                            host_numa_node: None,
2237                        }),
2238                        vps: VpAssignment::FromTopology,
2239                    }],
2240                    distances: vec![],
2241                }
2242            }
2243        },
2244        processor_topology: ProcessorTopologyConfig {
2245            proc_count: opt.processors,
2246            vps_per_socket: opt.vps_per_socket,
2247            enable_smt: match opt.smt {
2248                cli_args::SmtConfigCli::Auto => None,
2249                cli_args::SmtConfigCli::Force => Some(true),
2250                cli_args::SmtConfigCli::Off => Some(false),
2251            },
2252            arch: Some(topology_arch),
2253        },
2254        hypervisor: HypervisorConfig {
2255            with_hv,
2256            with_vtl2: opt.vtl2.then_some(Vtl2Config {
2257                vtl0_alias_map: !opt.no_alias_map,
2258                late_map_vtl0_memory: match opt.late_map_vtl0_policy {
2259                    cli_args::Vtl0LateMapPolicyCli::Off => None,
2260                    cli_args::Vtl0LateMapPolicyCli::Log => Some(LateMapVtl0MemoryPolicy::Log),
2261                    cli_args::Vtl0LateMapPolicyCli::Halt => Some(LateMapVtl0MemoryPolicy::Halt),
2262                    cli_args::Vtl0LateMapPolicyCli::Exception => {
2263                        Some(LateMapVtl0MemoryPolicy::InjectException)
2264                    }
2265                },
2266            }),
2267            with_isolation,
2268            nested_virt: opt.nested_virt,
2269        },
2270        #[cfg(windows)]
2271        kernel_vmnics,
2272        input: mesh::Receiver::new(),
2273        framebuffer,
2274        vga_firmware,
2275        vtl2_gfx: opt.vtl2_gfx,
2276        virtio_devices,
2277        vmbus: (with_hv && !opt.no_vmbus).then_some(VmbusConfig {
2278            vsock_listener: vtl0_vsock_listener,
2279            vsock_path: opt.vmbus_vsock_path.clone(),
2280            vtl2_redirect: opt.vmbus_redirect,
2281            vmbus_max_version: opt.vmbus_max_version,
2282            #[cfg(windows)]
2283            vmbusproxy_handle,
2284        }),
2285        vtl2_vmbus: (with_hv && opt.vtl2).then_some(VmbusConfig {
2286            vsock_listener: vtl2_vsock_listener,
2287            vsock_path: opt.vmbus_vtl2_vsock_path.clone(),
2288            ..Default::default()
2289        }),
2290        vmbus_devices,
2291        chipset_devices,
2292        pci_chipset_devices,
2293        isa_dma_controller,
2294        chipset_capabilities: capabilities,
2295        layout: layout_config,
2296        #[cfg(windows)]
2297        vpci_resources,
2298        vmgs,
2299        firmware_event_send: None,
2300        debugger_rpc: None,
2301        rtc_delta_milliseconds: 0,
2302    };
2303
2304    storage.build_config(&mut cfg, &mut resources, opt.scsi_sub_channels)?;
2305    let mut pcie_port_names = HashSet::new();
2306    for device in &cfg.pcie_devices {
2307        anyhow::ensure!(
2308            pcie_port_names.insert(&device.port_name),
2309            "multiple devices use PCIe port '{}'",
2310            device.port_name
2311        );
2312    }
2313    resources.serial_driver = Some(serial_driver);
2314    validate_snp_config(&cfg)?;
2315    Ok((cfg, resources))
2316}
2317
2318fn validate_snp_config(cfg: &Config) -> anyhow::Result<()> {
2319    if !matches!(
2320        cfg.hypervisor.with_isolation,
2321        Some(openvmm_defs::config::IsolationType::Snp { .. })
2322    ) {
2323        return Ok(());
2324    }
2325
2326    if !matches!(
2327        cfg.load_mode,
2328        LoadMode::Linux { .. } | LoadMode::Igvm { .. }
2329    ) {
2330        anyhow::bail!("SNP isolation currently only supports Linux direct or IGVM boot");
2331    }
2332    if cfg.hypervisor.with_vtl2.is_some() {
2333        anyhow::bail!("SNP isolation currently does not support VTL2");
2334    }
2335    if cfg.vmbus.is_some() || cfg.vtl2_vmbus.is_some() || !cfg.vmbus_devices.is_empty() {
2336        anyhow::bail!("SNP isolation currently does not support VMBus devices");
2337    }
2338
2339    let only_supported_chipset_devices = cfg.chipset_devices.iter().all(|device| {
2340        matches!(
2341            device.resource.id(),
2342            "serial_16550"
2343                | "pic"
2344                | "pit"
2345                | "generic-ioapic"
2346                | "hyperv_power_management"
2347                | "missing-dev"
2348        )
2349    });
2350    let only_virtio_pcie_devices = cfg
2351        .pcie_devices
2352        .iter()
2353        .all(|device| device.resource.id() == "virtio");
2354    if !cfg.floppy_disks.is_empty()
2355        || !cfg.ide_disks.is_empty()
2356        || !cfg.virtio_devices.is_empty()
2357        || !only_virtio_pcie_devices
2358        || !cfg.vpci_devices.is_empty()
2359        || !only_supported_chipset_devices
2360        || !cfg.pci_chipset_devices.is_empty()
2361    {
2362        anyhow::bail!("SNP isolation currently only supports virtio devices");
2363    }
2364    if cfg.framebuffer.is_some() || cfg.vga_firmware.is_some() || cfg.debugger_rpc.is_some() {
2365        anyhow::bail!("SNP isolation currently does not support this VM configuration");
2366    }
2367
2368    Ok(())
2369}
2370
2371/// Gets the terminal to use for externally launched console windows.
2372pub(crate) fn openvmm_terminal_app() -> Option<PathBuf> {
2373    std::env::var_os("OPENVMM_TERM")
2374        .or_else(|| std::env::var_os("HVLITE_TERM"))
2375        .map(Into::into)
2376}
2377
2378// Tries to remove `path` if it is confirmed to be a Unix socket.
2379fn cleanup_socket(path: &Path) {
2380    let is_socket = cfg_select! {
2381        windows => pal::windows::fs::is_unix_socket(path).unwrap_or(false),
2382        _ => path
2383            .metadata()
2384            .is_ok_and(|meta| std::os::unix::fs::FileTypeExt::is_socket(&meta.file_type())),
2385    };
2386
2387    if is_socket {
2388        let _ = std::fs::remove_file(path);
2389    }
2390}
2391
2392#[cfg(windows)]
2393fn new_switch_port(
2394    switch_id: Option<&str>,
2395) -> anyhow::Result<(
2396    openvmm_defs::config::SwitchPortId,
2397    vmswitch::kernel::SwitchPort,
2398)> {
2399    let id = vmswitch::kernel::SwitchPortId {
2400        switch: match switch_id {
2401            Some(s) => s.parse().context("invalid switch id")?,
2402            None => vmswitch::hcn::DEFAULT_SWITCH,
2403        },
2404        port: Guid::new_random(),
2405    };
2406    let _ = vmswitch::hcn::Network::open(&id.switch)
2407        .with_context(|| format!("could not find switch {}", id.switch))?;
2408
2409    let port = vmswitch::kernel::SwitchPort::new(&id).context("failed to create switch port")?;
2410
2411    let id = openvmm_defs::config::SwitchPortId {
2412        switch: id.switch,
2413        port: id.port,
2414    };
2415    Ok((id, port))
2416}
2417
2418fn parse_endpoint(
2419    cli_cfg: &NicConfigCli,
2420    index: &mut usize,
2421    resources: &mut VmResources,
2422) -> anyhow::Result<NicConfig> {
2423    let _ = resources;
2424    let endpoint = match &cli_cfg.endpoint {
2425        EndpointConfigCli::Consomme { cidr, host_fwd } => {
2426            let ports = host_fwd
2427                .iter()
2428                .map(|fwd| {
2429                    use net_backend_resources::consomme::HostPortProtocol;
2430                    net_backend_resources::consomme::HostPortConfig {
2431                        protocol: match fwd.protocol {
2432                            cli_args::HostPortProtocolCli::Tcp => HostPortProtocol::Tcp,
2433                            cli_args::HostPortProtocolCli::Udp => HostPortProtocol::Udp,
2434                        },
2435                        host_address: fwd
2436                            .host_address
2437                            .map(net_backend_resources::consomme::HostIpAddress::from),
2438                        host_port: net_backend_resources::consomme::HostPort::Fixed(fwd.host_port),
2439                        guest_port: fwd.guest_port,
2440                    }
2441                })
2442                .collect();
2443            // Only wire the bind/unbind RPC channel to the first consomme
2444            // endpoint. Additional consomme NICs work normally but cannot be
2445            // targeted by runtime bind/unbind commands.
2446            let recv = if resources.consomme_rpc.is_none() {
2447                let (send, recv) = mesh::channel();
2448                resources.consomme_rpc = Some(send);
2449                Some(recv)
2450            } else {
2451                None
2452            };
2453            net_backend_resources::consomme::ConsommeHandle {
2454                cidr: cidr.clone(),
2455                params: Default::default(),
2456                ports,
2457                internal_dns: true,
2458                recv,
2459            }
2460            .into_resource()
2461        }
2462        EndpointConfigCli::None => net_backend_resources::null::NullHandle.into_resource(),
2463        EndpointConfigCli::Dio { id } => cfg_select! {
2464            windows => {{
2465                let (port_id, port) = new_switch_port(id.as_deref())?;
2466                resources.switch_ports.push(port);
2467                net_backend_resources::dio::WindowsDirectIoHandle {
2468                    switch_port_id: net_backend_resources::dio::SwitchPortId {
2469                        switch: port_id.switch,
2470                        port: port_id.port,
2471                    },
2472                }
2473                .into_resource()
2474            }}
2475            _ => {{
2476                let _ = id;
2477                bail!("cannot use dio on non-windows platforms")
2478            }}
2479        },
2480        EndpointConfigCli::Tap { name } => cfg_select! {
2481            target_os = "linux" => {{
2482                let fd = net_tap::tap::open_tap(name)
2483                    .with_context(|| format!("failed to open TAP device '{name}'"))?;
2484                net_backend_resources::tap::TapHandle { fd }.into_resource()
2485            }}
2486            _ => {{
2487                let _ = name;
2488                bail!("TAP backend is only supported on Linux")
2489            }}
2490        },
2491    };
2492
2493    let mac_address = cli_cfg.mac_address.unwrap_or_else(|| {
2494        let mut mac_address = [0x00, 0x15, 0x5D, 0, 0, 0];
2495        getrandom::fill(&mut mac_address[3..]).expect("rng failure");
2496        mac_address.into()
2497    });
2498
2499    // Pick a fixed instance ID based on the index.
2500    const BASE_INSTANCE_ID: Guid = guid::guid!("00000000-da43-11ed-936a-00155d6db52f");
2501    let instance_id = Guid {
2502        data1: *index as u32,
2503        ..BASE_INSTANCE_ID
2504    };
2505    *index += 1;
2506
2507    Ok(NicConfig {
2508        vtl: cli_cfg.vtl,
2509        instance_id,
2510        endpoint,
2511        mac_address,
2512        max_queues: cli_cfg.max_queues,
2513        enable_rx_gso: cli_cfg.enable_rx_gso,
2514        pcie_port: cli_cfg.pcie_port.clone(),
2515    })
2516}
2517
2518#[derive(Debug)]
2519struct NicConfig {
2520    vtl: DeviceVtl,
2521    instance_id: Guid,
2522    mac_address: MacAddress,
2523    endpoint: Resource<NetEndpointHandleKind>,
2524    max_queues: Option<u16>,
2525    enable_rx_gso: bool,
2526    pcie_port: Option<String>,
2527}
2528
2529impl NicConfig {
2530    fn into_netvsp_handle(self) -> (DeviceVtl, Resource<VmbusDeviceHandleKind>) {
2531        (
2532            self.vtl,
2533            netvsp_resources::NetvspHandle {
2534                instance_id: self.instance_id,
2535                mac_address: self.mac_address,
2536                endpoint: self.endpoint,
2537                max_queues: self.max_queues,
2538            }
2539            .into_resource(),
2540        )
2541    }
2542}
2543
2544fn non_volatile_store_resource(
2545    with_vmgs: bool,
2546    file_id: vmgs_format::FileId,
2547) -> Resource<NonVolatileStoreKind> {
2548    if with_vmgs {
2549        VmgsFileHandle::new(file_id, true).into_resource()
2550    } else {
2551        EphemeralNonVolatileStoreHandle.into_resource()
2552    }
2553}
2554
2555async fn open_vmgs(
2556    kind: &DiskCliKind,
2557    provision: ProvisionCondition,
2558    encryption_policy: GuestStateEncryptionPolicy,
2559) -> anyhow::Result<VmgsResource> {
2560    let disk = VmgsDisk {
2561        disk: disk_open(kind, false).await?,
2562        encryption_policy,
2563    };
2564    Ok(match provision {
2565        ProvisionCondition::OnEmpty => VmgsResource::Disk(disk),
2566        ProvisionCondition::OnFailure => VmgsResource::ReprovisionOnFailure(disk),
2567        ProvisionCondition::Single => VmgsResource::ReprovisionSingle(disk),
2568    })
2569}
2570
2571enum LayerOrDisk {
2572    Layer(DiskLayerDescription),
2573    Disk(Resource<DiskHandleKind>),
2574}
2575
2576async fn disk_open(
2577    disk_cli: &DiskCliKind,
2578    read_only: bool,
2579) -> anyhow::Result<Resource<DiskHandleKind>> {
2580    let mut layers = Vec::new();
2581    disk_open_inner(disk_cli, read_only, &mut layers).await?;
2582    if layers.len() == 1 && matches!(layers[0], LayerOrDisk::Disk(_)) {
2583        let LayerOrDisk::Disk(disk) = layers.pop().unwrap() else {
2584            unreachable!()
2585        };
2586        Ok(disk)
2587    } else {
2588        Ok(Resource::new(disk_backend_resources::LayeredDiskHandle {
2589            layers: layers
2590                .into_iter()
2591                .map(|layer| match layer {
2592                    LayerOrDisk::Layer(layer) => layer,
2593                    LayerOrDisk::Disk(disk) => DiskLayerDescription {
2594                        layer: DiskLayerHandle(disk).into_resource(),
2595                        read_cache: false,
2596                        write_through: false,
2597                    },
2598                })
2599                .collect(),
2600        }))
2601    }
2602}
2603
2604fn disk_open_inner<'a>(
2605    disk_cli: &'a DiskCliKind,
2606    read_only: bool,
2607    layers: &'a mut Vec<LayerOrDisk>,
2608) -> futures::future::BoxFuture<'a, anyhow::Result<()>> {
2609    Box::pin(async move {
2610        fn layer<T: IntoResource<DiskLayerHandleKind>>(layer: T) -> LayerOrDisk {
2611            LayerOrDisk::Layer(layer.into_resource().into())
2612        }
2613        fn disk<T: IntoResource<DiskHandleKind>>(disk: T) -> LayerOrDisk {
2614            LayerOrDisk::Disk(disk.into_resource())
2615        }
2616        match disk_cli {
2617            &DiskCliKind::Memory(len) => {
2618                layers.push(layer(RamDiskLayerHandle {
2619                    len: Some(len),
2620                    sector_size: None,
2621                }));
2622            }
2623            DiskCliKind::File {
2624                path,
2625                create_with_len,
2626                direct,
2627            } => layers.push(LayerOrDisk::Disk(if let Some(size) = create_with_len {
2628                create_disk_type(
2629                    path,
2630                    *size,
2631                    OpenDiskOptions {
2632                        read_only: false,
2633                        direct: *direct,
2634                    },
2635                )
2636                .with_context(|| format!("failed to create {}", path.display()))?
2637            } else {
2638                open_disk_type(
2639                    path,
2640                    OpenDiskOptions {
2641                        read_only,
2642                        direct: *direct,
2643                    },
2644                )
2645                .await
2646                .with_context(|| format!("failed to open {}", path.display()))?
2647            })),
2648            DiskCliKind::Blob { kind, url } => {
2649                layers.push(disk(disk_backend_resources::BlobDiskHandle {
2650                    url: url.to_owned(),
2651                    format: match kind {
2652                        cli_args::BlobKind::Flat => disk_backend_resources::BlobDiskFormat::Flat,
2653                        cli_args::BlobKind::Vhd1 => {
2654                            disk_backend_resources::BlobDiskFormat::FixedVhd1
2655                        }
2656                    },
2657                }))
2658            }
2659            DiskCliKind::MemoryDiff(inner) => {
2660                layers.push(layer(RamDiskLayerHandle {
2661                    len: None,
2662                    sector_size: None,
2663                }));
2664                disk_open_inner(inner, true, layers).await?;
2665            }
2666            DiskCliKind::PersistentReservationsWrapper(inner) => {
2667                layers.push(disk(disk_backend_resources::DiskWithReservationsHandle(
2668                    disk_open(inner, read_only).await?,
2669                )))
2670            }
2671            DiskCliKind::DelayDiskWrapper {
2672                delay_ms,
2673                disk: inner,
2674            } => layers.push(disk(DelayDiskHandle {
2675                delay: CellUpdater::new(Duration::from_millis(*delay_ms)).cell(),
2676                disk: disk_open(inner, read_only).await?,
2677            })),
2678            DiskCliKind::Crypt {
2679                disk: inner,
2680                cipher,
2681                key_file,
2682            } => layers.push(disk(disk_crypt_resources::DiskCryptHandle {
2683                disk: disk_open(inner, read_only).await?,
2684                cipher: match cipher {
2685                    cli_args::DiskCipher::XtsAes256 => disk_crypt_resources::Cipher::XtsAes256,
2686                },
2687                key: fs_err::read(key_file).context("failed to read key file")?,
2688            })),
2689            DiskCliKind::Sqlite {
2690                path,
2691                create_with_len,
2692            } => {
2693                // FUTURE: this code should be responsible for opening
2694                // file-handle(s) itself, and passing them into sqlite via a custom
2695                // vfs. For now though - simply check if the file exists or not, and
2696                // perform early validation of filesystem-level create options.
2697                match (create_with_len.is_some(), path.exists()) {
2698                    (true, true) => anyhow::bail!(
2699                        "cannot create new sqlite disk at {} - file already exists",
2700                        path.display()
2701                    ),
2702                    (false, false) => anyhow::bail!(
2703                        "cannot open sqlite disk at {} - file not found",
2704                        path.display()
2705                    ),
2706                    _ => {}
2707                }
2708
2709                layers.push(layer(SqliteDiskLayerHandle {
2710                    dbhd_path: path.display().to_string(),
2711                    format_dbhd: create_with_len.map(|len| {
2712                        disk_backend_resources::layer::SqliteDiskLayerFormatParams {
2713                            logically_read_only: false,
2714                            len: Some(len),
2715                        }
2716                    }),
2717                }));
2718            }
2719            DiskCliKind::SqliteDiff { path, create, disk } => {
2720                // FUTURE: this code should be responsible for opening
2721                // file-handle(s) itself, and passing them into sqlite via a custom
2722                // vfs. For now though - simply check if the file exists or not, and
2723                // perform early validation of filesystem-level create options.
2724                match (create, path.exists()) {
2725                    (true, true) => anyhow::bail!(
2726                        "cannot create new sqlite disk at {} - file already exists",
2727                        path.display()
2728                    ),
2729                    (false, false) => anyhow::bail!(
2730                        "cannot open sqlite disk at {} - file not found",
2731                        path.display()
2732                    ),
2733                    _ => {}
2734                }
2735
2736                layers.push(layer(SqliteDiskLayerHandle {
2737                    dbhd_path: path.display().to_string(),
2738                    format_dbhd: create.then_some(
2739                        disk_backend_resources::layer::SqliteDiskLayerFormatParams {
2740                            logically_read_only: false,
2741                            len: None,
2742                        },
2743                    ),
2744                }));
2745                disk_open_inner(disk, true, layers).await?;
2746            }
2747            DiskCliKind::AutoCacheSqlite {
2748                cache_path,
2749                key,
2750                disk,
2751            } => {
2752                layers.push(LayerOrDisk::Layer(DiskLayerDescription {
2753                    read_cache: true,
2754                    write_through: false,
2755                    layer: SqliteAutoCacheDiskLayerHandle {
2756                        cache_path: cache_path.clone(),
2757                        cache_key: key.clone(),
2758                    }
2759                    .into_resource(),
2760                }));
2761                disk_open_inner(disk, read_only, layers).await?;
2762            }
2763        }
2764        Ok(())
2765    })
2766}
2767
2768/// Get the system page size.
2769pub(crate) fn system_page_size() -> u32 {
2770    sparse_mmap::SparseMapping::page_size() as u32
2771}
2772
2773/// The guest architecture string, derived from the compile-time `guest_arch` cfg.
2774pub(crate) const GUEST_ARCH: &str = cfg_select! {
2775    guest_arch = "x86_64" => "x86_64",
2776    guest_arch = "aarch64" => "aarch64",
2777};
2778
2779/// Open a snapshot directory and validate it against the current VM config.
2780/// Returns the shared memory fd (from memory.bin) and the saved device state.
2781fn prepare_snapshot_restore(
2782    snapshot_dir: &Path,
2783    opt: &Options,
2784) -> anyhow::Result<(
2785    openvmm_defs::worker::SharedMemoryFd,
2786    mesh::payload::message::ProtobufMessage,
2787)> {
2788    let (manifest, state_bytes) = openvmm_helpers::snapshot::read_snapshot(snapshot_dir)?;
2789
2790    // Validate manifest against current VM config.
2791    openvmm_helpers::snapshot::validate_manifest(
2792        &manifest,
2793        GUEST_ARCH,
2794        opt.memory_size(),
2795        opt.processors,
2796        system_page_size(),
2797    )?;
2798
2799    // Open memory.bin (existing file, no create, no resize).
2800    let memory_file = fs_err::OpenOptions::new()
2801        .read(true)
2802        .write(true)
2803        .open(snapshot_dir.join("memory.bin"))?;
2804
2805    // Validate file size matches expected memory size.
2806    let file_size = memory_file.metadata()?.len();
2807    if file_size != manifest.memory_size_bytes {
2808        anyhow::bail!(
2809            "memory.bin size ({file_size} bytes) doesn't match manifest ({} bytes)",
2810            manifest.memory_size_bytes,
2811        );
2812    }
2813
2814    let shared_memory_fd =
2815        openvmm_helpers::shared_memory::file_to_shared_memory_fd(memory_file.into())?;
2816
2817    // Reconstruct ProtobufMessage from the saved state bytes.
2818    // The save side wrote mesh::payload::encode(ProtobufMessage), so we decode
2819    // back to ProtobufMessage.
2820    let state_msg: mesh::payload::message::ProtobufMessage = mesh::payload::decode(&state_bytes)
2821        .context("failed to decode saved state from snapshot")?;
2822
2823    Ok((shared_memory_fd, state_msg))
2824}
2825
2826fn do_main(pidfile_guard: &mut Option<pidfile::Pidfile>) -> anyhow::Result<i32> {
2827    #[cfg(windows)]
2828    pal::windows::disable_hard_error_dialog();
2829
2830    let _tracing_guard = tracing_init::enable_tracing()?;
2831
2832    // Try to run as a worker host.
2833    // On success the worker runs to completion and then exits the process (does
2834    // not return). Any worker host setup errors are return and bubbled up.
2835    meshworker::run_vmm_mesh_host()?;
2836
2837    let opt = cli_args::parse_options();
2838
2839    // Print the version number. This comes after argument parsing to not interfere
2840    // with --version and --help.
2841    tracing::info!(version = openvmm_build_info::get().version());
2842
2843    if let Some(path) = &opt.write_saved_state_proto {
2844        mesh::payload::protofile::DescriptorWriter::new(vmcore::save_restore::saved_state_roots())
2845            .write_to_path(path)
2846            .context("failed to write protobuf descriptors")?;
2847        return Ok(0);
2848    }
2849
2850    if let Some(ref path) = opt.pidfile {
2851        *pidfile_guard = Some(pidfile::Pidfile::new(path).context("failed to create pidfile")?);
2852    }
2853
2854    if let Some(path) = opt.relay_console_path {
2855        let console_title = opt.relay_console_title.unwrap_or_default();
2856        return console_relay::relay_console(&path, console_title.as_str()).map(|()| 0);
2857    }
2858
2859    #[cfg(any(feature = "grpc", feature = "ttrpc"))]
2860    {
2861        let rpc = opt
2862            .rpc
2863            .as_ref()
2864            .map(|rpc| {
2865                let transport = match rpc.transport {
2866                    cli_args::RpcTransportCli::Auto => ttrpc::RpcTransport::Auto,
2867                    cli_args::RpcTransportCli::Ttrpc => ttrpc::RpcTransport::Ttrpc,
2868                    cli_args::RpcTransportCli::Grpc => ttrpc::RpcTransport::Grpc,
2869                };
2870                (rpc.path.as_path(), transport)
2871            })
2872            .or_else(|| {
2873                opt.ttrpc
2874                    .as_deref()
2875                    .map(|p| (p, ttrpc::RpcTransport::Ttrpc))
2876            })
2877            .or_else(|| opt.grpc.as_deref().map(|p| (p, ttrpc::RpcTransport::Grpc)));
2878
2879        if let Some((path, transport)) = rpc {
2880            return block_on(async {
2881                let _ = std::fs::remove_file(path);
2882                let listener =
2883                    unix_socket::UnixListener::bind(path).context("failed to bind to socket")?;
2884
2885                // This is a local launch
2886                let (exit_code_send, exit_code_recv) = mesh::oneshot();
2887                let mut handle =
2888                    mesh_worker::launch_local_worker::<ttrpc::TtrpcWorker>(ttrpc::Parameters {
2889                        listener,
2890                        transport,
2891                        exit_code: exit_code_send,
2892                    })
2893                    .await?;
2894
2895                tracing::info!(%transport, path = %path.display(), "listening");
2896
2897                // Signal the parent process that the server is ready.
2898                pal::close_stdout().context("failed to close stdout")?;
2899
2900                handle.join().await?;
2901
2902                exit_code_recv
2903                    .await
2904                    .context("RPC worker did not report an exit code")
2905            });
2906        }
2907    }
2908
2909    DefaultPool::run_with(async |driver| run_control(&driver, opt).await)
2910}
2911
2912fn new_hvsock_service_id(port: u32) -> Guid {
2913    // This GUID is an embedding of the AF_VSOCK port into an
2914    // AF_HYPERV service ID.
2915    Guid {
2916        data1: port,
2917        .."00000000-facb-11e6-bd58-64006a7986d3".parse().unwrap()
2918    }
2919}
2920
2921async fn run_control(driver: &DefaultDriver, opt: Options) -> anyhow::Result<i32> {
2922    let mut mesh = Some(VmmMesh::new(&driver, opt.single_process)?);
2923    let result = run_control_inner(driver, &mut mesh, opt).await;
2924    // If setup failed before the mesh was handed to the controller, shut it
2925    // down so the child host process exits cleanly without noisy logs.
2926    if let Some(mesh) = mesh {
2927        mesh.shutdown().await;
2928    }
2929    result
2930}
2931
2932async fn run_control_inner(
2933    driver: &DefaultDriver,
2934    mesh_slot: &mut Option<VmmMesh>,
2935    opt: Options,
2936) -> anyhow::Result<i32> {
2937    let mesh = mesh_slot.as_ref().unwrap();
2938    let (mut vm_config, mut resources) = vm_config_from_command_line(driver, mesh, &opt).await?;
2939
2940    let mut vnc_worker = None;
2941    if opt.gfx || opt.vnc.vnc {
2942        // Parse the listen address. Try as a full SocketAddr (host:port) first;
2943        // fall back to a bare IP, using the configured port.
2944        let addr: std::net::SocketAddr = if let Ok(sa) =
2945            opt.vnc.vnc_listen.parse::<std::net::SocketAddr>()
2946        {
2947            sa
2948        } else {
2949            let ip: std::net::IpAddr = opt.vnc.vnc_listen.parse().with_context(|| {
2950                format!(
2951                    "invalid VNC listen address: {} (expected IP address or socket address like [::1]:5900)",
2952                    opt.vnc.vnc_listen
2953                )
2954            })?;
2955            std::net::SocketAddr::new(ip, opt.vnc.vnc_port)
2956        };
2957
2958        let socket = socket2::Socket::new(
2959            if addr.is_ipv6() {
2960                socket2::Domain::IPV6
2961            } else {
2962                socket2::Domain::IPV4
2963            },
2964            socket2::Type::STREAM,
2965            None,
2966        )
2967        .with_context(|| format!("creating VNC socket for {}", addr))?;
2968
2969        if addr.is_ipv6() {
2970            if let Err(e) = socket.set_only_v6(false) {
2971                tracing::warn!(
2972                    error = %e,
2973                    "failed to enable dual-stack on IPv6 VNC socket, IPv4 clients may not be able to connect"
2974                );
2975            }
2976        }
2977        socket.set_reuse_address(true)?;
2978        socket
2979            .bind(&addr.into())
2980            .with_context(|| format!("binding VNC socket to {}", addr))?;
2981        socket
2982            .listen(128)
2983            .with_context(|| format!("listening on VNC socket {}", addr))?;
2984        let listener: TcpListener = socket.into();
2985
2986        if !addr.ip().is_loopback() {
2987            tracing::warn!(
2988                address = %addr,
2989                "VNC server listening on non-localhost address without authentication"
2990            );
2991        }
2992
2993        let input_send = vm_config.input.sender();
2994        let framebuffer = resources
2995            .framebuffer_access
2996            .take()
2997            .expect("synth video enabled");
2998
2999        let vnc_host = mesh
3000            .make_host("vnc", None)
3001            .await
3002            .context("spawning vnc process failed")?;
3003
3004        vnc_worker = Some(
3005            vnc_host
3006                .launch_worker(
3007                    vnc_worker_defs::VNC_WORKER_TCP,
3008                    VncParameters {
3009                        listener,
3010                        framebuffer,
3011                        input_send,
3012                        dirty_recv: resources.dirty_rect_recv.take(),
3013                        max_clients: opt.vnc.vnc_max_clients,
3014                        evict_oldest: opt.vnc.vnc_evict_oldest,
3015                    },
3016                )
3017                .await?,
3018        )
3019    }
3020
3021    // spin up the debug worker
3022    let gdb_worker = if let Some(port) = opt.gdb {
3023        let listener = TcpListener::bind(format!("127.0.0.1:{}", port))
3024            .with_context(|| format!("binding to gdb port {}", port))?;
3025
3026        let (req_tx, req_rx) = mesh::channel();
3027        vm_config.debugger_rpc = Some(req_rx);
3028
3029        let gdb_host = mesh
3030            .make_host("gdb", None)
3031            .await
3032            .context("spawning gdbstub process failed")?;
3033
3034        Some(
3035            gdb_host
3036                .launch_worker(
3037                    debug_worker_defs::DEBUGGER_WORKER,
3038                    debug_worker_defs::DebuggerParameters {
3039                        listener,
3040                        req_chan: req_tx,
3041                        vp_count: vm_config.processor_topology.proc_count,
3042                        target_arch: cfg_select! {
3043                            guest_arch = "x86_64" => debug_worker_defs::TargetArch::X86_64,
3044                            guest_arch = "aarch64" => debug_worker_defs::TargetArch::Aarch64,
3045                        },
3046                    },
3047                )
3048                .await
3049                .context("failed to launch gdbstub worker")?,
3050        )
3051    } else {
3052        None
3053    };
3054
3055    // spin up the VM
3056    let (vm_rpc, rpc_recv) = mesh::channel();
3057    let (notify_send, notify_recv) = mesh::channel();
3058    let vm_worker = {
3059        let vm_host = mesh.make_host("vm", opt.log_file.clone()).await?;
3060
3061        let (shared_memory, saved_state) = if let Some(snapshot_dir) = &opt.restore_snapshot {
3062            let (fd, state_msg) = prepare_snapshot_restore(snapshot_dir, &opt)?;
3063            (Some(fd), Some(state_msg))
3064        } else {
3065            let shared_memory = opt
3066                .memory_backing_file()
3067                .map(|path| {
3068                    openvmm_helpers::shared_memory::open_memory_backing_file(
3069                        path,
3070                        opt.memory_size(),
3071                    )
3072                })
3073                .transpose()?;
3074            (shared_memory, None)
3075        };
3076
3077        let params = VmWorkerParameters {
3078            hypervisor: match &opt.hypervisor {
3079                Some(name) => openvmm_helpers::hypervisor::hypervisor_resource(name)?,
3080                None => openvmm_helpers::hypervisor::choose_hypervisor()?,
3081            },
3082            cfg: vm_config,
3083            saved_state,
3084            shared_memory,
3085            rpc: rpc_recv,
3086            notify: notify_send,
3087        };
3088        vm_host
3089            .launch_worker(VM_WORKER, params)
3090            .await
3091            .context("failed to launch vm worker")?
3092    };
3093
3094    if opt.restore_snapshot.is_some() {
3095        tracing::info!("restoring VM from snapshot");
3096    }
3097
3098    if !opt.paused {
3099        vm_rpc.call(VmRpc::Resume, ()).await?;
3100    }
3101
3102    let paravisor_diag = Arc::new(diag_client::DiagClient::from_dialer(
3103        driver.clone(),
3104        DiagDialer {
3105            driver: driver.clone(),
3106            vm_rpc: vm_rpc.clone(),
3107            openhcl_vtl: if opt.vtl2 {
3108                DeviceVtl::Vtl2
3109            } else {
3110                DeviceVtl::Vtl0
3111            },
3112        },
3113    ));
3114
3115    let diag_inspector = DiagInspector::new(driver.clone(), paravisor_diag.clone());
3116
3117    // Create channels between the REPL and VmController.
3118    let (vm_controller_send, vm_controller_recv) = mesh::channel();
3119    let (vm_controller_event_send, vm_controller_event_recv) = mesh::channel();
3120
3121    let has_vtl2 = resources.vtl2_settings.is_some();
3122    let serial_driver = resources
3123        .serial_driver
3124        .take()
3125        .expect("serial driver must outlive serial resources");
3126
3127    // Build the VmController with exclusive resources.
3128    let controller = vm_controller::VmController {
3129        mesh: mesh_slot.take().unwrap(),
3130        vm_worker,
3131        vnc_worker,
3132        gdb_worker,
3133        diag_inspector: Some(diag_inspector),
3134        vtl2_settings: resources.vtl2_settings,
3135        ged_rpc: resources.ged_rpc.clone(),
3136        vm_rpc: vm_rpc.clone(),
3137        paravisor_diag: Some(paravisor_diag),
3138        igvm_path: opt.igvm.as_ref().map(|igvm| igvm.firmware.clone()),
3139        memory_backing_file: opt.memory_backing_file().cloned(),
3140        memory: opt.memory_size(),
3141        processors: opt.processors,
3142        log_file: opt.log_file.clone(),
3143        crash_dump_path: opt.crash_dump_path.clone(),
3144        guest_power_actions: vm_controller::GuestPowerActions {
3145            shutdown: opt.guest_shutdown_action,
3146            reset: opt.guest_reset_action,
3147            crash: opt.guest_crash_action,
3148            watchdog: opt.guest_watchdog_action,
3149        },
3150    };
3151
3152    // Spawn the VmController as a task.
3153    let controller_task = driver.spawn(
3154        "vm-controller",
3155        controller.run(vm_controller_recv, vm_controller_event_send, notify_recv),
3156    );
3157
3158    // Run the REPL with shareable resources.
3159    let repl_result = repl::run_repl(
3160        driver,
3161        repl::ReplResources {
3162            vm_rpc,
3163            vm_controller: vm_controller_send,
3164            vm_controller_events: vm_controller_event_recv,
3165            scsi_rpc: resources.scsi_rpc,
3166            nvme_vtl2_rpc: resources.nvme_vtl2_rpc,
3167            consomme_rpc: resources.consomme_rpc,
3168            shutdown_ic: resources.shutdown_ic,
3169            acpi_power_button: resources.acpi_power_button,
3170            kvp_ic: resources.kvp_ic,
3171            console_in: resources.console_in,
3172            has_vtl2,
3173        },
3174    )
3175    .await;
3176
3177    // Wait for the controller task to finish (it stops the VM worker and
3178    // shuts down the mesh).
3179    controller_task.await;
3180    drop(serial_driver);
3181
3182    // run_repl returns the exit status: the code the guest drove via an opt-in
3183    // exit (VmControllerEvent::ExitRequested), or 0 when the VM stopped normally.
3184    repl_result
3185}
3186
3187struct DiagDialer {
3188    driver: DefaultDriver,
3189    vm_rpc: mesh::Sender<VmRpc>,
3190    openhcl_vtl: DeviceVtl,
3191}
3192
3193impl mesh_rpc::client::Dial for DiagDialer {
3194    type Stream = PolledSocket<unix_socket::UnixStream>;
3195
3196    async fn dial(&mut self) -> io::Result<Self::Stream> {
3197        let service_id = new_hvsock_service_id(1);
3198        let socket = self
3199            .vm_rpc
3200            .call_failable(
3201                VmRpc::ConnectHvsock,
3202                (
3203                    CancelContext::new().with_timeout(Duration::from_secs(2)),
3204                    service_id,
3205                    self.openhcl_vtl,
3206                ),
3207            )
3208            .await
3209            .map_err(io::Error::other)?;
3210
3211        PolledSocket::new(&self.driver, socket)
3212    }
3213}
3214
3215/// An object that implements [`InspectMut`] by sending an inspect request over
3216/// TTRPC to the guest (typically the paravisor running in VTL2), then stitching
3217/// the response back into the inspect tree.
3218///
3219/// This also caches the TTRPC connection to the guest so that only the first
3220/// inspect request has to wait for the connection to be established.
3221pub(crate) struct DiagInspector(DiagInspectorInner);
3222
3223enum DiagInspectorInner {
3224    NotStarted(DefaultDriver, Arc<diag_client::DiagClient>),
3225    Started {
3226        send: mesh::Sender<inspect::Deferred>,
3227        _task: Task<()>,
3228    },
3229    Invalid,
3230}
3231
3232impl DiagInspector {
3233    pub fn new(driver: DefaultDriver, diag_client: Arc<diag_client::DiagClient>) -> Self {
3234        Self(DiagInspectorInner::NotStarted(driver, diag_client))
3235    }
3236
3237    fn start(&mut self) -> &mesh::Sender<inspect::Deferred> {
3238        loop {
3239            match self.0 {
3240                DiagInspectorInner::NotStarted { .. } => {
3241                    let DiagInspectorInner::NotStarted(driver, client) =
3242                        std::mem::replace(&mut self.0, DiagInspectorInner::Invalid)
3243                    else {
3244                        unreachable!()
3245                    };
3246                    let (send, recv) = mesh::channel();
3247                    let task = driver.clone().spawn("diag-inspect", async move {
3248                        Self::run(&client, recv).await
3249                    });
3250
3251                    self.0 = DiagInspectorInner::Started { send, _task: task };
3252                }
3253                DiagInspectorInner::Started { ref send, .. } => break send,
3254                DiagInspectorInner::Invalid => unreachable!(),
3255            }
3256        }
3257    }
3258
3259    async fn run(
3260        diag_client: &diag_client::DiagClient,
3261        mut recv: mesh::Receiver<inspect::Deferred>,
3262    ) {
3263        while let Some(deferred) = recv.next().await {
3264            let info = deferred.external_request();
3265            let result = match info.request_type {
3266                inspect::ExternalRequestType::Inspect { depth } => {
3267                    if depth == 0 {
3268                        Ok(inspect::Node::Unevaluated)
3269                    } else {
3270                        // TODO: Support taking timeouts from the command line
3271                        diag_client
3272                            .inspect(info.path, Some(depth - 1), Some(Duration::from_secs(1)))
3273                            .await
3274                    }
3275                }
3276                inspect::ExternalRequestType::Update { value } => {
3277                    (diag_client.update(info.path, value).await).map(inspect::Node::Value)
3278                }
3279            };
3280            deferred.complete_external(
3281                result.unwrap_or_else(|err| {
3282                    inspect::Node::Failed(inspect::Error::Mesh(format!("{err:#}")))
3283                }),
3284                inspect::SensitivityLevel::Unspecified,
3285            )
3286        }
3287    }
3288}
3289
3290impl InspectMut for DiagInspector {
3291    fn inspect_mut(&mut self, req: inspect::Request<'_>) {
3292        self.start().send(req.defer());
3293    }
3294}
3295
3296#[cfg(test)]
3297mod tests {
3298    use super::*;
3299    use clap::Parser;
3300    use std::fs::File;
3301    use test_with_tracing::test;
3302    use vm_resource::ResourceId;
3303
3304    /// The same boot flags give the same ACPI power button with and without
3305    /// `--restore-snapshot`.
3306    #[test]
3307    fn restore_builds_the_acpi_power_button_of_its_boot_command_line() {
3308        DefaultPool::run_with(async |driver| {
3309            let temp_dir = tempfile::tempdir().unwrap();
3310            let file = |name: &str| {
3311                let path = temp_dir.path().join(name);
3312                File::create(&path).unwrap();
3313                path.to_str().unwrap().to_owned()
3314            };
3315            let kernel = file("kernel");
3316            let initrd = file("initrd");
3317            let uefi = format!("--uefi=firmware={}", file("firmware"));
3318            // The config build does not read the snapshot directory.
3319            let snapshot = temp_dir.path().join("snapshot");
3320            let linux = ["--kernel", kernel.as_str(), "--initrd", initrd.as_str()];
3321
3322            for (boot, has_button) in [
3323                (linux.to_vec(), true),
3324                ([&linux[..], &["--device-tree"]].concat(), false),
3325                (vec![uefi.as_str()], true),
3326            ] {
3327                for restore in [false, true] {
3328                    let mut args = vec!["openvmm", "--single-process"];
3329                    args.extend(&boot);
3330                    if restore {
3331                        args.extend(["--restore-snapshot", snapshot.to_str().unwrap()]);
3332                    }
3333                    let opt = Options::try_parse_from(&args).unwrap();
3334                    let mesh = VmmMesh::new(&driver, true).unwrap();
3335                    let (config, _) = vm_config_from_command_line(driver.clone(), &mesh, &opt)
3336                        .await
3337                        .unwrap();
3338                    mesh.shutdown().await;
3339
3340                    let built = config
3341                        .chipset_devices
3342                        .iter()
3343                        .any(|device| device.name == "acpi-power-button");
3344                    assert_eq!(built, has_button, "{args:?}");
3345                }
3346            }
3347        });
3348    }
3349
3350    #[test]
3351    fn selects_non_volatile_store_backing() {
3352        for file_id in [
3353            vmgs_format::FileId::BIOS_NVRAM,
3354            vmgs_format::FileId::TPM_PPI,
3355            tpm_vmgs::tpm_nvram_file_id(TpmVersion::V138),
3356            tpm_vmgs::tpm_nvram_file_id(TpmVersion::V185),
3357        ] {
3358            assert_eq!(
3359                non_volatile_store_resource(true, file_id).id(),
3360                <VmgsFileHandle as ResourceId<NonVolatileStoreKind>>::ID
3361            );
3362            assert_eq!(
3363                non_volatile_store_resource(false, file_id).id(),
3364                <EphemeralNonVolatileStoreHandle as ResourceId<NonVolatileStoreKind>>::ID
3365            );
3366        }
3367    }
3368
3369    #[test]
3370    fn opens_vmgs_with_provisioning_and_encryption_policy() {
3371        block_on(async {
3372            for provision in [
3373                ProvisionCondition::OnEmpty,
3374                ProvisionCondition::OnFailure,
3375                ProvisionCondition::Single,
3376            ] {
3377                for encryption_policy in [
3378                    GuestStateEncryptionPolicy::None(true),
3379                    GuestStateEncryptionPolicy::GspById(true),
3380                ] {
3381                    let vmgs = open_vmgs(
3382                        &DiskCliKind::Memory(vmgs_format::VMGS_DEFAULT_CAPACITY),
3383                        provision,
3384                        encryption_policy,
3385                    )
3386                    .await
3387                    .unwrap();
3388                    let disk = match (provision, vmgs) {
3389                        (ProvisionCondition::OnEmpty, VmgsResource::Disk(disk))
3390                        | (
3391                            ProvisionCondition::OnFailure,
3392                            VmgsResource::ReprovisionOnFailure(disk),
3393                        )
3394                        | (ProvisionCondition::Single, VmgsResource::ReprovisionSingle(disk)) => {
3395                            disk
3396                        }
3397                        _ => panic!("incorrect VMGS provisioning"),
3398                    };
3399                    assert!(matches!(
3400                        (encryption_policy, disk.encryption_policy),
3401                        (
3402                            GuestStateEncryptionPolicy::None(true),
3403                            GuestStateEncryptionPolicy::None(true)
3404                        ) | (
3405                            GuestStateEncryptionPolicy::GspById(true),
3406                            GuestStateEncryptionPolicy::GspById(true)
3407                        )
3408                    ));
3409                }
3410            }
3411        });
3412    }
3413
3414    /// Declaring controllers alone must not create implicit device attachments.
3415    #[test]
3416    fn usb_cli_empty_controllers() {
3417        let opt = Options::try_parse_from(["openvmm", "--usb-xhci", "usb0", "--usb-xhci", "usb1"])
3418            .unwrap();
3419        let controllers = usb_controller_configs(&opt.usb_xhci, &opt.usb_device).unwrap();
3420        assert_eq!(controllers.len(), 2);
3421        assert!(
3422            controllers
3423                .iter()
3424                .all(|(_, config)| config.devices.is_empty())
3425        );
3426    }
3427
3428    /// A sole controller supplies omitted on values; omitted connectors are left to the resolver.
3429    #[test]
3430    fn usb_cli_infers_single_controller() {
3431        let opt = Options::try_parse_from([
3432            "openvmm",
3433            "--usb-xhci",
3434            "usb0",
3435            "--usb-device",
3436            "mock-hid",
3437            "--usb-device",
3438            "mock-loopback",
3439            "--usb-device",
3440            "mock-storage",
3441            "--usb-device",
3442            "mock-mutt",
3443            "--usb-device",
3444            "mock-supermutt",
3445            "--usb-device",
3446            r"winusb,instance_id=USB\A",
3447            "--usb-device",
3448            "winusb-all",
3449        ])
3450        .unwrap();
3451        let controllers = usb_controller_configs(&opt.usb_xhci, &opt.usb_device).unwrap();
3452        assert_eq!(controllers[0].0, "usb0");
3453        assert_eq!(controllers[0].1.devices.len(), 7);
3454        for config in controllers[0].1.devices.iter().take(6) {
3455            let usb_config_resources::UsbDeviceConfig::Device { connector, .. } = config else {
3456                panic!("expected a device attachment");
3457            };
3458            assert_eq!(*connector, None);
3459        }
3460        assert!(matches!(
3461            controllers[0].1.devices[6],
3462            usb_config_resources::UsbDeviceConfig::Discover(_)
3463        ));
3464    }
3465
3466    /// An omitted on value is ambiguous when more than one controller is declared.
3467    #[test]
3468    fn usb_cli_requires_explicit_controller_when_ambiguous() {
3469        for selection in [
3470            "mock-hid",
3471            "mock-hid,connector=1",
3472            r"winusb,instance_id=USB\A",
3473            "winusb-all",
3474        ] {
3475            let opt = Options::try_parse_from([
3476                "openvmm",
3477                "--usb-xhci",
3478                "usb0",
3479                "--usb-xhci",
3480                "usb1",
3481                "--usb-device",
3482                selection,
3483            ])
3484            .unwrap();
3485            let error = usb_controller_configs(&opt.usb_xhci, &opt.usb_device).unwrap_err();
3486            assert!(error.to_string().contains("on=PCIE_PORT is required"));
3487        }
3488    }
3489
3490    /// Normalization preserves one-based connector numbers and explicit mock overrides.
3491    #[test]
3492    fn usb_cli_preserves_one_based_connectors_and_mock_overrides() {
3493        let opt = Options::try_parse_from([
3494            "openvmm",
3495            "--usb-xhci",
3496            "usb0",
3497            "--usb-xhci",
3498            "usb1",
3499            "--usb-device",
3500            "mock-hid,on=usb0,connector=1,vid=0x1234,pid=65535,serial=HID001",
3501            "--usb-device",
3502            "mock-storage,on=usb1,connector=15,blocks=4096",
3503        ])
3504        .unwrap();
3505        let controllers = usb_controller_configs(&opt.usb_xhci, &opt.usb_device).unwrap();
3506        let usb_config_resources::UsbDeviceConfig::Device { connector, device } =
3507            &controllers[0].1.devices[0]
3508        else {
3509            panic!("expected fixed HID attachment");
3510        };
3511        assert_eq!(*connector, Some(1));
3512        assert_eq!(device.id(), "mock_hid_config");
3513        let usb_config_resources::UsbDeviceConfig::Device { connector, .. } =
3514            &controllers[1].1.devices[0]
3515        else {
3516            panic!("expected fixed storage attachment");
3517        };
3518        assert_eq!(*connector, Some(15));
3519        let cli_args::UsbDeviceCli::Device {
3520            backend: usb::UsbBackend::Hid(handle),
3521            ..
3522        } = &opt.usb_device[0]
3523        else {
3524            panic!("expected HID");
3525        };
3526        assert_eq!(
3527            (handle.identity.vendor_id, handle.identity.product_id),
3528            (Some(0x1234), Some(65535))
3529        );
3530        assert_eq!(handle.identity.serial_number.as_deref(), Some("HID001"));
3531        let cli_args::UsbDeviceCli::Device {
3532            backend: usb::UsbBackend::Storage(handle),
3533            ..
3534        } = &opt.usb_device[1]
3535        else {
3536            panic!("expected storage");
3537        };
3538        assert_eq!(handle.block_count, Some(4096));
3539    }
3540
3541    /// Explicit host IDs are preserved and automatic discovery remains a separate request.
3542    #[test]
3543    fn usb_cli_preserves_winusb_ids_and_automatic_selection() {
3544        let instance_id = r"USB\MS_COMP_WINUSB\A&B C";
3545        let selection = format!("winusb,on=usb0,connector=3,instance_id={instance_id}");
3546        let opt = Options::try_parse_from([
3547            "openvmm",
3548            "--usb-xhci",
3549            "usb0",
3550            "--usb-device",
3551            &selection,
3552            "--usb-device",
3553            "winusb-all,on=usb0",
3554        ])
3555        .unwrap();
3556        let controllers = usb_controller_configs(&opt.usb_xhci, &opt.usb_device).unwrap();
3557        let cli_args::UsbDeviceCli::Device {
3558            backend: usb::UsbBackend::WinUsb(handle),
3559            ..
3560        } = &opt.usb_device[0]
3561        else {
3562            panic!("expected WinUSB");
3563        };
3564        assert_eq!(handle.instance_id, instance_id);
3565        let usb_config_resources::UsbDeviceConfig::Device { connector, device } =
3566            &controllers[0].1.devices[0]
3567        else {
3568            panic!("expected fixed WinUSB attachment");
3569        };
3570        assert_eq!(*connector, Some(3));
3571        assert_eq!(device.id(), "winusb_config");
3572        let usb_config_resources::UsbDeviceConfig::Discover(discovery) =
3573            &controllers[0].1.devices[1]
3574        else {
3575            panic!("expected WinUSB discovery");
3576        };
3577        assert_eq!(discovery.id(), "winusb_all_config");
3578    }
3579
3580    /// Reject malformed backend options and missing, duplicate, or unknown controllers.
3581    #[test]
3582    fn usb_cli_rejects_bad_syntax_and_unknown_controllers() {
3583        for selection in [
3584            "usb0:hid",
3585            "winusb",
3586            "mock-hid,on=usb0,connector=0",
3587            "mock-hid,on=usb0,connector=256",
3588            "mock-hid,on=usb0,connector=1,vid=0x10000",
3589            "mock-hid,on=usb0,connector=1,pid=bad",
3590            "mock-hid,on=usb0,connector=1,unknown=1",
3591            "mock-hid,on=usb0,connector=1,instance_id=USB",
3592            "mock-hid,on=usb0,connector=1,blocks=1",
3593            "mock-supermutt,on=usb0,connector=1,vid=1",
3594            "mock-storage,on=usb0,connector=1,blocks=0",
3595            "winusb,on=usb0,connector=1,instance_id=USB,vid=1",
3596            "winusb-all,on=usb0,connector=1",
3597        ] {
3598            assert!(
3599                Options::try_parse_from([
3600                    "openvmm",
3601                    "--usb-xhci",
3602                    "usb0",
3603                    "--usb-device",
3604                    selection
3605                ])
3606                .is_err()
3607            );
3608        }
3609        assert!(
3610            Options::try_parse_from(["openvmm", "--usb-device", "mock-hid,on=usb0,connector=1"])
3611                .is_err()
3612        );
3613        assert!(Options::try_parse_from(["openvmm", "--usb-xhci", "usb0,unknown=1"]).is_err());
3614        let opt = Options::try_parse_from([
3615            "openvmm",
3616            "--usb-xhci",
3617            "usb0",
3618            "--usb-device",
3619            "mock-hid,on=missing,connector=1",
3620        ])
3621        .unwrap();
3622        assert!(usb_controller_configs(&opt.usb_xhci, &opt.usb_device).is_err());
3623        let opt = Options::try_parse_from(["openvmm", "--usb-xhci", "usb0", "--usb-xhci", "usb0"])
3624            .unwrap();
3625        assert!(usb_controller_configs(&opt.usb_xhci, &opt.usb_device).is_err());
3626    }
3627
3628    /// Backend defaults, capacity limits, and connector conflicts must not be
3629    /// baked into the shared CLI.
3630    #[test]
3631    fn usb_cli_leaves_defaults_and_implementation_limits_to_resolvers() {
3632        let opt = Options::try_parse_from([
3633            "openvmm",
3634            "--usb-xhci",
3635            "usb0",
3636            "--usb-device",
3637            "mock-storage,on=usb0,connector=16,blocks=524289",
3638            "--usb-device",
3639            "mock-storage,on=usb0,connector=1",
3640            "--usb-device",
3641            "mock-hid,on=usb0,connector=1",
3642        ])
3643        .unwrap();
3644        assert!(usb_controller_configs(&opt.usb_xhci, &opt.usb_device).is_ok());
3645        let cli_args::UsbDeviceCli::Device {
3646            backend: usb::UsbBackend::Storage(config),
3647            ..
3648        } = &opt.usb_device[1]
3649        else {
3650            panic!("expected storage");
3651        };
3652        assert_eq!(config.block_count, None);
3653        assert_eq!(config.identity, Default::default());
3654    }
3655
3656    /// Discovery excludes explicit host IDs across controllers regardless of option order.
3657    /// Resource resolution is async even when the test only decodes configuration.
3658    #[pal_async::async_test]
3659    async fn usb_cli_excludes_explicit_winusb_across_controllers() {
3660        use usb_config_resources::UsbDeviceDiscoveryConfigKind;
3661        use usb_config_resources::WinUsbAllConfig;
3662        use vm_resource::CanResolveTo;
3663        use vm_resource::ResolveResource;
3664        use vm_resource::ResourceResolver;
3665
3666        /// Discovery settings decoded for assertions without host enumeration.
3667        struct CapturedDiscoveryConfig {
3668            /// The actual configuration emitted by the CLI.
3669            config: WinUsbAllConfig,
3670        }
3671        /// Reads an opaque discovery resource without constructing USB devices.
3672        struct CaptureDiscoveryConfig;
3673
3674        impl CanResolveTo<CapturedDiscoveryConfig> for UsbDeviceDiscoveryConfigKind {
3675            type Input<'a> = ();
3676        }
3677
3678        impl ResolveResource<UsbDeviceDiscoveryConfigKind, WinUsbAllConfig> for CaptureDiscoveryConfig {
3679            type Output = CapturedDiscoveryConfig;
3680            type Error = std::convert::Infallible;
3681
3682            fn resolve(&self, config: WinUsbAllConfig, _: ()) -> Result<Self::Output, Self::Error> {
3683                Ok(CapturedDiscoveryConfig { config })
3684            }
3685        }
3686
3687        let mut resolver = ResourceResolver::new();
3688        resolver.add_resolver(CaptureDiscoveryConfig);
3689        for selections in [
3690            [
3691                "winusb-all,on=usb0",
3692                r"winusb,on=usb1,instance_id=usb\z",
3693                r"winusb,on=usb1,instance_id=USB\a",
3694            ],
3695            [
3696                r"winusb,on=usb1,instance_id=usb\z",
3697                r"winusb,on=usb1,instance_id=USB\a",
3698                "winusb-all,on=usb0",
3699            ],
3700        ] {
3701            let mut opt =
3702                Options::try_parse_from(["openvmm", "--usb-xhci", "usb0", "--usb-xhci", "usb1"])
3703                    .unwrap();
3704            opt.usb_device = selections
3705                .into_iter()
3706                .map(|selection| selection.parse().unwrap())
3707                .collect();
3708            let mut controllers = usb_controller_configs(&opt.usb_xhci, &opt.usb_device).unwrap();
3709            assert_eq!(controllers[1].1.devices.len(), 2);
3710            let usb_config_resources::UsbDeviceConfig::Discover(discovery) =
3711                controllers[0].1.devices.pop().unwrap()
3712            else {
3713                panic!("expected discovery on usb0");
3714            };
3715            let captured: CapturedDiscoveryConfig = resolver.resolve(discovery, ()).await.unwrap();
3716            assert_eq!(captured.config.excluded_instance_ids, [r"USB\A", r"USB\Z"]);
3717        }
3718    }
3719
3720    /// Reject duplicate host IDs and multiple automatic discovery requests across the VM.
3721    #[test]
3722    fn usb_cli_rejects_overlapping_host_selections() {
3723        for (first, second) in [
3724            (
3725                "winusb,on=usb0,connector=1,instance_id=USB\\A",
3726                "winusb,on=usb1,connector=1,instance_id=usb\\a",
3727            ),
3728            ("winusb-all,on=usb0", "winusb-all,on=usb1"),
3729        ] {
3730            let opt = Options::try_parse_from([
3731                "openvmm",
3732                "--usb-xhci",
3733                "usb0",
3734                "--usb-xhci",
3735                "usb1",
3736                "--usb-device",
3737                first,
3738                "--usb-device",
3739                second,
3740            ])
3741            .unwrap();
3742            assert!(usb_controller_configs(&opt.usb_xhci, &opt.usb_device).is_err());
3743        }
3744    }
3745
3746    #[test]
3747    fn maps_igvm_personalities_to_chipsets() {
3748        for (args, expected) in [
3749            (
3750                vec!["openvmm", "--igvm", "firmware=guest.igvm,personality=uefi"],
3751                BaseChipsetType::HypervGen2Uefi,
3752            ),
3753            (
3754                vec![
3755                    "openvmm",
3756                    "--igvm",
3757                    "firmware=guest.igvm,personality=linux-direct",
3758                ],
3759                BaseChipsetType::UnenlightenedLinuxDirect,
3760            ),
3761            (
3762                vec![
3763                    "openvmm",
3764                    "--igvm",
3765                    "firmware=guest.igvm,personality=linux-direct",
3766                    "--hv",
3767                ],
3768                BaseChipsetType::HyperVGen2LinuxDirect,
3769            ),
3770            (
3771                vec![
3772                    "openvmm",
3773                    "--igvm",
3774                    "firmware=guest.igvm,personality=linux-direct",
3775                    "--isolation",
3776                    "snp",
3777                ],
3778                BaseChipsetType::EnlightenedLinuxDirect,
3779            ),
3780            (
3781                vec![
3782                    "openvmm",
3783                    "--igvm",
3784                    "firmware=guest.igvm,personality=openhcl",
3785                    "--hv",
3786                    "--vtl2",
3787                ],
3788                BaseChipsetType::HclHost,
3789            ),
3790        ] {
3791            let opt = Options::try_parse_from(args).unwrap();
3792            opt.validate_igvm_options().unwrap();
3793            assert!(
3794                std::mem::discriminant(&base_chipset_type(&opt))
3795                    == std::mem::discriminant(&expected)
3796            );
3797        }
3798    }
3799
3800    #[test]
3801    fn builds_openhcl_igvm_config_without_host_uefi() {
3802        DefaultPool::run_with(async |driver| {
3803            let temp_dir = tempfile::tempdir().unwrap();
3804            let igvm_path = temp_dir.path().join("guest.igvm");
3805            File::create(&igvm_path).unwrap();
3806            let igvm_options = format!("firmware={},personality=openhcl", igvm_path.display());
3807            let mesh = VmmMesh::new(&driver, true).unwrap();
3808
3809            for (extra, expected_alias, expected_policy) in [
3810                (vec![], true, Some(LateMapVtl0MemoryPolicy::Halt)),
3811                (
3812                    vec!["--uefi", "console=com1,disable_frontpage"],
3813                    true,
3814                    Some(LateMapVtl0MemoryPolicy::Halt),
3815                ),
3816                (
3817                    vec!["--net", "uh:consomme", "--no-alias-map"],
3818                    false,
3819                    Some(LateMapVtl0MemoryPolicy::Halt),
3820                ),
3821                (
3822                    vec!["--isolation", "vbs", "--no-alias-map"],
3823                    false,
3824                    Some(LateMapVtl0MemoryPolicy::Halt),
3825                ),
3826                (
3827                    vec!["--no-alias-map", "--late-map-vtl0-policy", "exception"],
3828                    false,
3829                    Some(LateMapVtl0MemoryPolicy::InjectException),
3830                ),
3831                (
3832                    vec!["--late-map-vtl0-policy", "log"],
3833                    true,
3834                    Some(LateMapVtl0MemoryPolicy::Log),
3835                ),
3836                (
3837                    vec!["--igvm-vtl2-relocation-type", "vtl2=filesize"],
3838                    true,
3839                    Some(LateMapVtl0MemoryPolicy::Halt),
3840                ),
3841                (
3842                    vec![
3843                        "--igvm-vtl2-relocation-type",
3844                        "vtl2=filesize",
3845                        "--late-map-vtl0-policy",
3846                        "off",
3847                    ],
3848                    true,
3849                    None,
3850                ),
3851            ] {
3852                let mut args = vec![
3853                    "openvmm",
3854                    "--igvm",
3855                    &igvm_options,
3856                    "--hv",
3857                    "--vtl2",
3858                    "--single-process",
3859                ];
3860                args.extend(extra);
3861                let opt = Options::try_parse_from(args).unwrap();
3862                let (config, resources) = vm_config_from_command_line(driver.clone(), &mesh, &opt)
3863                    .await
3864                    .unwrap();
3865
3866                assert!(matches!(config.load_mode, LoadMode::Igvm { .. }));
3867                assert!(config.hypervisor.with_hv);
3868                let vtl2 = config.hypervisor.with_vtl2.as_ref().unwrap();
3869                assert_eq!(vtl2.vtl0_alias_map, expected_alias);
3870                assert_eq!(vtl2.late_map_vtl0_memory, expected_policy);
3871                assert!(config.vmbus.is_some());
3872                assert!(config.vtl2_vmbus.is_some());
3873                assert!(resources.ged_rpc.is_some());
3874                assert!(resources.acpi_power_button.is_none());
3875                for id in ["ged", "gel"] {
3876                    assert!(
3877                        config.vmbus_devices.iter().any(|(vtl, resource)| {
3878                            *vtl == DeviceVtl::Vtl2 && resource.id() == id
3879                        })
3880                    );
3881                }
3882                assert!(
3883                    config
3884                        .chipset_devices
3885                        .iter()
3886                        .all(|device| { device.resource.id() != "hyperv_firmware_uefi" })
3887                );
3888            }
3889            for (extra, expected_error) in [
3890                (
3891                    ["--net", "uh:consomme"],
3892                    "must specify --no-alias-map to offer NICs to VTL2",
3893                ),
3894                (
3895                    ["--isolation", "vbs"],
3896                    "alias map not supported with isolation",
3897                ),
3898            ] {
3899                let mut args = vec![
3900                    "openvmm",
3901                    "--igvm",
3902                    &igvm_options,
3903                    "--hv",
3904                    "--vtl2",
3905                    "--single-process",
3906                ];
3907                args.extend(extra);
3908                let opt = Options::try_parse_from(args).unwrap();
3909                let err = vm_config_from_command_line(driver.clone(), &mesh, &opt)
3910                    .await
3911                    .err()
3912                    .unwrap();
3913                assert_eq!(err.to_string(), expected_error);
3914            }
3915            mesh.shutdown().await;
3916        });
3917    }
3918
3919    #[test]
3920    fn maps_virtio_vsock_to_named_pcie_port() {
3921        DefaultPool::run_with(async |driver| {
3922            let temp_dir = tempfile::tempdir().unwrap();
3923            let kernel_path = temp_dir.path().join("kernel");
3924            File::create(&kernel_path).unwrap();
3925            let initrd_path = temp_dir.path().join("initrd");
3926            File::create(&initrd_path).unwrap();
3927            let socket_path = temp_dir.path().join("vsock");
3928            let opt = Options::try_parse_from([
3929                "openvmm",
3930                "--kernel",
3931                kernel_path.to_str().unwrap(),
3932                "--initrd",
3933                initrd_path.to_str().unwrap(),
3934                "--virtio-vsock-path",
3935                socket_path.to_str().unwrap(),
3936                "--virtio-vsock-bus",
3937                "pcie:custom",
3938                "--single-process",
3939            ])
3940            .unwrap();
3941            let mesh = VmmMesh::new(&driver, true).unwrap();
3942
3943            let (config, _resources) = vm_config_from_command_line(driver, &mesh, &opt)
3944                .await
3945                .unwrap();
3946
3947            assert_eq!(config.pcie_devices.len(), 1);
3948            assert_eq!(config.pcie_devices[0].port_name, "custom");
3949            mesh.shutdown().await;
3950        });
3951    }
3952
3953    #[test]
3954    fn maps_virtio_fs_and_rng_to_named_pcie_ports() {
3955        DefaultPool::run_with(async |driver| {
3956            let temp_dir = tempfile::tempdir().unwrap();
3957            let kernel_path = temp_dir.path().join("kernel");
3958            File::create(&kernel_path).unwrap();
3959            let initrd_path = temp_dir.path().join("initrd");
3960            File::create(&initrd_path).unwrap();
3961            let root_path = temp_dir.path().to_str().unwrap();
3962            let opt = Options::try_parse_from([
3963                "openvmm",
3964                "--kernel",
3965                kernel_path.to_str().unwrap(),
3966                "--initrd",
3967                initrd_path.to_str().unwrap(),
3968                "--virtio-fs",
3969                &format!("fs,{root_path}"),
3970                "--virtio-fs-bus",
3971                "pcie:fs",
3972                "--virtio-rng",
3973                "--virtio-rng-bus",
3974                "pcie:rng",
3975                "--single-process",
3976            ])
3977            .unwrap();
3978            let mesh = VmmMesh::new(&driver, true).unwrap();
3979
3980            let (config, _resources) = vm_config_from_command_line(driver, &mesh, &opt)
3981                .await
3982                .unwrap();
3983
3984            let port_names: Vec<_> = config
3985                .pcie_devices
3986                .iter()
3987                .map(|device| device.port_name.as_str())
3988                .collect();
3989            assert_eq!(port_names, ["fs", "rng"]);
3990            mesh.shutdown().await;
3991        });
3992    }
3993
3994    #[test]
3995    fn rejects_duplicate_pcie_port_assignments() {
3996        DefaultPool::run_with(async |driver| {
3997            let temp_dir = tempfile::tempdir().unwrap();
3998            let kernel_path = temp_dir.path().join("kernel");
3999            File::create(&kernel_path).unwrap();
4000            let initrd_path = temp_dir.path().join("initrd");
4001            File::create(&initrd_path).unwrap();
4002            let root_path = temp_dir.path().to_str().unwrap();
4003            let opt = Options::try_parse_from([
4004                "openvmm",
4005                "--kernel",
4006                kernel_path.to_str().unwrap(),
4007                "--initrd",
4008                initrd_path.to_str().unwrap(),
4009                "--virtio-fs",
4010                &format!("pcie_port=custom:fs,{root_path}"),
4011                "--virtio-rng",
4012                "--virtio-rng-bus",
4013                "pcie:custom",
4014                "--single-process",
4015            ])
4016            .unwrap();
4017            let mesh = VmmMesh::new(&driver, true).unwrap();
4018
4019            let error = vm_config_from_command_line(driver, &mesh, &opt)
4020                .await
4021                .err()
4022                .unwrap();
4023
4024            assert_eq!(error.to_string(), "multiple devices use PCIe port 'custom'");
4025            mesh.shutdown().await;
4026        });
4027    }
4028
4029    #[test]
4030    fn rejects_duplicate_pcie_port_assignment_from_storage() {
4031        DefaultPool::run_with(async |driver| {
4032            let temp_dir = tempfile::tempdir().unwrap();
4033            let kernel_path = temp_dir.path().join("kernel");
4034            File::create(&kernel_path).unwrap();
4035            let initrd_path = temp_dir.path().join("initrd");
4036            File::create(&initrd_path).unwrap();
4037            let opt = Options::try_parse_from([
4038                "openvmm",
4039                "--kernel",
4040                kernel_path.to_str().unwrap(),
4041                "--initrd",
4042                initrd_path.to_str().unwrap(),
4043                "--nvme-pci",
4044                "id=nvme0,pcie_port=custom",
4045                "--virtio-rng",
4046                "--virtio-rng-bus",
4047                "pcie:custom",
4048                "--single-process",
4049            ])
4050            .unwrap();
4051            let mesh = VmmMesh::new(&driver, true).unwrap();
4052
4053            let error = vm_config_from_command_line(driver, &mesh, &opt)
4054                .await
4055                .err()
4056                .unwrap();
4057
4058            assert_eq!(error.to_string(), "multiple devices use PCIe port 'custom'");
4059            mesh.shutdown().await;
4060        });
4061    }
4062}