1 // 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 
17 use aarch64_paging::MapError;
18 use core::{fmt, result};
19 use diced_open_dice::DiceError;
20 use fdtpci::PciError;
21 use libfdt::FdtError;
22 use service_vm_comm::RequestProcessingError;
23 use vmbase::{hyp::Error as HypervisorError, memory::MemoryTrackerError, virtio::pci};
24 
25 pub type Result<T> = result::Result<T, Error>;
26 
27 type CiboriumSerError = ciborium::ser::Error<virtio_drivers::Error>;
28 type CiboriumDeError = ciborium::de::Error<virtio_drivers::Error>;
29 
30 #[derive(Debug)]
31 pub enum Error {
32     /// Hypervisor error.
33     Hypervisor(HypervisorError),
34     /// Failed when attempting to map some range in the page table.
35     PageTableMapping(MapError),
36     /// Invalid FDT.
37     InvalidFdt(FdtError),
38     /// Invalid PCI.
39     InvalidPci(PciError),
40     /// Failed memory operation.
41     MemoryOperationFailed(MemoryTrackerError),
42     /// Failed to initialize PCI.
43     PciInitializationFailed(pci::PciError),
44     /// Failed to create VirtIO Socket device.
45     VirtIOSocketCreationFailed(virtio_drivers::Error),
46     /// Missing socket device.
47     MissingVirtIOSocketDevice,
48     /// Failed VirtIO driver operation.
49     VirtIODriverOperationFailed(virtio_drivers::Error),
50     /// Failed to serialize.
51     SerializationFailed(CiboriumSerError),
52     /// Failed to deserialize.
53     DeserializationFailed(CiboriumDeError),
54     /// Failed DICE operation.
55     DiceOperationFailed(DiceError),
56     /// Failed to process request.
57     RequestProcessingFailed(RequestProcessingError),
58 }
59 
60 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result61     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62         match self {
63             Self::Hypervisor(e) => write!(f, "Hypervisor error: {e}."),
64             Self::PageTableMapping(e) => {
65                 write!(f, "Failed when attempting to map some range in the page table: {e}.")
66             }
67             Self::InvalidFdt(e) => write!(f, "Invalid FDT: {e}"),
68             Self::InvalidPci(e) => write!(f, "Invalid PCI: {e}"),
69             Self::MemoryOperationFailed(e) => write!(f, "Failed memory operation: {e}"),
70             Self::PciInitializationFailed(e) => write!(f, "Failed to initialize PCI: {e}"),
71             Self::VirtIOSocketCreationFailed(e) => {
72                 write!(f, "Failed to create VirtIO Socket device: {e}")
73             }
74             Self::MissingVirtIOSocketDevice => write!(f, "Missing VirtIO Socket device."),
75             Self::VirtIODriverOperationFailed(e) => {
76                 write!(f, "Failed VirtIO driver operation: {e}")
77             }
78             Self::SerializationFailed(e) => write!(f, "Failed to serialize: {e}"),
79             Self::DeserializationFailed(e) => write!(f, "Failed to deserialize: {e}"),
80             Self::DiceOperationFailed(e) => write!(f, "Failed DICE operation: {e}"),
81             Self::RequestProcessingFailed(e) => write!(f, "Failed to process request: {e}"),
82         }
83     }
84 }
85 
86 impl From<HypervisorError> for Error {
from(e: HypervisorError) -> Self87     fn from(e: HypervisorError) -> Self {
88         Self::Hypervisor(e)
89     }
90 }
91 
92 impl From<MapError> for Error {
from(e: MapError) -> Self93     fn from(e: MapError) -> Self {
94         Self::PageTableMapping(e)
95     }
96 }
97 
98 impl From<FdtError> for Error {
from(e: FdtError) -> Self99     fn from(e: FdtError) -> Self {
100         Self::InvalidFdt(e)
101     }
102 }
103 
104 impl From<PciError> for Error {
from(e: PciError) -> Self105     fn from(e: PciError) -> Self {
106         Self::InvalidPci(e)
107     }
108 }
109 
110 impl From<MemoryTrackerError> for Error {
from(e: MemoryTrackerError) -> Self111     fn from(e: MemoryTrackerError) -> Self {
112         Self::MemoryOperationFailed(e)
113     }
114 }
115 
116 impl From<virtio_drivers::Error> for Error {
from(e: virtio_drivers::Error) -> Self117     fn from(e: virtio_drivers::Error) -> Self {
118         Self::VirtIODriverOperationFailed(e)
119     }
120 }
121 
122 impl From<CiboriumSerError> for Error {
from(e: CiboriumSerError) -> Self123     fn from(e: CiboriumSerError) -> Self {
124         Self::SerializationFailed(e)
125     }
126 }
127 
128 impl From<CiboriumDeError> for Error {
from(e: CiboriumDeError) -> Self129     fn from(e: CiboriumDeError) -> Self {
130         Self::DeserializationFailed(e)
131     }
132 }
133 
134 impl From<DiceError> for Error {
from(e: DiceError) -> Self135     fn from(e: DiceError) -> Self {
136         Self::DiceOperationFailed(e)
137     }
138 }
139 
140 impl From<RequestProcessingError> for Error {
from(e: RequestProcessingError) -> Self141     fn from(e: RequestProcessingError) -> Self {
142         Self::RequestProcessingFailed(e)
143     }
144 }
145