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; |
| 18 | use crate::smccc; |
| 19 | use core::{fmt, result}; |
| 20 | |
| 21 | #[derive(Debug, Clone)] |
| 22 | pub enum Error { |
Pierre-Clément Tosi | b579eee | 2022-10-19 17:32:53 +0100 | [diff] [blame] | 23 | /// Failed the necessary MMIO_GUARD_ENROLL call. |
| 24 | EnrollFailed(smccc::Error), |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 25 | /// Failed to obtain the MMIO_GUARD granule size. |
| 26 | InfoFailed(smccc::Error), |
| 27 | /// Failed to MMIO_GUARD_MAP a page. |
| 28 | MapFailed(smccc::Error), |
| 29 | /// The MMIO_GUARD granule used by the hypervisor is not supported. |
| 30 | UnsupportedGranule(usize), |
| 31 | } |
| 32 | |
| 33 | type Result<T> = result::Result<T, Error>; |
| 34 | |
| 35 | impl fmt::Display for Error { |
| 36 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 37 | match self { |
Pierre-Clément Tosi | b579eee | 2022-10-19 17:32:53 +0100 | [diff] [blame] | 38 | 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] | 39 | Self::InfoFailed(e) => write!(f, "Failed to get the MMIO_GUARD granule: {e}"), |
| 40 | Self::MapFailed(e) => write!(f, "Failed to MMIO_GUARD map: {e}"), |
| 41 | Self::UnsupportedGranule(g) => write!(f, "Unsupported MMIO_GUARD granule: {g}"), |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub fn init() -> Result<()> { |
Pierre-Clément Tosi | b579eee | 2022-10-19 17:32:53 +0100 | [diff] [blame] | 47 | mmio_guard_enroll().map_err(Error::EnrollFailed)?; |
Pierre-Clément Tosi | 6c4c4f7 | 2022-10-21 16:44:16 +0100 | [diff] [blame] | 48 | 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] | 49 | if mmio_granule != helpers::SIZE_4KB { |
| 50 | return Err(Error::UnsupportedGranule(mmio_granule)); |
| 51 | } |
| 52 | Ok(()) |
| 53 | } |
| 54 | |
| 55 | pub fn map(addr: usize) -> Result<()> { |
Pierre-Clément Tosi | 6c4c4f7 | 2022-10-21 16:44:16 +0100 | [diff] [blame] | 56 | mmio_guard_map(helpers::page_4kb_of(addr) as u64).map_err(Error::MapFailed) |
| 57 | } |
| 58 | |
| 59 | fn mmio_guard_info() -> smccc::Result<u64> { |
| 60 | const VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID: u32 = 0xc6000005; |
| 61 | let args = [0u64; 17]; |
| 62 | |
| 63 | smccc::checked_hvc64(VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID, args) |
| 64 | } |
| 65 | |
Pierre-Clément Tosi | b579eee | 2022-10-19 17:32:53 +0100 | [diff] [blame] | 66 | fn mmio_guard_enroll() -> smccc::Result<()> { |
| 67 | const VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID: u32 = 0xc6000006; |
| 68 | let args = [0u64; 17]; |
| 69 | |
| 70 | smccc::checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID, args) |
| 71 | } |
| 72 | |
Pierre-Clément Tosi | 6c4c4f7 | 2022-10-21 16:44:16 +0100 | [diff] [blame] | 73 | fn mmio_guard_map(ipa: u64) -> smccc::Result<()> { |
| 74 | const VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID: u32 = 0xc6000007; |
| 75 | let mut args = [0u64; 17]; |
| 76 | args[0] = ipa; |
| 77 | |
| 78 | smccc::checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID, args) |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 79 | } |