dbs_address_space/
lib.rs

1// Copyright (C) 2021 Alibaba Cloud. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4#![deny(missing_docs)]
5
6//! Traits and Structs to manage guest physical address space for virtual machines.
7//!
8//! The [vm-memory](https://crates.io/crates/vm-memory) implements mechanisms to manage and access
9//! guest memory resident in guest physical address space. In addition to guest memory, there may
10//! be other type of devices resident in the same guest physical address space.
11//!
12//! The `dbs-address-space` crate provides traits and structs to manage the guest physical address
13//! space for virtual machines, and mechanisms to coordinate all the devices resident in the
14//! guest physical address space.
15
16use vm_memory::GuestUsize;
17
18mod address_space;
19pub use self::address_space::{AddressSpace, AddressSpaceBase};
20
21mod layout;
22pub use layout::{AddressSpaceLayout, USABLE_END};
23
24mod memory;
25pub use memory::{GuestMemoryHybrid, GuestMemoryManager, GuestRegionHybrid, GuestRegionRaw};
26
27mod numa;
28pub use self::numa::{NumaIdTable, NumaNode, NumaNodeInfo, MPOL_MF_MOVE, MPOL_PREFERRED};
29
30mod region;
31pub use region::{AddressSpaceRegion, AddressSpaceRegionType};
32
33/// Errors associated with virtual machine address space management.
34#[derive(Debug, thiserror::Error)]
35pub enum AddressSpaceError {
36    /// Invalid address space region type.
37    #[error("invalid address space region type")]
38    InvalidRegionType,
39
40    /// Invalid address range.
41    #[error("invalid address space region (0x{0:x}, 0x{1:x})")]
42    InvalidAddressRange(u64, GuestUsize),
43
44    /// Invalid guest memory source type.
45    #[error("invalid memory source type {0}")]
46    InvalidMemorySourceType(String),
47
48    /// Failed to create memfd to map anonymous memory.
49    #[error("can not create memfd to map anonymous memory")]
50    CreateMemFd(#[source] nix::Error),
51
52    /// Failed to open memory file.
53    #[error("can not open memory file")]
54    OpenFile(#[source] std::io::Error),
55
56    /// Failed to create directory.
57    #[error("can not create directory")]
58    CreateDir(#[source] std::io::Error),
59
60    /// Failed to set size for memory file.
61    #[error("can not set size for memory file")]
62    SetFileSize(#[source] std::io::Error),
63
64    /// Failed to unlink memory file.
65    #[error("can not unlink memory file")]
66    UnlinkFile(#[source] nix::Error),
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_error_code() {
75        let e = AddressSpaceError::InvalidRegionType;
76
77        assert_eq!(format!("{e}"), "invalid address space region type");
78        assert_eq!(format!("{e:?}"), "InvalidRegionType");
79        assert_eq!(
80            format!(
81                "{}",
82                AddressSpaceError::InvalidMemorySourceType("test".to_string())
83            ),
84            "invalid memory source type test"
85        );
86    }
87}