mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
This commit is the first in a series of similar commits to clean up
obsolete `extern crate` statements
Since Rust 1.30, normal macros can be imported via `use`, and with Rust
1.31 and edition 2018 this has become the preferred approach.
`extern crate` is only needed for `alloc` in `no_std` crates, which does
not apply here.
By dropping these (often redundant or odd) `extern crate` lines, we
expose the actual dependencies more clearly and reduce technical debt.
## Auto-generation of the series
Most of this series was produced automatically:
1. Removed all "extern crate" references
2. Run the script [0] to add missing `use` statements
3. Run `cargo +nightly fmt --all`
4. Fix the remaining problems manually
The treewide changes were then split into per-folder commits.
[0]
```python
import os
import re
# Mapping of macro/function usage to imports
MACRO_IMPORTS = {
"info!": "use log::info;\n",
"debug!": "use log::debug;\n",
"error!": "use log::error;\n",
"trace!": "use log::trace;\n",
"warn!": "use log::warn;\n",
"event!": "use event_monitor::event;\n",
"anyhow!(": "use anyhow::anyhow;\n",
"bitflags!(": "use bitflags::bitflags;\n",
"ioctl_ior_nr!": "use vmm_sys_util::{ioctl_ior_nr};\n",
"ioctl_iow_nr!": "use vmm_sys_util::{ioctl_iow_nr};\n",
}
# Regex for finding the first use statement
USE_REGEX = re.compile(r"^\s*(use|pub use) .+?;")
def process_file(path):
with open(path, "r", encoding="utf-8") as f:
lines = f.readlines()
content = "".join(lines)
existing_imports = set(lines)
needed_imports = set()
# Check macros/functions against mapping, only add if not already present
for key, import_stmt in MACRO_IMPORTS.items():
if key in content and import_stmt not in existing_imports:
needed_imports.add(import_stmt)
if not needed_imports:
print(f"Unmodified {path} (no new imports needed)")
return # Nothing to do
# Find first use or pub use statement
for i, line in enumerate(lines):
if USE_REGEX.match(line):
insertion_index = i + 1
break
else:
print(f"Unmodified {path} (no use or pub use statement found)")
return # No use statement found, skip file
# Insert imports
lines[insertion_index:insertion_index] = list(needed_imports)
# Write back file
with open(path, "w", encoding="utf-8") as f:
f.writelines(lines)
print(f"Modified {path}, added imports: {''.join(needed_imports).strip()}")
for root, _, files in os.walk("."):
for file in files:
if file.endswith(".rs"):
process_file(os.path.join(root, file))
```
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
On-behalf-of: SAP philipp.schuster@sap.com
194 lines
6.0 KiB
Rust
194 lines
6.0 KiB
Rust
// Copyright © 2024 Institute of Software, CAS. All rights reserved.
|
|
// Copyright 2020 Arm Limited (or its affiliates). All rights reserved.
|
|
// Copyright © 2020, Oracle and/or its affiliates.
|
|
//
|
|
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//! Implements platform specific functionality.
|
|
//! Supported platforms: x86_64, aarch64, riscv64.
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::sync::Arc;
|
|
use std::{fmt, result};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use thiserror::Error;
|
|
|
|
type GuestMemoryMmap = vm_memory::GuestMemoryMmap<vm_memory::bitmap::AtomicBitmap>;
|
|
type GuestRegionMmap = vm_memory::GuestRegionMmap<vm_memory::bitmap::AtomicBitmap>;
|
|
|
|
/// Type for returning error code.
|
|
#[derive(Debug, Error)]
|
|
pub enum Error {
|
|
#[cfg(target_arch = "x86_64")]
|
|
#[error("Platform specific error (x86_64)")]
|
|
PlatformSpecific(#[from] x86_64::Error),
|
|
#[cfg(target_arch = "aarch64")]
|
|
#[error("Platform specific error (aarch64)")]
|
|
PlatformSpecific(#[from] aarch64::Error),
|
|
#[cfg(target_arch = "riscv64")]
|
|
#[error("Platform specific error (riscv64)")]
|
|
PlatformSpecific(#[from] riscv64::Error),
|
|
#[error("The memory map table extends past the end of guest memory")]
|
|
MemmapTablePastRamEnd,
|
|
#[error("Error writing memory map table to guest memory")]
|
|
MemmapTableSetup,
|
|
#[error("The hvm_start_info structure extends past the end of guest memory")]
|
|
StartInfoPastRamEnd,
|
|
#[error("Error writing hvm_start_info to guest memory")]
|
|
StartInfoSetup,
|
|
#[error("Failed to compute initramfs address")]
|
|
InitramfsAddress,
|
|
#[error("Error writing module entry to guest memory")]
|
|
ModlistSetup(#[source] vm_memory::GuestMemoryError),
|
|
#[error("RSDP extends past the end of guest memory")]
|
|
RsdpPastRamEnd,
|
|
#[error("Failed to setup Zero Page for bzImage")]
|
|
ZeroPageSetup(#[source] vm_memory::GuestMemoryError),
|
|
#[error("Zero Page for bzImage past RAM end")]
|
|
ZeroPagePastRamEnd,
|
|
}
|
|
|
|
/// Type for returning public functions outcome.
|
|
pub type Result<T> = result::Result<T, Error>;
|
|
|
|
/// Type for memory region types.
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
|
|
pub enum RegionType {
|
|
/// RAM type
|
|
Ram,
|
|
|
|
/// 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.
|
|
Reserved,
|
|
}
|
|
|
|
/// Module for aarch64 related functionality.
|
|
#[cfg(target_arch = "aarch64")]
|
|
pub mod aarch64;
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
pub use aarch64::{
|
|
_NSIG, EntryPoint, arch_memory_regions, configure_system, configure_vcpu,
|
|
fdt::DeviceInfoForFdt, get_host_cpu_phys_bits, initramfs_load_addr, layout,
|
|
layout::CMDLINE_MAX_SIZE, layout::IRQ_BASE, uefi,
|
|
};
|
|
|
|
/// Module for riscv64 related functionality.
|
|
#[cfg(target_arch = "riscv64")]
|
|
pub mod riscv64;
|
|
|
|
#[cfg(target_arch = "riscv64")]
|
|
pub use riscv64::{
|
|
_NSIG, EntryPoint, arch_memory_regions, configure_system, configure_vcpu,
|
|
fdt::DeviceInfoForFdt, get_host_cpu_phys_bits, initramfs_load_addr, layout,
|
|
layout::CMDLINE_MAX_SIZE, layout::IRQ_BASE, uefi,
|
|
};
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
pub mod x86_64;
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
pub use x86_64::{
|
|
_NSIG, CpuidConfig, CpuidFeatureEntry, EntryPoint, arch_memory_regions, configure_system,
|
|
configure_vcpu, generate_common_cpuid, generate_ram_ranges, get_host_cpu_phys_bits,
|
|
initramfs_load_addr, layout, layout::CMDLINE_MAX_SIZE, layout::CMDLINE_START, regs,
|
|
};
|
|
|
|
/// Safe wrapper for `sysconf(_SC_PAGESIZE)`.
|
|
#[cfg(target_arch = "x86_64")]
|
|
#[inline(always)]
|
|
fn pagesize() -> usize {
|
|
// SAFETY: Trivially safe
|
|
unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize }
|
|
}
|
|
|
|
#[derive(Clone, Default)]
|
|
pub struct NumaNode {
|
|
pub memory_regions: Vec<Arc<GuestRegionMmap>>,
|
|
pub hotplug_regions: Vec<Arc<GuestRegionMmap>>,
|
|
pub cpus: Vec<u32>,
|
|
pub pci_segments: Vec<u16>,
|
|
pub distances: BTreeMap<u32, u8>,
|
|
pub memory_zones: Vec<String>,
|
|
}
|
|
|
|
pub type NumaNodes = BTreeMap<u32, NumaNode>;
|
|
|
|
/// 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,
|
|
}
|
|
|
|
/// 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(any(target_arch = "aarch64", target_arch = "riscv64"))]
|
|
Serial,
|
|
/// Device Type: RTC.
|
|
#[cfg(target_arch = "aarch64")]
|
|
Rtc,
|
|
/// Device Type: GPIO.
|
|
#[cfg(target_arch = "aarch64")]
|
|
Gpio,
|
|
/// Device Type: fw_cfg.
|
|
#[cfg(feature = "fw_cfg")]
|
|
FwCfg,
|
|
}
|
|
|
|
/// 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(any(target_arch = "aarch64", target_arch = "riscv64"))]
|
|
pub struct MmioDeviceInfo {
|
|
pub addr: u64,
|
|
pub len: u64,
|
|
pub irq: u32,
|
|
}
|
|
|
|
/// Structure to describe PCI space information
|
|
#[derive(Clone, Debug)]
|
|
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
|
|
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,
|
|
}
|
|
|
|
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
|
|
impl DeviceInfoForFdt for MmioDeviceInfo {
|
|
fn addr(&self) -> u64 {
|
|
self.addr
|
|
}
|
|
fn irq(&self) -> u32 {
|
|
self.irq
|
|
}
|
|
fn length(&self) -> u64 {
|
|
self.len
|
|
}
|
|
}
|