Merge "Detailed error message when parsing /proc/<pid>/smaps fails" into main
diff --git a/libs/libfdt/src/ctypes.rs b/libs/libfdt/src/ctypes.rs
index 640d447..a65f8e7 100644
--- a/libs/libfdt/src/ctypes.rs
+++ b/libs/libfdt/src/ctypes.rs
@@ -14,6 +14,7 @@
//! Safe zero-cost wrappers around integer values used by libfdt.
+use crate::result::FdtRawResult;
use crate::{FdtError, Result};
/// Wrapper guaranteed to contain a valid phandle.
@@ -50,3 +51,11 @@
Self::new(value).ok_or(FdtError::BadPhandle)
}
}
+
+impl TryFrom<FdtRawResult> for Phandle {
+ type Error = FdtError;
+
+ fn try_from(res: FdtRawResult) -> Result<Self> {
+ Self::new(res.try_into()?).ok_or(FdtError::BadPhandle)
+ }
+}
diff --git a/libs/libfdt/src/lib.rs b/libs/libfdt/src/lib.rs
index d1ab24e..a34ecbb 100644
--- a/libs/libfdt/src/lib.rs
+++ b/libs/libfdt/src/lib.rs
@@ -33,7 +33,6 @@
use core::ops::Range;
use cstr::cstr;
use libfdt::get_slice_at_ptr;
-use result::{fdt_err, fdt_err_expect_zero, fdt_err_or_option};
use zerocopy::AsBytes as _;
use crate::libfdt::{Libfdt, LibfdtMut};
@@ -434,7 +433,7 @@
}
/// Adds new subnodes to the given node.
- pub fn add_subnodes(&mut self, names: &[&CStr]) -> Result<()> {
+ pub fn add_subnodes(self, names: &[&CStr]) -> Result<()> {
for name in names {
self.fdt.add_subnode_namelen(self.offset, name.to_bytes())?;
}
@@ -442,7 +441,7 @@
}
/// Adds a new subnode to the given node and return it as a FdtNodeMut on success.
- pub fn add_subnode(&'a mut self, name: &CStr) -> Result<Self> {
+ pub fn add_subnode(self, name: &CStr) -> Result<Self> {
let name = name.to_bytes();
let offset = self.fdt.add_subnode_namelen(self.offset, name)?;
@@ -451,7 +450,7 @@
/// Adds a new subnode to the given node with name and namelen, and returns it as a FdtNodeMut
/// on success.
- pub fn add_subnode_with_namelen(&'a mut self, name: &CStr, namelen: usize) -> Result<Self> {
+ pub fn add_subnode_with_namelen(self, name: &CStr, namelen: usize) -> Result<Self> {
let name = &name.to_bytes()[..namelen];
let offset = self.fdt.add_subnode_namelen(self.offset, name)?;
@@ -637,40 +636,30 @@
/// Unpacks the DT to cover the whole slice it is contained in.
pub fn unpack(&mut self) -> Result<()> {
- // SAFETY: "Opens" the DT in-place (supported use-case) by updating its header and
- // internal structures to make use of the whole self.fdt slice but performs no accesses
- // outside of it and leaves the DT in a state that will be detected by other functions.
- let ret = unsafe {
- libfdt_bindgen::fdt_open_into(
- self.as_ptr(),
- self.as_mut_ptr(),
- self.capacity().try_into().map_err(|_| FdtError::Internal)?,
- )
- };
- fdt_err_expect_zero(ret)
+ self.open_into_self()
}
/// Packs the DT to take a minimum amount of memory.
///
/// Doesn't shrink the underlying memory slice.
pub fn pack(&mut self) -> Result<()> {
- // SAFETY: "Closes" the DT in-place by updating its header and relocating its structs.
- let ret = unsafe { libfdt_bindgen::fdt_pack(self.as_mut_ptr()) };
- fdt_err_expect_zero(ret)
+ LibfdtMut::pack(self)
}
/// Applies a DT overlay on the base DT.
///
/// # Safety
///
- /// On failure, the library corrupts the DT and overlay so both must be discarded.
- pub unsafe fn apply_overlay<'a>(&'a mut self, overlay: &'a mut Fdt) -> Result<&'a mut Self> {
- let ret =
- // SAFETY: Both pointers are valid because they come from references, and fdt_overlay_apply
- // doesn't keep them after it returns. It may corrupt their contents if there is an error,
- // but that's our caller's responsibility.
- unsafe { libfdt_bindgen::fdt_overlay_apply(self.as_mut_ptr(), overlay.as_mut_ptr()) };
- fdt_err_expect_zero(ret)?;
+ /// As libfdt corrupts the input DT on failure, `self` should be discarded on error:
+ ///
+ /// let fdt = fdt.apply_overlay(overlay)?;
+ ///
+ /// Furthermore, `overlay` is _always_ corrupted by libfdt and will never refer to a valid
+ /// `Fdt` after this function returns and must therefore be discarded by the caller.
+ pub unsafe fn apply_overlay<'a>(&'a mut self, overlay: &mut Fdt) -> Result<&'a mut Self> {
+ // SAFETY: Our caller will properly discard overlay and/or self as needed.
+ unsafe { self.overlay_apply(overlay) }?;
+
Ok(self)
}
@@ -785,14 +774,6 @@
self.buffer.as_ptr().cast()
}
- fn as_mut_ptr(&mut self) -> *mut c_void {
- self.buffer.as_mut_ptr().cast::<_>()
- }
-
- fn capacity(&self) -> usize {
- self.buffer.len()
- }
-
fn header(&self) -> &libfdt_bindgen::fdt_header {
let p = self.as_ptr().cast();
// SAFETY: A valid FDT (verified by constructor) must contain a valid fdt_header.
diff --git a/libs/libfdt/src/libfdt.rs b/libs/libfdt/src/libfdt.rs
index 7737718..1e82c9f 100644
--- a/libs/libfdt/src/libfdt.rs
+++ b/libs/libfdt/src/libfdt.rs
@@ -22,7 +22,8 @@
use core::mem;
use core::ptr;
-use crate::{fdt_err, fdt_err_expect_zero, fdt_err_or_option, FdtError, Phandle, Result};
+use crate::result::FdtRawResult;
+use crate::{FdtError, Phandle, Result};
// Function names are the C function names without the `fdt_` prefix.
@@ -35,7 +36,7 @@
// There will be no memory write outside of the given fdt.
let ret = unsafe { libfdt_bindgen::fdt_create_empty_tree(fdt, len) };
- fdt_err_expect_zero(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_check_full()` (C function).
@@ -49,7 +50,7 @@
// calls as it expects the client code to keep track of the objects (DT, nodes, ...).
let ret = unsafe { libfdt_bindgen::fdt_check_full(fdt, len) };
- fdt_err_expect_zero(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Wrapper for the read-only libfdt.h functions.
@@ -76,7 +77,7 @@
// function respects the passed number of characters.
let ret = unsafe { libfdt_bindgen::fdt_path_offset_namelen(fdt, path, len) };
- fdt_err_or_option(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_node_offset_by_phandle()` (C function).
@@ -86,7 +87,7 @@
// SAFETY: Accesses are constrained to the DT totalsize.
let ret = unsafe { libfdt_bindgen::fdt_node_offset_by_phandle(fdt, phandle) };
- fdt_err_or_option(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_node_offset_by_compatible()` (C function).
@@ -96,7 +97,7 @@
// SAFETY: Accesses (read-only) are constrained to the DT totalsize.
let ret = unsafe { libfdt_bindgen::fdt_node_offset_by_compatible(fdt, prev, compatible) };
- fdt_err_or_option(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_next_node()` (C function).
@@ -106,7 +107,7 @@
// SAFETY: Accesses (read-only) are constrained to the DT totalsize.
let ret = unsafe { libfdt_bindgen::fdt_next_node(fdt, node, &mut depth) };
- match fdt_err_or_option(ret)? {
+ match FdtRawResult::from(ret).try_into()? {
Some(offset) if depth >= 0 => {
let depth = depth.try_into().unwrap();
Ok(Some((offset, depth)))
@@ -123,7 +124,7 @@
// SAFETY: Accesses (read-only) are constrained to the DT totalsize.
let ret = unsafe { libfdt_bindgen::fdt_parent_offset(fdt, node) };
- fdt_err(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_supernode_atdepth_offset()` (C function).
@@ -138,7 +139,7 @@
// SAFETY: Accesses (read-only) are constrained to the DT totalsize.
unsafe { libfdt_bindgen::fdt_supernode_atdepth_offset(fdt, node, depth, nodedepth) };
- fdt_err(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_subnode_offset_namelen()` (C function).
@@ -149,7 +150,7 @@
// SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
let ret = unsafe { libfdt_bindgen::fdt_subnode_offset_namelen(fdt, parent, name, namelen) };
- fdt_err_or_option(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_first_subnode()` (C function).
fn first_subnode(&self, node: c_int) -> Result<Option<c_int>> {
@@ -157,7 +158,7 @@
// SAFETY: Accesses (read-only) are constrained to the DT totalsize.
let ret = unsafe { libfdt_bindgen::fdt_first_subnode(fdt, node) };
- fdt_err_or_option(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_next_subnode()` (C function).
@@ -166,7 +167,7 @@
// SAFETY: Accesses (read-only) are constrained to the DT totalsize.
let ret = unsafe { libfdt_bindgen::fdt_next_subnode(fdt, node) };
- fdt_err_or_option(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_address_cells()` (C function).
@@ -175,7 +176,7 @@
// SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
let ret = unsafe { libfdt_bindgen::fdt_address_cells(fdt, node) };
- Ok(fdt_err(ret)?.try_into().unwrap())
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_size_cells()` (C function).
@@ -184,7 +185,7 @@
// SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
let ret = unsafe { libfdt_bindgen::fdt_size_cells(fdt, node) };
- Ok(fdt_err(ret)?.try_into().unwrap())
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_get_name()` (C function).
@@ -194,7 +195,7 @@
// SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). On success, the
// function returns valid null terminating string and otherwise returned values are dropped.
let name = unsafe { libfdt_bindgen::fdt_get_name(fdt, node, &mut len) };
- let len = usize::try_from(fdt_err(len)?).unwrap().checked_add(1).unwrap();
+ let len = usize::try_from(FdtRawResult::from(len))?.checked_add(1).unwrap();
get_slice_at_ptr(self.as_fdt_slice(), name.cast(), len).ok_or(FdtError::Internal)
}
@@ -210,8 +211,7 @@
// function respects the passed number of characters.
unsafe { libfdt_bindgen::fdt_getprop_namelen(fdt, node, name, namelen, &mut len) };
- if let Some(len) = fdt_err_or_option(len)? {
- let len = usize::try_from(len).unwrap();
+ if let Some(len) = FdtRawResult::from(len).try_into()? {
let bytes = get_slice_at_ptr(self.as_fdt_slice(), prop.cast(), len);
Ok(Some(bytes.ok_or(FdtError::Internal)?))
@@ -227,7 +227,7 @@
// SAFETY: Accesses (read-only) are constrained to the DT totalsize.
let prop = unsafe { libfdt_bindgen::fdt_get_property_by_offset(fdt, offset, &mut len) };
- let data_len = fdt_err(len)?.try_into().unwrap();
+ let data_len = FdtRawResult::from(len).try_into()?;
// TODO(stable_feature(offset_of)): mem::offset_of!(fdt_property, data).
let data_offset = memoffset::offset_of!(libfdt_bindgen::fdt_property, data);
let len = data_offset.checked_add(data_len).ok_or(FdtError::Internal)?;
@@ -252,7 +252,7 @@
// SAFETY: Accesses (read-only) are constrained to the DT totalsize.
let ret = unsafe { libfdt_bindgen::fdt_first_property_offset(fdt, node) };
- fdt_err_or_option(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_next_property_offset()` (C function).
@@ -261,7 +261,7 @@
// SAFETY: Accesses (read-only) are constrained to the DT totalsize.
let ret = unsafe { libfdt_bindgen::fdt_next_property_offset(fdt, prev) };
- fdt_err_or_option(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_find_max_phandle()` (C function).
@@ -271,7 +271,7 @@
// SAFETY: Accesses (read-only) are constrained to the DT totalsize.
let ret = unsafe { libfdt_bindgen::fdt_find_max_phandle(fdt, &mut phandle) };
- fdt_err_expect_zero(ret)?;
+ FdtRawResult::from(ret).try_into()?;
phandle.try_into()
}
@@ -286,6 +286,13 @@
CStr::from_bytes_until_nul(bytes).map_err(|_| FdtError::Internal)
}
+
+ /// Safe wrapper around `fdt_open_into()` (C function).
+ fn open_into(&self, dest: &mut [u8]) -> Result<()> {
+ let fdt = self.as_fdt_slice().as_ptr().cast();
+
+ open_into(fdt, dest)
+ }
}
/// Wrapper for the read-write libfdt.h functions.
@@ -314,7 +321,7 @@
// SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
let ret = unsafe { libfdt_bindgen::fdt_nop_node(fdt, node) };
- fdt_err_expect_zero(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_add_subnode_namelen()` (C function).
@@ -325,7 +332,7 @@
// SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
let ret = unsafe { libfdt_bindgen::fdt_add_subnode_namelen(fdt, node, name, namelen) };
- fdt_err(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_setprop()` (C function).
@@ -338,7 +345,7 @@
// (validated by underlying libfdt).
let ret = unsafe { libfdt_bindgen::fdt_setprop(fdt, node, name, value, len) };
- fdt_err_expect_zero(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_setprop_placeholder()` (C function).
@@ -351,7 +358,7 @@
// SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
unsafe { libfdt_bindgen::fdt_setprop_placeholder(fdt, node, name, len, &mut data) };
- fdt_err_expect_zero(ret)?;
+ FdtRawResult::from(ret).try_into()?;
get_mut_slice_at_ptr(self.as_fdt_slice_mut(), data.cast(), size).ok_or(FdtError::Internal)
}
@@ -366,7 +373,7 @@
// (validated by underlying libfdt).
let ret = unsafe { libfdt_bindgen::fdt_setprop_inplace(fdt, node, name, value, len) };
- fdt_err_expect_zero(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_appendprop()` (C function).
@@ -378,7 +385,7 @@
// SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
let ret = unsafe { libfdt_bindgen::fdt_appendprop(fdt, node, name, value, len) };
- fdt_err_expect_zero(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_appendprop_addrrange()` (C function).
@@ -397,7 +404,7 @@
libfdt_bindgen::fdt_appendprop_addrrange(fdt, parent, node, name, addr, size)
};
- fdt_err_expect_zero(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_delprop()` (C function).
@@ -410,7 +417,7 @@
// being called when FdtNode instances are in use.
let ret = unsafe { libfdt_bindgen::fdt_delprop(fdt, node, name) };
- fdt_err_expect_zero(ret)
+ FdtRawResult::from(ret).try_into()
}
/// Safe wrapper around `fdt_nop_property()` (C function).
@@ -421,7 +428,49 @@
// library locates the node's property.
let ret = unsafe { libfdt_bindgen::fdt_nop_property(fdt, node, name) };
- fdt_err_expect_zero(ret)
+ FdtRawResult::from(ret).try_into()
+ }
+
+ /// Safe and aliasing-compatible wrapper around `fdt_open_into()` (C function).
+ ///
+ /// The C API allows both input (`const void*`) and output (`void *`) to point to the same
+ /// memory region but the borrow checker would reject an API such as
+ ///
+ /// self.open_into(&mut self.buffer)
+ ///
+ /// so this wrapper is provided to implement such a common aliasing case.
+ fn open_into_self(&mut self) -> Result<()> {
+ let fdt = self.as_fdt_slice_mut();
+
+ open_into(fdt.as_ptr().cast(), fdt)
+ }
+
+ /// Safe wrapper around `fdt_pack()` (C function).
+ fn pack(&mut self) -> Result<()> {
+ let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast();
+ // SAFETY: Accesses (R/W) are constrained to the DT totalsize (validated by ctor).
+ let ret = unsafe { libfdt_bindgen::fdt_pack(fdt) };
+
+ FdtRawResult::from(ret).try_into()
+ }
+
+ /// Wrapper around `fdt_overlay_apply()` (C function).
+ ///
+ /// # Safety
+ ///
+ /// This function safely wraps the C function call but is unsafe because the caller must
+ ///
+ /// - discard `overlay` as a &LibfdtMut because libfdt corrupts its header before returning;
+ /// - on error, discard `self` as a &LibfdtMut for the same reason.
+ unsafe fn overlay_apply(&mut self, overlay: &mut Self) -> Result<()> {
+ let fdt = self.as_fdt_slice_mut().as_mut_ptr().cast();
+ let overlay = overlay.as_fdt_slice_mut().as_mut_ptr().cast();
+ // SAFETY: Both pointers are valid because they come from references, and fdt_overlay_apply
+ // doesn't keep them after it returns. It may corrupt their contents if there is an error,
+ // but that's our caller's responsibility.
+ let ret = unsafe { libfdt_bindgen::fdt_overlay_apply(fdt, overlay) };
+
+ FdtRawResult::from(ret).try_into()
}
}
@@ -449,6 +498,19 @@
})
}
+fn open_into(fdt: *const u8, dest: &mut [u8]) -> Result<()> {
+ let fdt = fdt.cast();
+ let len = dest.len().try_into().map_err(|_| FdtError::Internal)?;
+ let dest = dest.as_mut_ptr().cast();
+ // SAFETY: Reads the whole fdt slice (based on the validated totalsize) and, if it fits, copies
+ // it to the (properly mutable) dest buffer of size len. On success, the resulting dest
+ // contains a valid DT with the nodes and properties of the original one but of a different
+ // size, reflected in its fdt_header::totalsize.
+ let ret = unsafe { libfdt_bindgen::fdt_open_into(fdt, dest, len) };
+
+ FdtRawResult::from(ret).try_into()
+}
+
// TODO(stable_feature(pointer_is_aligned)): p.is_aligned()
fn is_aligned<T>(p: *const T) -> bool {
(p as usize) % mem::align_of::<T>() == 0
diff --git a/libs/libfdt/src/result.rs b/libs/libfdt/src/result.rs
index 9643e1e..52291ca 100644
--- a/libs/libfdt/src/result.rs
+++ b/libs/libfdt/src/result.rs
@@ -14,7 +14,7 @@
//! Rust types related to the libfdt C integer results.
-use core::ffi::c_int;
+use core::ffi::{c_int, c_uint};
use core::fmt;
use core::result;
@@ -94,46 +94,99 @@
/// Result type with FdtError enum.
pub type Result<T> = result::Result<T, FdtError>;
-pub(crate) fn fdt_err(val: c_int) -> Result<c_int> {
- if val >= 0 {
- Ok(val)
- } else {
- Err(match -val as _ {
- libfdt_bindgen::FDT_ERR_NOTFOUND => FdtError::NotFound,
- libfdt_bindgen::FDT_ERR_EXISTS => FdtError::Exists,
- libfdt_bindgen::FDT_ERR_NOSPACE => FdtError::NoSpace,
- libfdt_bindgen::FDT_ERR_BADOFFSET => FdtError::BadOffset,
- libfdt_bindgen::FDT_ERR_BADPATH => FdtError::BadPath,
- libfdt_bindgen::FDT_ERR_BADPHANDLE => FdtError::BadPhandle,
- libfdt_bindgen::FDT_ERR_BADSTATE => FdtError::BadState,
- libfdt_bindgen::FDT_ERR_TRUNCATED => FdtError::Truncated,
- libfdt_bindgen::FDT_ERR_BADMAGIC => FdtError::BadMagic,
- libfdt_bindgen::FDT_ERR_BADVERSION => FdtError::BadVersion,
- libfdt_bindgen::FDT_ERR_BADSTRUCTURE => FdtError::BadStructure,
- libfdt_bindgen::FDT_ERR_BADLAYOUT => FdtError::BadLayout,
- libfdt_bindgen::FDT_ERR_INTERNAL => FdtError::Internal,
- libfdt_bindgen::FDT_ERR_BADNCELLS => FdtError::BadNCells,
- libfdt_bindgen::FDT_ERR_BADVALUE => FdtError::BadValue,
- libfdt_bindgen::FDT_ERR_BADOVERLAY => FdtError::BadOverlay,
- libfdt_bindgen::FDT_ERR_NOPHANDLES => FdtError::NoPhandles,
- libfdt_bindgen::FDT_ERR_BADFLAGS => FdtError::BadFlags,
- libfdt_bindgen::FDT_ERR_ALIGNMENT => FdtError::Alignment,
- _ => FdtError::Unknown(val),
- })
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub(crate) struct FdtRawResult(c_int);
+
+impl From<c_int> for FdtRawResult {
+ fn from(value: c_int) -> Self {
+ Self(value)
}
}
-pub(crate) fn fdt_err_expect_zero(val: c_int) -> Result<()> {
- match fdt_err(val)? {
- 0 => Ok(()),
- _ => Err(FdtError::Unknown(val)),
+impl TryFrom<FdtRawResult> for c_int {
+ type Error = FdtError;
+
+ fn try_from(res: FdtRawResult) -> Result<Self> {
+ use libfdt_bindgen::{
+ FDT_ERR_ALIGNMENT, FDT_ERR_BADFLAGS, FDT_ERR_BADLAYOUT, FDT_ERR_BADMAGIC,
+ FDT_ERR_BADNCELLS, FDT_ERR_BADOFFSET, FDT_ERR_BADOVERLAY, FDT_ERR_BADPATH,
+ FDT_ERR_BADPHANDLE, FDT_ERR_BADSTATE, FDT_ERR_BADSTRUCTURE, FDT_ERR_BADVALUE,
+ FDT_ERR_BADVERSION, FDT_ERR_EXISTS, FDT_ERR_INTERNAL, FDT_ERR_NOPHANDLES,
+ FDT_ERR_NOSPACE, FDT_ERR_NOTFOUND, FDT_ERR_TRUNCATED,
+ };
+ match res.0 {
+ x if x >= 0 => Ok(x),
+ x if x == -(FDT_ERR_NOTFOUND as c_int) => Err(FdtError::NotFound),
+ x if x == -(FDT_ERR_EXISTS as c_int) => Err(FdtError::Exists),
+ x if x == -(FDT_ERR_NOSPACE as c_int) => Err(FdtError::NoSpace),
+ x if x == -(FDT_ERR_BADOFFSET as c_int) => Err(FdtError::BadOffset),
+ x if x == -(FDT_ERR_BADPATH as c_int) => Err(FdtError::BadPath),
+ x if x == -(FDT_ERR_BADPHANDLE as c_int) => Err(FdtError::BadPhandle),
+ x if x == -(FDT_ERR_BADSTATE as c_int) => Err(FdtError::BadState),
+ x if x == -(FDT_ERR_TRUNCATED as c_int) => Err(FdtError::Truncated),
+ x if x == -(FDT_ERR_BADMAGIC as c_int) => Err(FdtError::BadMagic),
+ x if x == -(FDT_ERR_BADVERSION as c_int) => Err(FdtError::BadVersion),
+ x if x == -(FDT_ERR_BADSTRUCTURE as c_int) => Err(FdtError::BadStructure),
+ x if x == -(FDT_ERR_BADLAYOUT as c_int) => Err(FdtError::BadLayout),
+ x if x == -(FDT_ERR_INTERNAL as c_int) => Err(FdtError::Internal),
+ x if x == -(FDT_ERR_BADNCELLS as c_int) => Err(FdtError::BadNCells),
+ x if x == -(FDT_ERR_BADVALUE as c_int) => Err(FdtError::BadValue),
+ x if x == -(FDT_ERR_BADOVERLAY as c_int) => Err(FdtError::BadOverlay),
+ x if x == -(FDT_ERR_NOPHANDLES as c_int) => Err(FdtError::NoPhandles),
+ x if x == -(FDT_ERR_BADFLAGS as c_int) => Err(FdtError::BadFlags),
+ x if x == -(FDT_ERR_ALIGNMENT as c_int) => Err(FdtError::Alignment),
+ x => Err(FdtError::Unknown(x)),
+ }
}
}
-pub(crate) fn fdt_err_or_option(val: c_int) -> Result<Option<c_int>> {
- match fdt_err(val) {
- Ok(val) => Ok(Some(val)),
- Err(FdtError::NotFound) => Ok(None),
- Err(e) => Err(e),
+impl TryFrom<FdtRawResult> for Option<c_int> {
+ type Error = FdtError;
+
+ fn try_from(res: FdtRawResult) -> Result<Self> {
+ match res.try_into() {
+ Ok(n) => Ok(Some(n)),
+ Err(FdtError::NotFound) => Ok(None),
+ Err(e) => Err(e),
+ }
+ }
+}
+
+impl TryFrom<FdtRawResult> for c_uint {
+ type Error = FdtError;
+
+ fn try_from(res: FdtRawResult) -> Result<Self> {
+ Ok(c_int::try_from(res)?.try_into().unwrap())
+ }
+}
+
+impl TryFrom<FdtRawResult> for usize {
+ type Error = FdtError;
+
+ fn try_from(res: FdtRawResult) -> Result<Self> {
+ Ok(c_int::try_from(res)?.try_into().unwrap())
+ }
+}
+
+impl TryFrom<FdtRawResult> for Option<usize> {
+ type Error = FdtError;
+
+ fn try_from(res: FdtRawResult) -> Result<Self> {
+ match res.try_into() {
+ Ok(n) => Ok(Some(n)),
+ Err(FdtError::NotFound) => Ok(None),
+ Err(e) => Err(e),
+ }
+ }
+}
+
+impl TryFrom<FdtRawResult> for () {
+ type Error = FdtError;
+
+ fn try_from(res: FdtRawResult) -> Result<Self> {
+ match res.try_into()? {
+ 0 => Ok(()),
+ n => Err(FdtError::Unknown(n)),
+ }
}
}
diff --git a/libs/libfdt/tests/api_test.rs b/libs/libfdt/tests/api_test.rs
index ddc4538..c2ddda0 100644
--- a/libs/libfdt/tests/api_test.rs
+++ b/libs/libfdt/tests/api_test.rs
@@ -267,8 +267,8 @@
let node = fdt.node(node_path).unwrap().unwrap();
assert_eq!(Ok(None), node.subnode_with_name_bytes(name));
- let mut node = fdt.node_mut(node_path).unwrap().unwrap();
- node.add_subnode_with_namelen(subnode_name, len).unwrap();
+ let node = fdt.node_mut(node_path).unwrap().unwrap();
+ let _ = node.add_subnode_with_namelen(subnode_name, len).unwrap();
let node = fdt.node(node_path).unwrap().unwrap();
assert_ne!(Ok(None), node.subnode_with_name_bytes(name));
@@ -456,7 +456,7 @@
let mut data = vec![0_u8; 1000];
let fdt = Fdt::create_empty_tree(&mut data).unwrap();
- let mut root = fdt.root_mut().unwrap();
+ let root = fdt.root_mut().unwrap();
let names = [cstr!("a"), cstr!("b")];
root.add_subnodes(&names).unwrap();
diff --git a/pvmfw/platform.dts b/pvmfw/platform.dts
index cea1c33..275a1c9 100644
--- a/pvmfw/platform.dts
+++ b/pvmfw/platform.dts
@@ -52,11 +52,40 @@
cpus {
#address-cells = <1>;
#size-cells = <0>;
- cpu@0 {
+
+ cpu-map {
+ cluster0 {
+ core0 { cpu = <PLACEHOLDER>; };
+ core1 { cpu = <PLACEHOLDER>; };
+ core2 { cpu = <PLACEHOLDER>; };
+ core3 { cpu = <PLACEHOLDER>; };
+ core4 { cpu = <PLACEHOLDER>; };
+ core5 { cpu = <PLACEHOLDER>; };
+ };
+ cluster1 {
+ core0 { cpu = <PLACEHOLDER>; };
+ core1 { cpu = <PLACEHOLDER>; };
+ core2 { cpu = <PLACEHOLDER>; };
+ core3 { cpu = <PLACEHOLDER>; };
+ core4 { cpu = <PLACEHOLDER>; };
+ core5 { cpu = <PLACEHOLDER>; };
+ };
+ cluster2 {
+ core0 { cpu = <PLACEHOLDER>; };
+ core1 { cpu = <PLACEHOLDER>; };
+ core2 { cpu = <PLACEHOLDER>; };
+ core3 { cpu = <PLACEHOLDER>; };
+ core4 { cpu = <PLACEHOLDER>; };
+ core5 { cpu = <PLACEHOLDER>; };
+ };
+ };
+
+ cpu0: cpu@0 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <0>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table0>;
opp_table0: opp-table-0 {
compatible = "operating-points-v2";
@@ -83,11 +112,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@1 {
+ cpu1: cpu@1 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <1>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table1>;
opp_table1: opp-table-1 {
compatible = "operating-points-v2";
@@ -114,11 +144,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@2 {
+ cpu2: cpu@2 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <2>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table2>;
opp_table2: opp-table-2 {
compatible = "operating-points-v2";
@@ -145,11 +176,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@3 {
+ cpu3: cpu@3 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <3>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table3>;
opp_table3: opp-table-3 {
compatible = "operating-points-v2";
@@ -176,11 +208,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@4 {
+ cpu4: cpu@4 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <4>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table4>;
opp_table4: opp-table-4 {
compatible = "operating-points-v2";
@@ -207,11 +240,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@5 {
+ cpu5: cpu@5 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <5>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table5>;
opp_table5: opp-table-5 {
compatible = "operating-points-v2";
@@ -238,11 +272,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@6 {
+ cpu6: cpu@6 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <6>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table6>;
opp_table6: opp-table-6 {
compatible = "operating-points-v2";
@@ -269,11 +304,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@7 {
+ cpu7: cpu@7 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <7>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table7>;
opp_table7: opp-table-7 {
compatible = "operating-points-v2";
@@ -300,12 +336,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@8 {
+ cpu8: cpu@8 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <8>;
-
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table8>;
opp_table8: opp-table-8 {
compatible = "operating-points-v2";
@@ -332,11 +368,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@9 {
+ cpu9: cpu@9 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <9>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table9>;
opp_table9: opp-table-9 {
compatible = "operating-points-v2";
@@ -363,11 +400,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@10 {
+ cpu10: cpu@10 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <10>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table10>;
opp_table10: opp-table-10 {
compatible = "operating-points-v2";
@@ -394,11 +432,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@11 {
+ cpu11: cpu@11 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <11>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table11>;
opp_table11: opp-table-11 {
compatible = "operating-points-v2";
@@ -425,11 +464,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@12 {
+ cpu12: cpu@12 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <12>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table12>;
opp_table12: opp-table-12 {
compatible = "operating-points-v2";
@@ -456,11 +496,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@13 {
+ cpu13: cpu@13 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <13>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table13>;
opp_table13: opp-table-13 {
compatible = "operating-points-v2";
@@ -487,11 +528,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@14 {
+ cpu14: cpu@14 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <14>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table14>;
opp_table14: opp-table-14 {
compatible = "operating-points-v2";
@@ -518,11 +560,12 @@
opp20 { opp-hz = <PLACEHOLDER2>; };
};
};
- cpu@15 {
+ cpu15: cpu@15 {
device_type = "cpu";
compatible = "arm,arm-v8";
enable-method = "psci";
reg = <15>;
+ capacity-dmips-mhz = <PLACEHOLDER>;
operating-points-v2 = <&opp_table15>;
opp_table15: opp-table-15 {
compatible = "operating-points-v2";
diff --git a/pvmfw/src/fdt.rs b/pvmfw/src/fdt.rs
index f20451a..311f467 100644
--- a/pvmfw/src/fdt.rs
+++ b/pvmfw/src/fdt.rs
@@ -21,6 +21,7 @@
use crate::RebootReason;
use alloc::collections::BTreeMap;
use alloc::ffi::CString;
+use alloc::format;
use alloc::vec::Vec;
use core::cmp::max;
use core::cmp::min;
@@ -37,6 +38,7 @@
use libfdt::FdtError;
use libfdt::FdtNode;
use libfdt::FdtNodeMut;
+use libfdt::Phandle;
use log::debug;
use log::error;
use log::info;
@@ -178,10 +180,10 @@
.setprop_inplace(cstr!("reg"), [addr.to_be(), size.to_be()].as_bytes())
}
-//TODO: Need to add info for cpu capacity
#[derive(Debug, Default)]
struct CpuInfo {
opptable_info: Option<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>>,
+ cpu_capacity: Option<u32>,
}
impl CpuInfo {
@@ -200,10 +202,64 @@
Ok(table)
}
-fn read_cpu_info_from(fdt: &Fdt) -> libfdt::Result<ArrayVec<[CpuInfo; DeviceTreeInfo::MAX_CPUS]>> {
+#[derive(Debug, Default)]
+struct ClusterTopology {
+ // TODO: Support multi-level clusters & threads.
+ cores: [Option<usize>; ClusterTopology::MAX_CORES_PER_CLUSTER],
+}
+
+impl ClusterTopology {
+ const MAX_CORES_PER_CLUSTER: usize = 6;
+}
+
+#[derive(Debug, Default)]
+struct CpuTopology {
+ // TODO: Support sockets.
+ clusters: [Option<ClusterTopology>; CpuTopology::MAX_CLUSTERS],
+}
+
+impl CpuTopology {
+ const MAX_CLUSTERS: usize = 3;
+}
+
+fn read_cpu_map_from(fdt: &Fdt) -> libfdt::Result<Option<BTreeMap<Phandle, (usize, usize)>>> {
+ let Some(cpu_map) = fdt.node(cstr!("/cpus/cpu-map"))? else {
+ return Ok(None);
+ };
+
+ let mut topology = BTreeMap::new();
+ for n in 0..CpuTopology::MAX_CLUSTERS {
+ let name = CString::new(format!("cluster{n}")).unwrap();
+ let Some(cluster) = cpu_map.subnode(&name)? else {
+ break;
+ };
+ for m in 0..ClusterTopology::MAX_CORES_PER_CLUSTER {
+ let name = CString::new(format!("core{m}")).unwrap();
+ let Some(core) = cluster.subnode(&name)? else {
+ break;
+ };
+ let cpu = core.getprop_u32(cstr!("cpu"))?.ok_or(FdtError::NotFound)?;
+ let prev = topology.insert(cpu.try_into()?, (n, m));
+ if prev.is_some() {
+ return Err(FdtError::BadValue);
+ }
+ }
+ }
+
+ Ok(Some(topology))
+}
+
+fn read_cpu_info_from(
+ fdt: &Fdt,
+) -> libfdt::Result<(ArrayVec<[CpuInfo; DeviceTreeInfo::MAX_CPUS]>, Option<CpuTopology>)> {
let mut cpus = ArrayVec::new();
+
+ let cpu_map = read_cpu_map_from(fdt)?;
+ let mut topology: CpuTopology = Default::default();
+
let mut cpu_nodes = fdt.compatible_nodes(cstr!("arm,arm-v8"))?;
- for cpu in cpu_nodes.by_ref().take(cpus.capacity()) {
+ for (idx, cpu) in cpu_nodes.by_ref().take(cpus.capacity()).enumerate() {
+ let cpu_capacity = cpu.getprop_u32(cstr!("capacity-dmips-mhz"))?;
let opp_phandle = cpu.getprop_u32(cstr!("operating-points-v2"))?;
let opptable_info = if let Some(phandle) = opp_phandle {
let phandle = phandle.try_into()?;
@@ -212,21 +268,31 @@
} else {
None
};
- let info = CpuInfo { opptable_info };
+ let info = CpuInfo { opptable_info, cpu_capacity };
cpus.push(info);
+
+ if let Some(ref cpu_map) = cpu_map {
+ let phandle = cpu.get_phandle()?.ok_or(FdtError::NotFound)?;
+ let (cluster, core_idx) = cpu_map.get(&phandle).ok_or(FdtError::BadValue)?;
+ let cluster = topology.clusters[*cluster].get_or_insert(Default::default());
+ if cluster.cores[*core_idx].is_some() {
+ return Err(FdtError::BadValue);
+ }
+ cluster.cores[*core_idx] = Some(idx);
+ }
}
+
if cpu_nodes.next().is_some() {
warn!("DT has more than {} CPU nodes: discarding extra nodes.", cpus.capacity());
}
- Ok(cpus)
+ Ok((cpus, cpu_map.map(|_| topology)))
}
fn validate_cpu_info(cpus: &[CpuInfo]) -> Result<(), FdtValidationError> {
if cpus.is_empty() {
return Err(FdtValidationError::InvalidCpuCount(0));
}
-
Ok(())
}
@@ -304,16 +370,53 @@
Ok(node)
}
-fn patch_cpus(fdt: &mut Fdt, cpus: &[CpuInfo]) -> libfdt::Result<()> {
+fn patch_cpus(
+ fdt: &mut Fdt,
+ cpus: &[CpuInfo],
+ topology: &Option<CpuTopology>,
+) -> libfdt::Result<()> {
const COMPAT: &CStr = cstr!("arm,arm-v8");
+ let mut cpu_phandles = Vec::new();
for (idx, cpu) in cpus.iter().enumerate() {
- let cur = get_nth_compatible(fdt, idx, COMPAT)?.ok_or(FdtError::NoSpace)?;
+ let mut cur = get_nth_compatible(fdt, idx, COMPAT)?.ok_or(FdtError::NoSpace)?;
+ let phandle = cur.as_node().get_phandle()?.unwrap();
+ cpu_phandles.push(phandle);
+ if let Some(cpu_capacity) = cpu.cpu_capacity {
+ cur.setprop_inplace(cstr!("capacity-dmips-mhz"), &cpu_capacity.to_be_bytes())?;
+ }
patch_opptable(cur, cpu.opptable_info)?;
}
let mut next = get_nth_compatible(fdt, cpus.len(), COMPAT)?;
while let Some(current) = next {
next = current.delete_and_next_compatible(COMPAT)?;
}
+
+ if let Some(topology) = topology {
+ for (n, cluster) in topology.clusters.iter().enumerate() {
+ let path = CString::new(format!("/cpus/cpu-map/cluster{n}")).unwrap();
+ let cluster_node = fdt.node_mut(&path)?.unwrap();
+ if let Some(cluster) = cluster {
+ let mut iter = cluster_node.first_subnode()?;
+ for core in cluster.cores {
+ let mut core_node = iter.unwrap();
+ iter = if let Some(core_idx) = core {
+ let phandle = *cpu_phandles.get(core_idx).unwrap();
+ let value = u32::from(phandle).to_be_bytes();
+ core_node.setprop_inplace(cstr!("cpu"), &value)?;
+ core_node.next_subnode()?
+ } else {
+ core_node.delete_and_next_subnode()?
+ };
+ }
+ assert!(iter.is_none());
+ } else {
+ cluster_node.nop()?;
+ }
+ }
+ } else {
+ fdt.node_mut(cstr!("/cpus/cpu-map"))?.unwrap().nop()?;
+ }
+
Ok(())
}
@@ -340,7 +443,7 @@
vm_ref_dt: &Fdt,
props_info: &BTreeMap<CString, Vec<u8>>,
) -> libfdt::Result<()> {
- let mut root_vm_dt = vm_dt.root_mut()?;
+ let root_vm_dt = vm_dt.root_mut()?;
let mut avf_vm_dt = root_vm_dt.add_subnode(cstr!("avf"))?;
// TODO(b/318431677): Validate nodes beyond /avf.
let avf_node = vm_ref_dt.node(cstr!("/avf"))?.ok_or(FdtError::NotFound)?;
@@ -756,6 +859,7 @@
pub memory_range: Range<usize>,
bootargs: Option<CString>,
cpus: ArrayVec<[CpuInfo; DeviceTreeInfo::MAX_CPUS]>,
+ cpu_topology: Option<CpuTopology>,
pci_info: PciInfo,
serial_info: SerialInfo,
pub swiotlb_info: SwiotlbInfo,
@@ -865,7 +969,7 @@
RebootReason::InvalidFdt
})?;
- let cpus = read_cpu_info_from(fdt).map_err(|e| {
+ let (cpus, cpu_topology) = read_cpu_info_from(fdt).map_err(|e| {
error!("Failed to read CPU info from DT: {e}");
RebootReason::InvalidFdt
})?;
@@ -930,6 +1034,7 @@
memory_range,
bootargs,
cpus,
+ cpu_topology,
pci_info,
serial_info,
swiotlb_info,
@@ -956,7 +1061,7 @@
RebootReason::InvalidFdt
})?;
}
- patch_cpus(fdt, &info.cpus).map_err(|e| {
+ patch_cpus(fdt, &info.cpus, &info.cpu_topology).map_err(|e| {
error!("Failed to patch cpus to DT: {e}");
RebootReason::InvalidFdt
})?;
diff --git a/tests/hostside/Android.bp b/tests/hostside/Android.bp
index e3d9cbe..13a9925 100644
--- a/tests/hostside/Android.bp
+++ b/tests/hostside/Android.bp
@@ -37,6 +37,8 @@
"lpunpack",
"sign_virt_apex",
"simg2img",
+ "dtdiff",
+ "dtc", // for dtdiff
],
// java_test_host doesn't have data_native_libs but jni_libs can be used to put
// native modules under ./lib directory.
@@ -51,5 +53,6 @@
"liblp",
"libsparse",
"libz",
+ "libfdt", // for dtc
],
}
diff --git a/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java b/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java
index 2cd4577..20b5a50 100644
--- a/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java
+++ b/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java
@@ -47,6 +47,7 @@
import com.android.tradefed.device.TestDevice;
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestMetrics;
import com.android.tradefed.util.CommandResult;
+import com.android.tradefed.util.CommandStatus;
import com.android.tradefed.util.FileUtil;
import com.android.tradefed.util.RunUtil;
import com.android.tradefed.util.xml.AbstractXmlParser;
@@ -985,23 +986,71 @@
@Test
public void testDeviceAssignment() throws Exception {
- assumeProtectedVm();
+ // Check for preconditions
assumeVfioPlatformSupported();
List<String> devices = getAssignableDevices();
assumeFalse("no assignable devices", devices.isEmpty());
+ String vmFdtPath = "/sys/firmware/fdt";
+ File testDir = FileUtil.createTempDir("device_assignment_test");
+ File baseFdtFile = new File(testDir, "base_fdt.dtb");
+ File fdtFile = new File(testDir, "fdt.dtb");
+
+ // Generates baseline DT
+ launchWithDeviceAssignment(/* device= */ null);
+ assertThat(mMicrodroidDevice.pullFile(vmFdtPath, baseFdtFile)).isTrue();
+ getAndroidDevice().shutdownMicrodroid(mMicrodroidDevice);
+
+ // Prepares to run dtdiff. It requires dtc.
+ File dtdiff = findTestFile("dtdiff");
+ RunUtil runner = new RunUtil();
+ String separator = System.getProperty("path.separator");
+ String path = dtdiff.getParent() + separator + System.getenv("PATH");
+ runner.setEnvVariable("PATH", path);
+
+ // Try assign devices one by one
+ for (String device : devices) {
+ assertThat(device).isNotNull();
+ launchWithDeviceAssignment(device);
+ assertThat(mMicrodroidDevice.pullFile(vmFdtPath, fdtFile)).isTrue();
+ getAndroidDevice().shutdownMicrodroid(mMicrodroidDevice);
+
+ CommandResult result =
+ runner.runTimedCmd(
+ 500,
+ dtdiff.getAbsolutePath(),
+ baseFdtFile.getPath(),
+ fdtFile.getPath());
+
+ assertWithMessage(
+ "VM's device tree hasn't changed when assigning "
+ + device
+ + ", result="
+ + result)
+ .that(result.getStatus())
+ .isNotEqualTo(CommandStatus.SUCCESS);
+ }
+
+ mMicrodroidDevice = null;
+ }
+
+ private void launchWithDeviceAssignment(String device) throws Exception {
final String configPath = "assets/" + mOs + "/vm_config.json";
- mMicrodroidDevice =
+
+ MicrodroidBuilder builder =
MicrodroidBuilder.fromDevicePath(getPathForPackage(PACKAGE_NAME), configPath)
.debugLevel("full")
.memoryMib(minMemorySize())
.cpuTopology("match_host")
- .protectedVm(true)
- .addAssignableDevice(devices.get(0))
- .build(getAndroidDevice());
+ .protectedVm(mProtectedVm);
+ if (device != null) {
+ builder.addAssignableDevice(device);
+ }
+ mMicrodroidDevice = builder.build(getAndroidDevice());
- mMicrodroidDevice.waitForBootComplete(BOOT_COMPLETE_TIMEOUT);
+ assertThat(mMicrodroidDevice.waitForBootComplete(BOOT_COMPLETE_TIMEOUT)).isTrue();
+ assertThat(mMicrodroidDevice.enableAdbRoot()).isTrue();
}
@Test
diff --git a/virtualizationmanager/Android.bp b/virtualizationmanager/Android.bp
index 48b5cd1..c46385c 100644
--- a/virtualizationmanager/Android.bp
+++ b/virtualizationmanager/Android.bp
@@ -35,6 +35,7 @@
"libavflog",
"libbase_rust",
"libbinder_rs",
+ "libcfg_if",
"libclap",
"libcstr",
"libcommand_fds",
diff --git a/virtualizationmanager/src/crosvm.rs b/virtualizationmanager/src/crosvm.rs
index 26b7dff..3380df3 100644
--- a/virtualizationmanager/src/crosvm.rs
+++ b/virtualizationmanager/src/crosvm.rs
@@ -812,7 +812,11 @@
if config.host_cpu_topology {
if cfg!(virt_cpufreq) {
command.arg("--host-cpu-topology");
- command.arg("--virt-cpufreq");
+ cfg_if::cfg_if! {
+ if #[cfg(any(target_arch = "aarch64"))] {
+ command.arg("--virt-cpufreq");
+ }
+ }
} else if let Some(cpus) = get_num_cpus() {
command.arg("--cpus").arg(cpus.to_string());
} else {
diff --git a/virtualizationmanager/src/dt_overlay.rs b/virtualizationmanager/src/dt_overlay.rs
index 83f7734..71d3a26 100644
--- a/virtualizationmanager/src/dt_overlay.rs
+++ b/virtualizationmanager/src/dt_overlay.rs
@@ -57,20 +57,19 @@
let fdt =
Fdt::create_empty_tree(buffer).map_err(|e| anyhow!("Failed to create empty Fdt: {e:?}"))?;
- let mut root = fdt.root_mut().map_err(|e| anyhow!("Failed to get root: {e:?}"))?;
+ let root = fdt.root_mut().map_err(|e| anyhow!("Failed to get root: {e:?}"))?;
let mut node =
root.add_subnode(cstr!("fragment@0")).map_err(|e| anyhow!("Failed to fragment: {e:?}"))?;
node.setprop(cstr!("target-path"), b"/\0")
.map_err(|e| anyhow!("Failed to set target-path: {e:?}"))?;
- let mut node = node
+ let node = node
.add_subnode(cstr!("__overlay__"))
.map_err(|e| anyhow!("Failed to __overlay__ node: {e:?}"))?;
if !untrusted_props.is_empty() {
let mut node = node
.add_subnode(AVF_NODE_NAME)
- .map_err(|e| anyhow!("Failed to add avf node: {e:?}"))?;
- let mut node = node
+ .map_err(|e| anyhow!("Failed to add avf node: {e:?}"))?
.add_subnode(UNTRUSTED_NODE_NAME)
.map_err(|e| anyhow!("Failed to add /avf/untrusted node: {e:?}"))?;
for (name, value) in untrusted_props {
diff --git a/vmbase/example/src/main.rs b/vmbase/example/src/main.rs
index 6f513ee..48b24be 100644
--- a/vmbase/example/src/main.rs
+++ b/vmbase/example/src/main.rs
@@ -181,7 +181,7 @@
info!("FDT successfully unpacked.");
let path = cstr!("/memory");
- let mut node = writer.node_mut(path).unwrap().unwrap();
+ let node = writer.node_mut(path).unwrap().unwrap();
let name = cstr!("child");
let mut child = node.add_subnode(name).unwrap();
info!("Created subnode '{}/{}'.", path.to_str().unwrap(), name.to_str().unwrap());