Skip to content

Commit 53af3f0

Browse files
vireshkjiangliu
authored andcommitted
vm-memory: Add Xen memory mapping support
Xen allows two memory mapping mechanisms: - Foreign mapping: entire guest space is mapped at once. - Grant mapping: guest controls what backend is allowed to map. Both these mechanisms required additional mapping support, for example an ioctl() needs to be issued (with arguments passed from backend), along with mmap(). Also for grant memory mapping, the mapping may or may not be done in advance and may be required to be done on-demand. Support for that will be added by a separate commit later. Add memory mapping support for both these cases, under a new feature "xen". Also add support to do generic Unix type mapping via Xen, in absence of foreign or grant mapping, which will be used by test code for other modules. Signed-off-by: Viresh Kumar <[email protected]>
1 parent cfd89ae commit 53af3f0

File tree

4 files changed

+1075
-2
lines changed

4 files changed

+1075
-2
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ default = []
1616
backend-bitmap = []
1717
backend-mmap = []
1818
backend-atomic = ["arc-swap"]
19+
xen = ["backend-mmap", "bitflags", "vmm-sys-util"]
1920

2021
[dependencies]
2122
libc = "0.2.39"
2223
arc-swap = { version = "1.0.0", optional = true }
24+
bitflags = { version = "1.0", optional = true }
2325
thiserror = "1.0.40"
26+
vmm-sys-util = { version = "0.11.0", optional = true }
2427

2528
[target.'cfg(windows)'.dependencies.winapi]
2629
version = "0.3"

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,21 @@ pub use guest_memory::{
4545
GuestMemoryRegion, GuestUsize, MemoryRegionAddress, Result as GuestMemoryResult,
4646
};
4747

48-
#[cfg(all(feature = "backend-mmap", unix))]
48+
#[cfg(all(feature = "backend-mmap", not(feature = "xen"), unix))]
4949
mod mmap_unix;
5050

51+
#[cfg(all(feature = "backend-mmap", feature = "xen", unix))]
52+
mod mmap_xen;
53+
5154
#[cfg(all(feature = "backend-mmap", windows))]
5255
mod mmap_windows;
5356

5457
#[cfg(feature = "backend-mmap")]
5558
pub mod mmap;
5659
#[cfg(feature = "backend-mmap")]
5760
pub use mmap::{Error, GuestMemoryMmap, GuestRegionMmap, MmapRegion};
61+
#[cfg(all(feature = "backend-mmap", feature = "xen", unix))]
62+
pub use mmap::{MmapRange, MmapXenFlags};
5863

5964
pub mod volatile_memory;
6065
pub use volatile_memory::{

src/mmap.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ use crate::guest_memory::{
3030
use crate::volatile_memory::{VolatileMemory, VolatileSlice};
3131
use crate::{AtomicAccess, Bytes};
3232

33-
#[cfg(unix)]
33+
#[cfg(all(not(feature = "xen"), unix))]
3434
pub use crate::mmap_unix::{Error as MmapRegionError, MmapRegion, MmapRegionBuilder};
3535

36+
#[cfg(all(feature = "xen", unix))]
37+
pub use crate::mmap_xen::{Error as MmapRegionError, MmapRange, MmapRegion, MmapXenFlags};
38+
3639
#[cfg(windows)]
3740
pub use crate::mmap_windows::MmapRegion;
3841
#[cfg(windows)]
@@ -130,6 +133,7 @@ impl<B: Bitmap> GuestRegionMmap<B> {
130133
}
131134
}
132135

136+
#[cfg(not(feature = "xen"))]
133137
impl<B: NewBitmap> GuestRegionMmap<B> {
134138
/// Create a new memory-mapped memory region from guest's physical memory, size and file.
135139
pub fn from_range(
@@ -148,6 +152,22 @@ impl<B: NewBitmap> GuestRegionMmap<B> {
148152
}
149153
}
150154

155+
#[cfg(feature = "xen")]
156+
impl<B: NewBitmap> GuestRegionMmap<B> {
157+
/// Create a new Unix memory-mapped memory region from guest's physical memory, size and file.
158+
/// This must only be used for tests, doctests, benches and is not designed for end consumers.
159+
pub fn from_range(
160+
addr: GuestAddress,
161+
size: usize,
162+
file: Option<FileOffset>,
163+
) -> result::Result<Self, Error> {
164+
let range = MmapRange::new_unix(size, file, addr);
165+
166+
let region = MmapRegion::from_range(range).map_err(Error::MmapRegion)?;
167+
Self::new(region, addr)
168+
}
169+
}
170+
151171
impl<B: Bitmap> Bytes<MemoryRegionAddress> for GuestRegionMmap<B> {
152172
type E = guest_memory::Error;
153173

0 commit comments

Comments
 (0)