From b47ef9451b3d97d5e82bfe97b28ace60884764ef Mon Sep 17 00:00:00 2001 From: Matias Ezequiel Vara Larsen Date: Fri, 9 Aug 2024 04:50:26 -0400 Subject: [PATCH] Add CCA feature This is WIP Signed-off-by: Matias Ezequiel Vara Larsen --- Makefile | 3 +++ src/vmm/src/builder.rs | 28 ++++++++++++++++++++++++++-- src/vmm/src/linux/vstate.rs | 18 ++++++++++++++++-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 3dcb2ad8..f0fec397 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/vmm/src/builder.rs b/src/vmm/src/builder.rs index 627d7134..d87a5f72 100644 --- a/src/vmm/src/builder.rs +++ b/src/vmm/src/builder.rs @@ -12,6 +12,7 @@ use std::io; use std::os::fd::AsRawFd; use std::path::PathBuf; use std::sync::{Arc, Mutex}; +use std::cmp::max; use super::{Error, Vmm}; @@ -68,7 +69,7 @@ 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}; #[cfg(feature = "efi")] static EDK2_BINARY: &[u8] = include_bytes!("../../../edk2/KRUN_EFI.silent.fd"); @@ -809,7 +810,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 { @@ -824,6 +825,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 { + 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()); + } + + 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, diff --git a/src/vmm/src/linux/vstate.rs b/src/vmm/src/linux/vstate.rs index 439dd203..19f785e0 100644 --- a/src/vmm/src/linux/vstate.rs +++ b/src/vmm/src/linux/vstate.rs @@ -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; @@ -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; @@ -487,7 +488,7 @@ pub struct Vm { 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 { //create fd for interacting with kvm-vm specific functions let vm_fd = kvm.create_vm().map_err(Error::VmFd)?; @@ -511,6 +512,19 @@ impl Vm { }) } + #[cfg(feature = "cca")] + pub fn new(kvm: &Kvm, max_ipa: usize) -> Result { + //create fd for interacting with kvm-vm specific functions + let ipa_bits = max(1 << max_ipa.trailing_zeros(), 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)?; + + Ok(Vm { + fd: vm_fd, + #[cfg(target_arch = "aarch64")] + irqchip_handle: None, + }) + } + #[cfg(feature = "amd-sev")] pub fn new(kvm: &Kvm, tee_config: &TeeConfig) -> Result { //create fd for interacting with kvm-vm specific functions