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 | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 19 | use fdtpci::PciError; |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 20 | use hyp::Error as HypervisorError; |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 21 | use libfdt::FdtError; |
Alice Wang | d36c711 | 2023-07-04 09:50:45 +0000 | [diff] [blame] | 22 | use vmbase::{memory::MemoryTrackerError, virtio::pci}; |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 23 | |
| 24 | pub type Result<T> = result::Result<T, Error>; |
| 25 | |
| 26 | #[derive(Clone, Debug)] |
| 27 | pub enum Error { |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 28 | /// Hypervisor error. |
| 29 | Hypervisor(HypervisorError), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 30 | /// Failed when attempting to map some range in the page table. |
| 31 | PageTableMapping(MapError), |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 32 | /// Invalid FDT. |
| 33 | InvalidFdt(FdtError), |
| 34 | /// Invalid PCI. |
| 35 | InvalidPci(PciError), |
Alice Wang | 74f7f4b | 2023-06-13 08:24:50 +0000 | [diff] [blame] | 36 | /// Failed memory operation. |
| 37 | MemoryOperationFailed(MemoryTrackerError), |
Alice Wang | d36c711 | 2023-07-04 09:50:45 +0000 | [diff] [blame] | 38 | /// Failed to initialize PCI. |
| 39 | PciInitializationFailed(pci::PciError), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | impl fmt::Display for Error { |
| 43 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 44 | match self { |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 45 | Self::Hypervisor(e) => write!(f, "Hypervisor error: {e}."), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 46 | Self::PageTableMapping(e) => { |
| 47 | write!(f, "Failed when attempting to map some range in the page table: {e}.") |
| 48 | } |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 49 | Self::InvalidFdt(e) => write!(f, "Invalid FDT: {e}"), |
| 50 | Self::InvalidPci(e) => write!(f, "Invalid PCI: {e}"), |
Alice Wang | 74f7f4b | 2023-06-13 08:24:50 +0000 | [diff] [blame] | 51 | Self::MemoryOperationFailed(e) => write!(f, "Failed memory operation: {e}"), |
Alice Wang | d36c711 | 2023-07-04 09:50:45 +0000 | [diff] [blame] | 52 | Self::PciInitializationFailed(e) => write!(f, "Failed to initialize PCI: {e}"), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 57 | impl From<HypervisorError> for Error { |
| 58 | fn from(e: HypervisorError) -> Self { |
| 59 | Self::Hypervisor(e) |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | impl From<MapError> for Error { |
| 64 | fn from(e: MapError) -> Self { |
| 65 | Self::PageTableMapping(e) |
| 66 | } |
| 67 | } |
Alice Wang | dda3ba9 | 2023-05-25 15:15:30 +0000 | [diff] [blame] | 68 | |
| 69 | impl From<FdtError> for Error { |
| 70 | fn from(e: FdtError) -> Self { |
| 71 | Self::InvalidFdt(e) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl From<PciError> for Error { |
| 76 | fn from(e: PciError) -> Self { |
| 77 | Self::InvalidPci(e) |
| 78 | } |
| 79 | } |
Alice Wang | 74f7f4b | 2023-06-13 08:24:50 +0000 | [diff] [blame] | 80 | |
| 81 | impl From<MemoryTrackerError> for Error { |
| 82 | fn from(e: MemoryTrackerError) -> Self { |
| 83 | Self::MemoryOperationFailed(e) |
| 84 | } |
| 85 | } |