1 // Copyright 2018 The Chromium OS Authors. All rights reserved. 2 // Copyright © 2019 Intel Corporation 3 // 4 // Portions Copyright 2017 The Chromium OS Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style license that can be 6 // found in the LICENSE-BSD-3-Clause file. 7 // 8 // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause 9 10 use vm_memory::{GuestAddress, GuestUsize}; 11 12 use crate::address::AddressAllocator; 13 use crate::gsi::GsiAllocator; 14 #[cfg(target_arch = "x86_64")] 15 use crate::gsi::GsiApic; 16 17 use libc::{sysconf, _SC_PAGESIZE}; 18 19 /// Safe wrapper for `sysconf(_SC_PAGESIZE)`. 20 #[inline(always)] 21 fn pagesize() -> usize { 22 // Trivially safe 23 unsafe { sysconf(_SC_PAGESIZE) as usize } 24 } 25 26 /// Manages allocating system resources such as address space and interrupt numbers. 27 /// 28 /// # Example - Use the `SystemAddress` builder. 29 /// 30 /// ``` 31 /// # #[cfg(target_arch = "x86_64")] 32 /// # use vm_allocator::{GsiApic, SystemAllocator}; 33 /// # #[cfg(target_arch = "aarch64")] 34 /// # use vm_allocator::SystemAllocator; 35 /// # use vm_memory::{Address, GuestAddress, GuestUsize}; 36 /// let mut allocator = SystemAllocator::new( 37 /// #[cfg(target_arch = "x86_64")] GuestAddress(0x1000), 38 /// #[cfg(target_arch = "x86_64")] 0x10000, 39 /// GuestAddress(0x10000000), 0x10000000, 40 /// GuestAddress(0x20000000), 0x100000, 41 /// #[cfg(target_arch = "x86_64")] vec![GsiApic::new(5, 19)]).unwrap(); 42 /// #[cfg(target_arch = "x86_64")] 43 /// assert_eq!(allocator.allocate_irq(), Some(5)); 44 /// #[cfg(target_arch = "aarch64")] 45 /// assert_eq!(allocator.allocate_irq(), Some(32)); 46 /// #[cfg(target_arch = "x86_64")] 47 /// assert_eq!(allocator.allocate_irq(), Some(6)); 48 /// #[cfg(target_arch = "aarch64")] 49 /// assert_eq!(allocator.allocate_irq(), Some(33)); 50 /// assert_eq!(allocator.allocate_mmio_addresses(None, 0x1000, Some(0x1000)), Some(GuestAddress(0x1fff_f000))); 51 /// 52 /// ``` 53 pub struct SystemAllocator { 54 #[cfg(target_arch = "x86_64")] 55 io_address_space: AddressAllocator, 56 mmio_address_space: AddressAllocator, 57 mmio_hole_address_space: AddressAllocator, 58 gsi_allocator: GsiAllocator, 59 } 60 61 impl SystemAllocator { 62 /// Creates a new `SystemAllocator` for managing addresses and irq numvers. 63 /// Can return `None` if `base` + `size` overflows a u64 64 /// 65 /// * `io_base` - (X86) The starting address of IO memory. 66 /// * `io_size` - (X86) The size of IO memory. 67 /// * `mmio_base` - The starting address of MMIO memory. 68 /// * `mmio_size` - The size of MMIO memory. 69 /// * `mmio_hole_base` - The starting address of MMIO memory in 32-bit address space. 70 /// * `mmio_hole_size` - The size of MMIO memory in 32-bit address space. 71 /// * `apics` - (X86) Vector of APIC's. 72 /// 73 pub fn new( 74 #[cfg(target_arch = "x86_64")] io_base: GuestAddress, 75 #[cfg(target_arch = "x86_64")] io_size: GuestUsize, 76 mmio_base: GuestAddress, 77 mmio_size: GuestUsize, 78 mmio_hole_base: GuestAddress, 79 mmio_hole_size: GuestUsize, 80 #[cfg(target_arch = "x86_64")] apics: Vec<GsiApic>, 81 ) -> Option<Self> { 82 Some(SystemAllocator { 83 #[cfg(target_arch = "x86_64")] 84 io_address_space: AddressAllocator::new(io_base, io_size)?, 85 mmio_address_space: AddressAllocator::new(mmio_base, mmio_size)?, 86 mmio_hole_address_space: AddressAllocator::new(mmio_hole_base, mmio_hole_size)?, 87 #[cfg(target_arch = "x86_64")] 88 gsi_allocator: GsiAllocator::new(apics), 89 #[cfg(target_arch = "aarch64")] 90 gsi_allocator: GsiAllocator::new(), 91 }) 92 } 93 94 /// Reserves the next available system irq number. 95 pub fn allocate_irq(&mut self) -> Option<u32> { 96 self.gsi_allocator.allocate_irq().ok() 97 } 98 99 /// Reserves the next available GSI. 100 pub fn allocate_gsi(&mut self) -> Option<u32> { 101 self.gsi_allocator.allocate_gsi().ok() 102 } 103 104 #[cfg(target_arch = "x86_64")] 105 /// Reserves a section of `size` bytes of IO address space. 106 pub fn allocate_io_addresses( 107 &mut self, 108 address: Option<GuestAddress>, 109 size: GuestUsize, 110 align_size: Option<GuestUsize>, 111 ) -> Option<GuestAddress> { 112 self.io_address_space 113 .allocate(address, size, Some(align_size.unwrap_or(0x1))) 114 } 115 116 /// Reserves a section of `size` bytes of MMIO address space. 117 pub fn allocate_mmio_addresses( 118 &mut self, 119 address: Option<GuestAddress>, 120 size: GuestUsize, 121 align_size: Option<GuestUsize>, 122 ) -> Option<GuestAddress> { 123 self.mmio_address_space.allocate( 124 address, 125 size, 126 Some(align_size.unwrap_or(pagesize() as u64)), 127 ) 128 } 129 130 /// Reserves a section of `size` bytes of MMIO address space. 131 pub fn allocate_mmio_hole_addresses( 132 &mut self, 133 address: Option<GuestAddress>, 134 size: GuestUsize, 135 align_size: Option<GuestUsize>, 136 ) -> Option<GuestAddress> { 137 self.mmio_hole_address_space.allocate( 138 address, 139 size, 140 Some(align_size.unwrap_or(pagesize() as u64)), 141 ) 142 } 143 144 #[cfg(target_arch = "x86_64")] 145 /// Free an IO address range. 146 /// We can only free a range if it matches exactly an already allocated range. 147 pub fn free_io_addresses(&mut self, address: GuestAddress, size: GuestUsize) { 148 self.io_address_space.free(address, size) 149 } 150 151 /// Free an MMIO address range. 152 /// We can only free a range if it matches exactly an already allocated range. 153 pub fn free_mmio_addresses(&mut self, address: GuestAddress, size: GuestUsize) { 154 self.mmio_address_space.free(address, size) 155 } 156 157 /// Free an MMIO address range from the 32 bits hole. 158 /// We can only free a range if it matches exactly an already allocated range. 159 pub fn free_mmio_hole_addresses(&mut self, address: GuestAddress, size: GuestUsize) { 160 self.mmio_hole_address_space.free(address, size) 161 } 162 } 163