Struct kvm_ioctls::VcpuFd[][src]

pub struct VcpuFd { /* fields omitted */ }
Expand description

Wrapper over KVM vCPU ioctls.

Implementations

impl VcpuFd[src]

pub fn get_regs(&self) -> Result<kvm_regs, Error>[src]

Returns the vCPU general purpose registers.

The registers are returned in a kvm_regs structure as defined in the KVM API documentation. See documentation for KVM_GET_REGS.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
let regs = vcpu.get_regs().unwrap();

pub fn set_regs(&self, regs: &kvm_regs) -> Result<(), Error>[src]

Sets the vCPU general purpose registers using the KVM_SET_REGS ioctl.

Arguments

  • regs - general purpose registers. For details check the kvm_regs structure in the KVM API doc.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();

#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))] {
    // Get the current vCPU registers.
    let mut regs = vcpu.get_regs().unwrap();
    // Set a new value for the Instruction Pointer.
    regs.rip = 0x100;
    vcpu.set_regs(&regs).unwrap();
}

pub fn get_sregs(&self) -> Result<kvm_sregs, Error>[src]

Returns the vCPU special registers.

The registers are returned in a kvm_sregs structure as defined in the KVM API documentation. See documentation for KVM_GET_SREGS.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
let sregs = vcpu.get_sregs().unwrap();

pub fn set_sregs(&self, sregs: &kvm_sregs) -> Result<(), Error>[src]

Sets the vCPU special registers using the KVM_SET_SREGS ioctl.

Arguments

  • sregs - Special registers. For details check the kvm_sregs structure in the KVM API doc.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))] {
    let mut sregs = vcpu.get_sregs().unwrap();
    // Update the code segment (cs).
    sregs.cs.base = 0;
    sregs.cs.selector = 0;
    vcpu.set_sregs(&sregs).unwrap();
}

pub fn get_fpu(&self) -> Result<kvm_fpu, Error>[src]

Returns the floating point state (FPU) from the vCPU.

The state is returned in a kvm_fpu structure as defined in the KVM API doc. See the documentation for KVM_GET_FPU.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
let fpu = vcpu.get_fpu().unwrap();

pub fn set_fpu(&self, fpu: &kvm_fpu) -> Result<(), Error>[src]

Set the floating point state (FPU) of a vCPU using the KVM_SET_FPU ioct.

Arguments

  • fpu - FPU configuration. For details check the kvm_fpu structure in the KVM API doc.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
    let KVM_FPU_CWD: u16 = 0x37f;
    let fpu = kvm_fpu {
        fcw: KVM_FPU_CWD,
        ..Default::default()
    };
    vcpu.set_fpu(&fpu).unwrap();
}

pub fn set_cpuid2(&self, cpuid: &CpuId) -> Result<(), Error>[src]

X86 specific call to setup the CPUID registers.

See the documentation for KVM_SET_CPUID2.

Arguments

  • cpuid - CPUID registers.

Example

let kvm = Kvm::new().unwrap();
let mut kvm_cpuid = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES).unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();

// Update the CPUID entries to disable the EPB feature.
const ECX_EPB_SHIFT: u32 = 3;
{
   let entries = kvm_cpuid.as_mut_slice();
   for entry in entries.iter_mut() {
       match entry.function {
           6 => entry.ecx &= !(1 << ECX_EPB_SHIFT),
           _ => (),
       }
   }
}

vcpu.set_cpuid2(&kvm_cpuid).unwrap();

pub fn get_cpuid2(&self, num_entries: usize) -> Result<CpuId, Error>[src]

X86 specific call to retrieve the CPUID registers.

It requires knowledge of how many kvm_cpuid_entry2 entries there are to get. See the documentation for KVM_GET_CPUID2 in the KVM API doc.

Arguments

  • num_entries - Number of CPUID entries to be read.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let cpuid = vcpu.get_cpuid2(KVM_MAX_CPUID_ENTRIES).unwrap();

pub fn enable_cap(&self, cap: &kvm_enable_cap) -> Result<(), Error>[src]

See the documentation for KVM_ENABLE_CAP.

Arguments

  • kvm_enable_cap - KVM capability structure. For details check the kvm_enable_cap structure in the KVM API doc.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let mut cap: kvm_enable_cap = Default::default();
