[Kernel] Stub MapMemoryBlock, hopefully touch the memory allocator for the last time in a while

This commit is contained in:
wheremyfoodat 2022-09-19 14:15:24 +03:00
parent 45c016d12e
commit 765b51696e
6 changed files with 233 additions and 109 deletions

View file

@ -17,7 +17,9 @@ namespace KernelHandles {
LCD = 0xFFFF8006, // LCD service
MinServiceHandle = APT,
MaxServiceHandle = LCD
MaxServiceHandle = LCD,
GSPSharedMemHandle = MaxServiceHandle + 1 // Handle for the GSP shared memory
};
// Returns whether "handle" belongs to one of the OS services

View file

@ -51,13 +51,13 @@ namespace KernelMemoryTypes {
// I assume this is referring to a single piece of allocated memory? If it's for pages, it makes no sense.
// If it's for multiple allocations, it also makes no sense
struct MemoryInfo {
u32 baseVaddr; // Base process virtual address. TODO: What even is this
u32 baseAddr; // Base process virtual address. Used as a paddr in lockedMemoryInfo instead
u32 size; // Of what?
u32 perms; // Is this referring to a single page or?
u32 state;
u32 end() { return baseVaddr + size; }
MemoryInfo(u32 baseVaddr, u32 size, u32 perms, u32 state) : baseVaddr(baseVaddr), size(size)
u32 end() { return baseAddr + size; }
MemoryInfo(u32 baseAddr, u32 size, u32 perms, u32 state) : baseAddr(baseAddr), size(size)
, perms(perms), state(state) {}
};
}
@ -67,7 +67,11 @@ class Memory {
// Our dynarmic core uses page tables for reads and writes with 4096 byte pages
std::vector<uintptr_t> readTable, writeTable;
// This tracks our OS' memory allocations
std::vector<KernelMemoryTypes::MemoryInfo> memoryInfo;
// This tracks our physical memory reservations when the memory is not actually mapped to a vaddr
std::vector<KernelMemoryTypes::MemoryInfo> lockedMemoryInfo;
static constexpr u32 pageShift = 12;
static constexpr u32 pageSize = 1 << pageShift;
@ -84,6 +88,7 @@ class Memory {
public:
u32 usedUserMemory = 0;
std::optional<int> gspMemIndex; // Index of GSP shared mem in lockedMemoryInfo or nullopt if it's already reserved
Memory();
void reset();
@ -95,24 +100,37 @@ public:
u16 read16(u32 vaddr);
u32 read32(u32 vaddr);
u64 read64(u32 vaddr);
std::string readString(u32 vaddr, u32 maxCharacters);
void write8(u32 vaddr, u8 value);
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;
}
std::string readString(u32 vaddr, u32 maxCharacters);
// 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
// adjustAddrs: If it's true paddr == 0 or vaddr == 0 tell the allocator to pick its own addresses. Used for eg svc ControlMemory
// 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,
bool adjustsAddrs = false);
KernelMemoryTypes::MemoryInfo queryMemory(u32 vaddr);
// For internal use:
// Reserve FCRAM linearly starting from physical address "paddr" (paddr == 0 is NOT special) with a size of "size"
// Without actually mapping the memory to a vaddr
// r, w, x: Permissions for the reserved memory
// Returns the index of the allocation in lockedMemoryInfo if allocation succeeded and nullopt if it failed
std::optional<int> reserveMemory(u32 paddr, u32 size, bool r, bool w, bool x);
// Map GSP shared memory to virtual address vaddr with permissions "myPerms"
// The kernel has a second permission parameter in MapMemoryBlock but not sure what's used for
// TODO: Find out
void mapGSPSharedMemory(u32 vaddr, u32 myPerms, u32 otherPerms);
};