diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2024-09-30 11:08:46 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2024-09-30 11:08:46 -0400 |
commit | 19d9a04e36e7fb96eebe89e24311408460c29a70 (patch) | |
tree | 4d5f5ba595d5a5e2b59ce7b102c06b77c7be7721 /src/memory.cpp | |
parent | 85c8fd05f1a0c0224882c4fafa60003d3ef56cf3 (diff) |
reorganize files
Diffstat (limited to 'src/memory.cpp')
-rw-r--r-- | src/memory.cpp | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/memory.cpp b/src/memory.cpp new file mode 100644 index 0000000..108c240 --- /dev/null +++ b/src/memory.cpp @@ -0,0 +1,107 @@ +#include "textoutput.hpp" + +#include <array> +#include <cstdint> + +struct PageDirectory +{ + static constexpr std::uint32_t NotPresent = 0x2; + + PageDirectory(): value(NotPresent) {} + PageDirectory(void *addr): value(reinterpret_cast<std::uint32_t>(addr) | 7) {} + + std::uint32_t value; +}; +static_assert(sizeof(PageDirectory) == sizeof(std::uint32_t)); + +extern std::uint32_t lowerMem; +extern std::uint32_t upperMem; +extern TextOutput& term; + +static std::uintptr_t lowerFree = 0x400; +static std::uintptr_t upperFree = 0x100000; + +alignas(4096) +static std::array<PageDirectory, 1024> pageDirectory; + +alignas(4096) +static std::array<std::uint32_t, 1024> pageTable; + +void memory_initialize() +{ + lowerMem -= 1024; + + const auto totalKb = (lowerMem + upperMem) / 1024u; + + term.write("Claiming "); + term.write(totalKb); + term.write(" kB for allocations...\n"); + + std::uint32_t addr = 0; + for (auto& p : pageTable) { + p = addr | 7; // supervisor, r/w, present + addr += 0x1000; + } + + pageDirectory[0] = PageDirectory(pageTable.data()); + + asm volatile(R"( + mov %%eax, %%cr3 + mov %%cr0, %%eax + or $0x80000000, %%eax + mov %%eax, %%cr0 + )" :: "a"(pageDirectory.data())); + + term.write("Paging enabled.\n"); +} + +static void *memory_alloc(std::size_t size) +{ + void *ret = nullptr; + + if (lowerMem > size) { + ret = reinterpret_cast<void *>(lowerFree); + lowerFree += size; + lowerMem -= size; + } else if (upperMem > size) { + ret = reinterpret_cast<void *>(upperFree); + upperFree += size; + upperMem -= size; + } else { + // Uh oh! + term.write("!!! Kernel allocation failed !!!"); + } + + return ret; +} + +void *operator new(std::size_t size) +{ + return memory_alloc(size); +} + +void *operator new[](std::size_t size) +{ + return memory_alloc(size); +} + +void operator delete(void *) +{ + +} + +void operator delete[](void *) +{ + +} + +void operator delete(void *, std::size_t) +{ + +} + +void operator delete[](void *, std::size_t) +{ + +} + |