blob: 3fdad701f1d94b8a26302f109bed1676ece0dca8 [file] [log] [blame]
Alice Wang90e6f162023-04-17 13:49:45 +00001// 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
Yi-De Wu725bd922023-04-24 16:26:52 +080017use crate::GeniezoneError;
Andrew Walbran9afab672023-04-17 14:26:23 +000018use crate::KvmError;
Alice Wang90e6f162023-04-17 13:49:45 +000019use core::{fmt, result};
Srivatsa Vaddagiri353f5d02023-04-18 23:49:01 -070020use uuid::Uuid;
Alice Wang90e6f162023-04-17 13:49:45 +000021
22/// Result type with hypervisor error.
23pub type Result<T> = result::Result<T, Error>;
24
25/// Hypervisor error.
26#[derive(Debug, Clone)]
27pub enum Error {
28 /// MMIO guard is not supported.
Pierre-Clément Tosi5430a932023-07-12 10:24:11 +000029 MmioGuardNotSupported,
Andrew Walbran9afab672023-04-17 14:26:23 +000030 /// Failed to invoke a certain KVM HVC function.
31 KvmError(KvmError, u32),
Yi-De Wu725bd922023-04-24 16:26:52 +080032 /// Failed to invoke GenieZone HVC function.
33 GeniezoneError(GeniezoneError, u32),
34 /// Unsupported Hypervisor
Srivatsa Vaddagiri353f5d02023-04-18 23:49:01 -070035 UnsupportedHypervisorUuid(Uuid),
Alice Wang90e6f162023-04-17 13:49:45 +000036 /// The MMIO_GUARD granule used by the hypervisor is not supported.
37 UnsupportedMmioGuardGranule(usize),
38}
39
40impl fmt::Display for Error {
41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42 match self {
Pierre-Clément Tosi5430a932023-07-12 10:24:11 +000043 Self::MmioGuardNotSupported => write!(f, "MMIO guard is not supported"),
Andrew Walbran9afab672023-04-17 14:26:23 +000044 Self::KvmError(e, function_id) => {
Alice Wang90e6f162023-04-17 13:49:45 +000045 write!(f, "Failed to invoke the HVC function with function ID {function_id}: {e}")
46 }
Yi-De Wu725bd922023-04-24 16:26:52 +080047 Self::GeniezoneError(e, function_id) => {
48 write!(
49 f,
50 "Failed to invoke GenieZone HVC function with function ID {function_id}: {e}"
51 )
52 }
Srivatsa Vaddagiri353f5d02023-04-18 23:49:01 -070053 Self::UnsupportedHypervisorUuid(u) => {
54 write!(f, "Unsupported Hypervisor UUID {u}")
55 }
Alice Wang90e6f162023-04-17 13:49:45 +000056 Self::UnsupportedMmioGuardGranule(g) => {
57 write!(f, "Unsupported MMIO guard granule: {g}")
58 }
59 }
60 }
61}