Skip to main content

openvmm_defs/
worker.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Mesh worker definitions for the VM worker.
5
6use crate::config::Config;
7use crate::rpc::VmRpc;
8use hypervisor_resources::HypervisorKind;
9use mesh::MeshPayload;
10use mesh::payload::message::ProtobufMessage;
11use mesh_worker::WorkerId;
12use vm_resource::Resource;
13use vmm_core_defs::HaltReason;
14
15cfg_select! {
16    unix => {
17        /// File descriptor (Unix) or handle (Windows) for file-backed guest RAM.
18        pub type SharedMemoryFd = std::os::fd::OwnedFd;
19    }
20    windows => {
21        /// File descriptor (Unix) or handle (Windows) for file-backed guest RAM.
22        pub type SharedMemoryFd = std::os::windows::io::OwnedHandle;
23    }
24}
25
26pub const VM_WORKER: WorkerId<VmWorkerParameters> = WorkerId::new("VmWorker");
27
28/// How the VM worker sources guest RAM.
29#[derive(MeshPayload)]
30pub enum GuestRamSource {
31    /// Map this file-backed object as guest RAM. Used for CreateVM with a
32    /// backing file, and for restoring a snapshot in place.
33    MapShared(SharedMemoryFd),
34    /// Map this file-backed object as guest RAM privately copy-on-write, so
35    /// the guest's writes stay in the VM worker's process and never reach the
36    /// file. Used for restoring a snapshot without modifying it.
37    ///
38    /// Pages that the guest hasn't written are read from the file, so the
39    /// file must not change while the VM runs.
40    MapPrivate(SharedMemoryFd),
41}
42
43/// Launch parameters for the VM worker.
44#[derive(MeshPayload)]
45pub struct VmWorkerParameters {
46    /// The hypervisor to use.
47    pub hypervisor: Resource<HypervisorKind>,
48    /// The initial configuration.
49    pub cfg: Config,
50    /// The saved state.
51    pub saved_state: Option<ProtobufMessage>,
52    /// How guest RAM is sourced. When `None`, fresh memory is allocated
53    /// according to `cfg`.
54    pub guest_ram: Option<GuestRamSource>,
55    /// The VM RPC channel.
56    pub rpc: mesh::Receiver<VmRpc>,
57    /// The notification channel.
58    pub notify: mesh::Sender<HaltReason>,
59}