Skip to content

Commit e4ce53d

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 e4ce53d

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-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: 18 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, GuestMemoryRegionBytes, GuestRegionCollection,
32+
GuestUsize, MemoryRegionAddress,
33+
};
3134

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

200+
impl<B: Bitmap> GuestMemoryRegionBytes for MmapRegion<B> {}
201+
202+
/// A collection of Xen guest memory regions.
203+
///
204+
/// Represents the entire physical memory of the guest by tracking all its memory regions.
205+
/// Each region is an instance of [`MmapRegionXen`].
206+
pub type GuestMemoryXen<B> = GuestRegionCollection<MmapRegion<B>>;
207+
197208
// SAFETY: Send and Sync aren't automatically inherited for the raw address pointer.
198209
// Accessing that pointer is only done through the stateless interface which
199210
// allows the object to be shared by multiple threads without a decrease in
@@ -215,8 +226,7 @@ impl<B: NewBitmap> MmapRegion<B> {
215226
/// use std::fs::File;
216227
/// use std::path::Path;
217228
/// use vm_memory::{
218-
/// Bytes, FileOffset, GuestAddress, GuestMemoryMmap, GuestRegionMmap, MmapRange, MmapRegion,
219-
/// MmapXenFlags,
229+
/// Bytes, FileOffset, GuestAddress, GuestMemoryXen, MmapRange, MmapRegionXen, MmapXenFlags,
220230
/// };
221231
/// # use vmm_sys_util::tempfile::TempFile;
222232
///
@@ -232,13 +242,9 @@ impl<B: NewBitmap> MmapRegion<B> {
232242
/// # // We need a UNIX mapping for tests to succeed.
233243
/// # let range = MmapRange::new_unix(0x400, None, addr);
234244
///
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");
245+
/// let r = MmapRegionXen::<()>::from_range(range).expect("Could not create mmap region");
240246
///
241-
/// let mut gm = GuestMemoryMmap::from_regions(vec![r]).expect("Could not create guest memory");
247+
/// let mut gm = GuestMemoryXen::from_regions(vec![r]).expect("Could not create guest memory");
242248
/// let res = gm
243249
/// .write(&[1, 2, 3, 4, 5], GuestAddress(0x1200))
244250
/// .expect("Could not write to guest memory");
@@ -251,8 +257,7 @@ impl<B: NewBitmap> MmapRegion<B> {
251257
/// use std::fs::File;
252258
/// use std::path::Path;
253259
/// use vm_memory::{
254-
/// Bytes, FileOffset, GuestAddress, GuestMemoryMmap, GuestRegionMmap, MmapRange, MmapRegion,
255-
/// MmapXenFlags,
260+
/// Bytes, FileOffset, GuestAddress, GuestMemoryXen, MmapRange, MmapRegionXen, MmapXenFlags,
256261
/// };
257262
/// # use vmm_sys_util::tempfile::TempFile;
258263
///
@@ -268,13 +273,9 @@ impl<B: NewBitmap> MmapRegion<B> {
268273
/// # // We need a UNIX mapping for tests to succeed.
269274
/// # let range = MmapRange::new_unix(0x400, None, addr);
270275
///
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");
276+
/// let r = MmapRegionXen::<()>::from_range(range).expect("Could not create mmap region");
276277
///
277-
/// let mut gm = GuestMemoryMmap::from_regions(vec![r]).expect("Could not create guest memory");
278+
/// let mut gm = GuestMemoryXen::from_regions(vec![r]).expect("Could not create guest memory");
278279
/// let res = gm
279280
/// .write(&[1, 2, 3, 4, 5], GuestAddress(0x1200))
280281
/// .expect("Could not write to guest memory");

0 commit comments

Comments
 (0)