Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make some host state related operations as percpu operation #5

Merged
merged 3 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub trait PhysFrameIf {
/// automatically on drop.
#[derive(Debug)]
pub struct PhysFrame {
start_paddr: HostPhysAddr,
start_paddr: Option<HostPhysAddr>,
}

impl PhysFrame {
Expand All @@ -30,7 +30,9 @@ impl PhysFrame {
.ok_or_else(|| ax_err_type!(NoMemory, "allocate physical frame failed"))?;
assert_ne!(start_paddr.as_usize(), 0);
debug!("[AxVM] allocated PhysFrame({:#x})", start_paddr);
Ok(Self { start_paddr })
Ok(Self {
start_paddr: Some(start_paddr),
})
}

pub fn alloc_zero() -> AxResult<Self> {
Expand All @@ -40,17 +42,15 @@ impl PhysFrame {
}

pub const unsafe fn uninit() -> Self {
Self {
start_paddr: PhysAddr::from(0xdead_beef),
}
Self { start_paddr: None }
}

pub fn start_paddr(&self) -> HostPhysAddr {
self.start_paddr
self.start_paddr.expect("uninitialized PhysFrame")
}

pub fn as_mut_ptr(&self) -> *mut u8 {
crate_interface::call_interface!(PhysFrameIf::phys_to_virt(self.start_paddr)).as_mut_ptr()
crate_interface::call_interface!(PhysFrameIf::phys_to_virt(self.start_paddr())).as_mut_ptr()
}

pub fn fill(&mut self, byte: u8) {
Expand All @@ -60,9 +60,9 @@ impl PhysFrame {

impl Drop for PhysFrame {
fn drop(&mut self) {
if self.start_paddr.as_usize() > 0 {
crate_interface::call_interface!(PhysFrameIf::dealloc_frame(self.start_paddr));
debug!("[AxVM] deallocated PhysFrame({:#x})", self.start_paddr);
if let Some(start_paddr) = self.start_paddr {
crate_interface::call_interface!(PhysFrameIf::dealloc_frame(start_paddr));
debug!("[AxVM] deallocated PhysFrame({:#x})", start_paddr);
}
}
}
3 changes: 3 additions & 0 deletions src/vmx/percpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ impl AxArchPerCpu for VmxPerCpuState {
return ax_err!(ResourceBusy, "VMX is already turned on");
}

// Enable XSAVE/XRSTOR.
super::vcpu::XState::enable_xsave();

// Enable VMXON, if required.
let ctrl = FeatureControl::read();
let locked = ctrl.contains(FeatureControlFlags::LOCKED);
Expand Down
8 changes: 4 additions & 4 deletions src/vmx/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ impl XState {
}
}

fn enable_xsave() {
/// Enables extended processor state management instructions, including XGETBV and XSAVE.
pub fn enable_xsave() {
unsafe { Cr4::write(Cr4::read() | Cr4Flags::OSXSAVE) };
}
}
Expand Down Expand Up @@ -84,7 +85,6 @@ impl VmxVcpu {
/// Create a new [`VmxVcpu`].
pub fn new() -> AxResult<Self> {
let vmcs_revision_id = super::read_vmcs_revision_id();
XState::enable_xsave();
let vcpu = Self {
guest_regs: GeneralRegisters::default(),
host_stack_top: 0,
Expand Down Expand Up @@ -122,6 +122,7 @@ impl VmxVcpu {
unsafe {
vmx::vmptrld(self.vmcs.phys_addr().as_usize() as u64).map_err(as_axerr)?;
}
self.setup_vmcs_host()?;
Ok(())
}

Expand Down Expand Up @@ -422,14 +423,13 @@ impl VmxVcpu {
vmx::vmclear(paddr).map_err(as_axerr)?;
}
self.bind_to_current_processor()?;
self.setup_vmcs_host()?;
self.setup_vmcs_guest(entry)?;
self.setup_vmcs_control(ept_root, true)?;
self.unbind_from_current_processor()?;
Ok(())
}

fn setup_vmcs_host(&mut self) -> AxResult {
fn setup_vmcs_host(&self) -> AxResult {
VmcsHost64::IA32_PAT.write(Msr::IA32_PAT.read())?;
VmcsHost64::IA32_EFER.write(Msr::IA32_EFER.read())?;

Expand Down