From 62f220b1b95d262d0ae950fc5cd20756419c470e Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 4 Apr 2026 13:49:07 +0200 Subject: [PATCH] Use cfg! rather than #[cfg] for ACPI enabling --- src/acpi/mod.rs | 2 -- src/arch/x86_shared/device/ioapic.rs | 12 +++--------- src/arch/x86_shared/device/mod.rs | 11 ++++------- src/arch/x86_shared/start.rs | 8 ++------ src/arch/x86_shared/stop.rs | 12 +++++++----- src/arch/x86_shared/time.rs | 10 ++++------ src/main.rs | 1 - src/scheme/irq.rs | 10 ++++++---- src/scheme/mod.rs | 16 ++++++---------- src/startup/mod.rs | 1 - 10 files changed, 32 insertions(+), 51 deletions(-) diff --git a/src/acpi/mod.rs b/src/acpi/mod.rs index aa0b2ebf58..59bba21915 100644 --- a/src/acpi/mod.rs +++ b/src/acpi/mod.rs @@ -205,10 +205,8 @@ pub fn get_sdt_signature(sdt: &'static Sdt) -> SdtSignature { pub struct Acpi { pub hpet: RwLock>, - pub next_ctx: RwLock, } pub static ACPI_TABLE: Acpi = Acpi { hpet: RwLock::new(None), - next_ctx: RwLock::new(0), }; diff --git a/src/arch/x86_shared/device/ioapic.rs b/src/arch/x86_shared/device/ioapic.rs index 2f8c9bf260..fb66d3bf2b 100644 --- a/src/arch/x86_shared/device/ioapic.rs +++ b/src/arch/x86_shared/device/ioapic.rs @@ -3,16 +3,13 @@ use core::{cell::SyncUnsafeCell, fmt, ptr}; use alloc::vec::Vec; use spin::Mutex; -#[cfg(feature = "acpi")] +use super::{local_apic::ApicId, pic}; use crate::{ acpi::madt::{self, Madt, MadtEntry, MadtIntSrcOverride, MadtIoApic}, + arch::{cpuid::cpuid, interrupt::irq}, memory::{map_device_memory, PhysicalAddress}, }; -use crate::arch::{cpuid::cpuid, interrupt::irq}; - -use super::{local_apic::ApicId, pic}; - pub struct IoApicRegs { pointer: *const u32, } @@ -232,7 +229,6 @@ pub fn src_overrides() -> &'static [Override] { .map_or(&[], |vector| &vector[..]) } -#[cfg(feature = "acpi")] pub unsafe fn handle_ioapic(madt_ioapic: &'static MadtIoApic) { unsafe { // map the I/O APIC registers @@ -250,7 +246,6 @@ pub unsafe fn handle_ioapic(madt_ioapic: &'static MadtIoApic) { (*IOAPICS.get()).get_or_insert_with(Vec::new).push(ioapic); } } -#[cfg(feature = "acpi")] pub unsafe fn handle_src_override(src_override: &'static MadtIntSrcOverride) { unsafe { let flags = src_override.flags; @@ -295,8 +290,7 @@ pub unsafe fn init() { )); // TODO: remove unwraps // search the madt for all IOAPICs. - #[cfg(feature = "acpi")] - { + if cfg!(feature = "acpi") { let madt: &'static Madt = match madt::madt() { Some(m) => m, // TODO: Parse MP tables too. diff --git a/src/arch/x86_shared/device/mod.rs b/src/arch/x86_shared/device/mod.rs index 94112e6f50..6f41770601 100644 --- a/src/arch/x86_shared/device/mod.rs +++ b/src/arch/x86_shared/device/mod.rs @@ -1,7 +1,6 @@ use core::cell::Cell; pub mod cpu; -#[cfg(feature = "acpi")] pub mod hpet; pub mod ioapic; pub mod local_apic; @@ -28,8 +27,11 @@ pub unsafe fn init_after_acpi() { //ioapic::init(mapper); } -#[cfg(feature = "acpi")] unsafe fn init_hpet() -> bool { + if cfg!(not(feature = "acpi")) { + return false; + } + unsafe { use crate::acpi::ACPI_TABLE; match *ACPI_TABLE.hpet.write() { @@ -46,11 +48,6 @@ unsafe fn init_hpet() -> bool { } } -#[cfg(not(feature = "acpi"))] -unsafe fn init_hpet() -> bool { - false -} - pub unsafe fn init_noncore() { unsafe { debug!("Initializing system timer"); diff --git a/src/arch/x86_shared/start.rs b/src/arch/x86_shared/start.rs index 7e99426ca4..c4972fbd07 100644 --- a/src/arch/x86_shared/start.rs +++ b/src/arch/x86_shared/start.rs @@ -4,9 +4,6 @@ //! defined in other files inside of the `arch` module use core::{arch::naked_asm, cell::SyncUnsafeCell, mem::offset_of}; -#[cfg(feature = "acpi")] -use crate::acpi; - use crate::{ allocator, cpu_set::LogicalCpuId, device, devices::graphical_debug, gdt, idt, interrupt, paging, startup::KernelArgs, @@ -131,9 +128,8 @@ unsafe extern "C" fn start(args_ptr: *const KernelArgs, stack_end: usize) -> ! { device::init(); // Read ACPI tables, starts APs - #[cfg(feature = "acpi")] - { - acpi::init(args.acpi_rsdp()); + if cfg!(feature = "acpi") { + crate::acpi::init(args.acpi_rsdp()); device::init_after_acpi(); } diff --git a/src/arch/x86_shared/stop.rs b/src/arch/x86_shared/stop.rs index df689d7302..498345bbc2 100644 --- a/src/arch/x86_shared/stop.rs +++ b/src/arch/x86_shared/stop.rs @@ -1,9 +1,9 @@ -#[cfg(feature = "acpi")] -use crate::{context, scheme::acpi, time}; - use crate::{ + context, + scheme::acpi, sync::CleanLockToken, syscall::io::{Io, Pio}, + time, }; pub unsafe fn kreset() -> ! { @@ -58,8 +58,11 @@ pub unsafe fn emergency_reset() -> ! { } } -#[cfg(feature = "acpi")] fn userspace_acpi_shutdown(token: &mut CleanLockToken) { + if cfg!(not(feature = "acpi")) { + return; + } + info!("Notifying any potential ACPI driver"); // Tell whatever driver that handles ACPI, that it should enter the S5 state (i.e. // shutdown). @@ -93,7 +96,6 @@ pub unsafe fn kstop(token: &mut CleanLockToken) -> ! { unsafe { info!("Running kstop()"); - #[cfg(feature = "acpi")] userspace_acpi_shutdown(token); // Magic shutdown code for bochs and qemu (older versions). diff --git a/src/arch/x86_shared/time.rs b/src/arch/x86_shared/time.rs index 0fef35e00d..79c3f13c74 100644 --- a/src/arch/x86_shared/time.rs +++ b/src/arch/x86_shared/time.rs @@ -1,9 +1,6 @@ +use super::device::{hpet, pit}; use crate::sync::CleanLockToken; -#[cfg(feature = "acpi")] -use super::device::hpet; -use super::device::pit; - pub fn monotonic_absolute(token: &mut CleanLockToken) -> u128 { // The paravirtualized TSC is already guaranteed to be monotonic, and thus doesn't need to be // readjusted. @@ -16,8 +13,9 @@ pub fn monotonic_absolute(token: &mut CleanLockToken) -> u128 { offset + hpet_or_pit() } fn hpet_or_pit() -> u128 { - #[cfg(feature = "acpi")] - if let Some(ref hpet) = *crate::acpi::ACPI_TABLE.hpet.read() { + if cfg!(feature = "acpi") + && let Some(ref hpet) = *crate::acpi::ACPI_TABLE.hpet.read() + { //TODO: handle rollover? //TODO: improve performance diff --git a/src/main.rs b/src/main.rs index 488d3a50dc..1cae28067a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,6 @@ const PHYS_OFFSET: usize = ::PHYS_OFFSET; mod allocator; /// ACPI table parsing -#[cfg(feature = "acpi")] #[allow(dead_code)] // TODO mod acpi; diff --git a/src/scheme/irq.rs b/src/scheme/irq.rs index c556840e4a..a295ba7f85 100644 --- a/src/scheme/irq.rs +++ b/src/scheme/irq.rs @@ -102,8 +102,10 @@ pub struct IrqScheme; impl IrqScheme { pub fn init() { - #[cfg(all(feature = "acpi", any(target_arch = "x86", target_arch = "x86_64")))] - let cpus = { + let cpus = if cfg!(all( + feature = "acpi", + any(target_arch = "x86", target_arch = "x86_64") + )) { use crate::acpi::madt::*; match madt() { @@ -119,9 +121,9 @@ impl IrqScheme { vec![0] } } + } else { + vec![0] }; - #[cfg(not(all(feature = "acpi", any(target_arch = "x86", target_arch = "x86_64"))))] - let cpus = vec![0]; CPUS.call_once(|| cpus); } diff --git a/src/scheme/mod.rs b/src/scheme/mod.rs index 895b016889..541e7ec60d 100644 --- a/src/scheme/mod.rs +++ b/src/scheme/mod.rs @@ -35,7 +35,6 @@ use crate::{ syscall::usercopy::{UserSliceRo, UserSliceRw, UserSliceWo}, }; -#[cfg(feature = "acpi")] use self::acpi::AcpiScheme; #[cfg(dtb)] use self::dtb::DtbScheme; @@ -54,7 +53,6 @@ use self::{ }; /// When compiled with the "acpi" feature - `acpi:` - allows drivers to read a limited set of ACPI tables. -#[cfg(feature = "acpi")] pub mod acpi; #[cfg(dtb)] pub mod dtb; @@ -156,8 +154,9 @@ fn init_schemes() -> RwLock> { use GlobalSchemes::*; insert_globals(&[Debug, Event, Memory, Pipe, Serio, Irq, Time, Sys, Proc]); - #[cfg(feature = "acpi")] - insert_globals(&[Acpi]); + if cfg!(feature = "acpi") { + insert_globals(&[Acpi]); + } #[cfg(dtb)] insert_globals(&[Dtb]); @@ -418,7 +417,6 @@ pub const ALL_KERNEL_SCHEMES: &[GlobalSchemes] = &[ GlobalSchemes::Time, GlobalSchemes::Sys, GlobalSchemes::Proc, - #[cfg(feature = "acpi")] GlobalSchemes::Acpi, #[cfg(dtb)] GlobalSchemes::Dtb, @@ -446,12 +444,11 @@ impl SchemeExt for GlobalSchemes { Self::Time => &TimeScheme, Self::Sys => &SysScheme, Self::Proc => &ProcScheme, - #[cfg(feature = "acpi")] Self::Acpi => &AcpiScheme, #[cfg(dtb)] Self::Dtb => &DtbScheme, - #[cfg(not(all(feature = "acpi", dtb)))] - _ => panic!("Unknown global scheme"), + #[cfg(not(dtb))] + Self::Dtb => panic!("Unknown global scheme"), } } fn scheme_id(self) -> SchemeId { @@ -461,8 +458,7 @@ impl SchemeExt for GlobalSchemes { #[cold] pub fn init_globals() { - #[cfg(feature = "acpi")] - { + if cfg!(feature = "acpi") { AcpiScheme::init(); } #[cfg(dtb)] diff --git a/src/startup/mod.rs b/src/startup/mod.rs index 6b68f8105b..ae2a979833 100644 --- a/src/startup/mod.rs +++ b/src/startup/mod.rs @@ -84,7 +84,6 @@ impl KernelArgs { } } - #[cfg(feature = "acpi")] pub(crate) fn acpi_rsdp(&self) -> Option<*const u8> { if self.hwdesc_base != 0 { let data = unsafe {