Skip to content

Commit 6cca839

Browse files
committed
[refactor] remove redundant eptp related apis
1 parent a324985 commit 6cca839

File tree

3 files changed

+10
-79
lines changed

3 files changed

+10
-79
lines changed

src/vmx/structs.rs

-31
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,6 @@ impl EPTPointer {
271271
}
272272
}
273273

274-
pub const EPTP_LIST_SIZE: usize = 512;
275-
276274
/// EPTP list, the 4-KByte structure,
277275
/// The EPTP list comprises 512 8-Byte entries (each an EPTP value)
278276
/// and is used by the EPTP-switching VM function (see Section 26.5.6.3).
@@ -290,33 +288,4 @@ impl<H: AxVCpuHal> EptpList<H> {
290288
pub fn phys_addr(&self) -> HostPhysAddr {
291289
self.frame.start_paddr()
292290
}
293-
294-
pub fn set_entry(&mut self, idx: usize, eptp: EPTPointer) {
295-
assert!(idx < EPTP_LIST_SIZE);
296-
// Todo: validate eptp refer to 26.5.6.3 EPTP Switching.
297-
let ptr = self.frame.as_mut_ptr() as *mut u64;
298-
unsafe {
299-
ptr.add(idx).write(eptp.bits());
300-
}
301-
}
302-
303-
pub fn entry_is_set(&self, idx: usize) -> bool {
304-
assert!(idx < EPTP_LIST_SIZE);
305-
let ptr = self.frame.as_mut_ptr() as *const u64;
306-
unsafe { ptr.add(idx).read() != 0 }
307-
}
308-
309-
pub fn get_entry(&self, idx: usize) -> EPTPointer {
310-
assert!(idx < EPTP_LIST_SIZE);
311-
let ptr = self.frame.as_mut_ptr() as *const u64;
312-
unsafe { EPTPointer::from_bits_truncate(ptr.add(idx).read()) }
313-
}
314-
315-
pub fn remove_entry(&mut self, idx: usize) {
316-
assert!(idx < EPTP_LIST_SIZE);
317-
let ptr = self.frame.as_mut_ptr() as *mut u64;
318-
unsafe {
319-
ptr.add(idx).write(0);
320-
}
321-
}
322291
}

src/vmx/vcpu.rs

+6-48
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use super::VmxExitInfo;
2424
use super::as_axerr;
2525
use super::definitions::VmxExitReason;
2626
use super::read_vmcs_revision_id;
27-
use super::structs::{EPTP_LIST_SIZE, EPTPointer, EptpList, IOBitmap, MsrBitmap, VmxRegion};
27+
use super::structs::{EptpList, IOBitmap, MsrBitmap, VmxRegion};
2828
use super::vmcs::{
2929
self, VmcsControl32, VmcsControl64, VmcsControlNW, VmcsGuest16, VmcsGuest32, VmcsGuest64,
3030
VmcsGuestNW, VmcsHost16, VmcsHost32, VmcsHost64, VmcsHostNW, interrupt_exit_info,
@@ -755,11 +755,6 @@ impl<H: AxVCpuHal> VmxVcpu<H> {
755755
// Bit 0: EPTP switching
756756
VmcsControl64::VM_FUNCTION_CONTROLS.write(0b1)?;
757757

758-
assert!(
759-
self.eptp_list.entry_is_set(0),
760-
"The First EPTP list entry must be set as initial EPTP."
761-
);
762-
763758
VmcsControl64::EPTP_LIST_ADDR.write(self.eptp_list.phys_addr().as_usize() as _)?;
764759

765760
// Pass-through exceptions (except #UD(6)), don't use I/O bitmap, set MSR bitmaps.
@@ -1369,7 +1364,6 @@ impl<H: AxVCpuHal> AxArchVCpu for VmxVcpu<H> {
13691364

13701365
fn set_ept_root(&mut self, ept_root: HostPhysAddr) -> AxResult {
13711366
self.ept_root = Some(ept_root);
1372-
self.append_eptp_list(0, ept_root)?;
13731367
Ok(())
13741368
}
13751369

@@ -1383,10 +1377,6 @@ impl<H: AxVCpuHal> AxArchVCpu for VmxVcpu<H> {
13831377
}
13841378

13851379
fn run(&mut self) -> AxResult<AxVCpuExitReason> {
1386-
if self.id == 3 {
1387-
warn!("Instance vcpu run {:#x?}", self);
1388-
}
1389-
13901380
match self.inner_run() {
13911381
Some(exit_info) => Ok(if exit_info.entry_failure {
13921382
match exit_info.exit_reason {
@@ -1399,6 +1389,7 @@ impl<H: AxVCpuHal> AxArchVCpu for VmxVcpu<H> {
13991389
};
14001390

14011391
warn!("VMX entry failure: {:#x?}", exit_info);
1392+
warn!("VCpu {:#x?}", self);
14021393

14031394
AxVCpuExitReason::FailEntry {
14041395
// Todo: get `hardware_entry_failure_reason` somehow.
@@ -1576,44 +1567,11 @@ impl<H: AxVCpuHal> AxVcpuAccessGuestState for VmxVcpu<H> {
15761567
self.guest_page_table_query(gva).ok()
15771568
}
15781569

1579-
fn append_eptp_list(&mut self, idx: usize, eptp: HostPhysAddr) -> AxResult {
1580-
if idx >= EPTP_LIST_SIZE {
1581-
return ax_err!(InvalidInput);
1582-
}
1583-
1584-
if self.eptp_list.entry_is_set(idx) {
1585-
return ax_err!(InvalidInput);
1586-
}
1587-
1588-
self.eptp_list
1589-
.set_entry(idx, EPTPointer::from_table_phys(eptp));
1590-
Ok(())
1570+
fn current_ept_root(&self) -> HostPhysAddr {
1571+
vmcs::get_ept_pointer()
15911572
}
15921573

1593-
fn remove_eptp_list_entry(&mut self, idx: usize) -> AxResult {
1594-
if idx >= EPTP_LIST_SIZE {
1595-
return ax_err!(InvalidInput);
1596-
}
1597-
if !self.eptp_list.entry_is_set(idx) {
1598-
return ax_err!(InvalidInput);
1599-
}
1600-
1601-
self.eptp_list.remove_entry(idx);
1602-
1603-
Ok(())
1604-
}
1605-
1606-
fn get_eptp_list_entry(&self, idx: usize) -> AxResult<HostPhysAddr> {
1607-
if idx >= EPTP_LIST_SIZE {
1608-
return ax_err!(InvalidInput);
1609-
}
1610-
if !self.eptp_list.entry_is_set(idx) {
1611-
return ax_err!(InvalidInput);
1612-
}
1613-
let entry = self.eptp_list.get_entry(idx);
1614-
1615-
Ok(HostPhysAddr::from_usize(memory_addr::align_down_4k(
1616-
entry.bits() as _,
1617-
)))
1574+
fn eptp_list_region(&self) -> HostPhysAddr {
1575+
self.eptp_list.phys_addr()
16181576
}
16191577
}

src/vmx/vmcs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,10 @@ pub fn set_ept_pointer(pml4_paddr: HostPhysAddr) -> AxResult {
638638
Ok(())
639639
}
640640

641+
pub fn get_ept_pointer() -> HostPhysAddr {
642+
HostPhysAddr::from(VmcsControl64::EPTP.read().expect("Failed to read EPTP") as usize)
643+
}
644+
641645
pub fn instruction_error() -> VmxInstructionError {
642646
VmcsReadOnly32::VM_INSTRUCTION_ERROR.read().unwrap().into()
643647
}

0 commit comments

Comments
 (0)