blob: 1b45f38952de8603f823451e4f220b9e491f247c [file] [log] [blame]
Alice Wang0bdc3f62023-03-15 10:46:12 +00001// Copyright 2023, 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 hypervisor back-ends.
16
Alice Wang31329112023-04-13 09:02:36 +000017mod common;
Yi-De Wu725bd922023-04-24 16:26:52 +080018mod geniezone;
Srivatsa Vaddagiricb5959e2023-04-20 05:27:16 -070019mod gunyah;
Alice Wang0bdc3f62023-03-15 10:46:12 +000020mod kvm;
21
Pierre-Clément Tosi000f5b42024-04-27 01:10:06 +010022use super::{Error, Result};
Srivatsa Vaddagiri353f5d02023-04-18 23:49:01 -070023use alloc::boxed::Box;
Pierre-Clément Tosid643cfe2023-06-29 09:30:51 +000024use common::Hypervisor;
Pierre-Clément Tosic08e63c2024-05-14 11:17:47 +010025pub use common::{DeviceAssigningHypervisor, MemSharingHypervisor, MmioGuardedHypervisor};
Yi-De Wu725bd922023-04-24 16:26:52 +080026pub use geniezone::GeniezoneError;
27use geniezone::GeniezoneHypervisor;
Srivatsa Vaddagiricb5959e2023-04-20 05:27:16 -070028use gunyah::GunyahHypervisor;
Andrew Walbran9afab672023-04-17 14:26:23 +000029pub use kvm::KvmError;
Pierre-Clément Tosibd98d922023-06-29 14:17:25 +000030use kvm::{ProtectedKvmHypervisor, RegularKvmHypervisor};
Srivatsa Vaddagiri353f5d02023-04-18 23:49:01 -070031use once_cell::race::OnceBox;
Andrew Walbranf44f1602023-05-30 14:59:19 +000032use smccc::hvc64;
Srivatsa Vaddagiri353f5d02023-04-18 23:49:01 -070033use uuid::Uuid;
Alice Wang31329112023-04-13 09:02:36 +000034
35enum HypervisorBackend {
Pierre-Clément Tosibd98d922023-06-29 14:17:25 +000036 RegularKvm,
Srivatsa Vaddagiricb5959e2023-04-20 05:27:16 -070037 Gunyah,
Yi-De Wu725bd922023-04-24 16:26:52 +080038 Geniezone,
Pierre-Clément Tosibd98d922023-06-29 14:17:25 +000039 ProtectedKvm,
Alice Wang0bdc3f62023-03-15 10:46:12 +000040}
41
Alice Wang31329112023-04-13 09:02:36 +000042impl HypervisorBackend {
43 fn get_hypervisor(&self) -> &'static dyn Hypervisor {
44 match self {
Pierre-Clément Tosibd98d922023-06-29 14:17:25 +000045 Self::RegularKvm => &RegularKvmHypervisor,
Srivatsa Vaddagiricb5959e2023-04-20 05:27:16 -070046 Self::Gunyah => &GunyahHypervisor,
Yi-De Wu725bd922023-04-24 16:26:52 +080047 Self::Geniezone => &GeniezoneHypervisor,
Pierre-Clément Tosibd98d922023-06-29 14:17:25 +000048 Self::ProtectedKvm => &ProtectedKvmHypervisor,
Alice Wang31329112023-04-13 09:02:36 +000049 }
50 }
Alice Wang0bdc3f62023-03-15 10:46:12 +000051}
52
Srivatsa Vaddagiri353f5d02023-04-18 23:49:01 -070053impl TryFrom<Uuid> for HypervisorBackend {
54 type Error = Error;
55
56 fn try_from(uuid: Uuid) -> Result<HypervisorBackend> {
57 match uuid {
Yi-De Wu725bd922023-04-24 16:26:52 +080058 GeniezoneHypervisor::UUID => Ok(HypervisorBackend::Geniezone),
Srivatsa Vaddagiricb5959e2023-04-20 05:27:16 -070059 GunyahHypervisor::UUID => Ok(HypervisorBackend::Gunyah),
Pierre-Clément Tosibd98d922023-06-29 14:17:25 +000060 RegularKvmHypervisor::UUID => {
Pierre-Clément Tosicf215852023-07-11 13:33:14 +000061 // Protected KVM has the same UUID as "regular" KVM so issue an HVC that is assumed
62 // to only be supported by pKVM: if it returns SUCCESS, deduce that this is pKVM
63 // and if it returns NOT_SUPPORTED assume that it is "regular" KVM.
64 match ProtectedKvmHypervisor.as_mmio_guard().unwrap().granule() {
Pierre-Clément Tosibd98d922023-06-29 14:17:25 +000065 Ok(_) => Ok(HypervisorBackend::ProtectedKvm),
66 Err(Error::KvmError(KvmError::NotSupported, _)) => {
67 Ok(HypervisorBackend::RegularKvm)
68 }
69 Err(e) => Err(e),
70 }
71 }
Srivatsa Vaddagiri353f5d02023-04-18 23:49:01 -070072 u => Err(Error::UnsupportedHypervisorUuid(u)),
73 }
74 }
75}
76
77const ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID: u32 = 0x8600ff01;
78
79fn query_vendor_hyp_call_uid() -> Uuid {
80 let args = [0u64; 17];
81 let res = hvc64(ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID, args);
82
83 // KVM's UUID of "28b46fb6-2ec5-11e9-a9ca-4b564d003a74" is generated by
84 // Uuid::from_u128() from an input value of
85 // 0x28b46fb6_2ec511e9_a9ca4b56_4d003a74. ARM's SMC calling convention
86 // (Document number ARM DEN 0028E) describes the UUID register mapping such
87 // that W0 contains bytes 0..3 of UUID, with byte 0 in lower order bits. In
88 // the KVM example, byte 0 of KVM's UUID (0x28) will be returned in the low
89 // 8-bits of W0, while byte 15 (0x74) will be returned in bits 31-24 of W3.
90 //
91 // `uuid` value derived below thus need to be byte-reversed before
92 // being used in Uuid::from_u128(). Alternately use Uuid::from_u128_le()
93 // to achieve the same.
94
95 let uuid = ((res[3] as u32 as u128) << 96)
96 | ((res[2] as u32 as u128) << 64)
97 | ((res[1] as u32 as u128) << 32)
98 | (res[0] as u32 as u128);
99
100 Uuid::from_u128_le(uuid)
101}
102
103fn detect_hypervisor() -> HypervisorBackend {
Pierre-Clément Tosicf215852023-07-11 13:33:14 +0000104 query_vendor_hyp_call_uid().try_into().expect("Failed to detect hypervisor")
Srivatsa Vaddagiri353f5d02023-04-18 23:49:01 -0700105}
106
Alice Wang31329112023-04-13 09:02:36 +0000107/// Gets the hypervisor singleton.
Pierre-Clément Tosid643cfe2023-06-29 09:30:51 +0000108fn get_hypervisor() -> &'static dyn Hypervisor {
Srivatsa Vaddagiri353f5d02023-04-18 23:49:01 -0700109 static HYPERVISOR: OnceBox<HypervisorBackend> = OnceBox::new();
110
111 HYPERVISOR.get_or_init(|| Box::new(detect_hypervisor())).get_hypervisor()
Alice Wang0bdc3f62023-03-15 10:46:12 +0000112}
Pierre-Clément Tosid643cfe2023-06-29 09:30:51 +0000113
114/// Gets the MMIO_GUARD hypervisor singleton, if any.
115pub fn get_mmio_guard() -> Option<&'static dyn MmioGuardedHypervisor> {
116 get_hypervisor().as_mmio_guard()
117}
118
119/// Gets the dynamic memory sharing hypervisor singleton, if any.
120pub fn get_mem_sharer() -> Option<&'static dyn MemSharingHypervisor> {
121 get_hypervisor().as_mem_sharer()
122}
Jaewan Kimd9095f82023-11-13 11:04:27 +0900123
124/// Gets the device assigning hypervisor singleton, if any.
125pub fn get_device_assigner() -> Option<&'static dyn DeviceAssigningHypervisor> {
126 get_hypervisor().as_device_assigner()
127}