Skip to content

Commit 43f2e5c

Browse files
committed
xen: implement GuestMemoryRegion for mmap_xen::MmapRegion
Xen's MmapRegion struct already stores a `GuestAddress` inside of it, so there's no need to wrap it into a tuple of `(MmapRegion, GuestAddress)` to implement this trait on (which is essentially what happens in `mmap.rs`) - in fact, duplicating the GuestAddress seems wrong to be because I don't see any mechanism for keeping them in-sync. Do not duplication over the functions defined on mmap::GuestRegionMmap, because there already exist functions with colliding names in mmap_xen. Signed-off-by: Patrick Roy <[email protected]>
1 parent aa37f15 commit 43f2e5c

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ pub use mmap::{GuestMemoryMmap, GuestRegionMmap, MmapRegion};
7373
#[cfg(all(feature = "backend-mmap", feature = "xen", unix))]
7474
pub use mmap::{MmapRange, MmapXenFlags};
7575

76+
#[cfg(all(feature = "xen", unix))]
77+
pub use mmap_xen::MmapRegion as MmapRegionXen;
78+
7679
pub mod volatile_memory;
7780
pub use volatile_memory::{
7881
Error as VolatileMemoryError, Result as VolatileMemoryResult, VolatileArrayRef, VolatileMemory,

src/mmap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<B: Bitmap> GuestMemoryRegion for GuestRegionMmap<B> {
195195
offset: MemoryRegionAddress,
196196
count: usize,
197197
) -> guest_memory::Result<VolatileSlice<BS<B>>> {
198-
let slice = self.mapping.get_slice(offset.raw_value() as usize, count)?;
198+
let slice = VolatileMemory::get_slice(&self.mapping, offset.raw_value() as usize, count)?;
199199
Ok(slice)
200200
}
201201

src/mmap_xen.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::bitmap::{Bitmap, BS};
2828
use crate::guest_memory::{FileOffset, GuestAddress};
2929
use crate::mmap::{check_file_offset, CheckFileOffsetError, NewBitmap};
3030
use crate::volatile_memory::{self, VolatileMemory, VolatileSlice};
31+
use crate::{guest_memory, Address, GuestMemoryRegion, GuestUsize, MemoryRegionAddress};
3132

3233
/// Error conditions that may arise when creating a new `MmapRegion` object.
3334
#[derive(Debug, thiserror::Error)]
@@ -149,6 +150,48 @@ pub struct MmapRegion<B = ()> {
149150
mmap: MmapXen,
150151
}
151152

153+
impl<B: Bitmap> GuestMemoryRegion for MmapRegion<B> {
154+
type B = B;
155+
156+
fn len(&self) -> GuestUsize {
157+
self.size as GuestUsize
158+
}
159+
160+
fn start_addr(&self) -> GuestAddress {
161+
self.mmap.mmap.guest_base()
162+
}
163+
164+
fn bitmap(&self) -> &Self::B {
165+
&self.bitmap
166+
}
167+
168+
// TODO: MmapRegion::as_ptr states that it should only be used for passing pointers to ioctls. Should this function then just remain the default implementation of returning Err(InvalidHostAddress)?
169+
fn get_host_address(&self, addr: MemoryRegionAddress) -> crate::guest_memory::Result<*mut u8> {
170+
self.check_address(addr)
171+
.ok_or(guest_memory::Error::InvalidBackendAddress)
172+
.map(|addr| self.as_ptr().wrapping_offset(addr.raw_value() as isize))
173+
}
174+
175+
fn file_offset(&self) -> Option<&FileOffset> {
176+
self.file_offset.as_ref()
177+
}
178+
179+
fn get_slice(
180+
&self,
181+
offset: MemoryRegionAddress,
182+
count: usize,
183+
) -> crate::guest_memory::Result<VolatileSlice<BS<Self::B>>> {
184+
VolatileMemory::get_slice(self, offset.raw_value() as usize, count).map_err(Into::into)
185+
}
186+
187+
// TODO: does this make sense in the context of Xen, or should it just return None, as the default implementation does?
188+
// (and if running on Xen, will target_os="linux" even be true?)
189+
#[cfg(target_os = "linux")]
190+
fn is_hugetlbfs(&self) -> Option<bool> {
191+
self.hugetlbfs
192+
}
193+
}
194+
152195
// SAFETY: Send and Sync aren't automatically inherited for the raw address pointer.
153196
// Accessing that pointer is only done through the stateless interface which
154197
// allows the object to be shared by multiple threads without a decrease in
@@ -306,8 +349,8 @@ impl<B: Bitmap> MmapRegion<B> {
306349
if f_off1.file().as_raw_fd() == f_off2.file().as_raw_fd() {
307350
let s1 = f_off1.start();
308351
let s2 = f_off2.start();
309-
let l1 = self.len() as u64;
310-
let l2 = other.len() as u64;
352+
let l1 = self.size as u64;
353+
let l2 = other.size as u64;
311354

312355
if s1 < s2 {
313356
return s1 + l1 > s2;

0 commit comments

Comments
 (0)