• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

google / alioth / 17385795674

01 Sep 2025 07:28PM UTC coverage: 18.009% (-0.1%) from 18.149%
17385795674

Pull #281

github

web-flow
Merge 2fe071710 into 6ec9a6d6b
Pull Request #281: Port to Apple Hypervisor framework

0 of 154 new or added lines in 11 files covered. (0.0%)

1166 existing lines in 29 files now uncovered.

1362 of 7563 relevant lines covered (18.01%)

18.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/alioth/src/vfio/container.rs
1
// Copyright 2024 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
use std::fs::File;
16
use std::os::fd::AsRawFd;
17
use std::path::Path;
18
use std::sync::Arc;
19

20
use parking_lot::Mutex;
21
use snafu::ResultExt;
22

23
use crate::errors::BoxTrace;
24
use crate::mem::mapped::ArcMemPages;
25
use crate::mem::{self, LayoutChanged};
26
use crate::vfio::bindings::{
27
    VfioDmaMapFlag, VfioDmaUnmapFlag, VfioIommu, VfioIommuType1DmaMap, VfioIommuType1DmaUnmap,
28
};
29
use crate::vfio::ioctls::{vfio_iommu_map_dma, vfio_iommu_unmap_dma, vfio_set_iommu};
30
use crate::vfio::{Result, error};
31

32
#[derive(Debug)]
33
pub struct Container {
34
    fd: File,
35
    iommu: Mutex<Option<VfioIommu>>,
36
}
37

38
impl Container {
39
    pub fn new(vfio_dev: impl AsRef<Path>) -> Result<Self> {
×
40
        let fd = File::open(&vfio_dev).context(error::AccessDevice {
×
41
            path: vfio_dev.as_ref(),
×
42
        })?;
43
        Ok(Container {
×
44
            fd,
×
45
            iommu: Mutex::new(None),
×
46
        })
47
    }
48

UNCOV
49
    pub fn fd(&self) -> &File {
UNCOV
50
        &self.fd
51
    }
52

UNCOV
53
    pub fn set_iommu(&self, iommu: VfioIommu) -> Result<()> {
UNCOV
54
        let current = &mut *self.iommu.lock();
UNCOV
55
        if let Some(current_iommu) = current {
UNCOV
56
            if *current_iommu == iommu {
UNCOV
57
                Ok(())
58
            } else {
59
                error::SetContainerIommu {
UNCOV
60
                    current: *current_iommu,
61
                    new: iommu,
62
                }
63
                .fail()
64
            }
65
        } else {
UNCOV
66
            unsafe { vfio_set_iommu(&self.fd, iommu) }?;
UNCOV
67
            current.replace(iommu);
UNCOV
68
            Ok(())
69
        }
70
    }
71

UNCOV
72
    fn map(&self, hva: usize, iova: u64, size: u64) -> Result<()> {
UNCOV
73
        let flags = VfioDmaMapFlag::READ | VfioDmaMapFlag::WRITE;
74
        let dma_map = VfioIommuType1DmaMap {
UNCOV
75
            argsz: size_of::<VfioIommuType1DmaMap>() as u32,
76
            flags,
UNCOV
77
            vaddr: hva as u64,
78
            iova,
79
            size,
80
        };
UNCOV
81
        unsafe { vfio_iommu_map_dma(&self.fd, &dma_map) }?;
UNCOV
82
        log::debug!(
UNCOV
83
            "container-{}: mapped: {iova:#018x} -> {hva:#018x}, size = {size:#x}",
UNCOV
84
            self.fd.as_raw_fd()
85
        );
UNCOV
86
        Ok(())
87
    }
88

UNCOV
89
    fn unmap(&self, iova: u64, size: u64) -> Result<()> {
90
        let mut dma_unmap = VfioIommuType1DmaUnmap {
UNCOV
91
            argsz: size_of::<VfioIommuType1DmaUnmap>() as u32,
UNCOV
92
            flags: VfioDmaUnmapFlag::empty(),
93
            iova,
94
            size,
95
        };
UNCOV
96
        unsafe { vfio_iommu_unmap_dma(&self.fd, &mut dma_unmap) }?;
UNCOV
97
        log::debug!(
UNCOV
98
            "container-{}: unmapped: {iova:#018x}, size = {size:#x}",
UNCOV
99
            self.fd.as_raw_fd(),
100
        );
UNCOV
101
        Ok(())
102
    }
103
}
104

105
#[derive(Debug)]
106
pub struct UpdateContainerMapping {
107
    pub container: Arc<Container>,
108
}
109

110
impl LayoutChanged for UpdateContainerMapping {
UNCOV
111
    fn ram_added(&self, gpa: u64, pages: &ArcMemPages) -> mem::Result<()> {
UNCOV
112
        let ret = self.container.map(pages.addr(), gpa, pages.size());
UNCOV
113
        ret.box_trace(mem::error::ChangeLayout)?;
UNCOV
114
        Ok(())
115
    }
116

UNCOV
117
    fn ram_removed(&self, gpa: u64, pages: &ArcMemPages) -> mem::Result<()> {
UNCOV
118
        let ret = self.container.unmap(gpa, pages.size());
UNCOV
119
        ret.box_trace(mem::error::ChangeLayout)?;
UNCOV
120
        Ok(())
121
    }
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc