blob: 28f928fb6c2adfdad288886cdef3b905f35d245c [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};
Pierre-Clément Tosif9c6a9b2022-10-19 22:04:39 +010020use log::info;
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010021
22#[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
36type Result<T> = result::Result<T, Error>;
37
38impl fmt::Display for Error {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 match self {
Pierre-Clément Tosib579eee2022-10-19 17:32:53 +010041 Self::EnrollFailed(e) => write!(f, "Failed to enroll into MMIO_GUARD: {e}"),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010042 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 Tosia99bfa62022-10-06 13:30:52 +010044 Self::UnmapFailed(e) => write!(f, "Failed to MMIO_GUARD unmap: {e}"),
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010045 Self::UnsupportedGranule(g) => write!(f, "Unsupported MMIO_GUARD granule: {g}"),
46 }
47 }
48}
49
50pub fn init() -> Result<()> {
Pierre-Clément Tosib579eee2022-10-19 17:32:53 +010051 mmio_guard_enroll().map_err(Error::EnrollFailed)?;
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010052 let mmio_granule = mmio_guard_info().map_err(Error::InfoFailed)? as usize;
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010053 if mmio_granule != helpers::SIZE_4KB {
54 return Err(Error::UnsupportedGranule(mmio_granule));
55 }
56 Ok(())
57}
58
59pub fn map(addr: usize) -> Result<()> {
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010060 mmio_guard_map(helpers::page_4kb_of(addr) as u64).map_err(Error::MapFailed)
61}
62
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010063pub fn unmap(addr: usize) -> Result<()> {
64 mmio_guard_unmap(helpers::page_4kb_of(addr) as u64).map_err(Error::UnmapFailed)
65}
66
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010067fn mmio_guard_info() -> smccc::Result<u64> {
68 const VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID: u32 = 0xc6000005;
69 let args = [0u64; 17];
70
71 smccc::checked_hvc64(VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID, args)
72}
73
Pierre-Clément Tosib579eee2022-10-19 17:32:53 +010074fn mmio_guard_enroll() -> smccc::Result<()> {
75 const VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID: u32 = 0xc6000006;
76 let args = [0u64; 17];
77
78 smccc::checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID, args)
79}
80
Pierre-Clément Tosi6c4c4f72022-10-21 16:44:16 +010081fn mmio_guard_map(ipa: u64) -> smccc::Result<()> {
82 const VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID: u32 = 0xc6000007;
83 let mut args = [0u64; 17];
84 args[0] = ipa;
85
Pierre-Clément Tosidf664982022-10-19 16:50:35 +010086 // TODO(b/253586500): pKVM currently returns a i32 instead of a i64.
87 let is_i32_error_code = |n| u32::try_from(n).ok().filter(|v| (*v as i32) < 0).is_some();
88 match smccc::checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID, args) {
89 Err(smccc::Error::Unexpected(e)) if is_i32_error_code(e) => {
90 info!("Handled a pKVM bug by interpreting the MMIO_GUARD_MAP return value as i32");
91 match e as u32 as i32 {
92 -1 => Err(smccc::Error::NotSupported),
93 -2 => Err(smccc::Error::NotRequired),
94 -3 => Err(smccc::Error::InvalidParameter),
95 ret => Err(smccc::Error::Unknown(ret as i64)),
96 }
97 }
98 res => res,
99 }
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +0100100}
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +0100101
102fn mmio_guard_unmap(ipa: u64) -> smccc::Result<()> {
103 const VENDOR_HYP_KVM_MMIO_GUARD_UNMAP_FUNC_ID: u32 = 0xc6000008;
104 let mut args = [0u64; 17];
105 args[0] = ipa;
106
Pierre-Clément Tosif9c6a9b2022-10-19 22:04:39 +0100107 // TODO(b/251426790): pKVM currently returns NOT_SUPPORTED for SUCCESS.
Pierre-Clément Tosif9c6a9b2022-10-19 22:04:39 +0100108 match smccc::checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_UNMAP_FUNC_ID, args) {
109 Err(smccc::Error::NotSupported) | Ok(_) => Ok(()),
110 x => x,
111 }
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +0100112}