[vmbase] Move RangeExt to vmbase for reuse
in both pvmfw and rialto.
No behavior change in this cl.
Bug: 284462758
Test: m pvmfw_img
Change-Id: Ifda7f4a2a08ebb6646c3c7e189a77c5ff3fe74a3
diff --git a/pvmfw/src/entry.rs b/pvmfw/src/entry.rs
index a2bd156..19ba9c3 100644
--- a/pvmfw/src/entry.rs
+++ b/pvmfw/src/entry.rs
@@ -18,7 +18,6 @@
use crate::crypto;
use crate::fdt;
use crate::heap;
-use crate::helpers::RangeExt as _;
use crate::memory::{self, MemoryTracker, MEMORY};
use crate::rand;
use core::arch::asm;
@@ -32,6 +31,7 @@
use log::info;
use log::warn;
use log::LevelFilter;
+use vmbase::util::RangeExt as _;
use vmbase::{
console, layout, logger, main,
memory::{min_dcache_line_size, SIZE_2MB, SIZE_4KB},
diff --git a/pvmfw/src/fdt.rs b/pvmfw/src/fdt.rs
index a68cf3a..ea002df 100644
--- a/pvmfw/src/fdt.rs
+++ b/pvmfw/src/fdt.rs
@@ -16,7 +16,6 @@
use crate::bootargs::BootArgsIterator;
use crate::cstr;
-use crate::helpers::RangeExt as _;
use crate::helpers::GUEST_PAGE_SIZE;
use crate::memory::MAX_ADDR;
use crate::Box;
@@ -43,6 +42,7 @@
use vmbase::layout::crosvm::MEM_START;
use vmbase::memory::SIZE_4KB;
use vmbase::util::flatten;
+use vmbase::util::RangeExt as _;
/// Extract from /config the address range containing the pre-loaded kernel. Absence of /config is
/// not an error.
diff --git a/pvmfw/src/helpers.rs b/pvmfw/src/helpers.rs
index b22acc1..5ad721e 100644
--- a/pvmfw/src/helpers.rs
+++ b/pvmfw/src/helpers.rs
@@ -14,31 +14,11 @@
//! Miscellaneous helper functions.
-use core::ops::Range;
use vmbase::memory::{PAGE_SIZE, SIZE_4KB};
pub const GUEST_PAGE_SIZE: usize = SIZE_4KB;
pub const PVMFW_PAGE_SIZE: usize = PAGE_SIZE;
-/// Trait to check containment of one range within another.
-pub(crate) trait RangeExt {
- /// Returns true if `self` is contained within the `other` range.
- fn is_within(&self, other: &Self) -> bool;
-
- /// Returns true if `self` overlaps with the `other` range.
- fn overlaps(&self, other: &Self) -> bool;
-}
-
-impl<T: PartialOrd> RangeExt for Range<T> {
- fn is_within(&self, other: &Self) -> bool {
- self.start >= other.start && self.end <= other.end
- }
-
- fn overlaps(&self, other: &Self) -> bool {
- self.start < other.end && other.start < self.end
- }
-}
-
/// Create &CStr out of &str literal
#[macro_export]
macro_rules! cstr {
diff --git a/pvmfw/src/memory.rs b/pvmfw/src/memory.rs
index 30c11eb..216a621 100644
--- a/pvmfw/src/memory.rs
+++ b/pvmfw/src/memory.rs
@@ -16,7 +16,7 @@
#![deny(unsafe_op_in_unsafe_fn)]
-use crate::helpers::{RangeExt, PVMFW_PAGE_SIZE};
+use crate::helpers::PVMFW_PAGE_SIZE;
use aarch64_paging::idmap::IdMap;
use aarch64_paging::paging::{Attributes, Descriptor, MemoryRegion as VaRange};
use aarch64_paging::MapError;
@@ -44,7 +44,7 @@
MMIO_LAZY_MAP_FLAG, SIZE_2MB, SIZE_4KB, SIZE_4MB,
},
tlbi,
- util::align_up,
+ util::{align_up, RangeExt as _},
};
/// First address that can't be translated by a level 1 TTBR0_EL1.
diff --git a/pvmfw/src/virtio/hal.rs b/pvmfw/src/virtio/hal.rs
index ce246b1..c8b279d 100644
--- a/pvmfw/src/virtio/hal.rs
+++ b/pvmfw/src/virtio/hal.rs
@@ -15,7 +15,6 @@
//! HAL for the virtio_drivers crate.
use super::pci::PCI_INFO;
-use crate::helpers::RangeExt as _;
use crate::memory::{alloc_shared, dealloc_shared};
use core::alloc::Layout;
use core::mem::size_of;
@@ -23,6 +22,7 @@
use log::trace;
use virtio_drivers::{BufferDirection, Hal, PhysAddr, PAGE_SIZE};
use vmbase::memory::{phys_to_virt, virt_to_phys};
+use vmbase::util::RangeExt as _;
/// The alignment to use for the temporary buffers allocated by `HalImpl::share`. There doesn't seem
/// to be any particular alignment required by VirtIO for these, so 16 bytes should be enough to
diff --git a/vmbase/src/util.rs b/vmbase/src/util.rs
index 7396edc..8c230a1 100644
--- a/vmbase/src/util.rs
+++ b/vmbase/src/util.rs
@@ -14,6 +14,8 @@
//! Utility functions.
+use core::ops::Range;
+
/// 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] {
@@ -70,3 +72,22 @@
r.checked_div(den)
}
+
+/// Trait to check containment of one range within another.
+pub trait RangeExt {
+ /// Returns true if `self` is contained within the `other` range.
+ fn is_within(&self, other: &Self) -> bool;
+
+ /// Returns true if `self` overlaps with the `other` range.
+ fn overlaps(&self, other: &Self) -> bool;
+}
+
+impl<T: PartialOrd> RangeExt for Range<T> {
+ fn is_within(&self, other: &Self) -> bool {
+ self.start >= other.start && self.end <= other.end
+ }
+
+ fn overlaps(&self, other: &Self) -> bool {
+ self.start < other.end && other.start < self.end
+ }
+}