Skip to content

Commit

Permalink
A simple attempt at MKTME page encryption, and it is still under debu…
Browse files Browse the repository at this point in the history
…gging
  • Loading branch information
bronzeMe committed Nov 6, 2024
1 parent a474264 commit 6d55fe8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ intel = ["libvmm/vmx"]
amd = ["libvmm/svm"]
stats = []
sme = ["amd"]
mktme =["intel"]
enclave_interrupt = []
epc48 = []
epc96 = []
Expand Down
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ ARCH ?= x86_64
VENDOR ?= amd
LOG ?=
STATS ?= off
SME ?= on
SME ?= off
MKTME ?= off
INTR ?= on

# do not support debug mode
Expand All @@ -55,6 +56,7 @@ export ARCH
export VENDOR
export STATS
export SME
export MKTME
export INTR

OBJDUMP ?= objdump
Expand Down Expand Up @@ -85,6 +87,14 @@ ifeq ($(SME), on)
features += sme
endif

ifeq ($(MKTME), on)
ifneq ($(VENDOR), intel)
$(error `MKTME=on` is only available when `VENDOR=intel`)
endif
features += mktme
endif


ifeq ($(INTR), on)
features += enclave_interrupt
endif
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86_64/vmm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub use vendor::{

#[cfg(feature = "amd")]
pub use vendor::{EncHW, HmacSWEncHW};

//TODO: add intel mktme style EncHW
pub trait VcpuAccessGuestState {
// Architecture independent methods:
fn regs(&self) -> &GuestRegisters;
Expand Down
6 changes: 3 additions & 3 deletions src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Cell {
// preventing guest vm read out the encrypted view of EPC
// from high addr with c-bit = 1
// expected behavior: return plaintext view of the empty page
#[cfg(feature = "sme")]
#[cfg(any(feature = "sme", feature = "mktme"))]
gpm.insert(MemoryRegion::new_with_empty_mapper(
crate::memory::addr::phys_encrypted(hv_phys_start),
hv_phys_size,
Expand All @@ -80,7 +80,7 @@ impl Cell {
epc_size,
MemFlags::READ | MemFlags::ENCRYPTED,
))?;
#[cfg(feature = "sme")]
#[cfg(any(feature = "sme", feature = "mktme"))]
gpm.insert(MemoryRegion::new_with_empty_mapper(
crate::memory::addr::phys_encrypted(epc_start_hpa),
epc_size,
Expand Down Expand Up @@ -156,7 +156,7 @@ impl Cell {
MemFlags::READ | MemFlags::WRITE,
))?;
// Support hardware encrypt when swap out EPC page to guest RAM
#[cfg(feature = "sme")]
#[cfg(any(feature = "sme", feature = "mktme"))]
hvm.insert(MemoryRegion::new_with_offset_mapper(
region.virt_start as HostVirtAddr,
region.phys_start as HostPhysAddr,
Expand Down
11 changes: 11 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ pub const SME_C_BIT_OFFSET: usize = 1 << 47;
#[cfg(not(feature = "sme"))]
pub const SME_C_BIT_OFFSET: usize = 0;

#[cfg(feature = "mktme")]
pub const MKTME_KEYID_MASK: usize = 0xFC0000000000; // bit 51:46
#[cfg(feature = "mktme")]
pub const MKTME_KEYID_SHIFT: usize = 46;
#[cfg(feature = "mktme")]
pub const MKTME_KEYID_OFFSET: usize = 1 << 46;
#[cfg(not(feature = "mktme"))]
pub const MKTME_KEYID_MASK: usize = 0;
#[cfg(not(feature = "mktme"))]
pub const MKTME_KEYID_SHIFT: usize = 0;

pub const HV_STACK_SIZE: usize = 512 * 1024; // 512 KB
33 changes: 30 additions & 3 deletions src/memory/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#![allow(dead_code)]

use crate::consts::{HV_BASE, PAGE_SIZE, SME_C_BIT_OFFSET};
use crate::consts::{HV_BASE, PAGE_SIZE, SME_C_BIT_OFFSET, MKTME_KEYID_MASK, MKTME_KEYID_SHIFT, MKTME_KEYID_OFFSET };

pub type VirtAddr = usize;
pub type PhysAddr = usize;
Expand All @@ -35,15 +35,42 @@ lazy_static! {
}

pub fn phys_encrypted(paddr: PhysAddr) -> PhysAddr {
paddr | SME_C_BIT_OFFSET
#[cfg(feature = "mktme")]
{
// if enable mktme, enable page encryption with default keyid = 1
phys_encrypted_with_keyid(paddr, 1)
}
#[cfg(not(feature = "mktme"))]
{
paddr | SME_C_BIT_OFFSET
}
}

fn phys_encrypted_with_keyid(paddr: PhysAddr, keyid: usize) -> PhysAddr {
// clear 51:46 bit
let cleared_paddr = paddr & !MKTME_KEYID_MASK;

// extract keyid bit
let keyid_bits = keyid & 0x3F;

// add keyid into paddr
cleared_paddr | (keyid_bits << MKTME_KEYID_SHIFT)
}

pub fn virt_to_phys(vaddr: VirtAddr) -> PhysAddr {
vaddr - *PHYS_VIRT_OFFSET
}

pub fn phys_to_virt(paddr: PhysAddr) -> VirtAddr {
(paddr & (SME_C_BIT_OFFSET.wrapping_sub(1))) + *PHYS_VIRT_OFFSET
#[cfg(feature = "mktme")]
{
// if turn on mktme, extract 45:0 of paddr
(paddr & (MKTME_KEYID_OFFSET.wrapping_sub(1))) + *PHYS_VIRT_OFFSET
}
#[cfg(not(feature = "mktme"))]
{
(paddr & (SME_C_BIT_OFFSET.wrapping_sub(1))) + *PHYS_VIRT_OFFSET
}
}

pub const fn align_down(addr: usize) -> usize {
Expand Down

0 comments on commit 6d55fe8

Please sign in to comment.