Sort of working OS allocator, except freeing is impossible

This commit is contained in:
wheremyfoodat 2022-09-16 23:36:55 +03:00
parent 2128d5060b
commit 93f5ec7bb4
8 changed files with 186 additions and 40 deletions

View file

@ -1,5 +1,6 @@
#pragma once
#include <filesystem>
#include <bitset>
#include <fstream>
#include <optional>
#include <vector>
#include "helpers.hpp"
@ -16,8 +17,12 @@ namespace VirtualAddrs {
StackBottom = 0x0FFFC000,
StackSize = 0x4000,
NormalHeapStart = 0x08000000,
LinearHeapStart = 0x14000000,
// Start of TLS for first thread. Next thread's storage will be at TLSBase + 0x1000, and so on
TLSBase = 0xFF400000
TLSBase = 0xFF400000,
TLSSize = 0x1000
};
}
@ -30,12 +35,23 @@ class Memory {
static constexpr u32 pageSize = 1 << pageShift;
static constexpr u32 pageMask = pageSize - 1;
static constexpr u32 totalPageCount = 1 << (32 - pageShift);
static constexpr u32 FCRAM_SIZE = 128_MB;
static constexpr u32 FCRAM_APPLICATION_SIZE = 64_MB;
static constexpr u32 FCRAM_PAGE_COUNT = FCRAM_SIZE / pageSize;
static constexpr u32 FCRAM_APPLICATION_PAGE_COUNT = FCRAM_APPLICATION_SIZE / pageSize;
std::bitset<FCRAM_PAGE_COUNT> usedFCRAMPages;
std::optional<u32> findPaddr(u32 size);
public:
u32 usedUserMemory = 0;
Memory();
void reset();
void* getReadPointer(u32 address);
void* getWritePointer(u32 address);
std::optional<u32> loadELF(std::filesystem::path& path);
std::optional<u32> loadELF(std::ifstream& file);
u8 read8(u32 vaddr);
u16 read16(u32 vaddr);
@ -46,4 +62,16 @@ public:
void write16(u32 vaddr, u16 value);
void write32(u32 vaddr, u32 value);
void write64(u32 vaddr, u64 value);
// Allocate "size" bytes of RAM starting from physical FCRAM address "paddr" (We pick it ourself if paddr == 0)
// And map them to virtual address "vaddr" (We also pick it ourself if vaddr == 0).
// If the "linear" flag is on, the paddr pages must be adjacent in FCRAM
// r, w, x: Permissions for the allocated memory
// Returns the vaddr the FCRAM was mapped to or nullopt if allocation failed
std::optional<u32> allocateMemory(u32 vaddr, u32 paddr, u32 size, bool linear, bool r = true, bool w = true, bool x = true);
// Returns whether "addr" is aligned to a page (4096 byte) boundary
static constexpr bool isAligned(u32 addr) {
return (addr & pageMask) == 0;
}
};