From 4d4f8d38be6324a8e745a215478b133d8d2fef0d Mon Sep 17 00:00:00 2001 From: RarDog Date: Thu, 3 Sep 2026 12:10:44 +0300 Subject: [PATCH] fix: accurate total system RAM calculation from all physical memory regions --- kernel/src/arch/syscall.rs | 2 +- kernel/src/mm/pfa.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/src/arch/syscall.rs b/kernel/src/arch/syscall.rs index 157ac31..7d671b9 100644 --- a/kernel/src/arch/syscall.rs +++ b/kernel/src/arch/syscall.rs @@ -323,7 +323,7 @@ pub extern "C" fn kernel_syscall_dispatcher(regs: &mut SyscallRegisters) { let active = sched.threads.iter().filter(|t| t.state != crate::sched::thread::ThreadState::Unused).count(); (*out_ptr).uptime_ticks = sched.ticks; - (*out_ptr).total_memory_bytes = pfa.usable_ram_bytes; + (*out_ptr).total_memory_bytes = pfa.total_ram_bytes; (*out_ptr).free_memory_bytes = (pfa.free_frames * PAGE_SIZE) as u64; (*out_ptr).active_threads = active as u32; (*out_ptr).cpu_cores = CORES_ONLINE.load(Ordering::Relaxed) as u32; diff --git a/kernel/src/mm/pfa.rs b/kernel/src/mm/pfa.rs index 1086d6c..ff289bd 100644 --- a/kernel/src/mm/pfa.rs +++ b/kernel/src/mm/pfa.rs @@ -12,6 +12,7 @@ pub struct FrameAllocator { pub total_frames: usize, pub free_frames: usize, pub usable_ram_bytes: u64, + pub total_ram_bytes: u64, pub hhdm_offset: u64, } @@ -24,6 +25,7 @@ pub static mut PFA: FrameAllocator = FrameAllocator { total_frames: 0, free_frames: 0, usable_ram_bytes: 0, + total_ram_bytes: 0, hhdm_offset: 0, }; @@ -38,15 +40,25 @@ impl FrameAllocator { let entry_count = (*memmap).entry_count as usize; let entries_ptr = (*memmap).entries; let mut max_paddr: u64 = 0; + let mut total_ram: u64 = 0; for i in 0..entry_count { let entry = *entries_ptr.add(i); let end = (*entry).base + (*entry).length; if end > max_paddr { max_paddr = end; } + let t = (*entry).typ; + if t == LimineMemoryType::Usable as u64 + || t == LimineMemoryType::BootloaderReclaimable as u64 + || t == LimineMemoryType::KernelAndModules as u64 + || t == LimineMemoryType::AcpiReclaimable as u64 + { + total_ram += (*entry).length; + } } self.total_frames = (max_paddr as usize) / PAGE_SIZE; + self.total_ram_bytes = total_ram; self.bitmap_size_bytes = (self.total_frames + 7) / 8; // Find a usable region large enough to place the bitmap