blob: 512eb88976b4bec7b187309c0d305572494f5e1a [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
Pierre-Clément Tosi4ce55c02023-03-09 15:31:03 +000017use crate::hypervisor::{mmio_guard_enroll, mmio_guard_info, mmio_guard_map, mmio_guard_unmap};
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.
27 InfoFailed(smccc::Error),
28 /// 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}"),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010040 Self::InfoFailed(e) => write!(f, "Failed to get the MMIO_GUARD granule: {e}"),
41 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<()> {
Pierre-Clément Tosib579eee2022-10-19 17:32:53 +010053 mmio_guard_enroll().map_err(Error::EnrollFailed)?;
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010054 let mmio_granule = mmio_guard_info().map_err(Error::InfoFailed)? as usize;
Alice Wang0bdc3f62023-03-15 10:46:12 +000055 if mmio_granule != SIZE_4KB {
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010056 return Err(Error::UnsupportedGranule(mmio_granule));
57 }
58 Ok(())
59}
60
Alice Wang0bdc3f62023-03-15 10:46:12 +000061/// Maps a memory address to the hypervisor MMIO guard.
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010062pub fn map(addr: usize) -> Result<()> {
Alice Wang0bdc3f62023-03-15 10:46:12 +000063 mmio_guard_map(page_address(addr)).map_err(Error::MapFailed)
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010064}
65
Alice Wang0bdc3f62023-03-15 10:46:12 +000066/// Unmaps a memory address from the hypervisor MMIO guard.
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010067pub fn unmap(addr: usize) -> Result<()> {
Alice Wang0bdc3f62023-03-15 10:46:12 +000068 mmio_guard_unmap(page_address(addr)).map_err(Error::UnmapFailed)
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010069}