1 // Copyright 2023, The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //! This module regroups some common traits shared by all the hypervisors. 16 17 use crate::Result; 18 19 /// Trait for the hypervisor. 20 pub trait Hypervisor { 21 /// Returns the hypervisor's MMIO_GUARD implementation, if any. as_mmio_guard(&self) -> Option<&dyn MmioGuardedHypervisor>22 fn as_mmio_guard(&self) -> Option<&dyn MmioGuardedHypervisor> { 23 None 24 } 25 26 /// Returns the hypervisor's dynamic memory sharing implementation, if any. as_mem_sharer(&self) -> Option<&dyn MemSharingHypervisor>27 fn as_mem_sharer(&self) -> Option<&dyn MemSharingHypervisor> { 28 None 29 } 30 31 /// Returns the hypervisor's device assigning implementation, if any. as_device_assigner(&self) -> Option<&dyn DeviceAssigningHypervisor>32 fn as_device_assigner(&self) -> Option<&dyn DeviceAssigningHypervisor> { 33 None 34 } 35 36 /// Returns the granule used by all APIs (MEM_SHARE, MMIO_GUARD, device assignment, ...). 37 /// 38 /// If no such API is supported or if they support different granule sizes, returns None. get_granule_size(&self) -> Option<usize>39 fn get_granule_size(&self) -> Option<usize> { 40 None 41 } 42 } 43 44 pub trait MmioGuardedHypervisor { 45 /// Enrolls with the MMIO guard so that all MMIO will be blocked unless allow-listed with 46 /// `MmioGuardedHypervisor::map`. enroll(&self) -> Result<()>47 fn enroll(&self) -> Result<()>; 48 49 /// Maps a page containing the given memory address to the hypervisor MMIO guard. 50 /// The page size corresponds to the MMIO guard granule size. map(&self, addr: usize) -> Result<()>51 fn map(&self, addr: usize) -> Result<()>; 52 53 /// Unmaps a page containing the given memory address from the hypervisor MMIO guard. 54 /// The page size corresponds to the MMIO guard granule size. unmap(&self, addr: usize) -> Result<()>55 fn unmap(&self, addr: usize) -> Result<()>; 56 57 /// Returns the MMIO guard granule size in bytes. granule(&self) -> Result<usize>58 fn granule(&self) -> Result<usize>; 59 } 60 61 pub trait MemSharingHypervisor { 62 /// Shares a region of memory with host, granting it read, write and execute permissions. 63 /// The size of the region is equal to the memory protection granule returned by 64 /// [`hyp_meminfo`]. share(&self, base_ipa: u64) -> Result<()>65 fn share(&self, base_ipa: u64) -> Result<()>; 66 67 /// Revokes access permission from host to a memory region previously shared with 68 /// [`mem_share`]. The size of the region is equal to the memory protection granule returned by 69 /// [`hyp_meminfo`]. unshare(&self, base_ipa: u64) -> Result<()>70 fn unshare(&self, base_ipa: u64) -> Result<()>; 71 72 /// Returns the memory protection granule size in bytes. granule(&self) -> Result<usize>73 fn granule(&self) -> Result<usize>; 74 } 75 76 /// Device assigning hypervisor 77 pub trait DeviceAssigningHypervisor { 78 /// Returns MMIO token. get_phys_mmio_token(&self, base_ipa: u64) -> Result<u64>79 fn get_phys_mmio_token(&self, base_ipa: u64) -> Result<u64>; 80 81 /// Returns DMA token as a tuple of (phys_iommu_id, phys_sid). get_phys_iommu_token(&self, pviommu_id: u64, vsid: u64) -> Result<(u64, u64)>82 fn get_phys_iommu_token(&self, pviommu_id: u64, vsid: u64) -> Result<(u64, u64)>; 83 } 84