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