Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 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}; |
Alice Wang | 474c0ee | 2023-09-14 12:52:33 +0000 | [diff] [blame] | 19 | use diced_open_dice::DiceError; |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 20 | use fdtpci::PciError; |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 21 | use libfdt::FdtError; |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 22 | use service_vm_comm::RequestProcessingError; |
Pierre-Clément Tosi | a9b345f | 2024-04-27 01:01:42 +0100 | [diff] [blame] | 23 | use vmbase::{hyp::Error as HypervisorError, memory::MemoryTrackerError, virtio::pci}; |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 24 | |
| 25 | pub type Result<T> = result::Result<T, Error>; |
| 26 | |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 27 | type CiboriumSerError = ciborium::ser::Error<virtio_drivers::Error>; |
| 28 | type CiboriumDeError = ciborium::de::Error<virtio_drivers::Error>; |
| 29 | |
| 30 | #[derive(Debug)] |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 31 | pub enum Error { |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 32 | /// Hypervisor error. |
| 33 | Hypervisor(HypervisorError), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 34 | /// Failed when attempting to map some range in the page table. |
| 35 | PageTableMapping(MapError), |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 36 | /// Invalid FDT. |
| 37 | InvalidFdt(FdtError), |
| 38 | /// Invalid PCI. |
| 39 | InvalidPci(PciError), |
Alice Wang | 74f7f4b | 2023-06-13 08:24:50 +0000 | [diff] [blame] | 40 | /// Failed memory operation. |
| 41 | MemoryOperationFailed(MemoryTrackerError), |
Alice Wang | d36c711 | 2023-07-04 09:50:45 +0000 | [diff] [blame] | 42 | /// Failed to initialize PCI. |
| 43 | PciInitializationFailed(pci::PciError), |
Alice Wang | 6218335 | 2023-07-04 08:33:27 +0000 | [diff] [blame] | 44 | /// Failed to create VirtIO Socket device. |
| 45 | VirtIOSocketCreationFailed(virtio_drivers::Error), |
| 46 | /// Missing socket device. |
| 47 | MissingVirtIOSocketDevice, |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 48 | /// Failed VirtIO driver operation. |
| 49 | VirtIODriverOperationFailed(virtio_drivers::Error), |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 50 | /// Failed to serialize. |
| 51 | SerializationFailed(CiboriumSerError), |
| 52 | /// Failed to deserialize. |
| 53 | DeserializationFailed(CiboriumDeError), |
Alice Wang | 474c0ee | 2023-09-14 12:52:33 +0000 | [diff] [blame] | 54 | /// Failed DICE operation. |
| 55 | DiceOperationFailed(DiceError), |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 56 | /// Failed to process request. |
| 57 | RequestProcessingFailed(RequestProcessingError), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | impl fmt::Display for Error { |
| 61 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 62 | match self { |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 63 | Self::Hypervisor(e) => write!(f, "Hypervisor error: {e}."), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 64 | Self::PageTableMapping(e) => { |
| 65 | write!(f, "Failed when attempting to map some range in the page table: {e}.") |
| 66 | } |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 67 | Self::InvalidFdt(e) => write!(f, "Invalid FDT: {e}"), |
| 68 | Self::InvalidPci(e) => write!(f, "Invalid PCI: {e}"), |
Alice Wang | 74f7f4b | 2023-06-13 08:24:50 +0000 | [diff] [blame] | 69 | Self::MemoryOperationFailed(e) => write!(f, "Failed memory operation: {e}"), |
Alice Wang | d36c711 | 2023-07-04 09:50:45 +0000 | [diff] [blame] | 70 | Self::PciInitializationFailed(e) => write!(f, "Failed to initialize PCI: {e}"), |
Alice Wang | 6218335 | 2023-07-04 08:33:27 +0000 | [diff] [blame] | 71 | Self::VirtIOSocketCreationFailed(e) => { |
| 72 | write!(f, "Failed to create VirtIO Socket device: {e}") |
| 73 | } |
| 74 | Self::MissingVirtIOSocketDevice => write!(f, "Missing VirtIO Socket device."), |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 75 | Self::VirtIODriverOperationFailed(e) => { |
| 76 | write!(f, "Failed VirtIO driver operation: {e}") |
| 77 | } |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 78 | Self::SerializationFailed(e) => write!(f, "Failed to serialize: {e}"), |
| 79 | Self::DeserializationFailed(e) => write!(f, "Failed to deserialize: {e}"), |
Alice Wang | 474c0ee | 2023-09-14 12:52:33 +0000 | [diff] [blame] | 80 | Self::DiceOperationFailed(e) => write!(f, "Failed DICE operation: {e}"), |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 81 | Self::RequestProcessingFailed(e) => write!(f, "Failed to process request: {e}"), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 86 | impl From<HypervisorError> for Error { |
| 87 | fn from(e: HypervisorError) -> Self { |
| 88 | Self::Hypervisor(e) |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | impl From<MapError> for Error { |
| 93 | fn from(e: MapError) -> Self { |
| 94 | Self::PageTableMapping(e) |
| 95 | } |
| 96 | } |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 97 | |
| 98 | impl From<FdtError> for Error { |
| 99 | fn from(e: FdtError) -> Self { |
| 100 | Self::InvalidFdt(e) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | impl From<PciError> for Error { |
| 105 | fn from(e: PciError) -> Self { |
| 106 | Self::InvalidPci(e) |
| 107 | } |
| 108 | } |
Alice Wang | 74f7f4b | 2023-06-13 08:24:50 +0000 | [diff] [blame] | 109 | |
| 110 | impl From<MemoryTrackerError> for Error { |
| 111 | fn from(e: MemoryTrackerError) -> Self { |
| 112 | Self::MemoryOperationFailed(e) |
| 113 | } |
| 114 | } |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 115 | |
| 116 | impl From<virtio_drivers::Error> for Error { |
| 117 | fn from(e: virtio_drivers::Error) -> Self { |
| 118 | Self::VirtIODriverOperationFailed(e) |
| 119 | } |
| 120 | } |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 121 | |
| 122 | impl From<CiboriumSerError> for Error { |
| 123 | fn from(e: CiboriumSerError) -> Self { |
| 124 | Self::SerializationFailed(e) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl From<CiboriumDeError> for Error { |
| 129 | fn from(e: CiboriumDeError) -> Self { |
| 130 | Self::DeserializationFailed(e) |
| 131 | } |
| 132 | } |
Alice Wang | 474c0ee | 2023-09-14 12:52:33 +0000 | [diff] [blame] | 133 | |
| 134 | impl From<DiceError> for Error { |
| 135 | fn from(e: DiceError) -> Self { |
| 136 | Self::DiceOperationFailed(e) |
| 137 | } |
| 138 | } |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 139 | |
| 140 | impl From<RequestProcessingError> for Error { |
| 141 | fn from(e: RequestProcessingError) -> Self { |
| 142 | Self::RequestProcessingFailed(e) |
| 143 | } |
| 144 | } |