From 27dff281aea2e090cad6c84dfbe5c6d899f7963d Mon Sep 17 00:00:00 2001 From: Yi-De Wu Date: Thu, 30 Mar 2023 14:43:16 +0800 Subject: [PATCH] geniezone: Pass memory region purpose to hypervisor Feature: - Modify set_user_memory_region function - Encode memory region purpose to flags Bug: 278959491 Test: tools/presubmit --all Signed-off-by: Jerry Wang Signed-off-by: Yi-De Wu Change-Id: I8b46219ba73c24eae9ca7ca80174cdec3a6f822d Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4413735 Reviewed-by: Frederick Mayle Reviewed-by: Daniel Verkamp --- .../geniezone/geniezone_sys/aarch64/bindings.rs | 5 ++++- hypervisor/src/geniezone/mod.rs | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/hypervisor/src/geniezone/geniezone_sys/aarch64/bindings.rs b/hypervisor/src/geniezone/geniezone_sys/aarch64/bindings.rs index 090adf8d24..19ed9c1c88 100644 --- a/hypervisor/src/geniezone/geniezone_sys/aarch64/bindings.rs +++ b/hypervisor/src/geniezone/geniezone_sys/aarch64/bindings.rs @@ -134,6 +134,9 @@ pub const GIC_V3_NR_LRS: u32 = 16; pub const GZVM_IOC_MAGIC: u32 = 146; pub const GZVM_CAP_ARM_VM_IPA_SIZE: u32 = 165; pub const GZVM_CAP_ARM_PROTECTED_VM: u32 = 4290435761; +pub const GZVM_USER_MEM_REGION_GUEST_MEM: u32 = 1; +pub const GZVM_USER_MEM_REGION_PROTECT_FW: u32 = 2; +pub const GZVM_USER_MEM_REGION_STATIC_SWIOTLB: u32 = 4; pub const GZVM_IRQ_VCPU2_SHIFT: u32 = 28; pub const GZVM_IRQ_VCPU2_MASK: u32 = 15; pub const GZVM_IRQ_TYPE_SHIFT: u32 = 24; @@ -282,7 +285,7 @@ pub const GZVM_FUNC_IRQ_LINE: ::std::os::raw::c_uint = 10; pub const GZVM_FUNC_CREATE_DEVICE: ::std::os::raw::c_uint = 11; pub const GZVM_FUNC_PROBE: ::std::os::raw::c_uint = 12; pub const GZVM_FUNC_ENABLE_CAP: ::std::os::raw::c_uint = 13; -pub const NR_GZVM_FUNC: ::std::os::raw::c_uint = 14; +pub const GZVM_FUNC_MEMREGION_PURPOSE: ::std::os::raw::c_uint = 14; pub type _bindgen_ty_1 = ::std::os::raw::c_uint; pub type gzvm_id_t = u16; pub type gzvm_vcpu_id_t = u16; diff --git a/hypervisor/src/geniezone/mod.rs b/hypervisor/src/geniezone/mod.rs index f21a61182a..66b854937f 100644 --- a/hypervisor/src/geniezone/mod.rs +++ b/hypervisor/src/geniezone/mod.rs @@ -56,6 +56,7 @@ use sync::Mutex; use vm_memory::GuestAddress; use vm_memory::GuestMemory; use vm_memory::MemoryRegionInformation; +use vm_memory::MemoryRegionPurpose; use crate::ClockState; use crate::Config; @@ -493,8 +494,8 @@ unsafe fn set_user_memory_region( guest_addr: u64, memory_size: u64, userspace_addr: *mut u8, + flags: u32, ) -> Result<()> { - let flags = 0; let region = gzvm_userspace_memory_region { slot, flags, @@ -607,8 +608,14 @@ impl GeniezoneVm { guest_addr, size, host_addr, + options, .. }| { + let flags = match options.purpose { + MemoryRegionPurpose::GuestMemoryRegion => GZVM_USER_MEM_REGION_GUEST_MEM, + MemoryRegionPurpose::ProtectedFirmwareRegion => GZVM_USER_MEM_REGION_PROTECT_FW, + MemoryRegionPurpose::StaticSwiotlbRegion => GZVM_USER_MEM_REGION_STATIC_SWIOTLB, + }; unsafe { // Safe because the guest regions are guaranteed not to overlap. set_user_memory_region( @@ -619,6 +626,7 @@ impl GeniezoneVm { guest_addr.offset(), size as u64, host_addr as *mut u8, + flags, ) } }, @@ -912,6 +920,7 @@ impl Vm for GeniezoneVm { Some(gap) => gap.0, None => (regions.len() + self.guest_mem.num_regions() as usize) as MemSlot, }; + let flags = 0; // Safe because we check that the given guest address is valid and has no overlaps. We also // know that the pointer and size are correct because the MemoryMapping interface ensures @@ -926,6 +935,7 @@ impl Vm for GeniezoneVm { guest_addr.offset(), size, mem.as_ptr(), + flags, ) }; @@ -956,7 +966,7 @@ impl Vm for GeniezoneVm { } // Safe because the slot is checked against the list of memory slots. unsafe { - set_user_memory_region(&self.vm, slot, false, false, 0, 0, std::ptr::null_mut())?; + set_user_memory_region(&self.vm, slot, false, false, 0, 0, std::ptr::null_mut(), 0)?; } self.mem_slot_gaps.lock().push(Reverse(slot)); // This remove will always succeed because of the contains_key check above.