Skip to content

Commit 68dff0c

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

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
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/arch/src/aarch64/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn arch_memory_regions(size: usize) -> (ArchMemoryInfo, Vec<(GuestAddress, u
6969
} else {
7070
vec![
7171
(GuestAddress(layout::DRAM_MEM_START), dram_size),
72-
(GuestAddress(shm_start_addr), MMIO_SHM_SIZE as usize),
72+
//(GuestAddress(shm_start_addr), MMIO_SHM_SIZE as usize),
7373
]
7474
};
7575

src/vmm/src/builder.rs

+49-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ 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;
16+
use cca::Algo;
1517

1618
use super::{Error, Vmm};
1719

@@ -68,7 +70,9 @@ use vm_memory::mmap::MmapRegion;
6870
#[cfg(any(target_arch = "aarch64", feature = "tee"))]
6971
use vm_memory::Bytes;
7072
use vm_memory::GuestMemory;
71-
use vm_memory::{GuestAddress, GuestMemoryMmap};
73+
use vm_memory::{GuestAddress, GuestMemoryMmap, GuestMemoryRegion, Address};
74+
75+
use kvm_bindings::KVM_ARM_VCPU_REC;
7276

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

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

575+
#[cfg(feature = "cca")]
576+
{
577+
let _ = vm.realm.configure_measurement(vm.fd(), Algo::AlgoSha256);
578+
vm.realm.create_realm_descriptor(vm.fd()).unwrap();
579+
580+
for (_index, region) in guest_memory.iter().enumerate() {
581+
vm.realm.populate(vm.fd(), region.start_addr().raw_value(), region.len()).unwrap();
582+
}
583+
let feature = KVM_ARM_VCPU_REC as i32;
584+
585+
// not really sure if the finalize and the activate should go here
586+
for vcpu in vcpus.iter() {
587+
vcpu.fd.vcpu_finalize(&feature).unwrap();
588+
}
589+
590+
vm.realm.activate(vm.fd()).unwrap();
591+
}
592+
571593
let mut vmm = Vmm {
572594
guest_memory,
573595
arch_memory_info,
@@ -809,7 +831,7 @@ fn load_cmdline(vmm: &Vmm) -> std::result::Result<(), StartMicrovmError> {
809831
.map_err(StartMicrovmError::LoadCommandline)
810832
}
811833

812-
#[cfg(all(target_os = "linux", not(feature = "tee")))]
834+
#[cfg(all(target_os = "linux", not(feature = "tee"), not(feature = "cca")))]
813835
pub(crate) fn setup_vm(
814836
guest_memory: &GuestMemoryMmap,
815837
) -> std::result::Result<Vm, StartMicrovmError> {
@@ -824,6 +846,29 @@ pub(crate) fn setup_vm(
824846
.map_err(StartMicrovmError::Internal)?;
825847
Ok(vm)
826848
}
849+
#[cfg(all(target_os = "linux", feature = "cca"))]
850+
pub(crate) fn setup_vm(
851+
guest_memory: &GuestMemoryMmap,
852+
) -> std::result::Result<Vm, StartMicrovmError> {
853+
let kvm = KvmContext::new()
854+
.map_err(Error::KvmContext)
855+
.map_err(StartMicrovmError::Internal)?;
856+
857+
// calculate max_addr for max_ipa
858+
let mut max_addr = 0;
859+
for (_index, region) in guest_memory.iter().enumerate() {
860+
max_addr = max(max_addr, region.start_addr().raw_value() + region.len() - 1);
861+
}
862+
863+
let mut vm = Vm::new(kvm.fd(), max_addr as usize)
864+
.map_err(Error::Vm)
865+
.map_err(StartMicrovmError::Internal)?;
866+
867+
vm.memory_init(guest_memory, kvm.max_memslots(), true)
868+
.map_err(Error::Vm)
869+
.map_err(StartMicrovmError::Internal)?;
870+
Ok(vm)
871+
}
827872
#[cfg(all(target_os = "linux", feature = "tee"))]
828873
pub(crate) fn setup_vm(
829874
kvm: &KvmContext,
@@ -1021,7 +1066,7 @@ fn create_vcpus_aarch64(
10211066
) -> super::Result<Vec<Vcpu>> {
10221067
let mut vcpus = Vec::with_capacity(vcpu_config.vcpu_count as usize);
10231068
for cpu_index in 0..vcpu_config.vcpu_count {
1024-
let mut vcpu = Vcpu::new_aarch64(
1069+
let mut vcpu: Vcpu = Vcpu::new_aarch64(
10251070
cpu_index,
10261071
vm.fd(),
10271072
exit_evt.try_clone().map_err(Error::EventFd)?,

src/vmm/src/linux/vstate.rs

+26-3
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;
@@ -65,6 +66,9 @@ use sev::launch::sev as sev_launch;
6566
#[cfg(feature = "amd-sev")]
6667
use sev::launch::snp;
6768

69+
#[cfg(feature = "cca")]
70+
use cca::Realm;
71+
6872
/// Signal number (SIGRTMIN) used to kick Vcpus.
6973
pub(crate) const VCPU_RTSIG_OFFSET: i32 = 0;
7074

@@ -483,11 +487,14 @@ pub struct Vm {
483487

484488
#[cfg(feature = "amd-sev")]
485489
pub tee: Tee,
490+
491+
#[cfg(feature = "cca")]
492+
pub realm: Realm,
486493
}
487494

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

521+
#[cfg(feature = "cca")]
522+
pub fn new(kvm: &Kvm, max_ipa: usize) -> Result<Self> {
523+
//create fd for interacting with kvm-vm specific functions
524+
let ipa_bits = max(64u32 - max_ipa.leading_zeros()- 1, 32) + 1;
525+
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)?;
526+
527+
let realm = Realm::new().unwrap();
528+
529+
Ok(Vm {
530+
fd: vm_fd,
531+
#[cfg(target_arch = "aarch64")]
532+
irqchip_handle: None,
533+
realm,
534+
})
535+
}
536+
514537
#[cfg(feature = "amd-sev")]
515538
pub fn new(kvm: &Kvm, tee_config: &TeeConfig) -> Result<Self> {
516539
//create fd for interacting with kvm-vm specific functions
@@ -808,7 +831,7 @@ type VcpuCell = Cell<Option<*mut Vcpu>>;
808831

809832
/// A wrapper around creating and using a kvm-based VCPU.
810833
pub struct Vcpu {
811-
fd: VcpuFd,
834+
pub fd: VcpuFd,
812835
id: u8,
813836
mmio_bus: Option<devices::Bus>,
814837
#[allow(dead_code)]

0 commit comments

Comments
 (0)