blob: 23667ed80eef49dac3694eb0c450fa45ea6077cb [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
Alice Wang748b0322023-07-24 12:51:18 +000026type CiboriumSerError = ciborium::ser::Error<virtio_drivers::Error>;
27type CiboriumDeError = ciborium::de::Error<virtio_drivers::Error>;
28
29#[derive(Debug)]
Alice Wang9a8b39f2023-04-12 15:31:48 +000030pub enum Error {
Alice Wang90e6f162023-04-17 13:49:45 +000031 /// Hypervisor error.
32 Hypervisor(HypervisorError),
Alice Wang9a8b39f2023-04-12 15:31:48 +000033 /// Failed when attempting to map some range in the page table.
34 PageTableMapping(MapError),
Alice Wangdda3ba92023-05-25 15:15:30 +000035 /// Invalid FDT.
36 InvalidFdt(FdtError),
37 /// Invalid PCI.
38 InvalidPci(PciError),
Alice Wang74f7f4b2023-06-13 08:24:50 +000039 /// Failed memory operation.
40 MemoryOperationFailed(MemoryTrackerError),
Alice Wangd36c7112023-07-04 09:50:45 +000041 /// Failed to initialize PCI.
42 PciInitializationFailed(pci::PciError),
Alice Wang62183352023-07-04 08:33:27 +000043 /// Failed to create VirtIO Socket device.
44 VirtIOSocketCreationFailed(virtio_drivers::Error),
45 /// Missing socket device.
46 MissingVirtIOSocketDevice,
Alice Wang4e082c32023-07-11 07:41:50 +000047 /// Failed VirtIO driver operation.
48 VirtIODriverOperationFailed(virtio_drivers::Error),
Alice Wang748b0322023-07-24 12:51:18 +000049 /// Failed to serialize.
50 SerializationFailed(CiboriumSerError),
51 /// Failed to deserialize.
52 DeserializationFailed(CiboriumDeError),
Alice Wang9a8b39f2023-04-12 15:31:48 +000053}
54
55impl fmt::Display for Error {
56 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57 match self {
Alice Wangdda3ba92023-05-25 15:15:30 +000058 Self::Hypervisor(e) => write!(f, "Hypervisor error: {e}."),
Alice Wang9a8b39f2023-04-12 15:31:48 +000059 Self::PageTableMapping(e) => {
60 write!(f, "Failed when attempting to map some range in the page table: {e}.")
61 }
Alice Wangdda3ba92023-05-25 15:15:30 +000062 Self::InvalidFdt(e) => write!(f, "Invalid FDT: {e}"),
63 Self::InvalidPci(e) => write!(f, "Invalid PCI: {e}"),
Alice Wang74f7f4b2023-06-13 08:24:50 +000064 Self::MemoryOperationFailed(e) => write!(f, "Failed memory operation: {e}"),
Alice Wangd36c7112023-07-04 09:50:45 +000065 Self::PciInitializationFailed(e) => write!(f, "Failed to initialize PCI: {e}"),
Alice Wang62183352023-07-04 08:33:27 +000066 Self::VirtIOSocketCreationFailed(e) => {
67 write!(f, "Failed to create VirtIO Socket device: {e}")
68 }
69 Self::MissingVirtIOSocketDevice => write!(f, "Missing VirtIO Socket device."),
Alice Wang4e082c32023-07-11 07:41:50 +000070 Self::VirtIODriverOperationFailed(e) => {
71 write!(f, "Failed VirtIO driver operation: {e}")
72 }
Alice Wang748b0322023-07-24 12:51:18 +000073 Self::SerializationFailed(e) => write!(f, "Failed to serialize: {e}"),
74 Self::DeserializationFailed(e) => write!(f, "Failed to deserialize: {e}"),
Alice Wang9a8b39f2023-04-12 15:31:48 +000075 }
76 }
77}
78
Alice Wang90e6f162023-04-17 13:49:45 +000079impl From<HypervisorError> for Error {
80 fn from(e: HypervisorError) -> Self {
81 Self::Hypervisor(e)
Alice Wang9a8b39f2023-04-12 15:31:48 +000082 }
83}
84
85impl From<MapError> for Error {
86 fn from(e: MapError) -> Self {
87 Self::PageTableMapping(e)
88 }
89}
Alice Wangdda3ba92023-05-25 15:15:30 +000090
91impl From<FdtError> for Error {
92 fn from(e: FdtError) -> Self {
93 Self::InvalidFdt(e)
94 }
95}
96
97impl From<PciError> for Error {
98 fn from(e: PciError) -> Self {
99 Self::InvalidPci(e)
100 }
101}
Alice Wang74f7f4b2023-06-13 08:24:50 +0000102
103impl From<MemoryTrackerError> for Error {
104 fn from(e: MemoryTrackerError) -> Self {
105 Self::MemoryOperationFailed(e)
106 }
107}
Alice Wang4e082c32023-07-11 07:41:50 +0000108
109impl From<virtio_drivers::Error> for Error {
110 fn from(e: virtio_drivers::Error) -> Self {
111 Self::VirtIODriverOperationFailed(e)
112 }
113}
Alice Wang748b0322023-07-24 12:51:18 +0000114
115impl From<CiboriumSerError> for Error {
116 fn from(e: CiboriumSerError) -> Self {
117 Self::SerializationFailed(e)
118 }
119}
120
121impl From<CiboriumDeError> for Error {
122 fn from(e: CiboriumDeError) -> Self {
123 Self::DeserializationFailed(e)
124 }
125}