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