if cfg!(target_arch = "x86") || cfg!(target_arch = "x86_64") {
   // KVM_CAP_HYPERV_SYNIC needs KVM_CAP_SPLIT_IRQCHIP enabled
   cap.cap = KVM_CAP_SPLIT_IRQCHIP;
   cap.args[0] = 24;
   vm.enable_cap(&cap).unwrap();

   let vcpu = vm.create_vcpu(0).unwrap();
   if kvm.check_extension(Cap::HypervSynic) {
       let mut cap: kvm_enable_cap = Default::default();
       cap.cap = KVM_CAP_HYPERV_SYNIC;
       vcpu.enable_cap(&cap).unwrap();
   }
}

pub fn get_lapic(&self) -> Result<kvm_lapic_state, Error>[src]

Returns the state of the LAPIC (Local Advanced Programmable Interrupt Controller).

The state is returned in a kvm_lapic_state structure as defined in the KVM API doc. See the documentation for KVM_GET_LAPIC.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
// For `get_lapic` to work, you first need to create a IRQ chip before creating the vCPU.
vm.create_irq_chip().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let lapic = vcpu.get_lapic().unwrap();

pub fn set_lapic(&self, klapic: &kvm_lapic_state) -> Result<(), Error>[src]

Sets the state of the LAPIC (Local Advanced Programmable Interrupt Controller).

See the documentation for KVM_SET_LAPIC.

Arguments

  • klapic - LAPIC state. For details check the kvm_lapic_state structure in the KVM API doc.

Example

use std::io::Write;

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
// For `get_lapic` to work, you first need to create a IRQ chip before creating the vCPU.
vm.create_irq_chip().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mut lapic = vcpu.get_lapic().unwrap();

// Write to APIC_ICR offset the value 2.
let apic_icr_offset = 0x300;
let write_value: &[u8] = &[2, 0, 0, 0];
let mut apic_icr_slice =
    unsafe { &mut *(&mut lapic.regs[apic_icr_offset..] as *mut [i8] as *mut [u8]) };
apic_icr_slice.write(write_value).unwrap();

// Update the value of LAPIC.
vcpu.set_lapic(&lapic).unwrap();

pub fn get_msrs(&self, msrs: &mut Msrs) -> Result<usize, Error>[src]

Returns the model-specific registers (MSR) for this vCPU.

It emulates KVM_GET_MSRS ioctl’s behavior by returning the number of MSRs successfully read upon success or the last error number in case of failure. The MSRs are returned in the msr method argument.

Arguments

  • msrs - MSRs (input/output). For details check the kvm_msrs structure in the KVM API doc.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
// Configure the struct to say which entries we want to get.
let mut msrs = Msrs::from_entries(&[
    kvm_msr_entry {
        index: 0x0000_0174,
        ..Default::default()
    },
    kvm_msr_entry {
        index: 0x0000_0175,
        ..Default::default()
    },
]).unwrap();
let read = vcpu.get_msrs(&mut msrs).unwrap();
assert_eq!(read, 2);

pub fn set_msrs(&self, msrs: &Msrs) -> Result<usize, Error>[src]

Setup the model-specific registers (MSR) for this vCPU. Returns the number of MSR entries actually written.

See the documentation for KVM_SET_MSRS.

Arguments

  • msrs - MSRs. For details check the kvm_msrs structure in the KVM API doc.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();

// Configure the entries we want to set.
let mut msrs = Msrs::from_entries(&[
    kvm_msr_entry {
        index: 0x0000_0174,
        ..Default::default()
    },
]).unwrap();
let written = vcpu.set_msrs(&msrs).unwrap();
assert_eq!(written, 1);

pub fn get_mp_state(&self) -> Result<kvm_mp_state, Error>[src]

Returns the vcpu’s current “multiprocessing state”.

See the documentation for KVM_GET_MP_STATE in the KVM API doc.

Arguments

  • kvm_mp_state - multiprocessing state to be read.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mp_state = vcpu.get_mp_state().unwrap();

pub fn set_mp_state(&self, mp_state: kvm_mp_state) -> Result<(), Error>[src]

Sets the vcpu’s current “multiprocessing state”.

