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::hyp::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 
37 pub trait MmioGuardedHypervisor {
38     /// Enrolls with the MMIO guard so that all MMIO will be blocked unless allow-listed with
39     /// `MmioGuardedHypervisor::map`.
enroll(&self) -> Result<()>40     fn enroll(&self) -> Result<()>;
41 
42     /// Maps a page containing the given memory address to the hypervisor MMIO guard.
43     /// The page size corresponds to the MMIO guard granule size.
map(&self, addr: usize) -> Result<()>44     fn map(&self, addr: usize) -> Result<()>;
45 
46     /// Unmaps a page containing the given memory address from the hypervisor MMIO guard.
47     /// The page size corresponds to the MMIO guard granule size.
unmap(&self, addr: usize) -> Result<()>48     fn unmap(&self, addr: usize) -> Result<()>;
49 
50     /// Returns the MMIO guard granule size in bytes.
granule(&self) -> Result<usize>51     fn granule(&self) -> Result<usize>;
52 }
53 
54 pub trait MemSharingHypervisor {
55     /// Shares a region of memory with host, granting it read, write and execute permissions.
56     /// The size of the region is equal to the memory protection granule returned by
57     /// [`hyp_meminfo`].
share(&self, base_ipa: u64) -> Result<()>58     fn share(&self, base_ipa: u64) -> Result<()>;
59 
60     /// Revokes access permission from host to a memory region previously shared with
61     /// [`mem_share`]. The size of the region is equal to the memory protection granule returned by
62     /// [`hyp_meminfo`].
unshare(&self, base_ipa: u64) -> Result<()>63     fn unshare(&self, base_ipa: u64) -> Result<()>;
64 
65     /// Returns the memory protection granule size in bytes.
granule(&self) -> Result<usize>66     fn granule(&self) -> Result<usize>;
67 }
68 
69 /// Device assigning hypervisor
70 pub trait DeviceAssigningHypervisor {
71     /// Returns MMIO token.
get_phys_mmio_token(&self, base_ipa: u64, size: u64) -> Result<u64>72     fn get_phys_mmio_token(&self, base_ipa: u64, size: u64) -> Result<u64>;
73 
74     /// 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)>75     fn get_phys_iommu_token(&self, pviommu_id: u64, vsid: u64) -> Result<(u64, u64)>;
76 }
77