Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "hypervisor-fw"
version = "0.5.0"
authors = ["The Rust Hypervisor Firmware Authors"]
edition = "2021"
edition = "2024"

# Despite "panic-strategy": "abort" being set in x86_64-unknown-none.json, panic = "abort" is
# needed here to make "cargo check" and "cargo clippy" run without errors.
Expand Down
14 changes: 7 additions & 7 deletions src/arch/aarch64/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use crate::layout::{MemoryAttribute, MemoryDescriptor, MemoryLayout};

use super::paging::*;

extern "Rust" {
static code_start: UnsafeCell<()>;
static code_end: UnsafeCell<()>;
static data_start: UnsafeCell<()>;
static data_end: UnsafeCell<()>;
static stack_start: UnsafeCell<()>;
static stack_end: UnsafeCell<()>;
unsafe extern "Rust" {
unsafe static code_start: UnsafeCell<()>;
unsafe static code_end: UnsafeCell<()>;
unsafe static data_start: UnsafeCell<()>;
unsafe static data_end: UnsafeCell<()>;
unsafe static stack_start: UnsafeCell<()>;
unsafe static stack_end: UnsafeCell<()>;
}

pub mod map {
Expand Down
10 changes: 6 additions & 4 deletions src/arch/aarch64/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,14 @@ impl interface::Mmu for MemoryManagementUnit {
// Prepare the memory attribute indirection register.
self.setup_mair();

let kernel_tables = &mut *KERNEL_TABLES.get();
let kernel_tables = unsafe { &mut *KERNEL_TABLES.get() };

// Populate translation tables.
kernel_tables
.populate_tt_entries()
.map_err(MmuEnableError::Other)?;
unsafe {
kernel_tables
.populate_tt_entries()
.map_err(MmuEnableError::Other)?;
}

// Set the "Translation Table Base Register".
TTBR0_EL1.set_baddr(kernel_tables.phys_base_address());
Expand Down
14 changes: 7 additions & 7 deletions src/arch/riscv64/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use core::{cell::UnsafeCell, ops::Range};

use crate::layout::{MemoryAttribute, MemoryDescriptor, MemoryLayout};

extern "Rust" {
static code_start: UnsafeCell<()>;
static code_end: UnsafeCell<()>;
static data_start: UnsafeCell<()>;
static data_end: UnsafeCell<()>;
static stack_start: UnsafeCell<()>;
static stack_end: UnsafeCell<()>;
unsafe extern "Rust" {
unsafe static code_start: UnsafeCell<()>;
unsafe static code_end: UnsafeCell<()>;
unsafe static data_start: UnsafeCell<()>;
unsafe static data_end: UnsafeCell<()>;
unsafe static stack_start: UnsafeCell<()>;
unsafe static stack_end: UnsafeCell<()>;
}

pub fn code_range() -> Range<usize> {
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86_64/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ impl Pointer {
}

// Our 64-bit GDT lives in RAM, so it can be accessed like any other global.
#[no_mangle]
#[unsafe(no_mangle)]
static GDT64_PTR: Pointer = Pointer::new(&GDT64);
static GDT64: [Descriptor; 3] = [Descriptor::empty(), Descriptor::CODE64, Descriptor::DATA64];
16 changes: 8 additions & 8 deletions src/arch/x86_64/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use core::{cell::UnsafeCell, ops::Range};

use crate::layout::{MemoryAttribute, MemoryDescriptor, MemoryLayout};

extern "Rust" {
static ram_min: UnsafeCell<()>;
static code_start: UnsafeCell<()>;
static code_end: UnsafeCell<()>;
static data_start: UnsafeCell<()>;
static data_end: UnsafeCell<()>;
static stack_start: UnsafeCell<()>;
static stack_end: UnsafeCell<()>;
unsafe extern "Rust" {
unsafe static ram_min: UnsafeCell<()>;
unsafe static code_start: UnsafeCell<()>;
unsafe static code_end: UnsafeCell<()>;
unsafe static data_start: UnsafeCell<()>;
unsafe static data_end: UnsafeCell<()>;
unsafe static stack_start: UnsafeCell<()>;
unsafe static stack_end: UnsafeCell<()>;
}

pub fn header_range() -> Range<usize> {
Expand Down
8 changes: 4 additions & 4 deletions src/arch/x86_64/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
use core::cell::SyncUnsafeCell;
use log::info;
use x86_64::{
PhysAddr,
registers::control::Cr3,
structures::paging::{PageSize, PageTable, PageTableFlags, PhysFrame, Size2MiB},
PhysAddr,
};

// Amount of memory we identity map in setup(), max 512 GiB.
const ADDRESS_SPACE_GIB: usize = 4;
const TABLE: PageTable = PageTable::new();

// Put the Page Tables in static muts to make linking easier
#[no_mangle]
#[unsafe(no_mangle)]
static L4_TABLE: SyncUnsafeCell<PageTable> = SyncUnsafeCell::new(PageTable::new());
#[no_mangle]
#[unsafe(no_mangle)]
static L3_TABLE: SyncUnsafeCell<PageTable> = SyncUnsafeCell::new(PageTable::new());
#[no_mangle]
#[unsafe(no_mangle)]
static L2_TABLES: SyncUnsafeCell<[PageTable; ADDRESS_SPACE_GIB]> =
SyncUnsafeCell::new([TABLE; ADDRESS_SPACE_GIB]);

Expand Down
12 changes: 4 additions & 8 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@

#[macro_export]
macro_rules! container_of {
($ptr:ident, $container:ty, $field:ident) => {{
(($ptr as usize) - core::mem::offset_of!($container, $field)) as *const $container
}};
($ptr:ident, $container:ty, $field:ident) => {{ (($ptr as usize) - core::mem::offset_of!($container, $field)) as *const $container }};
}

#[macro_export]
macro_rules! container_of_mut {
($ptr:ident, $container:ty, $field:ident) => {{
(($ptr as usize) - core::mem::offset_of!($container, $field)) as *mut $container
}};
($ptr:ident, $container:ty, $field:ident) => {{ (($ptr as usize) - core::mem::offset_of!($container, $field)) as *mut $container }};
}

// SAFETY: Requires that addr point to a static, null-terminated C-string.
Expand All @@ -24,10 +20,10 @@ pub unsafe fn from_cstring(addr: u64) -> &'static [u8] {
}
let start = addr as *const u8;
let mut size: usize = 0;
while start.add(size).read() != 0 {
while unsafe { start.add(size).read() } != 0 {
size += 1;
}
core::slice::from_raw_parts(start, size)
unsafe { core::slice::from_raw_parts(start, size) }
}

pub fn ascii_strip(s: &[u8]) -> &str {
Expand Down
8 changes: 4 additions & 4 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::arch::x86_64::_rdtsc;
#[inline]
unsafe fn rdtsc() -> u64 {
let value: u64;
asm!("mrs {}, cntvct_el0", out(reg) value);
unsafe { asm!("mrs {}, cntvct_el0", out(reg) value) };
value
}

Expand All @@ -26,19 +26,19 @@ unsafe fn rdtsc() -> u64 {
#[cfg(target_arch = "x86_64")]
#[inline]
unsafe fn rdtsc() -> u64 {
_rdtsc()
unsafe { _rdtsc() }
}

#[cfg(target_arch = "aarch64")]
#[inline]
unsafe fn pause() {
asm!("yield");
unsafe { asm!("yield") }
}

#[cfg(target_arch = "x86_64")]
#[inline]
unsafe fn pause() {
asm!("pause");
unsafe { asm!("pause") }
}

pub fn ndelay(ns: u64) {
Expand Down
2 changes: 1 addition & 1 deletion src/efi/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use r_efi::{

use crate::{
block::{SectorBuf, VirtioBlockDevice},
part::{get_partitions, PartitionEntry},
part::{PartitionEntry, get_partitions},
};

#[allow(dead_code)]
Expand Down
4 changes: 2 additions & 2 deletions src/efi/boot_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use r_efi::{eficall, eficall_abi};
use crate::fat;

use super::{
block, device_path::DevicePath, file, mem_file, new_image_handle, HandleType, HandleWrapper,
LoadedImageWrapper, ALLOCATOR, BLOCK_WRAPPERS, ST,
ALLOCATOR, BLOCK_WRAPPERS, HandleType, HandleWrapper, LoadedImageWrapper, ST, block,
device_path::DevicePath, file, mem_file, new_image_handle,
};

pub static mut BS: SyncUnsafeCell<efi::BootServices> = SyncUnsafeCell::new(efi::BootServices {
Expand Down
70 changes: 36 additions & 34 deletions src/efi/runtime_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,42 @@ pub static mut RS: SyncUnsafeCell<efi::RuntimeServices> =

#[allow(clippy::missing_transmute_annotations)]
unsafe fn fixup_at_virtual(descriptors: &[MemoryDescriptor]) {
#[allow(static_mut_refs)]
let st = ST.get_mut();
#[allow(static_mut_refs)]
let rs = RS.get_mut();

let ptr = ALLOCATOR
.borrow()
.convert_internal_pointer(descriptors, (not_available as *const ()) as u64)
.unwrap();
rs.get_time = transmute(ptr);
rs.set_time = transmute(ptr);
rs.get_wakeup_time = transmute(ptr);
rs.set_wakeup_time = transmute(ptr);
rs.get_variable = transmute(ptr);
rs.set_variable = transmute(ptr);
rs.get_next_variable_name = transmute(ptr);
rs.reset_system = transmute(ptr);
rs.update_capsule = transmute(ptr);
rs.query_capsule_capabilities = transmute(ptr);
rs.query_variable_info = transmute(ptr);

let ct = st.configuration_table;
let ptr = ALLOCATOR
.borrow()
.convert_internal_pointer(descriptors, (ct as *const _) as u64)
.unwrap();
st.configuration_table = ptr as *mut ConfigurationTable;

let rs = st.runtime_services;
let ptr = ALLOCATOR
.borrow()
.convert_internal_pointer(descriptors, (rs as *const _) as u64)
.unwrap();
st.runtime_services = ptr as *mut RuntimeServices;
unsafe {
#[allow(static_mut_refs)]
let st = ST.get_mut();
#[allow(static_mut_refs)]
let rs = RS.get_mut();

let ptr = ALLOCATOR
.borrow()
.convert_internal_pointer(descriptors, (not_available as *const ()) as u64)
.unwrap();
rs.get_time = transmute(ptr);
rs.set_time = transmute(ptr);
rs.get_wakeup_time = transmute(ptr);
rs.set_wakeup_time = transmute(ptr);
rs.get_variable = transmute(ptr);
rs.set_variable = transmute(ptr);
rs.get_next_variable_name = transmute(ptr);
rs.reset_system = transmute(ptr);
rs.update_capsule = transmute(ptr);
rs.query_capsule_capabilities = transmute(ptr);
rs.query_variable_info = transmute(ptr);

let ct = st.configuration_table;
let ptr = ALLOCATOR
.borrow()
.convert_internal_pointer(descriptors, (ct as *const _) as u64)
.unwrap();
st.configuration_table = ptr as *mut ConfigurationTable;

let rs = st.runtime_services;
let ptr = ALLOCATOR
.borrow()
.convert_internal_pointer(descriptors, (rs as *const _) as u64)
.unwrap();
st.runtime_services = ptr as *mut RuntimeServices;
}
}

pub extern "efiapi" fn not_available() -> Status {
Expand Down
2 changes: 1 addition & 1 deletion src/fat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ impl<'a> Filesystem<'a> {
break;
}
FileType::File => {
return Ok(self.get_file(de.cluster, de.size).unwrap().into())
return Ok(self.get_file(de.cluster, de.size).unwrap().into());
}
}
}
Expand Down
68 changes: 38 additions & 30 deletions src/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,40 +257,48 @@ mod tests {
}

fn prepare_tap(net: &GuestNetworkConfig) {
assert!(std::process::Command::new("bash")
.args([
"-c",
&format!("sudo ip tuntap add name {} mode tap", net.tap_name),
])
.status()
.expect("Expected creating interface to work")
.success());
assert!(
std::process::Command::new("bash")
.args([
"-c",
&format!("sudo ip tuntap add name {} mode tap", net.tap_name),
])
.status()
.expect("Expected creating interface to work")
.success()
);

assert!(std::process::Command::new("bash")
.args([
"-c",
&format!("sudo ip addr add {}/24 dev {}", net.host_ip, net.tap_name),
])
.status()
.expect("Expected programming interface to work")
.success());

assert!(std::process::Command::new("bash")
.args(["-c", &format!("sudo ip link set dev {} up", net.tap_name)])
.status()
.expect("Expected upping interface to work")
.success());
assert!(
std::process::Command::new("bash")
.args([
"-c",
&format!("sudo ip addr add {}/24 dev {}", net.host_ip, net.tap_name),
])
.status()
.expect("Expected programming interface to work")
.success()
);

assert!(
std::process::Command::new("bash")
.args(["-c", &format!("sudo ip link set dev {} up", net.tap_name)])
.status()
.expect("Expected upping interface to work")
.success()
);
}

fn cleanup_tap(net: &GuestNetworkConfig) {
assert!(std::process::Command::new("bash")
.args([
"-c",
&format!("sudo ip tuntap de name {} mode tap", net.tap_name),
])
.status()
.expect("Expected deleting interface to work")
.success());
assert!(
std::process::Command::new("bash")
.args([
"-c",
&format!("sudo ip tuntap de name {} mode tap", net.tap_name),
])
.status()
.expect("Expected deleting interface to work")
.success()
);
}

fn handle_child_output(
Expand Down
5 changes: 4 additions & 1 deletion src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ mod tests {
assert_eq!(s, "/EFI/org.clearlinux/kernel-org.clearlinux.kvm.5.0.6-318");
let s = super::ascii_strip(&entry.cmdline);
let s = s.trim_matches(char::from(0));
assert_eq!(s, "root=PARTUUID=ae06d187-e9fc-4d3b-9e5b-8e6ff28e894f console=tty0 console=ttyS0,115200n8 console=hvc0 quiet init=/usr/lib/systemd/systemd-bootchart initcall_debug tsc=reliable no_timer_check noreplace-smp cryptomgr.notests rootfstype=ext4,btrfs,xfs kvm-intel.nested=1 rw");
assert_eq!(
s,
"root=PARTUUID=ae06d187-e9fc-4d3b-9e5b-8e6ff28e894f console=tty0 console=ttyS0,115200n8 console=hvc0 quiet init=/usr/lib/systemd/systemd-bootchart initcall_debug tsc=reliable no_timer_check noreplace-smp cryptomgr.notests rootfstype=ext4,btrfs,xfs kvm-intel.nested=1 rw"
);
}

macro_rules! entry_pattern_matches {
Expand Down
Loading
Loading