Skip to content

Commit ee29b88

Browse files
Serban Iorgaalxiord
authored andcommitted
[mmap_unix.rs] documentation improvements and fixes
Signed-off-by: Serban Iorga <seriorga@amazon.com>
1 parent d6229fc commit ee29b88

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

src/mmap_unix.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,7 @@
88
// Use of this source code is governed by a BSD-style license that can be
99
// found in the THIRD-PARTY file.
1010

11-
//! A default Unix implementation of the GuestMemory trait by mmap()-ing guest's memory into
12-
//! the current process.
13-
//!
14-
//! The main structs to access guest's memory are:
15-
//! - [MmapRegion](struct.MmapRegion.html): mmap a continuous region of guest's memory into the
16-
//! current process
17-
//! - [GuestRegionMmap](struct.GuestRegionMmap.html): tracks a mapping of memory in the current
18-
//! process and the corresponding base address. It relays guest memory access requests to the
19-
//! underline [MmapRegion](struct.MmapRegion.html) object.
20-
//! - [GuestMemoryMmap](struct.GuestMemoryMmap.html): provides methods to access a collection of
21-
//! GuestRegionMmap objects.
11+
//! Helper structure for working with mmaped memory regions in Unix.
2212
2313
use std::error;
2414
use std::fmt;
@@ -73,11 +63,15 @@ impl error::Error for Error {}
7363

7464
pub type Result<T> = result::Result<T, Error>;
7565

76-
/// A backend driver to access guest's physical memory by mmapping guest's memory into the current
77-
/// process.
78-
/// For a combination of 32-bit hypervisor and 64-bit virtual machine, only partial of guest's
79-
/// physical memory may be mapped into current process due to limited process virtual address
80-
/// space size.
66+
/// Helper structure for working with mmaped memory regions in Unix.
67+
///
68+
/// The structure is used for accessing the guest's physical memory by mmapping it into
69+
/// the current process.
70+
///
71+
/// # Limitations
72+
/// When running a 64-bit virtual machine on a 32-bit hypervisor, only part of the guest's
73+
/// physical memory may be mapped into the current process due to the limited virtual address
74+
/// space size of the process.
8175
#[derive(Debug)]
8276
pub struct MmapRegion {
8377
addr: *mut u8,
@@ -95,10 +89,10 @@ unsafe impl Send for MmapRegion {}
9589
unsafe impl Sync for MmapRegion {}
9690

9791
impl MmapRegion {
98-
/// Creates an anonymous shared mapping of `size` bytes.
92+
/// Creates a shared anonymous mapping of `size` bytes.
9993
///
10094
/// # Arguments
101-
/// * `size` - Size of memory region in bytes.
95+
/// * `size` - The size of the memory region in bytes.
10296
pub fn new(size: usize) -> Result<Self> {
10397
Self::build(
10498
None,
@@ -108,11 +102,12 @@ impl MmapRegion {
108102
)
109103
}
110104

111-
/// Maps the `size` bytes starting at `offset` bytes of the given `fd`.
105+
/// Creates a shared file mapping of `size` bytes.
112106
///
113107
/// # Arguments
114-
/// * `file_offset` - File object and offset to mmap from.
115-
/// * `size` - Size of memory region in bytes.
108+
/// * `file_offset` - The mapping will be created at offset `file_offset.start` in the file
109+
/// referred to by `file_offset.file`.
110+
/// * `size` - The size of the memory region in bytes.
116111
pub fn from_file(file_offset: FileOffset, size: usize) -> Result<Self> {
117112
Self::build(
118113
Some(file_offset),
@@ -122,7 +117,16 @@ impl MmapRegion {
122117
)
123118
}
124119

125-
/// Creates a new mapping based on the provided arguments.
120+
/// Creates a mapping based on the provided arguments.
121+
///
122+
/// # Arguments
123+
/// * `file_offset` - if provided, the method will create a file mapping at offset
124+
/// `file_offset.start` in the file referred to by `file_offset.file`.
125+
/// * `size` - The size of the memory region in bytes.
126+
/// * `prot` - The desired memory protection of the mapping.
127+
/// * `flags` - This argument determines whether updates to the mapping are visible to other
128+
/// processes mapping the same region, and whether updates are carried through to
129+
/// the underlying file.
126130
pub fn build(
127131
file_offset: Option<FileOffset>,
128132
size: usize,
@@ -159,8 +163,9 @@ impl MmapRegion {
159163
})
160164
}
161165

162-
/// Returns a pointer to the beginning of the memory region. Should only be
163-
/// used for passing this region to ioctls for setting guest memory.
166+
/// Returns a pointer to the beginning of the memory region.
167+
///
168+
/// Should only be used for passing this region to ioctls for setting guest memory.
164169
pub fn as_ptr(&self) -> *mut u8 {
165170
self.addr
166171
}
@@ -185,9 +190,11 @@ impl MmapRegion {
185190
self.flags
186191
}
187192

188-
/// Returns true if `self` and `other` map the same file descriptor, and the `(offset, size)`
189-
/// pairs overlap. This is mostly a sanity check available for convenience, as different file
190-
/// descriptors can alias the same file.
193+
/// Checks whether this region and `other` are backed by overlapping
194+
/// [`FileOffset`](struct.FileOffset.html) objects.
195+
///
196+
/// This is mostly a sanity check available for convenience, as different file descriptors
197+
/// can alias the same file.
191198
pub fn fds_overlap(&self, other: &MmapRegion) -> bool {
192199
if let Some(f_off1) = self.file_offset() {
193200
if let Some(f_off2) = other.file_offset() {
@@ -210,15 +217,12 @@ impl MmapRegion {
210217
}
211218

212219
impl AsSlice for MmapRegion {
213-
// Returns the region as a slice
214-
// used to do crap
215220
unsafe fn as_slice(&self) -> &[u8] {
216221
// This is safe because we mapped the area at addr ourselves, so this slice will not
217222
// overflow. However, it is possible to alias.
218223
std::slice::from_raw_parts(self.addr, self.size)
219224
}
220225

221-
// safe because it's expected interior mutability
222226
#[allow(clippy::mut_from_ref)]
223227
unsafe fn as_mut_slice(&self) -> &mut [u8] {
224228
// This is safe because we mapped the area at addr ourselves, so this slice will not

0 commit comments

Comments
 (0)