See the documentation for KVM_SET_MP_STATE in the KVM API doc.

Arguments

  • kvm_mp_state - multiprocessing state to be written.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mp_state = Default::default();
// Your `mp_state` manipulation here.
vcpu.set_mp_state(mp_state).unwrap();

pub fn get_xsave(&self) -> Result<kvm_xsave, Error>[src]

X86 specific call that returns the vcpu’s current “xsave struct”.

See the documentation for KVM_GET_XSAVE in the KVM API doc.

Arguments

  • kvm_xsave - xsave struct to be read.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let xsave = vcpu.get_xsave().unwrap();

pub fn set_xsave(&self, xsave: &kvm_xsave) -> Result<(), Error>[src]

X86 specific call that sets the vcpu’s current “xsave struct”.

See the documentation for KVM_SET_XSAVE in the KVM API doc.

Arguments

  • kvm_xsave - xsave struct to be written.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let xsave = Default::default();
// Your `xsave` manipulation here.
vcpu.set_xsave(&xsave).unwrap();

pub fn get_xcrs(&self) -> Result<kvm_xcrs, Error>[src]

X86 specific call that returns the vcpu’s current “xcrs”.

See the documentation for KVM_GET_XCRS in the KVM API doc.

Arguments

  • kvm_xcrs - xcrs to be read.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let xcrs = vcpu.get_xcrs().unwrap();

pub fn set_xcrs(&self, xcrs: &kvm_xcrs) -> Result<(), Error>[src]

X86 specific call that sets the vcpu’s current “xcrs”.

See the documentation for KVM_SET_XCRS in the KVM API doc.

Arguments

  • kvm_xcrs - xcrs to be written.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let xcrs = Default::default();
// Your `xcrs` manipulation here.
vcpu.set_xcrs(&xcrs).unwrap();

pub fn get_debug_regs(&self) -> Result<kvm_debugregs, Error>[src]

X86 specific call that returns the vcpu’s current “debug registers”.

See the documentation for KVM_GET_DEBUGREGS in the KVM API doc.

Arguments

  • kvm_debugregs - debug registers to be read.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let debug_regs = vcpu.get_debug_regs().unwrap();

pub fn set_debug_regs(&self, debug_regs: &kvm_debugregs) -> Result<(), Error>[src]

X86 specific call that sets the vcpu’s current “debug registers”.

See the documentation for KVM_SET_DEBUGREGS in the KVM API doc.

Arguments

  • kvm_debugregs - debug registers to be written.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let debug_regs = Default::default();
// Your `debug_regs` manipulation here.
vcpu.set_debug_regs(&debug_regs).unwrap();

pub fn get_vcpu_events(&self) -> Result<kvm_vcpu_events, Error>[src]

Returns currently pending exceptions, interrupts, and NMIs as well as related states of the vcpu.

See the documentation for KVM_GET_VCPU_EVENTS in the KVM API doc.

Arguments

  • kvm_vcpu_events - vcpu events to be read.

Example

let kvm = Kvm::new().unwrap();
if kvm.check_extension(Cap::VcpuEvents) {
    let vm = kvm.create_vm().unwrap();
    let vcpu = vm.create_vcpu(0).unwrap();
    let vcpu_events = vcpu.get_vcpu_events().unwrap();
}

pub fn set_vcpu_events(
    &self,
    vcpu_events: &kvm_vcpu_events
) -> Result<(), Error>
[src]

Sets pending exceptions, interrupts, and NMIs as well as related states of the vcpu.

See the documentation for KVM_SET_VCPU_EVENTS in the KVM API doc.

Arguments

  • kvm_vcpu_events - vcpu events to be written.

Example

let kvm = Kvm::new().unwrap();
if kvm.check_extension(Cap::VcpuEvents) {
    let vm = kvm.create_vm().unwrap();
    let vcpu = vm.create_vcpu(0).unwrap();
    let vcpu_events = Default::default();
    // Your `vcpu_events` manipulation here.
    vcpu.set_vcpu_events(&vcpu_events).unwrap();
}

pub fn set_guest_debug(
    &self,
    debug_struct: &kvm_guest_debug
) -> Result<(), Error>
[src]

Sets processor-specific debug registers and configures the vcpu for handling certain guest debug events using the KVM_SET_GUEST_DEBUG ioctl.

