2020-06-05 10:14:17 +08:00
|
|
|
// Copyright 2020 Arm Limited (or its affiliates). All rights reserved.
|
2020-02-04 22:07:16 -05:00
|
|
|
// Copyright © 2020, Oracle and/or its affiliates.
|
|
|
|
//
|
2019-02-25 22:53:01 +01:00
|
|
|
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-06-03 13:01:56 +08:00
|
|
|
//! Implements platform specific functionality.
|
|
|
|
//! Supported platforms: x86_64, aarch64.
|
2019-02-25 22:53:01 +01:00
|
|
|
|
2020-07-08 16:58:10 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2019-02-25 22:53:01 +01:00
|
|
|
|
2021-08-06 19:28:42 -04:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
use crate::x86_64::SgxEpcSection;
|
2022-05-17 14:04:38 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-08-06 19:28:42 -04:00
|
|
|
use std::collections::BTreeMap;
|
2020-06-03 13:01:56 +08:00
|
|
|
use std::fmt;
|
2019-02-25 22:53:01 +01:00
|
|
|
use std::result;
|
2021-08-06 19:28:42 -04:00
|
|
|
use std::sync::Arc;
|
2022-04-20 16:43:53 +01:00
|
|
|
use thiserror::Error;
|
2021-10-05 14:20:19 +02:00
|
|
|
use versionize::{VersionMap, Versionize, VersionizeError, VersionizeResult};
|
|
|
|
use versionize_derive::Versionize;
|
|
|
|
use vm_migration::VersionMapped;
|
2019-02-25 22:53:01 +01:00
|
|
|
|
2021-06-02 12:08:04 -07:00
|
|
|
type GuestMemoryMmap = vm_memory::GuestMemoryMmap<vm_memory::bitmap::AtomicBitmap>;
|
2021-08-06 19:28:42 -04:00
|
|
|
type GuestRegionMmap = vm_memory::GuestRegionMmap<vm_memory::bitmap::AtomicBitmap>;
|
2021-06-02 12:08:04 -07:00
|
|
|
|
2020-06-03 13:01:56 +08:00
|
|
|
/// Type for returning error code.
|
2022-04-20 16:43:53 +01:00
|
|
|
#[derive(Debug, Error)]
|
2019-02-25 22:53:01 +01:00
|
|
|
pub enum Error {
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2022-04-20 16:43:53 +01:00
|
|
|
#[error("Platform specific error (x86_64): {0:?}")]
|
|
|
|
PlatformSpecific(x86_64::Error),
|
2020-05-28 15:27:22 +08:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
2022-04-20 16:43:53 +01:00
|
|
|
#[error("Platform specific error (aarch64): {0:?}")]
|
|
|
|
PlatformSpecific(aarch64::Error),
|
|
|
|
#[error("The memory map table extends past the end of guest memory")]
|
2020-02-04 22:07:16 -05:00
|
|
|
MemmapTablePastRamEnd,
|
2022-04-20 16:43:53 +01:00
|
|
|
#[error("Error writing memory map table to guest memory")]
|
2020-02-04 22:07:16 -05:00
|
|
|
MemmapTableSetup,
|
2022-04-20 16:43:53 +01:00
|
|
|
#[error("The hvm_start_info structure extends past the end of guest memory")]
|
2020-02-04 22:07:16 -05:00
|
|
|
StartInfoPastRamEnd,
|
2022-04-20 16:43:53 +01:00
|
|
|
#[error("Error writing hvm_start_info to guest memory")]
|
2020-02-04 22:07:16 -05:00
|
|
|
StartInfoSetup,
|
2022-04-20 16:43:53 +01:00
|
|
|
#[error("Failed to compute initramfs address")]
|
2020-03-15 18:56:07 +01:00
|
|
|
InitramfsAddress,
|
2022-04-20 16:43:53 +01:00
|
|
|
#[error("Error writing module entry to guest memory: {0}")]
|
|
|
|
ModlistSetup(#[source] vm_memory::GuestMemoryError),
|
|
|
|
#[error("RSDP extends past the end of guest memory")]
|
2021-03-25 17:01:21 +00:00
|
|
|
RsdpPastRamEnd,
|
2024-02-09 17:35:29 +01:00
|
|
|
#[error("Failed to setup Zero Page for bzImage")]
|
|
|
|
ZeroPageSetup(#[source] vm_memory::GuestMemoryError),
|
|
|
|
#[error("Zero Page for bzImage past RAM end")]
|
|
|
|
ZeroPagePastRamEnd,
|
2019-02-25 22:53:01 +01:00
|
|
|
}
|
2020-06-03 13:01:56 +08:00
|
|
|
|
|
|
|
/// Type for returning public functions outcome.
|
2019-02-25 22:53:01 +01:00
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
|
|
|
|
2020-06-03 13:01:56 +08:00
|
|
|
/// Type for memory region types.
|
2022-06-30 16:41:46 +01:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize, Versionize)]
|
2019-07-17 09:54:11 -07:00
|
|
|
pub enum RegionType {
|
|
|
|
/// RAM type
|
|
|
|
Ram,
|
2019-07-25 10:20:59 +02:00
|
|
|
|
|
|
|
/// SubRegion memory region.
|
|
|
|
/// A SubRegion is a memory region sub-region, allowing for a region
|
|
|
|
/// to be split into sub regions managed separately.
|
|
|
|
/// For example, the x86 32-bit memory hole is a SubRegion.
|
|
|
|
SubRegion,
|
|
|
|
|
|
|
|
/// Reserved type.
|
|
|
|
/// A Reserved memory region is one that should not be used for memory
|
|
|
|
/// allocation. This type can be used to prevent the VMM from allocating
|
|
|
|
/// memory ranges in a specific address range.
|
2019-07-17 09:54:11 -07:00
|
|
|
Reserved,
|
|
|
|
}
|
|
|
|
|
2021-10-05 14:20:19 +02:00
|
|
|
impl VersionMapped for RegionType {}
|
|
|
|
|
2020-06-03 13:01:56 +08:00
|
|
|
/// Module for aarch64 related functionality.
|
2019-02-25 22:53:01 +01:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
pub mod aarch64;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
pub use aarch64::{
|
2021-03-25 17:01:21 +00:00
|
|
|
arch_memory_regions, configure_system, configure_vcpu, fdt::DeviceInfoForFdt,
|
2022-04-06 06:01:28 +08:00
|
|
|
get_host_cpu_phys_bits, initramfs_load_addr, layout, layout::CMDLINE_MAX_SIZE,
|
2023-03-23 17:10:19 +00:00
|
|
|
layout::IRQ_BASE, uefi, EntryPoint, _NSIG,
|
2019-02-25 22:53:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub mod x86_64;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub use x86_64::{
|
2021-07-14 14:44:38 -07:00
|
|
|
arch_memory_regions, configure_system, configure_vcpu, generate_common_cpuid,
|
2023-12-12 10:23:28 -08:00
|
|
|
generate_ram_ranges, get_host_cpu_phys_bits, initramfs_load_addr, layout,
|
|
|
|
layout::CMDLINE_MAX_SIZE, layout::CMDLINE_START, regs, CpuidConfig, CpuidFeatureEntry,
|
|
|
|
EntryPoint, _NSIG,
|
2019-02-25 22:53:01 +01:00
|
|
|
};
|
2020-03-22 20:44:06 +01:00
|
|
|
|
|
|
|
/// Safe wrapper for `sysconf(_SC_PAGESIZE)`.
|
2020-05-12 17:49:12 +08:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2020-03-22 20:44:06 +01:00
|
|
|
#[inline(always)]
|
|
|
|
fn pagesize() -> usize {
|
2022-11-16 22:01:31 +00:00
|
|
|
// SAFETY: Trivially safe
|
2020-03-22 20:44:06 +01:00
|
|
|
unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize }
|
|
|
|
}
|
2020-03-15 18:56:07 +01:00
|
|
|
|
2021-08-06 19:28:42 -04:00
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct NumaNode {
|
|
|
|
pub memory_regions: Vec<Arc<GuestRegionMmap>>,
|
|
|
|
pub hotplug_regions: Vec<Arc<GuestRegionMmap>>,
|
|
|
|
pub cpus: Vec<u8>,
|
2023-10-15 19:36:34 -07:00
|
|
|
pub pci_segments: Vec<u16>,
|
2021-08-06 19:28:42 -04:00
|
|
|
pub distances: BTreeMap<u32, u8>,
|
|
|
|
pub memory_zones: Vec<String>,
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub sgx_epc_sections: Vec<SgxEpcSection>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type NumaNodes = BTreeMap<u32, NumaNode>;
|
|
|
|
|
2020-03-15 18:56:07 +01:00
|
|
|
/// Type for passing information about the initramfs in the guest memory.
|
|
|
|
pub struct InitramfsConfig {
|
|
|
|
/// Load address of initramfs in guest memory
|
|
|
|
pub address: vm_memory::GuestAddress,
|
|
|
|
/// Size of initramfs in guest memory
|
|
|
|
pub size: usize,
|
|
|
|
}
|
2020-06-03 13:01:56 +08:00
|
|
|
|
|
|
|
/// Types of devices that can get attached to this platform.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy)]
|
|
|
|
pub enum DeviceType {
|
|
|
|
/// Device Type: Virtio.
|
|
|
|
Virtio(u32),
|
|
|
|
/// Device Type: Serial.
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
Serial,
|
|
|
|
/// Device Type: RTC.
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
2021-03-25 17:01:21 +00:00
|
|
|
Rtc,
|
2021-03-07 20:11:07 +08:00
|
|
|
/// Device Type: GPIO.
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
2021-03-25 17:01:21 +00:00
|
|
|
Gpio,
|
2020-06-03 13:01:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Default (smallest) memory page size for the supported architectures.
|
|
|
|
pub const PAGE_SIZE: usize = 4096;
|
|
|
|
|
|
|
|
impl fmt::Display for DeviceType {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2022-12-14 11:41:15 +00:00
|
|
|
write!(f, "{self:?}")
|
2020-06-03 13:01:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Structure to describe MMIO device information
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
2021-03-25 17:01:21 +00:00
|
|
|
pub struct MmioDeviceInfo {
|
2020-06-03 13:01:56 +08:00
|
|
|
pub addr: u64,
|
2021-11-28 02:52:15 +00:00
|
|
|
pub len: u64,
|
2020-06-03 13:01:56 +08:00
|
|
|
pub irq: u32,
|
|
|
|
}
|
|
|
|
|
2021-11-25 03:12:37 -05:00
|
|
|
/// Structure to describe PCI space information
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
pub struct PciSpaceInfo {
|
|
|
|
pub pci_segment_id: u16,
|
|
|
|
pub mmio_config_address: u64,
|
|
|
|
pub pci_device_space_start: u64,
|
|
|
|
pub pci_device_space_size: u64,
|
|
|
|
}
|
|
|
|
|
2020-06-03 13:01:56 +08:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
2021-03-25 17:01:21 +00:00
|
|
|
impl DeviceInfoForFdt for MmioDeviceInfo {
|
2020-06-03 13:01:56 +08:00
|
|
|
fn addr(&self) -> u64 {
|
|
|
|
self.addr
|
|
|
|
}
|
|
|
|
fn irq(&self) -> u32 {
|
|
|
|
self.irq
|
|
|
|
}
|
|
|
|
fn length(&self) -> u64 {
|
2021-11-28 02:52:15 +00:00
|
|
|
self.len
|
2020-06-03 13:01:56 +08:00
|
|
|
}
|
|
|
|
}
|