mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-05-28 15:34:33 +00:00
Implemented memory tracker
This commit is contained in:
parent
16bcb896fa
commit
ab7f20d5f5
@ -7,8 +7,8 @@ namespace Xalloc
|
|||||||
struct SpinLockData
|
struct SpinLockData
|
||||||
{
|
{
|
||||||
uint64_t LockData = 0x0;
|
uint64_t LockData = 0x0;
|
||||||
const char *CurrentHolder = "(nul)";
|
const char *CurrentHolder = "(null)";
|
||||||
const char *AttemptingToGet = "(nul)";
|
const char *AttemptingToGet = "(null)";
|
||||||
uint64_t Count = 0;
|
uint64_t Count = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
53
Core/Memory/Tracker.cpp
Normal file
53
Core/Memory/Tracker.cpp
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#include <memory.hpp>
|
||||||
|
#include <debug.h>
|
||||||
|
|
||||||
|
namespace Memory
|
||||||
|
{
|
||||||
|
uint64_t Tracker::GetAllocatedMemorySize()
|
||||||
|
{
|
||||||
|
uint64_t Size = 0;
|
||||||
|
foreach (auto var in AllocatedPagesList)
|
||||||
|
Size += var.PageCount;
|
||||||
|
return FROM_PAGES(Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *Tracker::RequestPages(uint64_t Count)
|
||||||
|
{
|
||||||
|
void *Address = KernelAllocator.RequestPages(Count);
|
||||||
|
for (uint64_t i = 0; i < Count; i++)
|
||||||
|
Memory::Virtual(this->PageTable).Remap((void *)((uint64_t)Address + (i * PAGE_SIZE)), (void *)((uint64_t)Address + (i * PAGE_SIZE)), Memory::PTFlag::RW | Memory::PTFlag::US);
|
||||||
|
AllocatedPagesList.push_back({Address, Count});
|
||||||
|
return Address;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Tracker::FreePages(void *Address, uint64_t Count)
|
||||||
|
{
|
||||||
|
KernelAllocator.FreePages(Address, Count);
|
||||||
|
for (uint64_t i = 0; i < Count; i++)
|
||||||
|
Memory::Virtual(this->PageTable).Remap((void *)((uint64_t)Address + (i * PAGE_SIZE)), (void *)((uint64_t)Address + (i * PAGE_SIZE)), Memory::PTFlag::RW);
|
||||||
|
|
||||||
|
for (uint64_t i = 0; i < AllocatedPagesList.size(); i++)
|
||||||
|
if (AllocatedPagesList[i].Address == Address)
|
||||||
|
{
|
||||||
|
AllocatedPagesList.remove(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Tracker::Tracker(PageTable4 *PageTable)
|
||||||
|
{
|
||||||
|
this->PageTable = PageTable;
|
||||||
|
debug("Tracker initialized.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Tracker::~Tracker()
|
||||||
|
{
|
||||||
|
foreach (auto var in AllocatedPagesList)
|
||||||
|
{
|
||||||
|
KernelAllocator.FreePages(var.Address, var.PageCount);
|
||||||
|
for (uint64_t i = 0; i < var.PageCount; i++)
|
||||||
|
Memory::Virtual(this->PageTable).Remap((void *)((uint64_t)var.Address + (i * PAGE_SIZE)), (void *)((uint64_t)var.Address + (i * PAGE_SIZE)), Memory::PTFlag::RW);
|
||||||
|
}
|
||||||
|
debug("Tracker destroyed.");
|
||||||
|
}
|
||||||
|
}
|
@ -13,7 +13,7 @@
|
|||||||
/** @{ */
|
/** @{ */
|
||||||
|
|
||||||
// If we are told to not define our own size_t, then we skip the define.
|
// If we are told to not define our own size_t, then we skip the define.
|
||||||
//#define _HAVE_UINTPTR_T
|
// #define _HAVE_UINTPTR_T
|
||||||
// typedef unsigned long uintptr_t;
|
// typedef unsigned long uintptr_t;
|
||||||
|
|
||||||
// This lets you prefix malloc and friends
|
// This lets you prefix malloc and friends
|
||||||
|
@ -94,6 +94,7 @@ namespace Tasking
|
|||||||
Thread->Name, Thread->ID, Thread->Parent->Name, Thread->Parent->ID);
|
Thread->Name, Thread->ID, Thread->Parent->Name, Thread->Parent->ID);
|
||||||
// Free memory
|
// Free memory
|
||||||
delete Thread->Stack;
|
delete Thread->Stack;
|
||||||
|
delete Thread->Memory;
|
||||||
SecurityManager.DestroyToken(Thread->Security.UniqueToken);
|
SecurityManager.DestroyToken(Thread->Security.UniqueToken);
|
||||||
delete Thread->Parent->Threads[i];
|
delete Thread->Parent->Threads[i];
|
||||||
// Remove from the list
|
// Remove from the list
|
||||||
@ -753,6 +754,7 @@ namespace Tasking
|
|||||||
case TaskTrustLevel::User:
|
case TaskTrustLevel::User:
|
||||||
{
|
{
|
||||||
Thread->Stack = new Memory::StackGuard(true, Parent->PageTable);
|
Thread->Stack = new Memory::StackGuard(true, Parent->PageTable);
|
||||||
|
Thread->Memory = new Memory::Tracker(Parent->PageTable);
|
||||||
#if defined(__amd64__)
|
#if defined(__amd64__)
|
||||||
SecurityManager.TrustToken(Thread->Security.UniqueToken, TokenTrustLevel::Untrusted);
|
SecurityManager.TrustToken(Thread->Security.UniqueToken, TokenTrustLevel::Untrusted);
|
||||||
Thread->GSBase = 0;
|
Thread->GSBase = 0;
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
#include <boot/binfo.h>
|
#include <boot/binfo.h>
|
||||||
#include <bitmap.hpp>
|
#include <bitmap.hpp>
|
||||||
|
#include <vector.hpp>
|
||||||
#include <lock.hpp>
|
#include <lock.hpp>
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
#include <types.h>
|
#include <types.h>
|
||||||
@ -41,7 +42,7 @@ extern uint64_t _kernel_text_end, _kernel_data_end, _kernel_rodata_end;
|
|||||||
// To pages
|
// To pages
|
||||||
#define TO_PAGES(d) ((d) / PAGE_SIZE + 1)
|
#define TO_PAGES(d) ((d) / PAGE_SIZE + 1)
|
||||||
// From pages
|
// From pages
|
||||||
#define FROM_PAGES(d) ((d) * PAGE_SIZE - 1)
|
#define FROM_PAGES(d) ((d)*PAGE_SIZE - 1)
|
||||||
|
|
||||||
#define NORMAL_VMA_OFFSET 0xFFFF800000000000
|
#define NORMAL_VMA_OFFSET 0xFFFF800000000000
|
||||||
#define KERNEL_VMA_OFFSET 0xFFFFFFFF80000000
|
#define KERNEL_VMA_OFFSET 0xFFFFFFFF80000000
|
||||||
@ -625,6 +626,30 @@ namespace Memory
|
|||||||
*/
|
*/
|
||||||
~StackGuard();
|
~StackGuard();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Tracker
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
Bitmap PageBitmap;
|
||||||
|
PageTable4 *PageTable;
|
||||||
|
|
||||||
|
struct AllocatedPages
|
||||||
|
{
|
||||||
|
void *Address;
|
||||||
|
uint64_t PageCount;
|
||||||
|
};
|
||||||
|
|
||||||
|
Vector<AllocatedPages> AllocatedPagesList;
|
||||||
|
|
||||||
|
public:
|
||||||
|
uint64_t GetAllocatedMemorySize();
|
||||||
|
|
||||||
|
void *RequestPages(uint64_t Count);
|
||||||
|
void FreePages(void *Address, uint64_t Count);
|
||||||
|
|
||||||
|
Tracker(PageTable4 *PageTable);
|
||||||
|
~Tracker();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -89,6 +89,7 @@ namespace Tasking
|
|||||||
IPOffset Offset;
|
IPOffset Offset;
|
||||||
int ExitCode;
|
int ExitCode;
|
||||||
Memory::StackGuard *Stack;
|
Memory::StackGuard *Stack;
|
||||||
|
Memory::Tracker *Memory;
|
||||||
TaskStatus Status;
|
TaskStatus Status;
|
||||||
#if defined(__amd64__)
|
#if defined(__amd64__)
|
||||||
CPU::x64::TrapFrame Registers;
|
CPU::x64::TrapFrame Registers;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user