Arguments

  • debug_struct - control bitfields and debug registers, depending on the specific architecture. For details check the kvm_guest_debug structure in the KVM API doc.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
    let debug_struct = kvm_guest_debug {
        // Configure the vcpu so that a KVM_DEBUG_EXIT would be generated
        // when encountering a software breakpoint during execution
        control: KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP,
        pad: 0,
        // Reset all x86-specific debug registers
        arch: kvm_guest_debug_arch {
            debugreg: [0, 0, 0, 0, 0, 0, 0, 0],
        },
    };

    vcpu.set_guest_debug(&debug_struct).unwrap();
}

pub fn kvmclock_ctrl(&self) -> Result<(), Error>[src]

Notify the guest about the vCPU being paused.

See the documentation for KVM_KVMCLOCK_CTRL in the KVM API documentation.

pub fn run(&self) -> Result<VcpuExit<'_>, Error>[src]

Triggers the running of the current virtual CPU returning an exit reason.

See documentation for KVM_RUN.

Example

// This is a dummy example for running on x86 based on https://lwn.net/Articles/658511/.
#[cfg(target_arch = "x86_64")] {
    let mem_size = 0x4000;
    let guest_addr: u64 = 0x1000;
    let load_addr: *mut u8 = unsafe {
        libc::mmap(
            null_mut(),
            mem_size,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_ANONYMOUS | libc::MAP_SHARED | libc::MAP_NORESERVE,
            -1,
            0,
        ) as *mut u8
    };

    let mem_region = kvm_userspace_memory_region {
        slot: 0,
        guest_phys_addr: guest_addr,
        memory_size: mem_size as u64,
        userspace_addr: load_addr as u64,
        flags: 0,
    };
    unsafe { vm.set_user_memory_region(mem_region).unwrap() };

    // Dummy x86 code that just calls halt.
    let x86_code = [
            0xf4,             /* hlt */
    ];

    // Write the code in the guest memory. This will generate a dirty page.
    unsafe {
        let mut slice = slice::from_raw_parts_mut(load_addr, mem_size);
        slice.write(&x86_code).unwrap();
    }

    let vcpu_fd = vm.create_vcpu(0).unwrap();

    let mut vcpu_sregs = vcpu_fd.get_sregs().unwrap();
    vcpu_sregs.cs.base = 0;
    vcpu_sregs.cs.selector = 0;
    vcpu_fd.set_sregs(&vcpu_sregs).unwrap();

    let mut vcpu_regs = vcpu_fd.get_regs().unwrap();
    // Set the Instruction Pointer to the guest address where we loaded the code.
    vcpu_regs.rip = guest_addr;
    vcpu_regs.rax = 2;
    vcpu_regs.rbx = 3;
    vcpu_regs.rflags = 2;
    vcpu_fd.set_regs(&vcpu_regs).unwrap();

    loop {
        match vcpu_fd.run().expect("run failed") {
            VcpuExit::Hlt => {
                break;
            }
            exit_reason => panic!("unexpected exit reason: {:?}", exit_reason),
        }
    }
}

pub fn set_kvm_immediate_exit(&self, val: u8)[src]

Sets the immediate_exit flag on the kvm_run struct associated with this vCPU to val.

pub fn get_tsc_khz(&self) -> Result<u32, Error>[src]

Returns the vCPU TSC frequency in KHz or an error if the host has unstable TSC.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let tsc_khz = vcpu.get_tsc_khz().unwrap();

pub fn set_tsc_khz(&self, freq: u32) -> Result<(), Error>[src]

Sets the specified vCPU TSC frequency.

Arguments

  • freq - The frequency unit is KHz as per the the KVM API documentation for KVM_SET_TSC_KHZ.

Example

let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
vcpu.set_tsc_khz(1000).unwrap();

Trait Implementations

impl AsRawFd for VcpuFd[src]

fn as_raw_fd(&self) -> RawFd[src]

Extracts the raw file descriptor. Read more

Auto Trait Implementations

impl RefUnwindSafe for VcpuFd

impl Send for VcpuFd

impl Sync for VcpuFd

impl Unpin for VcpuFd

impl UnwindSafe for VcpuFd

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.