Jaewan Kim | c03f661 | 2023-02-20 00:06:26 +0900 | [diff] [blame] | 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 | //! Functions for AVF debug policy and debug level |
| 16 | |
| 17 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
Jiyong Park | ed18093 | 2023-02-24 19:55:41 +0900 | [diff] [blame] | 18 | VirtualMachineAppConfig::DebugLevel::DebugLevel, VirtualMachineConfig::VirtualMachineConfig, |
Jaewan Kim | c03f661 | 2023-02-20 00:06:26 +0900 | [diff] [blame] | 19 | }; |
| 20 | use std::fs::File; |
| 21 | use std::io::Read; |
| 22 | |
| 23 | /// Get debug policy value in bool. It's true iff the value is explicitly set to <1>. |
| 24 | fn get_debug_policy_bool(path: &'static str) -> Option<bool> { |
Jaewan Kim | 66f062e | 2023-02-25 01:07:43 +0900 | [diff] [blame] | 25 | let mut file = File::open(path).ok()?; |
| 26 | let mut log: [u8; 4] = Default::default(); |
| 27 | file.read_exact(&mut log).ok()?; |
| 28 | // DT spec uses big endian although Android is always little endian. |
| 29 | Some(u32::from_be_bytes(log) == 1) |
Jaewan Kim | c03f661 | 2023-02-20 00:06:26 +0900 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /// Get whether console output should be configred for VM to leave console and adb log. |
| 33 | /// Caller should create pipe and prepare for receiving VM log with it. |
| 34 | pub fn should_prepare_console_output(debug_level: DebugLevel) -> bool { |
| 35 | debug_level != DebugLevel::NONE |
| 36 | || get_debug_policy_bool("/proc/device-tree/avf/guest/common/log").unwrap_or_default() |
Jaewan Kim | b24d1dc | 2023-02-13 15:34:56 +0900 | [diff] [blame^] | 37 | || get_debug_policy_bool("/proc/device-tree/avf/guest/microdroid/adb").unwrap_or_default() |
| 38 | } |
| 39 | |
| 40 | /// Get whether debug apexes (MICRODROID_REQUIRED_APEXES_DEBUG) are required. |
| 41 | pub fn should_include_debug_apexes(debug_level: DebugLevel) -> bool { |
| 42 | debug_level != DebugLevel::NONE |
| 43 | || get_debug_policy_bool("/proc/device-tree/avf/guest/microdroid/adb").unwrap_or_default() |
Jaewan Kim | c03f661 | 2023-02-20 00:06:26 +0900 | [diff] [blame] | 44 | } |
Jiyong Park | ed18093 | 2023-02-24 19:55:41 +0900 | [diff] [blame] | 45 | |
| 46 | /// Decision to support ramdump |
| 47 | pub fn is_ramdump_needed(config: &VirtualMachineConfig) -> bool { |
| 48 | let enabled_in_dp = |
| 49 | get_debug_policy_bool("/proc/device-tree/avf/guest/common/ramdump").unwrap_or_default(); |
| 50 | let (protected, debuggable) = match config { |
| 51 | VirtualMachineConfig::RawConfig(config) => { |
| 52 | // custom VMs are considered debuggable for flexibility |
| 53 | (config.protectedVm, true) |
| 54 | } |
| 55 | VirtualMachineConfig::AppConfig(config) => { |
| 56 | (config.protectedVm, config.debugLevel == DebugLevel::FULL) |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | if protected { |
| 61 | enabled_in_dp |
| 62 | } else { |
| 63 | enabled_in_dp || debuggable |
| 64 | } |
| 65 | } |