1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //! Detection for nested virtualization.
18 
19 use anyhow::Result;
20 use rustutils::system_properties;
21 
22 /// Return whether we will be running our VM in a VM, which causes the nested VM to run very slowly.
is_nested_virtualization() -> Result<bool>23 pub fn is_nested_virtualization() -> Result<bool> {
24     // Nested virtualization occurs when we run KVM inside the cuttlefish VM or when
25     // we run trusty within qemu.
26     let checks = [
27         ("ro.product.vendor.device", "vsoc_"), // vsoc_x86, vsoc_x86_64, vsoc_x86_64_only, ...
28         ("ro.hardware", "qemu_"),              // qemu_trusty, ...
29     ];
30 
31     for (property, prefix) in checks {
32         if let Some(value) = system_properties::read(property)? {
33             if value.starts_with(prefix) {
34                 return Ok(true);
35             }
36         }
37     }
38 
39     // No match -> not nested
40     Ok(false)
41 }
42