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 vm_controller;
21
22// `pub` so that the missing_docs warning fires for options without
23// documentation.
24pub use cli_args::Options;
25use console_relay::ConsoleLaunchOptions;
26
27use crate::cli_args::SecureBootTemplateCli;
28use anyhow::Context;
29use anyhow::bail;
30use chipset_resources::battery::HostBatteryUpdate;
31use cli_args::DiskCliKind;
32use cli_args::EfiDiagnosticsLogLevelCli;
33use cli_args::EndpointConfigCli;
34use cli_args::GuestPowerAction;
35use cli_args::NicConfigCli;
36use cli_args::ProvisionVmgs;
37use cli_args::SerialConfigCli;
38use cli_args::UefiConsoleModeCli;
39use cli_args::VirtioBusCli;
40use cli_args::VmgsCli;
41use crash_dump::spawn_dump_handler;
42use cxl_spec::test::CxlTestDeviceHandle;
43use disk_backend_resources::DelayDiskHandle;
44use disk_backend_resources::DiskLayerDescription;
45use disk_backend_resources::layer::DiskLayerHandle;
46use disk_backend_resources::layer::RamDiskLayerHandle;
47use disk_backend_resources::layer::SqliteAutoCacheDiskLayerHandle;
48use disk_backend_resources::layer::SqliteDiskLayerHandle;
49use floppy_resources::FloppyDiskConfig;
50use framebuffer::FRAMEBUFFER_SIZE;
51use framebuffer::FramebufferAccess;
52use futures::AsyncReadExt;
53use futures::AsyncWrite;
54use futures::StreamExt;
55use futures::executor::block_on;
56use futures::io::AllowStdIo;
57use gdma_resources::GdmaDeviceHandle;
58use gdma_resources::VportDefinition;
59use guid::Guid;
60use input_core::MultiplexedInputHandle;
61use inspect::InspectMut;
62use mesh::CancelContext;
63use mesh::CellUpdater;
64use mesh::rpc::RpcSend;
65use meshworker::VmmMesh;
66use net_backend_resources::mac_address::MacAddress;
67use nvme_resources::NvmeControllerRequest;
68use openvmm_defs::config::Config;
69use openvmm_defs::config::DEFAULT_PCAT_BOOT_ORDER;
70use openvmm_defs::config::DeviceVtl;
71use openvmm_defs::config::HypervisorConfig;
72use openvmm_defs::config::LateMapVtl0MemoryPolicy;
73use openvmm_defs::config::LoadMode;
74use openvmm_defs::config::MemoryConfig;
75use openvmm_defs::config::NumaDistance;
76use openvmm_defs::config::NumaNode;
77use openvmm_defs::config::NumaTopology;
78use openvmm_defs::config::PcieDeviceConfig;
79use openvmm_defs::config::PcieMmioRangeConfig;
80use openvmm_defs::config::PciePortConfig;
81use openvmm_defs::config::PcieRootComplexConfig;
82use openvmm_defs::config::PcieSwitchConfig;
83use openvmm_defs::config::ProcessorTopologyConfig;
84use openvmm_defs::config::RootComplexCxlConfig;
85use openvmm_defs::config::SerialInformation;
86use openvmm_defs::config::VirtioBus;
87use openvmm_defs::config::VmbusConfig;
88use openvmm_defs::config::VpAssignment;
89use openvmm_defs::config::VpciDeviceConfig;
90use openvmm_defs::config::Vtl2Config;
91use openvmm_defs::rpc::VmRpc;
92use openvmm_defs::worker::VM_WORKER;
93use openvmm_defs::worker::VmWorkerParameters;
94use openvmm_helpers::disk::OpenDiskOptions;
95use openvmm_helpers::disk::create_disk_type;
96use openvmm_helpers::disk::open_disk_type;
97use pal_async::DefaultDriver;
98use pal_async::DefaultPool;
99use pal_async::socket::PolledSocket;
100use pal_async::task::Spawn;
101use pal_async::task::Task;
102use serial_16550_resources::ComPort;
103use serial_core::resources::DisconnectedSerialBackendHandle;
104use sparse_mmap::alloc_shared_memory;
105use std::cell::RefCell;
106use std::collections::BTreeMap;
107use std::fmt::Write as _;
108use std::io;
109#[cfg(unix)]
110use std::io::IsTerminal;
111use std::io::Write;
112use std::net::TcpListener;
113use std::path::Path;
114use std::path::PathBuf;
115use std::sync::Arc;
116use std::thread;
117use std::time::Duration;
118use storvsp_resources::ScsiControllerRequest;
119use tpm_resources::TpmDeviceHandle;
120use tpm_resources::TpmRegisterLayout;
121use uidevices_resources::SynthKeyboardHandle;
122use uidevices_resources::SynthMouseHandle;
123use uidevices_resources::SynthVideoHandle;
124use video_core::SharedFramebufferHandle;
125use virtio_resources::VirtioPciDeviceHandle;
126use vm_manifest_builder::BaseChipsetType;
127use vm_manifest_builder::MachineArch;
128use vm_manifest_builder::VmChipsetResult;
129use vm_manifest_builder::VmManifestBuilder;
130use vm_resource::IntoResource;
131use vm_resource::Resource;
132use vm_resource::kind::DiskHandleKind;
133use vm_resource::kind::DiskLayerHandleKind;
134use vm_resource::kind::NetEndpointHandleKind;
135use vm_resource::kind::VirtioDeviceHandle;
136use vm_resource::kind::VmbusDeviceHandleKind;
137use vmbus_serial_resources::VmbusSerialDeviceHandle;
138use vmbus_serial_resources::VmbusSerialPort;
139use vmcore::non_volatile_store::resources::EphemeralNonVolatileStoreHandle;
140use vmgs_resources::GuestStateEncryptionPolicy;
141use vmgs_resources::VmgsDisk;
142use vmgs_resources::VmgsFileHandle;
143use vmgs_resources::VmgsResource;
144use vmotherboard::ChipsetDeviceHandle;
145use vnc_worker_defs::VncParameters;
146
147pub fn openvmm_main() {
148    // Save the current state of the terminal so we can restore it back to
149    // normal before exiting.
150    #[cfg(unix)]
151    let orig_termios = io::stderr().is_terminal().then(term::get_termios);
152
153    let mut pidfile_guard: Option<pidfile::Pidfile> = None;
154    let exit_code = match do_main(&mut pidfile_guard) {
155        Ok(code) => code,
156        Err(err) => {
157            eprintln!("fatal error: {:?}", err);
158            1
159        }
160    };
161
162    // Restore the terminal to its initial state.
163    #[cfg(unix)]
164    if let Some(orig_termios) = orig_termios {
165        term::set_termios(orig_termios);
166    }
167
168    // Clean up the pidfile before terminating, since
169    // pal::process::terminate skips destructors.
170    drop(pidfile_guard);
171
172    // Terminate the process immediately without graceful shutdown of DLLs or
173    // C++ destructors or anything like that. This is all unnecessary and saves
174    // time on Windows.
175    //
176    // Do flush stdout, though, since there may be buffered data.
177    let _ = io::stdout().flush();
178    pal::process::terminate(exit_code);
179}
180
181#[derive(Default)]
182struct VmResources {
183    console_in: Option<Box<dyn AsyncWrite + Send + Unpin>>,
184    /// Keeps the dedicated serial reactor alive while serial I/O objects exist.
185    serial_driver: Option<DefaultDriver>,
186    framebuffer_access: Option<FramebufferAccess>,
187    shutdown_ic: Option<mesh::Sender<hyperv_ic_resources::shutdown::ShutdownRpc>>,
188    kvp_ic: Option<mesh::Sender<hyperv_ic_resources::kvp::KvpConnectRpc>>,
189    scsi_rpc: Option<mesh::Sender<ScsiControllerRequest>>,
190    nvme_vtl2_rpc: Option<mesh::Sender<NvmeControllerRequest>>,
191    consomme_rpc: Option<mesh::Sender<net_backend_resources::consomme::ConsommeRequest>>,
192    ged_rpc: Option<mesh::Sender<get_resources::ged::GuestEmulationRequest>>,
193    vtl2_settings: Option<vtl2_settings_proto::Vtl2Settings>,
194    /// Receives dirty rectangles from the synthetic video device for the VNC worker.
195    dirty_rect_recv: Option<mesh::Receiver<Vec<video_core::DirtyRect>>>,
196    #[cfg(windows)]
197    switch_ports: Vec<vmswitch::kernel::SwitchPort>,
198}
199
200struct ConsoleState<'a> {
201    device: &'a str,
202    input: Box<dyn AsyncWrite + Unpin + Send>,
203}
204
205/// Build a flat list of switches with their parent port assignments.
206///
207/// This function converts hierarchical CLI switch definitions into a flat list
208/// where each switch specifies its parent port directly.
209fn build_switch_list(all_switches: &[cli_args::GenericPcieSwitchCli]) -> Vec<PcieSwitchConfig> {
210    all_switches
211        .iter()
212        .map(|switch_cli| PcieSwitchConfig {
213            name: switch_cli.name.clone(),
214            parent_port: switch_cli.port_name.clone(),
215            ports: (0..switch_cli.num_downstream_ports)
216                .map(|i| PciePortConfig {
217                    name: format!("{}-downstream-{}", switch_cli.name, i),
218                    devfn: None,
219                    hotplug: switch_cli.hotplug,
220                    acs_capabilities_supported: switch_cli.acs_capabilities_supported,
221                    cxl: false,
222                })
223                .collect(),
224        })
225        .collect()
226}
227
228async fn vm_config_from_command_line(
229    spawner: impl Spawn,
230    mesh: &VmmMesh,
231    opt: &Options,
232) -> anyhow::Result<(Config, VmResources)> {
233    let (_, serial_driver) = DefaultPool::spawn_on_thread("serial");
234
235    let openhcl_vtl = if opt.vtl2 {
236        DeviceVtl::Vtl2
237    } else {
238        DeviceVtl::Vtl0
239    };
240
241    let console_state: RefCell<Option<ConsoleState<'_>>> = RefCell::new(None);
242    let setup_serial = |name: &str, cli_cfg, device| -> anyhow::Result<_> {
243        Ok(match cli_cfg {
244            SerialConfigCli::Console => {
245                if let Some(console_state) = console_state.borrow().as_ref() {
246                    bail!("console already set by {}", console_state.device);
247                }
248                let (config, serial) = serial_io::anonymous_serial_pair(&serial_driver)?;
249                let (serial_read, serial_write) = AsyncReadExt::split(serial);
250                *console_state.borrow_mut() = Some(ConsoleState {
251                    device,
252                    input: Box::new(serial_write),
253                });
254                thread::Builder::new()
255                    .name(name.to_owned())
256                    .spawn(move || {
257                        let _ = block_on(futures::io::copy(
258                            serial_read,
259                            &mut AllowStdIo::new(term::raw_stdout()),
260                        ));
261                    })
262                    .unwrap();
263                Some(config)
264            }
265            SerialConfigCli::Stderr => {
266                let (config, serial) = serial_io::anonymous_serial_pair(&serial_driver)?;
267                thread::Builder::new()
268                    .name(name.to_owned())
269                    .spawn(move || {
270                        let _ = block_on(futures::io::copy(
271                            serial,
272                            &mut AllowStdIo::new(term::raw_stderr()),
273                        ));
274                    })
275                    .unwrap();
276                Some(config)
277            }
278            SerialConfigCli::File(path) => {
279                let (config, serial) = serial_io::anonymous_serial_pair(&serial_driver)?;
280                let file = fs_err::File::create(path).context("failed to create file")?;
281
282                thread::Builder::new()
283                    .name(name.to_owned())
284                    .spawn(move || {
285                        let _ = block_on(futures::io::copy(serial, &mut AllowStdIo::new(file)));
286                    })
287                    .unwrap();
288                Some(config)
289            }
290            SerialConfigCli::None => None,
291            SerialConfigCli::Pipe(path) => {
292                Some(serial_io::bind_serial(&path).context("failed to bind serial")?)
293            }
294            SerialConfigCli::Tcp(addr) => {
295                Some(serial_io::bind_tcp_serial(&addr).context("failed to bind serial")?)
296            }
297            SerialConfigCli::NewConsole(app, window_title) => {
298                let path = console_relay::random_console_path();
299                let config =
300                    serial_io::bind_serial(&path).context("failed to bind console serial")?;
301                let window_title =
302                    window_title.unwrap_or_else(|| name.to_uppercase() + " [OpenVMM]");
303
304                console_relay::launch_console(
305                    app.or_else(openvmm_terminal_app).as_deref(),
306                    &path,
307                    ConsoleLaunchOptions {
308                        window_title: Some(window_title),
309                    },
310                )
311                .context("failed to launch console")?;
312
313                Some(config)
314            }
315        })
316    };
317
318    let mut vmbus_devices = Vec::new();
319
320    let com_debugger_mode = [
321        opt.com1.as_ref().is_some_and(|c| c.debugger_mode),
322        opt.com2.as_ref().is_some_and(|c| c.debugger_mode),
323        opt.com3.as_ref().is_some_and(|c| c.debugger_mode),
324        opt.com4.as_ref().is_some_and(|c| c.debugger_mode),
325    ];
326
327    let serial0_cfg = setup_serial(
328        "com1",
329        opt.com1
330            .clone()
331            .map_or(SerialConfigCli::Console, |c| c.backend),
332        if cfg!(guest_arch = "x86_64") {
333            "ttyS0"
334        } else {
335            "ttyAMA0"
336        },
337    )?;
338    let serial1_cfg = setup_serial(
339        "com2",
340        opt.com2
341            .clone()
342            .map_or(SerialConfigCli::None, |c| c.backend),
343        if cfg!(guest_arch = "x86_64") {
344            "ttyS1"
345        } else {
346            "ttyAMA1"
347        },
348    )?;
349    let serial2_cfg = setup_serial(
350        "com3",
351        opt.com3
352            .clone()
353            .map_or(SerialConfigCli::None, |c| c.backend),
354        if cfg!(guest_arch = "x86_64") {
355            "ttyS2"
356        } else {
357            "ttyAMA2"
358        },
359    )?;
360    let serial3_cfg = setup_serial(
361        "com4",
362        opt.com4
363            .clone()
364            .map_or(SerialConfigCli::None, |c| c.backend),
365        if cfg!(guest_arch = "x86_64") {
366            "ttyS3"
367        } else {
368            "ttyAMA3"
369        },
370    )?;
371    let with_vmbus_com1_serial = if let Some(vmbus_com1_cfg) = setup_serial(
372        "vmbus_com1",
373        opt.vmbus_com1_serial
374            .clone()
375            .unwrap_or(SerialConfigCli::None),
376        "vmbus_com1",
377    )? {
378        vmbus_devices.push((
379            openhcl_vtl,
380            VmbusSerialDeviceHandle {
381                port: VmbusSerialPort::Com1,
382                backend: vmbus_com1_cfg,
383            }
384            .into_resource(),
385        ));
386        true
387    } else {
388        false
389    };
390    let with_vmbus_com2_serial = if let Some(vmbus_com2_cfg) = setup_serial(
391        "vmbus_com2",
392        opt.vmbus_com2_serial
393            .clone()
394            .unwrap_or(SerialConfigCli::None),
395        "vmbus_com2",
396    )? {
397        vmbus_devices.push((
398            openhcl_vtl,
399            VmbusSerialDeviceHandle {
400                port: VmbusSerialPort::Com2,
401                backend: vmbus_com2_cfg,
402            }
403            .into_resource(),
404        ));
405        true
406    } else {
407        false
408    };
409    let debugcon_cfg = setup_serial(
410        "debugcon",
411        opt.debugcon
412            .clone()
413            .map(|cfg| cfg.serial)
414            .unwrap_or(SerialConfigCli::None),
415        "debugcon",
416    )?;
417
418    let virtio_console_backend = if let Some(serial_cfg) = opt.virtio_console.clone() {
419        setup_serial("virtio-console", serial_cfg, "hvc0")?
420    } else {
421        None
422    };
423
424    let mut resources = VmResources::default();
425    let mut console_str = "";
426    if let Some(ConsoleState { device, input }) = console_state.into_inner() {
427        resources.console_in = Some(input);
428        console_str = device;
429    }
430
431    if opt.shared_memory {
432        tracing::warn!("--shared-memory/-M flag has no effect and will be removed");
433    }
434    if opt.deprecated_prefetch {
435        tracing::warn!("--prefetch is deprecated; use --memory prefetch=on");
436    }
437    if opt.deprecated_private_memory {
438        tracing::warn!("--private-memory is deprecated; use --memory shared=off");
439    }
440    if opt.deprecated_thp {
441        tracing::warn!("--thp is deprecated; use --memory shared=off,thp=on");
442    }
443    if opt.deprecated_memory_backing_file.is_some() {
444        tracing::warn!("--memory-backing-file is deprecated; use --memory file=<path>");
445    }
446
447    opt.validate_memory_options()?;
448
449    const MAX_PROCESSOR_COUNT: u32 = 1024;
450
451    if opt.processors == 0 || opt.processors > MAX_PROCESSOR_COUNT {
452        bail!("invalid proc count: {}", opt.processors);
453    }
454
455    // Total SCSI channel count should not exceed the processor count
456    // (at most, one channel per VP).
457    if opt.scsi_sub_channels > (MAX_PROCESSOR_COUNT - 1) as u16 {
458        bail!(
459            "invalid SCSI sub-channel count: requested {}, max {}",
460            opt.scsi_sub_channels,
461            MAX_PROCESSOR_COUNT - 1
462        );
463    }
464
465    let with_get = opt.get || (opt.vtl2 && !opt.no_get);
466
467    let mut storage = storage_builder::StorageBuilder::new(with_get.then_some(openhcl_vtl));
468
469    // Register named controllers first, so that --disk on=<name>
470    // references can be resolved.
471    for ctrl in &opt.nvme_pci {
472        let transport = match &ctrl.transport {
473            cli_args::NvmeControllerTransport::Pcie(port) => {
474                storage_builder::NvmeControllerTransport::Pcie(port.clone())
475            }
476            cli_args::NvmeControllerTransport::Vpci(guid) => {
477                let guid = guid.unwrap_or_else(|| storage_builder::deterministic_guid(&ctrl.id));
478                storage_builder::NvmeControllerTransport::Vpci(guid)
479            }
480        };
481        storage.add_nvme_controller(ctrl.id.clone(), ctrl.vtl, transport, None)?;
482    }
483
484    for ctrl in &opt.vmbus_scsi {
485        let instance_id = storage_builder::deterministic_guid(&ctrl.id);
486        storage.add_scsi_controller(ctrl.id.clone(), ctrl.vtl, instance_id, ctrl.sub_channels)?;
487    }
488
489    for ctrl in &opt.openhcl_controller {
490        let controller_type = match ctrl.controller_type {
491            cli_args::OpenhclControllerType::Scsi => storage_builder::OpenhclControllerType::Scsi,
492            cli_args::OpenhclControllerType::Nvme => storage_builder::OpenhclControllerType::Nvme,
493        };
494        let instance_id = ctrl
495            .guid
496            .unwrap_or_else(|| storage_builder::deterministic_guid(&ctrl.id));
497        storage.add_openhcl_controller(ctrl.id.clone(), controller_type, instance_id)?;
498    }
499
500    for &cli_args::DiskCli {
501        vtl,
502        ref kind,
503        read_only,
504        is_dvd,
505        underhill,
506        ref pcie_port,
507        ref controller,
508        nsid,
509        lun,
510        ref relay,
511    } in &opt.disk
512    {
513        if controller.is_none() && underhill.is_none() && relay.is_none() {
514            tracing::warn!(
515                "--disk without `on` is deprecated; \
516                 use --vmbus-scsi and --disk on=<name> instead"
517            );
518        }
519
520        let relay_target = relay
521            .as_ref()
522            .map(|(name, loc)| storage_builder::RelayTarget {
523                controller: name.clone(),
524                location: *loc,
525            });
526
527        let target = if let Some(name) = controller {
528            if pcie_port.is_some() {
529                anyhow::bail!("`on` is incompatible with `pcie_port` on `--disk`");
530            }
531            storage_builder::DiskLocation::Named {
532                controller: name.clone(),
533                nsid,
534                lun,
535            }
536        } else if pcie_port.is_some() {
537            anyhow::bail!("`--disk` is incompatible with `pcie_port` without `controller`");
538        } else {
539            if opt.no_vmbus {
540                anyhow::bail!(
541                    "`--disk` without `on=` attaches to the default VMBus SCSI controller and \
542                     cannot be used with `--no-vmbus`; use `on=<name>` to attach to a named controller"
543                );
544            }
545            storage_builder::DiskLocation::Scsi(None)
546        };
547
548        storage
549            .add(
550                vtl,
551                underhill,
552                relay_target,
553                target,
554                kind,
555                is_dvd,
556                read_only,
557            )
558            .await?;
559    }
560
561    for &cli_args::IdeDiskCli {
562        ref kind,
563        read_only,
564        channel,
565        device,
566        is_dvd,
567    } in &opt.ide
568    {
569        storage
570            .add(
571                DeviceVtl::Vtl0,
572                None,
573                None,
574                storage_builder::DiskLocation::Ide(channel, device),
575                kind,
576                is_dvd,
577                read_only,
578            )
579            .await?;
580    }
581
582    if !opt.nvme.is_empty() {
583        tracing::warn!("--nvme is deprecated; use --nvme-pci and --disk on=<name> instead");
584
585        // Pre-register implicit PCIe controllers for unique port names.
586        let mut registered_ports = std::collections::BTreeSet::new();
587        for disk in &opt.nvme {
588            if let Some(port) = &disk.pcie_port {
589                if registered_ports.insert(port.clone()) {
590                    storage.add_nvme_controller(
591                        port.clone(),
592                        DeviceVtl::Vtl0,
593                        storage_builder::NvmeControllerTransport::Pcie(port.clone()),
594                        None,
595                    ).with_context(|| format!(
596                        "legacy --nvme flag conflicts with an explicit controller named '{port}'; \
597                         use --nvme-pci and --disk on=<name> instead"
598                    ))?;
599                }
600            }
601        }
602    }
603
604    for &cli_args::DiskCli {
605        vtl,
606        ref kind,
607        read_only,
608        is_dvd,
609        underhill,
610        ref pcie_port,
611        controller: _,
612        nsid: _,
613        lun: _,
614        relay: _,
615    } in &opt.nvme
616    {
617        let target = if let Some(port) = pcie_port {
618            storage_builder::DiskLocation::Named {
619                controller: port.clone(),
620                nsid: None,
621                lun: None,
622            }
623        } else {
624            storage_builder::DiskLocation::Nvme(None)
625        };
626        storage
627            .add(vtl, underhill, None, target, kind, is_dvd, read_only)
628            .await?;
629    }
630
631    for &cli_args::DiskCli {
632        vtl,
633        ref kind,
634        read_only,
635        is_dvd,
636        ref underhill,
637        ref pcie_port,
638        controller: _,
639        nsid: _,
640        lun: _,
641        relay: _,
642    } in &opt.virtio_blk
643    {
644        if underhill.is_some() {
645            anyhow::bail!("underhill not supported with virtio-blk");
646        }
647        storage
648            .add(
649                vtl,
650                None,
651                None,
652                storage_builder::DiskLocation::VirtioBlk(pcie_port.clone()),
653                kind,
654                is_dvd,
655                read_only,
656            )
657            .await?;
658    }
659
660    let mut floppy_disks = Vec::new();
661    for disk in &opt.floppy {
662        let &cli_args::FloppyDiskCli {
663            ref kind,
664            read_only,
665        } = disk;
666        floppy_disks.push(FloppyDiskConfig {
667            disk_type: disk_open(kind, read_only).await?,
668            read_only,
669        });
670    }
671
672    let mut vpci_mana_nics = [(); 3].map(|()| None);
673    let mut pcie_mana_nics = BTreeMap::<String, GdmaDeviceHandle>::new();
674    let mut underhill_nics = Vec::new();
675    let mut vpci_devices = Vec::new();
676
677    let mut nic_index = 0;
678    for cli_cfg in &opt.net {
679        if cli_cfg.pcie_port.is_some() {
680            anyhow::bail!("`--net` does not support PCIe");
681        }
682        let vport = parse_endpoint(cli_cfg, &mut nic_index, &mut resources)?;
683        if cli_cfg.underhill {
684            if !opt.no_alias_map {
685                anyhow::bail!("must specify --no-alias-map to offer NICs to VTL2");
686            }
687            let mana = vpci_mana_nics[openhcl_vtl as usize].get_or_insert_with(|| {
688                let vpci_instance_id = Guid::new_random();
689                underhill_nics.push(vtl2_settings_proto::NicDeviceLegacy {
690                    instance_id: vpci_instance_id.to_string(),
691                    subordinate_instance_id: None,
692                    max_sub_channels: None,
693                });
694                (vpci_instance_id, GdmaDeviceHandle { vports: Vec::new() })
695            });
696            mana.1.vports.push(VportDefinition {
697                mac_address: vport.mac_address,
698                endpoint: vport.endpoint,
699            });
700        } else {
701            vmbus_devices.push(vport.into_netvsp_handle());
702        }
703    }
704
705    if opt.nic {
706        let nic_config = parse_endpoint(
707            &NicConfigCli {
708                vtl: DeviceVtl::Vtl0,
709                endpoint: EndpointConfigCli::Consomme {
710                    cidr: None,
711                    host_fwd: Vec::new(),
712                },
713                max_queues: None,
714                underhill: false,
715                pcie_port: None,
716            },
717            &mut nic_index,
718            &mut resources,
719        )?;
720        vmbus_devices.push(nic_config.into_netvsp_handle());
721    }
722
723    // Build initial PCIe devices list from CLI options. Storage devices
724    // (e.g., NVMe controllers on PCIe ports) are added later by storage_builder.
725    let mut pcie_devices = Vec::new();
726    for (index, cli_cfg) in opt.pcie_remote.iter().enumerate() {
727        tracing::info!(
728            port_name = %cli_cfg.port_name,
729            socket_addr = ?cli_cfg.socket_addr,
730            "instantiating PCIe remote device"
731        );
732
733        // Generate a deterministic instance ID based on index
734        const PCIE_REMOTE_BASE_INSTANCE_ID: Guid =
735            guid::guid!("28ed784d-c059-429f-9d9a-46bea02562c0");
736        let instance_id = Guid {
737            data1: index as u32,
738            ..PCIE_REMOTE_BASE_INSTANCE_ID
739        };
740
741        pcie_devices.push(PcieDeviceConfig {
742            port_name: cli_cfg.port_name.clone(),
743            resource: pcie_remote_resources::PcieRemoteHandle {
744                instance_id,
745                socket_addr: cli_cfg.socket_addr.clone(),
746                hu: cli_cfg.hu,
747                controller: cli_cfg.controller,
748            }
749            .into_resource(),
750        });
751    }
752
753    #[cfg(windows)]
754    let mut kernel_vmnics = Vec::new();
755    #[cfg(windows)]
756    for (index, switch_id) in opt.kernel_vmnic.iter().enumerate() {
757        // Pick a random MAC address.
758        let mut mac_address = [0x00, 0x15, 0x5D, 0, 0, 0];
759        getrandom::fill(&mut mac_address[3..]).expect("rng failure");
760
761        // Pick a fixed instance ID based on the index.
762        const BASE_INSTANCE_ID: Guid = guid::guid!("00000000-435d-11ee-9f59-00155d5016fc");
763        let instance_id = Guid {
764            data1: index as u32,
765            ..BASE_INSTANCE_ID
766        };
767
768        let switch_id = if switch_id == "default" {
769            None
770        } else {
771            Some(switch_id.as_str())
772        };
773        let (port_id, port) = new_switch_port(switch_id)?;
774        resources.switch_ports.push(port);
775
776        kernel_vmnics.push(openvmm_defs::config::KernelVmNicConfig {
777            instance_id,
778            mac_address: mac_address.into(),
779            switch_port_id: port_id,
780        });
781    }
782
783    for vport in &opt.mana {
784        let vport = parse_endpoint(vport, &mut nic_index, &mut resources)?;
785        let vport_array = match (vport.vtl as usize, vport.pcie_port) {
786            (vtl, None) => {
787                &mut vpci_mana_nics[vtl]
788                    .get_or_insert_with(|| {
789                        (Guid::new_random(), GdmaDeviceHandle { vports: Vec::new() })
790                    })
791                    .1
792                    .vports
793            }
794            (0, Some(pcie_port)) => {
795                &mut pcie_mana_nics
796                    .entry(pcie_port)
797                    .or_insert(GdmaDeviceHandle { vports: Vec::new() })
798                    .vports
799            }
800            _ => anyhow::bail!("PCIe NICs only supported to VTL0"),
801        };
802        vport_array.push(VportDefinition {
803            mac_address: vport.mac_address,
804            endpoint: vport.endpoint,
805        });
806    }
807
808    vpci_devices.extend(
809        vpci_mana_nics
810            .into_iter()
811            .enumerate()
812            .filter_map(|(vtl, nic)| {
813                nic.map(|(instance_id, handle)| VpciDeviceConfig {
814                    vtl: match vtl {
815                        0 => DeviceVtl::Vtl0,
816                        1 => DeviceVtl::Vtl1,
817                        2 => DeviceVtl::Vtl2,
818                        _ => unreachable!(),
819                    },
820                    instance_id,
821                    resource: handle.into_resource(),
822                    vnode: None,
823                })
824            }),
825    );
826
827    pcie_devices.extend(
828        pcie_mana_nics
829            .into_iter()
830            .map(|(pcie_port, handle)| PcieDeviceConfig {
831                port_name: pcie_port,
832                resource: handle.into_resource(),
833            }),
834    );
835
836    for cxl_test in &opt.cxl_test {
837        pcie_devices.push(PcieDeviceConfig {
838            port_name: cxl_test.pcie_port.clone(),
839            resource: CxlTestDeviceHandle {
840                hdm_size_bytes: cxl_test.hdm_size,
841            }
842            .into_resource(),
843        });
844    }
845
846    #[cfg(guest_arch = "aarch64")]
847    let arch = MachineArch::Aarch64;
848    #[cfg(guest_arch = "x86_64")]
849    let arch = MachineArch::X86_64;
850
851    #[cfg(guest_arch = "x86_64")]
852    anyhow::ensure!(
853        opt.amd_iommu.is_empty() || opt.intel_vtd.is_empty(),
854        "--amd-iommu and --intel-vtd cannot both be used in the same VM"
855    );
856
857    #[cfg(guest_arch = "x86_64")]
858    let mut amd_iommu_names: std::collections::HashSet<&str> =
859        opt.amd_iommu.iter().map(|s| s.as_str()).collect();
860    #[cfg(guest_arch = "x86_64")]
861    let mut vtd_names: std::collections::HashSet<&str> =
862        opt.intel_vtd.iter().map(|s| s.as_str()).collect();
863
864    // Map each `--smmu` entry to its root complex, rejecting duplicate `rc=`
865    // entries up front. Entries are removed as they are matched to a root
866    // complex below; any left over refer to unknown root complexes.
867    #[cfg(guest_arch = "aarch64")]
868    let mut smmu_names: std::collections::HashMap<&str, &cli_args::SmmuCli> = {
869        let mut map = std::collections::HashMap::new();
870        for s in &opt.smmu {
871            if map.insert(s.rc_name.as_str(), s).is_some() {
872                anyhow::bail!(
873                    "--smmu specified multiple times for root complex '{}'",
874                    s.rc_name
875                );
876            }
877        }
878        map
879    };
880
881    let mut pcie_root_complexes = Vec::new();
882    for (i, rc_cli) in opt.pcie_root_complex.iter().enumerate() {
883        let ports: Vec<PciePortConfig> = opt
884            .pcie_root_port
885            .iter()
886            .filter(|port_cli| port_cli.root_complex_name == rc_cli.name)
887            .map(|port_cli| PciePortConfig {
888                name: port_cli.name.clone(),
889                devfn: port_cli.devfn,
890                hotplug: port_cli.hotplug,
891                acs_capabilities_supported: port_cli.acs_capabilities_supported,
892                cxl: port_cli.cxl,
893            })
894            .collect();
895
896        const ONE_MB: u64 = 1024 * 1024;
897        // Keep all PCI windows 1MB-granular to match layout and downstream placement rules.
898        let low_mmio_size = (rc_cli.low_mmio as u64).next_multiple_of(ONE_MB);
899        let high_mmio_size = rc_cli
900            .high_mmio
901            .checked_next_multiple_of(ONE_MB)
902            .context("high mmio rounding error")?;
903
904        // Count CXL-capable ports under the root bus. If the root bus has CXL root ports, it needs CHBCR.
905        let cxl_port_count = ports.iter().filter(|port| port.cxl).count() as u64;
906
907        let cxl = if cxl_port_count != 0 {
908            Some(RootComplexCxlConfig {
909                hdm_size: rc_cli.hdm,
910                hdm_window_restrictions: rc_cli.hdm_window_restrictions.bits(),
911            })
912        } else {
913            None
914        };
915        pcie_root_complexes.push(PcieRootComplexConfig {
916            index: i as u32,
917            name: rc_cli.name.clone(),
918            segment: rc_cli.segment,
919            start_bus: rc_cli.start_bus,
920            end_bus: rc_cli.end_bus,
921            low_mmio: if let Some(base) = rc_cli.low_mmio_base {
922                PcieMmioRangeConfig::Fixed(
923                    memory_range::MemoryRange::try_new(base..base.wrapping_add(low_mmio_size))
924                        .context("invalid low MMIO range")?,
925                )
926            } else {
927                PcieMmioRangeConfig::Dynamic {
928                    size: low_mmio_size,
929                }
930            },
931            high_mmio: if let Some(base) = rc_cli.high_mmio_base {
932                PcieMmioRangeConfig::Fixed(
933                    memory_range::MemoryRange::try_new(base..base.wrapping_add(high_mmio_size))
934                        .context("invalid high MMIO range")?,
935                )
936            } else {
937                PcieMmioRangeConfig::Dynamic {
938                    size: high_mmio_size,
939                }
940            },
941            cxl,
942            ports,
943            #[cfg(guest_arch = "aarch64")]
944            iommu: smmu_names.remove(rc_cli.name.as_str()).map(|s| {
945                openvmm_defs::config::PcieIommuConfig::Smmu {
946                    accel: s.accel,
947                    oas: match s.oas {
948                        cli_args::SmmuOasCli::Auto => openvmm_defs::config::SmmuOas::Auto,
949                        cli_args::SmmuOasCli::Fixed(bits) => {
950                            openvmm_defs::config::SmmuOas::Fixed(bits)
951                        }
952                    },
953                }
954            }),
955            #[cfg(guest_arch = "x86_64")]
956            iommu: if amd_iommu_names.remove(rc_cli.name.as_str()) {
957                Some(openvmm_defs::config::PcieIommuConfig::AmdVi)
958            } else if vtd_names.remove(rc_cli.name.as_str()) {
959                Some(openvmm_defs::config::PcieIommuConfig::IntelVtd)
960            } else {
961                None
962            },
963            vnode: rc_cli.vnode,
964            preserve_bars: rc_cli.preserve_bars,
965        });
966    }
967
968    #[cfg(guest_arch = "aarch64")]
969    if let Some(name) = smmu_names.into_keys().next() {
970        anyhow::bail!("--smmu refers to unknown root complex '{name}'");
971    }
972    #[cfg(guest_arch = "x86_64")]
973    if let Some(name) = amd_iommu_names.into_iter().next() {
974        anyhow::bail!("--amd-iommu refers to unknown root complex '{name}'");
975    }
976    #[cfg(guest_arch = "x86_64")]
977    if let Some(name) = vtd_names.into_iter().next() {
978        anyhow::bail!("--intel-vtd refers to unknown root complex '{name}'");
979    }
980
981    let pcie_switches = build_switch_list(&opt.pcie_switch);
982    let pcie_generic_initiators = opt
983        .pcie_generic_initiator
984        .iter()
985        .map(|gi| openvmm_defs::config::PcieGenericInitiatorConfig {
986            port_name: gi.port_name.clone(),
987            node: gi.node,
988        })
989        .collect();
990    #[cfg(target_os = "linux")]
991    let vfio_pcie_devices: Vec<PcieDeviceConfig> = {
992        use std::collections::HashMap;
993        use vm_resource::IntoResource;
994
995        // Process --iommu flags: open /dev/iommu for each declared context.
996        let mut iommu_map: HashMap<String, std::fs::File> = HashMap::new();
997        for iommu_cli in &opt.iommu {
998            anyhow::ensure!(
999                !iommu_map.contains_key(&iommu_cli.id),
1000                "duplicate --iommu id={}",
1001                iommu_cli.id
1002            );
1003            let file = std::fs::OpenOptions::new()
1004                .read(true)
1005                .write(true)
1006                .open("/dev/iommu")
1007                .context("failed to open /dev/iommu (is iommufd available?)")?;
1008            iommu_map.insert(iommu_cli.id.clone(), file);
1009        }
1010
1011        opt.vfio
1012            .iter()
1013            .map(|cli_cfg| {
1014                let sysfs_path = Path::new("/sys/bus/pci/devices").join(&cli_cfg.pci_id);
1015
1016                if let Some(iommu_id) = &cli_cfg.iommu {
1017                    // cdev + iommufd path
1018                    let iommufd = iommu_map.get(iommu_id).with_context(|| {
1019                        format!(
1020                            "--vfio device {} references iommu={iommu_id}, \
1021                             but no --iommu id={iommu_id} was specified",
1022                            cli_cfg.pci_id
1023                        )
1024                    })?;
1025                    // Clone the iommufd fd so the per-iommu manager can own it.
1026                    // The first device for a given iommu ID uses the cloned fd
1027                    // to create the IoasManager; subsequent devices reuse the
1028                    // existing manager and the cloned fd is dropped.
1029                    let iommufd = iommufd.try_clone().with_context(|| {
1030                        format!("failed to dup iommufd fd for iommu={iommu_id}")
1031                    })?;
1032
1033                    // Open the cdev device node.
1034                    let vfio_dev_dir = sysfs_path.join("vfio-dev");
1035                    let entry = std::fs::read_dir(&vfio_dev_dir)
1036                        .with_context(|| {
1037                            format!(
1038                                "failed to read {}: is {} bound to vfio-pci?",
1039                                vfio_dev_dir.display(),
1040                                cli_cfg.pci_id
1041                            )
1042                        })?
1043                        .next()
1044                        .context("no vfio-dev entry found")?
1045                        .context("failed to read vfio-dev entry")?;
1046                    let dev_path = Path::new("/dev/vfio/devices").join(entry.file_name());
1047                    let cdev = std::fs::OpenOptions::new()
1048                        .read(true)
1049                        .write(true)
1050                        .open(&dev_path)
1051                        .with_context(|| format!("failed to open {}", dev_path.display()))?;
1052
1053                    Ok(PcieDeviceConfig {
1054                        port_name: cli_cfg.port_name.clone(),
1055                        resource: vfio_assigned_device_resources::VfioCdevDeviceHandle {
1056                            pci_id: cli_cfg.pci_id.clone(),
1057                            cdev,
1058                            iommufd,
1059                            iommu_id: iommu_id.clone(),
1060                            bar_pt: cli_cfg.bar_pt,
1061                        }
1062                        .into_resource(),
1063                    })
1064                } else {
1065                    // Legacy group/container path
1066                    let iommu_group_link = std::fs::read_link(sysfs_path.join("iommu_group"))
1067                        .with_context(|| {
1068                            format!("failed to read IOMMU group for {}", cli_cfg.pci_id)
1069                        })?;
1070                    let group_id: u64 = iommu_group_link
1071                        .file_name()
1072                        .and_then(|s| s.to_str())
1073                        .context("invalid iommu_group symlink")?
1074                        .parse()
1075                        .context("failed to parse IOMMU group ID")?;
1076                    let group = std::fs::OpenOptions::new()
1077                        .read(true)
1078                        .write(true)
1079                        .open(format!("/dev/vfio/{group_id}"))
1080                        .with_context(|| format!("failed to open /dev/vfio/{group_id}"))?;
1081
1082                    Ok(PcieDeviceConfig {
1083                        port_name: cli_cfg.port_name.clone(),
1084                        resource: vfio_assigned_device_resources::VfioDeviceHandle {
1085                            pci_id: cli_cfg.pci_id.clone(),
1086                            group,
1087                            bar_pt: cli_cfg.bar_pt,
1088                        }
1089                        .into_resource(),
1090                    })
1091                }
1092            })
1093            .collect::<anyhow::Result<Vec<_>>>()?
1094    };
1095
1096    #[cfg(windows)]
1097    let vpci_resources: Vec<_> = opt
1098        .device
1099        .iter()
1100        .map(|path| -> anyhow::Result<_> {
1101            Ok(virt_whp::device::DeviceHandle(
1102                whp::VpciResource::new(
1103                    None,
1104                    Default::default(),
1105                    &whp::VpciResourceDescriptor::Sriov(path, 0, 0),
1106                )
1107                .with_context(|| format!("opening PCI device {}", path))?,
1108            ))
1109        })
1110        .collect::<Result<_, _>>()?;
1111
1112    // Create a vmbusproxy handle if needed by any devices.
1113    #[cfg(windows)]
1114    let vmbusproxy_handle = if !kernel_vmnics.is_empty() {
1115        Some(vmbus_proxy::ProxyHandle::new().context("failed to open vmbusproxy handle")?)
1116    } else {
1117        None
1118    };
1119
1120    let framebuffer = if opt.gfx || opt.vtl2_gfx || opt.vnc.vnc || opt.pcat {
1121        let vram = alloc_shared_memory(FRAMEBUFFER_SIZE, "vram")?;
1122        let (fb, fba) =
1123            framebuffer::framebuffer(vram, FRAMEBUFFER_SIZE, 0).context("creating framebuffer")?;
1124        resources.framebuffer_access = Some(fba);
1125        Some(fb)
1126    } else {
1127        None
1128    };
1129
1130    let load_mode;
1131    let with_hv;
1132
1133    let any_serial_configured = serial0_cfg.is_some()
1134        || serial1_cfg.is_some()
1135        || serial2_cfg.is_some()
1136        || serial3_cfg.is_some();
1137
1138    let has_com3 = serial2_cfg.is_some();
1139
1140    let mut chipset = VmManifestBuilder::new(
1141        if opt.igvm.is_some() {
1142            BaseChipsetType::HclHost
1143        } else if opt.pcat {
1144            BaseChipsetType::HypervGen1
1145        } else if opt.uefi {
1146            BaseChipsetType::HypervGen2Uefi
1147        } else if opt.hv {
1148            BaseChipsetType::HyperVGen2LinuxDirect
1149        } else {
1150            BaseChipsetType::UnenlightenedLinuxDirect
1151        },
1152        arch,
1153    );
1154
1155    if framebuffer.is_some() {
1156        chipset = chipset.with_framebuffer();
1157    }
1158    if opt.guest_watchdog {
1159        chipset = chipset.with_guest_watchdog();
1160    }
1161    if any_serial_configured {
1162        chipset = chipset.with_serial([serial0_cfg, serial1_cfg, serial2_cfg, serial3_cfg]);
1163    }
1164    chipset = chipset.with_serial_debugger_mode(com_debugger_mode);
1165    if opt.battery {
1166        let (tx, rx) = mesh::channel();
1167        tx.send(HostBatteryUpdate::default_present());
1168        chipset = chipset.with_battery(rx);
1169    }
1170    if opt.no_vmbus {
1171        chipset = chipset.without_vmbus();
1172    }
1173    if let Some(cfg) = &opt.debugcon {
1174        chipset = chipset.with_debugcon(
1175            debugcon_cfg.unwrap_or_else(|| DisconnectedSerialBackendHandle.into_resource()),
1176            cfg.port,
1177        );
1178    }
1179
1180    let custom_uefi_vars = {
1181        use firmware_uefi_custom_vars::CustomVars;
1182
1183        // load base vars from specified template, or use an empty set of base
1184        // vars if none was specified.
1185        let base_vars = match opt.secure_boot_template {
1186            Some(template) => match (arch, template) {
1187                (MachineArch::X86_64, SecureBootTemplateCli::Windows) => {
1188                    hyperv_secure_boot_templates::x64::microsoft_windows()
1189                }
1190                (MachineArch::X86_64, SecureBootTemplateCli::UefiCa) => {
1191                    hyperv_secure_boot_templates::x64::microsoft_uefi_ca()
1192                }
1193                (MachineArch::Aarch64, SecureBootTemplateCli::Windows) => {
1194                    hyperv_secure_boot_templates::aarch64::microsoft_windows()
1195                }
1196                (MachineArch::Aarch64, SecureBootTemplateCli::UefiCa) => {
1197                    hyperv_secure_boot_templates::aarch64::microsoft_uefi_ca()
1198                }
1199            },
1200            None => CustomVars::default(),
1201        };
1202
1203        // TODO: fallback to VMGS read if no command line flag was given
1204
1205        let custom_uefi_json_data = match &opt.custom_uefi_json {
1206            Some(file) => Some(fs_err::read(file).context("opening custom uefi json file")?),
1207            None => None,
1208        };
1209
1210        // obtain the final custom uefi vars by applying the delta onto the base vars
1211        match custom_uefi_json_data {
1212            Some(data) => {
1213                let delta = hyperv_uefi_custom_vars_json::load_delta_from_json(&data)?;
1214                base_vars.apply_delta(delta)?
1215            }
1216            None => base_vars,
1217        }
1218    };
1219
1220    if opt.uefi {
1221        let log_level = match opt.efi_diagnostics_log_level.unwrap_or_default() {
1222            EfiDiagnosticsLogLevelCli::Default => firmware_uefi_resources::LogLevel::make_default(),
1223            EfiDiagnosticsLogLevelCli::Info => firmware_uefi_resources::LogLevel::make_info(),
1224            EfiDiagnosticsLogLevelCli::Full => firmware_uefi_resources::LogLevel::make_full(),
1225        };
1226        let nvram_storage = if opt.vmgs.is_some() {
1227            VmgsFileHandle::new(vmgs_format::FileId::BIOS_NVRAM, true).into_resource()
1228        } else {
1229            EphemeralNonVolatileStoreHandle.into_resource()
1230        };
1231        chipset = chipset.with_uefi(vm_manifest_builder::UefiManifest::new(
1232            arch,
1233            custom_uefi_vars,
1234            opt.secure_boot,
1235            log_level,
1236            None,
1237            nvram_storage,
1238            None,
1239        ));
1240    }
1241
1242    // TODO: load from VMGS file if it exists
1243    let bios_guid = Guid::new_random();
1244
1245    let layout_config = chipset.layout_config();
1246    let VmChipsetResult {
1247        chipset,
1248        mut chipset_devices,
1249        pci_chipset_devices,
1250        isa_dma_controller,
1251        capabilities,
1252    } = chipset
1253        .build()
1254        .context("failed to build chipset configuration")?;
1255
1256    if opt.restore_snapshot.is_some() {
1257        // Snapshot restore: skip firmware loading entirely. Device state and
1258        // memory come from the snapshot directory.
1259        load_mode = LoadMode::None;
1260        with_hv = true;
1261    } else if let Some(path) = &opt.igvm {
1262        let file = fs_err::File::open(path)
1263            .context("failed to open igvm file")?
1264            .into();
1265        let cmdline = opt.cmdline.join(" ");
1266        with_hv = true;
1267
1268        load_mode = LoadMode::Igvm {
1269            file,
1270            cmdline,
1271            vtl2_base_address: opt.igvm_vtl2_relocation_type,
1272            com_serial: has_com3.then(|| SerialInformation {
1273                io_port: ComPort::Com3.io_port(),
1274                irq: ComPort::Com3.irq().into(),
1275            }),
1276        };
1277    } else if opt.pcat {
1278        // Emit a nice error early instead of complaining about missing firmware.
1279        if arch != MachineArch::X86_64 {
1280            anyhow::bail!("pcat not supported on this architecture");
1281        }
1282        with_hv = true;
1283
1284        let firmware = openvmm_pcat_locator::find_pcat_bios(opt.pcat_firmware.as_deref())?;
1285        load_mode = LoadMode::Pcat {
1286            firmware,
1287            boot_order: opt
1288                .pcat_boot_order
1289                .map(|x| x.0)
1290                .unwrap_or(DEFAULT_PCAT_BOOT_ORDER),
1291        };
1292    } else if opt.uefi {
1293        use openvmm_defs::config::UefiConsoleMode;
1294
1295        with_hv = true;
1296
1297        let firmware = fs_err::File::open(
1298            (opt.uefi_firmware.0)
1299                .as_ref()
1300                .context("must provide uefi firmware when booting with uefi")?,
1301        )
1302        .context("failed to open uefi firmware")?;
1303
1304        // TODO: It would be better to default memory protections to on, but currently Linux does not boot via UEFI due to what
1305        //       appears to be a GRUB memory protection fault. Memory protections are therefore only enabled if configured.
1306        load_mode = LoadMode::Uefi {
1307            firmware: firmware.into(),
1308            enable_debugging: opt.uefi_debug,
1309            enable_memory_protections: opt.uefi_enable_memory_protections,
1310            disable_frontpage: opt.disable_frontpage,
1311            enable_tpm: opt.tpm,
1312            enable_battery: opt.battery,
1313            enable_serial: any_serial_configured,
1314            enable_vpci_boot: false,
1315            uefi_console_mode: opt.uefi_console_mode.map(|m| match m {
1316                UefiConsoleModeCli::Default => UefiConsoleMode::Default,
1317                UefiConsoleModeCli::Com1 => UefiConsoleMode::Com1,
1318                UefiConsoleModeCli::Com2 => UefiConsoleMode::Com2,
1319                UefiConsoleModeCli::None => UefiConsoleMode::None,
1320            }),
1321            default_boot_always_attempt: opt.default_boot_always_attempt,
1322            bios_guid,
1323            enable_vmbus: !opt.no_vmbus,
1324            force_dma_bounce: opt.uefi_force_dma_bounce,
1325        };
1326    } else {
1327        // Linux Direct
1328        let mut cmdline = "panic=-1 debug".to_string();
1329
1330        with_hv = opt.hv;
1331        if with_hv && opt.pcie_root_complex.is_empty() {
1332            cmdline += " pci=off";
1333        }
1334
1335        if !console_str.is_empty() {
1336            let _ = write!(&mut cmdline, " console={}", console_str);
1337        }
1338
1339        if opt.gfx {
1340            cmdline += " console=tty";
1341        }
1342        for extra in &opt.cmdline {
1343            let _ = write!(&mut cmdline, " {}", extra);
1344        }
1345
1346        let kernel = fs_err::File::open(
1347            (opt.kernel.0)
1348                .as_ref()
1349                .context("must provide kernel when booting with linux direct")?,
1350        )
1351        .context("failed to open kernel")?;
1352        let initrd = (opt.initrd.0)
1353            .as_ref()
1354            .map(fs_err::File::open)
1355            .transpose()
1356            .context("failed to open initrd")?;
1357
1358        load_mode = LoadMode::Linux {
1359            kernel: kernel.into(),
1360            initrd: initrd.map(Into::into),
1361            cmdline,
1362            enable_serial: any_serial_configured,
1363            boot_mode: if opt.device_tree {
1364                openvmm_defs::config::LinuxDirectBootMode::DeviceTree
1365            } else {
1366                openvmm_defs::config::LinuxDirectBootMode::Acpi
1367            },
1368        };
1369    }
1370
1371    let mut vmgs = Some(if let Some(VmgsCli { kind, provision }) = &opt.vmgs {
1372        let disk = VmgsDisk {
1373            disk: disk_open(kind, false)
1374                .await
1375                .context("failed to open vmgs disk")?,
1376            encryption_policy: if opt.test_gsp_by_id {
1377                GuestStateEncryptionPolicy::GspById(true)
1378            } else {
1379                GuestStateEncryptionPolicy::None(true)
1380            },
1381        };
1382        match provision {
1383            ProvisionVmgs::OnEmpty => VmgsResource::Disk(disk),
1384            ProvisionVmgs::OnFailure => VmgsResource::ReprovisionOnFailure(disk),
1385            ProvisionVmgs::True => VmgsResource::Reprovision(disk),
1386        }
1387    } else {
1388        VmgsResource::Ephemeral
1389    });
1390
1391    if with_get && with_hv {
1392        let has_vtl0_nvme = storage.has_vtl0_nvme();
1393        let vtl2_settings = vtl2_settings_proto::Vtl2Settings {
1394            version: vtl2_settings_proto::vtl2_settings_base::Version::V1.into(),
1395            fixed: Some(Default::default()),
1396            dynamic: Some(vtl2_settings_proto::Vtl2SettingsDynamic {
1397                storage_controllers: storage.build_openhcl_settings(opt.vmbus_redirect),
1398                nic_devices: underhill_nics,
1399            }),
1400            namespace_settings: Vec::default(),
1401        };
1402
1403        // Cache the VTL2 settings for later modification via the interactive console.
1404        resources.vtl2_settings = Some(vtl2_settings.clone());
1405
1406        let (send, guest_request_recv) = mesh::channel();
1407        resources.ged_rpc = Some(send);
1408
1409        let vmgs = vmgs.take().unwrap();
1410
1411        vmbus_devices.extend([
1412            (
1413                openhcl_vtl,
1414                get_resources::gel::GuestEmulationLogHandle.into_resource(),
1415            ),
1416            (
1417                openhcl_vtl,
1418                get_resources::ged::GuestEmulationDeviceHandle {
1419                    firmware: if opt.pcat {
1420                        get_resources::ged::GuestFirmwareConfig::Pcat {
1421                            boot_order: opt
1422                                .pcat_boot_order
1423                                .map_or(DEFAULT_PCAT_BOOT_ORDER, |x| x.0)
1424                                .map(|x| match x {
1425                                    openvmm_defs::config::PcatBootDevice::Floppy => {
1426                                        get_resources::ged::PcatBootDevice::Floppy
1427                                    }
1428                                    openvmm_defs::config::PcatBootDevice::HardDrive => {
1429                                        get_resources::ged::PcatBootDevice::HardDrive
1430                                    }
1431                                    openvmm_defs::config::PcatBootDevice::Optical => {
1432                                        get_resources::ged::PcatBootDevice::Optical
1433                                    }
1434                                    openvmm_defs::config::PcatBootDevice::Network => {
1435                                        get_resources::ged::PcatBootDevice::Network
1436                                    }
1437                                }),
1438                        }
1439                    } else {
1440                        use get_resources::ged::UefiConsoleMode;
1441
1442                        get_resources::ged::GuestFirmwareConfig::Uefi {
1443                            enable_vpci_boot: has_vtl0_nvme,
1444                            firmware_debug: opt.uefi_debug,
1445                            disable_frontpage: opt.disable_frontpage,
1446                            console_mode: match opt.uefi_console_mode.unwrap_or(UefiConsoleModeCli::Default) {
1447                                UefiConsoleModeCli::Default => UefiConsoleMode::Default,
1448                                UefiConsoleModeCli::Com1 => UefiConsoleMode::COM1,
1449                                UefiConsoleModeCli::Com2 => UefiConsoleMode::COM2,
1450                                UefiConsoleModeCli::None => UefiConsoleMode::None,
1451                            },
1452                            default_boot_always_attempt: opt.default_boot_always_attempt,
1453                        }
1454                    },
1455                    com1: with_vmbus_com1_serial,
1456                    com2: with_vmbus_com2_serial,
1457                    serial_tx_only: opt.serial_tx_only,
1458                    vtl2_settings: Some(prost::Message::encode_to_vec(&vtl2_settings)),
1459                    vmbus_redirection: opt.vmbus_redirect,
1460                    vmgs,
1461                    framebuffer: opt
1462                        .vtl2_gfx
1463                        .then(|| SharedFramebufferHandle.into_resource()),
1464                    guest_request_recv,
1465                    enable_tpm: opt.tpm,
1466                    firmware_event_send: None,
1467                    secure_boot_enabled: opt.secure_boot,
1468                    secure_boot_template: match opt.secure_boot_template {
1469                        Some(SecureBootTemplateCli::Windows) => {
1470                            get_resources::ged::GuestSecureBootTemplateType::MicrosoftWindows
1471                        },
1472                        Some(SecureBootTemplateCli::UefiCa) => {
1473                            get_resources::ged::GuestSecureBootTemplateType::MicrosoftUefiCertificateAuthority
1474                        }
1475                        None => {
1476                            get_resources::ged::GuestSecureBootTemplateType::None
1477                        },
1478                    },
1479                    enable_battery: opt.battery,
1480                    no_persistent_secrets: true,
1481                    igvm_attest_test_config: None,
1482                    test_gsp_by_id: opt.test_gsp_by_id,
1483                    efi_diagnostics_log_level: {
1484                        match opt.efi_diagnostics_log_level.unwrap_or_default() {
1485                            EfiDiagnosticsLogLevelCli::Default => get_resources::ged::EfiDiagnosticsLogLevelType::Default,
1486                            EfiDiagnosticsLogLevelCli::Info => get_resources::ged::EfiDiagnosticsLogLevelType::Info,
1487                            EfiDiagnosticsLogLevelCli::Full => get_resources::ged::EfiDiagnosticsLogLevelType::Full,
1488                        }
1489                    },
1490                    force_dma_bounce_enabled: opt.uefi_force_dma_bounce,
1491                }
1492                .into_resource(),
1493            ),
1494        ]);
1495    }
1496
1497    if opt.tpm && !opt.vtl2 {
1498        let register_layout = if cfg!(guest_arch = "x86_64") {
1499            TpmRegisterLayout::IoPort
1500        } else {
1501            TpmRegisterLayout::Mmio
1502        };
1503
1504        let (ppi_store, nvram_store) = if opt.vmgs.is_some() {
1505            (
1506                VmgsFileHandle::new(vmgs_format::FileId::TPM_PPI, true).into_resource(),
1507                VmgsFileHandle::new(vmgs_format::FileId::TPM_NVRAM, true).into_resource(),
1508            )
1509        } else {
1510            (
1511                EphemeralNonVolatileStoreHandle.into_resource(),
1512                EphemeralNonVolatileStoreHandle.into_resource(),
1513            )
1514        };
1515
1516        chipset_devices.push(ChipsetDeviceHandle {
1517            name: "tpm".to_string(),
1518            resource: chipset_device_worker_defs::RemoteChipsetDeviceHandle {
1519                device: TpmDeviceHandle {
1520                    ppi_store,
1521                    nvram_store,
1522                    nvram_size: None,
1523                    refresh_tpm_seeds: false,
1524                    ak_cert_type: tpm_resources::TpmAkCertTypeResource::None,
1525                    register_layout,
1526                    guest_secret_key: None,
1527                    logger: None,
1528                    is_confidential_vm: false,
1529                    bios_guid,
1530                }
1531                .into_resource(),
1532                worker_host: mesh.make_host("tpm", None).await?,
1533            }
1534            .into_resource(),
1535        });
1536    }
1537
1538    let vga_firmware = if opt.pcat {
1539        Some(openvmm_pcat_locator::find_svga_bios(
1540            opt.vga_firmware.as_deref(),
1541        )?)
1542    } else {
1543        None
1544    };
1545
1546    if opt.gfx {
1547        // Channel for the video device to report dirty rectangles to the VNC worker.
1548        let (dirt_send, dirt_recv) = mesh::channel();
1549        resources.dirty_rect_recv = Some(dirt_recv);
1550
1551        vmbus_devices.extend([
1552            (
1553                DeviceVtl::Vtl0,
1554                SynthVideoHandle {
1555                    framebuffer: SharedFramebufferHandle.into_resource(),
1556                    dirt_send: Some(dirt_send),
1557                }
1558                .into_resource(),
1559            ),
1560            (
1561                DeviceVtl::Vtl0,
1562                SynthKeyboardHandle {
1563                    source: MultiplexedInputHandle {
1564                        // Save 0 for PS/2
1565                        elevation: 1,
1566                    }
1567                    .into_resource(),
1568                }
1569                .into_resource(),
1570            ),
1571            (
1572                DeviceVtl::Vtl0,
1573                SynthMouseHandle {
1574                    source: MultiplexedInputHandle {
1575                        // Save 0 for PS/2
1576                        elevation: 1,
1577                    }
1578                    .into_resource(),
1579                }
1580                .into_resource(),
1581            ),
1582        ]);
1583    }
1584
1585    let vsock_listener = |path: Option<&str>| -> anyhow::Result<_> {
1586        if let Some(path) = path {
1587            cleanup_socket(path.as_ref());
1588            let listener = unix_socket::UnixListener::bind(path)
1589                .with_context(|| format!("failed to bind to hybrid vsock path: {}", path))?;
1590            Ok(Some(listener))
1591        } else {
1592            Ok(None)
1593        }
1594    };
1595
1596    let vtl0_vsock_listener = vsock_listener(opt.vmbus_vsock_path.as_deref())?;
1597    let vtl2_vsock_listener = vsock_listener(opt.vmbus_vtl2_vsock_path.as_deref())?;
1598
1599    if let Some(path) = &opt.openhcl_dump_path {
1600        let (resource, task) = spawn_dump_handler(&spawner, path.clone(), None);
1601        task.detach();
1602        vmbus_devices.push((openhcl_vtl, resource));
1603    }
1604
1605    #[cfg(guest_arch = "aarch64")]
1606    let topology_arch = openvmm_defs::config::ArchTopologyConfig::Aarch64(
1607        openvmm_defs::config::Aarch64TopologyConfig {
1608            // TODO: allow this to be configured from the command line
1609            gic_config: None,
1610            pmu_gsiv: openvmm_defs::config::PmuGsivConfig::Platform,
1611            gic_msi: match opt.gic_msi {
1612                cli_args::GicMsiCli::Auto => openvmm_defs::config::GicMsiConfig::Auto,
1613                cli_args::GicMsiCli::Its => openvmm_defs::config::GicMsiConfig::Its,
1614                cli_args::GicMsiCli::V2m => {
1615                    openvmm_defs::config::GicMsiConfig::V2m { spi_count: None }
1616                }
1617            },
1618        },
1619    );
1620    #[cfg(guest_arch = "x86_64")]
1621    let topology_arch =
1622        openvmm_defs::config::ArchTopologyConfig::X86(openvmm_defs::config::X86TopologyConfig {
1623            apic_id_offset: opt.apic_id_offset,
1624            x2apic: opt.x2apic,
1625        });
1626
1627    let with_isolation = if let Some(isolation) = &opt.isolation {
1628        // TODO: For now, isolation is only supported with VTL2.
1629        if !opt.vtl2 {
1630            anyhow::bail!("isolation is only currently supported with vtl2");
1631        }
1632
1633        // TODO: Alias map support is not yet implement with isolation.
1634        if !opt.no_alias_map {
1635            anyhow::bail!("alias map not supported with isolation");
1636        }
1637
1638        match isolation {
1639            cli_args::IsolationCli::Vbs => Some(openvmm_defs::config::IsolationType::Vbs),
1640        }
1641    } else {
1642        None
1643    };
1644
1645    if with_hv && !opt.no_vmbus {
1646        let (shutdown_send, shutdown_recv) = mesh::channel();
1647        resources.shutdown_ic = Some(shutdown_send);
1648        let (kvp_send, kvp_recv) = mesh::channel();
1649        resources.kvp_ic = Some(kvp_send);
1650        vmbus_devices.extend(
1651            [
1652                hyperv_ic_resources::shutdown::ShutdownIcHandle {
1653                    recv: shutdown_recv,
1654                }
1655                .into_resource(),
1656                hyperv_ic_resources::kvp::KvpIcHandle { recv: kvp_recv }.into_resource(),
1657                hyperv_ic_resources::timesync::TimesyncIcHandle.into_resource(),
1658            ]
1659            .map(|r| (DeviceVtl::Vtl0, r)),
1660        );
1661    }
1662
1663    if let Some(hive_path) = &opt.imc {
1664        let file = fs_err::File::open(hive_path).context("failed to open imc hive")?;
1665        vmbus_devices.push((
1666            DeviceVtl::Vtl0,
1667            vmbfs_resources::VmbfsImcDeviceHandle { file: file.into() }.into_resource(),
1668        ));
1669    }
1670
1671    let mut virtio_devices = Vec::new();
1672    let mut add_virtio_device = |bus, resource: Resource<VirtioDeviceHandle>| {
1673        let bus = match bus {
1674            VirtioBusCli::Auto => {
1675                // Use VPCI when possible (currently only on Windows and macOS due
1676                // to KVM backend limitations).
1677                if with_hv && (cfg!(windows) || cfg!(target_os = "macos")) {
1678                    None
1679                } else {
1680                    Some(VirtioBus::Pci)
1681                }
1682            }
1683            VirtioBusCli::Mmio => Some(VirtioBus::Mmio),
1684            VirtioBusCli::Pci => Some(VirtioBus::Pci),
1685            VirtioBusCli::Vpci => None,
1686        };
1687        if let Some(bus) = bus {
1688            virtio_devices.push((bus, resource));
1689        } else {
1690            vpci_devices.push(VpciDeviceConfig {
1691                vtl: DeviceVtl::Vtl0,
1692                instance_id: Guid::new_random(),
1693                resource: VirtioPciDeviceHandle(resource).into_resource(),
1694                vnode: None,
1695            });
1696        }
1697    };
1698
1699    for cli_cfg in &opt.virtio_net {
1700        if cli_cfg.underhill {
1701            anyhow::bail!("use --net uh:[...] to add underhill NICs")
1702        }
1703        let vport = parse_endpoint(cli_cfg, &mut nic_index, &mut resources)?;
1704        let resource = virtio_resources::net::VirtioNetHandle {
1705            max_queues: vport.max_queues,
1706            mac_address: vport.mac_address,
1707            endpoint: vport.endpoint,
1708        }
1709        .into_resource();
1710        if let Some(pcie_port) = &cli_cfg.pcie_port {
1711            pcie_devices.push(PcieDeviceConfig {
1712                port_name: pcie_port.clone(),
1713                resource: VirtioPciDeviceHandle(resource).into_resource(),
1714            });
1715        } else {
1716            add_virtio_device(VirtioBusCli::Auto, resource);
1717        }
1718    }
1719
1720    for args in &opt.virtio_fs {
1721        let resource: Resource<VirtioDeviceHandle> = virtio_resources::fs::VirtioFsHandle {
1722            tag: args.tag.clone(),
1723            fs: virtio_resources::fs::VirtioFsBackend::HostFs {
1724                root_path: args.path.clone(),
1725                mount_options: args.options.clone(),
1726            },
1727        }
1728        .into_resource();
1729        if let Some(pcie_port) = &args.pcie_port {
1730            pcie_devices.push(PcieDeviceConfig {
1731                port_name: pcie_port.clone(),
1732                resource: VirtioPciDeviceHandle(resource).into_resource(),
1733            });
1734        } else {
1735            add_virtio_device(opt.virtio_fs_bus, resource);
1736        }
1737    }
1738
1739    for args in &opt.virtio_fs_shmem {
1740        let resource: Resource<VirtioDeviceHandle> = virtio_resources::fs::VirtioFsHandle {
1741            tag: args.tag.clone(),
1742            fs: virtio_resources::fs::VirtioFsBackend::SectionFs {
1743                root_path: args.path.clone(),
1744            },
1745        }
1746        .into_resource();
1747        if let Some(pcie_port) = &args.pcie_port {
1748            pcie_devices.push(PcieDeviceConfig {
1749                port_name: pcie_port.clone(),
1750                resource: VirtioPciDeviceHandle(resource).into_resource(),
1751            });
1752        } else {
1753            add_virtio_device(opt.virtio_fs_bus, resource);
1754        }
1755    }
1756
1757    for args in &opt.virtio_9p {
1758        let resource: Resource<VirtioDeviceHandle> = virtio_resources::p9::VirtioPlan9Handle {
1759            tag: args.tag.clone(),
1760            root_path: args.path.clone(),
1761            debug: opt.virtio_9p_debug,
1762        }
1763        .into_resource();
1764        if let Some(pcie_port) = &args.pcie_port {
1765            pcie_devices.push(PcieDeviceConfig {
1766                port_name: pcie_port.clone(),
1767                resource: VirtioPciDeviceHandle(resource).into_resource(),
1768            });
1769        } else {
1770            add_virtio_device(VirtioBusCli::Auto, resource);
1771        }
1772    }
1773
1774    if let Some(pmem_args) = &opt.virtio_pmem {
1775        let resource: Resource<VirtioDeviceHandle> = virtio_resources::pmem::VirtioPmemHandle {
1776            path: pmem_args.path.clone(),
1777        }
1778        .into_resource();
1779        if let Some(pcie_port) = &pmem_args.pcie_port {
1780            pcie_devices.push(PcieDeviceConfig {
1781                port_name: pcie_port.clone(),
1782                resource: VirtioPciDeviceHandle(resource).into_resource(),
1783            });
1784        } else {
1785            add_virtio_device(VirtioBusCli::Auto, resource);
1786        }
1787    }
1788
1789    if opt.virtio_rng {
1790        let resource: Resource<VirtioDeviceHandle> =
1791            virtio_resources::rng::VirtioRngHandle.into_resource();
1792        if let Some(pcie_port) = &opt.virtio_rng_pcie_port {
1793            pcie_devices.push(PcieDeviceConfig {
1794                port_name: pcie_port.clone(),
1795                resource: VirtioPciDeviceHandle(resource).into_resource(),
1796            });
1797        } else {
1798            add_virtio_device(opt.virtio_rng_bus, resource);
1799        }
1800    }
1801
1802    if let Some(backend) = virtio_console_backend {
1803        let resource: Resource<VirtioDeviceHandle> =
1804            virtio_resources::console::VirtioConsoleHandle { backend }.into_resource();
1805        if let Some(pcie_port) = &opt.virtio_console_pcie_port {
1806            pcie_devices.push(PcieDeviceConfig {
1807                port_name: pcie_port.clone(),
1808                resource: VirtioPciDeviceHandle(resource).into_resource(),
1809            });
1810        } else {
1811            add_virtio_device(VirtioBusCli::Auto, resource);
1812        }
1813    }
1814
1815    // Handle --vhost-user arguments.
1816    #[cfg(target_os = "linux")]
1817    for vhost_cli in &opt.vhost_user {
1818        let stream =
1819            unix_socket::UnixStream::connect(&vhost_cli.socket_path).with_context(|| {
1820                format!(
1821                    "failed to connect to vhost-user socket: {}",
1822                    vhost_cli.socket_path
1823                )
1824            })?;
1825
1826        use crate::cli_args::VhostUserDeviceTypeCli;
1827        let resource: Resource<VirtioDeviceHandle> = match vhost_cli.device_type {
1828            VhostUserDeviceTypeCli::Fs {
1829                ref tag,
1830                num_queues,
1831                queue_size,
1832            } => virtio_resources::vhost_user::VhostUserFsHandle {
1833                socket: stream.into(),
1834                tag: tag.clone(),
1835                num_queues,
1836                queue_size,
1837            }
1838            .into_resource(),
1839            VhostUserDeviceTypeCli::Blk {
1840                num_queues,
1841                queue_size,
1842            } => virtio_resources::vhost_user::VhostUserBlkHandle {
1843                socket: stream.into(),
1844                num_queues,
1845                queue_size,
1846            }
1847            .into_resource(),
1848            VhostUserDeviceTypeCli::Other {
1849                device_id,
1850                ref queue_sizes,
1851            } => virtio_resources::vhost_user::VhostUserGenericHandle {
1852                socket: stream.into(),
1853                device_id,
1854                queue_sizes: queue_sizes.clone(),
1855            }
1856            .into_resource(),
1857        };
1858        if let Some(pcie_port) = &vhost_cli.pcie_port {
1859            pcie_devices.push(PcieDeviceConfig {
1860                port_name: pcie_port.clone(),
1861                resource: VirtioPciDeviceHandle(resource).into_resource(),
1862            });
1863        } else {
1864            add_virtio_device(VirtioBusCli::Auto, resource);
1865        }
1866    }
1867
1868    if let Some(vsock_path) = &opt.virtio_vsock_path {
1869        let listener = vsock_listener(Some(vsock_path))?.unwrap();
1870        add_virtio_device(
1871            VirtioBusCli::Auto,
1872            virtio_resources::vsock::VirtioVsockHandle {
1873                // The guest CID does not matter since the UDS relay does not use it. It just needs
1874                // to be some non-reserved value for the guest to use.
1875                guest_cid: 0x3,
1876                base_path: vsock_path.clone(),
1877                listener,
1878            }
1879            .into_resource(),
1880        );
1881    }
1882
1883    let mut cfg = Config {
1884        chipset,
1885        load_mode,
1886        floppy_disks,
1887        pcie_root_complexes,
1888        #[cfg(target_os = "linux")]
1889        pcie_devices: {
1890            let mut devs = pcie_devices;
1891            devs.extend(vfio_pcie_devices);
1892            devs
1893        },
1894        #[cfg(not(target_os = "linux"))]
1895        pcie_devices,
1896        pcie_switches,
1897        pcie_generic_initiators,
1898        vpci_devices,
1899        ide_disks: Vec::new(),
1900        numa: {
1901            if let Some(ref nodes) = opt.numa {
1902                // --numa mode: each --numa flag defines a node.
1903                NumaTopology {
1904                    nodes: nodes
1905                        .iter()
1906                        .map(|n| {
1907                            let vps = match &n.vps {
1908                                Some(vps) if vps.0.is_empty() => VpAssignment::Empty,
1909                                Some(vps) => {
1910                                    VpAssignment::Explicit(vps.expand_below(opt.processors)?)
1911                                }
1912                                None => VpAssignment::FromTopology,
1913                            };
1914                            Ok(NumaNode {
1915                                mem: Some(MemoryConfig {
1916                                    mem_size: n
1917                                        .memory
1918                                        .size
1919                                        .expect("NUMA memory size was validated")
1920                                        .0,
1921                                    prefetch_memory: n.memory.prefetch,
1922                                    private_memory: n.memory.shared == Some(false),
1923                                    transparent_hugepages: n
1924                                        .memory
1925                                        .transparent_hugepages
1926                                        .unwrap_or(!n.memory.hugepages),
1927                                    hugepages: n.memory.hugepages,
1928                                    hugepage_size: n.memory.hugepage_size.map(|m| m.0),
1929                                    host_numa_node: n.host_numa_node,
1930                                }),
1931                                vps,
1932                            })
1933                        })
1934                        .collect::<anyhow::Result<Vec<_>>>()?,
1935                    distances: opt
1936                        .numa_distance
1937                        .as_deref()
1938                        .unwrap_or(&[])
1939                        .iter()
1940                        .map(|d| NumaDistance {
1941                            src: d.src,
1942                            dst: d.dst,
1943                            distance: d.distance,
1944                        })
1945                        .collect(),
1946                }
1947            } else {
1948                // Single-node default from --memory.
1949                NumaTopology {
1950                    nodes: vec![NumaNode {
1951                        mem: Some(MemoryConfig {
1952                            mem_size: opt.memory_size(),
1953                            prefetch_memory: opt.prefetch_memory(),
1954                            private_memory: opt.private_memory(),
1955                            transparent_hugepages: opt.transparent_hugepages(),
1956                            hugepages: opt.memory.hugepages,
1957                            hugepage_size: opt.memory.hugepage_size.map(|m| m.0),
1958                            host_numa_node: None,
1959                        }),
1960                        vps: VpAssignment::FromTopology,
1961                    }],
1962                    distances: vec![],
1963                }
1964            }
1965        },
1966        processor_topology: ProcessorTopologyConfig {
1967            proc_count: opt.processors,
1968            vps_per_socket: opt.vps_per_socket,
1969            enable_smt: match opt.smt {
1970                cli_args::SmtConfigCli::Auto => None,
1971                cli_args::SmtConfigCli::Force => Some(true),
1972                cli_args::SmtConfigCli::Off => Some(false),
1973            },
1974            arch: Some(topology_arch),
1975        },
1976        hypervisor: HypervisorConfig {
1977            with_hv,
1978            with_vtl2: opt.vtl2.then_some(Vtl2Config {
1979                vtl0_alias_map: !opt.no_alias_map,
1980                late_map_vtl0_memory: match opt.late_map_vtl0_policy {
1981                    cli_args::Vtl0LateMapPolicyCli::Off => None,
1982                    cli_args::Vtl0LateMapPolicyCli::Log => Some(LateMapVtl0MemoryPolicy::Log),
1983                    cli_args::Vtl0LateMapPolicyCli::Halt => Some(LateMapVtl0MemoryPolicy::Halt),
1984                    cli_args::Vtl0LateMapPolicyCli::Exception => {
1985                        Some(LateMapVtl0MemoryPolicy::InjectException)
1986                    }
1987                },
1988            }),
1989            with_isolation,
1990            nested_virt: opt.nested_virt,
1991        },
1992        #[cfg(windows)]
1993        kernel_vmnics,
1994        input: mesh::Receiver::new(),
1995        framebuffer,
1996        vga_firmware,
1997        vtl2_gfx: opt.vtl2_gfx,
1998        virtio_devices,
1999        vmbus: (with_hv && !opt.no_vmbus).then_some(VmbusConfig {
2000            vsock_listener: vtl0_vsock_listener,
2001            vsock_path: opt.vmbus_vsock_path.clone(),
2002            vtl2_redirect: opt.vmbus_redirect,
2003            vmbus_max_version: opt.vmbus_max_version,
2004            #[cfg(windows)]
2005            vmbusproxy_handle,
2006        }),
2007        vtl2_vmbus: (with_hv && opt.vtl2).then_some(VmbusConfig {
2008            vsock_listener: vtl2_vsock_listener,
2009            vsock_path: opt.vmbus_vtl2_vsock_path.clone(),
2010            ..Default::default()
2011        }),
2012        vmbus_devices,
2013        chipset_devices,
2014        pci_chipset_devices,
2015        isa_dma_controller,
2016        chipset_capabilities: capabilities,
2017        layout: layout_config,
2018        #[cfg(windows)]
2019        vpci_resources,
2020        vmgs,
2021        firmware_event_send: None,
2022        debugger_rpc: None,
2023        rtc_delta_milliseconds: 0,
2024        // Only let the partition auto-reset when the reset action is `reset`.
2025        // For `halt` or `exit`, the guest reset must surface as a halt event so
2026        // the controller can hold the VM or exit instead of rebooting in place.
2027        automatic_guest_reset: matches!(opt.guest_reset_action, GuestPowerAction::Reset),
2028    };
2029
2030    storage.build_config(&mut cfg, &mut resources, opt.scsi_sub_channels)?;
2031    resources.serial_driver = Some(serial_driver);
2032    Ok((cfg, resources))
2033}
2034
2035/// Gets the terminal to use for externally launched console windows.
2036pub(crate) fn openvmm_terminal_app() -> Option<PathBuf> {
2037    std::env::var_os("OPENVMM_TERM")
2038        .or_else(|| std::env::var_os("HVLITE_TERM"))
2039        .map(Into::into)
2040}
2041
2042// Tries to remove `path` if it is confirmed to be a Unix socket.
2043fn cleanup_socket(path: &Path) {
2044    #[cfg(windows)]
2045    let is_socket = pal::windows::fs::is_unix_socket(path).unwrap_or(false);
2046    #[cfg(not(windows))]
2047    let is_socket = path
2048        .metadata()
2049        .is_ok_and(|meta| std::os::unix::fs::FileTypeExt::is_socket(&meta.file_type()));
2050
2051    if is_socket {
2052        let _ = std::fs::remove_file(path);
2053    }
2054}
2055
2056#[cfg(windows)]
2057fn new_switch_port(
2058    switch_id: Option<&str>,
2059) -> anyhow::Result<(
2060    openvmm_defs::config::SwitchPortId,
2061    vmswitch::kernel::SwitchPort,
2062)> {
2063    let id = vmswitch::kernel::SwitchPortId {
2064        switch: match switch_id {
2065            Some(s) => s.parse().context("invalid switch id")?,
2066            None => vmswitch::hcn::DEFAULT_SWITCH,
2067        },
2068        port: Guid::new_random(),
2069    };
2070    let _ = vmswitch::hcn::Network::open(&id.switch)
2071        .with_context(|| format!("could not find switch {}", id.switch))?;
2072
2073    let port = vmswitch::kernel::SwitchPort::new(&id).context("failed to create switch port")?;
2074
2075    let id = openvmm_defs::config::SwitchPortId {
2076        switch: id.switch,
2077        port: id.port,
2078    };
2079    Ok((id, port))
2080}
2081
2082fn parse_endpoint(
2083    cli_cfg: &NicConfigCli,
2084    index: &mut usize,
2085    resources: &mut VmResources,
2086) -> anyhow::Result<NicConfig> {
2087    let _ = resources;
2088    let endpoint = match &cli_cfg.endpoint {
2089        EndpointConfigCli::Consomme { cidr, host_fwd } => {
2090            let ports = host_fwd
2091                .iter()
2092                .map(|fwd| {
2093                    use net_backend_resources::consomme::HostPortProtocol;
2094                    net_backend_resources::consomme::HostPortConfig {
2095                        protocol: match fwd.protocol {
2096                            cli_args::HostPortProtocolCli::Tcp => HostPortProtocol::Tcp,
2097                            cli_args::HostPortProtocolCli::Udp => HostPortProtocol::Udp,
2098                        },
2099                        host_address: fwd
2100                            .host_address
2101                            .map(net_backend_resources::consomme::HostIpAddress::from),
2102                        host_port: net_backend_resources::consomme::HostPort::Fixed(fwd.host_port),
2103                        guest_port: fwd.guest_port,
2104                    }
2105                })
2106                .collect();
2107            // Only wire the bind/unbind RPC channel to the first consomme
2108            // endpoint. Additional consomme NICs work normally but cannot be
2109            // targeted by runtime bind/unbind commands.
2110            let recv = if resources.consomme_rpc.is_none() {
2111                let (send, recv) = mesh::channel();
2112                resources.consomme_rpc = Some(send);
2113                Some(recv)
2114            } else {
2115                None
2116            };
2117            net_backend_resources::consomme::ConsommeHandle {
2118                cidr: cidr.clone(),
2119                ports,
2120                recv,
2121            }
2122            .into_resource()
2123        }
2124        EndpointConfigCli::None => net_backend_resources::null::NullHandle.into_resource(),
2125        EndpointConfigCli::Dio { id } => {
2126            #[cfg(windows)]
2127            {
2128                let (port_id, port) = new_switch_port(id.as_deref())?;
2129                resources.switch_ports.push(port);
2130                net_backend_resources::dio::WindowsDirectIoHandle {
2131                    switch_port_id: net_backend_resources::dio::SwitchPortId {
2132                        switch: port_id.switch,
2133                        port: port_id.port,
2134                    },
2135                }
2136                .into_resource()
2137            }
2138
2139            #[cfg(not(windows))]
2140            {
2141                let _ = id;
2142                bail!("cannot use dio on non-windows platforms")
2143            }
2144        }
2145        EndpointConfigCli::Tap { name } => {
2146            #[cfg(target_os = "linux")]
2147            {
2148                let fd = net_tap::tap::open_tap(name)
2149                    .with_context(|| format!("failed to open TAP device '{name}'"))?;
2150                net_backend_resources::tap::TapHandle { fd }.into_resource()
2151            }
2152
2153            #[cfg(not(target_os = "linux"))]
2154            {
2155                let _ = name;
2156                bail!("TAP backend is only supported on Linux")
2157            }
2158        }
2159    };
2160
2161    // Pick a random MAC address.
2162    let mut mac_address = [0x00, 0x15, 0x5D, 0, 0, 0];
2163    getrandom::fill(&mut mac_address[3..]).expect("rng failure");
2164
2165    // Pick a fixed instance ID based on the index.
2166    const BASE_INSTANCE_ID: Guid = guid::guid!("00000000-da43-11ed-936a-00155d6db52f");
2167    let instance_id = Guid {
2168        data1: *index as u32,
2169        ..BASE_INSTANCE_ID
2170    };
2171    *index += 1;
2172
2173    Ok(NicConfig {
2174        vtl: cli_cfg.vtl,
2175        instance_id,
2176        endpoint,
2177        mac_address: mac_address.into(),
2178        max_queues: cli_cfg.max_queues,
2179        pcie_port: cli_cfg.pcie_port.clone(),
2180    })
2181}
2182
2183#[derive(Debug)]
2184struct NicConfig {
2185    vtl: DeviceVtl,
2186    instance_id: Guid,
2187    mac_address: MacAddress,
2188    endpoint: Resource<NetEndpointHandleKind>,
2189    max_queues: Option<u16>,
2190    pcie_port: Option<String>,
2191}
2192
2193impl NicConfig {
2194    fn into_netvsp_handle(self) -> (DeviceVtl, Resource<VmbusDeviceHandleKind>) {
2195        (
2196            self.vtl,
2197            netvsp_resources::NetvspHandle {
2198                instance_id: self.instance_id,
2199                mac_address: self.mac_address,
2200                endpoint: self.endpoint,
2201                max_queues: self.max_queues,
2202            }
2203            .into_resource(),
2204        )
2205    }
2206}
2207
2208enum LayerOrDisk {
2209    Layer(DiskLayerDescription),
2210    Disk(Resource<DiskHandleKind>),
2211}
2212
2213async fn disk_open(
2214    disk_cli: &DiskCliKind,
2215    read_only: bool,
2216) -> anyhow::Result<Resource<DiskHandleKind>> {
2217    let mut layers = Vec::new();
2218    disk_open_inner(disk_cli, read_only, &mut layers).await?;
2219    if layers.len() == 1 && matches!(layers[0], LayerOrDisk::Disk(_)) {
2220        let LayerOrDisk::Disk(disk) = layers.pop().unwrap() else {
2221            unreachable!()
2222        };
2223        Ok(disk)
2224    } else {
2225        Ok(Resource::new(disk_backend_resources::LayeredDiskHandle {
2226            layers: layers
2227                .into_iter()
2228                .map(|layer| match layer {
2229                    LayerOrDisk::Layer(layer) => layer,
2230                    LayerOrDisk::Disk(disk) => DiskLayerDescription {
2231                        layer: DiskLayerHandle(disk).into_resource(),
2232                        read_cache: false,
2233                        write_through: false,
2234                    },
2235                })
2236                .collect(),
2237        }))
2238    }
2239}
2240
2241fn disk_open_inner<'a>(
2242    disk_cli: &'a DiskCliKind,
2243    read_only: bool,
2244    layers: &'a mut Vec<LayerOrDisk>,
2245) -> futures::future::BoxFuture<'a, anyhow::Result<()>> {
2246    Box::pin(async move {
2247        fn layer<T: IntoResource<DiskLayerHandleKind>>(layer: T) -> LayerOrDisk {
2248            LayerOrDisk::Layer(layer.into_resource().into())
2249        }
2250        fn disk<T: IntoResource<DiskHandleKind>>(disk: T) -> LayerOrDisk {
2251            LayerOrDisk::Disk(disk.into_resource())
2252        }
2253        match disk_cli {
2254            &DiskCliKind::Memory(len) => {
2255                layers.push(layer(RamDiskLayerHandle {
2256                    len: Some(len),
2257                    sector_size: None,
2258                }));
2259            }
2260            DiskCliKind::File {
2261                path,
2262                create_with_len,
2263                direct,
2264            } => layers.push(LayerOrDisk::Disk(if let Some(size) = create_with_len {
2265                create_disk_type(
2266                    path,
2267                    *size,
2268                    OpenDiskOptions {
2269                        read_only: false,
2270                        direct: *direct,
2271                    },
2272                )
2273                .with_context(|| format!("failed to create {}", path.display()))?
2274            } else {
2275                open_disk_type(
2276                    path,
2277                    OpenDiskOptions {
2278                        read_only,
2279                        direct: *direct,
2280                    },
2281                )
2282                .await
2283                .with_context(|| format!("failed to open {}", path.display()))?
2284            })),
2285            DiskCliKind::Blob { kind, url } => {
2286                layers.push(disk(disk_backend_resources::BlobDiskHandle {
2287                    url: url.to_owned(),
2288                    format: match kind {
2289                        cli_args::BlobKind::Flat => disk_backend_resources::BlobDiskFormat::Flat,
2290                        cli_args::BlobKind::Vhd1 => {
2291                            disk_backend_resources::BlobDiskFormat::FixedVhd1
2292                        }
2293                    },
2294                }))
2295            }
2296            DiskCliKind::MemoryDiff(inner) => {
2297                layers.push(layer(RamDiskLayerHandle {
2298                    len: None,
2299                    sector_size: None,
2300                }));
2301                disk_open_inner(inner, true, layers).await?;
2302            }
2303            DiskCliKind::PersistentReservationsWrapper(inner) => {
2304                layers.push(disk(disk_backend_resources::DiskWithReservationsHandle(
2305                    disk_open(inner, read_only).await?,
2306                )))
2307            }
2308            DiskCliKind::DelayDiskWrapper {
2309                delay_ms,
2310                disk: inner,
2311            } => layers.push(disk(DelayDiskHandle {
2312                delay: CellUpdater::new(Duration::from_millis(*delay_ms)).cell(),
2313                disk: disk_open(inner, read_only).await?,
2314            })),
2315            DiskCliKind::Crypt {
2316                disk: inner,
2317                cipher,
2318                key_file,
2319            } => layers.push(disk(disk_crypt_resources::DiskCryptHandle {
2320                disk: disk_open(inner, read_only).await?,
2321                cipher: match cipher {
2322                    cli_args::DiskCipher::XtsAes256 => disk_crypt_resources::Cipher::XtsAes256,
2323                },
2324                key: fs_err::read(key_file).context("failed to read key file")?,
2325            })),
2326            DiskCliKind::Sqlite {
2327                path,
2328                create_with_len,
2329            } => {
2330                // FUTURE: this code should be responsible for opening
2331                // file-handle(s) itself, and passing them into sqlite via a custom
2332                // vfs. For now though - simply check if the file exists or not, and
2333                // perform early validation of filesystem-level create options.
2334                match (create_with_len.is_some(), path.exists()) {
2335                    (true, true) => anyhow::bail!(
2336                        "cannot create new sqlite disk at {} - file already exists",
2337                        path.display()
2338                    ),
2339                    (false, false) => anyhow::bail!(
2340                        "cannot open sqlite disk at {} - file not found",
2341                        path.display()
2342                    ),
2343                    _ => {}
2344                }
2345
2346                layers.push(layer(SqliteDiskLayerHandle {
2347                    dbhd_path: path.display().to_string(),
2348                    format_dbhd: create_with_len.map(|len| {
2349                        disk_backend_resources::layer::SqliteDiskLayerFormatParams {
2350                            logically_read_only: false,
2351                            len: Some(len),
2352                        }
2353                    }),
2354                }));
2355            }
2356            DiskCliKind::SqliteDiff { path, create, disk } => {
2357                // FUTURE: this code should be responsible for opening
2358                // file-handle(s) itself, and passing them into sqlite via a custom
2359                // vfs. For now though - simply check if the file exists or not, and
2360                // perform early validation of filesystem-level create options.
2361                match (create, path.exists()) {
2362                    (true, true) => anyhow::bail!(
2363                        "cannot create new sqlite disk at {} - file already exists",
2364                        path.display()
2365                    ),
2366                    (false, false) => anyhow::bail!(
2367                        "cannot open sqlite disk at {} - file not found",
2368                        path.display()
2369                    ),
2370                    _ => {}
2371                }
2372
2373                layers.push(layer(SqliteDiskLayerHandle {
2374                    dbhd_path: path.display().to_string(),
2375                    format_dbhd: create.then_some(
2376                        disk_backend_resources::layer::SqliteDiskLayerFormatParams {
2377                            logically_read_only: false,
2378                            len: None,
2379                        },
2380                    ),
2381                }));
2382                disk_open_inner(disk, true, layers).await?;
2383            }
2384            DiskCliKind::AutoCacheSqlite {
2385                cache_path,
2386                key,
2387                disk,
2388            } => {
2389                layers.push(LayerOrDisk::Layer(DiskLayerDescription {
2390                    read_cache: true,
2391                    write_through: false,
2392                    layer: SqliteAutoCacheDiskLayerHandle {
2393                        cache_path: cache_path.clone(),
2394                        cache_key: key.clone(),
2395                    }
2396                    .into_resource(),
2397                }));
2398                disk_open_inner(disk, read_only, layers).await?;
2399            }
2400        }
2401        Ok(())
2402    })
2403}
2404
2405/// Get the system page size.
2406pub(crate) fn system_page_size() -> u32 {
2407    sparse_mmap::SparseMapping::page_size() as u32
2408}
2409
2410/// The guest architecture string, derived from the compile-time `guest_arch` cfg.
2411pub(crate) const GUEST_ARCH: &str = if cfg!(guest_arch = "x86_64") {
2412    "x86_64"
2413} else {
2414    "aarch64"
2415};
2416
2417/// Open a snapshot directory and validate it against the current VM config.
2418/// Returns the shared memory fd (from memory.bin) and the saved device state.
2419fn prepare_snapshot_restore(
2420    snapshot_dir: &Path,
2421    opt: &Options,
2422) -> anyhow::Result<(
2423    openvmm_defs::worker::SharedMemoryFd,
2424    mesh::payload::message::ProtobufMessage,
2425)> {
2426    let (manifest, state_bytes) = openvmm_helpers::snapshot::read_snapshot(snapshot_dir)?;
2427
2428    // Validate manifest against current VM config.
2429    openvmm_helpers::snapshot::validate_manifest(
2430        &manifest,
2431        GUEST_ARCH,
2432        opt.memory_size(),
2433        opt.processors,
2434        system_page_size(),
2435    )?;
2436
2437    // Open memory.bin (existing file, no create, no resize).
2438    let memory_file = fs_err::OpenOptions::new()
2439        .read(true)
2440        .write(true)
2441        .open(snapshot_dir.join("memory.bin"))?;
2442
2443    // Validate file size matches expected memory size.
2444    let file_size = memory_file.metadata()?.len();
2445    if file_size != manifest.memory_size_bytes {
2446        anyhow::bail!(
2447            "memory.bin size ({file_size} bytes) doesn't match manifest ({} bytes)",
2448            manifest.memory_size_bytes,
2449        );
2450    }
2451
2452    let shared_memory_fd =
2453        openvmm_helpers::shared_memory::file_to_shared_memory_fd(memory_file.into())?;
2454
2455    // Reconstruct ProtobufMessage from the saved state bytes.
2456    // The save side wrote mesh::payload::encode(ProtobufMessage), so we decode
2457    // back to ProtobufMessage.
2458    let state_msg: mesh::payload::message::ProtobufMessage = mesh::payload::decode(&state_bytes)
2459        .context("failed to decode saved state from snapshot")?;
2460
2461    Ok((shared_memory_fd, state_msg))
2462}
2463
2464fn do_main(pidfile_guard: &mut Option<pidfile::Pidfile>) -> anyhow::Result<i32> {
2465    #[cfg(windows)]
2466    pal::windows::disable_hard_error_dialog();
2467
2468    tracing_init::enable_tracing()?;
2469
2470    // Try to run as a worker host.
2471    // On success the worker runs to completion and then exits the process (does
2472    // not return). Any worker host setup errors are return and bubbled up.
2473    meshworker::run_vmm_mesh_host()?;
2474
2475    let opt = cli_args::parse_options();
2476    if let Some(path) = &opt.write_saved_state_proto {
2477        mesh::payload::protofile::DescriptorWriter::new(vmcore::save_restore::saved_state_roots())
2478            .write_to_path(path)
2479            .context("failed to write protobuf descriptors")?;
2480        return Ok(0);
2481    }
2482
2483    if let Some(ref path) = opt.pidfile {
2484        *pidfile_guard = Some(pidfile::Pidfile::new(path).context("failed to create pidfile")?);
2485    }
2486
2487    if let Some(path) = opt.relay_console_path {
2488        let console_title = opt.relay_console_title.unwrap_or_default();
2489        return console_relay::relay_console(&path, console_title.as_str()).map(|()| 0);
2490    }
2491
2492    #[cfg(any(feature = "grpc", feature = "ttrpc"))]
2493    {
2494        let rpc = opt
2495            .rpc
2496            .as_ref()
2497            .map(|rpc| {
2498                let transport = match rpc.transport {
2499                    cli_args::RpcTransportCli::Auto => ttrpc::RpcTransport::Auto,
2500                    cli_args::RpcTransportCli::Ttrpc => ttrpc::RpcTransport::Ttrpc,
2501                    cli_args::RpcTransportCli::Grpc => ttrpc::RpcTransport::Grpc,
2502                };
2503                (rpc.path.as_path(), transport)
2504            })
2505            .or_else(|| {
2506                opt.ttrpc
2507                    .as_deref()
2508                    .map(|p| (p, ttrpc::RpcTransport::Ttrpc))
2509            })
2510            .or_else(|| opt.grpc.as_deref().map(|p| (p, ttrpc::RpcTransport::Grpc)));
2511
2512        if let Some((path, transport)) = rpc {
2513            return block_on(async {
2514                let _ = std::fs::remove_file(path);
2515                let listener =
2516                    unix_socket::UnixListener::bind(path).context("failed to bind to socket")?;
2517
2518                // This is a local launch
2519                let mut handle =
2520                    mesh_worker::launch_local_worker::<ttrpc::TtrpcWorker>(ttrpc::Parameters {
2521                        listener,
2522                        transport,
2523                    })
2524                    .await?;
2525
2526                tracing::info!(%transport, path = %path.display(), "listening");
2527
2528                // Signal the parent process that the server is ready.
2529                pal::close_stdout().context("failed to close stdout")?;
2530
2531                handle.join().await?;
2532
2533                Ok(0)
2534            });
2535        }
2536    }
2537
2538    DefaultPool::run_with(async |driver| run_control(&driver, opt).await)
2539}
2540
2541fn new_hvsock_service_id(port: u32) -> Guid {
2542    // This GUID is an embedding of the AF_VSOCK port into an
2543    // AF_HYPERV service ID.
2544    Guid {
2545        data1: port,
2546        .."00000000-facb-11e6-bd58-64006a7986d3".parse().unwrap()
2547    }
2548}
2549
2550async fn run_control(driver: &DefaultDriver, opt: Options) -> anyhow::Result<i32> {
2551    let mut mesh = Some(VmmMesh::new(&driver, opt.single_process)?);
2552    let result = run_control_inner(driver, &mut mesh, opt).await;
2553    // If setup failed before the mesh was handed to the controller, shut it
2554    // down so the child host process exits cleanly without noisy logs.
2555    if let Some(mesh) = mesh {
2556        mesh.shutdown().await;
2557    }
2558    result
2559}
2560
2561async fn run_control_inner(
2562    driver: &DefaultDriver,
2563    mesh_slot: &mut Option<VmmMesh>,
2564    opt: Options,
2565) -> anyhow::Result<i32> {
2566    let mesh = mesh_slot.as_ref().unwrap();
2567    let (mut vm_config, mut resources) = vm_config_from_command_line(driver, mesh, &opt).await?;
2568
2569    let mut vnc_worker = None;
2570    if opt.gfx || opt.vnc.vnc {
2571        // Parse the listen address. Try as a full SocketAddr (host:port) first;
2572        // fall back to a bare IP, using the configured port.
2573        let addr: std::net::SocketAddr = if let Ok(sa) =
2574            opt.vnc.vnc_listen.parse::<std::net::SocketAddr>()
2575        {
2576            sa
2577        } else {
2578            let ip: std::net::IpAddr = opt.vnc.vnc_listen.parse().with_context(|| {
2579                format!(
2580                    "invalid VNC listen address: {} (expected IP address or socket address like [::1]:5900)",
2581                    opt.vnc.vnc_listen
2582                )
2583            })?;
2584            std::net::SocketAddr::new(ip, opt.vnc.vnc_port)
2585        };
2586
2587        let socket = socket2::Socket::new(
2588            if addr.is_ipv6() {
2589                socket2::Domain::IPV6
2590            } else {
2591                socket2::Domain::IPV4
2592            },
2593            socket2::Type::STREAM,
2594            None,
2595        )
2596        .with_context(|| format!("creating VNC socket for {}", addr))?;
2597
2598        if addr.is_ipv6() {
2599            if let Err(e) = socket.set_only_v6(false) {
2600                tracing::warn!(
2601                    error = %e,
2602                    "failed to enable dual-stack on IPv6 VNC socket, IPv4 clients may not be able to connect"
2603                );
2604            }
2605        }
2606        socket.set_reuse_address(true)?;
2607        socket
2608            .bind(&addr.into())
2609            .with_context(|| format!("binding VNC socket to {}", addr))?;
2610        socket
2611            .listen(128)
2612            .with_context(|| format!("listening on VNC socket {}", addr))?;
2613        let listener: TcpListener = socket.into();
2614
2615        if !addr.ip().is_loopback() {
2616            tracing::warn!(
2617                address = %addr,
2618                "VNC server listening on non-localhost address without authentication"
2619            );
2620        }
2621
2622        let input_send = vm_config.input.sender();
2623        let framebuffer = resources
2624            .framebuffer_access
2625            .take()
2626            .expect("synth video enabled");
2627
2628        let vnc_host = mesh
2629            .make_host("vnc", None)
2630            .await
2631            .context("spawning vnc process failed")?;
2632
2633        vnc_worker = Some(
2634            vnc_host
2635                .launch_worker(
2636                    vnc_worker_defs::VNC_WORKER_TCP,
2637                    VncParameters {
2638                        listener,
2639                        framebuffer,
2640                        input_send,
2641                        dirty_recv: resources.dirty_rect_recv.take(),
2642                        max_clients: opt.vnc.vnc_max_clients,
2643                        evict_oldest: opt.vnc.vnc_evict_oldest,
2644                    },
2645                )
2646                .await?,
2647        )
2648    }
2649
2650    // spin up the debug worker
2651    let gdb_worker = if let Some(port) = opt.gdb {
2652        let listener = TcpListener::bind(format!("127.0.0.1:{}", port))
2653            .with_context(|| format!("binding to gdb port {}", port))?;
2654
2655        let (req_tx, req_rx) = mesh::channel();
2656        vm_config.debugger_rpc = Some(req_rx);
2657
2658        let gdb_host = mesh
2659            .make_host("gdb", None)
2660            .await
2661            .context("spawning gdbstub process failed")?;
2662
2663        Some(
2664            gdb_host
2665                .launch_worker(
2666                    debug_worker_defs::DEBUGGER_WORKER,
2667                    debug_worker_defs::DebuggerParameters {
2668                        listener,
2669                        req_chan: req_tx,
2670                        vp_count: vm_config.processor_topology.proc_count,
2671                        target_arch: if cfg!(guest_arch = "x86_64") {
2672                            debug_worker_defs::TargetArch::X86_64
2673                        } else {
2674                            debug_worker_defs::TargetArch::Aarch64
2675                        },
2676                    },
2677                )
2678                .await
2679                .context("failed to launch gdbstub worker")?,
2680        )
2681    } else {
2682        None
2683    };
2684
2685    // spin up the VM
2686    let (vm_rpc, rpc_recv) = mesh::channel();
2687    let (notify_send, notify_recv) = mesh::channel();
2688    let vm_worker = {
2689        let vm_host = mesh.make_host("vm", opt.log_file.clone()).await?;
2690
2691        let (shared_memory, saved_state) = if let Some(snapshot_dir) = &opt.restore_snapshot {
2692            let (fd, state_msg) = prepare_snapshot_restore(snapshot_dir, &opt)?;
2693            (Some(fd), Some(state_msg))
2694        } else {
2695            let shared_memory = opt
2696                .memory_backing_file()
2697                .map(|path| {
2698                    openvmm_helpers::shared_memory::open_memory_backing_file(
2699                        path,
2700                        opt.memory_size(),
2701                    )
2702                })
2703                .transpose()?;
2704            (shared_memory, None)
2705        };
2706
2707        let params = VmWorkerParameters {
2708            hypervisor: match &opt.hypervisor {
2709                Some(name) => openvmm_helpers::hypervisor::hypervisor_resource(name)?,
2710                None => openvmm_helpers::hypervisor::choose_hypervisor()?,
2711            },
2712            cfg: vm_config,
2713            saved_state,
2714            shared_memory,
2715            rpc: rpc_recv,
2716            notify: notify_send,
2717        };
2718        vm_host
2719            .launch_worker(VM_WORKER, params)
2720            .await
2721            .context("failed to launch vm worker")?
2722    };
2723
2724    if opt.restore_snapshot.is_some() {
2725        tracing::info!("restoring VM from snapshot");
2726    }
2727
2728    if !opt.paused {
2729        vm_rpc.call(VmRpc::Resume, ()).await?;
2730    }
2731
2732    let paravisor_diag = Arc::new(diag_client::DiagClient::from_dialer(
2733        driver.clone(),
2734        DiagDialer {
2735            driver: driver.clone(),
2736            vm_rpc: vm_rpc.clone(),
2737            openhcl_vtl: if opt.vtl2 {
2738                DeviceVtl::Vtl2
2739            } else {
2740                DeviceVtl::Vtl0
2741            },
2742        },
2743    ));
2744
2745    let diag_inspector = DiagInspector::new(driver.clone(), paravisor_diag.clone());
2746
2747    // Create channels between the REPL and VmController.
2748    let (vm_controller_send, vm_controller_recv) = mesh::channel();
2749    let (vm_controller_event_send, vm_controller_event_recv) = mesh::channel();
2750
2751    let has_vtl2 = resources.vtl2_settings.is_some();
2752    let serial_driver = resources
2753        .serial_driver
2754        .take()
2755        .expect("serial driver must outlive serial resources");
2756
2757    // Build the VmController with exclusive resources.
2758    let controller = vm_controller::VmController {
2759        mesh: mesh_slot.take().unwrap(),
2760        vm_worker,
2761        vnc_worker,
2762        gdb_worker,
2763        diag_inspector: Some(diag_inspector),
2764        vtl2_settings: resources.vtl2_settings,
2765        ged_rpc: resources.ged_rpc.clone(),
2766        vm_rpc: vm_rpc.clone(),
2767        paravisor_diag: Some(paravisor_diag),
2768        igvm_path: opt.igvm.clone(),
2769        memory_backing_file: opt.memory_backing_file().cloned(),
2770        memory: opt.memory_size(),
2771        processors: opt.processors,
2772        log_file: opt.log_file.clone(),
2773        guest_power_actions: vm_controller::GuestPowerActions {
2774            shutdown: opt.guest_shutdown_action,
2775            reset: opt.guest_reset_action,
2776            crash: opt.guest_crash_action,
2777            watchdog: opt.guest_watchdog_action,
2778        },
2779    };
2780
2781    // Spawn the VmController as a task.
2782    let controller_task = driver.spawn(
2783        "vm-controller",
2784        controller.run(vm_controller_recv, vm_controller_event_send, notify_recv),
2785    );
2786
2787    // Run the REPL with shareable resources.
2788    let repl_result = repl::run_repl(
2789        driver,
2790        repl::ReplResources {
2791            vm_rpc,
2792            vm_controller: vm_controller_send,
2793            vm_controller_events: vm_controller_event_recv,
2794            scsi_rpc: resources.scsi_rpc,
2795            nvme_vtl2_rpc: resources.nvme_vtl2_rpc,
2796            consomme_rpc: resources.consomme_rpc,
2797            shutdown_ic: resources.shutdown_ic,
2798            kvp_ic: resources.kvp_ic,
2799            console_in: resources.console_in,
2800            has_vtl2,
2801        },
2802    )
2803    .await;
2804
2805    // Wait for the controller task to finish (it stops the VM worker and
2806    // shuts down the mesh).
2807    controller_task.await;
2808    drop(serial_driver);
2809
2810    // run_repl returns the exit status: the code the guest drove via an opt-in
2811    // exit (VmControllerEvent::ExitRequested), or 0 when the VM stopped normally.
2812    repl_result
2813}
2814
2815struct DiagDialer {
2816    driver: DefaultDriver,
2817    vm_rpc: mesh::Sender<VmRpc>,
2818    openhcl_vtl: DeviceVtl,
2819}
2820
2821impl mesh_rpc::client::Dial for DiagDialer {
2822    type Stream = PolledSocket<unix_socket::UnixStream>;
2823
2824    async fn dial(&mut self) -> io::Result<Self::Stream> {
2825        let service_id = new_hvsock_service_id(1);
2826        let socket = self
2827            .vm_rpc
2828            .call_failable(
2829                VmRpc::ConnectHvsock,
2830                (
2831                    CancelContext::new().with_timeout(Duration::from_secs(2)),
2832                    service_id,
2833                    self.openhcl_vtl,
2834                ),
2835            )
2836            .await
2837            .map_err(io::Error::other)?;
2838
2839        PolledSocket::new(&self.driver, socket)
2840    }
2841}
2842
2843/// An object that implements [`InspectMut`] by sending an inspect request over
2844/// TTRPC to the guest (typically the paravisor running in VTL2), then stitching
2845/// the response back into the inspect tree.
2846///
2847/// This also caches the TTRPC connection to the guest so that only the first
2848/// inspect request has to wait for the connection to be established.
2849pub(crate) struct DiagInspector(DiagInspectorInner);
2850
2851enum DiagInspectorInner {
2852    NotStarted(DefaultDriver, Arc<diag_client::DiagClient>),
2853    Started {
2854        send: mesh::Sender<inspect::Deferred>,
2855        _task: Task<()>,
2856    },
2857    Invalid,
2858}
2859
2860impl DiagInspector {
2861    pub fn new(driver: DefaultDriver, diag_client: Arc<diag_client::DiagClient>) -> Self {
2862        Self(DiagInspectorInner::NotStarted(driver, diag_client))
2863    }
2864
2865    fn start(&mut self) -> &mesh::Sender<inspect::Deferred> {
2866        loop {
2867            match self.0 {
2868                DiagInspectorInner::NotStarted { .. } => {
2869                    let DiagInspectorInner::NotStarted(driver, client) =
2870                        std::mem::replace(&mut self.0, DiagInspectorInner::Invalid)
2871                    else {
2872                        unreachable!()
2873                    };
2874                    let (send, recv) = mesh::channel();
2875                    let task = driver.clone().spawn("diag-inspect", async move {
2876                        Self::run(&client, recv).await
2877                    });
2878
2879                    self.0 = DiagInspectorInner::Started { send, _task: task };
2880                }
2881                DiagInspectorInner::Started { ref send, .. } => break send,
2882                DiagInspectorInner::Invalid => unreachable!(),
2883            }
2884        }
2885    }
2886
2887    async fn run(
2888        diag_client: &diag_client::DiagClient,
2889        mut recv: mesh::Receiver<inspect::Deferred>,
2890    ) {
2891        while let Some(deferred) = recv.next().await {
2892            let info = deferred.external_request();
2893            let result = match info.request_type {
2894                inspect::ExternalRequestType::Inspect { depth } => {
2895                    if depth == 0 {
2896                        Ok(inspect::Node::Unevaluated)
2897                    } else {
2898                        // TODO: Support taking timeouts from the command line
2899                        diag_client
2900                            .inspect(info.path, Some(depth - 1), Some(Duration::from_secs(1)))
2901                            .await
2902                    }
2903                }
2904                inspect::ExternalRequestType::Update { value } => {
2905                    (diag_client.update(info.path, value).await).map(inspect::Node::Value)
2906                }
2907            };
2908            deferred.complete_external(
2909                result.unwrap_or_else(|err| {
2910                    inspect::Node::Failed(inspect::Error::Mesh(format!("{err:#}")))
2911                }),
2912                inspect::SensitivityLevel::Unspecified,
2913            )
2914        }
2915    }
2916}
2917
2918impl InspectMut for DiagInspector {
2919    fn inspect_mut(&mut self, req: inspect::Request<'_>) {
2920        self.start().send(req.defer());
2921    }
2922}