diff --git a/src/Kernel.cc b/src/Kernel.cc index 96ec248..0a2f382 100644 --- a/src/Kernel.cc +++ b/src/Kernel.cc @@ -66,7 +66,7 @@ Kernel::initialize(const StartupInformation& startupInformation) (*it).type == 1 ? "available" : "reserved"); } - mMemoryManager.initialize(); + mMemoryManager.initialize(startupInformation); } diff --git a/src/SConscript b/src/SConscript index 4717c72..b202123 100644 --- a/src/SConscript +++ b/src/SConscript @@ -24,6 +24,7 @@ files = [ 'kstd/Memory.cc', 'kstd/PrintFormat.cc', + 'memory/FrameAllocator.cc', 'memory/Memory.cc', ] diff --git a/src/memory/FrameAllocator.cc b/src/memory/FrameAllocator.cc index 5452d6c..0250503 100644 --- a/src/memory/FrameAllocator.cc +++ b/src/memory/FrameAllocator.cc @@ -6,10 +6,33 @@ * An object to tracks and allocate physical page frames. */ -#include "FrameAllocator.hh" +#include "Kernel.hh" +#include "kstd/Memory.hh" +#include "kstd/PrintFormat.hh" +#include "memory/FrameAllocator.hh" namespace kernel { +FrameAllocator::FrameAllocator() + : mBitmap(nullptr), + mBitmapSize(0) +{ } + + +void +FrameAllocator::initialize(const StartupInformation& startupInformation) +{ + // Page frame bitmap starts immediately after the kernel. + mBitmap = reinterpret_cast(startupInformation.kernelEnd); + + const u32 pageSize = startupInformation.pageSize; + const u32 numberOfPages = startupInformation.multibootInformation->memoryKB() * 1024 / pageSize; + mBitmapSize = numberOfPages / (sizeof(FrameBitmap) * 8); + + kstd::printFormat("Allocated bitmap of %ld bytes for %ld pages at 0x%08lX\n", mBitmapSize, numberOfPages, u32(mBitmap)); + + // TODO: Before modifying this memory, maybe make sure none of the multiboot information is hanging out there? + kstd::Memory::zero(mBitmap, mBitmapSize); +} - } /* namespace kernel */ diff --git a/src/memory/FrameAllocator.hh b/src/memory/FrameAllocator.hh index f0ec61d..5d99937 100644 --- a/src/memory/FrameAllocator.hh +++ b/src/memory/FrameAllocator.hh @@ -9,10 +9,25 @@ #ifndef __MEMORY_FRAMEALLOCATOR_HH__ #define __MEMORY_FRAMEALLOCATOR_HH__ +#include "StartupInformation.hh" +#include "kstd/Types.hh" + + namespace kernel { struct FrameAllocator { + FrameAllocator(); + + void initialize(const StartupInformation& startupInformation); + +private: + typedef u8 FrameBitmap; + + /** Starting address of the frame allocation bitmap. */ + FrameBitmap* mBitmap; + /** Size of the bitmap in bytes. */ + u32 mBitmapSize; }; } /* namespace */ diff --git a/src/memory/Memory.cc b/src/memory/Memory.cc index f850a24..b924cd8 100644 --- a/src/memory/Memory.cc +++ b/src/memory/Memory.cc @@ -6,8 +6,8 @@ * Top-level classes for managing system memory. */ -#include "Memory.hh" #include "kstd/PrintFormat.hh" +#include "memory/Memory.hh" namespace kernel { @@ -15,11 +15,16 @@ namespace kernel { * Public */ +MemoryManager::MemoryManager() + : mGDT(), + mFrameAllocator() +{ } + void -MemoryManager::initialize() +MemoryManager::initialize(const StartupInformation& startupInformation) { initializeGDT(); - kstd::printFormat("GDT loaded\n"); + mFrameAllocator.initialize(startupInformation); } /* @@ -33,6 +38,7 @@ MemoryManager::initializeGDT() mGDT.setDescriptor(1, x86::GDT::DescriptorSpec::kernelSegment(0, 0xFFFFFFFF, x86::GDT::Type::CodeEXR)); mGDT.setDescriptor(2, x86::GDT::DescriptorSpec::kernelSegment(0, 0xFFFFFFFF, x86::GDT::Type::DataRW)); mGDT.load(); + kstd::printFormat("GDT loaded\n"); } } /* namespace kernel */ diff --git a/src/memory/Memory.hh b/src/memory/Memory.hh index bac9852..b5fb4c5 100644 --- a/src/memory/Memory.hh +++ b/src/memory/Memory.hh @@ -6,25 +6,30 @@ * Declaration of top-level classes for managing system memory. */ -#ifndef __MEMORY_HH__ -#define __MEMORY_HH__ +#ifndef __MEMORY_MEMORY_HH__ +#define __MEMORY_MEMORY_HH__ #include "Console.hh" #include "Descriptors.hh" +#include "StartupInformation.hh" +#include "memory/FrameAllocator.hh" namespace kernel { struct MemoryManager { - void initialize(); + MemoryManager(); + + void initialize(const StartupInformation& startupInformation); private: x86::GDT mGDT; + kernel::FrameAllocator mFrameAllocator; void initializeGDT(); }; } /* namespace kernel */ -#endif /* __MEMORY_HH__ */ +#endif /* __MEMORY_MEMORY_HH__ */