mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-07-03 11:29:21 +00:00
memory: Add kernel stack manager
This commit is contained in:
@ -123,7 +123,7 @@ namespace Interrupts
|
||||
CPU::x64::wrmsr(CPU::x64::MSR_SHADOW_GS_BASE, (uint64_t)CoreData);
|
||||
CoreData->ID = Core;
|
||||
CoreData->IsActive = true;
|
||||
CoreData->Stack = (uintptr_t)KernelAllocator.RequestPages(TO_PAGES(STACK_SIZE + 1)) + STACK_SIZE;
|
||||
CoreData->Stack = (uintptr_t)StackManager.Allocate(STACK_SIZE) + STACK_SIZE;
|
||||
if (CoreData->Checksum != CPU_DATA_CHECKSUM)
|
||||
{
|
||||
KPrint("CPU %d checksum mismatch! %x != %x",
|
||||
@ -142,7 +142,7 @@ namespace Interrupts
|
||||
CPU::x32::wrmsr(CPU::x32::MSR_SHADOW_GS_BASE, (uint64_t)CoreData);
|
||||
CoreData->ID = Core;
|
||||
CoreData->IsActive = true;
|
||||
CoreData->Stack = (uintptr_t)KernelAllocator.RequestPages(TO_PAGES(STACK_SIZE + 1)) + STACK_SIZE;
|
||||
CoreData->Stack = (uintptr_t)StackManager.Allocate(STACK_SIZE) + STACK_SIZE;
|
||||
if (CoreData->Checksum != CPU_DATA_CHECKSUM)
|
||||
{
|
||||
KPrint("CPU %d checksum mismatch! %x != %x",
|
||||
|
75
core/memory/kstack.cpp
Normal file
75
core/memory/kstack.cpp
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
This file is part of Fennix Kernel.
|
||||
|
||||
Fennix Kernel is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
Fennix Kernel is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <memory.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
||||
namespace Memory
|
||||
{
|
||||
KernelStackManager::StackAllocation KernelStackManager::DetailedAllocate(size_t Size)
|
||||
{
|
||||
SmartLock(StackLock);
|
||||
Size += 0x10;
|
||||
|
||||
size_t pagesNeeded = TO_PAGES(Size);
|
||||
size_t stackSize = pagesNeeded * PAGE_SIZE;
|
||||
|
||||
assert((CurrentStackTop - stackSize) > KERNEL_STACK_BASE);
|
||||
|
||||
void *physicalMemory = KernelAllocator.RequestPages(pagesNeeded);
|
||||
void *virtualAddress = (void *)(CurrentStackTop - stackSize);
|
||||
|
||||
Memory::Virtual vmm(KernelPageTable);
|
||||
vmm.Map(virtualAddress, physicalMemory, stackSize, Memory::RW | Memory::G);
|
||||
|
||||
AllocatedStacks.push_back({physicalMemory, virtualAddress, stackSize});
|
||||
CurrentStackTop -= stackSize;
|
||||
TotalSize += stackSize;
|
||||
return {physicalMemory, virtualAddress, stackSize};
|
||||
}
|
||||
|
||||
void *KernelStackManager::Allocate(size_t Size)
|
||||
{
|
||||
return this->DetailedAllocate(Size).VirtualAddress;
|
||||
}
|
||||
|
||||
void KernelStackManager::Free(void *Address)
|
||||
{
|
||||
SmartLock(StackLock);
|
||||
|
||||
auto it = std::find_if(AllocatedStacks.begin(), AllocatedStacks.end(),
|
||||
[Address](const StackAllocation &stack)
|
||||
{
|
||||
return stack.VirtualAddress == Address;
|
||||
});
|
||||
|
||||
if (it == AllocatedStacks.end())
|
||||
return;
|
||||
|
||||
size_t pagesToFree = TO_PAGES(it->Size);
|
||||
Memory::Virtual vmm(KernelPageTable);
|
||||
vmm.Unmap(Address, it->Size);
|
||||
KernelAllocator.FreePages(it->PhysicalAddress, pagesToFree);
|
||||
|
||||
TotalSize -= it->Size;
|
||||
AllocatedStacks.erase(it);
|
||||
}
|
||||
|
||||
KernelStackManager::KernelStackManager() {}
|
||||
KernelStackManager::~KernelStackManager() {}
|
||||
}
|
@ -42,6 +42,7 @@
|
||||
using namespace Memory;
|
||||
|
||||
Physical KernelAllocator;
|
||||
Memory::KernelStackManager StackManager;
|
||||
PageTable *KernelPageTable = nullptr;
|
||||
bool Page1GBSupport = false;
|
||||
bool PSESupport = false;
|
||||
|
@ -128,10 +128,11 @@ namespace Memory
|
||||
}
|
||||
else
|
||||
{
|
||||
this->StackBottom = vma->RequestPages(TO_PAGES(STACK_SIZE));
|
||||
Memory::KernelStackManager::StackAllocation sa = StackManager.DetailedAllocate(STACK_SIZE);
|
||||
this->StackBottom = sa.VirtualAddress;
|
||||
this->StackTop = (void *)((uintptr_t)this->StackBottom + STACK_SIZE);
|
||||
this->StackPhysicalBottom = this->StackBottom;
|
||||
this->StackPhysicalTop = this->StackTop;
|
||||
this->StackPhysicalBottom = sa.PhysicalAddress;
|
||||
this->StackPhysicalTop = (void *)((uintptr_t)this->StackPhysicalBottom + STACK_SIZE);
|
||||
this->Size = STACK_SIZE;
|
||||
|
||||
debug("StackBottom: %#lx", this->StackBottom);
|
||||
@ -139,7 +140,7 @@ namespace Memory
|
||||
for (size_t i = 0; i < TO_PAGES(STACK_SIZE); i++)
|
||||
{
|
||||
AllocatedPages pa = {
|
||||
.PhysicalAddress = (void *)((uintptr_t)this->StackBottom + (i * PAGE_SIZE)),
|
||||
.PhysicalAddress = (void *)((uintptr_t)this->StackPhysicalBottom + (i * PAGE_SIZE)),
|
||||
.VirtualAddress = (void *)((uintptr_t)this->StackBottom + (i * PAGE_SIZE)),
|
||||
};
|
||||
AllocatedPagesList.push_back(pa);
|
||||
@ -152,6 +153,12 @@ namespace Memory
|
||||
|
||||
StackGuard::~StackGuard()
|
||||
{
|
||||
if (!this->UserMode)
|
||||
{
|
||||
for (auto Page : this->AllocatedPagesList)
|
||||
StackManager.Free(Page.VirtualAddress);
|
||||
}
|
||||
|
||||
/* VMA will free the stack */
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user