* Memory: Rework FCRAM management entirely Disables a lot of functionality... but I didn't want to commit too much to this commit Also reworks virtual memory management somewhat (but needs more work) * Accurately handle MemoryState for virtual memory Previously all non-free blocks were marked as Reserved * Memory: Consolidate state and permission changes Can now use a single function to change either state, permissions, or both Also merge vmem blocks that have the same state and permissions * Memory: Fix double reset for FCRAM manager Fix minor bug with permission tracking * Memory: Implement Protect operation in ControlMemory * Memory: Implement Unmap in ControlMemory Also do a sanity check to make sure the memory region is free for linear allocations * Memory: Make TLS only 0x200 bytes for each thread Also move TLS to Base region * RO: Unmap CROs when unloaded Thanks @noumidev * Kernel: Return used app memory for Commit ResourceLimit Not quite correct, but nothing to be done until process management is improved Also remove the stack limit for CXIs (thanks amogus) * Kernel: Report used app memory for GetProcessInfo 2 Not really correct, but it should be accurate for applications at least * Formatting changes * Initial fastmem support * PCSX2 fastmem depression * Move away from PCSX2 fastmem * Add enum_flag_ops.hpp * Finally building on Windows * Almost got a PoC * Fix arm64 builds * This somehow works * This also works... * Properly fix fastmem * Add free region manager * Update boost * Add ScopeExit * Comment out asserts on Linux/Mac/Android * Comment out ASSERT_MSG asserts too * Fix derp * Attempt to fix Android * Disable fastmem on Android * Fix Android again maybe pt 2 * android pls * AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA * AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA * Update host_memory.cpp * Properly reset memory arena on reset * Proper ashmem code for Android * more * Add temporary Android buildjet script for faster prototype builds * Fix fastmem (again) * Clean up shared memory * Remove Android BuildJet runner * a * Revert "a" This reverts commit 5443ad6f2a794c19c9b1a1567ca1c7f58eed78cd. * Re-add ELF support * Re-add 3DSX support * GetSystemInfo, GetProcessInfo: Memory sizes should be in bytes * Update Boost * Update metal-cpp * Fix metal renderer compilation * Fix fastmem mapping * Clean up fastmem code * Fix oopsie again * Emulator: Reorder struct * Kernel types: Cleanup * Cleanup * More cleanup * Make invalid mprotects warn instead of panicking * Add setting for toggling fastmem * More cleanup * Properly initialize BSS to zeroes * Remove unused code * Formatting * Cleanup * Memory/CRO: Workaround for Pokemon XY * NCSD loader: Fix BSS (again) * NCSD loader: Fix BSS (again) (again) * More memory fixes * Memory: Remove unused code * FS: Warn on unimplemented functions instead of panic * Update software_keyboard.cpp * Libretro: Add fastmem option * FRD: Stub SaveLocalAccountData --------- Co-authored-by: PSI-Rockin <PSI-Rockin@users.noreply.github.com>
331 lines
11 KiB
C++
331 lines
11 KiB
C++
#pragma once
|
|
#include <array>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <list>
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
#include "config.hpp"
|
|
#include "crypto/aes_engine.hpp"
|
|
#include "handles.hpp"
|
|
#include "helpers.hpp"
|
|
#include "host_memory/host_memory.h"
|
|
#include "kernel/fcram.hpp"
|
|
#include "loader/3dsx.hpp"
|
|
#include "loader/ncsd.hpp"
|
|
#include "result/result.hpp"
|
|
#include "services/region_codes.hpp"
|
|
|
|
namespace PhysicalAddrs {
|
|
enum : u32 {
|
|
VRAM = 0x18000000,
|
|
VRAMEnd = VRAM + 0x005FFFFF,
|
|
FCRAM = 0x20000000,
|
|
FCRAMEnd = FCRAM + 0x07FFFFFF,
|
|
|
|
DSP_RAM = 0x1FF00000,
|
|
DSP_RAM_End = DSP_RAM + 0x0007FFFF
|
|
};
|
|
}
|
|
|
|
namespace VirtualAddrs {
|
|
enum : u32 {
|
|
ExecutableStart = 0x00100000,
|
|
MaxExeSize = 0x03F00000,
|
|
ExecutableEnd = 0x00100000 + 0x03F00000,
|
|
|
|
// Stack for main ARM11 thread.
|
|
// Typically 0x4000 bytes, determined by exheader
|
|
StackTop = 0x10000000,
|
|
DefaultStackSize = 0x4000,
|
|
|
|
NormalHeapStart = 0x08000000,
|
|
LinearHeapStartOld = 0x14000000, // If kernel version < 0x22C
|
|
LinearHeapEndOld = 0x1C000000,
|
|
|
|
LinearHeapStartNew = 0x30000000,
|
|
LinearHeapEndNew = 0x40000000,
|
|
|
|
// Start of TLS for first thread. Next thread's storage will be at TLSBase + 0x200, and so on
|
|
TLSBase = 0x1FF82000,
|
|
TLSSize = 0x200,
|
|
|
|
VramStart = 0x1F000000,
|
|
VramSize = 0x00600000,
|
|
FcramTotalSize = 128_MB,
|
|
DSPMemStart = 0x1FF00000
|
|
};
|
|
}
|
|
|
|
// Types for svcQueryMemory
|
|
namespace KernelMemoryTypes {
|
|
// This makes no sense
|
|
enum MemoryState : u32 {
|
|
Free = 0,
|
|
Reserved = 1,
|
|
IO = 2,
|
|
Static = 3,
|
|
Code = 4,
|
|
Private = 5,
|
|
Shared = 6,
|
|
Continuous = 7,
|
|
Aliased = 8,
|
|
Alias = 9,
|
|
AliasCode = 10,
|
|
Locked = 11,
|
|
|
|
PERMISSION_R = 1 << 0,
|
|
PERMISSION_W = 1 << 1,
|
|
PERMISSION_X = 1 << 2
|
|
};
|
|
|
|
struct MemoryInfo {
|
|
u32 baseAddr;
|
|
u32 pages;
|
|
u32 perms;
|
|
u32 state;
|
|
|
|
u32 end() { return baseAddr + (pages << 12); }
|
|
MemoryInfo() : baseAddr(0), pages(0), perms(0), state(0) {}
|
|
MemoryInfo(u32 baseAddr, u32 pages, u32 perms, u32 state) : baseAddr(baseAddr), pages(pages), perms(perms), state(state) {}
|
|
};
|
|
|
|
// Shared memory block for HID, GSP:GPU etc
|
|
struct SharedMemoryBlock {
|
|
u32 paddr; // Physical address of this block's memory
|
|
u32 size; // Size of block
|
|
u32 handle; // The handle of the shared memory block
|
|
bool mapped; // Has this block been mapped at least once?
|
|
|
|
SharedMemoryBlock(u32 paddr, u32 size, u32 handle) : paddr(paddr), size(size), handle(handle), mapped(false) {}
|
|
};
|
|
} // namespace KernelMemoryTypes
|
|
|
|
class Memory {
|
|
// Used internally by changeMemoryState
|
|
struct Operation {
|
|
KernelMemoryTypes::MemoryState newState = KernelMemoryTypes::MemoryState::Free;
|
|
bool r = false, w = false, x = false;
|
|
bool changeState = false;
|
|
bool changePerms = false;
|
|
};
|
|
using Handle = HorizonHandle;
|
|
|
|
u8* fcram;
|
|
u8* dspRam; // Provided to us by Audio
|
|
u8* vram; // Provided to the memory class by the GPU class
|
|
|
|
const u64* cpuTicks = nullptr; // Pointer to the CPU tick counter, provided to us by the CPU class
|
|
using SharedMemoryBlock = KernelMemoryTypes::SharedMemoryBlock;
|
|
|
|
// TODO: remove this reference when Peach's excellent page table code is moved to a better home
|
|
KFcram& fcramManager;
|
|
|
|
// Our dynarmic core uses page tables for reads and writes with 4096 byte pages
|
|
std::vector<uintptr_t> readTable, writeTable;
|
|
|
|
// vaddr->paddr translation table
|
|
std::vector<u32> paddrTable;
|
|
|
|
// This tracks our OS' memory allocations
|
|
std::list<KernelMemoryTypes::MemoryInfo> memoryInfo;
|
|
|
|
std::array<SharedMemoryBlock, 5> sharedMemBlocks = {
|
|
SharedMemoryBlock(
|
|
0, 0, KernelHandles::FontSharedMemHandle
|
|
), // Shared memory for the system font (size is 0 because we read the size from the cmrc filesystem
|
|
SharedMemoryBlock(0, 0x1000, KernelHandles::GSPSharedMemHandle), // GSP shared memory
|
|
SharedMemoryBlock(0, 0x1000, KernelHandles::HIDSharedMemHandle), // HID shared memory
|
|
SharedMemoryBlock(0, 0x3000, KernelHandles::CSNDSharedMemHandle), // CSND shared memory
|
|
SharedMemoryBlock(0, 0xE7000, KernelHandles::APTCaptureSharedMemHandle), // APT Capture Buffer memory
|
|
};
|
|
|
|
public:
|
|
static constexpr u32 pageShift = 12;
|
|
static constexpr u32 pageSize = 1 << pageShift;
|
|
static constexpr u32 pageMask = pageSize - 1;
|
|
static constexpr u32 totalPageCount = 1 << (32 - pageShift);
|
|
|
|
static constexpr u32 FCRAM_SIZE = u32(128_MB);
|
|
static constexpr u32 FCRAM_APPLICATION_SIZE = u32(64_MB + 16_MB);
|
|
static constexpr u32 FCRAM_SYSTEM_SIZE = u32(44_MB - 16_MB);
|
|
static constexpr u32 FCRAM_BASE_SIZE = u32(20_MB);
|
|
|
|
static constexpr u32 FCRAM_PAGE_COUNT = FCRAM_SIZE / pageSize;
|
|
static constexpr u32 FCRAM_APPLICATION_PAGE_COUNT = FCRAM_APPLICATION_SIZE / pageSize;
|
|
|
|
static constexpr u32 DSP_RAM_SIZE = u32(512_KB);
|
|
static constexpr u32 DSP_CODE_MEMORY_OFFSET = u32(0_KB);
|
|
static constexpr u32 DSP_DATA_MEMORY_OFFSET = u32(256_KB);
|
|
|
|
private:
|
|
// We also use MMU-accelerated fastmem for fast memory emulation
|
|
// This means that we've got a 4GB memory arena which is organized the same way as the emulated 3DS' memory map
|
|
// And we can access this directly instead of calling the memory read/write functions, which would be slower
|
|
// Regions that are not mapped or can't be accelerated this way will segfault, and the caller (eg dynarmic), will
|
|
// handle this segfault and call the Slower memory read/write functions
|
|
bool useFastmem = false;
|
|
static constexpr size_t FASTMEM_FCRAM_OFFSET = 0; // Offset of FCRAM in the fastmem arena
|
|
static constexpr size_t FASTMEM_DSP_RAM_OFFSET = FASTMEM_FCRAM_OFFSET + FCRAM_SIZE; // Offset of DSP RAM
|
|
|
|
static constexpr size_t FASTMEM_BACKING_SIZE = FCRAM_SIZE + DSP_RAM_SIZE;
|
|
// Total size of the virtual address space we will occupy (4GB)
|
|
static constexpr size_t FASTMEM_VIRTUAL_SIZE = 4_GB;
|
|
|
|
Common::HostMemory* arena;
|
|
|
|
void addFastmemView(u32 guestVaddr, size_t arenaOffset, size_t size, bool w, bool x = false) {
|
|
if (useFastmem) {
|
|
Common::MemoryPermission perms = Common::MemoryPermission::Read;
|
|
if (w) {
|
|
perms |= Common::MemoryPermission::Write;
|
|
}
|
|
|
|
if (x) {
|
|
// perms |= Common::MemoryPermission::Execute;
|
|
}
|
|
|
|
// If anything is mapped at the place we're trying to map, unmap it. Then, create our mapping.
|
|
arena->Unmap(guestVaddr, size, false);
|
|
arena->Map(guestVaddr, arenaOffset, size, perms, false);
|
|
}
|
|
}
|
|
|
|
u64 timeSince3DSEpoch();
|
|
|
|
// https://www.3dbrew.org/wiki/Configuration_Memory#ENVINFO
|
|
// Report a retail unit without JTAG
|
|
static constexpr u32 envInfo = 1;
|
|
|
|
// Stored in Configuration Memory starting @ 0x1FF80060
|
|
struct FirmwareInfo {
|
|
u8 unk; // Usually 0 according to 3DBrew
|
|
u8 revision;
|
|
u8 minor;
|
|
u8 major;
|
|
u32 syscoreVer;
|
|
u32 sdkVer;
|
|
};
|
|
|
|
// Values taken from 3DBrew and Citra
|
|
static constexpr FirmwareInfo firm{.unk = 0, .revision = 0, .minor = 0x34, .major = 2, .syscoreVer = 2, .sdkVer = 0x0000F297};
|
|
// Adjusted upon loading a ROM based on the ROM header. Used by CFG::SecureInfoGetArea to get past region locks
|
|
Regions region = Regions::USA;
|
|
const EmulatorConfig& config;
|
|
|
|
static constexpr std::array<u8, 6> MACAddress = {0x40, 0xF4, 0x07, 0xFF, 0xFF, 0xEE};
|
|
|
|
void changeMemoryState(u32 vaddr, s32 pages, const Operation& op);
|
|
void queryPhysicalBlocks(std::list<FcramBlock>& outList, u32 vaddr, s32 pages);
|
|
void mapPhysicalMemory(u32 vaddr, u32 paddr, s32 pages, bool r, bool w, bool x);
|
|
void unmapPhysicalMemory(u32 vaddr, u32 paddr, s32 pages);
|
|
|
|
public:
|
|
u16 kernelVersion = 0;
|
|
|
|
Memory(KFcram& fcramManager, const EmulatorConfig& config);
|
|
void reset();
|
|
void* getReadPointer(u32 address);
|
|
void* getWritePointer(u32 address);
|
|
std::optional<u32> loadELF(std::ifstream& file);
|
|
std::optional<u32> load3DSX(const std::filesystem::path& path);
|
|
std::optional<NCSD> loadNCSD(Crypto::AESEngine& aesEngine, const std::filesystem::path& path);
|
|
std::optional<NCSD> loadCXI(Crypto::AESEngine& aesEngine, const std::filesystem::path& path);
|
|
|
|
bool mapCXI(NCSD& ncsd, NCCH& cxi);
|
|
bool map3DSX(HB3DSX& hb3dsx, const HB3DSX::Header& header);
|
|
|
|
u8 read8(u32 vaddr);
|
|
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);
|
|
|
|
u32 getLinearHeapVaddr();
|
|
u8* getFCRAM() { return fcram; }
|
|
|
|
enum class BatteryLevel {
|
|
Empty = 0,
|
|
AlmostEmpty,
|
|
OneBar,
|
|
TwoBars,
|
|
ThreeBars,
|
|
FourBars,
|
|
};
|
|
|
|
u8 getBatteryState(bool adapterConnected, bool charging, BatteryLevel batteryLevel) {
|
|
u8 value = static_cast<u8>(batteryLevel) << 2; // Bits 2:4 are the battery level from 0 to 5
|
|
if (adapterConnected) value |= 1 << 0; // Bit 0 shows if the charger is connected
|
|
if (charging) value |= 1 << 1; // Bit 1 shows if we're charging
|
|
|
|
return value;
|
|
}
|
|
|
|
NCCH* getCXI() {
|
|
if (loadedCXI.has_value()) {
|
|
return &loadedCXI.value();
|
|
} else {
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
HB3DSX* get3DSX() {
|
|
if (loaded3DSX.has_value()) {
|
|
return &loaded3DSX.value();
|
|
} else {
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
// Returns whether "addr" is aligned to a page (4096 byte) boundary
|
|
static constexpr bool isAligned(u32 addr) { return (addr & pageMask) == 0; }
|
|
|
|
bool allocMemory(u32 vaddr, s32 pages, FcramRegion region, bool r, bool w, bool x, KernelMemoryTypes::MemoryState state);
|
|
bool allocMemoryLinear(u32& outVaddr, u32 inVaddr, s32 pages, FcramRegion region, bool r, bool w, bool x);
|
|
bool mapVirtualMemory(
|
|
u32 dstVaddr, u32 srcVaddr, s32 pages, bool r, bool w, bool x, KernelMemoryTypes::MemoryState oldDstState,
|
|
KernelMemoryTypes::MemoryState oldSrcState, KernelMemoryTypes::MemoryState newDstState, KernelMemoryTypes::MemoryState newSrcState,
|
|
bool unmapPages = true
|
|
);
|
|
void changePermissions(u32 vaddr, s32 pages, bool r, bool w, bool x);
|
|
Result::HorizonResult queryMemory(KernelMemoryTypes::MemoryInfo& out, u32 vaddr);
|
|
Result::HorizonResult testMemoryState(u32 vaddr, s32 pages, KernelMemoryTypes::MemoryState desiredState);
|
|
|
|
void copyToVaddr(u32 dstVaddr, const u8* srcHost, s32 size);
|
|
|
|
// Map a shared memory block 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
|
|
// Returns a pointer to the FCRAM block used for the memory if allocation succeeded
|
|
u8* mapSharedMemory(Handle handle, u32 vaddr, u32 myPerms, u32 otherPerms);
|
|
|
|
// Backup of the game's CXI partition info, if any
|
|
std::optional<NCCH> loadedCXI = std::nullopt;
|
|
std::optional<HB3DSX> loaded3DSX = std::nullopt;
|
|
// File handle for reading the loaded ncch
|
|
IOFile CXIFile;
|
|
|
|
std::optional<u64> getProgramID();
|
|
|
|
u8* getDSPMem() { return dspRam; }
|
|
u8* getDSPDataMem() { return &dspRam[DSP_DATA_MEMORY_OFFSET]; }
|
|
u8* getDSPCodeMem() { return &dspRam[DSP_CODE_MEMORY_OFFSET]; }
|
|
|
|
void setVRAM(u8* pointer) { vram = pointer; }
|
|
void setDSPMem(u8* pointer) { dspRam = pointer; }
|
|
void setCPUTicks(const u64& ticks) { cpuTicks = &ticks; }
|
|
|
|
bool allocateMainThreadStack(u32 size);
|
|
Regions getConsoleRegion();
|
|
void copySharedFont(u8* ptr, u32 vaddr);
|
|
|
|
bool isFastmemEnabled() { return useFastmem; }
|
|
u8* getFastmemArenaBase() { return arena->VirtualBasePointer(); }
|
|
};
|