blob: 2206052118af8a7a99896e91bbb595f3fbb40376 [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
17use crate::helpers;
18use crate::smccc;
19use core::{fmt, result};
20
21#[derive(Debug, Clone)]
22pub enum Error {
Pierre-Clément Tosib579eee2022-10-19 17:32:53 +010023 /// Failed the necessary MMIO_GUARD_ENROLL call.
24 EnrollFailed(smccc::Error),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010025 /// Failed to obtain the MMIO_GUARD granule size.
26 InfoFailed(smccc::Error),
27 /// Failed to MMIO_GUARD_MAP a page.
28 MapFailed(smccc::Error),
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010029 /// Failed to MMIO_GUARD_UNMAP a page.
30 UnmapFailed(smccc::Error),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010031 /// The MMIO_GUARD granule used by the hypervisor is not supported.
32 UnsupportedGranule(usize),
33}
34
35type Result<T> = result::Result<T, Error>;
36
37impl fmt::Display for Error {
38 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 match self {
Pierre-Clément Tosib579eee2022-10-19 17:32:53 +010040 Self::EnrollFailed(e) => write!(f, "Failed to enroll into MMIO_GUARD: {e}"),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010041 Self::InfoFailed(e) => write!(f, "Failed to get the MMIO_GUARD granule: {e}"),
42 Self::MapFailed(e) => write!(f, "Failed to MMIO_GUARD map: {e}"),
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010043 Self::UnmapFailed(e) => write!(f, "Failed to MMIO_GUARD unmap: {e}"),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010044 Self::UnsupportedGranule(g) => write!(f, "Unsupported MMIO_GUARD granule: {g}"),
45 }
46 }
47}
48
49pub fn init() -> Result<()> {
Pierre-Clément Tosib579eee2022-10-19 17:32:53 +010050 mmio_guard_enroll().map_err(Error::EnrollFailed)?;
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010051 let mmio_granule = mmio_guard_info().map_err(Error::InfoFailed)? as usize;
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010052 if mmio_granule != helpers::SIZE_4KB {
53 return Err(Error::UnsupportedGranule(mmio_granule));
54 }
55 Ok(())
56}
57
58pub fn map(addr: usize) -> Result<()> {
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010059 mmio_guard_map(helpers::page_4kb_of(addr) as u64).map_err(Error::MapFailed)
60}
61
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010062pub fn unmap(addr: usize) -> Result<()> {
63 mmio_guard_unmap(helpers::page_4kb_of(addr) as u64).map_err(Error::UnmapFailed)
64}
65
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010066fn mmio_guard_info() -> smccc::Result<u64> {
67 const VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID: u32 = 0xc6000005;
68 let args = [0u64; 17];
69
70 smccc::checked_hvc64(VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID, args)
71}
72
Pierre-Clément Tosib579eee2022-10-19 17:32:53 +010073fn mmio_guard_enroll() -> smccc::Result<()> {
74 const VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID: u32 = 0xc6000006;
75 let args = [0u64; 17];
76
77 smccc::checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID, args)
78}
79
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010080fn mmio_guard_map(ipa: u64) -> smccc::Result<()> {
81 const VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID: u32 = 0xc6000007;
82 let mut args = [0u64; 17];
83 args[0] = ipa;
84
85 smccc::checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID, args)
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010086}
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010087
88fn mmio_guard_unmap(ipa: u64) -> smccc::Result<()> {
89 const VENDOR_HYP_KVM_MMIO_GUARD_UNMAP_FUNC_ID: u32 = 0xc6000008;
90 let mut args = [0u64; 17];
91 args[0] = ipa;
92
93 smccc::checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_UNMAP_FUNC_ID, args)
94}