2020-06-03 05:01:56 +00:00
|
|
|
// Copyright 2020 Arm Limited (or its affiliates). All rights reserved.
|
2019-02-25 21:53:01 +00:00
|
|
|
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-06-03 05:01:56 +00:00
|
|
|
/// Module for the flattened device tree.
|
|
|
|
pub mod fdt;
|
2020-04-20 02:03:00 +00:00
|
|
|
/// Module for the global interrupt controller configuration.
|
|
|
|
pub mod gic;
|
|
|
|
/// Layout for this aarch64 system.
|
2019-02-25 21:53:01 +00:00
|
|
|
pub mod layout;
|
2020-06-03 04:50:09 +00:00
|
|
|
/// Logic for configuring aarch64 registers.
|
|
|
|
pub mod regs;
|
2021-04-19 07:50:46 +00:00
|
|
|
/// Module for loading UEFI binary.
|
|
|
|
pub mod uefi;
|
2019-02-25 21:53:01 +00:00
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
pub use self::fdt::DeviceInfoForFdt;
|
2021-11-25 08:12:37 +00:00
|
|
|
use crate::{DeviceType, GuestMemoryMmap, NumaNodes, PciSpaceInfo, RegionType};
|
2021-05-11 14:19:04 +00:00
|
|
|
use gic::GicDevice;
|
2021-06-16 04:45:32 +00:00
|
|
|
use log::{log_enabled, Level};
|
2020-06-03 05:01:56 +00:00
|
|
|
use std::collections::HashMap;
|
2021-06-09 12:45:11 +00:00
|
|
|
use std::convert::TryInto;
|
2020-06-03 04:50:09 +00:00
|
|
|
use std::fmt::Debug;
|
2020-06-02 02:29:54 +00:00
|
|
|
use std::sync::Arc;
|
2022-01-14 18:21:08 +00:00
|
|
|
use vm_memory::{Address, GuestAddress, GuestMemory, GuestUsize};
|
2020-05-28 07:27:22 +00:00
|
|
|
|
2020-06-03 05:01:56 +00:00
|
|
|
/// Errors thrown while configuring aarch64 system.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2020-06-09 10:28:02 +00:00
|
|
|
/// Failed to create a FDT.
|
2021-05-07 02:34:37 +00:00
|
|
|
SetupFdt,
|
|
|
|
|
|
|
|
/// Failed to write FDT to memory.
|
|
|
|
WriteFdtToMemory(fdt::Error),
|
2020-06-09 07:24:23 +00:00
|
|
|
|
2020-06-09 10:28:02 +00:00
|
|
|
/// Failed to create a GIC.
|
2021-03-25 17:01:21 +00:00
|
|
|
SetupGic(gic::Error),
|
2020-06-09 10:28:02 +00:00
|
|
|
|
2020-07-14 10:11:03 +00:00
|
|
|
/// Failed to compute the initramfs address.
|
|
|
|
InitramfsAddress,
|
2020-06-09 07:24:23 +00:00
|
|
|
|
|
|
|
/// Error configuring the general purpose registers
|
2021-03-25 17:01:21 +00:00
|
|
|
RegsConfiguration(regs::Error),
|
2020-06-09 07:24:23 +00:00
|
|
|
|
2020-08-28 09:06:54 +00:00
|
|
|
/// Error configuring the MPIDR register
|
2021-03-25 17:01:21 +00:00
|
|
|
VcpuRegMpidr(hypervisor::HypervisorCpuError),
|
2022-01-15 07:27:55 +00:00
|
|
|
|
|
|
|
/// Error initializing PMU for vcpu
|
|
|
|
VcpuInitPmu,
|
2020-06-03 05:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Error> for super::Error {
|
|
|
|
fn from(e: Error) -> super::Error {
|
|
|
|
super::Error::AArch64Setup(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 07:24:23 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
/// Specifies the entry point address where the guest must start
|
|
|
|
/// executing code.
|
|
|
|
pub struct EntryPoint {
|
|
|
|
/// Address in guest memory where the guest must start execution
|
|
|
|
pub entry_addr: GuestAddress,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configure the specified VCPU, and return its MPIDR.
|
|
|
|
pub fn configure_vcpu(
|
2020-06-02 02:29:54 +00:00
|
|
|
fd: &Arc<dyn hypervisor::Vcpu>,
|
2020-06-09 07:24:23 +00:00
|
|
|
id: u8,
|
|
|
|
kernel_entry_point: Option<EntryPoint>,
|
|
|
|
) -> super::Result<u64> {
|
|
|
|
if let Some(kernel_entry_point) = kernel_entry_point {
|
2022-01-14 18:21:08 +00:00
|
|
|
regs::setup_regs(fd, id, kernel_entry_point.entry_addr.raw_value())
|
|
|
|
.map_err(Error::RegsConfiguration)?;
|
2020-06-09 07:24:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
let mpidr = fd.read_mpidr().map_err(Error::VcpuRegMpidr)?;
|
2020-06-09 07:24:23 +00:00
|
|
|
Ok(mpidr)
|
|
|
|
}
|
2020-06-03 05:01:56 +00:00
|
|
|
|
|
|
|
pub fn arch_memory_regions(size: GuestUsize) -> Vec<(GuestAddress, usize, RegionType)> {
|
2021-02-02 12:27:14 +00:00
|
|
|
// Normally UEFI should be loaded to a flash area at the beginning of memory.
|
|
|
|
// But now flash memory type is not supported.
|
2021-06-24 01:57:01 +00:00
|
|
|
// As a workaround, we take 4 MiB memory from the main RAM for UEFI.
|
2021-02-02 12:27:14 +00:00
|
|
|
// As a result, the RAM that the guest can see is less than what has been
|
|
|
|
// assigned in command line, when ACPI and UEFI is enabled.
|
|
|
|
let ram_deduction = if cfg!(feature = "acpi") {
|
|
|
|
layout::UEFI_SIZE
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
vec![
|
2021-06-24 01:57:01 +00:00
|
|
|
// 0 ~ 4 MiB: Reserved for UEFI space
|
2021-02-02 12:27:14 +00:00
|
|
|
#[cfg(feature = "acpi")]
|
|
|
|
(GuestAddress(0), layout::UEFI_SIZE as usize, RegionType::Ram),
|
|
|
|
#[cfg(not(feature = "acpi"))]
|
2021-03-25 17:01:21 +00:00
|
|
|
(
|
|
|
|
GuestAddress(0),
|
2021-02-02 12:27:14 +00:00
|
|
|
layout::UEFI_SIZE as usize,
|
|
|
|
RegionType::Reserved,
|
|
|
|
),
|
2021-06-24 01:57:01 +00:00
|
|
|
// 4 MiB ~ 256 MiB: Gic and legacy devices
|
2021-02-02 12:27:14 +00:00
|
|
|
(
|
|
|
|
GuestAddress(layout::UEFI_SIZE),
|
|
|
|
(layout::MEM_32BIT_DEVICES_START.0 - layout::UEFI_SIZE) as usize,
|
2021-03-25 17:01:21 +00:00
|
|
|
RegionType::Reserved,
|
|
|
|
),
|
2021-02-02 12:27:14 +00:00
|
|
|
// 256 MiB ~ 768 MiB: MMIO space
|
2021-03-25 17:01:21 +00:00
|
|
|
(
|
|
|
|
layout::MEM_32BIT_DEVICES_START,
|
|
|
|
layout::MEM_32BIT_DEVICES_SIZE as usize,
|
|
|
|
RegionType::SubRegion,
|
|
|
|
),
|
2021-02-02 12:27:14 +00:00
|
|
|
// 768 MiB ~ 1 GiB: reserved. The leading 256M for PCIe MMCONFIG space
|
2021-03-25 17:01:21 +00:00
|
|
|
(
|
|
|
|
layout::PCI_MMCONFIG_START,
|
2021-02-02 12:27:14 +00:00
|
|
|
layout::PCI_MMCONFIG_SIZE as usize,
|
2021-03-25 17:01:21 +00:00
|
|
|
RegionType::Reserved,
|
|
|
|
),
|
2021-02-02 12:27:14 +00:00
|
|
|
// 1 GiB ~ : Ram
|
2021-03-25 17:01:21 +00:00
|
|
|
(
|
|
|
|
GuestAddress(layout::RAM_64BIT_START),
|
2021-02-02 12:27:14 +00:00
|
|
|
(size - ram_deduction) as usize,
|
2021-03-25 17:01:21 +00:00
|
|
|
RegionType::Ram,
|
|
|
|
),
|
|
|
|
]
|
2020-06-03 05:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Configures the system and should be called once per vm before starting vcpu threads.
|
2021-07-29 07:44:50 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-03-25 17:01:21 +00:00
|
|
|
pub fn configure_system<T: DeviceInfoForFdt + Clone + Debug, S: ::std::hash::BuildHasher>(
|
2020-06-03 05:01:56 +00:00
|
|
|
guest_mem: &GuestMemoryMmap,
|
2021-09-24 07:43:14 +00:00
|
|
|
cmdline: &str,
|
2020-06-03 05:01:56 +00:00
|
|
|
vcpu_mpidr: Vec<u64>,
|
2021-07-29 07:44:50 +00:00
|
|
|
vcpu_topology: Option<(u8, u8, u8)>,
|
2020-08-23 07:44:57 +00:00
|
|
|
device_info: &HashMap<(DeviceType, String), T, S>,
|
2020-06-09 03:27:09 +00:00
|
|
|
initrd: &Option<super::InitramfsConfig>,
|
2021-11-25 08:12:37 +00:00
|
|
|
pci_space_info: &[PciSpaceInfo],
|
2021-08-12 13:37:44 +00:00
|
|
|
virtio_iommu_bdf: Option<u32>,
|
2021-05-31 14:30:24 +00:00
|
|
|
gic_device: &dyn GicDevice,
|
2021-08-06 23:41:33 +00:00
|
|
|
numa_nodes: &NumaNodes,
|
2022-01-13 10:00:15 +00:00
|
|
|
pmu_supported: bool,
|
2021-05-31 14:30:24 +00:00
|
|
|
) -> super::Result<()> {
|
2021-05-07 02:34:37 +00:00
|
|
|
let fdt_final = fdt::create_fdt(
|
2020-06-03 05:01:56 +00:00
|
|
|
guest_mem,
|
2021-09-24 07:43:14 +00:00
|
|
|
cmdline,
|
2020-06-03 05:01:56 +00:00
|
|
|
vcpu_mpidr,
|
2021-07-29 07:44:50 +00:00
|
|
|
vcpu_topology,
|
2020-06-03 05:01:56 +00:00
|
|
|
device_info,
|
2021-05-31 14:30:24 +00:00
|
|
|
gic_device,
|
2020-06-03 05:01:56 +00:00
|
|
|
initrd,
|
2021-11-25 08:12:37 +00:00
|
|
|
pci_space_info,
|
2021-08-06 23:41:33 +00:00
|
|
|
numa_nodes,
|
2021-08-12 13:37:44 +00:00
|
|
|
virtio_iommu_bdf,
|
2022-01-13 10:00:15 +00:00
|
|
|
pmu_supported,
|
2020-06-03 05:01:56 +00:00
|
|
|
)
|
2021-05-07 02:34:37 +00:00
|
|
|
.map_err(|_| Error::SetupFdt)?;
|
|
|
|
|
2021-06-16 04:45:32 +00:00
|
|
|
if log_enabled!(Level::Debug) {
|
|
|
|
fdt::print_fdt(&fdt_final);
|
|
|
|
}
|
|
|
|
|
2021-05-07 02:34:37 +00:00
|
|
|
fdt::write_fdt_to_memory(fdt_final, guest_mem).map_err(Error::WriteFdtToMemory)?;
|
2020-06-03 05:01:56 +00:00
|
|
|
|
2021-05-31 14:30:24 +00:00
|
|
|
Ok(())
|
2019-02-25 21:53:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 10:11:03 +00:00
|
|
|
/// Returns the memory address where the initramfs could be loaded.
|
|
|
|
pub fn initramfs_load_addr(
|
|
|
|
guest_mem: &GuestMemoryMmap,
|
|
|
|
initramfs_size: usize,
|
|
|
|
) -> super::Result<u64> {
|
|
|
|
let round_to_pagesize = |size| (size + (super::PAGE_SIZE - 1)) & !(super::PAGE_SIZE - 1);
|
2021-02-02 12:27:14 +00:00
|
|
|
match guest_mem
|
|
|
|
.last_addr()
|
|
|
|
.checked_sub(round_to_pagesize(initramfs_size) as u64 - 1)
|
2020-07-14 10:11:03 +00:00
|
|
|
{
|
|
|
|
Some(offset) => {
|
|
|
|
if guest_mem.address_in_range(offset) {
|
|
|
|
Ok(offset.raw_value())
|
|
|
|
} else {
|
|
|
|
Err(super::Error::AArch64Setup(Error::InitramfsAddress))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Err(super::Error::AArch64Setup(Error::InitramfsAddress)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 05:01:56 +00:00
|
|
|
/// Returns the memory address where the kernel could be loaded.
|
|
|
|
pub fn get_kernel_start() -> u64 {
|
2021-02-02 12:27:14 +00:00
|
|
|
layout::KERNEL_START
|
2019-02-25 21:53:01 +00:00
|
|
|
}
|
2020-05-12 09:49:12 +00:00
|
|
|
|
2021-04-19 07:50:46 +00:00
|
|
|
///Return guest memory address where the uefi should be loaded.
|
|
|
|
pub fn get_uefi_start() -> u64 {
|
|
|
|
layout::UEFI_START
|
|
|
|
}
|
|
|
|
|
2020-06-03 04:50:09 +00:00
|
|
|
// Auxiliary function to get the address where the device tree blob is loaded.
|
2021-02-02 12:27:14 +00:00
|
|
|
fn get_fdt_addr() -> u64 {
|
|
|
|
layout::FDT_START
|
2020-06-03 04:50:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 09:49:12 +00:00
|
|
|
pub fn get_host_cpu_phys_bits() -> u8 {
|
2021-06-09 12:45:11 +00:00
|
|
|
// A dummy hypervisor created only for querying the host IPA size and will
|
|
|
|
// be freed after the query.
|
|
|
|
let hv = hypervisor::new().unwrap();
|
|
|
|
let host_cpu_phys_bits = hv.get_host_ipa_limit().try_into().unwrap();
|
|
|
|
if host_cpu_phys_bits == 0 {
|
|
|
|
// Host kernel does not support `get_host_ipa_limit`,
|
|
|
|
// we return the default value 40 here.
|
|
|
|
40
|
|
|
|
} else {
|
|
|
|
host_cpu_phys_bits
|
|
|
|
}
|
2020-05-12 09:49:12 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 05:01:56 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_arch_memory_regions_dram() {
|
|
|
|
let regions = arch_memory_regions((1usize << 32) as u64); //4GB
|
2021-02-02 12:27:14 +00:00
|
|
|
assert_eq!(5, regions.len());
|
|
|
|
assert_eq!(GuestAddress(layout::RAM_64BIT_START), regions[4].0);
|
|
|
|
assert_eq!(1usize << 32, regions[4].1);
|
|
|
|
assert_eq!(RegionType::Ram, regions[4].2);
|
2020-06-03 05:01:56 +00:00
|
|
|
}
|
|
|
|
}
|