diff --git a/src/arch/aarch64/device/mod.rs b/src/arch/aarch64/device/mod.rs index 03b4d26250..cb99150ca4 100644 --- a/src/arch/aarch64/device/mod.rs +++ b/src/arch/aarch64/device/mod.rs @@ -51,5 +51,10 @@ pub unsafe fn init_devicetree(fdt: &Fdt) { } } -#[derive(Default)] pub struct ArchPercpuMisc; + +impl ArchPercpuMisc { + pub const fn default() -> Self { + Self + } +} diff --git a/src/arch/riscv64/device/mod.rs b/src/arch/riscv64/device/mod.rs index bceacae144..f072f943ae 100644 --- a/src/arch/riscv64/device/mod.rs +++ b/src/arch/riscv64/device/mod.rs @@ -101,5 +101,10 @@ pub unsafe fn init_noncore() { } } -#[derive(Default)] pub struct ArchPercpuMisc; + +impl ArchPercpuMisc { + pub const fn default() -> Self { + Self + } +} diff --git a/src/arch/x86_shared/device/mod.rs b/src/arch/x86_shared/device/mod.rs index 3bf824b0ce..0f3446c3d8 100644 --- a/src/arch/x86_shared/device/mod.rs +++ b/src/arch/x86_shared/device/mod.rs @@ -81,9 +81,18 @@ pub unsafe fn init_ap() { } } -#[derive(Default)] pub struct ArchPercpuMisc { pub apic_id_opt: Cell>, #[cfg(feature = "x86_kvm_pv")] pub tsc_info: tsc::TscPercpu, } + +impl ArchPercpuMisc { + pub const fn default() -> Self { + Self { + apic_id_opt: Cell::new(None), + #[cfg(feature = "x86_kvm_pv")] + tsc_info: tsc::TscPercpu::default(), + } + } +} diff --git a/src/arch/x86_shared/device/tsc.rs b/src/arch/x86_shared/device/tsc.rs index efe8a5b7eb..445d6e0811 100644 --- a/src/arch/x86_shared/device/tsc.rs +++ b/src/arch/x86_shared/device/tsc.rs @@ -46,8 +46,8 @@ pub struct TscPercpu { vcpu_page: Cell<*const PvclockVcpuTimeInfo>, prev: Cell, } -impl Default for TscPercpu { - fn default() -> Self { +impl TscPercpu { + pub const fn default() -> Self { Self { vcpu_page: Cell::new(core::ptr::null()), prev: Cell::new(0), diff --git a/src/context/switch.rs b/src/context/switch.rs index 033b7cb212..26533a199d 100644 --- a/src/context/switch.rs +++ b/src/context/switch.rs @@ -315,7 +315,6 @@ pub fn switch() -> SwitchResult { /// /// This struct contains information such as the idle context, current context, and PIT tick counts, /// as well as fields required for managing ptrace sessions and signals. -#[derive(Default)] pub struct ContextSwitchPercpu { switch_result: Cell>, pit_ticks: Cell, @@ -329,6 +328,16 @@ pub struct ContextSwitchPercpu { } impl ContextSwitchPercpu { + pub const fn default() -> Self { + Self { + switch_result: Cell::new(None), + pit_ticks: Cell::new(0), + current_ctxt: RefCell::new(None), + idle_ctxt: RefCell::new(None), + being_sigkilled: Cell::new(false), + } + } + /// Applies a function to the current context, allowing controlled access. /// /// # Parameters diff --git a/src/cpu_stats.rs b/src/cpu_stats.rs index eb1ff54d7e..0407df6351 100644 --- a/src/cpu_stats.rs +++ b/src/cpu_stats.rs @@ -44,6 +44,19 @@ pub struct CpuStats { state: AtomicU8, } +impl CpuStats { + pub const fn default() -> Self { + Self { + user: AtomicUsize::new(0), + nice: AtomicUsize::new(0), + kernel: AtomicUsize::new(0), + idle: AtomicUsize::new(0), + irq: AtomicUsize::new(0), + state: AtomicU8::new(0), + } + } +} + pub struct CpuStatsData { /// Number of ticks spent on userspace contexts pub user: usize, diff --git a/src/percpu.rs b/src/percpu.rs index a7c834867f..dbcde65831 100644 --- a/src/percpu.rs +++ b/src/percpu.rs @@ -8,6 +8,7 @@ use rmm::Arch; use syscall::PtraceFlags; use crate::{ + arch::device::ArchPercpuMisc, context::{empty_cr3, memory::AddrSpaceWrapper, switch::ContextSwitchPercpu}, cpu_set::{LogicalCpuId, MAX_CPU_COUNT}, cpu_stats::CpuStats, @@ -167,14 +168,14 @@ pub unsafe fn switch_arch_hook() { } } impl PercpuBlock { - pub fn init(cpu_id: LogicalCpuId) -> Self { + pub const fn init(cpu_id: LogicalCpuId) -> Self { Self { cpu_id, - switch_internals: Default::default(), + switch_internals: ContextSwitchPercpu::default(), current_addrsp: RefCell::new(None), new_addrsp_tmp: Cell::new(None), wants_tlb_shootdown: AtomicBool::new(false), - ptrace_flags: Cell::new(Default::default()), + ptrace_flags: Cell::new(PtraceFlags::empty()), ptrace_session: RefCell::new(None), inside_syscall: Cell::new(false), @@ -183,7 +184,7 @@ impl PercpuBlock { #[cfg(feature = "profiling")] profiling: None, - misc_arch_info: Default::default(), + misc_arch_info: ArchPercpuMisc::default(), stats: CpuStats::default(), } diff --git a/src/syscall/debug.rs b/src/syscall/debug.rs index f1728068e4..5b5538be73 100644 --- a/src/syscall/debug.rs +++ b/src/syscall/debug.rs @@ -196,12 +196,21 @@ pub struct SyscallDebugInfo { accumulated_time: u128, do_debug: bool, } -#[cfg(feature = "syscall_debug")] impl SyscallDebugInfo { + pub const fn default() -> Self { + Self { + this_switch_time: 0, + accumulated_time: 0, + do_debug: false, + } + } + + #[cfg(feature = "syscall_debug")] pub fn on_switch_from(&mut self) { let now = crate::time::monotonic(); self.accumulated_time += now - core::mem::replace(&mut self.this_switch_time, now); } + #[cfg(feature = "syscall_debug")] pub fn on_switch_to(&mut self) { self.this_switch_time = crate::time::monotonic(); }