blob: f44e26e81662b8f3d2f43adf2c5d40e7036d6981 [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
Pierre-Clément Tosia59103d2023-02-02 14:46:55 +000017pub mod trng;
18
Andrew Walbranba47d1d2022-12-14 15:21:44 +000019use log::info;
Alice Wang0c21f812023-04-12 12:22:46 +000020use smccc::{self, checked_hvc64, checked_hvc64_expect_zero};
Andrew Walbranba47d1d2022-12-14 15:21:44 +000021
Pierre-Clément Tosia59103d2023-02-02 14:46:55 +000022const ARM_SMCCC_TRNG_VERSION: u32 = 0x8400_0050;
23#[allow(dead_code)]
24const ARM_SMCCC_TRNG_FEATURES: u32 = 0x8400_0051;
25#[allow(dead_code)]
26const ARM_SMCCC_TRNG_GET_UUID: u32 = 0x8400_0052;
27#[allow(dead_code)]
28const ARM_SMCCC_TRNG_RND32: u32 = 0x8400_0053;
29const ARM_SMCCC_TRNG_RND64: u32 = 0xc400_0053;
Andrew Walbran41ebe932022-12-14 15:22:30 +000030const ARM_SMCCC_KVM_FUNC_HYP_MEMINFO: u32 = 0xc6000002;
31const ARM_SMCCC_KVM_FUNC_MEM_SHARE: u32 = 0xc6000003;
32const ARM_SMCCC_KVM_FUNC_MEM_UNSHARE: u32 = 0xc6000004;
Andrew Walbranba47d1d2022-12-14 15:21:44 +000033const VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID: u32 = 0xc6000005;
34const VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID: u32 = 0xc6000006;
35const VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID: u32 = 0xc6000007;
36const VENDOR_HYP_KVM_MMIO_GUARD_UNMAP_FUNC_ID: u32 = 0xc6000008;
37
Andrew Walbran41ebe932022-12-14 15:22:30 +000038/// Queries the memory protection parameters for a protected virtual machine.
39///
40/// Returns the memory protection granule size in bytes.
Pierre-Clément Tosi4ce55c02023-03-09 15:31:03 +000041pub fn kvm_hyp_meminfo() -> smccc::Result<u64> {
Andrew Walbran41ebe932022-12-14 15:22:30 +000042 let args = [0u64; 17];
43 checked_hvc64(ARM_SMCCC_KVM_FUNC_HYP_MEMINFO, args)
44}
45
46/// Shares a region of memory with the KVM host, granting it read, write and execute permissions.
47/// The size of the region is equal to the memory protection granule returned by [`hyp_meminfo`].
Pierre-Clément Tosi4ce55c02023-03-09 15:31:03 +000048pub fn kvm_mem_share(base_ipa: u64) -> smccc::Result<()> {
Andrew Walbran41ebe932022-12-14 15:22:30 +000049 let mut args = [0u64; 17];
50 args[0] = base_ipa;
51
52 checked_hvc64_expect_zero(ARM_SMCCC_KVM_FUNC_MEM_SHARE, args)
53}
54
55/// Revokes access permission from the KVM host to a memory region previously shared with
56/// [`mem_share`]. The size of the region is equal to the memory protection granule returned by
57/// [`hyp_meminfo`].
Pierre-Clément Tosi4ce55c02023-03-09 15:31:03 +000058pub fn kvm_mem_unshare(base_ipa: u64) -> smccc::Result<()> {
Andrew Walbran41ebe932022-12-14 15:22:30 +000059 let mut args = [0u64; 17];
60 args[0] = base_ipa;
61
62 checked_hvc64_expect_zero(ARM_SMCCC_KVM_FUNC_MEM_UNSHARE, args)
63}
64
Pierre-Clément Tosi4ce55c02023-03-09 15:31:03 +000065pub fn kvm_mmio_guard_info() -> smccc::Result<u64> {
Andrew Walbranba47d1d2022-12-14 15:21:44 +000066 let args = [0u64; 17];
67
68 checked_hvc64(VENDOR_HYP_KVM_MMIO_GUARD_INFO_FUNC_ID, args)
69}
70
Pierre-Clément Tosi4ce55c02023-03-09 15:31:03 +000071pub fn kvm_mmio_guard_enroll() -> smccc::Result<()> {
Andrew Walbranba47d1d2022-12-14 15:21:44 +000072 let args = [0u64; 17];
73
74 checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID, args)
75}
76
Pierre-Clément Tosi4ce55c02023-03-09 15:31:03 +000077pub fn kvm_mmio_guard_map(ipa: u64) -> smccc::Result<()> {
Andrew Walbranba47d1d2022-12-14 15:21:44 +000078 let mut args = [0u64; 17];
79 args[0] = ipa;
80
Alice Wang25063bc2023-04-12 07:55:14 +000081 // TODO(b/277859415): pKVM returns a i32 instead of a i64 in T.
82 // Drop this hack once T reaches EoL.
Andrew Walbranba47d1d2022-12-14 15:21:44 +000083 let is_i32_error_code = |n| u32::try_from(n).ok().filter(|v| (*v as i32) < 0).is_some();
84 match checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID, args) {
85 Err(smccc::Error::Unexpected(e)) if is_i32_error_code(e) => {
86 info!("Handled a pKVM bug by interpreting the MMIO_GUARD_MAP return value as i32");
87 match e as u32 as i32 {
88 -1 => Err(smccc::Error::NotSupported),
89 -2 => Err(smccc::Error::NotRequired),
90 -3 => Err(smccc::Error::InvalidParameter),
91 ret => Err(smccc::Error::Unknown(ret as i64)),
92 }
93 }
94 res => res,
95 }
96}
97
Pierre-Clément Tosi4ce55c02023-03-09 15:31:03 +000098pub fn kvm_mmio_guard_unmap(ipa: u64) -> smccc::Result<()> {
Andrew Walbranba47d1d2022-12-14 15:21:44 +000099 let mut args = [0u64; 17];
100 args[0] = ipa;
101
Alice Wang25063bc2023-04-12 07:55:14 +0000102 // TODO(b/277860860): pKVM returns NOT_SUPPORTED for SUCCESS in T.
103 // Drop this hack once T reaches EoL.
Andrew Walbranba47d1d2022-12-14 15:21:44 +0000104 match checked_hvc64_expect_zero(VENDOR_HYP_KVM_MMIO_GUARD_UNMAP_FUNC_ID, args) {
105 Err(smccc::Error::NotSupported) | Ok(_) => Ok(()),
106 x => x,
107 }
108}
Pierre-Clément Tosia59103d2023-02-02 14:46:55 +0000109
110/// Returns the (major, minor) version tuple, as defined by the SMCCC TRNG.
111pub fn trng_version() -> trng::Result<(u16, u16)> {
112 let args = [0u64; 17];
113
114 let version = trng::hvc64(ARM_SMCCC_TRNG_VERSION, args)?[0];
115 Ok(((version >> 16) as u16, version as u16))
116}
117
118pub type TrngRng64Entropy = (u64, u64, u64);
119
120pub fn trng_rnd64(nbits: u64) -> trng::Result<TrngRng64Entropy> {
121 let mut args = [0u64; 17];
122 args[0] = nbits;
123
124 let regs = trng::hvc64_expect_zero(ARM_SMCCC_TRNG_RND64, args)?;
125
126 Ok((regs[1], regs[2], regs[3]))
127}