blob: 003a7d4e9e336ab5b4bfac30cc09070983350d48 [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 Kim61f86142023-03-28 15:12:52 +090018 VirtualMachineAppConfig::DebugLevel::DebugLevel,
Jaewan Kimc03f6612023-02-20 00:06:26 +090019};
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090020use anyhow::{anyhow, Context, Error, Result};
21use std::fs;
22use std::io::ErrorKind;
23use std::path::{Path, PathBuf};
24use std::ffi::{CString, NulError};
25use log::{warn, info};
Jaewan Kim61f86142023-03-28 15:12:52 +090026use rustutils::system_properties;
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090027use libfdt::{Fdt, FdtError};
28use lazy_static::lazy_static;
Jaewan Kim61f86142023-03-28 15:12:52 +090029
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090030const CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP: &str =
31 "hypervisor.virtualizationmanager.debug_policy.path";
32const DEVICE_TREE_EMPTY_TREE_SIZE_BYTES: usize = 100; // rough estimation.
Jaewan Kim61f86142023-03-28 15:12:52 +090033
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090034struct DPPath {
35 node_path: CString,
36 prop_name: CString,
37}
38
39impl DPPath {
40 fn new(node_path: &str, prop_name: &str) -> Result<Self, NulError> {
41 Ok(Self { node_path: CString::new(node_path)?, prop_name: CString::new(prop_name)? })
42 }
43
44 fn to_path(&self) -> PathBuf {
Andrew Walbranb58d1b42023-07-07 13:54:49 +010045 // unwrap() is safe for to_str() because node_path and prop_name were &str.
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090046 PathBuf::from(
47 [
48 "/sys/firmware/devicetree/base",
49 self.node_path.to_str().unwrap(),
50 "/",
51 self.prop_name.to_str().unwrap(),
52 ]
53 .concat(),
54 )
55 }
56}
57
58lazy_static! {
59 static ref DP_LOG_PATH: DPPath = DPPath::new("/avf/guest/common", "log").unwrap();
60 static ref DP_RAMDUMP_PATH: DPPath = DPPath::new("/avf/guest/common", "ramdump").unwrap();
61 static ref DP_ADB_PATH: DPPath = DPPath::new("/avf/guest/microdroid", "adb").unwrap();
62}
63
64/// Get debug policy value in bool. It's true iff the value is explicitly set to <1>.
65fn get_debug_policy_bool(path: &Path) -> Result<bool> {
66 let value = match fs::read(path) {
67 Ok(value) => value,
68 Err(error) if error.kind() == ErrorKind::NotFound => return Ok(false),
69 Err(error) => Err(error).with_context(|| format!("Failed to read {path:?}"))?,
70 };
71
72 // DT spec uses big endian although Android is always little endian.
73 match u32::from_be_bytes(value.try_into().map_err(|_| anyhow!("Malformed value in {path:?}"))?)
74 {
75 0 => Ok(false),
76 1 => Ok(true),
77 value => Err(anyhow!("Invalid value {value} in {path:?}")),
78 }
79}
80
81/// Get property value in bool. It's true iff the value is explicitly set to <1>.
82/// It takes path as &str instead of &Path, because we don't want OsStr.
83fn get_fdt_prop_bool(fdt: &Fdt, path: &DPPath) -> Result<bool> {
84 let (node_path, prop_name) = (&path.node_path, &path.prop_name);
85 let node = match fdt.node(node_path) {
86 Ok(Some(node)) => node,
Chris Wailes9d09f572024-01-16 13:31:02 -080087 Err(error) if error != FdtError::NotFound => {
88 Err(Error::msg(error)).with_context(|| format!("Failed to get node {node_path:?}"))?
89 }
Jaewan Kim4cf20aa2023-04-03 10:25:38 +090090 _ => return Ok(false),
91 };
92
93 match node.getprop_u32(prop_name) {
94 Ok(Some(0)) => Ok(false),
95 Ok(Some(1)) => Ok(true),
96 Ok(Some(_)) => Err(anyhow!("Invalid prop value {prop_name:?} in node {node_path:?}")),
Chris Wailes9d09f572024-01-16 13:31:02 -080097 Err(error) if error != FdtError::NotFound => {
98 Err(Error::msg(error)).with_context(|| format!("Failed to get prop {prop_name:?}"))
99 }
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900100 _ => Ok(false),
101 }
102}
103
104/// Fdt with owned vector.
105struct OwnedFdt {
106 buffer: Vec<u8>,
107}
108
109impl OwnedFdt {
110 fn from_overlay_onto_new_fdt(overlay_file_path: &Path) -> Result<Self> {
111 let mut overlay_buf = match fs::read(overlay_file_path) {
112 Ok(fdt) => fdt,
113 Err(error) if error.kind() == ErrorKind::NotFound => Default::default(),
114 Err(error) => {
115 Err(error).with_context(|| format!("Failed to read {overlay_file_path:?}"))?
116 }
117 };
118
119 let overlay_buf_size = overlay_buf.len();
120
121 let fdt_estimated_size = overlay_buf_size + DEVICE_TREE_EMPTY_TREE_SIZE_BYTES;
122 let mut fdt_buf = vec![0_u8; fdt_estimated_size];
123 let fdt = Fdt::create_empty_tree(fdt_buf.as_mut_slice())
124 .map_err(Error::msg)
125 .context("Failed to create an empty device tree")?;
126
127 if !overlay_buf.is_empty() {
128 let overlay_fdt = Fdt::from_mut_slice(overlay_buf.as_mut_slice())
129 .map_err(Error::msg)
130 .with_context(|| "Malformed {overlay_file_path:?}")?;
131
Andrew Walbranb58d1b42023-07-07 13:54:49 +0100132 // SAFETY: Return immediately if error happens. Damaged fdt_buf and fdt are discarded.
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900133 unsafe {
134 fdt.apply_overlay(overlay_fdt).map_err(Error::msg).with_context(|| {
135 "Failed to overlay {overlay_file_path:?} onto empty device tree"
136 })?;
137 }
138 }
139
140 Ok(Self { buffer: fdt_buf })
141 }
142
143 fn as_fdt(&self) -> &Fdt {
Andrew Walbranb58d1b42023-07-07 13:54:49 +0100144 // SAFETY: Checked validity of buffer when instantiate.
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900145 unsafe { Fdt::unchecked_from_slice(&self.buffer) }
146 }
147}
Jaewan Kim61f86142023-03-28 15:12:52 +0900148
149/// Debug configurations for both debug level and debug policy
150#[derive(Debug)]
151pub struct DebugConfig {
152 pub debug_level: DebugLevel,
153 debug_policy_log: bool,
154 debug_policy_ramdump: bool,
155 debug_policy_adb: bool,
156}
Jaewan Kimc03f6612023-02-20 00:06:26 +0900157
Jaewan Kim61f86142023-03-28 15:12:52 +0900158impl DebugConfig {
159 pub fn new(debug_level: DebugLevel) -> Self {
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900160 match system_properties::read(CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP).unwrap_or_default() {
161 Some(path) if !path.is_empty() => {
162 match Self::from_custom_debug_overlay_policy(debug_level, Path::new(&path)) {
163 Ok(debug_config) => {
164 info!("Loaded custom debug policy overlay {path}: {debug_config:?}");
165 return debug_config;
166 }
167 Err(err) => warn!("Failed to load custom debug policy overlay {path}: {err:?}"),
168 };
Jaewan Kim61f86142023-03-28 15:12:52 +0900169 }
170 _ => {
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900171 match Self::from_host(debug_level) {
172 Ok(debug_config) => {
173 info!("Loaded debug policy from host OS: {debug_config:?}");
174 return debug_config;
175 }
176 Err(err) => warn!("Failed to load debug policy from host OS: {err:?}"),
Jaewan Kim61f86142023-03-28 15:12:52 +0900177 };
Jaewan Kim61f86142023-03-28 15:12:52 +0900178 }
179 }
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900180
181 info!("Debug policy is disabled");
182 Self {
183 debug_level,
184 debug_policy_log: false,
185 debug_policy_ramdump: false,
186 debug_policy_adb: false,
187 }
Jaewan Kim61f86142023-03-28 15:12:52 +0900188 }
Jiyong Parked180932023-02-24 19:55:41 +0900189
Jaewan Kim61f86142023-03-28 15:12:52 +0900190 /// Get whether console output should be configred for VM to leave console and adb log.
191 /// Caller should create pipe and prepare for receiving VM log with it.
192 pub fn should_prepare_console_output(&self) -> bool {
193 self.debug_level != DebugLevel::NONE || self.debug_policy_log || self.debug_policy_adb
194 }
Jiyong Parked180932023-02-24 19:55:41 +0900195
Jaewan Kim61f86142023-03-28 15:12:52 +0900196 /// Get whether debug apexes (MICRODROID_REQUIRED_APEXES_DEBUG) are required.
197 pub fn should_include_debug_apexes(&self) -> bool {
198 self.debug_level != DebugLevel::NONE || self.debug_policy_adb
199 }
200
201 /// Decision to support ramdump
202 pub fn is_ramdump_needed(&self) -> bool {
203 self.debug_level != DebugLevel::NONE || self.debug_policy_ramdump
204 }
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900205
206 // TODO: Remove this code path in user build for removing libfdt depenency.
207 fn from_custom_debug_overlay_policy(debug_level: DebugLevel, path: &Path) -> Result<Self> {
208 match OwnedFdt::from_overlay_onto_new_fdt(path) {
209 Ok(fdt) => Ok(Self {
210 debug_level,
211 debug_policy_log: get_fdt_prop_bool(fdt.as_fdt(), &DP_LOG_PATH)?,
212 debug_policy_ramdump: get_fdt_prop_bool(fdt.as_fdt(), &DP_RAMDUMP_PATH)?,
213 debug_policy_adb: get_fdt_prop_bool(fdt.as_fdt(), &DP_ADB_PATH)?,
214 }),
215 Err(err) => Err(err),
216 }
217 }
218
219 fn from_host(debug_level: DebugLevel) -> Result<Self> {
220 Ok(Self {
221 debug_level,
222 debug_policy_log: get_debug_policy_bool(&DP_LOG_PATH.to_path())?,
223 debug_policy_ramdump: get_debug_policy_bool(&DP_RAMDUMP_PATH.to_path())?,
224 debug_policy_adb: get_debug_policy_bool(&DP_ADB_PATH.to_path())?,
225 })
226 }
227}
228
229#[cfg(test)]
230mod tests {
231 use super::*;
232 use anyhow::ensure;
233
234 fn can_set_sysprop() -> bool {
235 if let Ok(Some(value)) = system_properties::read("ro.build.type") {
236 return "user".eq(&value);
237 }
238 false // if we're in doubt, skip test.
239 }
240
241 #[test]
Jaewan Kim46b96702023-09-07 15:24:51 +0900242 fn test_read_avf_debug_policy_with_ramdump() -> Result<()> {
243 let debug_config = DebugConfig::from_custom_debug_overlay_policy(
244 DebugLevel::FULL,
245 "avf_debug_policy_with_ramdump.dtbo".as_ref(),
246 )
247 .unwrap();
248
249 assert_eq!(DebugLevel::FULL, debug_config.debug_level);
250 assert!(!debug_config.debug_policy_log);
251 assert!(debug_config.debug_policy_ramdump);
252 assert!(debug_config.debug_policy_adb);
253
254 Ok(())
255 }
256
257 #[test]
258 fn test_read_avf_debug_policy_without_ramdump() -> Result<()> {
259 let debug_config = DebugConfig::from_custom_debug_overlay_policy(
260 DebugLevel::FULL,
261 "avf_debug_policy_without_ramdump.dtbo".as_ref(),
262 )
263 .unwrap();
264
265 assert_eq!(DebugLevel::FULL, debug_config.debug_level);
266 assert!(!debug_config.debug_policy_log);
267 assert!(!debug_config.debug_policy_ramdump);
268 assert!(debug_config.debug_policy_adb);
269
270 Ok(())
271 }
272
273 #[test]
Jaewan Kim4cf20aa2023-04-03 10:25:38 +0900274 fn test_read_avf_debug_policy_with_adb() -> Result<()> {
275 let debug_config = DebugConfig::from_custom_debug_overlay_policy(
276 DebugLevel::FULL,
277 "avf_debug_policy_with_adb.dtbo".as_ref(),
278 )
279 .unwrap();
280
281 assert_eq!(DebugLevel::FULL, debug_config.debug_level);
282 assert!(!debug_config.debug_policy_log);
283 assert!(!debug_config.debug_policy_ramdump);
284 assert!(debug_config.debug_policy_adb);
285
286 Ok(())
287 }
288
289 #[test]
290 fn test_read_avf_debug_policy_without_adb() -> Result<()> {
291 let debug_config = DebugConfig::from_custom_debug_overlay_policy(
292 DebugLevel::FULL,
293 "avf_debug_policy_without_adb.dtbo".as_ref(),
294 )
295 .unwrap();
296
297 assert_eq!(DebugLevel::FULL, debug_config.debug_level);
298 assert!(!debug_config.debug_policy_log);
299 assert!(!debug_config.debug_policy_ramdump);
300 assert!(!debug_config.debug_policy_adb);
301
302 Ok(())
303 }
304
305 #[test]
306 fn test_invalid_sysprop_disables_debug_policy() -> Result<()> {
307 let debug_config = DebugConfig::from_custom_debug_overlay_policy(
308 DebugLevel::NONE,
309 "/a/does/not/exist/path.dtbo".as_ref(),
310 )
311 .unwrap();
312
313 assert_eq!(DebugLevel::NONE, debug_config.debug_level);
314 assert!(!debug_config.debug_policy_log);
315 assert!(!debug_config.debug_policy_ramdump);
316 assert!(!debug_config.debug_policy_adb);
317
318 Ok(())
319 }
320
321 fn test_new_with_custom_policy_internal() -> Result<()> {
322 let debug_config = DebugConfig::new(DebugLevel::NONE);
323
324 ensure!(debug_config.debug_level == DebugLevel::NONE);
325 ensure!(!debug_config.debug_policy_log);
326 ensure!(!debug_config.debug_policy_ramdump);
327 ensure!(debug_config.debug_policy_adb);
328
329 Ok(())
330 }
331
332 #[test]
333 fn test_new_with_custom_policy() -> Result<()> {
334 if !can_set_sysprop() {
335 // Skip test if we can't override sysprop.
336 return Ok(());
337 }
338
339 // Setup
340 let old_sysprop = system_properties::read(CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP)
341 .context("Failed to read existing sysprop")?
342 .unwrap_or_default();
343 let file_name = "avf_debug_policy_with_adb.dtbo";
344 system_properties::write(CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP, file_name)
345 .context("Failed to set sysprop")?;
346
347 // Run test
348 let test_result = test_new_with_custom_policy_internal();
349
350 // Clean up.
351 system_properties::write(CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP, &old_sysprop)
352 .context("Failed to restore sysprop")?;
353
354 test_result
355 }
Jiyong Parked180932023-02-24 19:55:41 +0900356}