blob: 84dac64c26288c2373499aeb8c5d120f9d4cf3ab [file] [log] [blame]
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +01001// 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
Alice Wang31329112023-04-13 09:02:36 +000017use crate::hypervisor::get_hypervisor;
Alice Wang0bdc3f62023-03-15 10:46:12 +000018use crate::util::{page_address, SIZE_4KB};
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010019use core::{fmt, result};
20
Alice Wang0bdc3f62023-03-15 10:46:12 +000021/// MMIO guard error.
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010022#[derive(Debug, Clone)]
23pub enum Error {
Pierre-Clément Tosib579eee2022-10-19 17:32:53 +010024 /// Failed the necessary MMIO_GUARD_ENROLL call.
25 EnrollFailed(smccc::Error),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010026 /// Failed to obtain the MMIO_GUARD granule size.
Alice Wang31329112023-04-13 09:02:36 +000027 GranuleQueryFailed(smccc::Error),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010028 /// Failed to MMIO_GUARD_MAP a page.
29 MapFailed(smccc::Error),
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010030 /// Failed to MMIO_GUARD_UNMAP a page.
31 UnmapFailed(smccc::Error),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010032 /// The MMIO_GUARD granule used by the hypervisor is not supported.
33 UnsupportedGranule(usize),
34}
35
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010036impl fmt::Display for Error {
37 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38 match self {
Pierre-Clément Tosib579eee2022-10-19 17:32:53 +010039 Self::EnrollFailed(e) => write!(f, "Failed to enroll into MMIO_GUARD: {e}"),
Alice Wang31329112023-04-13 09:02:36 +000040 Self::GranuleQueryFailed(e) => write!(f, "Failed to get the MMIO_GUARD granule: {e}"),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010041 Self::MapFailed(e) => write!(f, "Failed to MMIO_GUARD map: {e}"),
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010042 Self::UnmapFailed(e) => write!(f, "Failed to MMIO_GUARD unmap: {e}"),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010043 Self::UnsupportedGranule(g) => write!(f, "Unsupported MMIO_GUARD granule: {g}"),
44 }
45 }
46}
47
Alice Wang0bdc3f62023-03-15 10:46:12 +000048/// Result type with mmio_guard::Error.
49pub type Result<T> = result::Result<T, Error>;
50
51/// Initializes the hypervisor by enrolling a MMIO guard and checking the memory granule size.
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010052pub fn init() -> Result<()> {
Alice Wang31329112023-04-13 09:02:36 +000053 let hyp = get_hypervisor();
54 hyp.mmio_guard_enroll().map_err(Error::EnrollFailed)?;
55 let mmio_granule = hyp.mmio_guard_granule().map_err(Error::GranuleQueryFailed)?;
Alice Wang0bdc3f62023-03-15 10:46:12 +000056 if mmio_granule != SIZE_4KB {
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010057 return Err(Error::UnsupportedGranule(mmio_granule));
58 }
59 Ok(())
60}
61
Alice Wang0bdc3f62023-03-15 10:46:12 +000062/// Maps a memory address to the hypervisor MMIO guard.
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010063pub fn map(addr: usize) -> Result<()> {
Alice Wang31329112023-04-13 09:02:36 +000064 get_hypervisor().mmio_guard_map(page_address(addr)).map_err(Error::MapFailed)
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010065}
66
Alice Wang0bdc3f62023-03-15 10:46:12 +000067/// Unmaps a memory address from the hypervisor MMIO guard.
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010068pub fn unmap(addr: usize) -> Result<()> {
Alice Wang31329112023-04-13 09:02:36 +000069 get_hypervisor().mmio_guard_unmap(page_address(addr)).map_err(Error::UnmapFailed)
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010070}