Skip to content

Commit 9064636

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 ce075a2 commit 9064636

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-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: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ 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::{
31+
guest_memory, Address, GuestMemoryRegion, GuestMemoryRegionBytes, GuestUsize,
32+
MemoryRegionAddress,
33+
};
3034

3135
/// Error conditions that may arise when creating a new `MmapRegion` object.
3236
#[derive(Debug, thiserror::Error)]
@@ -151,6 +155,50 @@ pub struct MmapRegion<B = ()> {
151155
mmap: MmapXen,
152156
}
153157

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

314362
if s1 < s2 {
315363
return s1 + l1 > s2;

0 commit comments

Comments
 (0)