blob: 6e2bfefe43fe61fa2a952591896371861fe42a37 [file] [log] [blame]
Jaewan Kimc03f6612023-02-20 00:06:26 +09001// 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
17use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
Jaewan Kimf3143242024-03-15 06:56:31 +000018 VirtualMachineAppConfig::DebugLevel::DebugLevel, VirtualMachineConfig::VirtualMachineConfig,
Jaewan Kimc03f6612023-02-20 00:06:26 +090019};
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090020use anyhow::{anyhow, Context, Error, Result};
Jaewan Kimf3143242024-03-15 06:56:31 +000021use libfdt::{Fdt, FdtError};
22use log::{info, warn};
23use rustutils::system_properties;
24use std::ffi::{CString, NulError};
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090025use std::fs;
26use std::io::ErrorKind;
27use std::path::{Path, PathBuf};
Andrew Walbran9c03a3a2024-09-03 12:12:59 +010028use std::sync::LazyLock;
Pierre-Clément Tosid3bbe1d2024-04-15 18:03:51 +010029use vmconfig::get_debug_level;
Jaewan Kim61f86142023-03-28 15:12:52 +090030
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090031const CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP: &str =
32 "hypervisor.virtualizationmanager.debug_policy.path";
Pierre-Clément Tosi9515d0f2024-04-15 15:58:07 +010033const DUMP_DT_SYSPROP: &str = "hypervisor.virtualizationmanager.dump_device_tree";
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090034const DEVICE_TREE_EMPTY_TREE_SIZE_BYTES: usize = 100; // rough estimation.
Jaewan Kim61f86142023-03-28 15:12:52 +090035
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090036struct DPPath {
37 node_path: CString,
38 prop_name: CString,
39}
40
41impl DPPath {
42 fn new(node_path: &str, prop_name: &str) -> Result<Self, NulError> {
43 Ok(Self { node_path: CString::new(node_path)?, prop_name: CString::new(prop_name)? })
44 }
45
46 fn to_path(&self) -> PathBuf {
Andrew Walbranb58d1b42023-07-07 13:54:49 +010047 // unwrap() is safe for to_str() because node_path and prop_name were &str.
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090048 PathBuf::from(
49 [
Jaewan Kim1f0135b2024-01-31 14:59:47 +090050 "/proc/device-tree",
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090051 self.node_path.to_str().unwrap(),
52 "/",
53 self.prop_name.to_str().unwrap(),
54 ]
55 .concat(),
56 )
57 }
58}
59
Andrew Walbran9c03a3a2024-09-03 12:12:59 +010060static DP_LOG_PATH: LazyLock<DPPath> =
61 LazyLock::new(|| DPPath::new("/avf/guest/common", "log").unwrap());
62static DP_RAMDUMP_PATH: LazyLock<DPPath> =
63 LazyLock::new(|| DPPath::new("/avf/guest/common", "ramdump").unwrap());
64static DP_ADB_PATH: LazyLock<DPPath> =
65 LazyLock::new(|| DPPath::new("/avf/guest/microdroid", "adb").unwrap());
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090066
67/// Get debug policy value in bool. It's true iff the value is explicitly set to <1>.
68fn get_debug_policy_bool(path: &Path) -> Result<bool> {
69 let value = match fs::read(path) {
70 Ok(value) => value,
71 Err(error) if error.kind() == ErrorKind::NotFound => return Ok(false),
72 Err(error) => Err(error).with_context(|| format!("Failed to read {path:?}"))?,
73 };
74
75 // DT spec uses big endian although Android is always little endian.
76 match u32::from_be_bytes(value.try_into().map_err(|_| anyhow!("Malformed value in {path:?}"))?)
77 {
78 0 => Ok(false),
79 1 => Ok(true),
80 value => Err(anyhow!("Invalid value {value} in {path:?}")),
81 }
82}
83
84/// Get property value in bool. It's true iff the value is explicitly set to <1>.
85/// It takes path as &str instead of &Path, because we don't want OsStr.
86fn get_fdt_prop_bool(fdt: &Fdt, path: &DPPath) -> Result<bool> {
87 let (node_path, prop_name) = (&path.node_path, &path.prop_name);
88 let node = match fdt.node(node_path) {
89 Ok(Some(node)) => node,
Chris Wailes9d09f572024-01-16 13:31:02 -080090 Err(error) if error != FdtError::NotFound => {
91 Err(Error::msg(error)).with_context(|| format!("Failed to get node {node_path:?}"))?
92 }
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090093 _ => return Ok(false),
94 };
95
96 match node.getprop_u32(prop_name) {
97 Ok(Some(0)) => Ok(false),
98 Ok(Some(1)) => Ok(true),
99 Ok(Some(_)) => Err(anyhow!("Invalid prop value {prop_name:?} in node {node_path:?}")),
Chris Wailes9d09f572024-01-16 13:31:02 -0800100 Err(error) if error != FdtError::NotFound => {
101 Err(Error::msg(error)).with_context(|| format!("Failed to get prop {prop_name:?}"))
102 }
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900103 _ => Ok(false),
104 }
105}
106
107/// Fdt with owned vector.
108struct OwnedFdt {
109 buffer: Vec<u8>,
110}
111
112impl OwnedFdt {
113 fn from_overlay_onto_new_fdt(overlay_file_path: &Path) -> Result<Self> {
114 let mut overlay_buf = match fs::read(overlay_file_path) {
115 Ok(fdt) => fdt,
116 Err(error) if error.kind() == ErrorKind::NotFound => Default::default(),
117 Err(error) => {
118 Err(error).with_context(|| format!("Failed to read {overlay_file_path:?}"))?
119 }
120 };
121
122 let overlay_buf_size = overlay_buf.len();
123
124 let fdt_estimated_size = overlay_buf_size + DEVICE_TREE_EMPTY_TREE_SIZE_BYTES;
125 let mut fdt_buf = vec![0_u8; fdt_estimated_size];
126 let fdt = Fdt::create_empty_tree(fdt_buf.as_mut_slice())
127 .map_err(Error::msg)
128 .context("Failed to create an empty device tree")?;
129
130 if !overlay_buf.is_empty() {
131 let overlay_fdt = Fdt::from_mut_slice(overlay_buf.as_mut_slice())
132 .map_err(Error::msg)
133 .with_context(|| "Malformed {overlay_file_path:?}")?;
134
Andrew Walbranb58d1b42023-07-07 13:54:49 +0100135 // SAFETY: Return immediately if error happens. Damaged fdt_buf and fdt are discarded.
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900136 unsafe {
137 fdt.apply_overlay(overlay_fdt).map_err(Error::msg).with_context(|| {
138 "Failed to overlay {overlay_file_path:?} onto empty device tree"
139 })?;
140 }
141 }
142
143 Ok(Self { buffer: fdt_buf })
144 }
145
146 fn as_fdt(&self) -> &Fdt {
Andrew Walbranb58d1b42023-07-07 13:54:49 +0100147 // SAFETY: Checked validity of buffer when instantiate.
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900148 unsafe { Fdt::unchecked_from_slice(&self.buffer) }
149 }
150}
Jaewan Kim61f86142023-03-28 15:12:52 +0900151
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100152/// Debug configurations for debug policy.
153#[derive(Debug, Default)]
154pub struct DebugPolicy {
155 log: bool,
156 ramdump: bool,
157 adb: bool,
158}
159
160impl DebugPolicy {
161 /// Build from the passed DTBO path.
162 pub fn from_overlay(path: &Path) -> Result<Self> {
163 let owned_fdt = OwnedFdt::from_overlay_onto_new_fdt(path)?;
164 let fdt = owned_fdt.as_fdt();
165
166 Ok(Self {
167 log: get_fdt_prop_bool(fdt, &DP_LOG_PATH)?,
168 ramdump: get_fdt_prop_bool(fdt, &DP_RAMDUMP_PATH)?,
169 adb: get_fdt_prop_bool(fdt, &DP_ADB_PATH)?,
170 })
171 }
172
173 /// Build from the /avf/guest subtree of the host DT.
174 pub fn from_host() -> Result<Self> {
175 Ok(Self {
176 log: get_debug_policy_bool(&DP_LOG_PATH.to_path())?,
177 ramdump: get_debug_policy_bool(&DP_RAMDUMP_PATH.to_path())?,
178 adb: get_debug_policy_bool(&DP_ADB_PATH.to_path())?,
179 })
180 }
181}
182
Jaewan Kim61f86142023-03-28 15:12:52 +0900183/// Debug configurations for both debug level and debug policy
Pierre-Clément Tosic57c4df2024-04-15 16:51:01 +0100184#[derive(Debug, Default)]
Jaewan Kim61f86142023-03-28 15:12:52 +0900185pub struct DebugConfig {
186 pub debug_level: DebugLevel,
Pierre-Clément Tosi9515d0f2024-04-15 15:58:07 +0100187 pub dump_device_tree: bool,
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100188 debug_policy: DebugPolicy,
Jaewan Kim61f86142023-03-28 15:12:52 +0900189}
Jaewan Kimc03f6612023-02-20 00:06:26 +0900190
Jaewan Kim61f86142023-03-28 15:12:52 +0900191impl DebugConfig {
Jaewan Kimf3143242024-03-15 06:56:31 +0000192 pub fn new(config: &VirtualMachineConfig) -> Self {
Pierre-Clément Tosid3bbe1d2024-04-15 18:03:51 +0100193 let debug_level = get_debug_level(config).unwrap_or(DebugLevel::NONE);
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100194 let debug_policy = Self::get_debug_policy().unwrap_or_else(|| {
195 info!("Debug policy is disabled");
196 Default::default()
197 });
Pierre-Clément Tosi9515d0f2024-04-15 15:58:07 +0100198 let dump_dt_sysprop = system_properties::read_bool(DUMP_DT_SYSPROP, false);
199 let dump_device_tree = dump_dt_sysprop.unwrap_or_else(|e| {
200 warn!("Failed to read sysprop {DUMP_DT_SYSPROP}: {e}");
201 false
202 });
Jaewan Kimf3143242024-03-15 06:56:31 +0000203
Pierre-Clément Tosi9515d0f2024-04-15 15:58:07 +0100204 Self { debug_level, debug_policy, dump_device_tree }
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100205 }
206
207 fn get_debug_policy() -> Option<DebugPolicy> {
Pierre-Clément Tosie4d9f392024-04-16 15:51:00 +0100208 let dp_sysprop = system_properties::read(CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP);
209 let custom_dp = dp_sysprop.unwrap_or_else(|e| {
210 warn!("Failed to read sysprop {CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP}: {e}");
211 Default::default()
212 });
213
214 match custom_dp {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100215 Some(path) if !path.is_empty() => match DebugPolicy::from_overlay(Path::new(&path)) {
216 Ok(dp) => {
217 info!("Loaded custom debug policy overlay {path}: {dp:?}");
218 Some(dp)
219 }
220 Err(err) => {
221 warn!("Failed to load custom debug policy overlay {path}: {err:?}");
222 None
223 }
224 },
225 _ => match DebugPolicy::from_host() {
226 Ok(dp) => {
227 info!("Loaded debug policy from host OS: {dp:?}");
228 Some(dp)
229 }
230 Err(err) => {
231 warn!("Failed to load debug policy from host OS: {err:?}");
232 None
233 }
234 },
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900235 }
Jaewan Kim61f86142023-03-28 15:12:52 +0900236 }
Jiyong Parked180932023-02-24 19:55:41 +0900237
Pierre-Clément Tosic57c4df2024-04-15 16:51:01 +0100238 #[cfg(test)]
239 /// Creates a new DebugConfig with debug level. Only use this for test purpose.
240 pub(crate) fn new_with_debug_level(debug_level: DebugLevel) -> Self {
241 Self { debug_level, ..Default::default() }
242 }
243
Jaewan Kim61f86142023-03-28 15:12:52 +0900244 /// Get whether console output should be configred for VM to leave console and adb log.
245 /// Caller should create pipe and prepare for receiving VM log with it.
246 pub fn should_prepare_console_output(&self) -> bool {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100247 self.debug_level != DebugLevel::NONE || self.debug_policy.log || self.debug_policy.adb
Jaewan Kim61f86142023-03-28 15:12:52 +0900248 }
Jiyong Parked180932023-02-24 19:55:41 +0900249
Jaewan Kim61f86142023-03-28 15:12:52 +0900250 /// Get whether debug apexes (MICRODROID_REQUIRED_APEXES_DEBUG) are required.
251 pub fn should_include_debug_apexes(&self) -> bool {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100252 self.debug_level != DebugLevel::NONE || self.debug_policy.adb
Jaewan Kim61f86142023-03-28 15:12:52 +0900253 }
254
255 /// Decision to support ramdump
256 pub fn is_ramdump_needed(&self) -> bool {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100257 self.debug_level != DebugLevel::NONE || self.debug_policy.ramdump
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900258 }
259}
260
261#[cfg(test)]
262mod tests {
263 use super::*;
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900264
265 #[test]
Jaewan Kim46b96702023-09-07 15:24:51 +0900266 fn test_read_avf_debug_policy_with_ramdump() -> Result<()> {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100267 let debug_policy =
268 DebugPolicy::from_overlay("avf_debug_policy_with_ramdump.dtbo".as_ref()).unwrap();
Jaewan Kim46b96702023-09-07 15:24:51 +0900269
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100270 assert!(!debug_policy.log);
271 assert!(debug_policy.ramdump);
272 assert!(debug_policy.adb);
Jaewan Kim46b96702023-09-07 15:24:51 +0900273
274 Ok(())
275 }
276
277 #[test]
278 fn test_read_avf_debug_policy_without_ramdump() -> Result<()> {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100279 let debug_policy =
280 DebugPolicy::from_overlay("avf_debug_policy_without_ramdump.dtbo".as_ref()).unwrap();
Jaewan Kim46b96702023-09-07 15:24:51 +0900281
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100282 assert!(!debug_policy.log);
283 assert!(!debug_policy.ramdump);
284 assert!(debug_policy.adb);
Jaewan Kim46b96702023-09-07 15:24:51 +0900285
286 Ok(())
287 }
288
289 #[test]
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900290 fn test_read_avf_debug_policy_with_adb() -> Result<()> {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100291 let debug_policy =
292 DebugPolicy::from_overlay("avf_debug_policy_with_adb.dtbo".as_ref()).unwrap();
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900293
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100294 assert!(!debug_policy.log);
295 assert!(!debug_policy.ramdump);
296 assert!(debug_policy.adb);
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900297
298 Ok(())
299 }
300
301 #[test]
302 fn test_read_avf_debug_policy_without_adb() -> Result<()> {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100303 let debug_policy =
304 DebugPolicy::from_overlay("avf_debug_policy_without_adb.dtbo".as_ref()).unwrap();
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900305
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100306 assert!(!debug_policy.log);
307 assert!(!debug_policy.ramdump);
308 assert!(!debug_policy.adb);
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900309
310 Ok(())
311 }
312
313 #[test]
314 fn test_invalid_sysprop_disables_debug_policy() -> Result<()> {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100315 let debug_policy =
316 DebugPolicy::from_overlay("/a/does/not/exist/path.dtbo".as_ref()).unwrap();
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900317
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100318 assert!(!debug_policy.log);
319 assert!(!debug_policy.ramdump);
320 assert!(!debug_policy.adb);
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900321
322 Ok(())
323 }
Pierre-Clément Tosic57c4df2024-04-15 16:51:01 +0100324
325 #[test]
326 fn test_new_with_debug_level() -> Result<()> {
327 assert_eq!(
328 DebugConfig::new_with_debug_level(DebugLevel::NONE).debug_level,
329 DebugLevel::NONE
330 );
331 assert_eq!(
332 DebugConfig::new_with_debug_level(DebugLevel::FULL).debug_level,
333 DebugLevel::FULL
334 );
335
336 Ok(())
337 }
Jiyong Parked180932023-02-24 19:55:41 +0900338}