From 8f036d9293c114d43e7883cd51a18d2eff69d78b Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sun, 11 Aug 2024 12:40:13 -0700 Subject: [PATCH] [kernel] Fix the mysterious paging bug!! There has been a random bug (that occurs frequently outside the debugger but rarely inside the debugger, of course) that seemed to be caused by inconsistent page mappings. Sometimes loading an ELF would work. Other times loading that same ELF, the loader would complain of missing sections or invalid headers. Worse, occasionally program execution would jump off into random memory for no reason I could see by examining the disassembly. This issue has been plauging me FOR A YEAR and I've been pulling my hair out trying to find it. https://stackoverflow.com/a/28384866 Eventually this stack overflow answer to a different question about INVLPG gave me a hint that the 'accessed' flag of page table entries might not be set on pages even if they end up in the TLB. Good riddance to this damned bug. --- src/kernel/vm_space.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/kernel/vm_space.cpp b/src/kernel/vm_space.cpp index b380995..a7bd282 100644 --- a/src/kernel/vm_space.cpp +++ b/src/kernel/vm_space.cpp @@ -250,10 +250,10 @@ vm_space::clear(const obj::vm_area &vma, uintptr_t offset, size_t count, bool fr if (flags & page_flags::present) { e = 0; - if (flags & page_flags::accessed) { - auto *addr = reinterpret_cast(it.vaddress()); - asm ( "invlpg %0" :: "m"(*addr) : "memory" ); - } + + auto *addr = reinterpret_cast(it.vaddress()); + asm ( "invlpg %0" :: "m"(*addr) : "memory" ); + if (free_count && phys == free_start + (free_count * frame_size)) { ++free_count; } else {