@@ -12,6 +12,8 @@ use std::io;
12
12
use std:: os:: fd:: AsRawFd ;
13
13
use std:: path:: PathBuf ;
14
14
use std:: sync:: { Arc , Mutex } ;
15
+ use std:: cmp:: max;
16
+ use cca:: Algo ;
15
17
16
18
use super :: { Error , Vmm } ;
17
19
@@ -68,7 +70,9 @@ use vm_memory::mmap::MmapRegion;
68
70
#[ cfg( any( target_arch = "aarch64" , feature = "tee" ) ) ]
69
71
use vm_memory:: Bytes ;
70
72
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 ;
72
76
73
77
#[ cfg( feature = "efi" ) ]
74
78
static EDK2_BINARY : & [ u8 ] = include_bytes ! ( "../../../edk2/KRUN_EFI.silent.fd" ) ;
@@ -559,7 +563,7 @@ pub fn build_microvm(
559
563
) ?;
560
564
}
561
565
562
- #[ cfg( not( feature = "tee" ) ) ]
566
+ #[ cfg( all ( not( feature = "tee" ) , not ( feature = "cca" ) ) ) ]
563
567
let _shm_region = Some ( VirtioShmRegion {
564
568
host_addr : guest_memory
565
569
. get_host_address ( GuestAddress ( arch_memory_info. shm_start_addr ) )
@@ -568,6 +572,24 @@ pub fn build_microvm(
568
572
size : arch_memory_info. shm_size as usize ,
569
573
} ) ;
570
574
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
+
571
593
let mut vmm = Vmm {
572
594
guest_memory,
573
595
arch_memory_info,
@@ -809,7 +831,7 @@ fn load_cmdline(vmm: &Vmm) -> std::result::Result<(), StartMicrovmError> {
809
831
. map_err ( StartMicrovmError :: LoadCommandline )
810
832
}
811
833
812
- #[ cfg( all( target_os = "linux" , not( feature = "tee" ) ) ) ]
834
+ #[ cfg( all( target_os = "linux" , not( feature = "tee" ) , not ( feature = "cca" ) ) ) ]
813
835
pub ( crate ) fn setup_vm (
814
836
guest_memory : & GuestMemoryMmap ,
815
837
) -> std:: result:: Result < Vm , StartMicrovmError > {
@@ -824,6 +846,29 @@ pub(crate) fn setup_vm(
824
846
. map_err ( StartMicrovmError :: Internal ) ?;
825
847
Ok ( vm)
826
848
}
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
+ }
827
872
#[ cfg( all( target_os = "linux" , feature = "tee" ) ) ]
828
873
pub ( crate ) fn setup_vm (
829
874
kvm : & KvmContext ,
@@ -1021,7 +1066,7 @@ fn create_vcpus_aarch64(
1021
1066
) -> super :: Result < Vec < Vcpu > > {
1022
1067
let mut vcpus = Vec :: with_capacity ( vcpu_config. vcpu_count as usize ) ;
1023
1068
for cpu_index in 0 ..vcpu_config. vcpu_count {
1024
- let mut vcpu = Vcpu :: new_aarch64 (
1069
+ let mut vcpu: Vcpu = Vcpu :: new_aarch64 (
1025
1070
cpu_index,
1026
1071
vm. fd ( ) ,
1027
1072
exit_evt. try_clone ( ) . map_err ( Error :: EventFd ) ?,
0 commit comments