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 //! Error relating to memory management. 16 17 use core::fmt; 18 19 use hypervisor_backends::Error as HypervisorError; 20 21 /// Errors for MemoryTracker operations. 22 #[derive(Debug, Clone)] 23 pub enum MemoryTrackerError { 24 /// MemoryTracker not configured or deactivated. 25 Unavailable, 26 /// Tried to modify the memory base address. 27 DifferentBaseAddress, 28 /// Tried to shrink to a larger memory size. 29 SizeTooLarge, 30 /// Tracked regions would not fit in memory size. 31 SizeTooSmall, 32 /// Reached limit number of tracked regions. 33 Full, 34 /// Region is out of the tracked memory address space. 35 OutOfRange, 36 /// New region overlaps with tracked regions. 37 Overlaps, 38 /// Region couldn't be mapped. 39 FailedToMap, 40 /// Region couldn't be unmapped. 41 FailedToUnmap, 42 /// Error from the interaction with the hypervisor. 43 Hypervisor(HypervisorError), 44 /// Failure to set `SHARED_MEMORY`. 45 SharedMemorySetFailure, 46 /// Failure to set `SHARED_POOL`. 47 SharedPoolSetFailure, 48 /// Rejected request to map footer that is already mapped. 49 FooterAlreadyMapped, 50 /// Invalid page table entry. 51 InvalidPte, 52 /// Failed to flush memory region. 53 FlushRegionFailed, 54 /// Failed to set PTE dirty state. 55 SetPteDirtyFailed, 56 /// Attempting to MMIO_GUARD_MAP more than once the same region. 57 DuplicateMmioShare(usize), 58 /// The MMIO_GUARD granule used by the hypervisor is not supported. 59 UnsupportedMmioGuardGranule(usize), 60 } 61 62 impl fmt::Display for MemoryTrackerError { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 64 match self { 65 Self::Unavailable => write!(f, "MemoryTracker is not available"), 66 Self::DifferentBaseAddress => write!(f, "Received different base address"), 67 Self::SizeTooLarge => write!(f, "Tried to shrink to a larger memory size"), 68 Self::SizeTooSmall => write!(f, "Tracked regions would not fit in memory size"), 69 Self::Full => write!(f, "Reached limit number of tracked regions"), 70 Self::OutOfRange => write!(f, "Region is out of the tracked memory address space"), 71 Self::Overlaps => write!(f, "New region overlaps with tracked regions"), 72 Self::FailedToMap => write!(f, "Failed to map the new region"), 73 Self::FailedToUnmap => write!(f, "Failed to unmap the new region"), 74 Self::Hypervisor(e) => e.fmt(f), 75 Self::SharedMemorySetFailure => write!(f, "Failed to set SHARED_MEMORY"), 76 Self::SharedPoolSetFailure => write!(f, "Failed to set SHARED_POOL"), 77 Self::FooterAlreadyMapped => write!(f, "Refused to map image footer again"), 78 Self::InvalidPte => write!(f, "Page table entry is not valid"), 79 Self::FlushRegionFailed => write!(f, "Failed to flush memory region"), 80 Self::SetPteDirtyFailed => write!(f, "Failed to set PTE dirty state"), 81 Self::DuplicateMmioShare(addr) => { 82 write!(f, "Attempted to share the same MMIO region at {addr:#x} twice") 83 } 84 Self::UnsupportedMmioGuardGranule(g) => { 85 write!(f, "Unsupported MMIO guard granule: {g}") 86 } 87 } 88 } 89 } 90 91 impl From<HypervisorError> for MemoryTrackerError { from(e: HypervisorError) -> Self92 fn from(e: HypervisorError) -> Self { 93 Self::Hypervisor(e) 94 } 95 } 96