lower_vtl_permissions_guard/
device_dma.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Implements the [`MappedDmaTarget`] trait for a wrapped [`MemoryBlock`]
//! returned by [`LowerVtlMemorySpawner`].

// UNSAFETY: No unsafe code here, but required for implementing MappedDmaTarget.
// The implementation is just forwarding the calls to the underlying wrapped
// MemoryBlock.
#![expect(unsafe_code)]

use crate::PagesAccessibleToLowerVtl;
use inspect::Inspect;
use user_driver::memory::MappedDmaTarget;
use user_driver::memory::MemoryBlock;

/// A DMA buffer where permissions of the pages have been lowered to allow
/// access to all VTLs.
#[derive(Inspect)]
pub struct LowerVtlDmaBuffer {
    #[inspect(skip)]
    pub(crate) block: MemoryBlock,
    pub(crate) _vtl_guard: PagesAccessibleToLowerVtl,
}

// SAFETY: The underlying MemoryBlock is providing the implementation for this
// trait.
unsafe impl MappedDmaTarget for LowerVtlDmaBuffer {
    fn base(&self) -> *const u8 {
        self.block.base()
    }

    fn len(&self) -> usize {
        self.block.len()
    }

    fn pfns(&self) -> &[u64] {
        self.block.pfns()
    }

    fn pfn_bias(&self) -> u64 {
        self.block.pfn_bias()
    }
}