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