@@ -11,6 +11,7 @@ use std::cell::Cell;
1111use std:: fmt:: { Display , Formatter } ;
1212use std:: io;
1313use std:: os:: fd:: RawFd ;
14+ use std:: cmp:: max;
1415
1516#[ cfg( feature = "tee" ) ]
1617use std:: os:: unix:: io:: RawFd ;
@@ -48,8 +49,9 @@ use kvm_bindings::{
4849 KVM_MAX_CPUID_ENTRIES , KVM_PIT_SPEAKER_DUMMY ,
4950} ;
5051use kvm_bindings:: {
51- kvm_create_guest_memfd, kvm_userspace_memory_region, kvm_userspace_memory_region2,
52- KVM_API_VERSION , KVM_MEM_GUEST_MEMFD ,
52+ kvm_create_guest_memfd, kvm_userspace_memory_region, kvm_userspace_memory_region2, kvm_memory_attributes,
53+ KVM_API_VERSION , KVM_MEM_GUEST_MEMFD , KVM_VM_TYPE_ARM_REALM , KVM_VM_TYPE_ARM_IPA_SIZE_MASK ,
54+ KVM_MEMORY_ATTRIBUTE_PRIVATE
5355} ;
5456use kvm_ioctls:: * ;
5557use utils:: eventfd:: EventFd ;
@@ -65,6 +67,9 @@ use sev::launch::sev as sev_launch;
6567#[ cfg( feature = "amd-sev" ) ]
6668use sev:: launch:: snp;
6769
70+ #[ cfg( feature = "cca" ) ]
71+ use cca:: Realm ;
72+
6873/// Signal number (SIGRTMIN) used to kick Vcpus.
6974pub ( crate ) const VCPU_RTSIG_OFFSET : i32 = 0 ;
7075
@@ -483,11 +488,14 @@ pub struct Vm {
483488
484489 #[ cfg( feature = "amd-sev" ) ]
485490 pub tee : Tee ,
491+
492+ #[ cfg( feature = "cca" ) ]
493+ pub realm : Realm ,
486494}
487495
488496impl Vm {
489497 /// Constructs a new `Vm` using the given `Kvm` instance.
490- #[ cfg( not( feature = "tee" ) ) ]
498+ #[ cfg( all ( not( feature = "tee" ) , not ( feature = "cca" ) ) ) ]
491499 pub fn new ( kvm : & Kvm ) -> Result < Self > {
492500 //create fd for interacting with kvm-vm specific functions
493501 let vm_fd = kvm. create_vm ( ) . map_err ( Error :: VmFd ) ?;
@@ -511,6 +519,22 @@ impl Vm {
511519 } )
512520 }
513521
522+ #[ cfg( feature = "cca" ) ]
523+ pub fn new ( kvm : & Kvm , max_ipa : usize ) -> Result < Self > {
524+ //create fd for interacting with kvm-vm specific functions
525+ let ipa_bits = max ( 64u32 - max_ipa. leading_zeros ( ) - 1 , 32 ) + 1 ;
526+ 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 ) ?;
527+
528+ let realm = Realm :: new ( ) . unwrap ( ) ;
529+
530+ Ok ( Vm {
531+ fd : vm_fd,
532+ #[ cfg( target_arch = "aarch64" ) ]
533+ irqchip_handle : None ,
534+ realm,
535+ } )
536+ }
537+
514538 #[ cfg( feature = "amd-sev" ) ]
515539 pub fn new ( kvm : & Kvm , tee_config : & TeeConfig ) -> Result < Self > {
516540 //create fd for interacting with kvm-vm specific functions
@@ -581,7 +605,7 @@ impl Vm {
581605 . create_guest_memfd ( gmem)
582606 . map_err ( Error :: CreateGuestMemfd ) ?;
583607
584- let memory_region = kvm_userspace_memory_region2 {
608+ let memory_region: kvm_userspace_memory_region2 = kvm_userspace_memory_region2 {
585609 slot : index as u32 ,
586610 flags : KVM_MEM_GUEST_MEMFD ,
587611 guest_phys_addr : region. start_addr ( ) . raw_value ( ) ,
@@ -600,6 +624,14 @@ impl Vm {
600624 . set_user_memory_region2 ( memory_region)
601625 . map_err ( Error :: SetUserMemoryRegion2 ) ?;
602626 } ;
627+ // set private by default when using guestmemfd
628+ let attr = kvm_memory_attributes {
629+ address : region. start_addr ( ) . raw_value ( ) ,
630+ size : region. len ( ) ,
631+ attributes : KVM_MEMORY_ATTRIBUTE_PRIVATE as u64 ,
632+ flags : 0 ,
633+ } ;
634+ self . fd . set_memory_attributes ( attr) . unwrap ( ) ;
603635 } else {
604636 let memory_region = kvm_userspace_memory_region {
605637 slot : index as u32 ,
@@ -808,7 +840,7 @@ type VcpuCell = Cell<Option<*mut Vcpu>>;
808840
809841/// A wrapper around creating and using a kvm-based VCPU.
810842pub struct Vcpu {
811- fd : VcpuFd ,
843+ pub fd : VcpuFd ,
812844 id : u8 ,
813845 mmio_bus : Option < devices:: Bus > ,
814846 #[ allow( dead_code) ]
@@ -1267,6 +1299,12 @@ impl Vcpu {
12671299 info ! ( "Received KVM_EXIT_SHUTDOWN signal" ) ;
12681300 Ok ( VcpuEmulation :: Stopped )
12691301 }
1302+ VcpuExit :: MemoryFault { flags, gpa, size} => {
1303+ println ! ( "ignore memoryfault at {} {}" , gpa, size) ;
1304+ // TODO: To setup the setmemoryproperties I need to have access to vm
1305+ // but vm is not a shared resource
1306+ Ok ( VcpuEmulation :: Handled )
1307+ }
12701308 // Documentation specifies that below kvm exits are considered
12711309 // errors.
12721310 VcpuExit :: FailEntry ( reason, vcpu) => {
@@ -1280,6 +1318,7 @@ impl Vcpu {
12801318 r => {
12811319 // TODO: Are we sure we want to finish running a vcpu upon
12821320 // receiving a vm exit that is not necessarily an error?
1321+ println ! ( "error! {:?}" , r) ;
12831322 error ! ( "Unexpected exit reason on vcpu run: {:?}" , r) ;
12841323 Err ( Error :: VcpuUnhandledKvmExit )
12851324 }
0 commit comments