Skip to main content

SvmVcpu

Struct SvmVcpu 

Source
pub struct SvmVcpu {
Show 15 fields vmcb_frame: PhysFrame, msrpm: PhysFrame, iopm: PhysFrame, regs: GuestGprs, guest: GuestState, fpu: FpuSwitch, guest_dr: [u64; 4], seen_gen: u64, msr: MsrModel, ncr3: u64, asid: u32, flush_by_asid: bool, last_cpu: Option<usize>, exit: Option<SvmExit>, entered: bool,
}
Expand description

An AMD-V virtual CPU.

Fields§

§vmcb_frame: PhysFrame§msrpm: PhysFrame§iopm: PhysFrame§regs: GuestGprs§guest: GuestState§fpu: FpuSwitch§guest_dr: [u64; 4]

Guest DR0-DR3, switched around every entry.

§seen_gen: u64

Flush generation of the ASID this vCPU has already honoured.

§msr: MsrModel

Per-vCPU shadow of the modelled MSRs (see Self::sync_msr_state).

§ncr3: u64

Root of the nested page table (nCR3).

§asid: u32§flush_by_asid: bool§last_cpu: Option<usize>

CPU of the previous entry; a change (or none) forces a TLB flush.

§exit: Option<SvmExit>§entered: bool

Implementations§

Source§

impl SvmVcpu

Source

pub fn new(ncr3: u64, asid: u32) -> Result<Self, SvmVcpuError>

Creates a vCPU backed by the nested page table rooted at ncr3.

asid is the guest’s TLB tag: non-zero (0 is the host’s) and below the CPU’s ASID count, otherwise SvmVcpuError::InvalidAsid. It must be unique among the virtual machines that may run on the same CPU. vCPUs of one machine may share it together with the NPT: the entry path flushes whenever a different VMCB was the last one run on the CPU.

Source

fn vmcb_phys(&self) -> u64

Source

fn vmcb(&self) -> &Vmcb

Source

fn vmcb_mut(&mut self) -> &mut Vmcb

Source

pub fn configure(&mut self, guest: &GuestState)

Loads a complete guest state: segments, control registers, rip, and the general registers.

Source

pub fn nested_cr3(&self) -> u64

Nested page table root (nCR3) of this vCPU.

Source

pub fn paging_enabled(&self) -> bool

True if the guest runs with paging enabled.

Source

pub fn msr_mut(&mut self) -> &mut MsrModel

Per-vCPU MSR shadow state.

Source

pub fn interruptibility(&self) -> u32

Interrupt-shadow state in the VT-x interruptibility encoding (bit 0 = blocking by STI / MOV SS).

Source

pub fn sync_msr_state(&mut self)

Pushes the MSR shadows into the VMCB.

Must be called after a replayed wrmsr whose effect is Efer, Pat, SyscallTargets, SegmentBase or State, before the next entry. Unlike VT-x, the syscall targets and KERNEL_GS_BASE are real VMCB state here: the guest’s syscall/swapgs use them natively.

Source

pub fn diagnostics(&self) -> String

One-line dump of the guest state, for logs.

Source

pub fn guest(&self) -> &GuestState

The guest state as of the last exit.

Source

pub fn asid(&self) -> u32

ASID of this vCPU.

Source

pub fn entered(&self) -> bool

True once the guest has been entered at least once.

Source

pub fn last_exit(&self) -> Option<SvmExit>

Last observed exit.

Source

pub unsafe fn run(&mut self) -> Result<SvmExit, SvmVcpuError>

Enters the guest and returns on the next #VMEXIT.

Interrupts are disabled for the whole entry, so the vCPU cannot migrate between CPUs mid-sequence. A host interrupt that arrives during the guest run causes an INTR exit and is taken once interrupts are re-enabled when this function returns.

§Safety

SVM must be activated on the current CPU (svm::activate_current_cpu), and the NPT root given to Self::new must stay valid.

Source

unsafe fn run_irqs_off(&mut self) -> Result<SvmExit, SvmVcpuError>

§Safety

As Self::run, with local interrupts disabled.

Source

fn read_exit(&mut self) -> SvmExit

Source

pub fn guest_gpr(&self, register: u8) -> u64

Reads a guest general-purpose register (x86 encoding order: 0 = RAX, 1 = RCX, 2 = RDX, 3 = RBX, 4 = RSP, 5 = RBP, 6 = RSI, 7 = RDI, then 8..=15 = R8..R15). Out-of-range indices read 0.

Source

pub fn set_guest_gpr(&mut self, register: u8, value: u64)

Writes a guest general-purpose register (encoding order, see Self::guest_gpr). Out-of-range indices are ignored.

Source

pub fn set_guest_rip(&mut self, rip: u64)

Sets the guest instruction pointer.

Source

pub fn advance_rip(&mut self, bytes: u64)

Advances the guest rip past the instruction that caused an exit.

Source

pub fn advance_rip_from_exit(&mut self) -> bool

Advances rip using the CPU-reported NEXT_RIP of the last exit. Returns false when the CPU did not provide it (no NRIP_SAVE): the caller must then decode the instruction length itself.

Source

pub fn request_tlb_flush(&self)

Invalidates the guest TLB translations of this vCPU’s ASID on every CPU.

Call it after changing or removing a mapping of the NPT. The flush generation of the ASID is bumped, so every vCPU sharing it flushes at its next entry; CPUs currently running a guest with this ASID are kicked with a reschedule IPI so they exit (the host interrupt makes the guest leave with an INTR exit) and re-enter through that path.

Source

fn event_pending(&self) -> bool

True if an event is already queued for the next entry.

Source

pub fn inject_interrupt(&mut self, vector: u8) -> Result<(), SvmVcpuError>

Queues an external interrupt for the next entry.

Rejected when the guest cannot take it now (IF clear, interrupt shadow) or when another event is already queued; the caller should then arm Self::set_interrupt_window_exiting.

Source

pub fn inject_nmi(&mut self) -> Result<(), SvmVcpuError>

Queues an NMI for the next entry.

Source

pub fn inject_exception( &mut self, vector: u8, error_code: Option<u32>, ) -> Result<(), SvmVcpuError>

Queues a hardware exception (with or without error code).

Source

pub fn set_interrupt_window_exiting(&mut self, enabled: bool)

Arms or disarms the interrupt-window exit: the hardware exits (class ExitClass::InterruptWindow) as soon as the guest can take an interrupt. One-shot: it disarms itself when it fires.

Source

pub fn interrupt_window_armed(&self) -> bool

True if the interrupt-window exit is armed.

Source

pub fn guest_xcr0(&self) -> u64

XCR0 as seen by the guest.

Source

pub fn emulate_xsetbv(&mut self) -> Result<(), SvmVcpuError>

Emulates the guest xsetbv that caused the last exit, then advances rip (xsetbv is always 3 bytes).

Returns SvmVcpuError::InvalidXcr0 without touching any state when the instruction must fault (wrong XCR index, CPL != 0, invalid value): the caller injects #GP(0).

Trait Implementations§

Source§

impl Drop for SvmVcpu

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.