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