Skip to content

Commit

Permalink
Merge pull request #72 from Freax13/feature/intel-compat
Browse files Browse the repository at this point in the history
intel compat
  • Loading branch information
Freax13 authored Sep 22, 2024
2 parents 4fad16f + 8289bba commit 737d068
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
3 changes: 1 addition & 2 deletions host/mushroom/src/insecure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ fn run_kernel_vcpu(id: u8, vm: Arc<VmHandle>, cpuid_entries: Arc<[KvmCpuidEntry2
sregs.efer = EferFlags::SYSTEM_CALL_EXTENSIONS.bits()
| EferFlags::LONG_MODE_ENABLE.bits()
| EferFlags::LONG_MODE_ACTIVE.bits()
| EferFlags::NO_EXECUTE_ENABLE.bits()
| EferFlags::SECURE_VIRTUAL_MACHINE_ENABLE.bits();
| EferFlags::NO_EXECUTE_ENABLE.bits();
sregs.cr4 = Cr4Flags::PHYSICAL_ADDRESS_EXTENSION.bits()
| Cr4Flags::PAGE_GLOBAL.bits()
| Cr4Flags::OSFXSR.bits()
Expand Down
2 changes: 1 addition & 1 deletion host/mushroom/src/kvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ impl KvmSegment {
db: 0,
s: 1,
l: 1,
g: 0,
g: 1,
avl: 0,
unusable: 0,
_padding: 0,
Expand Down
47 changes: 38 additions & 9 deletions tee/kernel/src/memory/invlpgb.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use core::{arch::asm, iter::Step, ops::RangeInclusive};
use core::{
arch::{asm, x86_64::__cpuid},
iter::Step,
ops::RangeInclusive,
};

use bit_field::BitField;
use bitflags::bitflags;
Expand Down Expand Up @@ -88,6 +92,20 @@ impl InvlpgbCompat {
}
}

enum Hypercall {
Vmmcall,
Vmcall,
}

static HYPERCALL: Lazy<Hypercall> = Lazy::new(|| {
let svm = unsafe { __cpuid(0x8000_0001) }.ecx.get_bit(2);
if svm {
Hypercall::Vmmcall
} else {
Hypercall::Vmcall
}
});

fn hv_flush_all() {
const HV_X64_MSR_GUEST_OS_ID: u32 = 0x40000000;
unsafe {
Expand All @@ -103,14 +121,25 @@ fn hv_flush_all() {

let result: u64;

unsafe {
asm! {
"vpxor xmm0, xmm0, xmm0",
"vmmcall",
in("rcx") hypercall_input,
in("rdx") flags.bits(),
inout("rax") 0x5a5a5a5a5a5a5a5au64 => result,
};
match *HYPERCALL {
Hypercall::Vmmcall => unsafe {
asm! {
"vpxor xmm0, xmm0, xmm0",
"vmmcall",
in("rcx") hypercall_input,
in("rdx") flags.bits(),
inout("rax") 0x5a5a5a5a5a5a5a5au64 => result,
};
},
Hypercall::Vmcall => unsafe {
asm! {
"vpxor xmm0, xmm0, xmm0",
"vmcall",
in("rcx") hypercall_input,
in("rdx") flags.bits(),
inout("rax") 0x5a5a5a5a5a5a5a5au64 => result,
};
},
}

assert_eq!(result.get_bits(0..=15), 0);
Expand Down

0 comments on commit 737d068

Please sign in to comment.