Skip to content

Commit

Permalink
Add CCA feature
Browse files Browse the repository at this point in the history
This is WIP

Signed-off-by: Matias Ezequiel Vara Larsen <[email protected]>
  • Loading branch information
MatiasVara committed Aug 22, 2024
1 parent 86f75cd commit 68dff0c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ ifeq ($(SEV),1)
INIT_SRC += $(SNP_INIT_SRC)
BUILD_INIT = 0
endif
ifeq ($(CCA), 1)
FEATURE_FLAGS := --features cca
endif
ifeq ($(GPU),1)
FEATURE_FLAGS += --features gpu
endif
Expand Down
4 changes: 3 additions & 1 deletion src/arch/src/aarch64/linux/regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ arm64_sys_reg!(MPIDR_EL1, 3, 0, 0, 0, 5);
/// * `boot_ip` - Starting instruction pointer.
/// * `mem` - Reserved DRAM for current VM.
pub fn setup_regs(vcpu: &VcpuFd, cpu_id: u8, boot_ip: u64, mem: &GuestMemoryMmap) -> Result<()> {
// Get the register index of the PSTATE (Processor State) register.
// PSTATE cannot be accesed from the host in CCA
#[cfg(not(feature = "cca"))]
#[allow(deref_nullptr)]
// Get the register index of the PSTATE (Processor State) register.
vcpu.set_one_reg(arm64_core_reg!(pstate), &PSTATE_FAULT_BITS_64.to_le_bytes())
.map_err(Error::SetCoreRegister)?;

Expand Down
2 changes: 1 addition & 1 deletion src/arch/src/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn arch_memory_regions(size: usize) -> (ArchMemoryInfo, Vec<(GuestAddress, u
} else {
vec![
(GuestAddress(layout::DRAM_MEM_START), dram_size),
(GuestAddress(shm_start_addr), MMIO_SHM_SIZE as usize),
//(GuestAddress(shm_start_addr), MMIO_SHM_SIZE as usize),
]
};

Expand Down
53 changes: 49 additions & 4 deletions src/vmm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use std::io;
use std::os::fd::AsRawFd;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::cmp::max;
use cca::Algo;

use super::{Error, Vmm};

Expand Down Expand Up @@ -68,7 +70,9 @@ use vm_memory::mmap::MmapRegion;
#[cfg(any(target_arch = "aarch64", feature = "tee"))]
use vm_memory::Bytes;
use vm_memory::GuestMemory;
use vm_memory::{GuestAddress, GuestMemoryMmap};
use vm_memory::{GuestAddress, GuestMemoryMmap, GuestMemoryRegion, Address};

use kvm_bindings::KVM_ARM_VCPU_REC;

#[cfg(feature = "efi")]
static EDK2_BINARY: &[u8] = include_bytes!("../../../edk2/KRUN_EFI.silent.fd");
Expand Down Expand Up @@ -559,7 +563,7 @@ pub fn build_microvm(
)?;
}

#[cfg(not(feature = "tee"))]
#[cfg(all(not(feature = "tee"), not(feature = "cca")))]
let _shm_region = Some(VirtioShmRegion {
host_addr: guest_memory
.get_host_address(GuestAddress(arch_memory_info.shm_start_addr))
Expand All @@ -568,6 +572,24 @@ pub fn build_microvm(
size: arch_memory_info.shm_size as usize,
});

#[cfg(feature = "cca")]
{
let _ = vm.realm.configure_measurement(vm.fd(), Algo::AlgoSha256);
vm.realm.create_realm_descriptor(vm.fd()).unwrap();

for (_index, region) in guest_memory.iter().enumerate() {
vm.realm.populate(vm.fd(), region.start_addr().raw_value(), region.len()).unwrap();
}
let feature = KVM_ARM_VCPU_REC as i32;

// not really sure if the finalize and the activate should go here
for vcpu in vcpus.iter() {
vcpu.fd.vcpu_finalize(&feature).unwrap();
}

vm.realm.activate(vm.fd()).unwrap();
}

let mut vmm = Vmm {
guest_memory,
arch_memory_info,
Expand Down Expand Up @@ -809,7 +831,7 @@ fn load_cmdline(vmm: &Vmm) -> std::result::Result<(), StartMicrovmError> {
.map_err(StartMicrovmError::LoadCommandline)
}

#[cfg(all(target_os = "linux", not(feature = "tee")))]
#[cfg(all(target_os = "linux", not(feature = "tee"), not(feature = "cca")))]
pub(crate) fn setup_vm(
guest_memory: &GuestMemoryMmap,
) -> std::result::Result<Vm, StartMicrovmError> {
Expand All @@ -824,6 +846,29 @@ pub(crate) fn setup_vm(
.map_err(StartMicrovmError::Internal)?;
Ok(vm)
}
#[cfg(all(target_os = "linux", feature = "cca"))]
pub(crate) fn setup_vm(
guest_memory: &GuestMemoryMmap,
) -> std::result::Result<Vm, StartMicrovmError> {
let kvm = KvmContext::new()
.map_err(Error::KvmContext)
.map_err(StartMicrovmError::Internal)?;

// calculate max_addr for max_ipa
let mut max_addr = 0;
for (_index, region) in guest_memory.iter().enumerate() {
max_addr = max(max_addr, region.start_addr().raw_value() + region.len() - 1);
}

let mut vm = Vm::new(kvm.fd(), max_addr as usize)
.map_err(Error::Vm)
.map_err(StartMicrovmError::Internal)?;

vm.memory_init(guest_memory, kvm.max_memslots(), true)
.map_err(Error::Vm)
.map_err(StartMicrovmError::Internal)?;
Ok(vm)
}
#[cfg(all(target_os = "linux", feature = "tee"))]
pub(crate) fn setup_vm(
kvm: &KvmContext,
Expand Down Expand Up @@ -1021,7 +1066,7 @@ fn create_vcpus_aarch64(
) -> super::Result<Vec<Vcpu>> {
let mut vcpus = Vec::with_capacity(vcpu_config.vcpu_count as usize);
for cpu_index in 0..vcpu_config.vcpu_count {
let mut vcpu = Vcpu::new_aarch64(
let mut vcpu: Vcpu = Vcpu::new_aarch64(
cpu_index,
vm.fd(),
exit_evt.try_clone().map_err(Error::EventFd)?,
Expand Down
29 changes: 26 additions & 3 deletions src/vmm/src/linux/vstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::cell::Cell;
use std::fmt::{Display, Formatter};
use std::io;
use std::os::fd::RawFd;
use std::cmp::max;

#[cfg(feature = "tee")]
use std::os::unix::io::RawFd;
Expand Down Expand Up @@ -49,7 +50,7 @@ use kvm_bindings::{
};
use kvm_bindings::{
kvm_create_guest_memfd, kvm_userspace_memory_region, kvm_userspace_memory_region2,
KVM_API_VERSION, KVM_MEM_GUEST_MEMFD,
KVM_API_VERSION, KVM_MEM_GUEST_MEMFD, KVM_VM_TYPE_ARM_REALM, KVM_VM_TYPE_ARM_IPA_SIZE_MASK
};
use kvm_ioctls::*;
use utils::eventfd::EventFd;
Expand All @@ -65,6 +66,9 @@ use sev::launch::sev as sev_launch;
#[cfg(feature = "amd-sev")]
use sev::launch::snp;

#[cfg(feature = "cca")]
use cca::Realm;

/// Signal number (SIGRTMIN) used to kick Vcpus.
pub(crate) const VCPU_RTSIG_OFFSET: i32 = 0;

Expand Down Expand Up @@ -483,11 +487,14 @@ pub struct Vm {

#[cfg(feature = "amd-sev")]
pub tee: Tee,

#[cfg(feature = "cca")]
pub realm: Realm,
}

impl Vm {
/// Constructs a new `Vm` using the given `Kvm` instance.
#[cfg(not(feature = "tee"))]
#[cfg(all(not(feature = "tee"), not(feature = "cca")))]
pub fn new(kvm: &Kvm) -> Result<Self> {
//create fd for interacting with kvm-vm specific functions
let vm_fd = kvm.create_vm().map_err(Error::VmFd)?;
Expand All @@ -511,6 +518,22 @@ impl Vm {
})
}

#[cfg(feature = "cca")]
pub fn new(kvm: &Kvm, max_ipa: usize) -> Result<Self> {
//create fd for interacting with kvm-vm specific functions
let ipa_bits = max(64u32 - max_ipa.leading_zeros()- 1, 32) + 1;
let vm_fd = kvm.create_vm_with_type((KVM_VM_TYPE_ARM_REALM | (ipa_bits & KVM_VM_TYPE_ARM_IPA_SIZE_MASK)).into()).map_err(Error::VmFd)?;

let realm = Realm::new().unwrap();

Ok(Vm {
fd: vm_fd,
#[cfg(target_arch = "aarch64")]
irqchip_handle: None,
realm,
})
}

#[cfg(feature = "amd-sev")]
pub fn new(kvm: &Kvm, tee_config: &TeeConfig) -> Result<Self> {
//create fd for interacting with kvm-vm specific functions
Expand Down Expand Up @@ -808,7 +831,7 @@ type VcpuCell = Cell<Option<*mut Vcpu>>;

/// A wrapper around creating and using a kvm-based VCPU.
pub struct Vcpu {
fd: VcpuFd,
pub fd: VcpuFd,
id: u8,
mmio_bus: Option<devices::Bus>,
#[allow(dead_code)]
Expand Down

0 comments on commit 68dff0c

Please sign in to comment.