Skip to content

Commit 65cb2a9

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 72ca3e1 commit 65cb2a9

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
@@ -66,6 +66,9 @@ pub use mmap::{GuestMemoryMmap, GuestRegionMmap, MmapRegion};
6666
#[cfg(all(feature = "backend-mmap", feature = "xen", target_family = "unix"))]
6767
pub use mmap::{MmapRange, MmapXenFlags};
6868

69+
#[cfg(all(feature = "xen", unix))]
70+
pub use mmap_xen::MmapRegion as MmapRegionXen;
71+
6972
pub mod volatile_memory;
7073
pub use volatile_memory::{
7174
Error as VolatileMemoryError, Result as VolatileMemoryResult, VolatileArrayRef, VolatileMemory,

src/mmap/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<B: Bitmap> GuestMemoryRegion for GuestRegionMmap<B> {
151151
offset: MemoryRegionAddress,
152152
count: usize,
153153
) -> guest_memory::Result<VolatileSlice<BS<B>>> {
154-
let slice = self.mapping.get_slice(offset.raw_value() as usize, count)?;
154+
let slice = VolatileMemory::get_slice(&self.mapping, offset.raw_value() as usize, count)?;
155155
Ok(slice)
156156
}
157157

src/mmap/xen.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use tests::ioctl_with_ref;
2727
use crate::bitmap::{Bitmap, NewBitmap, BS};
2828
use crate::guest_memory::{FileOffset, GuestAddress};
2929
use crate::volatile_memory::{self, VolatileMemory, VolatileSlice};
30+
use crate::{guest_memory, Address, GuestMemoryRegion, GuestUsize, MemoryRegionAddress};
3031

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

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

314357
if s1 < s2 {
315358
return s1 + l1 > s2;

0 commit comments

Comments
 (0)