@@ -50,10 +50,11 @@ use std::sync::atomic::Ordering;
5050use std:: sync:: Arc ;
5151
5252use crate :: address:: { Address , AddressValue } ;
53- use crate :: bitmap:: { Bitmap , BS , MS } ;
53+ use crate :: bitmap:: MS ;
5454use crate :: bytes:: { AtomicAccess , Bytes } ;
5555use crate :: io:: { ReadVolatile , WriteVolatile } ;
5656use crate :: volatile_memory:: { self , VolatileSlice } ;
57+ use crate :: GuestMemoryRegion ;
5758
5859/// Errors associated with handling guest memory accesses.
5960#[ allow( missing_docs) ]
@@ -158,139 +159,6 @@ impl FileOffset {
158159 }
159160}
160161
161- /// Represents a continuous region of guest physical memory.
162- #[ allow( clippy:: len_without_is_empty) ]
163- pub trait GuestMemoryRegion : Bytes < MemoryRegionAddress , E = Error > {
164- /// Type used for dirty memory tracking.
165- type B : Bitmap ;
166-
167- /// Returns the size of the region.
168- fn len ( & self ) -> GuestUsize ;
169-
170- /// Returns the minimum (inclusive) address managed by the region.
171- fn start_addr ( & self ) -> GuestAddress ;
172-
173- /// Returns the maximum (inclusive) address managed by the region.
174- fn last_addr ( & self ) -> GuestAddress {
175- // unchecked_add is safe as the region bounds were checked when it was created.
176- self . start_addr ( ) . unchecked_add ( self . len ( ) - 1 )
177- }
178-
179- /// Borrow the associated `Bitmap` object.
180- fn bitmap ( & self ) -> & Self :: B ;
181-
182- /// Returns the given address if it is within this region.
183- fn check_address ( & self , addr : MemoryRegionAddress ) -> Option < MemoryRegionAddress > {
184- if self . address_in_range ( addr) {
185- Some ( addr)
186- } else {
187- None
188- }
189- }
190-
191- /// Returns `true` if the given address is within this region.
192- fn address_in_range ( & self , addr : MemoryRegionAddress ) -> bool {
193- addr. raw_value ( ) < self . len ( )
194- }
195-
196- /// Returns the address plus the offset if it is in this region.
197- fn checked_offset (
198- & self ,
199- base : MemoryRegionAddress ,
200- offset : usize ,
201- ) -> Option < MemoryRegionAddress > {
202- base. checked_add ( offset as u64 )
203- . and_then ( |addr| self . check_address ( addr) )
204- }
205-
206- /// Tries to convert an absolute address to a relative address within this region.
207- ///
208- /// Returns `None` if `addr` is out of the bounds of this region.
209- fn to_region_addr ( & self , addr : GuestAddress ) -> Option < MemoryRegionAddress > {
210- addr. checked_offset_from ( self . start_addr ( ) )
211- . and_then ( |offset| self . check_address ( MemoryRegionAddress ( offset) ) )
212- }
213-
214- /// Returns the host virtual address corresponding to the region address.
215- ///
216- /// Some [`GuestMemory`](trait.GuestMemory.html) implementations, like `GuestMemoryMmap`,
217- /// have the capability to mmap guest address range into host virtual address space for
218- /// direct access, so the corresponding host virtual address may be passed to other subsystems.
219- ///
220- /// # Note
221- /// The underlying guest memory is not protected from memory aliasing, which breaks the
222- /// Rust memory safety model. It's the caller's responsibility to ensure that there's no
223- /// concurrent accesses to the underlying guest memory.
224- fn get_host_address ( & self , _addr : MemoryRegionAddress ) -> Result < * mut u8 > {
225- Err ( Error :: HostAddressNotAvailable )
226- }
227-
228- /// Returns information regarding the file and offset backing this memory region.
229- fn file_offset ( & self ) -> Option < & FileOffset > {
230- None
231- }
232-
233- /// Returns a [`VolatileSlice`](struct.VolatileSlice.html) of `count` bytes starting at
234- /// `offset`.
235- #[ allow( unused_variables) ]
236- fn get_slice (
237- & self ,
238- offset : MemoryRegionAddress ,
239- count : usize ,
240- ) -> Result < VolatileSlice < BS < Self :: B > > > {
241- Err ( Error :: HostAddressNotAvailable )
242- }
243-
244- /// Gets a slice of memory for the entire region that supports volatile access.
245- ///
246- /// # Examples (uses the `backend-mmap` feature)
247- ///
248- /// ```
249- /// # #[cfg(feature = "backend-mmap")]
250- /// # {
251- /// # use vm_memory::{GuestAddress, MmapRegion, GuestRegionMmap, GuestMemoryRegion};
252- /// # use vm_memory::volatile_memory::{VolatileMemory, VolatileSlice, VolatileRef};
253- /// #
254- /// let region = GuestRegionMmap::<()>::from_range(GuestAddress(0x0), 0x400, None)
255- /// .expect("Could not create guest memory");
256- /// let slice = region
257- /// .as_volatile_slice()
258- /// .expect("Could not get volatile slice");
259- ///
260- /// let v = 42u32;
261- /// let r = slice
262- /// .get_ref::<u32>(0x200)
263- /// .expect("Could not get reference");
264- /// r.store(v);
265- /// assert_eq!(r.load(), v);
266- /// # }
267- /// ```
268- fn as_volatile_slice ( & self ) -> Result < VolatileSlice < BS < Self :: B > > > {
269- self . get_slice ( MemoryRegionAddress ( 0 ) , self . len ( ) as usize )
270- }
271-
272- /// Show if the region is based on the `HugeTLBFS`.
273- /// Returns Some(true) if the region is backed by hugetlbfs.
274- /// None represents that no information is available.
275- ///
276- /// # Examples (uses the `backend-mmap` feature)
277- ///
278- /// ```
279- /// # #[cfg(feature = "backend-mmap")]
280- /// # {
281- /// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap, GuestRegionMmap};
282- /// let addr = GuestAddress(0x1000);
283- /// let mem = GuestMemoryMmap::<()>::from_ranges(&[(addr, 0x1000)]).unwrap();
284- /// let r = mem.find_region(addr).unwrap();
285- /// assert_eq!(r.is_hugetlbfs(), None);
286- /// # }
287- /// ```
288- #[ cfg( target_os = "linux" ) ]
289- fn is_hugetlbfs ( & self ) -> Option < bool > {
290- None
291- }
292- }
293-
294162/// `GuestAddressSpace` provides a way to retrieve a `GuestMemory` object.
295163/// The vm-memory crate already provides trivial implementation for
296164/// references to `GuestMemory` or reference-counted `GuestMemory` objects,
@@ -409,7 +277,8 @@ pub trait GuestMemory {
409277
410278 /// Returns the region containing the specified address or `None`.
411279 fn find_region ( & self , addr : GuestAddress ) -> Option < & Self :: R > {
412- self . iter ( ) . find ( |region| addr >= region. start_addr ( ) && addr <= region. last_addr ( ) )
280+ self . iter ( )
281+ . find ( |region| addr >= region. start_addr ( ) && addr <= region. last_addr ( ) )
413282 }
414283
415284 /// Gets an iterator over the entries in the collection.
0 commit comments