fix(kcp, IDT, Heap, kernel): Various fixes, scrap broken memory allocator
This commit is contained in:
Vendored
+14
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"miDebuggerPath": "/usr/bin/gdb",
|
||||||
|
"name": "QEMU",
|
||||||
|
"program": "${workspaceFolder}/kernel/bin-x86_64/kernel",
|
||||||
|
"miDebuggerServerAddress": "127.0.0.1:1234",
|
||||||
|
"cwd": "${workspaceFolder}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
+1
-1
@@ -6,7 +6,7 @@ MAKEFLAGS += -rR
|
|||||||
ARCH := x86_64
|
ARCH := x86_64
|
||||||
|
|
||||||
# Default user QEMU flags. These are appended to the QEMU command calls.
|
# Default user QEMU flags. These are appended to the QEMU command calls.
|
||||||
QEMUFLAGS := -m 2G
|
QEMUFLAGS := -m 2G -s -S
|
||||||
|
|
||||||
override IMAGE_NAME := template-$(ARCH)
|
override IMAGE_NAME := template-$(ARCH)
|
||||||
|
|
||||||
|
|||||||
@@ -7,12 +7,11 @@ void Panic(const char *meditationString, System::PanicFrame* frame) {
|
|||||||
|
|
||||||
#if defined (__x86_64__)
|
#if defined (__x86_64__)
|
||||||
if (frame != nullptr) {
|
if (frame != nullptr) {
|
||||||
|
kerr << "\t" << "InterruptVector: " << "0x" << base::hex << frame->InterruptVector << "\n";
|
||||||
if (frame->InterruptVector == 0xE) { // In case of #PF the CPU pushes an error code to the frame
|
if (frame->InterruptVector == 0xE) { // In case of #PF the CPU pushes an error code to the frame
|
||||||
auto pf_frame = (System::PageFaultPanicFrame*)frame;
|
auto pf_frame = (System::PageFaultPanicFrame*)frame;
|
||||||
frame = (System::PanicFrame*)&pf_frame->IP;
|
frame = (System::PanicFrame*)&pf_frame->IP;
|
||||||
|
|
||||||
kerr << "\t" << "InterruptVector: " << "0x" << base::hex << 0xE << "\n";
|
|
||||||
|
|
||||||
/* Page fault error details */
|
/* Page fault error details */
|
||||||
kerr << "\t" << "PageFaultPresent: " << base::dec << pf_frame->PageFaultError.Present << "\n";
|
kerr << "\t" << "PageFaultPresent: " << base::dec << pf_frame->PageFaultError.Present << "\n";
|
||||||
kerr << "\t" << "PageFaultWrite: " << base::dec << pf_frame->PageFaultError.Write << "\n";
|
kerr << "\t" << "PageFaultWrite: " << base::dec << pf_frame->PageFaultError.Write << "\n";
|
||||||
@@ -23,9 +22,11 @@ void Panic(const char *meditationString, System::PanicFrame* frame) {
|
|||||||
kerr << "\t" << "PageFaultShadowStack: " << base::dec << pf_frame->PageFaultError.ShadowStack << "\n";
|
kerr << "\t" << "PageFaultShadowStack: " << base::dec << pf_frame->PageFaultError.ShadowStack << "\n";
|
||||||
kerr << "\t" << "PageFaultSGX: " << base::dec << pf_frame->PageFaultError.ShadowStack << "\n";
|
kerr << "\t" << "PageFaultSGX: " << base::dec << pf_frame->PageFaultError.ShadowStack << "\n";
|
||||||
}
|
}
|
||||||
else
|
else if (frame->InterruptVector == 0xD) {
|
||||||
{
|
auto gpf_frame = (System::GPFPanicFrame*)frame;
|
||||||
kerr << "\t" << "InterruptVector: " << "0x" << base::hex << frame->InterruptVector << "\n";
|
frame = (System::PanicFrame*)&frame->IP;
|
||||||
|
|
||||||
|
kerr << "\t" << "ErrorCode: " << base::dec << gpf_frame->GeneralProtectionFaultError << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
kerr << "\t" << "InstructionPointer: " << "0x" << base::hex << frame->IP << "\n";
|
kerr << "\t" << "InstructionPointer: " << "0x" << base::hex << frame->IP << "\n";
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <Platform/Registers.hpp>
|
#include <Platform/Registers.hpp>
|
||||||
#include <Terminal/terminal.hpp>
|
#include <Terminal/Terminal.hpp>
|
||||||
|
|
||||||
void Panic(const char *meditationString, System::PanicFrame* frame);
|
void Panic(const char *meditationString, System::PanicFrame* frame);
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* CString.cpp
|
||||||
|
* C++ string tooling
|
||||||
|
* Copyright (c) 2025 Daniel Hammer
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "CString.hpp"
|
||||||
|
#include "Stream.hpp"
|
||||||
|
|
||||||
|
#include <Libraries/Memory.hpp>
|
||||||
|
#include <Memory/Heap.hpp>
|
||||||
|
|
||||||
|
namespace kcp {
|
||||||
|
vector<const char*> splitstr(const char* str, char delimiter) {
|
||||||
|
vector<const char*> result;
|
||||||
|
cstringstream current_stream = cstringstream();
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (*str == delimiter || *str == '\0') {
|
||||||
|
auto cstr = current_stream.cstr();
|
||||||
|
auto cstr_len = Lib::strlen(cstr);
|
||||||
|
|
||||||
|
result.push_back((const char*)Memory::g_heap->Request(sizeof(char) * (cstr_len + 1)));
|
||||||
|
|
||||||
|
memcpy((void*)result.at(result.size() - 1), cstr, cstr_len + 1);
|
||||||
|
|
||||||
|
if (*str == '\0') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
current_stream = cstringstream();
|
||||||
|
str++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
current_stream << (char)*str;
|
||||||
|
str++;
|
||||||
|
} while (*str != '\0');
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* CString.cpp
|
||||||
|
* C++ string tooling
|
||||||
|
* Copyright (c) 2025 Daniel Hammer
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <CppLib/Vector.hpp>
|
||||||
|
|
||||||
|
namespace kcp {
|
||||||
|
vector<const char*> splitstr(const char* str, char delimiter);
|
||||||
|
};
|
||||||
@@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
#include "Stream.hpp"
|
#include "Stream.hpp"
|
||||||
#include <Memory/Heap.hpp>
|
#include <Memory/Heap.hpp>
|
||||||
#include <Terminal/terminal.hpp>
|
#include <Terminal/Terminal.hpp>
|
||||||
#include <Libraries/string.hpp>
|
#include <Libraries/String.hpp>
|
||||||
|
|
||||||
kcp::cstringstream::cstringstream()
|
kcp::cstringstream::cstringstream()
|
||||||
{
|
{
|
||||||
@@ -21,7 +21,7 @@ kcp::cstringstream::~cstringstream()
|
|||||||
}
|
}
|
||||||
|
|
||||||
kcp::cstringstream& kcp::cstringstream::operator<<(char c) {
|
kcp::cstringstream& kcp::cstringstream::operator<<(char c) {
|
||||||
this->string = (const char *)Memory::g_heap->Realloc((void *)this->string, this->size + 1);
|
this->string = (char *)Memory::g_heap->Realloc((void *)this->string, this->size + 2);
|
||||||
|
|
||||||
if (!this->string)
|
if (!this->string)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace kcp {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class cstringstream {
|
class cstringstream {
|
||||||
const char* string;
|
char* string;
|
||||||
std::size_t size;
|
std::size_t size;
|
||||||
|
|
||||||
base current_base = base::dec;
|
base current_base = base::dec;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <Terminal/terminal.hpp>
|
#include <Terminal/Terminal.hpp>
|
||||||
#include <Memory/Heap.hpp>
|
#include <Memory/Heap.hpp>
|
||||||
|
|
||||||
namespace kcp
|
namespace kcp
|
||||||
@@ -24,11 +24,12 @@ namespace kcp
|
|||||||
array = nullptr;
|
array = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
T operator[](std::size_t position)
|
T& operator[](std::size_t position)
|
||||||
{
|
{
|
||||||
if (position > (sz - 1)) {
|
if (position > (sz - 1)) {
|
||||||
kerr << "Vector out of bounds caught!" << Kt::newline;
|
kerr << "Vector out of bounds caught!" << Kt::newline;
|
||||||
return T{};
|
|
||||||
|
return at(0);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -36,13 +37,17 @@ namespace kcp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
T at(std::int64_t position) {
|
T& at(std::int64_t position) {
|
||||||
return this->operator[](position);
|
return this->operator[](position);
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_back(T value) {
|
size_t push_back(T value) {
|
||||||
|
size_t _sz = sz;
|
||||||
|
|
||||||
array = (T *)Memory::g_heap->Realloc(array, sizeof(T) * (sz + 1));
|
array = (T *)Memory::g_heap->Realloc(array, sizeof(T) * (sz + 1));
|
||||||
array[sz++] = value;
|
array[sz++] = value;
|
||||||
|
|
||||||
|
return _sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
T* get_array()
|
T* get_array()
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "GDT.hpp"
|
#include "GDT.hpp"
|
||||||
#include "../Terminal/terminal.hpp"
|
#include "../Terminal/Terminal.hpp"
|
||||||
|
|
||||||
// Limine loads a GDT of course, (CS = 0x28) but we will need to make a TSS someday... therefore we load our own now
|
// Limine loads a GDT of course, (CS = 0x28) but we will need to make a TSS someday... therefore we load our own now
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ namespace Hal {
|
|||||||
BasicGDT kernelGDT{};
|
BasicGDT kernelGDT{};
|
||||||
|
|
||||||
void PrepareGDT() {
|
void PrepareGDT() {
|
||||||
kout << "[Hal] GDT at " << base::hex << (uint64_t)&kernelGDT << "\n";
|
kout << "HardwareAbstraction: GDT at " << base::hex << (uint64_t)&kernelGDT << "\n";
|
||||||
kernelGDT = {
|
kernelGDT = {
|
||||||
// Code segment offset 0x08
|
// Code segment offset 0x08
|
||||||
// Data segment offset 0x10
|
// Data segment offset 0x10
|
||||||
@@ -61,10 +61,10 @@ namespace Hal {
|
|||||||
|
|
||||||
void BridgeLoadGDT() {
|
void BridgeLoadGDT() {
|
||||||
// Puts the GDT pointer structure into the GDTR
|
// Puts the GDT pointer structure into the GDTR
|
||||||
kout << "[Hal] Setting GDTR" << Kt::newline;
|
kout << "HardwareAbstraction: Setting GDTR" << Kt::newline;
|
||||||
LoadGDT(&gdtPointer);
|
LoadGDT(&gdtPointer);
|
||||||
|
|
||||||
kout << "[Hal] Reloading segments" << Kt::newline;
|
kout << "HardwareAbstraction: Reloading segments" << Kt::newline;
|
||||||
ReloadSegments();
|
ReloadSegments();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -16,6 +16,7 @@ namespace Hal {
|
|||||||
constexpr auto TrapGate = 0x8F;
|
constexpr auto TrapGate = 0x8F;
|
||||||
|
|
||||||
InterruptDescriptor* IDT;
|
InterruptDescriptor* IDT;
|
||||||
|
InterruptDescriptor InterruptDescriptorTable[255]{};
|
||||||
IDTRStruct IDTR{};
|
IDTRStruct IDTR{};
|
||||||
|
|
||||||
const char* ExceptionStrings[] = {
|
const char* ExceptionStrings[] = {
|
||||||
@@ -55,12 +56,12 @@ namespace Hal {
|
|||||||
template<size_t i>
|
template<size_t i>
|
||||||
__attribute__((interrupt)) void ExceptionHandler(System::PanicFrame* frame)
|
__attribute__((interrupt)) void ExceptionHandler(System::PanicFrame* frame)
|
||||||
{
|
{
|
||||||
kcp::cstringstream stream;
|
// kcp::cstringstream stream;
|
||||||
stream << "Caught " << base::hex << "0x" << i << " " << ExceptionStrings[i] << " in kernel";
|
// stream << "Caught " << base::hex << "0x" << i << " " << ExceptionStrings[i] << " in kernel";
|
||||||
|
|
||||||
frame->InterruptVector = i;
|
frame->InterruptVector = i;
|
||||||
|
|
||||||
Panic(stream.cstr(), frame);
|
Panic(ExceptionStrings[i], frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoadIDT(IDTRStruct& idtr) {
|
void LoadIDT(IDTRStruct& idtr) {
|
||||||
@@ -112,16 +113,16 @@ namespace Hal {
|
|||||||
struct SetHandler<N,N> {static void run() {}};
|
struct SetHandler<N,N> {static void run() {}};
|
||||||
|
|
||||||
void IDTInitialize() {
|
void IDTInitialize() {
|
||||||
IDT = (InterruptDescriptor*)Memory::g_heap->Request(4096);
|
IDT = (InterruptDescriptor*)&InterruptDescriptorTable;
|
||||||
IDTR.Limit = 0x0FF;
|
IDTR.Limit = 0x0FF;
|
||||||
IDTR.Base = (uint64_t)&IDT;
|
IDTR.Base = (uint64_t)&IDT;
|
||||||
|
|
||||||
SetHandler<0, 31>::run();
|
SetHandler<0, 31>::run();
|
||||||
|
|
||||||
kout << "[Hal] Created exception interrupt vectors" << "\n";
|
kout << "HardwareAbstraction: Created exception interrupt vectors" << "\n";
|
||||||
|
|
||||||
LoadIDT(IDTR);
|
LoadIDT(IDTR);
|
||||||
|
|
||||||
kout << "[Hal] Loaded new IDT" << "\n";
|
kout << "HardwareAbstraction: Loaded new IDT" << "\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "string.hpp"
|
#include "String.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
@@ -9,19 +9,23 @@
|
|||||||
#include <limine.h>
|
#include <limine.h>
|
||||||
|
|
||||||
#include <Hal/GDT.hpp>
|
#include <Hal/GDT.hpp>
|
||||||
#include <Terminal/terminal.hpp>
|
#include <Terminal/Terminal.hpp>
|
||||||
#include <Libraries/string.hpp>
|
#include <Libraries/String.hpp>
|
||||||
#include <Efi/UEFI.hpp>
|
#include <Efi/UEFI.hpp>
|
||||||
#include <Common/Panic.hpp>
|
#include <Common/Panic.hpp>
|
||||||
#include <Memory/Memmap.hpp>
|
#include <Memory/Memmap.hpp>
|
||||||
#include <Memory/Heap.hpp>
|
#include <Memory/Heap.hpp>
|
||||||
|
#include <Memory/HHDM.hpp>
|
||||||
|
|
||||||
#include <CppLib/Stream.hpp>
|
#include <CppLib/Stream.hpp>
|
||||||
#include <CppLib/Vector.hpp>
|
#include <CppLib/Vector.hpp>
|
||||||
|
#include <CppLib/CString.hpp>
|
||||||
|
|
||||||
#include <Platform/Limine.hpp>
|
#include <Platform/Limine.hpp>
|
||||||
#include <Platform/Util.hpp>
|
#include <Platform/Util.hpp>
|
||||||
|
|
||||||
|
#include <Libraries/Memory.hpp>
|
||||||
|
|
||||||
#include <Hal/IDT.hpp>
|
#include <Hal/IDT.hpp>
|
||||||
|
|
||||||
using namespace Kt;
|
using namespace Kt;
|
||||||
@@ -39,7 +43,6 @@ extern void (*__init_array[])();
|
|||||||
extern void (*__init_array_end[])();
|
extern void (*__init_array_end[])();
|
||||||
|
|
||||||
extern "C" void kmain() {
|
extern "C" void kmain() {
|
||||||
// Ensure the bootloader actually understands our base revision (see spec).
|
|
||||||
if (LIMINE_BASE_REVISION_SUPPORTED == false) {
|
if (LIMINE_BASE_REVISION_SUPPORTED == false) {
|
||||||
hcf();
|
hcf();
|
||||||
}
|
}
|
||||||
@@ -49,19 +52,25 @@ extern "C" void kmain() {
|
|||||||
__init_array[i]();
|
__init_array[i]();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure we got a framebuffer.
|
|
||||||
if (framebuffer_request.response == nullptr
|
if (framebuffer_request.response == nullptr
|
||||||
|| framebuffer_request.response->framebuffer_count < 1) {
|
|| framebuffer_request.response->framebuffer_count < 1) {
|
||||||
hcf();
|
hcf();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the first framebuffer.
|
|
||||||
limine_framebuffer *framebuffer{framebuffer_request.response->framebuffers[0]};
|
limine_framebuffer *framebuffer{framebuffer_request.response->framebuffers[0]};
|
||||||
|
|
||||||
// Initialize the terminal
|
Kt::Initialize(
|
||||||
Kt::Initialize((uint32_t*)framebuffer->address, framebuffer->width, framebuffer->height, framebuffer->pitch, framebuffer->red_mask_size, framebuffer->red_mask_shift,
|
(uint32_t*)framebuffer->address,
|
||||||
framebuffer->green_mask_size, framebuffer->green_mask_shift,
|
framebuffer->width,
|
||||||
framebuffer->blue_mask_size, framebuffer->blue_mask_shift);
|
framebuffer->height,
|
||||||
|
framebuffer->pitch,
|
||||||
|
framebuffer->red_mask_size,
|
||||||
|
framebuffer->red_mask_shift,
|
||||||
|
framebuffer->green_mask_size,
|
||||||
|
framebuffer->green_mask_shift,
|
||||||
|
framebuffer->blue_mask_size,
|
||||||
|
framebuffer->blue_mask_shift
|
||||||
|
);
|
||||||
|
|
||||||
#if defined (__x86_64__)
|
#if defined (__x86_64__)
|
||||||
Hal::PrepareGDT();
|
Hal::PrepareGDT();
|
||||||
@@ -69,20 +78,10 @@ extern "C" void kmain() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
uint64_t hhdm_offset = hhdm_request.response->offset;
|
uint64_t hhdm_offset = hhdm_request.response->offset;
|
||||||
|
|
||||||
kout << "[Mem] HHDM offset: 0x" << base::hex << hhdm_offset << newline;
|
|
||||||
|
|
||||||
Memory::HHDMBase = hhdm_offset;
|
Memory::HHDMBase = hhdm_offset;
|
||||||
|
|
||||||
if (!system_table_request. response || !system_table_request.response->address) {
|
|
||||||
kout << "[Mp] Firmware type: PC BIOS" << newline;
|
|
||||||
} else {
|
|
||||||
kout << "[Mp] Firmware type: EFI" << newline;
|
|
||||||
kout << "[Efi] EFI system table at 0x" << base::hex << (uint64_t)system_table_request.response->address << newline;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (memmap_request.response != nullptr) {
|
if (memmap_request.response != nullptr) {
|
||||||
kout << "[Mem] Creating Allocator for system conventional memory" << newline;
|
kout << "MemoryManagement: Creating global HeapAllocator" << newline;
|
||||||
|
|
||||||
auto result = Memory::Scan(memmap_request.response);
|
auto result = Memory::Scan(memmap_request.response);
|
||||||
auto allocator = Memory::HeapAllocator(result);
|
auto allocator = Memory::HeapAllocator(result);
|
||||||
@@ -96,9 +95,7 @@ extern "C" void kmain() {
|
|||||||
Hal::IDTInitialize();
|
Hal::IDTInitialize();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int*x =(int*)0xffffffff;
|
int*a = (int*)0xffffff;
|
||||||
|
*a=10;
|
||||||
*x=0;
|
|
||||||
|
|
||||||
hcf();
|
hcf();
|
||||||
}
|
}
|
||||||
+19
-128
@@ -9,152 +9,43 @@
|
|||||||
#include "HHDM.hpp"
|
#include "HHDM.hpp"
|
||||||
|
|
||||||
#include <Libraries/Memory.hpp>
|
#include <Libraries/Memory.hpp>
|
||||||
#include <Terminal/terminal.hpp>
|
#include <Terminal/Terminal.hpp>
|
||||||
|
#include <Common/Panic.hpp>
|
||||||
|
#include <Platform/Util.hpp>
|
||||||
|
|
||||||
|
#include <Common/Panic.hpp>
|
||||||
|
|
||||||
namespace Memory
|
namespace Memory
|
||||||
{
|
{
|
||||||
|
constexpr size_t metadata_size = 8; // 8 bytes
|
||||||
|
|
||||||
HeapAllocator::HeapAllocator(LargestSection section)
|
HeapAllocator::HeapAllocator(LargestSection section)
|
||||||
{
|
{
|
||||||
g_section = section;
|
kout << "HeapAllocator: constructor called" << Kt::newline;
|
||||||
|
|
||||||
Block *current_ref{&head};
|
|
||||||
Block *start_block_ptr {(Block *)HHDM(section.address)};
|
|
||||||
Block& start_block = *start_block_ptr;
|
|
||||||
|
|
||||||
start_block = Block {
|
|
||||||
.size = section.size,
|
|
||||||
.next = nullptr
|
|
||||||
};
|
|
||||||
|
|
||||||
*start_block_ptr = start_block;
|
|
||||||
|
|
||||||
current_ref->next = start_block_ptr;
|
|
||||||
|
|
||||||
kout << "[Mem] HeapAllocator object created for memory section starting at 0x" << base::hex << section.address << Kt::newline;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void* HeapAllocator::Request(size_t size, bool phys) {
|
void* HeapAllocator::Request(size_t size) {
|
||||||
Block* current_block_ptr{head.next};
|
|
||||||
Block* current_prev_ptr{(Block *)&head};
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
if (current_block_ptr == nullptr) {
|
|
||||||
kerr << "A HeapAllocator was forced to return a Null pointer. (1)" << Kt::newline;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current_prev_ptr == nullptr) {
|
|
||||||
kerr << "An HeapAllocator was forced to return a Null pointer. (2)" << Kt::newline;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t descriptor_size = 8;
|
|
||||||
size_t total_size = size + descriptor_size;
|
|
||||||
|
|
||||||
if (current_block_ptr->size >= total_size) {
|
|
||||||
Block block_copy = *current_block_ptr;
|
|
||||||
BlockDescriptor* descriptor {(BlockDescriptor *)current_block_ptr};
|
|
||||||
descriptor->block_size = size;
|
|
||||||
|
|
||||||
void* block_start = (void *)((uint64_t)current_block_ptr + descriptor_size);
|
|
||||||
|
|
||||||
if (!block_copy.size) {
|
|
||||||
current_prev_ptr->next = block_copy.next;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
block_copy.size -= total_size;
|
|
||||||
|
|
||||||
current_prev_ptr->next = (Block *)(uint64_t)descriptor + total_size;
|
|
||||||
*current_prev_ptr->next = block_copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (phys) {
|
|
||||||
return (void *)SubHHDM(current_block_ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (void *)block_start;
|
|
||||||
} else {
|
|
||||||
if (current_block_ptr->next != nullptr) {
|
|
||||||
current_prev_ptr = current_block_ptr;
|
|
||||||
current_block_ptr = current_block_ptr->next;
|
|
||||||
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
kerr << "An HeapAllocator was forced to return a Null pointer. (3)" << Kt::newline;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current_block_ptr != nullptr) {
|
|
||||||
if (current_block_ptr->next == nullptr) {
|
|
||||||
kerr << "An HeapAllocator was forced to return a Null pointer. (4)" << Kt::newline;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
current_block_ptr = current_block_ptr->next;
|
|
||||||
} else
|
|
||||||
{
|
|
||||||
kerr << "An HeapAllocator was forced to return a Null pointer. (5)" << Kt::newline;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* HeapAllocator::Realloc(void* ptr, size_t size) {
|
void* HeapAllocator::Realloc(void* ptr, size_t size) {
|
||||||
void *new_block = this->Request(size);
|
return nullptr;
|
||||||
|
|
||||||
if (ptr == nullptr) {
|
|
||||||
return new_block;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_block == nullptr) {
|
|
||||||
kerr << "HeapAllocator: Reallocation failed due to memory starvation" << Kt::newline;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockDescriptor* descriptor = (BlockDescriptor*)((uint64_t)ptr - 8);
|
|
||||||
|
|
||||||
memcpy(new_block, ptr, descriptor->block_size);
|
|
||||||
|
|
||||||
this->Free(ptr);
|
|
||||||
|
|
||||||
return new_block;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HeapAllocator::Free(void *pagePtr) {
|
void HeapAllocator::Free(void* ptr) {
|
||||||
if (!IsHDDMVirtAddr((uint64_t)pagePtr)) {
|
|
||||||
pagePtr = (void *)HHDM(pagePtr);
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockDescriptor* descriptor_ptr = (BlockDescriptor*)((uint64_t)pagePtr - 8);
|
|
||||||
BlockDescriptor descriptor = *descriptor_ptr;
|
|
||||||
|
|
||||||
Block* fmrNext = head.next;
|
|
||||||
Block* blockPtr = (Block *)pagePtr;
|
|
||||||
|
|
||||||
// Merge the descriptor with the block
|
|
||||||
Block* base = (Block *)((uint64_t)blockPtr - 8);
|
|
||||||
head.next = base;
|
|
||||||
|
|
||||||
// Set the full size
|
|
||||||
base->size = descriptor.block_size + 8;
|
|
||||||
|
|
||||||
// Attach the merged block to the structure
|
|
||||||
head.next->next = fmrNext;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Traverses the Allocator's linked list for debugging
|
// Traverses the Allocator's linked list for debugging
|
||||||
void HeapAllocator::Walk() {
|
void HeapAllocator::Walk() {
|
||||||
Block* current = {head.next};
|
// Node* current = {head.next};
|
||||||
size_t i{0};
|
// size_t i{0};
|
||||||
|
|
||||||
while (current != nullptr) {
|
// while (current != nullptr) {
|
||||||
kout << "Block " << base::dec << i << " {" << current->size << " bytes & address 0x" << base::hex << (uint64_t)current << "}" << Kt::newline;
|
// kout << "Block " << base::dec << i << " {" << current->size << " bytes & address 0x" << base::hex << (uint64_t)current << "}" << Kt::newline;
|
||||||
current = current->next;
|
// current = current->next;
|
||||||
i++;
|
// i++;
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -5,19 +5,20 @@ namespace Memory {
|
|||||||
class HeapAllocator {
|
class HeapAllocator {
|
||||||
LargestSection g_section;
|
LargestSection g_section;
|
||||||
|
|
||||||
struct Block {
|
struct Node {
|
||||||
size_t size;
|
size_t size;
|
||||||
Block* next;
|
Node* next;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct BlockDescriptor {
|
struct Metadata {
|
||||||
size_t block_size;
|
size_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
Block head;
|
Node head{0, 0};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HeapAllocator(LargestSection section);
|
HeapAllocator(LargestSection section);
|
||||||
void* Request(size_t size, bool phys = false);
|
void* Request(size_t size);
|
||||||
void* Realloc(void* ptr, size_t size);
|
void* Realloc(void* ptr, size_t size);
|
||||||
void Free(void *pagePtr);
|
void Free(void *pagePtr);
|
||||||
void Walk();
|
void Walk();
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include "Memmap.hpp"
|
#include "Memmap.hpp"
|
||||||
#include <Terminal/terminal.hpp>
|
#include <Terminal/Terminal.hpp>
|
||||||
#include <Common/Panic.hpp>
|
#include <Common/Panic.hpp>
|
||||||
|
|
||||||
#include "Heap.hpp"
|
#include "Heap.hpp"
|
||||||
@@ -18,7 +18,7 @@ namespace Memory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (entry->type == LIMINE_MEMMAP_USABLE) {
|
if (entry->type == LIMINE_MEMMAP_USABLE) {
|
||||||
kout << "[Mem] Found conventional memory section (size = " << base::dec << entry->length << " bytes, address = 0x" << base::hex << (uint64_t)entry->base << ")" << newline;
|
kout << "MemoryManagement: Found conventional memory section (size = " << base::dec << entry->length << " bytes, address = 0x" << base::hex << (uint64_t)entry->base << ")" << newline;
|
||||||
|
|
||||||
if (entry->length > currentLargestSection.size) {
|
if (entry->length > currentLargestSection.size) {
|
||||||
currentLargestSection = {
|
currentLargestSection = {
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
extern "C" {
|
|
||||||
int foobar(){
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -39,5 +39,16 @@ namespace System {
|
|||||||
uint8_t InterruptVector;
|
uint8_t InterruptVector;
|
||||||
}__attribute__((packed));
|
}__attribute__((packed));
|
||||||
|
|
||||||
|
struct GPFPanicFrame {
|
||||||
|
uint64_t GeneralProtectionFaultError;
|
||||||
|
uint64_t IP;
|
||||||
|
uint64_t CS;
|
||||||
|
uint64_t Flags;
|
||||||
|
uint64_t SP;
|
||||||
|
uint64_t SS;
|
||||||
|
|
||||||
|
uint8_t InterruptVector;
|
||||||
|
}__attribute__((packed));
|
||||||
|
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
#include "terminal.hpp"
|
#include "Terminal.hpp"
|
||||||
#include "../Libraries/flanterm/backends/fb.h"
|
#include "../Libraries/flanterm/backends/fb.h"
|
||||||
#include "../Libraries/flanterm/flanterm.h"
|
#include "../Libraries/flanterm/flanterm.h"
|
||||||
|
|
||||||
#include "../Libraries/string.hpp"
|
#include "../Libraries/String.hpp"
|
||||||
|
|
||||||
namespace Kt {
|
namespace Kt {
|
||||||
flanterm_context *ctx;
|
flanterm_context *ctx;
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
#include <Libraries/string.hpp>
|
#include <Libraries/String.hpp>
|
||||||
#include <CppLib/Stream.hpp>
|
#include <CppLib/Stream.hpp>
|
||||||
|
|
||||||
using namespace kcp;
|
using namespace kcp;
|
||||||
Reference in New Issue
Block a user