blob: 0c1e25d43ef8605265b87c5a8d7b7bd2514b1875 [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 Wangd36c7112023-07-04 09:50:45 +000022use vmbase::{memory::MemoryTrackerError, virtio::pci};
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),
Alice Wangdda3ba92023-05-25 15:15:30 +000032 /// Invalid FDT.
33 InvalidFdt(FdtError),
34 /// Invalid PCI.
35 InvalidPci(PciError),
Alice Wang74f7f4b2023-06-13 08:24:50 +000036 /// Failed memory operation.
37 MemoryOperationFailed(MemoryTrackerError),
Alice Wangd36c7112023-07-04 09:50:45 +000038 /// Failed to initialize PCI.
39 PciInitializationFailed(pci::PciError),
Alice Wang62183352023-07-04 08:33:27 +000040 /// Failed to create VirtIO Socket device.
41 VirtIOSocketCreationFailed(virtio_drivers::Error),
42 /// Missing socket device.
43 MissingVirtIOSocketDevice,
Alice Wang9a8b39f2023-04-12 15:31:48 +000044}
45
46impl fmt::Display for Error {
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 match self {
Alice Wangdda3ba92023-05-25 15:15:30 +000049 Self::Hypervisor(e) => write!(f, "Hypervisor error: {e}."),
Alice Wang9a8b39f2023-04-12 15:31:48 +000050 Self::PageTableMapping(e) => {
51 write!(f, "Failed when attempting to map some range in the page table: {e}.")
52 }
Alice Wangdda3ba92023-05-25 15:15:30 +000053 Self::InvalidFdt(e) => write!(f, "Invalid FDT: {e}"),
54 Self::InvalidPci(e) => write!(f, "Invalid PCI: {e}"),
Alice Wang74f7f4b2023-06-13 08:24:50 +000055 Self::MemoryOperationFailed(e) => write!(f, "Failed memory operation: {e}"),
Alice Wangd36c7112023-07-04 09:50:45 +000056 Self::PciInitializationFailed(e) => write!(f, "Failed to initialize PCI: {e}"),
Alice Wang62183352023-07-04 08:33:27 +000057 Self::VirtIOSocketCreationFailed(e) => {
58 write!(f, "Failed to create VirtIO Socket device: {e}")
59 }
60 Self::MissingVirtIOSocketDevice => write!(f, "Missing VirtIO Socket device."),
Alice Wang9a8b39f2023-04-12 15:31:48 +000061 }
62 }
63}
64
Alice Wang90e6f162023-04-17 13:49:45 +000065impl From<HypervisorError> for Error {
66 fn from(e: HypervisorError) -> Self {
67 Self::Hypervisor(e)
Alice Wang9a8b39f2023-04-12 15:31:48 +000068 }
69}
70
71impl From<MapError> for Error {
72 fn from(e: MapError) -> Self {
73 Self::PageTableMapping(e)
74 }
75}
Alice Wangdda3ba92023-05-25 15:15:30 +000076
77impl From<FdtError> for Error {
78 fn from(e: FdtError) -> Self {
79 Self::InvalidFdt(e)
80 }
81}
82
83impl From<PciError> for Error {
84 fn from(e: PciError) -> Self {
85 Self::InvalidPci(e)
86 }
87}
Alice Wang74f7f4b2023-06-13 08:24:50 +000088
89impl From<MemoryTrackerError> for Error {
90 fn from(e: MemoryTrackerError) -> Self {
91 Self::MemoryOperationFailed(e)
92 }
93}