Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 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 | |
Andrew Walbran | 9afab67 | 2023-04-17 14:26:23 +0000 | [diff] [blame] | 17 | use crate::KvmError; |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 18 | use core::{fmt, result}; |
Srivatsa Vaddagiri | 353f5d0 | 2023-04-18 23:49:01 -0700 | [diff] [blame^] | 19 | use uuid::Uuid; |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 20 | |
| 21 | /// Result type with hypervisor error. |
| 22 | pub type Result<T> = result::Result<T, Error>; |
| 23 | |
| 24 | /// Hypervisor error. |
| 25 | #[derive(Debug, Clone)] |
| 26 | pub enum Error { |
| 27 | /// MMIO guard is not supported. |
| 28 | MmioGuardNotsupported, |
Andrew Walbran | 9afab67 | 2023-04-17 14:26:23 +0000 | [diff] [blame] | 29 | /// Failed to invoke a certain KVM HVC function. |
| 30 | KvmError(KvmError, u32), |
Srivatsa Vaddagiri | 353f5d0 | 2023-04-18 23:49:01 -0700 | [diff] [blame^] | 31 | /// Unsupported Hypervisor. |
| 32 | UnsupportedHypervisorUuid(Uuid), |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 33 | /// The MMIO_GUARD granule used by the hypervisor is not supported. |
| 34 | UnsupportedMmioGuardGranule(usize), |
| 35 | } |
| 36 | |
| 37 | impl fmt::Display for Error { |
| 38 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 39 | match self { |
| 40 | Self::MmioGuardNotsupported => write!(f, "MMIO guard is not supported"), |
Andrew Walbran | 9afab67 | 2023-04-17 14:26:23 +0000 | [diff] [blame] | 41 | Self::KvmError(e, function_id) => { |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 42 | write!(f, "Failed to invoke the HVC function with function ID {function_id}: {e}") |
| 43 | } |
Srivatsa Vaddagiri | 353f5d0 | 2023-04-18 23:49:01 -0700 | [diff] [blame^] | 44 | Self::UnsupportedHypervisorUuid(u) => { |
| 45 | write!(f, "Unsupported Hypervisor UUID {u}") |
| 46 | } |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 47 | Self::UnsupportedMmioGuardGranule(g) => { |
| 48 | write!(f, "Unsupported MMIO guard granule: {g}") |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |