Skip to content

Commit 18db9b5

Browse files
committed
xen: Introduce GuestMemoryXen and use it in doc tests
This is a xen specific version of `GuestMemoryMmap`. This makes the xen module independent of the cfg'd types in mmap.rs, meaning we are almost ready to allow enabling both Mmap and Xen support at the same time. Signed-off-by: Patrick Roy <[email protected]>
1 parent 43f2e5c commit 18db9b5

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub use mmap::{GuestMemoryMmap, GuestRegionMmap, MmapRegion};
7474
pub use mmap::{MmapRange, MmapXenFlags};
7575

7676
#[cfg(all(feature = "xen", unix))]
77-
pub use mmap_xen::MmapRegion as MmapRegionXen;
77+
pub use mmap_xen::{GuestMemoryXen, MmapRegion as MmapRegionXen};
7878

7979
pub mod volatile_memory;
8080
pub use volatile_memory::{

src/mmap_xen.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ 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};
31+
use crate::{
32+
guest_memory, Address, GuestMemoryRegion, GuestRegionCollection, GuestUsize,
33+
MemoryRegionAddress,
34+
};
3235

3336
/// Error conditions that may arise when creating a new `MmapRegion` object.
3437
#[derive(Debug, thiserror::Error)]
@@ -192,6 +195,12 @@ impl<B: Bitmap> GuestMemoryRegion for MmapRegion<B> {
192195
}
193196
}
194197

198+
/// A collection of Xen guest memory regions.
199+
///
200+
/// Represents the entire physical memory of the guest by tracking all its memory regions.
201+
/// Each region is an instance of [`MmapRegionXen`].
202+
pub type GuestMemoryXen<B> = GuestRegionCollection<MmapRegion<B>>;
203+
195204
// SAFETY: Send and Sync aren't automatically inherited for the raw address pointer.
196205
// Accessing that pointer is only done through the stateless interface which
197206
// allows the object to be shared by multiple threads without a decrease in
@@ -213,8 +222,7 @@ impl<B: NewBitmap> MmapRegion<B> {
213222
/// use std::fs::File;
214223
/// use std::path::Path;
215224
/// use vm_memory::{
216-
/// Bytes, FileOffset, GuestAddress, GuestMemoryMmap, GuestRegionMmap, MmapRange, MmapRegion,
217-
/// MmapXenFlags,
225+
/// Bytes, FileOffset, GuestAddress, GuestMemoryXen, MmapRange, MmapRegionXen, MmapXenFlags,
218226
/// };
219227
/// # use vmm_sys_util::tempfile::TempFile;
220228
///
@@ -230,13 +238,9 @@ impl<B: NewBitmap> MmapRegion<B> {
230238
/// # // We need a UNIX mapping for tests to succeed.
231239
/// # let range = MmapRange::new_unix(0x400, None, addr);
232240
///
233-
/// let r = GuestRegionMmap::new(
234-
/// MmapRegion::<()>::from_range(range).expect("Could not create mmap region"),
235-
/// addr,
236-
/// )
237-
/// .expect("Could not create guest region");
241+
/// let r = MmapRegionXen::<()>::from_range(range).expect("Could not create mmap region");
238242
///
239-
/// let mut gm = GuestMemoryMmap::from_regions(vec![r]).expect("Could not create guest memory");
243+
/// let mut gm = GuestMemoryXen::from_regions(vec![r]).expect("Could not create guest memory");
240244
/// let res = gm
241245
/// .write(&[1, 2, 3, 4, 5], GuestAddress(0x1200))
242246
/// .expect("Could not write to guest memory");
@@ -249,8 +253,7 @@ impl<B: NewBitmap> MmapRegion<B> {
249253
/// use std::fs::File;
250254
/// use std::path::Path;
251255
/// use vm_memory::{
252-
/// Bytes, FileOffset, GuestAddress, GuestMemoryMmap, GuestRegionMmap, MmapRange, MmapRegion,
253-
/// MmapXenFlags,
256+
/// Bytes, FileOffset, GuestAddress, GuestMemoryXen, MmapRange, MmapRegionXen, MmapXenFlags,
254257
/// };
255258
/// # use vmm_sys_util::tempfile::TempFile;
256259
///
@@ -266,13 +269,9 @@ impl<B: NewBitmap> MmapRegion<B> {
266269
/// # // We need a UNIX mapping for tests to succeed.
267270
/// # let range = MmapRange::new_unix(0x400, None, addr);
268271
///
269-
/// let r = GuestRegionMmap::new(
270-
/// MmapRegion::<()>::from_range(range).expect("Could not create mmap region"),
271-
/// addr,
272-
/// )
273-
/// .expect("Could not create guest region");
272+
/// let r = MmapRegionXen::<()>::from_range(range).expect("Could not create mmap region");
274273
///
275-
/// let mut gm = GuestMemoryMmap::from_regions(vec![r]).expect("Could not create guest memory");
274+
/// let mut gm = GuestMemoryXen::from_regions(vec![r]).expect("Could not create guest memory");
276275
/// let res = gm
277276
/// .write(&[1, 2, 3, 4, 5], GuestAddress(0x1200))
278277
/// .expect("Could not write to guest memory");

0 commit comments

Comments
 (0)