Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [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 | //! Safe MMIO_GUARD support. |
| 16 | |
| 17 | use crate::helpers; |
Pierre-Clément Tosi | 4ce55c0 | 2023-03-09 15:31:03 +0000 | [diff] [blame] | 18 | use crate::hypervisor::{mmio_guard_enroll, mmio_guard_info, mmio_guard_map, mmio_guard_unmap}; |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 19 | use crate::smccc; |
| 20 | use core::{fmt, result}; |
| 21 | |
| 22 | #[derive(Debug, Clone)] |
| 23 | pub enum Error { |
Pierre-Clément Tosi | b579eee | 2022-10-19 17:32:53 +0100 | [diff] [blame] | 24 | /// Failed the necessary MMIO_GUARD_ENROLL call. |
| 25 | EnrollFailed(smccc::Error), |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 26 | /// Failed to obtain the MMIO_GUARD granule size. |
| 27 | InfoFailed(smccc::Error), |
| 28 | /// Failed to MMIO_GUARD_MAP a page. |
| 29 | MapFailed(smccc::Error), |
Pierre-Clément Tosi | a99bfa6 | 2022-10-06 13:30:52 +0100 | [diff] [blame] | 30 | /// Failed to MMIO_GUARD_UNMAP a page. |
| 31 | UnmapFailed(smccc::Error), |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 32 | /// The MMIO_GUARD granule used by the hypervisor is not supported. |
| 33 | UnsupportedGranule(usize), |
| 34 | } |
| 35 | |
| 36 | type Result<T> = result::Result<T, Error>; |
| 37 | |
| 38 | impl fmt::Display for Error { |
| 39 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 40 | match self { |
Pierre-Clément Tosi | b579eee | 2022-10-19 17:32:53 +0100 | [diff] [blame] | 41 | Self::EnrollFailed(e) => write!(f, "Failed to enroll into MMIO_GUARD: {e}"), |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 42 | Self::InfoFailed(e) => write!(f, "Failed to get the MMIO_GUARD granule: {e}"), |
| 43 | Self::MapFailed(e) => write!(f, "Failed to MMIO_GUARD map: {e}"), |
Pierre-Clément Tosi | a99bfa6 | 2022-10-06 13:30:52 +0100 | [diff] [blame] | 44 | Self::UnmapFailed(e) => write!(f, "Failed to MMIO_GUARD unmap: {e}"), |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 45 | Self::UnsupportedGranule(g) => write!(f, "Unsupported MMIO_GUARD granule: {g}"), |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub fn init() -> Result<()> { |
Pierre-Clément Tosi | b579eee | 2022-10-19 17:32:53 +0100 | [diff] [blame] | 51 | mmio_guard_enroll().map_err(Error::EnrollFailed)?; |
Pierre-Clément Tosi | 6c4c4f7 | 2022-10-21 16:44:16 +0100 | [diff] [blame] | 52 | let mmio_granule = mmio_guard_info().map_err(Error::InfoFailed)? as usize; |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 53 | if mmio_granule != helpers::SIZE_4KB { |
| 54 | return Err(Error::UnsupportedGranule(mmio_granule)); |
| 55 | } |
| 56 | Ok(()) |
| 57 | } |
| 58 | |
| 59 | pub fn map(addr: usize) -> Result<()> { |
Pierre-Clément Tosi | 6c4c4f7 | 2022-10-21 16:44:16 +0100 | [diff] [blame] | 60 | mmio_guard_map(helpers::page_4kb_of(addr) as u64).map_err(Error::MapFailed) |
| 61 | } |
| 62 | |
Pierre-Clément Tosi | a99bfa6 | 2022-10-06 13:30:52 +0100 | [diff] [blame] | 63 | pub fn unmap(addr: usize) -> Result<()> { |
| 64 | mmio_guard_unmap(helpers::page_4kb_of(addr) as u64).map_err(Error::UnmapFailed) |
| 65 | } |