Skip to content

Commit 0c9d53a

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 9064636 commit 0c9d53a

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
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: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use crate::bitmap::{Bitmap, NewBitmap, BS};
2828
use crate::guest_memory::{FileOffset, GuestAddress};
2929
use crate::volatile_memory::{self, VolatileMemory, VolatileSlice};
3030
use crate::{
31-
guest_memory, Address, GuestMemoryRegion, GuestMemoryRegionBytes, GuestUsize,
32-
MemoryRegionAddress,
31+
guest_memory, Address, GuestMemoryRegion, GuestMemoryRegionBytes, GuestRegionCollection,
32+
GuestUsize, MemoryRegionAddress,
3333
};
3434

3535
/// Error conditions that may arise when creating a new `MmapRegion` object.
@@ -199,6 +199,12 @@ impl<B: Bitmap> GuestMemoryRegion for MmapRegion<B> {
199199

200200
impl<B: Bitmap> GuestMemoryRegionBytes for MmapRegion<B> {}
201201

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+
202208
// SAFETY: Send and Sync aren't automatically inherited for the raw address pointer.
203209
// Accessing that pointer is only done through the stateless interface which
204210
// allows the object to be shared by multiple threads without a decrease in
@@ -220,8 +226,7 @@ impl<B: NewBitmap> MmapRegion<B> {
220226
/// use std::fs::File;
221227
/// use std::path::Path;
222228
/// use vm_memory::{
223-
/// Bytes, FileOffset, GuestAddress, GuestMemoryMmap, GuestRegionMmap, MmapRange, MmapRegion,
224-
/// MmapXenFlags,
229+
/// Bytes, FileOffset, GuestAddress, GuestMemoryXen, MmapRange, MmapRegionXen, MmapXenFlags,
225230
/// };
226231
/// # use vmm_sys_util::tempfile::TempFile;
227232
///
@@ -237,13 +242,9 @@ impl<B: NewBitmap> MmapRegion<B> {
237242
/// # // We need a UNIX mapping for tests to succeed.
238243
/// # let range = MmapRange::new_unix(0x400, None, addr);
239244
///
240-
/// let r = GuestRegionMmap::new(
241-
/// MmapRegion::<()>::from_range(range).expect("Could not create mmap region"),
242-
/// addr,
243-
/// )
244-
/// .expect("Could not create guest region");
245+
/// let r = MmapRegionXen::<()>::from_range(range).expect("Could not create mmap region");
245246
///
246-
/// 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");
247248
/// let res = gm
248249
/// .write(&[1, 2, 3, 4, 5], GuestAddress(0x1200))
249250
/// .expect("Could not write to guest memory");
@@ -256,8 +257,7 @@ impl<B: NewBitmap> MmapRegion<B> {
256257
/// use std::fs::File;
257258
/// use std::path::Path;
258259
/// use vm_memory::{
259-
/// Bytes, FileOffset, GuestAddress, GuestMemoryMmap, GuestRegionMmap, MmapRange, MmapRegion,
260-
/// MmapXenFlags,
260+
/// Bytes, FileOffset, GuestAddress, GuestMemoryXen, MmapRange, MmapRegionXen, MmapXenFlags,
261261
/// };
262262
/// # use vmm_sys_util::tempfile::TempFile;
263263
///
@@ -273,13 +273,9 @@ impl<B: NewBitmap> MmapRegion<B> {
273273
/// # // We need a UNIX mapping for tests to succeed.
274274
/// # let range = MmapRange::new_unix(0x400, None, addr);
275275
///
276-
/// let r = GuestRegionMmap::new(
277-
/// MmapRegion::<()>::from_range(range).expect("Could not create mmap region"),
278-
/// addr,
279-
/// )
280-
/// .expect("Could not create guest region");
276+
/// let r = MmapRegionXen::<()>::from_range(range).expect("Could not create mmap region");
281277
///
282-
/// 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");
283279
/// let res = gm
284280
/// .write(&[1, 2, 3, 4, 5], GuestAddress(0x1200))
285281
/// .expect("Could not write to guest memory");

0 commit comments

Comments
 (0)