Skip to content

Commit 9e6e5bb

Browse files
committed
Add CCA feature
This is WIP Signed-off-by: Matias Ezequiel Vara Larsen <[email protected]>
1 parent 86f75cd commit 9e6e5bb

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ ifeq ($(SEV),1)
2727
INIT_SRC += $(SNP_INIT_SRC)
2828
BUILD_INIT = 0
2929
endif
30+
ifeq ($(CCA), 1)
31+
FEATURE_FLAGS := --features cca
32+
endif
3033
ifeq ($(GPU),1)
3134
FEATURE_FLAGS += --features gpu
3235
endif

src/arch/src/aarch64/linux/regs.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ arm64_sys_reg!(MPIDR_EL1, 3, 0, 0, 0, 5);
125125
/// * `boot_ip` - Starting instruction pointer.
126126
/// * `mem` - Reserved DRAM for current VM.
127127
pub fn setup_regs(vcpu: &VcpuFd, cpu_id: u8, boot_ip: u64, mem: &GuestMemoryMmap) -> Result<()> {
128-
// Get the register index of the PSTATE (Processor State) register.
128+
// PSTATE cannot be accesed from the host in CCA
129+
#[cfg(not(feature = "cca"))]
129130
#[allow(deref_nullptr)]
131+
// Get the register index of the PSTATE (Processor State) register.
130132
vcpu.set_one_reg(arm64_core_reg!(pstate), &PSTATE_FAULT_BITS_64.to_le_bytes())
131133
.map_err(Error::SetCoreRegister)?;
132134

src/vmm/src/builder.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::io;
1212
use std::os::fd::AsRawFd;
1313
use std::path::PathBuf;
1414
use std::sync::{Arc, Mutex};
15+
use std::cmp::max;
1516

1617
use super::{Error, Vmm};
1718

@@ -68,7 +69,7 @@ use vm_memory::mmap::MmapRegion;
6869
#[cfg(any(target_arch = "aarch64", feature = "tee"))]
6970
use vm_memory::Bytes;
7071
use vm_memory::GuestMemory;
71-
use vm_memory::{GuestAddress, GuestMemoryMmap};
72+
use vm_memory::{GuestAddress, GuestMemoryMmap, GuestMemoryRegion, Address};
7273

7374
#[cfg(feature = "efi")]
7475
static EDK2_BINARY: &[u8] = include_bytes!("../../../edk2/KRUN_EFI.silent.fd");
@@ -809,7 +810,7 @@ fn load_cmdline(vmm: &Vmm) -> std::result::Result<(), StartMicrovmError> {
809810
.map_err(StartMicrovmError::LoadCommandline)
810811
}
811812

812-
#[cfg(all(target_os = "linux", not(feature = "tee")))]
813+
#[cfg(all(target_os = "linux", not(feature = "tee"), not(feature = "cca")))]
813814
pub(crate) fn setup_vm(
814815
guest_memory: &GuestMemoryMmap,
815816
) -> std::result::Result<Vm, StartMicrovmError> {
@@ -824,6 +825,29 @@ pub(crate) fn setup_vm(
824825
.map_err(StartMicrovmError::Internal)?;
825826
Ok(vm)
826827
}
828+
#[cfg(all(target_os = "linux", feature = "cca"))]
829+
pub(crate) fn setup_vm(
830+
guest_memory: &GuestMemoryMmap,
831+
) -> std::result::Result<Vm, StartMicrovmError> {
832+
let kvm = KvmContext::new()
833+
.map_err(Error::KvmContext)
834+
.map_err(StartMicrovmError::Internal)?;
835+
836+
// calculate max_addr for max_ipa
837+
let mut max_addr = 0;
838+
for (_index, region) in guest_memory.iter().enumerate() {
839+
max_addr = max(max_addr, region.start_addr().raw_value() + region.len() - 1);
840+
}
841+
842+
let mut vm = Vm::new(kvm.fd(), max_addr as usize)
843+
.map_err(Error::Vm)
844+
.map_err(StartMicrovmError::Internal)?;
845+
846+
vm.memory_init(guest_memory, kvm.max_memslots(), true)
847+
.map_err(Error::Vm)
848+
.map_err(StartMicrovmError::Internal)?;
849+
Ok(vm)
850+
}
827851
#[cfg(all(target_os = "linux", feature = "tee"))]
828852
pub(crate) fn setup_vm(
829853
kvm: &KvmContext,

src/vmm/src/linux/vstate.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::cell::Cell;
1111
use std::fmt::{Display, Formatter};
1212
use std::io;
1313
use std::os::fd::RawFd;
14+
use std::cmp::max;
1415

1516
#[cfg(feature = "tee")]
1617
use std::os::unix::io::RawFd;
@@ -49,7 +50,7 @@ use kvm_bindings::{
4950
};
5051
use kvm_bindings::{
5152
kvm_create_guest_memfd, kvm_userspace_memory_region, kvm_userspace_memory_region2,
52-
KVM_API_VERSION, KVM_MEM_GUEST_MEMFD,
53+
KVM_API_VERSION, KVM_MEM_GUEST_MEMFD, KVM_VM_TYPE_ARM_REALM, KVM_VM_TYPE_ARM_IPA_SIZE_MASK
5354
};
5455
use kvm_ioctls::*;
5556
use utils::eventfd::EventFd;
@@ -487,7 +488,7 @@ pub struct Vm {
487488

488489
impl Vm {
489490
/// Constructs a new `Vm` using the given `Kvm` instance.
490-
#[cfg(not(feature = "tee"))]
491+
#[cfg(all(not(feature = "tee"), not(feature = "cca")))]
491492
pub fn new(kvm: &Kvm) -> Result<Self> {
492493
//create fd for interacting with kvm-vm specific functions
493494
let vm_fd = kvm.create_vm().map_err(Error::VmFd)?;
@@ -511,6 +512,19 @@ impl Vm {
511512
})
512513
}
513514

515+
#[cfg(feature = "cca")]
516+
pub fn new(kvm: &Kvm, max_ipa: usize) -> Result<Self> {
517+
//create fd for interacting with kvm-vm specific functions
518+
let ipa_bits = max(64u32 - max_ipa.leading_zeros()- 1, 32) + 1;
519+
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)?;
520+
521+
Ok(Vm {
522+
fd: vm_fd,
523+
#[cfg(target_arch = "aarch64")]
524+
irqchip_handle: None,
525+
})
526+
}
527+
514528
#[cfg(feature = "amd-sev")]
515529
pub fn new(kvm: &Kvm, tee_config: &TeeConfig) -> Result<Self> {
516530
//create fd for interacting with kvm-vm specific functions

0 commit comments

Comments
 (0)