blob: 8e2991c9bfacb9cd7a05b1478f4e35fbd3b46e4c [file] [log] [blame]
Alice Wang9a8b39f2023-04-12 15:31:48 +00001// Copyright 2022, 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//! This module contains the error thrown by Rialto.
16
17use aarch64_paging::MapError;
18use core::{fmt, result};
Alice Wangdda3ba92023-05-25 15:15:30 +000019use fdtpci::PciError;
Alice Wang90e6f162023-04-17 13:49:45 +000020use hyp::Error as HypervisorError;
Alice Wangdda3ba92023-05-25 15:15:30 +000021use libfdt::FdtError;
Alice Wang74f7f4b2023-06-13 08:24:50 +000022use vmbase::memory::MemoryTrackerError;
Alice Wang9a8b39f2023-04-12 15:31:48 +000023
24pub type Result<T> = result::Result<T, Error>;
25
26#[derive(Clone, Debug)]
27pub enum Error {
Alice Wang90e6f162023-04-17 13:49:45 +000028 /// Hypervisor error.
29 Hypervisor(HypervisorError),
Alice Wang9a8b39f2023-04-12 15:31:48 +000030 /// Failed when attempting to map some range in the page table.
31 PageTableMapping(MapError),
32 /// Failed to initialize the logger.
33 LoggerInit,
Alice Wangdda3ba92023-05-25 15:15:30 +000034 /// Invalid FDT.
35 InvalidFdt(FdtError),
36 /// Invalid PCI.
37 InvalidPci(PciError),
Alice Wang74f7f4b2023-06-13 08:24:50 +000038 /// Failed memory operation.
39 MemoryOperationFailed(MemoryTrackerError),
Alice Wang9a8b39f2023-04-12 15:31:48 +000040}
41
42impl fmt::Display for Error {
43 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44 match self {
Alice Wangdda3ba92023-05-25 15:15:30 +000045 Self::Hypervisor(e) => write!(f, "Hypervisor error: {e}."),
Alice Wang9a8b39f2023-04-12 15:31:48 +000046 Self::PageTableMapping(e) => {
47 write!(f, "Failed when attempting to map some range in the page table: {e}.")
48 }
49 Self::LoggerInit => write!(f, "Failed to initialize the logger."),
Alice Wangdda3ba92023-05-25 15:15:30 +000050 Self::InvalidFdt(e) => write!(f, "Invalid FDT: {e}"),
51 Self::InvalidPci(e) => write!(f, "Invalid PCI: {e}"),
Alice Wang74f7f4b2023-06-13 08:24:50 +000052 Self::MemoryOperationFailed(e) => write!(f, "Failed memory operation: {e}"),
Alice Wang9a8b39f2023-04-12 15:31:48 +000053 }
54 }
55}
56
Alice Wang90e6f162023-04-17 13:49:45 +000057impl From<HypervisorError> for Error {
58 fn from(e: HypervisorError) -> Self {
59 Self::Hypervisor(e)
Alice Wang9a8b39f2023-04-12 15:31:48 +000060 }
61}
62
63impl From<MapError> for Error {
64 fn from(e: MapError) -> Self {
65 Self::PageTableMapping(e)
66 }
67}
Alice Wangdda3ba92023-05-25 15:15:30 +000068
69impl From<FdtError> for Error {
70 fn from(e: FdtError) -> Self {
71 Self::InvalidFdt(e)
72 }
73}
74
75impl From<PciError> for Error {
76 fn from(e: PciError) -> Self {
77 Self::InvalidPci(e)
78 }
79}
Alice Wang74f7f4b2023-06-13 08:24:50 +000080
81impl From<MemoryTrackerError> for Error {
82 fn from(e: MemoryTrackerError) -> Self {
83 Self::MemoryOperationFailed(e)
84 }
85}