[vmbase] Move SwiotlbInfo from pvmfw to vmbase
for reuse in both pvmfw and rialto to init shared memory.
Bug: 284462758
Test: m pvmfw_img
Change-Id: Ibe7b8b1bb8d10fbbdc4ec3348e801084c9a93bfd
diff --git a/pvmfw/src/crypto.rs b/pvmfw/src/crypto.rs
index d607bee..3d9c8d1 100644
--- a/pvmfw/src/crypto.rs
+++ b/pvmfw/src/crypto.rs
@@ -21,8 +21,6 @@
use core::num::NonZeroU32;
use core::ptr;
-use crate::cstr;
-
use bssl_ffi::CRYPTO_library_init;
use bssl_ffi::ERR_get_error_line;
use bssl_ffi::ERR_lib_error_string;
@@ -37,6 +35,7 @@
use bssl_ffi::EVP_AEAD;
use bssl_ffi::EVP_AEAD_CTX;
use bssl_ffi::HKDF;
+use vmbase::cstr;
#[derive(Debug)]
pub struct Error {
diff --git a/pvmfw/src/dice.rs b/pvmfw/src/dice.rs
index 3116456..fbab013 100644
--- a/pvmfw/src/dice.rs
+++ b/pvmfw/src/dice.rs
@@ -14,17 +14,16 @@
//! Support for DICE derivation and BCC generation.
-use crate::cstr;
use core::ffi::c_void;
use core::mem::size_of;
use core::slice;
-use vmbase::memory::flushed_zeroize;
-
use diced_open_dice::{
bcc_format_config_descriptor, bcc_handover_main_flow, hash, Config, DiceMode, Hash,
InputValues, HIDDEN_SIZE,
};
use pvmfw_avb::{DebugLevel, Digest, VerifiedBootData};
+use vmbase::cstr;
+use vmbase::memory::flushed_zeroize;
fn to_dice_mode(debug_level: DebugLevel) -> DiceMode {
match debug_level {
diff --git a/pvmfw/src/fdt.rs b/pvmfw/src/fdt.rs
index d4c8385..b5b1958 100644
--- a/pvmfw/src/fdt.rs
+++ b/pvmfw/src/fdt.rs
@@ -15,7 +15,6 @@
//! High-level FDT functions.
use crate::bootargs::BootArgsIterator;
-use crate::cstr;
use crate::helpers::GUEST_PAGE_SIZE;
use crate::Box;
use crate::RebootReason;
@@ -38,6 +37,8 @@
use log::info;
use log::warn;
use tinyvec::ArrayVec;
+use vmbase::cstr;
+use vmbase::fdt::SwiotlbInfo;
use vmbase::layout::{crosvm::MEM_START, MAX_VIRT_ADDR};
use vmbase::memory::SIZE_4KB;
use vmbase::util::flatten;
@@ -430,36 +431,6 @@
Ok(())
}
-#[derive(Debug)]
-pub struct SwiotlbInfo {
- addr: Option<usize>,
- size: usize,
- align: Option<usize>,
-}
-
-impl SwiotlbInfo {
- /// Creates a `SwiotlbInfo` struct from the given device tree.
- pub fn new_from_fdt(fdt: &Fdt) -> libfdt::Result<SwiotlbInfo> {
- let node =
- fdt.compatible_nodes(cstr!("restricted-dma-pool"))?.next().ok_or(FdtError::NotFound)?;
-
- let (addr, size, align) = if let Some(mut reg) = node.reg()? {
- let reg = reg.next().ok_or(FdtError::NotFound)?;
- let size = reg.size.ok_or(FdtError::NotFound)?;
- (Some(reg.addr.try_into().unwrap()), size.try_into().unwrap(), None)
- } else {
- let size = node.getprop_u64(cstr!("size"))?.ok_or(FdtError::NotFound)?;
- let align = node.getprop_u64(cstr!("alignment"))?.ok_or(FdtError::NotFound)?;
- (None, size.try_into().unwrap(), Some(align.try_into().unwrap()))
- };
- Ok(Self { addr, size, align })
- }
-
- pub fn fixed_range(&self) -> Option<Range<usize>> {
- self.addr.map(|addr| addr..addr + self.size)
- }
-}
-
fn validate_swiotlb_info(
swiotlb_info: &SwiotlbInfo,
memory: &Range<usize>,
diff --git a/pvmfw/src/helpers.rs b/pvmfw/src/helpers.rs
index 5ad721e..8981408 100644
--- a/pvmfw/src/helpers.rs
+++ b/pvmfw/src/helpers.rs
@@ -18,11 +18,3 @@
pub const GUEST_PAGE_SIZE: usize = SIZE_4KB;
pub const PVMFW_PAGE_SIZE: usize = PAGE_SIZE;
-
-/// Create &CStr out of &str literal
-#[macro_export]
-macro_rules! cstr {
- ($str:literal) => {{
- core::ffi::CStr::from_bytes_with_nul(concat!($str, "\0").as_bytes()).unwrap()
- }};
-}
diff --git a/vmbase/Android.bp b/vmbase/Android.bp
index 6db9ff8..0ef47db 100644
--- a/vmbase/Android.bp
+++ b/vmbase/Android.bp
@@ -67,6 +67,7 @@
"libbuddy_system_allocator",
"libfdtpci",
"libhyp",
+ "liblibfdt",
"liblog_rust_nostd",
"libonce_cell_nostd",
"libsmccc",
diff --git a/vmbase/src/fdt.rs b/vmbase/src/fdt.rs
new file mode 100644
index 0000000..537ca03
--- /dev/null
+++ b/vmbase/src/fdt.rs
@@ -0,0 +1,54 @@
+// Copyright 2023, The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//! High-level FDT functions.
+
+use crate::cstr;
+use core::ops::Range;
+use libfdt::{self, Fdt, FdtError};
+
+/// Represents information about a SWIOTLB buffer.
+#[derive(Debug)]
+pub struct SwiotlbInfo {
+ /// The address of the SWIOTLB buffer, if available.
+ pub addr: Option<usize>,
+ /// The size of the SWIOTLB buffer.
+ pub size: usize,
+ /// The alignment of the SWIOTLB buffer, if available.
+ pub align: Option<usize>,
+}
+
+impl SwiotlbInfo {
+ /// Creates a `SwiotlbInfo` struct from the given device tree.
+ pub fn new_from_fdt(fdt: &Fdt) -> libfdt::Result<SwiotlbInfo> {
+ let node =
+ fdt.compatible_nodes(cstr!("restricted-dma-pool"))?.next().ok_or(FdtError::NotFound)?;
+
+ let (addr, size, align) = if let Some(mut reg) = node.reg()? {
+ let reg = reg.next().ok_or(FdtError::NotFound)?;
+ let size = reg.size.ok_or(FdtError::NotFound)?;
+ (Some(reg.addr.try_into().unwrap()), size.try_into().unwrap(), None)
+ } else {
+ let size = node.getprop_u64(cstr!("size"))?.ok_or(FdtError::NotFound)?;
+ let align = node.getprop_u64(cstr!("alignment"))?.ok_or(FdtError::NotFound)?;
+ (None, size.try_into().unwrap(), Some(align.try_into().unwrap()))
+ };
+ Ok(Self { addr, size, align })
+ }
+
+ /// Returns the fixed range of memory mapped by the SWIOTLB buffer, if available.
+ pub fn fixed_range(&self) -> Option<Range<usize>> {
+ self.addr.map(|addr| addr..addr + self.size)
+ }
+}
diff --git a/vmbase/src/lib.rs b/vmbase/src/lib.rs
index ebb3707..54f3384 100644
--- a/vmbase/src/lib.rs
+++ b/vmbase/src/lib.rs
@@ -22,6 +22,7 @@
mod bionic;
pub mod console;
mod entry;
+pub mod fdt;
pub mod layout;
mod linker;
pub mod logger;
diff --git a/vmbase/src/util.rs b/vmbase/src/util.rs
index 8c230a1..7fe6015 100644
--- a/vmbase/src/util.rs
+++ b/vmbase/src/util.rs
@@ -16,6 +16,14 @@
use core::ops::Range;
+/// Create &CStr out of &str literal
+#[macro_export]
+macro_rules! cstr {
+ ($str:literal) => {{
+ core::ffi::CStr::from_bytes_with_nul(concat!($str, "\0").as_bytes()).unwrap()
+ }};
+}
+
/// Flatten [[T; N]] into &[T]
/// TODO: use slice::flatten when it graduates from experimental
pub fn flatten<T, const N: usize>(original: &[[T; N]]) -> &[T] {