blob: 74559de9e67ba2ce3598073f80deec619492b62e [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";
33const DEVICE_TREE_EMPTY_TREE_SIZE_BYTES: usize = 100; // rough estimation.
Jaewan Kim61f86142023-03-28 15:12:52 +090034
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090035struct DPPath {
36 node_path: CString,
37 prop_name: CString,
38}
39
40impl DPPath {
41 fn new(node_path: &str, prop_name: &str) -> Result<Self, NulError> {
42 Ok(Self { node_path: CString::new(node_path)?, prop_name: CString::new(prop_name)? })
43 }
44
45 fn to_path(&self) -> PathBuf {
Andrew Walbranb58d1b42023-07-07 13:54:49 +010046 // unwrap() is safe for to_str() because node_path and prop_name were &str.
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090047 PathBuf::from(
48 [
Jaewan Kim1f0135b2024-01-31 14:59:47 +090049 "/proc/device-tree",
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090050 self.node_path.to_str().unwrap(),
51 "/",
52 self.prop_name.to_str().unwrap(),
53 ]
54 .concat(),
55 )
56 }
57}
58
Andrew Walbran9c03a3a2024-09-03 12:12:59 +010059static DP_LOG_PATH: LazyLock<DPPath> =
60 LazyLock::new(|| DPPath::new("/avf/guest/common", "log").unwrap());
61static DP_RAMDUMP_PATH: LazyLock<DPPath> =
62 LazyLock::new(|| DPPath::new("/avf/guest/common", "ramdump").unwrap());
63static DP_ADB_PATH: LazyLock<DPPath> =
64 LazyLock::new(|| DPPath::new("/avf/guest/microdroid", "adb").unwrap());
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090065
66/// Get debug policy value in bool. It's true iff the value is explicitly set to <1>.
67fn get_debug_policy_bool(path: &Path) -> Result<bool> {
68 let value = match fs::read(path) {
69 Ok(value) => value,
70 Err(error) if error.kind() == ErrorKind::NotFound => return Ok(false),
71 Err(error) => Err(error).with_context(|| format!("Failed to read {path:?}"))?,
72 };
73
74 // DT spec uses big endian although Android is always little endian.
75 match u32::from_be_bytes(value.try_into().map_err(|_| anyhow!("Malformed value in {path:?}"))?)
76 {
77 0 => Ok(false),
78 1 => Ok(true),
79 value => Err(anyhow!("Invalid value {value} in {path:?}")),
80 }
81}
82
83/// Get property value in bool. It's true iff the value is explicitly set to <1>.
84/// It takes path as &str instead of &Path, because we don't want OsStr.
85fn get_fdt_prop_bool(fdt: &Fdt, path: &DPPath) -> Result<bool> {
86 let (node_path, prop_name) = (&path.node_path, &path.prop_name);
87 let node = match fdt.node(node_path) {
88 Ok(Some(node)) => node,
Chris Wailes9d09f572024-01-16 13:31:02 -080089 Err(error) if error != FdtError::NotFound => {
90 Err(Error::msg(error)).with_context(|| format!("Failed to get node {node_path:?}"))?
91 }
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090092 _ => return Ok(false),
93 };
94
95 match node.getprop_u32(prop_name) {
96 Ok(Some(0)) => Ok(false),
97 Ok(Some(1)) => Ok(true),
98 Ok(Some(_)) => Err(anyhow!("Invalid prop value {prop_name:?} in node {node_path:?}")),
Chris Wailes9d09f572024-01-16 13:31:02 -080099 Err(error) if error != FdtError::NotFound => {
100 Err(Error::msg(error)).with_context(|| format!("Failed to get prop {prop_name:?}"))
101 }
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900102 _ => Ok(false),
103 }
104}
105
106/// Fdt with owned vector.
107struct OwnedFdt {
108 buffer: Vec<u8>,
109}
110
111impl OwnedFdt {
112 fn from_overlay_onto_new_fdt(overlay_file_path: &Path) -> Result<Self> {
113 let mut overlay_buf = match fs::read(overlay_file_path) {
114 Ok(fdt) => fdt,
115 Err(error) if error.kind() == ErrorKind::NotFound => Default::default(),
116 Err(error) => {
117 Err(error).with_context(|| format!("Failed to read {overlay_file_path:?}"))?
118 }
119 };
120
121 let overlay_buf_size = overlay_buf.len();
122
123 let fdt_estimated_size = overlay_buf_size + DEVICE_TREE_EMPTY_TREE_SIZE_BYTES;
124 let mut fdt_buf = vec![0_u8; fdt_estimated_size];
125 let fdt = Fdt::create_empty_tree(fdt_buf.as_mut_slice())
126 .map_err(Error::msg)
127 .context("Failed to create an empty device tree")?;
128
129 if !overlay_buf.is_empty() {
130 let overlay_fdt = Fdt::from_mut_slice(overlay_buf.as_mut_slice())
131 .map_err(Error::msg)
132 .with_context(|| "Malformed {overlay_file_path:?}")?;
133
Andrew Walbranb58d1b42023-07-07 13:54:49 +0100134 // SAFETY: Return immediately if error happens. Damaged fdt_buf and fdt are discarded.
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900135 unsafe {
136 fdt.apply_overlay(overlay_fdt).map_err(Error::msg).with_context(|| {
137 "Failed to overlay {overlay_file_path:?} onto empty device tree"
138 })?;
139 }
140 }
141
142 Ok(Self { buffer: fdt_buf })
143 }
144
145 fn as_fdt(&self) -> &Fdt {
Andrew Walbranb58d1b42023-07-07 13:54:49 +0100146 // SAFETY: Checked validity of buffer when instantiate.
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900147 unsafe { Fdt::unchecked_from_slice(&self.buffer) }
148 }
149}
Jaewan Kim61f86142023-03-28 15:12:52 +0900150
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100151/// Debug configurations for debug policy.
152#[derive(Debug, Default)]
153pub struct DebugPolicy {
154 log: bool,
155 ramdump: bool,
156 adb: bool,
157}
158
159impl DebugPolicy {
160 /// Build from the passed DTBO path.
161 pub fn from_overlay(path: &Path) -> Result<Self> {
162 let owned_fdt = OwnedFdt::from_overlay_onto_new_fdt(path)?;
163 let fdt = owned_fdt.as_fdt();
164
165 Ok(Self {
166 log: get_fdt_prop_bool(fdt, &DP_LOG_PATH)?,
167 ramdump: get_fdt_prop_bool(fdt, &DP_RAMDUMP_PATH)?,
168 adb: get_fdt_prop_bool(fdt, &DP_ADB_PATH)?,
169 })
170 }
171
172 /// Build from the /avf/guest subtree of the host DT.
173 pub fn from_host() -> Result<Self> {
174 Ok(Self {
175 log: get_debug_policy_bool(&DP_LOG_PATH.to_path())?,
176 ramdump: get_debug_policy_bool(&DP_RAMDUMP_PATH.to_path())?,
177 adb: get_debug_policy_bool(&DP_ADB_PATH.to_path())?,
178 })
179 }
180}
181
Jaewan Kim61f86142023-03-28 15:12:52 +0900182/// Debug configurations for both debug level and debug policy
Pierre-Clément Tosic57c4df2024-04-15 16:51:01 +0100183#[derive(Debug, Default)]
Jaewan Kim61f86142023-03-28 15:12:52 +0900184pub struct DebugConfig {
185 pub debug_level: DebugLevel,
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100186 debug_policy: DebugPolicy,
Jaewan Kim61f86142023-03-28 15:12:52 +0900187}
Jaewan Kimc03f6612023-02-20 00:06:26 +0900188
Jaewan Kim61f86142023-03-28 15:12:52 +0900189impl DebugConfig {
Jaewan Kimf3143242024-03-15 06:56:31 +0000190 pub fn new(config: &VirtualMachineConfig) -> Self {
Pierre-Clément Tosid3bbe1d2024-04-15 18:03:51 +0100191 let debug_level = get_debug_level(config).unwrap_or(DebugLevel::NONE);
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100192 let debug_policy = Self::get_debug_policy().unwrap_or_else(|| {
193 info!("Debug policy is disabled");
194 Default::default()
195 });
Jaewan Kimf3143242024-03-15 06:56:31 +0000196
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100197 Self { debug_level, debug_policy }
198 }
199
200 fn get_debug_policy() -> Option<DebugPolicy> {
Pierre-Clément Tosie4d9f392024-04-16 15:51:00 +0100201 let dp_sysprop = system_properties::read(CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP);
202 let custom_dp = dp_sysprop.unwrap_or_else(|e| {
203 warn!("Failed to read sysprop {CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP}: {e}");
204 Default::default()
205 });
206
207 match custom_dp {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100208 Some(path) if !path.is_empty() => match DebugPolicy::from_overlay(Path::new(&path)) {
209 Ok(dp) => {
210 info!("Loaded custom debug policy overlay {path}: {dp:?}");
211 Some(dp)
212 }
213 Err(err) => {
214 warn!("Failed to load custom debug policy overlay {path}: {err:?}");
215 None
216 }
217 },
218 _ => match DebugPolicy::from_host() {
219 Ok(dp) => {
220 info!("Loaded debug policy from host OS: {dp:?}");
221 Some(dp)
222 }
223 Err(err) => {
224 warn!("Failed to load debug policy from host OS: {err:?}");
225 None
226 }
227 },
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900228 }
Jaewan Kim61f86142023-03-28 15:12:52 +0900229 }
Jiyong Parked180932023-02-24 19:55:41 +0900230
Pierre-Clément Tosic57c4df2024-04-15 16:51:01 +0100231 #[cfg(test)]
232 /// Creates a new DebugConfig with debug level. Only use this for test purpose.
233 pub(crate) fn new_with_debug_level(debug_level: DebugLevel) -> Self {
234 Self { debug_level, ..Default::default() }
235 }
236
Jaewan Kim61f86142023-03-28 15:12:52 +0900237 /// Get whether console output should be configred for VM to leave console and adb log.
238 /// Caller should create pipe and prepare for receiving VM log with it.
239 pub fn should_prepare_console_output(&self) -> bool {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100240 self.debug_level != DebugLevel::NONE || self.debug_policy.log || self.debug_policy.adb
Jaewan Kim61f86142023-03-28 15:12:52 +0900241 }
Jiyong Parked180932023-02-24 19:55:41 +0900242
Jaewan Kim61f86142023-03-28 15:12:52 +0900243 /// Get whether debug apexes (MICRODROID_REQUIRED_APEXES_DEBUG) are required.
244 pub fn should_include_debug_apexes(&self) -> bool {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100245 self.debug_level != DebugLevel::NONE || self.debug_policy.adb
Jaewan Kim61f86142023-03-28 15:12:52 +0900246 }
247
248 /// Decision to support ramdump
249 pub fn is_ramdump_needed(&self) -> bool {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100250 self.debug_level != DebugLevel::NONE || self.debug_policy.ramdump
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900251 }
252}
253
254#[cfg(test)]
255mod tests {
256 use super::*;
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900257
258 #[test]
Jaewan Kim46b96702023-09-07 15:24:51 +0900259 fn test_read_avf_debug_policy_with_ramdump() -> Result<()> {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100260 let debug_policy =
261 DebugPolicy::from_overlay("avf_debug_policy_with_ramdump.dtbo".as_ref()).unwrap();
Jaewan Kim46b96702023-09-07 15:24:51 +0900262
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100263 assert!(!debug_policy.log);
264 assert!(debug_policy.ramdump);
265 assert!(debug_policy.adb);
Jaewan Kim46b96702023-09-07 15:24:51 +0900266
267 Ok(())
268 }
269
270 #[test]
271 fn test_read_avf_debug_policy_without_ramdump() -> Result<()> {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100272 let debug_policy =
273 DebugPolicy::from_overlay("avf_debug_policy_without_ramdump.dtbo".as_ref()).unwrap();
Jaewan Kim46b96702023-09-07 15:24:51 +0900274
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100275 assert!(!debug_policy.log);
276 assert!(!debug_policy.ramdump);
277 assert!(debug_policy.adb);
Jaewan Kim46b96702023-09-07 15:24:51 +0900278
279 Ok(())
280 }
281
282 #[test]
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900283 fn test_read_avf_debug_policy_with_adb() -> Result<()> {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100284 let debug_policy =
285 DebugPolicy::from_overlay("avf_debug_policy_with_adb.dtbo".as_ref()).unwrap();
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900286
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100287 assert!(!debug_policy.log);
288 assert!(!debug_policy.ramdump);
289 assert!(debug_policy.adb);
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900290
291 Ok(())
292 }
293
294 #[test]
295 fn test_read_avf_debug_policy_without_adb() -> Result<()> {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100296 let debug_policy =
297 DebugPolicy::from_overlay("avf_debug_policy_without_adb.dtbo".as_ref()).unwrap();
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900298
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100299 assert!(!debug_policy.log);
300 assert!(!debug_policy.ramdump);
301 assert!(!debug_policy.adb);
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900302
303 Ok(())
304 }
305
306 #[test]
307 fn test_invalid_sysprop_disables_debug_policy() -> Result<()> {
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100308 let debug_policy =
309 DebugPolicy::from_overlay("/a/does/not/exist/path.dtbo".as_ref()).unwrap();
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900310
Pierre-Clément Tosi27fe2b62024-04-15 18:36:33 +0100311 assert!(!debug_policy.log);
312 assert!(!debug_policy.ramdump);
313 assert!(!debug_policy.adb);
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900314
315 Ok(())
316 }
Pierre-Clément Tosic57c4df2024-04-15 16:51:01 +0100317
318 #[test]
319 fn test_new_with_debug_level() -> Result<()> {
320 assert_eq!(
321 DebugConfig::new_with_debug_level(DebugLevel::NONE).debug_level,
322 DebugLevel::NONE
323 );
324 assert_eq!(
325 DebugConfig::new_with_debug_level(DebugLevel::FULL).debug_level,
326 DebugLevel::FULL
327 );
328
329 Ok(())
330 }
Jiyong Parked180932023-02-24 19:55:41 +0900331}