Skip to main content

AxVCpu

Struct AxVCpu 

Source
pub struct AxVCpu<A: AxArchVCpu> { /* private fields */ }
Expand description

Architecture-independent virtual CPU implementation.

This is the main VCpu abstraction that provides a unified interface for managing virtual CPUs across different architectures. It delegates architecture-specific operations to implementations of the AxArchVCpu trait.

Note that:

  • This struct handles internal mutability itself, almost all the methods are &self.
  • This struct is not thread-safe. It’s caller’s responsibility to ensure the safety.

Implementations§

Source§

impl<A: AxArchVCpu> AxVCpu<A>

Source

pub fn new( vm_id: VMId, vcpu_id: VCpuId, favor_phys_cpu: usize, phys_cpu_set: Option<usize>, arch_config: A::CreateConfig, ) -> AxResult<Self>

Creates a new virtual CPU instance.

Initializes a VCpu with the given configuration and creates the underlying architecture-specific implementation. The VCpu starts in the Created state.

§Arguments
  • vm_id - Unique identifier of the VM this VCpu belongs to
  • vcpu_id - Unique identifier for this VCpu within the VM
  • favor_phys_cpu - Physical CPU ID that should preferentially run this VCpu
  • phys_cpu_set - Optional bitmask of allowed physical CPUs (None = no restriction)
  • arch_config - Architecture-specific configuration for VCpu creation
§Returns

Returns Ok(AxVCpu) on success, or an error if architecture-specific creation fails.

Source

pub fn setup( &self, entry: GuestPhysAddr, ept_root: HostPhysAddr, arch_config: A::SetupConfig, ) -> AxResult

Sets up the VCpu for execution.

Configures the VCpu’s entry point, memory management (EPT root), and any architecture-specific setup. Transitions the VCpu from Created to Free state.

Source

pub const fn id(&self) -> VCpuId

Returns the unique identifier of this VCpu.

Source

pub const fn vm_id(&self) -> VMId

Get the id of the VM this vcpu belongs to.

Source

pub const fn favor_phys_cpu(&self) -> usize

Returns the preferred physical CPU for this VCpu.

This is used for CPU affinity optimization - the scheduler should preferentially run this VCpu on the returned physical CPU ID.

§Note

Currently unused in the implementation but reserved for future scheduler optimizations.

Source

pub const fn phys_cpu_set(&self) -> Option<usize>

Returns the set of physical CPUs that can run this VCpu.

Source

pub const fn is_bsp(&self) -> bool

Checks if this VCpu is the Bootstrap Processor (BSP).

By convention, the VCpu with ID 0 is always considered the BSP, which is responsible for system initialization in multi-core VMs.

Source

pub fn state(&self) -> VCpuState

Gets the current execution state of the VCpu.

Source

pub unsafe fn set_state(&self, state: VCpuState)

Set the state of the VCpu.

§Safety

This method is unsafe because it may break the state transition model. Use it with caution.

Source

pub fn with_state_transition<F, T>( &self, from: VCpuState, to: VCpuState, f: F, ) -> AxResult<T>
where F: FnOnce() -> AxResult<T>,

Execute a block with the state of the VCpu transitioned from from to to. If the current state is not from, return an error.

The state will be set to VCpuState::Invalid if an error occurs (including the case that the current state is not from).

The state will be set to to if the block is executed successfully.

Source

pub fn with_current_cpu_set<F, T>(&self, f: F) -> T
where F: FnOnce() -> T,

Execute a block with the current VCpu set to &self.

Source

pub fn manipulate_arch_vcpu<F, T>( &self, from: VCpuState, to: VCpuState, f: F, ) -> AxResult<T>
where F: FnOnce(&mut A) -> AxResult<T>,

Execute an operation on the architecture-specific VCpu, with the state transitioned from from to to and the current VCpu set to &self.

This method is a combination of AxVCpu::with_state_transition and AxVCpu::with_current_cpu_set.

Source

pub fn transition_state(&self, from: VCpuState, to: VCpuState) -> AxResult

Transition the state of the VCpu. If the current state is not from, return an error.

Source

pub fn get_arch_vcpu(&self) -> &mut A

Get the architecture-specific VCpu.

Source

pub fn run(&self) -> AxResult<AxVCpuExitReason>

Run the VCpu.

Source

pub fn bind(&self) -> AxResult

Bind the VCpu to the current physical CPU.

Source

pub fn unbind(&self) -> AxResult

Unbind the VCpu from the current physical CPU.

Source

pub fn set_entry(&self, entry: GuestPhysAddr) -> AxResult

Sets the entry address of the VCpu.

Source

pub fn set_gpr(&self, reg: usize, val: usize)

Sets the value of a general-purpose register according to the given index.

Source

pub fn inject_interrupt(&self, vector: usize) -> AxResult

Inject an interrupt to the VCpu.

Source

pub fn set_return_value(&self, val: usize)

Sets the return value of the VCpu.

Auto Trait Implementations§

§

impl<A> !Freeze for AxVCpu<A>

§

impl<A> !RefUnwindSafe for AxVCpu<A>

§

impl<A> Send for AxVCpu<A>
where A: Send,

§

impl<A> !Sync for AxVCpu<A>

§

impl<A> Unpin for AxVCpu<A>
where A: Unpin,

§

impl<A> UnsafeUnpin for AxVCpu<A>
where A: UnsafeUnpin,

§

impl<A> UnwindSafe for AxVCpu<A>
where A: UnwindSafe,

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
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 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.