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 implementation of the GuestMemory trait by mmap()-ing guest's memory into the current
12
- //! process.
11
+ //! The default implementation for the [`GuestMemory`](trait.GuestMemory.html) trait.
13
12
//!
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.
13
+ //! This implementation is mmap-ing the memory of the guest into the current process.
22
14
23
15
use std:: borrow:: Borrow ;
24
16
use std:: error;
@@ -43,15 +35,25 @@ pub use crate::mmap_windows::MmapRegion;
43
35
#[ cfg( windows) ]
44
36
pub use std:: io:: Error as MmapRegionError ;
45
37
46
- // For MmapRegion
38
+ /// Trait implemented by the underlying ` MmapRegion`.
47
39
pub ( crate ) trait AsSlice {
40
+ /// Returns a slice corresponding to the data in the underlying `MmapRegion`.
41
+ ///
42
+ /// # Safety
43
+ ///
44
+ /// This is unsafe because of possible aliasing.
48
45
unsafe fn as_slice ( & self ) -> & [ u8 ] ;
49
46
47
+ /// Returns a mutable slice corresponding to the data in the underlying `MmapRegion`.
48
+ ///
49
+ /// # Safety
50
+ ///
51
+ /// This is unsafe because of possible aliasing.
50
52
#[ allow( clippy:: mut_from_ref) ]
51
53
unsafe fn as_mut_slice ( & self ) -> & mut [ u8 ] ;
52
54
}
53
55
54
- /// Errors that can happen when creating a memory map
56
+ /// Errors that can occur when creating a memory map.
55
57
#[ derive( Debug ) ]
56
58
pub enum Error {
57
59
/// Adding the guest base address to the length of the underlying mapping resulted
@@ -91,6 +93,8 @@ impl error::Error for Error {}
91
93
92
94
// TODO: use this for Windows as well after we redefine the Error type there.
93
95
#[ cfg( unix) ]
96
+ /// Checks if a mapping of `size` bytes fits at the provided `file_offset`.
97
+ ///
94
98
/// For a borrowed `FileOffset` and size, this function checks whether the mapping does not
95
99
/// extend past EOF, and that adding the size to the file offset does not lead to overflow.
96
100
pub fn check_file_offset (
@@ -113,16 +117,19 @@ pub fn check_file_offset(
113
117
Ok ( ( ) )
114
118
}
115
119
116
- /// Tracks a mapping of memory in the current process and the corresponding base address
117
- /// in the guest's memory space.
120
+ /// [`GuestMemoryRegion`](trait.GuestMemoryRegion.html) implementation that mmaps the guest's
121
+ /// memory region in the current process.
122
+ ///
123
+ /// Represents a continuous region of the guest's physical memory that is backed by a mapping
124
+ /// in the virtual address space of the calling process.
118
125
#[ derive( Debug ) ]
119
126
pub struct GuestRegionMmap {
120
127
mapping : MmapRegion ,
121
128
guest_base : GuestAddress ,
122
129
}
123
130
124
131
impl GuestRegionMmap {
125
- /// Create a new memory-mapped memory region for guest's physical memory.
132
+ /// Create a new memory-mapped memory region for the guest's physical memory.
126
133
pub fn new ( mapping : MmapRegion , guest_base : GuestAddress ) -> result:: Result < Self , Error > {
127
134
if guest_base. 0 . checked_add ( mapping. len ( ) as u64 ) . is_none ( ) {
128
135
return Err ( Error :: InvalidGuestRegion ) ;
@@ -350,7 +357,6 @@ impl GuestMemoryRegion for GuestRegionMmap {
350
357
Some ( self . mapping . as_mut_slice ( ) )
351
358
}
352
359
353
- /// Get the host virtual address corresponding to the region address.
354
360
fn get_host_address ( & self , addr : MemoryRegionAddress ) -> guest_memory:: Result < * mut u8 > {
355
361
// Not sure why wrapping_offset is not unsafe. Anyway this
356
362
// is safe because we've just range-checked addr using check_address.
@@ -360,20 +366,27 @@ impl GuestMemoryRegion for GuestRegionMmap {
360
366
}
361
367
}
362
368
363
- /// Tracks memory regions allocated/mapped for the guest in the current process.
369
+ /// [`GuestMemory`](trait.GuestMemory.html) implementation that mmaps the guest's memory
370
+ /// in the current process.
371
+ ///
372
+ /// Represents the entire physical memory of the guest by tracking all its memory regions.
373
+ /// Each region is an instance of `GuestRegionMmap`, being backed by a mapping in the
374
+ /// virtual address space of the calling process.
364
375
#[ derive( Clone , Debug ) ]
365
376
pub struct GuestMemoryMmap {
366
377
regions : Vec < Arc < GuestRegionMmap > > ,
367
378
}
368
379
369
380
impl GuestMemoryMmap {
370
381
/// Creates a container and allocates anonymous memory for guest memory regions.
382
+ ///
371
383
/// Valid memory regions are specified as a slice of (Address, Size) tuples sorted by Address.
372
384
pub fn new ( ranges : & [ ( GuestAddress , usize ) ] ) -> result:: Result < Self , Error > {
373
385
Self :: with_files ( ranges. iter ( ) . map ( |r| ( r. 0 , r. 1 , None ) ) )
374
386
}
375
387
376
388
/// Creates a container and allocates anonymous memory for guest memory regions.
389
+ ///
377
390
/// Valid memory regions are specified as a sequence of (Address, Size, Option<FileOffset>)
378
391
/// tuples sorted by Address.
379
392
pub fn with_files < A , T > ( ranges : T ) -> result:: Result < Self , Error >
0 commit comments