blob: 66f7977af41740e429fa534c649e3c4dac79cda3 [file] [log] [blame]
Andrew Walbranba47d1d2022-12-14 15:21:44 +00001// 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//! Wrappers around calls to the hypervisor.
16
17use crate::smccc::{self, checked_hvc64, checked_hvc64_expect_zero};
18use log::info;
19
20const VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID: u32 = 0xc6000005;
21const VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID: u32 = 0xc6000006;
22const VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID: u32 = 0xc6000007;
23const VENDOR_HYP_KVM_MMIO_GUARD_UNMAP_FUNC_ID: u32 = 0xc6000008;
24
25pub fn mmio_guard_info() -> smccc::Result<u64> {
26 let args = [0u64; 17];
27
28 checked_hvc64(VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID, args)
29}
30
31pub fn mmio_guard_enroll() -> smccc::Result<()> {
32 let args = [0u64; 17];
33
34 checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID, args)
35}
36
37pub fn mmio_guard_map(ipa: u64) -> smccc::Result<()> {
38 let mut args = [0u64; 17];
39 args[0] = ipa;
40
41 // TODO(b/253586500): pKVM currently returns a i32 instead of a i64.
42 let is_i32_error_code = |n| u32::try_from(n).ok().filter(|v| (*v as i32) < 0).is_some();
43 match checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID, args) {
44 Err(smccc::Error::Unexpected(e)) if is_i32_error_code(e) => {
45 info!("Handled a pKVM bug by interpreting the MMIO_GUARD_MAP return value as i32");
46 match e as u32 as i32 {
47 -1 => Err(smccc::Error::NotSupported),
48 -2 => Err(smccc::Error::NotRequired),
49 -3 => Err(smccc::Error::InvalidParameter),
50 ret => Err(smccc::Error::Unknown(ret as i64)),
51 }
52 }
53 res => res,
54 }
55}
56
57pub fn mmio_guard_unmap(ipa: u64) -> smccc::Result<()> {
58 let mut args = [0u64; 17];
59 args[0] = ipa;
60
61 // TODO(b/251426790): pKVM currently returns NOT_SUPPORTED for SUCCESS.
62 match checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_UNMAP_FUNC_ID, args) {
63 Err(smccc::Error::NotSupported) | Ok(_) => Ok(()),
64 x => x,
65 }
66}