Skip to content

Commit 4936fc8

Browse files
vireshkjiangliu
authored andcommitted
mmap_xen: Add xen specific documentation examples
Add a documentation examples for Xen specific mappings. They are built only for UNIX mapping type currently to make sure tests don't fail, but the documentation does show actual Xen code. Signed-off-by: Viresh Kumar <[email protected]>
1 parent 3adf67b commit 4936fc8

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/mmap_xen.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,79 @@ impl<B: NewBitmap> MmapRegion<B> {
171171
///
172172
/// # Arguments
173173
/// * `range` - An instance of type `MmapRange`.
174+
///
175+
/// # Examples
176+
/// * Write a slice at guest address 0x1200 with Xen's Grant mapping.
177+
///
178+
/// ```
179+
/// use std::fs::File;
180+
/// use std::path::Path;
181+
/// use vm_memory::{
182+
/// Bytes, FileOffset, GuestAddress, GuestMemoryMmap, GuestRegionMmap, MmapRange, MmapRegion,
183+
/// MmapXenFlags,
184+
/// };
185+
/// # use vmm_sys_util::tempfile::TempFile;
186+
///
187+
/// let addr = GuestAddress(0x1000);
188+
/// # if false {
189+
/// let file = Some(FileOffset::new(
190+
/// File::open(Path::new("/dev/xen/gntdev")).expect("Could not open file"),
191+
/// 0,
192+
/// ));
193+
///
194+
/// let range = MmapRange::new(0x400, file, addr, MmapXenFlags::GRANT.bits(), 0);
195+
/// # }
196+
/// # // We need a UNIX mapping for tests to succeed.
197+
/// # let range = MmapRange::new_unix(0x400, None, addr);
198+
///
199+
/// let r = GuestRegionMmap::new(
200+
/// MmapRegion::<()>::from_range(range).expect("Could not create mmap region"),
201+
/// addr,
202+
/// )
203+
/// .expect("Could not create guest region");
204+
///
205+
/// let mut gm = GuestMemoryMmap::from_regions(vec![r]).expect("Could not create guest memory");
206+
/// let res = gm
207+
/// .write(&[1, 2, 3, 4, 5], GuestAddress(0x1200))
208+
/// .expect("Could not write to guest memory");
209+
/// assert_eq!(5, res);
210+
/// ```
211+
///
212+
/// * Write a slice at guest address 0x1200 with Xen's Foreign mapping.
213+
///
214+
/// ```
215+
/// use std::fs::File;
216+
/// use std::path::Path;
217+
/// use vm_memory::{
218+
/// Bytes, FileOffset, GuestAddress, GuestMemoryMmap, GuestRegionMmap, MmapRange, MmapRegion,
219+
/// MmapXenFlags,
220+
/// };
221+
/// # use vmm_sys_util::tempfile::TempFile;
222+
///
223+
/// let addr = GuestAddress(0x1000);
224+
/// # if false {
225+
/// let file = Some(FileOffset::new(
226+
/// File::open(Path::new("/dev/xen/privcmd")).expect("Could not open file"),
227+
/// 0,
228+
/// ));
229+
///
230+
/// let range = MmapRange::new(0x400, file, addr, MmapXenFlags::FOREIGN.bits(), 0);
231+
/// # }
232+
/// # // We need a UNIX mapping for tests to succeed.
233+
/// # let range = MmapRange::new_unix(0x400, None, addr);
234+
///
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");
240+
///
241+
/// let mut gm = GuestMemoryMmap::from_regions(vec![r]).expect("Could not create guest memory");
242+
/// let res = gm
243+
/// .write(&[1, 2, 3, 4, 5], GuestAddress(0x1200))
244+
/// .expect("Could not write to guest memory");
245+
/// assert_eq!(5, res);
246+
/// ```
174247
pub fn from_range(mut range: MmapRange) -> Result<Self> {
175248
if range.prot.is_none() {
176249
range.prot = Some(libc::PROT_READ | libc::PROT_WRITE);

0 commit comments

Comments
 (0)