1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 use base::MappedRegion;
6 use base::SharedMemory;
7 use base::VolatileMemory;
8 use bitflags::bitflags;
9
10 use crate::GuestMemory;
11 use crate::MemoryRegion;
12 use crate::Result;
13
14 bitflags! {
15 #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
16 #[repr(transparent)]
17 pub struct MemoryPolicy: u32 {
18 }
19 }
20
finalize_shm(_shm: &mut SharedMemory) -> Result<()>21 pub(crate) fn finalize_shm(_shm: &mut SharedMemory) -> Result<()> {
22 // Seals are only a concept on Unix systems. On Windows, SharedMemory allocation cannot be
23 // updated after creation regardless, so the same operation is done implicitly.
24 Ok(())
25 }
26
27 impl GuestMemory {
28 /// Handles guest memory policy hints/advices.
set_memory_policy(&self, _mem_policy: MemoryPolicy)29 pub fn set_memory_policy(&self, _mem_policy: MemoryPolicy) {
30 // Hints aren't supported on Windows.
31 }
32 }
33
34 impl MemoryRegion {
35 /// Finds ranges of memory that might have non-zero data (i.e. not unallocated memory). The
36 /// ranges are offsets into the region's mmap, not offsets into the backing file.
37 ///
38 /// For example, if there were three bytes and the second byte was a hole, the return would be
39 /// `[1..2]` (in practice these are probably always at least page sized).
find_data_ranges(&self) -> anyhow::Result<Vec<std::ops::Range<usize>>>40 pub(crate) fn find_data_ranges(&self) -> anyhow::Result<Vec<std::ops::Range<usize>>> {
41 Ok(vec![0..self.mapping.size()])
42 }
43
zero_range(&self, offset: usize, size: usize) -> anyhow::Result<()>44 pub(crate) fn zero_range(&self, offset: usize, size: usize) -> anyhow::Result<()> {
45 self.mapping.get_slice(offset, size)?.write_bytes(0);
46 Ok(())
47 }
48 }
49