Merge "[pvmfw][vmbase] Add constant PAGE_SIZE to vmbase"
diff --git a/pvmfw/src/helpers.rs b/pvmfw/src/helpers.rs
index aa3d9e9..b22acc1 100644
--- a/pvmfw/src/helpers.rs
+++ b/pvmfw/src/helpers.rs
@@ -24,12 +24,19 @@
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
diff --git a/pvmfw/src/memory.rs b/pvmfw/src/memory.rs
index 44db85f..323ac74 100644
--- a/pvmfw/src/memory.rs
+++ b/pvmfw/src/memory.rs
@@ -24,8 +24,6 @@
use alloc::boxed::Box;
use buddy_system_allocator::LockedFrameAllocator;
use core::alloc::Layout;
-use core::cmp::max;
-use core::cmp::min;
use core::fmt;
use core::iter::once;
use core::num::NonZeroUsize;
@@ -74,29 +72,6 @@
mem_type: MemoryType,
}
-impl MemoryRegion {
- /// True if the instance overlaps with the passed range.
- pub fn overlaps(&self, range: &MemoryRange) -> bool {
- overlaps(&self.range, range)
- }
-
- /// True if the instance is fully contained within the passed range.
- pub fn is_within(&self, range: &MemoryRange) -> bool {
- self.as_ref().is_within(range)
- }
-}
-
-impl AsRef<MemoryRange> for MemoryRegion {
- fn as_ref(&self) -> &MemoryRange {
- &self.range
- }
-}
-
-/// Returns true if one range overlaps with the other at all.
-fn overlaps<T: Copy + Ord>(a: &Range<T>, b: &Range<T>) -> bool {
- max(a.start, b.start) < min(a.end, b.end)
-}
-
/// Tracks non-overlapping slices of main memory.
pub struct MemoryTracker {
total: MemoryRange,
@@ -206,7 +181,7 @@
if self.total.end < range.end {
return Err(MemoryTrackerError::SizeTooLarge);
}
- if !self.regions.iter().all(|r| r.is_within(range)) {
+ if !self.regions.iter().all(|r| r.range.is_within(range)) {
return Err(MemoryTrackerError::SizeTooSmall);
}
@@ -250,10 +225,10 @@
/// appropriately.
pub fn map_mmio_range(&mut self, range: MemoryRange) -> Result<()> {
// MMIO space is below the main memory region.
- if range.end > self.total.start || overlaps(&Self::PVMFW_RANGE, &range) {
+ if range.end > self.total.start || range.overlaps(&Self::PVMFW_RANGE) {
return Err(MemoryTrackerError::OutOfRange);
}
- if self.mmio_regions.iter().any(|r| overlaps(r, &range)) {
+ if self.mmio_regions.iter().any(|r| range.overlaps(r)) {
return Err(MemoryTrackerError::Overlaps);
}
if self.mmio_regions.len() == self.mmio_regions.capacity() {
@@ -276,10 +251,10 @@
/// with any other previously allocated regions, and that the regions ArrayVec has capacity to
/// add it.
fn check(&self, region: &MemoryRegion) -> Result<()> {
- if !region.is_within(&self.total) {
+ if !region.range.is_within(&self.total) {
return Err(MemoryTrackerError::OutOfRange);
}
- if self.regions.iter().any(|r| r.overlaps(®ion.range)) {
+ if self.regions.iter().any(|r| region.range.overlaps(&r.range)) {
return Err(MemoryTrackerError::Overlaps);
}
if self.regions.len() == self.regions.capacity() {
@@ -293,7 +268,7 @@
return Err(MemoryTrackerError::Full);
}
- Ok(self.regions.last().unwrap().as_ref().clone())
+ Ok(self.regions.last().unwrap().range.clone())
}
/// Unmaps all tracked MMIO regions from the MMIO guard.