Skip to content

Commit 8b8705b

Browse files
authored
Vpid & pat & mtrr
2 parents 1e37648 + 1f6f510 commit 8b8705b

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl LinuxContext {
161161
/// The initialized context of the instance's first process.
162162
/// It is used to initialize the vCPU context for the shim kernel which enters
163163
/// under Intel's long mode.
164-
pub fn construct_guest64(rip: u64, cr3: u64, cr4: Cr4Flags) -> Self {
164+
pub fn construct_guest64(rip: u64, cr3: u64, cr4: Cr4Flags, linux: &LinuxContext) -> Self {
165165
Self {
166166
rsp: 0,
167167
rip,
@@ -238,8 +238,8 @@ impl LinuxContext {
238238
ia32_sysenter_esp: 0,
239239
ia32_sysenter_eip: 0,
240240
kernel_gsbase: 0,
241-
pat: 0,
242-
mtrr_def_type: 0,
241+
pat: linux.pat, // Reuse PAT value provided by Linux.
242+
mtrr_def_type: linux.mtrr_def_type, // Reuse MTRR value provided by Linux.
243243
}
244244
}
245245

src/vmx/vcpu.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use super::definitions::VmxExitReason;
2727
use super::read_vmcs_revision_id;
2828
use super::structs::{EptpList, IOBitmap, MsrBitmap, VmxRegion};
2929
use super::vmcs::{
30-
self, VmcsControl32, VmcsControl64, VmcsControlNW, VmcsGuest16, VmcsGuest32, VmcsGuest64,
31-
VmcsGuestNW, VmcsHost16, VmcsHost32, VmcsHost64, VmcsHostNW, exit_qualification,
30+
self, VmcsControl16, VmcsControl32, VmcsControl64, VmcsControlNW, VmcsGuest16, VmcsGuest32,
31+
VmcsGuest64, VmcsGuestNW, VmcsHost16, VmcsHost32, VmcsHost64, VmcsHostNW, exit_qualification,
3232
interrupt_exit_info,
3333
};
3434
use crate::LinuxContext;
@@ -476,6 +476,7 @@ impl<H: AxVCpuHal> VmxVcpu<H> {
476476
}
477477

478478
self.setup_vmcs_control(ept_root, is_guest)?;
479+
self.set_vpid(self.id as u16 + 1)?;
479480
self.unbind_from_current_processor()?;
480481
Ok(())
481482
}
@@ -699,6 +700,9 @@ impl<H: AxVCpuHal> VmxVcpu<H> {
699700
}
700701
}
701702

703+
// Enable VPID to speed up `VMFUNC` operation.
704+
val |= CpuCtrl2::ENABLE_VPID;
705+
702706
vmcs::set_control(
703707
VmcsControl32::SECONDARY_PROCBASED_EXEC_CONTROLS,
704708
Msr::IA32_VMX_PROCBASED_CTLS2,
@@ -770,6 +774,14 @@ impl<H: AxVCpuHal> VmxVcpu<H> {
770774
Ok(())
771775
}
772776

777+
fn set_vpid(&mut self, vpid: u16) -> AxResult {
778+
if vpid == 0 || vpid > 0xfffe {
779+
return ax_err!(InvalidInput);
780+
}
781+
VmcsControl16::VPID.write(vpid)?;
782+
Ok(())
783+
}
784+
773785
fn load_vmcs_guest(&self, linux: &mut LinuxContext) -> AxResult {
774786
linux.rip = VmcsGuestNW::RIP.read()? as _;
775787
linux.rsp = VmcsGuestNW::RSP.read()? as _;

0 commit comments

Comments
 (0)