1 // Copyright 2018 The Chromium OS Authors. All rights reserved. 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 std::fmt::Display; 8 9 use serde::{Deserialize, Serialize}; 10 11 pub use crate::address_allocator::AddressAllocator; 12 pub use crate::system_allocator::{MmioType, SystemAllocator}; 13 14 mod address_allocator; 15 mod system_allocator; 16 17 /// Used to tag SystemAllocator allocations. 18 #[derive(Debug, Eq, PartialEq, Hash, Copy, Clone, Serialize, Deserialize)] 19 pub enum Alloc { 20 /// An anonymous resource allocation. 21 /// Should only be instantiated through `SystemAllocator::get_anon_alloc()`. 22 /// Avoid using these. Instead, use / create a more descriptive Alloc variant. 23 Anon(usize), 24 /// A PCI BAR region with associated bus, device, function and bar numbers. 25 PciBar { bus: u8, dev: u8, func: u8, bar: u8 }, 26 /// GPU render node region. 27 GpuRenderNode, 28 /// Pmem device region with associated device index. 29 PmemDevice(usize), 30 /// pstore region. 31 Pstore, 32 } 33 34 #[derive(Debug, Eq, PartialEq)] 35 pub enum Error { 36 AllocSizeZero, 37 BadAlignment, 38 ExistingAlloc(Alloc), 39 InvalidAlloc(Alloc), 40 MissingHighMMIOAddresses, 41 MissingLowMMIOAddresses, 42 NoIoAllocator, 43 OutOfSpace, 44 OutOfBounds, 45 PoolOverflow { base: u64, size: u64 }, 46 PoolSizeZero, 47 RegionOverlap { base: u64, size: u64 }, 48 BadAlloc(Alloc), 49 } 50 51 pub type Result<T> = std::result::Result<T, Error>; 52 53 impl Display for Error { fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result54 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 55 use self::Error::*; 56 match self { 57 AllocSizeZero => write!(f, "Allocation cannot have size of 0"), 58 BadAlignment => write!(f, "Pool alignment must be a power of 2"), 59 ExistingAlloc(tag) => write!(f, "Alloc already exists: {:?}", tag), 60 InvalidAlloc(tag) => write!(f, "Invalid Alloc: {:?}", tag), 61 MissingHighMMIOAddresses => write!(f, "High MMIO address range not specified"), 62 MissingLowMMIOAddresses => write!(f, "Low MMIO address range not specified"), 63 NoIoAllocator => write!(f, "No IO address range specified"), 64 OutOfSpace => write!(f, "Out of space"), 65 OutOfBounds => write!(f, "Out of bounds"), 66 PoolOverflow { base, size } => write!(f, "base={} + size={} overflows", base, size), 67 PoolSizeZero => write!(f, "Pool cannot have size of 0"), 68 RegionOverlap { base, size } => { 69 write!(f, "Overlapping region base={} size={}", base, size) 70 } 71 BadAlloc(tag) => write!(f, "Alloc does not exists: {:?}", tag), 72 } 73 } 74 } 75