Skip to content

Commit

Permalink
use caps to set configurable bits, native or not
Browse files Browse the repository at this point in the history
Signed-off-by: Jake Correnti <[email protected]>
  • Loading branch information
jakecorrenti committed Mar 21, 2024
1 parent 2fc3bbd commit bd23e7b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
34 changes: 25 additions & 9 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,52 @@ impl TdxVm {
})
}

pub fn init_vm(&self, kvm_fd: &Kvm, xfam: &Xfam) -> Result<(), TdxError> {
pub fn init_vm(&self, kvm_fd: &Kvm, caps: &TdxCapabilities) -> Result<(), TdxError> {
let cpuid = kvm_fd
.get_supported_cpuid(kvm_bindings::KVM_MAX_CPUID_ENTRIES)
.unwrap();
let mut cpuid_entries: Vec<kvm_bindings::kvm_cpuid_entry2> =
cpuid.as_slice().iter().map(|e| (*e).into()).collect();

cpuid_entries.resize(256, kvm_bindings::kvm_cpuid_entry2::default());

// hex for Ob1100000001011111111 based on the XSAVE state-components architecture
let xcr0_mask = 0x602ff;
let xss_mask = !xcr0_mask;

let xfam_fixed0 = caps.xfam.fixed0.bits();
let xfam_fixed1 = caps.xfam.fixed1.bits();

for entry in cpuid_entries.as_mut_slice() {
for cpuid_config in &caps.cpuid_configs {
// 0xffffffff means the cpuid leaf has no subleaf
if cpuid_config.leaf == entry.function
&& (cpuid_config.sub_leaf == 0xffffffff || cpuid_config.sub_leaf == entry.index)
{
entry.eax |= cpuid_config.eax;
entry.ebx |= cpuid_config.ebx;
entry.ecx |= cpuid_config.ecx;
entry.edx |= cpuid_config.edx;
}
}

match entry.index {
// XSAVE features and state-components
0xD => {
if entry.index == 0 {
// XSAVE XCR0 LO
entry.eax &= (xfam.fixed0.bits() as u32) & (xcr0_mask as u32);
entry.eax |= (xfam.fixed1.bits() as u32) & (xcr0_mask as u32);
entry.eax &= (xfam_fixed0 as u32) & (xcr0_mask as u32);
entry.eax |= (xfam_fixed1 as u32) & (xcr0_mask as u32);
// XSAVE XCR0 HI
entry.edx &= ((xfam.fixed0.bits() & xcr0_mask) >> 32) as u32;
entry.edx |= ((xfam.fixed1.bits() & xcr0_mask) >> 32) as u32;
entry.edx &= ((xfam_fixed0 & xcr0_mask) >> 32) as u32;
entry.edx |= ((xfam_fixed1 & xcr0_mask) >> 32) as u32;
} else if entry.index == 1 {
// XSAVE XCR0 LO
entry.ecx &= (xfam.fixed0.bits() as u32) & (xss_mask as u32);
entry.ecx |= (xfam.fixed1.bits() as u32) & (xss_mask as u32);
entry.ecx &= (xfam_fixed0 as u32) & (xss_mask as u32);
entry.ecx |= (xfam_fixed1 as u32) & (xss_mask as u32);
// XSAVE XCR0 HI
entry.edx &= ((xfam.fixed0.bits() & xss_mask) >> 32) as u32;
entry.edx |= ((xfam.fixed1.bits() & xss_mask) >> 32) as u32;
entry.edx &= ((xfam_fixed0 & xss_mask) >> 32) as u32;
entry.edx |= ((xfam_fixed1 & xss_mask) >> 32) as u32;
}
}
0x8000_0008 => {
Expand Down
2 changes: 1 addition & 1 deletion tests/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ fn launch() {
let kvm_fd = Kvm::new().unwrap();
let tdx_vm = TdxVm::new(&kvm_fd).unwrap();
let caps = tdx_vm.get_capabilities().unwrap();
let _ = tdx_vm.init_vm(&kvm_fd, &caps.xfam).unwrap();
let _ = tdx_vm.init_vm(&kvm_fd, &caps).unwrap();
}

0 comments on commit bd23e7b

Please sign in to comment.