resources/
lib.rs

1// Copyright 2018 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//! Manages system resources that can be allocated to VMs and their devices.
6
7use remain::sorted;
8use serde::Deserialize;
9use serde::Serialize;
10use thiserror::Error;
11
12pub use crate::address_range::AddressRange;
13pub use crate::pci_address::Error as PciAddressError;
14pub use crate::pci_address::PciAddress;
15pub use crate::system_allocator::AllocOptions;
16pub use crate::system_allocator::MmioType;
17pub use crate::system_allocator::SystemAllocator;
18pub use crate::system_allocator::SystemAllocatorConfig;
19
20pub mod address_allocator;
21mod address_range;
22mod pci_address;
23mod system_allocator;
24
25/// Used to tag SystemAllocator allocations.
26#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone, Serialize, Deserialize)]
27pub enum Alloc {
28    /// An anonymous resource allocation.
29    /// Should only be instantiated through `SystemAllocator::get_anon_alloc()`.
30    /// Avoid using these. Instead, use / create a more descriptive Alloc variant.
31    Anon(usize),
32    /// A PCI BAR region with associated bus, device, function and bar numbers.
33    PciBar { bus: u8, dev: u8, func: u8, bar: u8 },
34    /// GPU render node region.
35    GpuRenderNode,
36    /// Pmem device region with associated device index.
37    PmemDevice(usize),
38    /// pstore region.
39    Pstore,
40    /// A PCI bridge window with associated bus, dev, function.
41    PciBridgeWindow { bus: u8, dev: u8, func: u8 },
42    /// A PCI bridge prefetch window with associated bus, dev, function.
43    PciBridgePrefetchWindow { bus: u8, dev: u8, func: u8 },
44    /// File-backed memory mapping.
45    FileBacked(u64),
46}
47
48#[sorted]
49#[derive(Error, Debug, Eq, PartialEq)]
50pub enum Error {
51    #[error("Allocation cannot have size of 0")]
52    AllocSizeZero,
53    #[error("Pool alignment must be a power of 2")]
54    BadAlignment,
55    #[error("Alloc does not exist: {0:?}")]
56    BadAlloc(Alloc),
57    #[error("Alloc already exists: {0:?}")]
58    ExistingAlloc(Alloc),
59    #[error("Invalid Alloc: {0:?}")]
60    InvalidAlloc(Alloc),
61    #[error("IO port out of range: {0}")]
62    IOPortOutOfRange(AddressRange),
63    #[error("Platform MMIO address range not specified")]
64    MissingPlatformMMIOAddresses,
65    #[error("No IO address range specified")]
66    NoIoAllocator,
67    #[error("Out of bounds")]
68    OutOfBounds,
69    #[error("Out of space")]
70    OutOfSpace,
71    #[error("base={base} + size={size} overflows")]
72    PoolOverflow { base: u64, size: u64 },
73    #[error("Overlapping region {0}")]
74    RegionOverlap(AddressRange),
75}
76
77pub type Result<T> = std::result::Result<T, Error>;