2019-04-18 08:31:45 +00:00
|
|
|
// Copyright 2018 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
2019-05-08 10:22:53 +00:00
|
|
|
// found in the LICENSE-BSD-3-Clause file.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
2019-04-18 08:31:45 +00:00
|
|
|
|
|
|
|
use vm_memory::{GuestAddress, GuestUsize};
|
|
|
|
|
|
|
|
use crate::address::AddressAllocator;
|
2020-06-09 04:04:40 +00:00
|
|
|
use crate::gsi::GsiAllocator;
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
use crate::gsi::GsiApic;
|
2019-04-18 08:31:45 +00:00
|
|
|
|
|
|
|
use libc::{sysconf, _SC_PAGESIZE};
|
|
|
|
|
|
|
|
/// Safe wrapper for `sysconf(_SC_PAGESIZE)`.
|
|
|
|
#[inline(always)]
|
|
|
|
fn pagesize() -> usize {
|
|
|
|
// Trivially safe
|
|
|
|
unsafe { sysconf(_SC_PAGESIZE) as usize }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Manages allocating system resources such as address space and interrupt numbers.
|
|
|
|
///
|
|
|
|
/// # Example - Use the `SystemAddress` builder.
|
|
|
|
///
|
|
|
|
/// ```
|
2020-06-12 00:58:50 +00:00
|
|
|
/// # #[cfg(target_arch = "x86_64")]
|
2019-07-11 09:48:14 +00:00
|
|
|
/// # use vm_allocator::{GsiApic, SystemAllocator};
|
2020-06-12 00:58:50 +00:00
|
|
|
/// # #[cfg(target_arch = "aarch64")]
|
|
|
|
/// # use vm_allocator::SystemAllocator;
|
2019-04-18 08:31:45 +00:00
|
|
|
/// # use vm_memory::{Address, GuestAddress, GuestUsize};
|
|
|
|
/// let mut allocator = SystemAllocator::new(
|
2020-06-12 00:58:50 +00:00
|
|
|
/// #[cfg(target_arch = "x86_64")] GuestAddress(0x1000),
|
|
|
|
/// #[cfg(target_arch = "x86_64")] 0x10000,
|
2019-04-18 08:31:45 +00:00
|
|
|
/// GuestAddress(0x10000000), 0x10000000,
|
2019-07-17 16:54:11 +00:00
|
|
|
/// GuestAddress(0x20000000), 0x100000,
|
2020-06-12 00:58:50 +00:00
|
|
|
/// #[cfg(target_arch = "x86_64")] vec![GsiApic::new(5, 19)]).unwrap();
|
|
|
|
/// #[cfg(target_arch = "x86_64")]
|
|
|
|
/// assert_eq!(allocator.allocate_irq(), Some(5));
|
|
|
|
/// #[cfg(target_arch = "aarch64")]
|
|
|
|
/// assert_eq!(allocator.allocate_irq(), Some(32));
|
|
|
|
/// #[cfg(target_arch = "x86_64")]
|
|
|
|
/// assert_eq!(allocator.allocate_irq(), Some(6));
|
|
|
|
/// #[cfg(target_arch = "aarch64")]
|
|
|
|
/// assert_eq!(allocator.allocate_irq(), Some(33));
|
|
|
|
/// assert_eq!(allocator.allocate_mmio_addresses(None, 0x1000, Some(0x1000)), Some(GuestAddress(0x1fff_f000)));
|
2019-04-18 08:31:45 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
pub struct SystemAllocator {
|
2020-06-09 06:54:15 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2019-06-26 16:02:00 +00:00
|
|
|
io_address_space: AddressAllocator,
|
2019-04-18 08:31:45 +00:00
|
|
|
mmio_address_space: AddressAllocator,
|
2019-07-17 16:54:11 +00:00
|
|
|
mmio_hole_address_space: AddressAllocator,
|
2019-07-11 09:48:14 +00:00
|
|
|
gsi_allocator: GsiAllocator,
|
2019-04-18 08:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SystemAllocator {
|
|
|
|
/// Creates a new `SystemAllocator` for managing addresses and irq numvers.
|
2019-07-03 19:40:04 +00:00
|
|
|
/// Can return `None` if `base` + `size` overflows a u64
|
2019-04-18 08:31:45 +00:00
|
|
|
///
|
2020-06-09 06:54:15 +00:00
|
|
|
/// * `io_base` - (X86) The starting address of IO memory.
|
|
|
|
/// * `io_size` - (X86) The size of IO memory.
|
2019-04-18 08:31:45 +00:00
|
|
|
/// * `mmio_base` - The starting address of MMIO memory.
|
|
|
|
/// * `mmio_size` - The size of MMIO memory.
|
2020-06-09 04:04:40 +00:00
|
|
|
/// * `mmio_hole_base` - The starting address of MMIO memory in 32-bit address space.
|
|
|
|
/// * `mmio_hole_size` - The size of MMIO memory in 32-bit address space.
|
|
|
|
/// * `apics` - (X86) Vector of APIC's.
|
|
|
|
///
|
2019-04-18 08:31:45 +00:00
|
|
|
pub fn new(
|
2020-06-09 06:54:15 +00:00
|
|
|
#[cfg(target_arch = "x86_64")] io_base: GuestAddress,
|
|
|
|
#[cfg(target_arch = "x86_64")] io_size: GuestUsize,
|
2019-04-18 08:31:45 +00:00
|
|
|
mmio_base: GuestAddress,
|
|
|
|
mmio_size: GuestUsize,
|
2019-07-17 16:54:11 +00:00
|
|
|
mmio_hole_base: GuestAddress,
|
|
|
|
mmio_hole_size: GuestUsize,
|
2020-06-09 04:04:40 +00:00
|
|
|
#[cfg(target_arch = "x86_64")] apics: Vec<GsiApic>,
|
2019-04-18 08:31:45 +00:00
|
|
|
) -> Option<Self> {
|
|
|
|
Some(SystemAllocator {
|
2020-06-09 06:54:15 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2019-07-03 19:40:04 +00:00
|
|
|
io_address_space: AddressAllocator::new(io_base, io_size)?,
|
|
|
|
mmio_address_space: AddressAllocator::new(mmio_base, mmio_size)?,
|
2019-07-17 16:54:11 +00:00
|
|
|
mmio_hole_address_space: AddressAllocator::new(mmio_hole_base, mmio_hole_size)?,
|
2020-06-09 04:04:40 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2019-07-11 09:48:14 +00:00
|
|
|
gsi_allocator: GsiAllocator::new(apics),
|
2020-06-09 04:04:40 +00:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
gsi_allocator: GsiAllocator::new(),
|
2019-04-18 08:31:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reserves the next available system irq number.
|
|
|
|
pub fn allocate_irq(&mut self) -> Option<u32> {
|
2019-07-11 09:48:14 +00:00
|
|
|
self.gsi_allocator.allocate_irq().ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reserves the next available GSI.
|
|
|
|
pub fn allocate_gsi(&mut self) -> Option<u32> {
|
|
|
|
self.gsi_allocator.allocate_gsi().ok()
|
2019-04-18 08:31:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 06:54:15 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2019-04-18 08:31:45 +00:00
|
|
|
/// Reserves a section of `size` bytes of IO address space.
|
|
|
|
pub fn allocate_io_addresses(
|
|
|
|
&mut self,
|
2019-06-26 16:02:00 +00:00
|
|
|
address: Option<GuestAddress>,
|
2019-04-18 08:31:45 +00:00
|
|
|
size: GuestUsize,
|
2019-07-03 19:40:04 +00:00
|
|
|
align_size: Option<GuestUsize>,
|
2019-04-18 08:31:45 +00:00
|
|
|
) -> Option<GuestAddress> {
|
2019-07-03 19:40:04 +00:00
|
|
|
self.io_address_space
|
|
|
|
.allocate(address, size, Some(align_size.unwrap_or(0x1)))
|
2019-04-18 08:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reserves a section of `size` bytes of MMIO address space.
|
|
|
|
pub fn allocate_mmio_addresses(
|
|
|
|
&mut self,
|
|
|
|
address: Option<GuestAddress>,
|
|
|
|
size: GuestUsize,
|
2019-07-03 19:40:04 +00:00
|
|
|
align_size: Option<GuestUsize>,
|
2019-04-18 08:31:45 +00:00
|
|
|
) -> Option<GuestAddress> {
|
2019-07-03 19:40:04 +00:00
|
|
|
self.mmio_address_space.allocate(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
Some(align_size.unwrap_or(pagesize() as u64)),
|
|
|
|
)
|
2019-04-18 08:31:45 +00:00
|
|
|
}
|
2019-05-21 15:43:03 +00:00
|
|
|
|
2019-07-17 16:54:11 +00:00
|
|
|
/// Reserves a section of `size` bytes of MMIO address space.
|
|
|
|
pub fn allocate_mmio_hole_addresses(
|
|
|
|
&mut self,
|
|
|
|
address: Option<GuestAddress>,
|
|
|
|
size: GuestUsize,
|
|
|
|
align_size: Option<GuestUsize>,
|
|
|
|
) -> Option<GuestAddress> {
|
|
|
|
self.mmio_hole_address_space.allocate(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
Some(align_size.unwrap_or(pagesize() as u64)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-09 06:54:15 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2019-05-21 15:43:03 +00:00
|
|
|
/// Free an IO address range.
|
|
|
|
/// We can only free a range if it matches exactly an already allocated range.
|
|
|
|
pub fn free_io_addresses(&mut self, address: GuestAddress, size: GuestUsize) {
|
2019-06-26 16:02:00 +00:00
|
|
|
self.io_address_space.free(address, size)
|
2019-05-21 15:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Free an MMIO address range.
|
|
|
|
/// We can only free a range if it matches exactly an already allocated range.
|
|
|
|
pub fn free_mmio_addresses(&mut self, address: GuestAddress, size: GuestUsize) {
|
|
|
|
self.mmio_address_space.free(address, size)
|
|
|
|
}
|
2020-03-11 08:30:57 +00:00
|
|
|
|
|
|
|
/// Free an MMIO address range from the 32 bits hole.
|
|
|
|
/// We can only free a range if it matches exactly an already allocated range.
|
|
|
|
pub fn free_mmio_hole_addresses(&mut self, address: GuestAddress, size: GuestUsize) {
|
|
|
|
self.mmio_hole_address_space.free(address, size)
|
|
|
|
}
|
2019-04-18 08:31:45 +00:00
|
|
|
}
|