Skip to content

Commit

Permalink
Merge pull request #1133 from mkroening/non-exhaustive-platforms
Browse files Browse the repository at this point in the history
refactor: make platform matches non-exhaustive
  • Loading branch information
mkroening authored Apr 11, 2024
2 parents 0a7304d + eed3553 commit 0f488c2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 30 deletions.
22 changes: 10 additions & 12 deletions src/arch/x86_64/kernel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "common-os")]
use core::arch::asm;
use core::num::NonZeroU64;
#[cfg(feature = "newlib")]
use core::slice;
use core::sync::atomic::{AtomicU32, AtomicU64, Ordering};
Expand Down Expand Up @@ -71,14 +72,13 @@ pub fn get_limit() -> usize {
boot_info().hardware_info.phys_addr_range.end as usize
}

pub fn get_mbinfo() -> VirtAddr {
pub fn get_mbinfo() -> Option<NonZeroU64> {
match boot_info().platform_info {
PlatformInfo::Multiboot {
multiboot_info_addr,
..
} => VirtAddr(multiboot_info_addr.get()),
PlatformInfo::LinuxBootParams { .. } => VirtAddr(0),
PlatformInfo::Uhyve { .. } => VirtAddr(0),
} => Some(multiboot_info_addr),
_ => None,
}
}

Expand All @@ -94,13 +94,12 @@ pub fn get_possible_cpus() -> u32 {
use core::cmp;

match boot_info().platform_info {
PlatformInfo::LinuxBootParams { .. } => apic::local_apic_id_count(),
PlatformInfo::Multiboot { .. } => apic::local_apic_id_count(),
// FIXME: Remove get_processor_count after a transition period for uhyve 0.1.3 adoption
PlatformInfo::Uhyve { num_cpus, .. } => cmp::max(
u32::try_from(num_cpus.get()).unwrap(),
get_processor_count(),
),
_ => apic::local_apic_id_count(),
}
}

Expand All @@ -115,18 +114,17 @@ pub fn get_processor_count() -> u32 {
}

pub fn is_uhyve_with_pci() -> bool {
match boot_info().platform_info {
PlatformInfo::Multiboot { .. } => false,
PlatformInfo::LinuxBootParams { .. } => false,
PlatformInfo::Uhyve { has_pci, .. } => has_pci,
}
matches!(
boot_info().platform_info,
PlatformInfo::Uhyve { has_pci: true, .. }
)
}

pub fn args() -> Option<&'static str> {
match boot_info().platform_info {
PlatformInfo::Multiboot { command_line, .. } => command_line,
PlatformInfo::LinuxBootParams { command_line, .. } => command_line,
PlatformInfo::Uhyve { .. } => None,
_ => None,
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/arch/x86_64/kernel/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,11 @@ impl CpuFrequency {
fn detect_from_hypervisor(&mut self) -> Result<(), ()> {
fn detect_from_uhyve() -> Result<u16, ()> {
match boot_info().platform_info {
PlatformInfo::Multiboot { .. } => Err(()),
PlatformInfo::LinuxBootParams { .. } => Err(()),
PlatformInfo::Uhyve { cpu_freq, .. } => Ok(u16::try_from(
cpu_freq.map(NonZeroU32::get).unwrap_or_default() / 1000,
)
.unwrap()),
_ => Err(()),
}
}
// future implementations could add support for different hypervisors
Expand Down Expand Up @@ -1038,7 +1037,8 @@ fn qemu_exit(success: bool) {
pub fn shutdown(error_code: i32) -> ! {
match boot_info().platform_info {
PlatformInfo::LinuxBootParams { .. } => triple_fault(),
PlatformInfo::Multiboot { .. } => {
PlatformInfo::Uhyve { .. } => unreachable!(),
_ => {
qemu_exit(error_code == 0);

#[cfg(feature = "acpi")]
Expand All @@ -1050,7 +1050,6 @@ pub fn shutdown(error_code: i32) -> ! {
halt();
}
}
PlatformInfo::Uhyve { .. } => unreachable!(),
}
}

Expand Down
11 changes: 2 additions & 9 deletions src/arch/x86_64/kernel/systemtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,14 @@ static BOOT_TIME: OnceCell<u64> = OnceCell::new();

pub fn init() {
let boot_time = match boot_info().platform_info {
PlatformInfo::Multiboot { .. } => {
// Get the current time in microseconds since the epoch (1970-01-01) from the x86 RTC.
// Subtract the timer ticks to get the actual time when Hermit was booted.
let current_time = without_interrupts(|| Rtc::new().get_microseconds_since_epoch());
let boot_time = current_time - processor::get_timer_ticks();
OffsetDateTime::from_unix_timestamp_nanos(boot_time as i128 * 1000).unwrap()
}
PlatformInfo::LinuxBootParams { .. } => {
PlatformInfo::Uhyve { boot_time, .. } => boot_time,
_ => {
// Get the current time in microseconds since the epoch (1970-01-01) from the x86 RTC.
// Subtract the timer ticks to get the actual time when Hermit was booted.
let current_time = without_interrupts(|| Rtc::new().get_microseconds_since_epoch());
let boot_time = current_time - processor::get_timer_ticks();
OffsetDateTime::from_unix_timestamp_nanos(boot_time as i128 * 1000).unwrap()
}
PlatformInfo::Uhyve { boot_time, .. } => boot_time,
};
info!("Hermit booted on {boot_time}");

Expand Down
7 changes: 2 additions & 5 deletions src/arch/x86_64/mm/physicalmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ fn detect_from_fdt() -> Result<(), ()> {
}

fn detect_from_multiboot_info() -> Result<(), ()> {
let mb_info = get_mbinfo();
if mb_info.is_zero() {
return Err(());
}
let mb_info = get_mbinfo().ok_or(())?.get();

let mut mem = MultibootMemory;
let mb = unsafe { Multiboot::from_ptr(mb_info.as_u64(), &mut mem).unwrap() };
let mb = unsafe { Multiboot::from_ptr(mb_info, &mut mem).unwrap() };
let all_regions = mb
.memory_regions()
.expect("Could not find a memory map in the Multiboot information");
Expand Down

0 comments on commit 0f488c2

Please sign in to comment.