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