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 and Result types for hypervisor.
16 
17 use core::{fmt, result};
18 
19 use super::hypervisor::{GeniezoneError, KvmError};
20 use uuid::Uuid;
21 
22 /// Result type with hypervisor error.
23 pub type Result<T> = result::Result<T, Error>;
24 
25 /// Hypervisor error.
26 #[derive(Debug, Clone)]
27 pub enum Error {
28     /// MMIO guard is not supported.
29     MmioGuardNotSupported,
30     /// Failed to invoke a certain KVM HVC function.
31     KvmError(KvmError, u32),
32     /// Failed to invoke GenieZone HVC function.
33     GeniezoneError(GeniezoneError, u32),
34     /// Unsupported Hypervisor
35     UnsupportedHypervisorUuid(Uuid),
36 }
37 
38 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result39     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40         match self {
41             Self::MmioGuardNotSupported => write!(f, "MMIO guard is not supported"),
42             Self::KvmError(e, function_id) => {
43                 write!(f, "Failed to invoke the HVC function with function ID {function_id}: {e}")
44             }
45             Self::GeniezoneError(e, function_id) => {
46                 write!(
47                     f,
48                     "Failed to invoke GenieZone HVC function with function ID {function_id}: {e}"
49                 )
50             }
51             Self::UnsupportedHypervisorUuid(u) => {
52                 write!(f, "Unsupported Hypervisor UUID {u}")
53             }
54         }
55     }
56 }
57