8
8
// Use of this source code is governed by a BSD-style license that can be
9
9
// found in the THIRD-PARTY file.
10
10
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.
22
12
23
13
use std:: error;
24
14
use std:: fmt;
@@ -73,11 +63,15 @@ impl error::Error for Error {}
73
63
74
64
pub type Result < T > = result:: Result < T , Error > ;
75
65
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.
81
75
#[ derive( Debug ) ]
82
76
pub struct MmapRegion {
83
77
addr : * mut u8 ,
@@ -95,10 +89,10 @@ unsafe impl Send for MmapRegion {}
95
89
unsafe impl Sync for MmapRegion { }
96
90
97
91
impl MmapRegion {
98
- /// Creates an anonymous shared mapping of `size` bytes.
92
+ /// Creates a shared anonymous mapping of `size` bytes.
99
93
///
100
94
/// # Arguments
101
- /// * `size` - Size of memory region in bytes.
95
+ /// * `size` - The size of the memory region in bytes.
102
96
pub fn new ( size : usize ) -> Result < Self > {
103
97
Self :: build (
104
98
None ,
@@ -108,11 +102,12 @@ impl MmapRegion {
108
102
)
109
103
}
110
104
111
- /// Maps the `size` bytes starting at `offset ` bytes of the given `fd` .
105
+ /// Creates a shared file mapping of `size ` bytes.
112
106
///
113
107
/// # 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.
116
111
pub fn from_file ( file_offset : FileOffset , size : usize ) -> Result < Self > {
117
112
Self :: build (
118
113
Some ( file_offset) ,
@@ -122,7 +117,16 @@ impl MmapRegion {
122
117
)
123
118
}
124
119
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.
126
130
pub fn build (
127
131
file_offset : Option < FileOffset > ,
128
132
size : usize ,
@@ -159,8 +163,9 @@ impl MmapRegion {
159
163
} )
160
164
}
161
165
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.
164
169
pub fn as_ptr ( & self ) -> * mut u8 {
165
170
self . addr
166
171
}
@@ -185,9 +190,11 @@ impl MmapRegion {
185
190
self . flags
186
191
}
187
192
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.
191
198
pub fn fds_overlap ( & self , other : & MmapRegion ) -> bool {
192
199
if let Some ( f_off1) = self . file_offset ( ) {
193
200
if let Some ( f_off2) = other. file_offset ( ) {
@@ -210,15 +217,12 @@ impl MmapRegion {
210
217
}
211
218
212
219
impl AsSlice for MmapRegion {
213
- // Returns the region as a slice
214
- // used to do crap
215
220
unsafe fn as_slice ( & self ) -> & [ u8 ] {
216
221
// This is safe because we mapped the area at addr ourselves, so this slice will not
217
222
// overflow. However, it is possible to alias.
218
223
std:: slice:: from_raw_parts ( self . addr , self . size )
219
224
}
220
225
221
- // safe because it's expected interior mutability
222
226
#[ allow( clippy:: mut_from_ref) ]
223
227
unsafe fn as_mut_slice ( & self ) -> & mut [ u8 ] {
224
228
// This is safe because we mapped the area at addr ourselves, so this slice will not
0 commit comments