1 // 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 //! Access to hypervisor capabilities via system properties set by the bootloader.
16 
17 use anyhow::Result;
18 use platformproperties::hypervisorproperties;
19 
20 /// Returns whether there is a hypervisor present that supports non-protected VMs.
is_vm_supported() -> Result<bool>21 pub fn is_vm_supported() -> Result<bool> {
22     Ok(hypervisorproperties::hypervisor_vm_supported()?.unwrap_or(false))
23 }
24 
25 /// Returns whether there is a hypervisor present that supports protected VMs.
is_protected_vm_supported() -> Result<bool>26 pub fn is_protected_vm_supported() -> Result<bool> {
27     Ok(hypervisorproperties::hypervisor_protected_vm_supported()?.unwrap_or(false))
28 }
29 
30 /// Returns whether there is a hypervisor present that supports any sort of VM, either protected
31 /// or non-protected.
is_any_vm_supported() -> Result<bool>32 pub fn is_any_vm_supported() -> Result<bool> {
33     is_vm_supported().and_then(|ok| if ok { Ok(true) } else { is_protected_vm_supported() })
34 }
35 
36 /// Returns the version of the hypervisor, if there is one.
version() -> Result<Option<String>>37 pub fn version() -> Result<Option<String>> {
38     Ok(hypervisorproperties::hypervisor_version()?)
39 }
40 
41 /// Returns if the hypervisor is pKVM
is_pkvm() -> Result<bool>42 pub fn is_pkvm() -> Result<bool> {
43     Ok(version()?.unwrap_or_default().starts_with("kvm") && is_protected_vm_supported()?)
44 }
45