From eed35530cf3dfc0f5bfb3ca5fac532908ef8f6ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 10 Apr 2024 16:08:19 +0200 Subject: [PATCH] refactor: make platform matches non-exhaustive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/arch/x86_64/kernel/mod.rs | 22 ++++++++++------------ src/arch/x86_64/kernel/processor.rs | 7 +++---- src/arch/x86_64/kernel/systemtime.rs | 11 ++--------- src/arch/x86_64/mm/physicalmem.rs | 7 ++----- 4 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/arch/x86_64/kernel/mod.rs b/src/arch/x86_64/kernel/mod.rs index 4dd227a350..29993a508d 100644 --- a/src/arch/x86_64/kernel/mod.rs +++ b/src/arch/x86_64/kernel/mod.rs @@ -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}; @@ -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 { 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, } } @@ -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(), } } @@ -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, } } diff --git a/src/arch/x86_64/kernel/processor.rs b/src/arch/x86_64/kernel/processor.rs index 7371787fad..bde2af3131 100644 --- a/src/arch/x86_64/kernel/processor.rs +++ b/src/arch/x86_64/kernel/processor.rs @@ -371,12 +371,11 @@ impl CpuFrequency { fn detect_from_hypervisor(&mut self) -> Result<(), ()> { fn detect_from_uhyve() -> Result { 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 @@ -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")] @@ -1050,7 +1050,6 @@ pub fn shutdown(error_code: i32) -> ! { halt(); } } - PlatformInfo::Uhyve { .. } => unreachable!(), } } diff --git a/src/arch/x86_64/kernel/systemtime.rs b/src/arch/x86_64/kernel/systemtime.rs index 6b1f35122e..5969b44871 100644 --- a/src/arch/x86_64/kernel/systemtime.rs +++ b/src/arch/x86_64/kernel/systemtime.rs @@ -174,21 +174,14 @@ static BOOT_TIME: OnceCell = 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}"); diff --git a/src/arch/x86_64/mm/physicalmem.rs b/src/arch/x86_64/mm/physicalmem.rs index 7100b6677a..9b46475d04 100644 --- a/src/arch/x86_64/mm/physicalmem.rs +++ b/src/arch/x86_64/mm/physicalmem.rs @@ -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");