2
2
// SPDX-License-Identifier: Apache-2.0
3
3
//
4
4
5
- //! A default Windows implementation of the GuestMemory trait using VirtualAlloc() and MapViewOfFile().
6
- //!
7
- //! The main structs to access guest's memory are:
8
- //! - [MmapRegion](struct.MmapRegion.html): mmap a continuous region of guest's memory into the
9
- //! current process
10
- //! - [GuestRegionMmap](struct.GuestRegionMmap.html): tracks a mapping of memory in the current
11
- //! process and the corresponding base address. It relays guest memory access requests to the
12
- //! underline [MmapRegion](struct.MmapRegion.html) object.
13
- //! - [GuestMemoryMmap](struct.GuestMemoryMmap.html): provides methods to access a collection of
14
- //! GuestRegionMmap objects.
5
+ //! Helper structure for working with mmaped memory regions in Windows.
15
6
16
7
use std;
17
8
use std:: io;
@@ -70,11 +61,15 @@ pub const INVALID_HANDLE_VALUE: RawHandle = (-1isize) as RawHandle;
70
61
#[ allow( dead_code) ]
71
62
pub const ERROR_INVALID_PARAMETER : i32 = 87 ;
72
63
73
- /// A backend driver to access guest's physical memory by mmapping guest's memory into the current
74
- /// process.
75
- /// For a combination of 32-bit hypervisor and 64-bit virtual machine, only partial of guest's
76
- /// physical memory may be mapped into current process due to limited process virtual address
77
- /// space size.
64
+ /// Helper structure for working with mmaped memory regions in Unix.
65
+ ///
66
+ /// The structure is used for accessing the guest's physical memory by mmapping it into
67
+ /// the current process.
68
+ ///
69
+ /// # Limitations
70
+ /// When running a 64-bit virtual machine on a 32-bit hypervisor, only part of the guest's
71
+ /// physical memory may be mapped into the current process due to the limited virtual address
72
+ /// space size of the process.
78
73
#[ derive( Debug ) ]
79
74
pub struct MmapRegion {
80
75
addr : * mut u8 ,
@@ -90,10 +85,10 @@ unsafe impl Send for MmapRegion {}
90
85
unsafe impl Sync for MmapRegion { }
91
86
92
87
impl MmapRegion {
93
- /// Creates an anonymous shared mapping of `size` bytes.
88
+ /// Creates a shared anonymous mapping of `size` bytes.
94
89
///
95
90
/// # Arguments
96
- /// * `size` - Size of memory region in bytes.
91
+ /// * `size` - The size of the memory region in bytes.
97
92
pub fn new ( size : usize ) -> io:: Result < Self > {
98
93
if ( size == 0 ) || ( size > MM_HIGHEST_VAD_ADDRESS as usize ) {
99
94
return Err ( io:: Error :: from_raw_os_error ( libc:: EINVAL ) ) ;
@@ -111,12 +106,12 @@ impl MmapRegion {
111
106
} )
112
107
}
113
108
114
- /// Maps the `size` bytes starting at `offset ` bytes of the given `fd` .
109
+ /// Creates a shared file mapping of `size ` bytes.
115
110
///
116
111
/// # Arguments
117
- /// * `file ` - Raw handle to a file to map into the address space.
118
- /// * `size` - Size of memory region in bytes .
119
- /// * `offset ` - Offset in bytes from the beginning of `file` to start the mapping .
112
+ /// * `file_offset ` - The mapping will be created at offset `file_offset.start` in the file
113
+ /// referred to by `file_offset.file` .
114
+ /// * `size ` - The size of the memory region in bytes .
120
115
pub fn from_file ( file_offset : FileOffset , size : usize ) -> io:: Result < Self > {
121
116
let handle = file_offset. file ( ) . as_raw_handle ( ) ;
122
117
if handle == INVALID_HANDLE_VALUE {
@@ -165,8 +160,9 @@ impl MmapRegion {
165
160
} )
166
161
}
167
162
168
- /// Returns a pointer to the beginning of the memory region. Should only be
169
- /// used for passing this region to ioctls for setting guest memory.
163
+ /// Returns a pointer to the beginning of the memory region.
164
+ ///
165
+ /// Should only be used for passing this region to ioctls for setting guest memory.
170
166
pub fn as_ptr ( & self ) -> * mut u8 {
171
167
self . addr
172
168
}
@@ -183,15 +179,12 @@ impl MmapRegion {
183
179
}
184
180
185
181
impl AsSlice for MmapRegion {
186
- // Returns the region as a slice
187
- // used to do crap
188
182
unsafe fn as_slice ( & self ) -> & [ u8 ] {
189
183
// This is safe because we mapped the area at addr ourselves, so this slice will not
190
184
// overflow. However, it is possible to alias.
191
185
std:: slice:: from_raw_parts ( self . addr , self . size )
192
186
}
193
187
194
- // safe because it's expected interior mutability
195
188
#[ allow( clippy:: mut_from_ref) ]
196
189
unsafe fn as_mut_slice ( & self ) -> & mut [ u8 ] {
197
190
// This is safe because we mapped the area at addr ourselves, so this slice will not
0 commit comments