Struct kvm::Vm

source ·
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

source

pub fn new(kvm: &Kvm, guest_mem: GuestMemory) -> Result<Vm>

Constructs a new Vm using the given Kvm instance.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn create_irq_chip(&self) -> Result<()>

Crates an in kernel interrupt controller.

See the documentation on the KVM_CREATE_IRQCHIP ioctl.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub(crate) fn ioeventfd( &self, evt: &Event, addr: IoeventAddress, datamatch: Datamatch, deassign: bool ) -> Result<()>

source

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.

source

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.

source

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.

source

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.

Trait Implementations§

source§

impl AsRawDescriptor for Vm

source§

fn as_raw_descriptor(&self) -> RawDescriptor

Returns the underlying raw descriptor. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Vm

§

impl Send for Vm

§

impl Sync for Vm

§

impl Unpin for Vm

§

impl UnwindSafe for Vm

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AsRawDescriptors for Twhere T: AsRawDescriptor,

source§

fn as_raw_descriptors(&self) -> Vec<i32, Global>

Returns the underlying raw descriptors. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.