2020-06-05 02:14:17 +00:00
|
|
|
// Copyright 2020 Arm Limited (or its affiliates). All rights reserved.
|
2020-02-05 03:07:16 +00:00
|
|
|
// Copyright © 2020, Oracle and/or its affiliates.
|
|
|
|
//
|
2019-02-25 21:53:01 +00:00
|
|
|
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-06-03 05:01:56 +00:00
|
|
|
//! Implements platform specific functionality.
|
|
|
|
//! Supported platforms: x86_64, aarch64.
|
2019-02-25 21:53:01 +00:00
|
|
|
#![allow(
|
|
|
|
clippy::unreadable_literal,
|
2019-08-15 15:41:40 +00:00
|
|
|
clippy::redundant_static_lifetimes,
|
2019-02-25 21:53:01 +00:00
|
|
|
clippy::cast_lossless,
|
|
|
|
clippy::transmute_ptr_to_ptr,
|
|
|
|
clippy::cast_ptr_alignment
|
|
|
|
)]
|
|
|
|
|
|
|
|
extern crate byteorder;
|
2020-06-02 02:29:54 +00:00
|
|
|
extern crate hypervisor;
|
2019-02-25 21:53:01 +00:00
|
|
|
extern crate kvm_bindings;
|
2020-06-03 05:01:56 +00:00
|
|
|
extern crate kvm_ioctls;
|
2019-02-25 21:53:01 +00:00
|
|
|
extern crate libc;
|
2020-07-08 14:58:10 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2019-09-05 15:29:55 +00:00
|
|
|
#[cfg(feature = "acpi")]
|
2019-08-14 16:14:34 +00:00
|
|
|
extern crate acpi_tables;
|
2019-02-25 21:53:01 +00:00
|
|
|
extern crate arch_gen;
|
2019-06-10 09:14:02 +00:00
|
|
|
extern crate linux_loader;
|
2020-07-08 14:58:10 +00:00
|
|
|
extern crate vm_memory;
|
2019-02-25 21:53:01 +00:00
|
|
|
|
2020-06-03 05:01:56 +00:00
|
|
|
use std::fmt;
|
2019-02-25 21:53:01 +00:00
|
|
|
use std::result;
|
|
|
|
|
2020-06-03 05:01:56 +00:00
|
|
|
/// Type for returning error code.
|
2020-01-24 06:51:15 +00:00
|
|
|
#[derive(Debug)]
|
2019-02-25 21:53:01 +00:00
|
|
|
pub enum Error {
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
/// X86_64 specific error triggered during system configuration.
|
|
|
|
X86_64Setup(x86_64::Error),
|
2020-05-28 07:27:22 +00:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
/// AArch64 specific error triggered during system configuration.
|
|
|
|
AArch64Setup(aarch64::Error),
|
2019-02-25 21:53:01 +00:00
|
|
|
/// The zero page extends past the end of guest_mem.
|
|
|
|
ZeroPagePastRamEnd,
|
|
|
|
/// Error writing the zero page of guest memory.
|
2020-01-24 06:51:15 +00:00
|
|
|
ZeroPageSetup(vm_memory::GuestMemoryError),
|
2020-02-05 03:07:16 +00:00
|
|
|
/// The memory map table extends past the end of guest memory.
|
|
|
|
MemmapTablePastRamEnd,
|
|
|
|
/// Error writing memory map table to guest memory.
|
|
|
|
MemmapTableSetup,
|
|
|
|
/// The hvm_start_info structure extends past the end of guest memory.
|
|
|
|
StartInfoPastRamEnd,
|
|
|
|
/// Error writing hvm_start_info to guest memory.
|
|
|
|
StartInfoSetup,
|
2020-03-15 17:56:07 +00:00
|
|
|
/// Failed to compute initramfs address.
|
|
|
|
InitramfsAddress,
|
2020-04-06 19:21:41 +00:00
|
|
|
/// Error writing module entry to guest memory.
|
|
|
|
ModlistSetup(vm_memory::GuestMemoryError),
|
2020-05-11 15:55:38 +00:00
|
|
|
/// RSDP Beyond Guest Memory
|
|
|
|
RSDPPastRamEnd,
|
2019-02-25 21:53:01 +00:00
|
|
|
}
|
2020-06-03 05:01:56 +00:00
|
|
|
|
|
|
|
/// Type for returning public functions outcome.
|
2019-02-25 21:53:01 +00:00
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
|
|
|
|
2020-06-03 05:01:56 +00:00
|
|
|
/// Type for memory region types.
|
|
|
|
#[derive(PartialEq, Debug)]
|
2019-07-17 16:54:11 +00:00
|
|
|
pub enum RegionType {
|
|
|
|
/// RAM type
|
|
|
|
Ram,
|
2019-07-25 08:20:59 +00: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 16:54:11 +00:00
|
|
|
Reserved,
|
|
|
|
}
|
|
|
|
|
2020-06-03 05:01:56 +00:00
|
|
|
/// Module for aarch64 related functionality.
|
2019-02-25 21:53:01 +00:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
pub mod aarch64;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
pub use aarch64::{
|
2020-06-02 02:29:54 +00:00
|
|
|
arch_memory_regions, configure_system, configure_vcpu, fdt::DeviceInfoForFDT,
|
|
|
|
get_host_cpu_phys_bits, get_kernel_start, layout, layout::CMDLINE_MAX_SIZE, layout::IRQ_BASE,
|
|
|
|
layout::IRQ_MAX, EntryPoint,
|
2019-02-25 21:53:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub mod x86_64;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub use x86_64::{
|
2020-06-02 02:29:54 +00:00
|
|
|
arch_memory_regions, configure_system, configure_vcpu, get_host_cpu_phys_bits,
|
|
|
|
initramfs_load_addr, layout, layout::CMDLINE_MAX_SIZE, layout::CMDLINE_START, regs,
|
|
|
|
BootProtocol, CpuidPatch, CpuidReg, EntryPoint,
|
2019-02-25 21:53:01 +00:00
|
|
|
};
|
2020-03-22 19:44:06 +00:00
|
|
|
|
|
|
|
/// Safe wrapper for `sysconf(_SC_PAGESIZE)`.
|
2020-05-12 09:49:12 +00:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2020-03-22 19:44:06 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn pagesize() -> usize {
|
|
|
|
// Trivially safe
|
|
|
|
unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize }
|
|
|
|
}
|
2020-03-15 17:56:07 +00: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 05:01:56 +00: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")]
|
|
|
|
RTC,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
|
|
|
write!(f, "{:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Structure to describe MMIO device information
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
pub struct MMIODeviceInfo {
|
|
|
|
pub addr: u64,
|
|
|
|
pub irq: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
impl DeviceInfoForFDT for MMIODeviceInfo {
|
|
|
|
fn addr(&self) -> u64 {
|
|
|
|
self.addr
|
|
|
|
}
|
|
|
|
fn irq(&self) -> u32 {
|
|
|
|
self.irq
|
|
|
|
}
|
|
|
|
fn length(&self) -> u64 {
|
|
|
|
4096 as u64
|
|
|
|
}
|
|
|
|
}
|