mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-05-25 14:04:36 +00:00
memory: Add kernel stack manager
This commit is contained in:
parent
99292467ed
commit
ab6529f6e6
@ -151,7 +151,7 @@ namespace GlobalDescriptorTable
|
||||
"movw %%ax, %%es\n" ::
|
||||
: "memory", "rax");
|
||||
|
||||
CPUStackPointer[Core] = KernelAllocator.RequestPages(TO_PAGES(STACK_SIZE + 1));
|
||||
CPUStackPointer[Core] = StackManager.Allocate(STACK_SIZE);
|
||||
memset(CPUStackPointer[Core], 0, STACK_SIZE);
|
||||
debug("CPU %d Stack Pointer: %#lx-%#lx (%d pages)", Core,
|
||||
CPUStackPointer[Core], (uintptr_t)CPUStackPointer[Core] + STACK_SIZE,
|
||||
@ -183,8 +183,7 @@ namespace GlobalDescriptorTable
|
||||
|
||||
for (size_t i = 0; i < sizeof(tss[Core].InterruptStackTable) / sizeof(tss[Core].InterruptStackTable[7]); i++)
|
||||
{
|
||||
void *NewStack = KernelAllocator.RequestPages(TO_PAGES(STACK_SIZE + 1));
|
||||
|
||||
void *NewStack = StackManager.Allocate(STACK_SIZE);
|
||||
tss[Core].InterruptStackTable[i] = (uint64_t)NewStack + STACK_SIZE;
|
||||
memset((void *)(tss[Core].InterruptStackTable[i] - STACK_SIZE), 0, STACK_SIZE);
|
||||
debug("IST-%d: %#lx-%#lx", i, NewStack, (uintptr_t)NewStack + STACK_SIZE);
|
||||
|
@ -216,7 +216,7 @@ namespace GlobalDescriptorTable
|
||||
"movw %%ax, %%es\n" ::
|
||||
: "memory", "eax");
|
||||
|
||||
CPUStackPointer[Core] = KernelAllocator.RequestPages(TO_PAGES(STACK_SIZE + 1));
|
||||
CPUStackPointer[Core] = StackManager.Allocate(STACK_SIZE);
|
||||
memset(CPUStackPointer[Core], 0, STACK_SIZE);
|
||||
debug("CPU %d Stack Pointer: %#lx-%#lx (%d pages)", Core,
|
||||
CPUStackPointer[Core], (uintptr_t)CPUStackPointer[Core] + STACK_SIZE,
|
||||
@ -242,7 +242,7 @@ namespace GlobalDescriptorTable
|
||||
|
||||
for (size_t i = 0; i < sizeof(tss[Core].InterruptStackTable) / sizeof(tss[Core].InterruptStackTable[7]); i++)
|
||||
{
|
||||
void *NewStack = KernelAllocator.RequestPages(TO_PAGES(STACK_SIZE + 1));
|
||||
void *NewStack = StackManager.Allocate(STACK_SIZE);
|
||||
|
||||
tss[Core].InterruptStackTable[i] = (uint32_t)NewStack + STACK_SIZE;
|
||||
memset((void *)(tss[Core].InterruptStackTable[i] - STACK_SIZE), 0, STACK_SIZE);
|
||||
|
@ -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 */
|
||||
}
|
||||
}
|
||||
|
@ -63,6 +63,7 @@ namespace Memory
|
||||
#include <memory/physical.hpp>
|
||||
#include <memory/virtual.hpp>
|
||||
#include <memory/swap_pt.hpp>
|
||||
#include <memory/kstack.hpp>
|
||||
#include <memory/table.hpp>
|
||||
#include <memory/macro.hpp>
|
||||
#include <memory/stack.hpp>
|
||||
@ -73,6 +74,7 @@ void InitializeMemoryManagement();
|
||||
void CreatePageTable(Memory::PageTable *pt);
|
||||
|
||||
extern Memory::Physical KernelAllocator;
|
||||
extern Memory::KernelStackManager StackManager;
|
||||
extern Memory::PageTable *KernelPageTable;
|
||||
|
||||
#endif // __cplusplus
|
||||
|
69
include/memory/kstack.hpp
Normal file
69
include/memory/kstack.hpp
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#ifndef __FENNIX_KERNEL_MEMORY_KERNEL_STACK_MANAGER_H__
|
||||
#define __FENNIX_KERNEL_MEMORY_KERNEL_STACK_MANAGER_H__
|
||||
|
||||
#include <memory.hpp>
|
||||
|
||||
namespace Memory
|
||||
{
|
||||
class KernelStackManager
|
||||
{
|
||||
public:
|
||||
struct StackAllocation
|
||||
{
|
||||
void *PhysicalAddress;
|
||||
void *VirtualAddress;
|
||||
size_t Size;
|
||||
};
|
||||
|
||||
private:
|
||||
NewLock(StackLock);
|
||||
std::list<StackAllocation> AllocatedStacks;
|
||||
size_t TotalSize = 0;
|
||||
uintptr_t CurrentStackTop = KERNEL_STACK_END;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Allocate a new stack with detailed information
|
||||
*
|
||||
* @param Size Size in bytes to allocate
|
||||
* @return {PhysicalAddress, VirtualAddress, Size}
|
||||
*/
|
||||
StackAllocation DetailedAllocate(size_t Size);
|
||||
|
||||
/**
|
||||
* Allocate a new stack
|
||||
*
|
||||
* @param Size Size in bytes to allocate
|
||||
* @return Pointer to the BASE of the stack
|
||||
*/
|
||||
void *Allocate(size_t Size);
|
||||
|
||||
/**
|
||||
* Free a previously allocated stack
|
||||
*
|
||||
* @param Address Virtual Address
|
||||
*/
|
||||
void Free(void *Address);
|
||||
KernelStackManager();
|
||||
~KernelStackManager();
|
||||
};
|
||||
}
|
||||
|
||||
#endif // !__FENNIX_KERNEL_MEMORY_KERNEL_STACK_MANAGER_H__
|
@ -50,17 +50,23 @@
|
||||
#if defined(a64) || defined(aa64)
|
||||
#define KERNEL_VMA_OFFSET 0xFFFFFFFF80000000
|
||||
|
||||
#define USER_ALLOC_BASE 0xFFFFA00000000000 /* 256 GiB */
|
||||
#define USER_ALLOC_END 0xFFFFB00000000000
|
||||
#define USER_ALLOC_BASE 0xFFFFA00000000000 /* 256 GiB */
|
||||
#define USER_ALLOC_END 0xFFFFB00000000000
|
||||
|
||||
#define USER_STACK_END 0xFFFFEFFF00000000 /* 256 MiB */
|
||||
#define USER_STACK_BASE 0xFFFFEFFFFFFF0000
|
||||
#define KERNEL_STACK_BASE 0xFFFFB00000000000 /* 256 GiB */
|
||||
#define KERNEL_STACK_END 0xFFFFC00000000000
|
||||
|
||||
#define USER_STACK_END 0xFFFFEFFF00000000 /* 256 MiB */
|
||||
#define USER_STACK_BASE 0xFFFFEFFFFFFF0000
|
||||
#elif defined(a32)
|
||||
#define KERNEL_VMA_OFFSET 0xC0000000
|
||||
|
||||
#define USER_ALLOC_BASE 0x80000000
|
||||
#define USER_ALLOC_END 0xA0000000
|
||||
|
||||
#define KERNEL_STACK_BASE 0xA0000000
|
||||
#define KERNEL_STACK_END 0xB0000000
|
||||
|
||||
#define USER_STACK_BASE 0xEFFFFFFF
|
||||
#define USER_STACK_END 0xE0000000
|
||||
#endif
|
||||
|
@ -324,6 +324,7 @@ EXTERNC __no_stack_protector NIF void Entry(BootInfo *Info)
|
||||
InitializeMemoryManagement();
|
||||
|
||||
void *KernelStackAddress = KernelAllocator.RequestPages(TO_PAGES(STACK_SIZE));
|
||||
// void *KernelStackAddress = StackManager.Allocate(STACK_SIZE); /* FIXME: This breaks stl tests, how? */
|
||||
uintptr_t KernelStack = (uintptr_t)KernelStackAddress + STACK_SIZE - 0x10;
|
||||
debug("Kernel stack: %#lx-%#lx", KernelStackAddress, KernelStack);
|
||||
#if defined(a64)
|
||||
|
Loading…
x
Reference in New Issue
Block a user