Skip to content

Commit d66a48d

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 65cb2a9 commit d66a48d

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
@@ -67,7 +67,7 @@ pub use mmap::{GuestMemoryMmap, GuestRegionMmap, MmapRegion};
6767
pub use mmap::{MmapRange, MmapXenFlags};
6868

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

7272
pub mod volatile_memory;
7373
pub use volatile_memory::{

src/mmap/xen.rs

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

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

200+
/// A collection of Xen guest memory regions.
201+
///
202+
/// Represents the entire physical memory of the guest by tracking all its memory regions.
203+
/// Each region is an instance of [`MmapRegionXen`].
204+
pub type GuestMemoryXen<B> = GuestRegionCollection<MmapRegion<B>>;
205+
197206
// SAFETY: Send and Sync aren't automatically inherited for the raw address pointer.
198207
// Accessing that pointer is only done through the stateless interface which
199208
// allows the object to be shared by multiple threads without a decrease in
@@ -215,8 +224,7 @@ impl<B: NewBitmap> MmapRegion<B> {
215224
/// use std::fs::File;
216225
/// use std::path::Path;
217226
/// use vm_memory::{
218-
/// Bytes, FileOffset, GuestAddress, GuestMemoryMmap, GuestRegionMmap, MmapRange, MmapRegion,
219-
/// MmapXenFlags,
227+
/// Bytes, FileOffset, GuestAddress, GuestMemoryXen, MmapRange, MmapRegionXen, MmapXenFlags,
220228
/// };
221229
/// # use vmm_sys_util::tempfile::TempFile;
222230
///
@@ -232,13 +240,9 @@ impl<B: NewBitmap> MmapRegion<B> {
232240
/// # // We need a UNIX mapping for tests to succeed.
233241
/// # let range = MmapRange::new_unix(0x400, None, addr);
234242
///
235-
/// let r = GuestRegionMmap::new(
236-
/// MmapRegion::<()>::from_range(range).expect("Could not create mmap region"),
237-
/// addr,
238-
/// )
239-
/// .expect("Could not create guest region");
243+
/// let r = MmapRegionXen::<()>::from_range(range).expect("Could not create mmap region");
240244
///
241-
/// let mut gm = GuestMemoryMmap::from_regions(vec![r]).expect("Could not create guest memory");
245+
/// let mut gm = GuestMemoryXen::from_regions(vec![r]).expect("Could not create guest memory");
242246
/// let res = gm
243247
/// .write(&[1, 2, 3, 4, 5], GuestAddress(0x1200))
244248
/// .expect("Could not write to guest memory");
@@ -251,8 +255,7 @@ impl<B: NewBitmap> MmapRegion<B> {
251255
/// use std::fs::File;
252256
/// use std::path::Path;
253257
/// use vm_memory::{
254-
/// Bytes, FileOffset, GuestAddress, GuestMemoryMmap, GuestRegionMmap, MmapRange, MmapRegion,
255-
/// MmapXenFlags,
258+
/// Bytes, FileOffset, GuestAddress, GuestMemoryXen, MmapRange, MmapRegionXen, MmapXenFlags,
256259
/// };
257260
/// # use vmm_sys_util::tempfile::TempFile;
258261
///
@@ -268,13 +271,9 @@ impl<B: NewBitmap> MmapRegion<B> {
268271
/// # // We need a UNIX mapping for tests to succeed.
269272
/// # let range = MmapRange::new_unix(0x400, None, addr);
270273
///
271-
/// let r = GuestRegionMmap::new(
272-
/// MmapRegion::<()>::from_range(range).expect("Could not create mmap region"),
273-
/// addr,
274-
/// )
275-
/// .expect("Could not create guest region");
274+
/// let r = MmapRegionXen::<()>::from_range(range).expect("Could not create mmap region");
276275
///
277-
/// let mut gm = GuestMemoryMmap::from_regions(vec![r]).expect("Could not create guest memory");
276+
/// let mut gm = GuestMemoryXen::from_regions(vec![r]).expect("Could not create guest memory");
278277
/// let res = gm
279278
/// .write(&[1, 2, 3, 4, 5], GuestAddress(0x1200))
280279
/// .expect("Could not write to guest memory");

0 commit comments

Comments
 (0)