core: Rework memory manager

This commit is contained in:
raphaelthegreat 2024-06-10 02:13:44 +03:00
parent 623b1d6837
commit fc887bf3f5
10 changed files with 339 additions and 179 deletions

View file

@ -14,9 +14,23 @@ enum class MemoryPermission : u32 {
Write = 1 << 1,
ReadWrite = Read | Write,
Execute = 1 << 2,
ReadWriteExecute = Read | Write | Execute,
};
DECLARE_ENUM_FLAG_OPERATORS(MemoryPermission)
constexpr VAddr SYSTEM_RESERVED = 0x800000000ULL;
constexpr VAddr CODE_BASE_OFFSET = 0x100000000ULL;
constexpr VAddr SYSTEM_MANAGED_MIN = 0x0000040000ULL;
constexpr VAddr SYSTEM_MANAGED_MAX = 0x07FFFFBFFFULL;
constexpr VAddr USER_MIN = 0x1000000000ULL;
constexpr VAddr USER_MAX = 0xFBFFFFFFFFULL;
// User area size is normally larger than this. However games are unlikely to map to high
// regions of that area, so by default we allocate a smaller virtual address space (about 1/4th).
// to save space on page tables.
static constexpr size_t UserSize = 1ULL << 38;
static constexpr size_t SystemSize = USER_MIN - SYSTEM_MANAGED_MIN;
/**
* Represents the user virtual address space backed by a dmem memory block
*/
@ -25,12 +39,15 @@ public:
explicit AddressSpace();
~AddressSpace();
[[nodiscard]] u8* VirtualBase() noexcept {
return virtual_base;
[[nodiscard]] VAddr VirtualBase() noexcept {
return reinterpret_cast<VAddr>(virtual_base);
}
[[nodiscard]] const u8* VirtualBase() const noexcept {
return virtual_base;
}
[[nodiscard]] size_t VirtualSize() const noexcept {
return virtual_size;
}
/**
* @brief Maps memory to the specified virtual address.
@ -42,20 +59,20 @@ public:
* If zero is provided the mapping is considered as private.
* @return A pointer to the mapped memory.
*/
void* Map(VAddr virtual_addr, size_t size, u64 alignment = 0, PAddr phys_addr = -1);
void* Map(VAddr virtual_addr, size_t size, u64 alignment = 0, PAddr phys_addr = -1,
bool exec = false);
/// Unmaps specified virtual memory area.
void Unmap(VAddr virtual_addr, size_t size);
void Unmap(VAddr virtual_addr, size_t size, PAddr phys_addr);
void Protect(VAddr virtual_addr, size_t size, MemoryPermission perms);
void* Reserve(size_t size, u64 alignment);
private:
struct Impl;
std::unique_ptr<Impl> impl;
u8* backing_base{};
u8* virtual_base{};
size_t virtual_size{};
};
} // namespace Core