blob: d2bdbbe1006dbd4684433728f0f0a01719868d28 [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 Wang474c0ee2023-09-14 12:52:33 +000019use diced_open_dice::DiceError;
Alice Wangdda3ba92023-05-25 15:15:30 +000020use fdtpci::PciError;
Alice Wang90e6f162023-04-17 13:49:45 +000021use hyp::Error as HypervisorError;
Alice Wangdda3ba92023-05-25 15:15:30 +000022use libfdt::FdtError;
Alice Wangd80e99e2023-09-15 13:26:01 +000023use service_vm_comm::RequestProcessingError;
Alice Wangd36c7112023-07-04 09:50:45 +000024use vmbase::{memory::MemoryTrackerError, virtio::pci};
Alice Wang9a8b39f2023-04-12 15:31:48 +000025
26pub type Result<T> = result::Result<T, Error>;
27
Alice Wang748b0322023-07-24 12:51:18 +000028type CiboriumSerError = ciborium::ser::Error<virtio_drivers::Error>;
29type CiboriumDeError = ciborium::de::Error<virtio_drivers::Error>;
30
31#[derive(Debug)]
Alice Wang9a8b39f2023-04-12 15:31:48 +000032pub enum Error {
Alice Wang90e6f162023-04-17 13:49:45 +000033 /// Hypervisor error.
34 Hypervisor(HypervisorError),
Alice Wang9a8b39f2023-04-12 15:31:48 +000035 /// Failed when attempting to map some range in the page table.
36 PageTableMapping(MapError),
Alice Wangdda3ba92023-05-25 15:15:30 +000037 /// Invalid FDT.
38 InvalidFdt(FdtError),
39 /// Invalid PCI.
40 InvalidPci(PciError),
Alice Wang74f7f4b2023-06-13 08:24:50 +000041 /// Failed memory operation.
42 MemoryOperationFailed(MemoryTrackerError),
Alice Wangd36c7112023-07-04 09:50:45 +000043 /// Failed to initialize PCI.
44 PciInitializationFailed(pci::PciError),
Alice Wang62183352023-07-04 08:33:27 +000045 /// Failed to create VirtIO Socket device.
46 VirtIOSocketCreationFailed(virtio_drivers::Error),
47 /// Missing socket device.
48 MissingVirtIOSocketDevice,
Alice Wang4e082c32023-07-11 07:41:50 +000049 /// Failed VirtIO driver operation.
50 VirtIODriverOperationFailed(virtio_drivers::Error),
Alice Wang748b0322023-07-24 12:51:18 +000051 /// Failed to serialize.
52 SerializationFailed(CiboriumSerError),
53 /// Failed to deserialize.
54 DeserializationFailed(CiboriumDeError),
Alice Wang474c0ee2023-09-14 12:52:33 +000055 /// Failed DICE operation.
56 DiceOperationFailed(DiceError),
Alice Wangd80e99e2023-09-15 13:26:01 +000057 /// Failed to process request.
58 RequestProcessingFailed(RequestProcessingError),
Alice Wang9a8b39f2023-04-12 15:31:48 +000059}
60
61impl fmt::Display for Error {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63 match self {
Alice Wangdda3ba92023-05-25 15:15:30 +000064 Self::Hypervisor(e) => write!(f, "Hypervisor error: {e}."),
Alice Wang9a8b39f2023-04-12 15:31:48 +000065 Self::PageTableMapping(e) => {
66 write!(f, "Failed when attempting to map some range in the page table: {e}.")
67 }
Alice Wangdda3ba92023-05-25 15:15:30 +000068 Self::InvalidFdt(e) => write!(f, "Invalid FDT: {e}"),
69 Self::InvalidPci(e) => write!(f, "Invalid PCI: {e}"),
Alice Wang74f7f4b2023-06-13 08:24:50 +000070 Self::MemoryOperationFailed(e) => write!(f, "Failed memory operation: {e}"),
Alice Wangd36c7112023-07-04 09:50:45 +000071 Self::PciInitializationFailed(e) => write!(f, "Failed to initialize PCI: {e}"),
Alice Wang62183352023-07-04 08:33:27 +000072 Self::VirtIOSocketCreationFailed(e) => {
73 write!(f, "Failed to create VirtIO Socket device: {e}")
74 }
75 Self::MissingVirtIOSocketDevice => write!(f, "Missing VirtIO Socket device."),
Alice Wang4e082c32023-07-11 07:41:50 +000076 Self::VirtIODriverOperationFailed(e) => {
77 write!(f, "Failed VirtIO driver operation: {e}")
78 }
Alice Wang748b0322023-07-24 12:51:18 +000079 Self::SerializationFailed(e) => write!(f, "Failed to serialize: {e}"),
80 Self::DeserializationFailed(e) => write!(f, "Failed to deserialize: {e}"),
Alice Wang474c0ee2023-09-14 12:52:33 +000081 Self::DiceOperationFailed(e) => write!(f, "Failed DICE operation: {e}"),
Alice Wangd80e99e2023-09-15 13:26:01 +000082 Self::RequestProcessingFailed(e) => write!(f, "Failed to process request: {e}"),
Alice Wang9a8b39f2023-04-12 15:31:48 +000083 }
84 }
85}
86
Alice Wang90e6f162023-04-17 13:49:45 +000087impl From<HypervisorError> for Error {
88 fn from(e: HypervisorError) -> Self {
89 Self::Hypervisor(e)
Alice Wang9a8b39f2023-04-12 15:31:48 +000090 }
91}
92
93impl From<MapError> for Error {
94 fn from(e: MapError) -> Self {
95 Self::PageTableMapping(e)
96 }
97}
Alice Wangdda3ba92023-05-25 15:15:30 +000098
99impl From<FdtError> for Error {
100 fn from(e: FdtError) -> Self {
101 Self::InvalidFdt(e)
102 }
103}
104
105impl From<PciError> for Error {
106 fn from(e: PciError) -> Self {
107 Self::InvalidPci(e)
108 }
109}
Alice Wang74f7f4b2023-06-13 08:24:50 +0000110
111impl From<MemoryTrackerError> for Error {
112 fn from(e: MemoryTrackerError) -> Self {
113 Self::MemoryOperationFailed(e)
114 }
115}
Alice Wang4e082c32023-07-11 07:41:50 +0000116
117impl From<virtio_drivers::Error> for Error {
118 fn from(e: virtio_drivers::Error) -> Self {
119 Self::VirtIODriverOperationFailed(e)
120 }
121}
Alice Wang748b0322023-07-24 12:51:18 +0000122
123impl From<CiboriumSerError> for Error {
124 fn from(e: CiboriumSerError) -> Self {
125 Self::SerializationFailed(e)
126 }
127}
128
129impl From<CiboriumDeError> for Error {
130 fn from(e: CiboriumDeError) -> Self {
131 Self::DeserializationFailed(e)
132 }
133}
Alice Wang474c0ee2023-09-14 12:52:33 +0000134
135impl From<DiceError> for Error {
136 fn from(e: DiceError) -> Self {
137 Self::DiceOperationFailed(e)
138 }
139}
Alice Wangd80e99e2023-09-15 13:26:01 +0000140
141impl From<RequestProcessingError> for Error {
142 fn from(e: RequestProcessingError) -> Self {
143 Self::RequestProcessingFailed(e)
144 }
145}