pub struct Vm {
pub(crate) vm: File,
pub(crate) guest_mem: GuestMemory,
pub(crate) mem_regions: Arc<Mutex<BTreeMap<u32, Box<dyn MappedRegion>>>>,
pub(crate) mem_slot_gaps: Arc<Mutex<BinaryHeap<MemSlot>>>,
}
Expand description
A wrapper around creating and using a VM.
Fields§
§vm: File
§guest_mem: GuestMemory
§mem_regions: Arc<Mutex<BTreeMap<u32, Box<dyn MappedRegion>>>>
§mem_slot_gaps: Arc<Mutex<BinaryHeap<MemSlot>>>
Implementations§
source§impl Vm
impl Vm
sourcepub fn new(kvm: &Kvm, guest_mem: GuestMemory) -> Result<Vm>
pub fn new(kvm: &Kvm, guest_mem: GuestMemory) -> Result<Vm>
Constructs a new Vm
using the given Kvm
instance.
sourcepub fn check_extension(&self, c: Cap) -> bool
pub fn check_extension(&self, c: Cap) -> bool
Checks if a particular Cap
is available.
This is distinct from the Kvm
version of this method because the some extensions depend on
the particular Vm
existence. This method is encouraged by the kernel because it more
accurately reflects the usable capabilities.
sourcepub fn add_memory_region(
&mut self,
guest_addr: GuestAddress,
mem: Box<dyn MappedRegion>,
read_only: bool,
log_dirty_pages: bool
) -> Result<u32>
pub fn add_memory_region( &mut self, guest_addr: GuestAddress, mem: Box<dyn MappedRegion>, read_only: bool, log_dirty_pages: bool ) -> Result<u32>
Inserts the given mem
into the VM’s address space at guest_addr
.
The slot that was assigned the kvm memory mapping is returned on success. The slot can be
given to Vm::remove_memory_region
to remove the memory from the VM’s address space and
take back ownership of mem
.
Note that memory inserted into the VM’s address space must not overlap with any other memory slot’s region.
If read_only
is true, the guest will be able to read the memory as normal, but attempts to
write will trigger a mmio VM exit, leaving the memory untouched.
If log_dirty_pages
is true, the slot number can be used to retrieve the pages written to
by the guest with get_dirty_log
.
sourcepub fn remove_memory_region(
&mut self,
slot: u32
) -> Result<Box<dyn MappedRegion>>
pub fn remove_memory_region( &mut self, slot: u32 ) -> Result<Box<dyn MappedRegion>>
Removes memory that was previously added at the given slot.
Ownership of the host memory mapping associated with the given slot is returned on success.
sourcepub fn get_dirty_log(&self, slot: u32, dirty_log: &mut [u8]) -> Result<()>
pub fn get_dirty_log(&self, slot: u32, dirty_log: &mut [u8]) -> Result<()>
Gets the bitmap of dirty pages since the last call to get_dirty_log
for the memory at
slot
.
The size of dirty_log
must be at least as many bits as there are pages in the memory
region slot
represents. For example, if the size of slot
is 16 pages, dirty_log
must
be 2 bytes or greater.
sourcepub fn get_memory(&self) -> &GuestMemory
pub fn get_memory(&self) -> &GuestMemory
Gets a reference to the guest memory owned by this VM.
Note that GuestMemory
does not include any mmio memory that may have been added after
this VM was constructed.
sourcepub fn set_identity_map_addr(&self, addr: GuestAddress) -> Result<()>
pub fn set_identity_map_addr(&self, addr: GuestAddress) -> Result<()>
Sets the address of a one-page region in the VM’s address space.
See the documentation on the KVM_SET_IDENTITY_MAP_ADDR ioctl.
sourcepub fn get_clock(&self) -> Result<kvm_clock_data>
pub fn get_clock(&self) -> Result<kvm_clock_data>
Retrieves the current timestamp of kvmclock as seen by the current guest.
See the documentation on the KVM_GET_CLOCK ioctl.
sourcepub fn set_clock(&self, clock_data: &kvm_clock_data) -> Result<()>
pub fn set_clock(&self, clock_data: &kvm_clock_data) -> Result<()>
Sets the current timestamp of kvmclock to the specified value.
See the documentation on the KVM_SET_CLOCK ioctl.
sourcepub fn create_irq_chip(&self) -> Result<()>
pub fn create_irq_chip(&self) -> Result<()>
Crates an in kernel interrupt controller.
See the documentation on the KVM_CREATE_IRQCHIP ioctl.
sourcepub fn get_pic_state(&self, id: PicId) -> Result<kvm_pic_state>
pub fn get_pic_state(&self, id: PicId) -> Result<kvm_pic_state>
Retrieves the state of given interrupt controller by issuing KVM_GET_IRQCHIP ioctl.
Note that this call can only succeed after a call to Vm::create_irq_chip
.
sourcepub fn set_pic_state(&self, id: PicId, state: &kvm_pic_state) -> Result<()>
pub fn set_pic_state(&self, id: PicId, state: &kvm_pic_state) -> Result<()>
Sets the state of given interrupt controller by issuing KVM_SET_IRQCHIP ioctl.
Note that this call can only succeed after a call to Vm::create_irq_chip
.
sourcepub fn get_ioapic_state(&self) -> Result<kvm_ioapic_state>
pub fn get_ioapic_state(&self) -> Result<kvm_ioapic_state>
Retrieves the state of IOAPIC by issuing KVM_GET_IRQCHIP ioctl.
Note that this call can only succeed after a call to Vm::create_irq_chip
.
sourcepub fn set_ioapic_state(&self, state: &kvm_ioapic_state) -> Result<()>
pub fn set_ioapic_state(&self, state: &kvm_ioapic_state) -> Result<()>
Sets the state of IOAPIC by issuing KVM_SET_IRQCHIP ioctl.
Note that this call can only succeed after a call to Vm::create_irq_chip
.
sourcepub fn set_irq_line(&self, irq: u32, active: bool) -> Result<()>
pub fn set_irq_line(&self, irq: u32, active: bool) -> Result<()>
Sets the level on the given irq to 1 if active
is true, and 0 otherwise.
sourcepub fn create_pit(&self) -> Result<()>
pub fn create_pit(&self) -> Result<()>
Creates a PIT as per the KVM_CREATE_PIT2 ioctl.
Note that this call can only succeed after a call to Vm::create_irq_chip
.
sourcepub fn get_pit_state(&self) -> Result<kvm_pit_state2>
pub fn get_pit_state(&self) -> Result<kvm_pit_state2>
Retrieves the state of PIT by issuing KVM_GET_PIT2 ioctl.
Note that this call can only succeed after a call to Vm::create_pit
.
sourcepub fn set_pit_state(&self, pit_state: &kvm_pit_state2) -> Result<()>
pub fn set_pit_state(&self, pit_state: &kvm_pit_state2) -> Result<()>
Sets the state of PIT by issuing KVM_SET_PIT2 ioctl.
Note that this call can only succeed after a call to Vm::create_pit
.
sourcepub fn register_ioevent(
&self,
evt: &Event,
addr: IoeventAddress,
datamatch: Datamatch
) -> Result<()>
pub fn register_ioevent( &self, evt: &Event, addr: IoeventAddress, datamatch: Datamatch ) -> Result<()>
Registers an event to be signaled whenever a certain address is written to.
The datamatch
parameter can be used to limit signaling evt
to only the cases where the
value being written is equal to datamatch
. Note that the size of datamatch
is important
and must match the expected size of the guest’s write.
In all cases where evt
is signaled, the ordinary vmexit to userspace that would be
triggered is prevented.
sourcepub fn unregister_ioevent(
&self,
evt: &Event,
addr: IoeventAddress,
datamatch: Datamatch
) -> Result<()>
pub fn unregister_ioevent( &self, evt: &Event, addr: IoeventAddress, datamatch: Datamatch ) -> Result<()>
Unregisters an event previously registered with register_ioevent
.
The evt
, addr
, and datamatch
set must be the same as the ones passed into
register_ioevent
.
pub(crate) fn ioeventfd( &self, evt: &Event, addr: IoeventAddress, datamatch: Datamatch, deassign: bool ) -> Result<()>
sourcepub fn register_irqfd_resample(
&self,
evt: &Event,
resample_evt: &Event,
gsi: u32
) -> Result<()>
pub fn register_irqfd_resample( &self, evt: &Event, resample_evt: &Event, gsi: u32 ) -> Result<()>
Registers an event that will, when signalled, trigger the gsi
irq, and resample_evt
will
get triggered when the irqchip is resampled.
sourcepub fn unregister_irqfd(&self, evt: &Event, gsi: u32) -> Result<()>
pub fn unregister_irqfd(&self, evt: &Event, gsi: u32) -> Result<()>
Unregisters an event that was previously registered with
register_irqfd
/register_irqfd_resample
.
The evt
and gsi
pair must be the same as the ones passed into
register_irqfd
/register_irqfd_resample
.
sourcepub fn set_gsi_routing(&self, routes: &[IrqRoute]) -> Result<()>
pub fn set_gsi_routing(&self, routes: &[IrqRoute]) -> Result<()>
Sets the GSI routing table, replacing any table set with previous calls to
set_gsi_routing
.
sourcepub unsafe fn kvm_enable_cap(&self, cap: &kvm_enable_cap) -> Result<()>
pub unsafe fn kvm_enable_cap(&self, cap: &kvm_enable_cap) -> Result<()>
Enable the specified capability. See documentation for KVM_ENABLE_CAP.
§Safety
This function is marked as unsafe because cap
may contain values which are interpreted as
pointers by the kernel.