|
| 1 | +//! This module holds abstractions that enable tracking the areas dirtied by writes of a specified |
| 2 | +//! length to a given offset. In particular, this is used to track write accesses within a |
| 3 | +//! `GuestMemoryRegion` object, and the resulting bitmaps can then be aggregated to build the |
| 4 | +//! global view for the entire `GuestMemory`. |
| 5 | +
|
| 6 | +use std::fmt::{self, Debug}; |
| 7 | + |
| 8 | +/// Common bitmap operations. |
| 9 | +// This is just an initial proposal; we'll add other methods if they need to be part of the main |
| 10 | +// interface as well. |
| 11 | +pub trait Bitmap: Default { |
| 12 | + // For now this is mostly useful to preserve compatibility with the old `GuestMemoryMmap` (and |
| 13 | + // potentially other backends) constructors, which are not bitmap-aware. We can remove this |
| 14 | + // from the interface (and apply changes elsewhere) if it's too restrictive and/or configuring |
| 15 | + // the bitmap post-creation (via `GuestMemoryRegion::bitmap_mut`) gets unwieldy. |
| 16 | + /// Create a new bitmap of the given size. |
| 17 | + fn new(size: usize) -> Self; |
| 18 | + |
| 19 | + /// Mark the memory range specified by the given `offset` and `len` as dirtied. |
| 20 | + fn mark_dirty(&self, offset: usize, len: usize); |
| 21 | +} |
| 22 | + |
| 23 | +/// A no-op `Bitmap` implementation that can be provided to backends that do not actually |
| 24 | +/// require the tracking functionality. |
| 25 | +impl Bitmap for () { |
| 26 | + fn new(_size: usize) -> Self { |
| 27 | + () |
| 28 | + } |
| 29 | + |
| 30 | + fn mark_dirty(&self, _offset: usize, _len: usize) {} |
| 31 | +} |
| 32 | + |
| 33 | +/// Represents a slice into a `Bitmap` object, starting at `base_offset`. |
| 34 | +pub struct BitmapSlice<'a, B> { |
| 35 | + inner: &'a B, |
| 36 | + base_offset: usize, |
| 37 | +} |
| 38 | + |
| 39 | +impl<'a, B: Bitmap> BitmapSlice<'a, B> { |
| 40 | + /// Create a new `BitmapSlice`, starting at the specified `offset`. |
| 41 | + pub fn new(bitmap: &'a B, offset: usize) -> Self { |
| 42 | + BitmapSlice { |
| 43 | + inner: bitmap, |
| 44 | + base_offset: offset, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Mark the memory range specified by the given `offset` (relative to the base offset of |
| 49 | + /// the slice) and `len` as dirtied. |
| 50 | + pub fn mark_dirty(&self, offset: usize, len: usize) { |
| 51 | + // The `Bitmap` operations are supposed to accompany guest memory accesses defined by the |
| 52 | + // same parameters (i.e. offset & length), so we use simple wrapping arithmetic instead of |
| 53 | + // performing additional checks. If an overflow would occur, we simply end up marking some |
| 54 | + // other region as dirty (which is just a false positive) instead of a region that could |
| 55 | + // not have been accessed to begin with. |
| 56 | + self.inner |
| 57 | + .mark_dirty(self.base_offset.wrapping_add(offset), len) |
| 58 | + } |
| 59 | + |
| 60 | + /// Create a new `BitmapSlice` starting from the specified `offset` into the current slice. |
| 61 | + pub fn at_offset(&self, offset: usize) -> Self { |
| 62 | + BitmapSlice { |
| 63 | + inner: self.inner, |
| 64 | + base_offset: self.base_offset.wrapping_add(offset), |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl<'a, B> Clone for BitmapSlice<'a, B> { |
| 70 | + fn clone(&self) -> Self { |
| 71 | + BitmapSlice { |
| 72 | + inner: self.inner, |
| 73 | + base_offset: self.base_offset, |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl<'a, B> Copy for BitmapSlice<'a, B> {} |
| 79 | + |
| 80 | +impl<'a, B> Debug for BitmapSlice<'a, B> { |
| 81 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 82 | + // Dummy impl for now. |
| 83 | + write!(f, "(bitmap slice)") |
| 84 | + } |
| 85 | +} |
0 commit comments