From cc6ec04814cb4225fca9dcae799bf4ac50e79d2e Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Tue, 24 Dec 2024 03:54:13 +0200 Subject: [PATCH] kernel/stack: Fix stack guard expansion Kernel didn't mapped the pages correctly Signed-off-by: EnderIce2 --- Kernel/core/memory/stack_guard.cpp | 37 +++++++++++++++--------------- Kernel/include/memory/stack.hpp | 4 ++-- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Kernel/core/memory/stack_guard.cpp b/Kernel/core/memory/stack_guard.cpp index c052b5eb..9fb9ca4f 100644 --- a/Kernel/core/memory/stack_guard.cpp +++ b/Kernel/core/memory/stack_guard.cpp @@ -26,8 +26,7 @@ namespace Memory if (!this->UserMode) assert(!"Kernel mode stack expansion not implemented"); - if (FaultAddress < USER_STACK_END || - FaultAddress > USER_STACK_BASE) + if (FaultAddress < USER_STACK_END || FaultAddress > USER_STACK_BASE) { info("Fault address %#lx is not in range of stack %#lx - %#lx", FaultAddress, USER_STACK_END, USER_STACK_BASE); @@ -39,16 +38,16 @@ namespace Memory size_t stackPages = TO_PAGES(diff); stackPages = stackPages < 1 ? 1 : stackPages; - debug("roundFA: %#lx, sb: %#lx, diff: %#lx, stackPages: %d", + debug("roundFA: %#lx, StackBottom: %#lx, diff: %#lx, stackPages: %d", roundFA, this->StackBottom, diff, stackPages); - void *AllocatedStack = vma->RequestPages(stackPages); - debug("AllocatedStack: %#lx", AllocatedStack); + void *pPage = vma->RequestPages(stackPages); + debug("pPage: %#lx", pPage); - for (size_t i = 0; i < stackPages; i++) + for (size_t i = 1; i < stackPages + 1; i++) { void *vAddress = (void *)((uintptr_t)this->StackBottom - (i * PAGE_SIZE)); - void *pAddress = (void *)((uintptr_t)AllocatedStack + (i * PAGE_SIZE)); + void *pAddress = (void *)((uintptr_t)pPage + (i * PAGE_SIZE)); vma->Map(vAddress, pAddress, PAGE_SIZE, PTFlag::RW | PTFlag::US); AllocatedPages ap = { @@ -56,11 +55,11 @@ namespace Memory .VirtualAddress = vAddress, }; AllocatedPagesList.push_back(ap); - debug("Mapped %#lx to %#lx", pAddress, vAddress); + debug("Mapped p:%#lx to v:%#lx", pAddress, vAddress); } this->StackBottom = (void *)((uintptr_t)this->StackBottom - (stackPages * PAGE_SIZE)); - this->Size += stackPages * PAGE_SIZE; + this->CurrentSize += stackPages * PAGE_SIZE; debug("Stack expanded to %#lx", this->StackBottom); this->Expanded = true; return true; @@ -76,7 +75,7 @@ namespace Memory this->StackTop = Parent->StackTop; this->StackPhysicalBottom = Parent->StackPhysicalBottom; this->StackPhysicalTop = Parent->StackPhysicalTop; - this->Size = Parent->Size; + this->CurrentSize = Parent->CurrentSize; this->Expanded = Parent->Expanded; std::list ParentAllocatedPages = Parent->GetAllocatedPages(); @@ -103,19 +102,19 @@ namespace Memory if (this->UserMode) { - void *AllocatedStack = vma->RequestPages(TO_PAGES(USER_STACK_SIZE)); + void *pPage = vma->RequestPages(TO_PAGES(USER_STACK_SIZE)); + debug("pPage: %#lx", pPage); + this->StackBottom = (void *)USER_STACK_BASE; this->StackTop = (void *)(USER_STACK_BASE + USER_STACK_SIZE); - this->StackPhysicalBottom = AllocatedStack; - this->StackPhysicalTop = (void *)((uintptr_t)AllocatedStack + USER_STACK_SIZE); - this->Size = USER_STACK_SIZE; - - debug("AllocatedStack: %#lx", AllocatedStack); + this->StackPhysicalBottom = pPage; + this->StackPhysicalTop = (void *)((uintptr_t)pPage + USER_STACK_SIZE); + this->CurrentSize = USER_STACK_SIZE; for (size_t i = 0; i < TO_PAGES(USER_STACK_SIZE); i++) { void *vAddress = (void *)(USER_STACK_BASE + (i * PAGE_SIZE)); - void *pAddress = (void *)((uintptr_t)AllocatedStack + (i * PAGE_SIZE)); + void *pAddress = (void *)((uintptr_t)pPage + (i * PAGE_SIZE)); vma->Map(vAddress, pAddress, PAGE_SIZE, PTFlag::RW | PTFlag::US); AllocatedPages ap = { @@ -123,7 +122,7 @@ namespace Memory .VirtualAddress = vAddress, }; AllocatedPagesList.push_back(ap); - debug("Mapped %#lx to %#lx", pAddress, vAddress); + debug("Mapped p:%#lx to v:%#lx", pAddress, vAddress); } } else @@ -133,7 +132,7 @@ namespace Memory this->StackTop = (void *)((uintptr_t)this->StackBottom + LARGE_STACK_SIZE); this->StackPhysicalBottom = sa.PhysicalAddress; this->StackPhysicalTop = (void *)((uintptr_t)this->StackPhysicalBottom + LARGE_STACK_SIZE); - this->Size = LARGE_STACK_SIZE; + this->CurrentSize = LARGE_STACK_SIZE; debug("StackBottom: %#lx", this->StackBottom); diff --git a/Kernel/include/memory/stack.hpp b/Kernel/include/memory/stack.hpp index db6ce473..c5722885 100644 --- a/Kernel/include/memory/stack.hpp +++ b/Kernel/include/memory/stack.hpp @@ -39,7 +39,7 @@ namespace Memory void *StackTop = nullptr; void *StackPhysicalBottom = nullptr; void *StackPhysicalTop = nullptr; - uint64_t Size = 0; + uint64_t CurrentSize = 0; bool UserMode = false; bool Expanded = false; VirtualMemoryArea *vma = nullptr; @@ -55,7 +55,7 @@ namespace Memory void Fork(StackGuard *Parent); /** For general info */ - uint64_t GetSize() { return Size; } + uint64_t GetSize() { return CurrentSize; } /** For general info */ bool GetUserMode() { return UserMode; }