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 #[cfg(target_arch = "aarch64")] 20 use super::hypervisor::GeniezoneError; 21 #[cfg(target_arch = "aarch64")] 22 use super::hypervisor::GunyahError; 23 use super::hypervisor::KvmError; 24 #[cfg(target_arch = "aarch64")] 25 use uuid::Uuid; 26 27 /// Result type with hypervisor error. 28 pub type Result<T> = result::Result<T, Error>; 29 30 /// Hypervisor error. 31 #[derive(Debug, Clone)] 32 pub enum Error { 33 /// MMIO guard is not supported. 34 MmioGuardNotSupported, 35 /// Failed to invoke a certain KVM HVC function. 36 KvmError(KvmError, u32), 37 #[cfg(target_arch = "aarch64")] 38 /// Failed to invoke GenieZone HVC function. 39 GeniezoneError(GeniezoneError, u32), 40 #[cfg(target_arch = "aarch64")] 41 /// Unsupported Hypervisor 42 UnsupportedHypervisorUuid(Uuid), 43 #[cfg(target_arch = "x86_64")] 44 /// Unsupported x86_64 Hypervisor 45 UnsupportedHypervisor(u128), 46 #[cfg(target_arch = "aarch64")] 47 /// Failed to invoke Gunyah HVC. 48 GunyahError(GunyahError), 49 } 50 51 impl fmt::Display for Error { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 53 match self { 54 Self::MmioGuardNotSupported => write!(f, "MMIO guard is not supported"), 55 Self::KvmError(e, function_id) => { 56 write!(f, "Failed to invoke the HVC function with function ID {function_id}: {e}") 57 } 58 #[cfg(target_arch = "aarch64")] 59 Self::GeniezoneError(e, function_id) => { 60 write!( 61 f, 62 "Failed to invoke GenieZone HVC function with function ID {function_id}: {e}" 63 ) 64 } 65 #[cfg(target_arch = "aarch64")] 66 Self::GunyahError(e) => { 67 write!(f, "Failed to invoke Gunyah HVC: {e}") 68 } 69 #[cfg(target_arch = "aarch64")] 70 Self::UnsupportedHypervisorUuid(u) => { 71 write!(f, "Unsupported Hypervisor UUID {u}") 72 } 73 #[cfg(target_arch = "x86_64")] 74 Self::UnsupportedHypervisor(c) => { 75 write!(f, "Unsupported x86_64 Hypervisor {c}") 76 } 77 } 78 } 79 } 80