blob: 461870b748f3883e596ba1e7cab2e8e686107ef3 [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 Wang4e082c32023-07-11 07:41:50 +000044 /// Failed VirtIO driver operation.
45 VirtIODriverOperationFailed(virtio_drivers::Error),
46 /// Failed to receive data.
47 ReceivingDataFailed(virtio_drivers::Error),
Alice Wang9a8b39f2023-04-12 15:31:48 +000048}
49
50impl fmt::Display for Error {
51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 match self {
Alice Wangdda3ba92023-05-25 15:15:30 +000053 Self::Hypervisor(e) => write!(f, "Hypervisor error: {e}."),
Alice Wang9a8b39f2023-04-12 15:31:48 +000054 Self::PageTableMapping(e) => {
55 write!(f, "Failed when attempting to map some range in the page table: {e}.")
56 }
Alice Wangdda3ba92023-05-25 15:15:30 +000057 Self::InvalidFdt(e) => write!(f, "Invalid FDT: {e}"),
58 Self::InvalidPci(e) => write!(f, "Invalid PCI: {e}"),
Alice Wang74f7f4b2023-06-13 08:24:50 +000059 Self::MemoryOperationFailed(e) => write!(f, "Failed memory operation: {e}"),
Alice Wangd36c7112023-07-04 09:50:45 +000060 Self::PciInitializationFailed(e) => write!(f, "Failed to initialize PCI: {e}"),
Alice Wang62183352023-07-04 08:33:27 +000061 Self::VirtIOSocketCreationFailed(e) => {
62 write!(f, "Failed to create VirtIO Socket device: {e}")
63 }
64 Self::MissingVirtIOSocketDevice => write!(f, "Missing VirtIO Socket device."),
Alice Wang4e082c32023-07-11 07:41:50 +000065 Self::VirtIODriverOperationFailed(e) => {
66 write!(f, "Failed VirtIO driver operation: {e}")
67 }
68 Self::ReceivingDataFailed(e) => write!(f, "Failed to receive data: {e}"),
Alice Wang9a8b39f2023-04-12 15:31:48 +000069 }
70 }
71}
72
Alice Wang90e6f162023-04-17 13:49:45 +000073impl From<HypervisorError> for Error {
74 fn from(e: HypervisorError) -> Self {
75 Self::Hypervisor(e)
Alice Wang9a8b39f2023-04-12 15:31:48 +000076 }
77}
78
79impl From<MapError> for Error {
80 fn from(e: MapError) -> Self {
81 Self::PageTableMapping(e)
82 }
83}
Alice Wangdda3ba92023-05-25 15:15:30 +000084
85impl From<FdtError> for Error {
86 fn from(e: FdtError) -> Self {
87 Self::InvalidFdt(e)
88 }
89}
90
91impl From<PciError> for Error {
92 fn from(e: PciError) -> Self {
93 Self::InvalidPci(e)
94 }
95}
Alice Wang74f7f4b2023-06-13 08:24:50 +000096
97impl From<MemoryTrackerError> for Error {
98 fn from(e: MemoryTrackerError) -> Self {
99 Self::MemoryOperationFailed(e)
100 }
101}
Alice Wang4e082c32023-07-11 07:41:50 +0000102
103impl From<virtio_drivers::Error> for Error {
104 fn from(e: virtio_drivers::Error) -> Self {
105 Self::VirtIODriverOperationFailed(e)
106 }
107}