2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project / 2012 PPSSPP Project
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2017-02-27 20:57:46 +00:00
|
|
|
#include "ppsspp_config.h"
|
|
|
|
|
2017-03-09 13:35:12 +00:00
|
|
|
#if PPSSPP_PLATFORM(UWP)
|
|
|
|
#include "Common/CommonWindows.h"
|
|
|
|
#endif
|
|
|
|
|
2013-12-11 12:55:57 +00:00
|
|
|
#include <algorithm>
|
2017-02-27 20:57:46 +00:00
|
|
|
#include <mutex>
|
2013-12-11 12:55:57 +00:00
|
|
|
|
2014-03-15 17:38:46 +00:00
|
|
|
#include "Common/Common.h"
|
|
|
|
#include "Common/MemoryUtil.h"
|
|
|
|
#include "Common/MemArena.h"
|
2020-08-10 07:12:51 +00:00
|
|
|
#include "Common/Serialize/Serializer.h"
|
|
|
|
#include "Common/Serialize/SerializeFuncs.h"
|
2019-02-12 09:58:20 +00:00
|
|
|
|
2013-12-18 10:42:19 +00:00
|
|
|
#include "Core/Core.h"
|
2013-11-28 19:58:59 +00:00
|
|
|
#include "Core/Config.h"
|
2018-06-17 01:42:31 +00:00
|
|
|
#include "Core/ConfigValues.h"
|
2021-04-12 00:28:27 +00:00
|
|
|
#include "Core/Debugger/SymbolMap.h"
|
|
|
|
#include "Core/Debugger/MemBlockInfo.h"
|
|
|
|
#include "Core/HDRemaster.h"
|
|
|
|
#include "Core/HLE/HLE.h"
|
2013-12-18 10:42:19 +00:00
|
|
|
#include "Core/HLE/ReplaceTables.h"
|
2021-04-12 00:28:27 +00:00
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "Core/MemFault.h"
|
|
|
|
#include "Core/MIPS/MIPS.h"
|
2016-04-30 23:20:21 +00:00
|
|
|
#include "Core/MIPS/JitCommon/JitBlockCache.h"
|
2019-02-12 09:58:20 +00:00
|
|
|
#include "Core/MIPS/JitCommon/JitCommon.h"
|
2020-11-27 23:12:06 +00:00
|
|
|
#include "Common/Thread/ParallelLoop.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-12-18 10:42:19 +00:00
|
|
|
namespace Memory {
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// The base pointer to the auto-mirrored arena.
|
2020-07-15 10:10:33 +00:00
|
|
|
u8* base = nullptr;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// The MemArena class
|
|
|
|
MemArena g_arena;
|
|
|
|
// ==============
|
|
|
|
|
|
|
|
u8 *m_pPhysicalScratchPad;
|
2012-11-06 15:01:41 +00:00
|
|
|
u8 *m_pUncachedScratchPad;
|
2012-11-01 15:19:01 +00:00
|
|
|
// 64-bit: Pointers to high-mem mirrors
|
|
|
|
// 32-bit: Same as above
|
2021-04-24 05:42:05 +00:00
|
|
|
u8 *m_pPhysicalRAM[3];
|
|
|
|
u8 *m_pUncachedRAM[3];
|
|
|
|
u8 *m_pKernelRAM[3]; // RAM mirrored up to "kernel space". Fully accessible at all times currently.
|
2021-04-24 06:21:51 +00:00
|
|
|
// Technically starts at 0xA0000000, which we don't properly support (but we don't really support kernel code.)
|
|
|
|
// This matches how we handle 32-bit masking.
|
|
|
|
u8 *m_pUncachedKernelRAM[3];
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2014-05-06 07:41:49 +00:00
|
|
|
// VRAM is mirrored 4 times. The second and fourth mirrors are swizzled.
|
|
|
|
// In practice, a game accessing the mirrors most likely is deswizzling the depth buffer.
|
2021-04-24 05:42:05 +00:00
|
|
|
u8 *m_pPhysicalVRAM[4];
|
|
|
|
u8 *m_pUncachedVRAM[4];
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-06-21 17:34:57 +00:00
|
|
|
// Holds the ending address of the PSP's user space.
|
|
|
|
// Required for HD Remasters to work properly.
|
2014-05-06 07:41:49 +00:00
|
|
|
// This replaces RAM_NORMAL_SIZE at runtime.
|
2013-06-21 17:34:57 +00:00
|
|
|
u32 g_MemorySize;
|
2013-11-28 20:34:11 +00:00
|
|
|
// Used to store the PSP model on game startup.
|
|
|
|
u32 g_PSPModel;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-03-03 10:17:59 +00:00
|
|
|
std::recursive_mutex g_shutdownLock;
|
2014-07-20 06:25:32 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// We don't declare the IO region in here since its handled by other means.
|
2013-06-22 09:14:01 +00:00
|
|
|
static MemoryView views[] =
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2017-01-25 14:15:17 +00:00
|
|
|
{&m_pPhysicalScratchPad, 0x00010000, SCRATCHPAD_SIZE, 0},
|
|
|
|
{&m_pUncachedScratchPad, 0x40010000, SCRATCHPAD_SIZE, MV_MIRROR_PREVIOUS},
|
2021-04-24 05:42:05 +00:00
|
|
|
{&m_pPhysicalVRAM[0], 0x04000000, 0x00200000, 0},
|
|
|
|
{&m_pPhysicalVRAM[1], 0x04200000, 0x00200000, MV_MIRROR_PREVIOUS},
|
|
|
|
{&m_pPhysicalVRAM[2], 0x04400000, 0x00200000, MV_MIRROR_PREVIOUS},
|
|
|
|
{&m_pPhysicalVRAM[3], 0x04600000, 0x00200000, MV_MIRROR_PREVIOUS},
|
|
|
|
{&m_pUncachedVRAM[0], 0x44000000, 0x00200000, MV_MIRROR_PREVIOUS},
|
|
|
|
{&m_pUncachedVRAM[1], 0x44200000, 0x00200000, MV_MIRROR_PREVIOUS},
|
|
|
|
{&m_pUncachedVRAM[2], 0x44400000, 0x00200000, MV_MIRROR_PREVIOUS},
|
|
|
|
{&m_pUncachedVRAM[3], 0x44600000, 0x00200000, MV_MIRROR_PREVIOUS},
|
|
|
|
{&m_pPhysicalRAM[0], 0x08000000, g_MemorySize, MV_IS_PRIMARY_RAM}, // only from 0x08800000 is it usable (last 24 megs)
|
|
|
|
{&m_pUncachedRAM[0], 0x48000000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_PRIMARY_RAM},
|
|
|
|
{&m_pKernelRAM[0], 0x88000000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_PRIMARY_RAM | MV_KERNEL},
|
2021-04-24 06:21:51 +00:00
|
|
|
{&m_pUncachedKernelRAM[0],0xC0000000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_PRIMARY_RAM | MV_KERNEL},
|
2013-12-10 16:25:38 +00:00
|
|
|
// Starts at memory + 31 MB.
|
2021-04-24 05:42:05 +00:00
|
|
|
{&m_pPhysicalRAM[1], 0x09F00000, g_MemorySize, MV_IS_EXTRA1_RAM},
|
|
|
|
{&m_pUncachedRAM[1], 0x49F00000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_EXTRA1_RAM},
|
|
|
|
{&m_pKernelRAM[1], 0x89F00000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_EXTRA1_RAM | MV_KERNEL},
|
2021-04-24 06:21:51 +00:00
|
|
|
{&m_pUncachedKernelRAM[1],0xC9F00000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_EXTRA1_RAM | MV_KERNEL},
|
2013-12-10 16:25:38 +00:00
|
|
|
// Starts at memory + 31 * 2 MB.
|
2021-04-24 05:42:05 +00:00
|
|
|
{&m_pPhysicalRAM[2], 0x0BE00000, g_MemorySize, MV_IS_EXTRA2_RAM},
|
|
|
|
{&m_pUncachedRAM[2], 0x4BE00000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_EXTRA2_RAM},
|
|
|
|
{&m_pKernelRAM[2], 0x8BE00000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_EXTRA2_RAM | MV_KERNEL},
|
2021-04-24 06:21:51 +00:00
|
|
|
{&m_pUncachedKernelRAM[2],0xCBE00000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_EXTRA2_RAM | MV_KERNEL},
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// TODO: There are a few swizzled mirrors of VRAM, not sure about the best way to
|
|
|
|
// implement those.
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int num_views = sizeof(views) / sizeof(MemoryView);
|
|
|
|
|
2014-06-19 07:30:02 +00:00
|
|
|
inline static bool CanIgnoreView(const MemoryView &view) {
|
2020-11-02 22:11:44 +00:00
|
|
|
#ifdef MASKED_PSP_MEMORY
|
2014-06-19 07:30:02 +00:00
|
|
|
// Basically, 32-bit platforms can ignore views that are masked out anyway.
|
|
|
|
return (view.flags & MV_MIRROR_PREVIOUS) && (view.virtual_address & ~MEMVIEW32_MASK) != 0;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-03 04:57:25 +00:00
|
|
|
#if PPSSPP_PLATFORM(IOS) && PPSSPP_ARCH(64BIT)
|
2017-01-25 17:35:09 +00:00
|
|
|
#define SKIP(a_flags, b_flags) \
|
|
|
|
if ((b_flags) & MV_KERNEL) \
|
|
|
|
continue;
|
|
|
|
#else
|
|
|
|
#define SKIP(a_flags, b_flags) \
|
|
|
|
;
|
|
|
|
#endif
|
2014-05-28 17:29:54 +00:00
|
|
|
|
|
|
|
static bool Memory_TryBase(u32 flags) {
|
|
|
|
// OK, we know where to find free space. Now grab it!
|
|
|
|
// We just mimic the popular BAT setup.
|
|
|
|
|
|
|
|
size_t position = 0;
|
|
|
|
size_t last_position = 0;
|
|
|
|
|
|
|
|
// Zero all the pointers to be sure.
|
2017-01-25 14:15:17 +00:00
|
|
|
for (int i = 0; i < num_views; i++) {
|
2014-05-28 17:29:54 +00:00
|
|
|
if (views[i].out_ptr)
|
|
|
|
*views[i].out_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i;
|
2017-01-25 14:43:24 +00:00
|
|
|
for (i = 0; i < num_views; i++) {
|
2014-05-28 17:29:54 +00:00
|
|
|
const MemoryView &view = views[i];
|
|
|
|
if (view.size == 0)
|
|
|
|
continue;
|
|
|
|
SKIP(flags, view.flags);
|
|
|
|
|
|
|
|
if (view.flags & MV_MIRROR_PREVIOUS) {
|
|
|
|
position = last_position;
|
|
|
|
}
|
2017-03-09 13:35:12 +00:00
|
|
|
#ifndef MASKED_PSP_MEMORY
|
2014-05-28 17:29:54 +00:00
|
|
|
*view.out_ptr = (u8*)g_arena.CreateView(
|
|
|
|
position, view.size, base + view.virtual_address);
|
2017-01-25 16:14:43 +00:00
|
|
|
if (!*view.out_ptr) {
|
2017-01-25 14:43:24 +00:00
|
|
|
goto bail;
|
2017-03-19 14:44:44 +00:00
|
|
|
DEBUG_LOG(MEMMAP, "Failed at view %d", i);
|
2017-01-25 16:14:43 +00:00
|
|
|
}
|
2014-05-28 17:29:54 +00:00
|
|
|
#else
|
2014-06-19 07:30:02 +00:00
|
|
|
if (CanIgnoreView(view)) {
|
2017-01-25 14:43:24 +00:00
|
|
|
// This is handled by address masking in 32-bit, no view needs to be created.
|
2014-05-28 17:29:54 +00:00
|
|
|
*view.out_ptr = *views[i - 1].out_ptr;
|
|
|
|
} else {
|
|
|
|
*view.out_ptr = (u8*)g_arena.CreateView(
|
2014-06-19 07:30:02 +00:00
|
|
|
position, view.size, base + (view.virtual_address & MEMVIEW32_MASK));
|
2017-01-25 16:14:43 +00:00
|
|
|
if (!*view.out_ptr) {
|
2017-03-19 14:44:44 +00:00
|
|
|
DEBUG_LOG(MEMMAP, "Failed at view %d", i);
|
2014-05-28 17:29:54 +00:00
|
|
|
goto bail;
|
2017-01-25 16:14:43 +00:00
|
|
|
}
|
2014-05-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
last_position = position;
|
|
|
|
position += g_arena.roundup(view.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
bail:
|
|
|
|
// Argh! ERROR! Free what we grabbed so far so we can try again.
|
2017-01-25 14:43:24 +00:00
|
|
|
for (int j = 0; j <= i; j++) {
|
2014-05-28 17:29:54 +00:00
|
|
|
if (views[i].size == 0)
|
|
|
|
continue;
|
|
|
|
SKIP(flags, views[i].flags);
|
2017-01-25 14:43:24 +00:00
|
|
|
if (*views[j].out_ptr) {
|
2014-06-19 07:30:02 +00:00
|
|
|
if (!CanIgnoreView(views[j])) {
|
2014-05-28 17:29:54 +00:00
|
|
|
g_arena.ReleaseView(*views[j].out_ptr, views[j].size);
|
|
|
|
}
|
|
|
|
*views[j].out_ptr = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-25 14:43:24 +00:00
|
|
|
bool MemoryMap_Setup(u32 flags) {
|
2017-02-28 00:47:13 +00:00
|
|
|
#if PPSSPP_PLATFORM(UWP)
|
2017-03-09 13:35:12 +00:00
|
|
|
// We reserve the memory, then simply commit in TryBase.
|
|
|
|
base = (u8*)VirtualAllocFromApp(0, 0x10000000, MEM_RESERVE, PAGE_READWRITE);
|
2017-02-28 00:47:13 +00:00
|
|
|
#else
|
2017-03-09 13:35:12 +00:00
|
|
|
|
2017-01-25 14:43:24 +00:00
|
|
|
// Figure out how much memory we need to allocate in total.
|
2014-05-28 17:29:54 +00:00
|
|
|
size_t total_mem = 0;
|
2017-01-25 14:43:24 +00:00
|
|
|
for (int i = 0; i < num_views; i++) {
|
2014-05-28 17:29:54 +00:00
|
|
|
if (views[i].size == 0)
|
|
|
|
continue;
|
|
|
|
SKIP(flags, views[i].flags);
|
2014-06-19 07:30:02 +00:00
|
|
|
if (!CanIgnoreView(views[i]))
|
2014-05-28 17:29:54 +00:00
|
|
|
total_mem += g_arena.roundup(views[i].size);
|
|
|
|
}
|
2017-01-25 14:43:24 +00:00
|
|
|
|
2014-05-28 17:29:54 +00:00
|
|
|
// Grab some pagefile backed memory out of the void ...
|
2021-07-07 17:01:42 +00:00
|
|
|
if (!g_arena.GrabMemSpace(total_mem)) {
|
|
|
|
// It'll already have logged.
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-09 13:35:12 +00:00
|
|
|
#endif
|
2014-05-28 17:29:54 +00:00
|
|
|
|
2017-03-03 13:01:12 +00:00
|
|
|
#if !PPSSPP_PLATFORM(ANDROID)
|
2017-01-25 16:14:43 +00:00
|
|
|
if (g_arena.NeedsProbing()) {
|
|
|
|
int base_attempts = 0;
|
2020-07-14 07:31:58 +00:00
|
|
|
#if PPSSPP_PLATFORM(WINDOWS) && PPSSPP_ARCH(32BIT)
|
2017-01-25 14:43:24 +00:00
|
|
|
// Try a whole range of possible bases. Return once we got a valid one.
|
2017-01-25 16:14:43 +00:00
|
|
|
uintptr_t max_base_addr = 0x7FFF0000 - 0x10000000;
|
|
|
|
uintptr_t min_base_addr = 0x01000000;
|
|
|
|
uintptr_t stride = 0x400000;
|
2020-07-14 07:31:58 +00:00
|
|
|
#elif PPSSPP_ARCH(ARM64) && PPSSPP_PLATFORM(IOS)
|
2017-01-25 16:14:43 +00:00
|
|
|
// iOS
|
2017-03-03 13:01:12 +00:00
|
|
|
uintptr_t max_base_addr = 0x1FFFF0000ULL - 0x80000000ULL;
|
2017-01-25 17:35:09 +00:00
|
|
|
uintptr_t min_base_addr = 0x100000000ULL;
|
2017-01-25 16:14:43 +00:00
|
|
|
uintptr_t stride = 0x800000;
|
2020-07-14 07:31:58 +00:00
|
|
|
#else
|
|
|
|
uintptr_t max_base_addr = 0;
|
|
|
|
uintptr_t min_base_addr = 0;
|
|
|
|
uintptr_t stride = 0;
|
|
|
|
ERROR_LOG(MEMMAP, "MemoryMap_Setup: Hit a wrong path, should not be needed on this platform.");
|
|
|
|
return false;
|
2017-01-25 16:14:43 +00:00
|
|
|
#endif
|
|
|
|
for (uintptr_t base_addr = min_base_addr; base_addr < max_base_addr; base_addr += stride) {
|
2017-01-25 14:43:24 +00:00
|
|
|
base_attempts++;
|
|
|
|
base = (u8 *)base_addr;
|
|
|
|
if (Memory_TryBase(flags)) {
|
|
|
|
INFO_LOG(MEMMAP, "Found valid memory base at %p after %i tries.", base, base_attempts);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 17:29:54 +00:00
|
|
|
ERROR_LOG(MEMMAP, "MemoryMap_Setup: Failed finding a memory base.");
|
2017-01-25 14:43:24 +00:00
|
|
|
return false;
|
2017-03-03 13:01:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2017-03-09 13:35:12 +00:00
|
|
|
#if !PPSSPP_PLATFORM(UWP)
|
2017-01-25 16:14:43 +00:00
|
|
|
base = g_arena.Find4GBBase();
|
2023-01-12 00:09:51 +00:00
|
|
|
if (!base) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-09 13:35:12 +00:00
|
|
|
#endif
|
2014-05-28 17:29:54 +00:00
|
|
|
}
|
2017-01-25 14:43:24 +00:00
|
|
|
|
|
|
|
// Should return true...
|
|
|
|
return Memory_TryBase(flags);
|
2014-05-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 14:43:24 +00:00
|
|
|
void MemoryMap_Shutdown(u32 flags) {
|
|
|
|
for (int i = 0; i < num_views; i++) {
|
2014-05-28 17:29:54 +00:00
|
|
|
if (views[i].size == 0)
|
|
|
|
continue;
|
|
|
|
SKIP(flags, views[i].flags);
|
2017-01-25 14:15:17 +00:00
|
|
|
if (*views[i].out_ptr)
|
2014-05-28 17:29:54 +00:00
|
|
|
g_arena.ReleaseView(*views[i].out_ptr, views[i].size);
|
2017-01-25 14:15:17 +00:00
|
|
|
*views[i].out_ptr = nullptr;
|
2014-05-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
g_arena.ReleaseSpace();
|
2017-03-01 11:51:06 +00:00
|
|
|
|
|
|
|
#if PPSSPP_PLATFORM(UWP)
|
|
|
|
VirtualFree(base, 0, MEM_RELEASE);
|
|
|
|
#endif
|
2014-05-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 18:32:15 +00:00
|
|
|
bool Init() {
|
2020-09-27 19:54:34 +00:00
|
|
|
// On some 32 bit platforms (like Android, iOS, etc.), you can only map < 32 megs at a time.
|
2013-12-10 16:25:38 +00:00
|
|
|
const static int MAX_MMAP_SIZE = 31 * 1024 * 1024;
|
2020-09-27 19:54:34 +00:00
|
|
|
_dbg_assert_msg_(g_MemorySize <= MAX_MMAP_SIZE * 3, "ACK - too much memory for three mmap views.");
|
2013-07-08 03:28:47 +00:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(views); i++) {
|
|
|
|
if (views[i].flags & MV_IS_PRIMARY_RAM)
|
2013-12-10 16:25:38 +00:00
|
|
|
views[i].size = std::min((int)g_MemorySize, MAX_MMAP_SIZE);
|
|
|
|
if (views[i].flags & MV_IS_EXTRA1_RAM)
|
|
|
|
views[i].size = std::min(std::max((int)g_MemorySize - MAX_MMAP_SIZE, 0), MAX_MMAP_SIZE);
|
|
|
|
if (views[i].flags & MV_IS_EXTRA2_RAM)
|
|
|
|
views[i].size = std::min(std::max((int)g_MemorySize - MAX_MMAP_SIZE * 2, 0), MAX_MMAP_SIZE);
|
2013-06-22 08:10:42 +00:00
|
|
|
}
|
2020-07-19 18:32:15 +00:00
|
|
|
|
2017-01-25 14:15:17 +00:00
|
|
|
int flags = 0;
|
2020-07-19 18:32:15 +00:00
|
|
|
if (!MemoryMap_Setup(flags)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-01-25 14:15:17 +00:00
|
|
|
INFO_LOG(MEMMAP, "Memory system initialized. Base at %p (RAM at @ %p, uncached @ %p)",
|
|
|
|
base, m_pPhysicalRAM, m_pUncachedRAM);
|
2019-02-12 09:58:20 +00:00
|
|
|
|
2020-07-15 10:10:33 +00:00
|
|
|
MemFault_Init();
|
2020-07-19 18:32:15 +00:00
|
|
|
return true;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2018-05-06 17:40:02 +00:00
|
|
|
void Reinit() {
|
2020-07-19 15:47:02 +00:00
|
|
|
_assert_msg_(PSP_IsInited(), "Cannot reinit during startup/shutdown");
|
2018-05-06 17:40:02 +00:00
|
|
|
Core_NotifyLifecycle(CoreLifecycle::MEMORY_REINITING);
|
|
|
|
Shutdown();
|
|
|
|
Init();
|
|
|
|
Core_NotifyLifecycle(CoreLifecycle::MEMORY_REINITED);
|
|
|
|
}
|
|
|
|
|
2021-04-12 00:28:27 +00:00
|
|
|
static void DoMemoryVoid(PointerWrap &p, uint32_t start, uint32_t size) {
|
2022-07-20 10:40:22 +00:00
|
|
|
uint8_t *d = GetPointerWrite(start);
|
2021-04-12 00:28:27 +00:00
|
|
|
uint8_t *&storage = *p.ptr;
|
|
|
|
|
2021-04-12 01:02:21 +00:00
|
|
|
// We only handle aligned data and sizes.
|
|
|
|
if ((size & 0x3F) != 0 || ((uintptr_t)d & 0x3F) != 0)
|
|
|
|
return p.DoVoid(d, size);
|
|
|
|
|
2021-04-12 00:28:27 +00:00
|
|
|
switch (p.mode) {
|
|
|
|
case PointerWrap::MODE_READ:
|
2020-11-27 23:12:06 +00:00
|
|
|
ParallelMemcpy(&g_threadManager, d, storage, size);
|
2021-04-12 00:28:27 +00:00
|
|
|
break;
|
|
|
|
case PointerWrap::MODE_WRITE:
|
2020-11-27 23:12:06 +00:00
|
|
|
ParallelMemcpy(&g_threadManager, storage, d, size);
|
2021-04-12 00:28:27 +00:00
|
|
|
break;
|
|
|
|
case PointerWrap::MODE_MEASURE:
|
|
|
|
// Nothing to do here.
|
|
|
|
break;
|
|
|
|
case PointerWrap::MODE_VERIFY:
|
2020-11-27 23:12:06 +00:00
|
|
|
ParallelRangeLoop(&g_threadManager, [&](int l, int h) {
|
2021-04-12 00:28:27 +00:00
|
|
|
for (int i = l; i < h; i++)
|
|
|
|
_dbg_assert_msg_(d[i] == storage[i], "Savestate verification failure: %d (0x%X) (at %p) != %d (0x%X) (at %p).\n", d[i], d[i], &d[i], storage[i], storage[i], &storage[i]);
|
2021-06-12 19:21:28 +00:00
|
|
|
}, 0, size, 128);
|
2021-04-12 00:28:27 +00:00
|
|
|
break;
|
2022-12-04 15:57:30 +00:00
|
|
|
case PointerWrap::MODE_NOOP:
|
|
|
|
break;
|
2021-04-12 00:28:27 +00:00
|
|
|
}
|
|
|
|
storage += size;
|
|
|
|
}
|
|
|
|
|
2017-01-25 14:43:24 +00:00
|
|
|
void DoState(PointerWrap &p) {
|
2015-12-23 23:10:08 +00:00
|
|
|
auto s = p.Section("Memory", 1, 3);
|
2013-09-15 03:23:03 +00:00
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2013-11-28 20:34:11 +00:00
|
|
|
if (s < 2) {
|
2013-11-28 21:28:25 +00:00
|
|
|
if (!g_RemasterMode)
|
|
|
|
g_MemorySize = RAM_NORMAL_SIZE;
|
2013-11-28 20:34:11 +00:00
|
|
|
g_PSPModel = PSP_MODEL_FAT;
|
2015-12-23 23:10:08 +00:00
|
|
|
} else if (s == 2) {
|
|
|
|
// In version 2, we determine memory size based on PSP model.
|
2014-01-08 08:06:27 +00:00
|
|
|
u32 oldMemorySize = g_MemorySize;
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, g_PSPModel);
|
2013-11-28 20:34:11 +00:00
|
|
|
p.DoMarker("PSPModel");
|
2014-01-08 08:06:27 +00:00
|
|
|
if (!g_RemasterMode) {
|
2013-11-28 21:28:25 +00:00
|
|
|
g_MemorySize = g_PSPModel == PSP_MODEL_FAT ? RAM_NORMAL_SIZE : RAM_DOUBLE_SIZE;
|
2014-01-08 08:06:27 +00:00
|
|
|
if (oldMemorySize < g_MemorySize) {
|
2018-05-06 17:40:02 +00:00
|
|
|
Reinit();
|
2014-01-08 08:06:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-23 23:10:08 +00:00
|
|
|
} else {
|
|
|
|
// In version 3, we started just saving the memory size directly.
|
|
|
|
// It's no longer based strictly on the PSP model.
|
|
|
|
u32 oldMemorySize = g_MemorySize;
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, g_PSPModel);
|
2015-12-23 23:10:08 +00:00
|
|
|
p.DoMarker("PSPModel");
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, g_MemorySize);
|
2015-12-23 23:10:08 +00:00
|
|
|
if (oldMemorySize != g_MemorySize) {
|
2018-05-06 17:40:02 +00:00
|
|
|
Reinit();
|
2015-12-23 23:10:08 +00:00
|
|
|
}
|
2013-11-28 20:34:11 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 00:28:27 +00:00
|
|
|
DoMemoryVoid(p, PSP_GetKernelMemoryBase(), g_MemorySize);
|
2012-11-01 15:19:01 +00:00
|
|
|
p.DoMarker("RAM");
|
2013-11-28 20:34:11 +00:00
|
|
|
|
2021-04-12 00:28:27 +00:00
|
|
|
DoMemoryVoid(p, PSP_GetVidMemBase(), VRAM_SIZE);
|
2012-11-01 15:19:01 +00:00
|
|
|
p.DoMarker("VRAM");
|
2020-08-10 04:20:42 +00:00
|
|
|
DoArray(p, m_pPhysicalScratchPad, SCRATCHPAD_SIZE);
|
2012-11-01 15:19:01 +00:00
|
|
|
p.DoMarker("ScratchPad");
|
|
|
|
}
|
|
|
|
|
2016-05-20 04:17:17 +00:00
|
|
|
void Shutdown() {
|
2017-03-03 10:17:59 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(g_shutdownLock);
|
2012-11-01 15:19:01 +00:00
|
|
|
u32 flags = 0;
|
2014-05-28 17:29:54 +00:00
|
|
|
MemoryMap_Shutdown(flags);
|
2016-05-20 04:17:17 +00:00
|
|
|
base = nullptr;
|
2013-12-09 12:45:17 +00:00
|
|
|
DEBUG_LOG(MEMMAP, "Memory system shut down.");
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 04:17:17 +00:00
|
|
|
bool IsActive() {
|
|
|
|
return base != nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-20 06:25:32 +00:00
|
|
|
// Wanting to avoid include pollution, MemMap.h is included a lot.
|
|
|
|
MemoryInitedLock::MemoryInitedLock()
|
|
|
|
{
|
|
|
|
g_shutdownLock.lock();
|
|
|
|
}
|
|
|
|
MemoryInitedLock::~MemoryInitedLock()
|
|
|
|
{
|
|
|
|
g_shutdownLock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryInitedLock Lock()
|
|
|
|
{
|
|
|
|
return MemoryInitedLock();
|
|
|
|
}
|
|
|
|
|
2014-11-03 15:36:17 +00:00
|
|
|
__forceinline static Opcode Read_Instruction(u32 address, bool resolveReplacements, Opcode inst)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2014-03-02 05:15:17 +00:00
|
|
|
if (!MIPS_IS_EMUHACK(inst.encoding)) {
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
2021-11-28 00:11:51 +00:00
|
|
|
// No mutex on jit access here, but we assume the caller has locked, if necessary.
|
2013-12-18 15:27:23 +00:00
|
|
|
if (MIPS_IS_RUNBLOCK(inst.encoding) && MIPSComp::jit) {
|
2016-05-07 19:35:12 +00:00
|
|
|
inst = MIPSComp::jit->GetOriginalOp(inst);
|
|
|
|
if (resolveReplacements && MIPS_IS_REPLACEMENT(inst)) {
|
|
|
|
u32 op;
|
|
|
|
if (GetReplacedOpAt(address, &op)) {
|
|
|
|
if (MIPS_IS_EMUHACK(op)) {
|
2017-03-06 12:10:23 +00:00
|
|
|
ERROR_LOG(MEMMAP, "WTF 1");
|
2016-05-07 19:35:12 +00:00
|
|
|
return Opcode(op);
|
2013-12-20 12:51:26 +00:00
|
|
|
} else {
|
2016-05-07 19:35:12 +00:00
|
|
|
return Opcode(op);
|
2013-12-20 12:51:26 +00:00
|
|
|
}
|
2016-05-07 19:35:12 +00:00
|
|
|
} else {
|
2017-03-06 12:10:23 +00:00
|
|
|
ERROR_LOG(MEMMAP, "Replacement, but no replacement op? %08x", inst.encoding);
|
2013-12-20 12:51:26 +00:00
|
|
|
}
|
2013-04-29 03:59:13 +00:00
|
|
|
}
|
2016-05-07 19:35:12 +00:00
|
|
|
return inst;
|
2014-06-23 15:20:38 +00:00
|
|
|
} else if (resolveReplacements && MIPS_IS_REPLACEMENT(inst.encoding)) {
|
2013-12-18 10:42:19 +00:00
|
|
|
u32 op;
|
|
|
|
if (GetReplacedOpAt(address, &op)) {
|
2013-12-20 12:51:26 +00:00
|
|
|
if (MIPS_IS_EMUHACK(op)) {
|
2017-03-06 12:10:23 +00:00
|
|
|
ERROR_LOG(MEMMAP, "WTF 2");
|
2013-12-20 12:51:26 +00:00
|
|
|
return Opcode(op);
|
|
|
|
} else {
|
|
|
|
return Opcode(op);
|
|
|
|
}
|
2013-12-18 10:42:19 +00:00
|
|
|
} else {
|
|
|
|
return inst;
|
|
|
|
}
|
2013-04-26 22:22:18 +00:00
|
|
|
} else {
|
2012-11-01 15:19:01 +00:00
|
|
|
return inst;
|
2013-04-26 22:22:18 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2014-03-02 05:15:17 +00:00
|
|
|
Opcode Read_Instruction(u32 address, bool resolveReplacements)
|
|
|
|
{
|
|
|
|
Opcode inst = Opcode(Read_U32(address));
|
|
|
|
return Read_Instruction(address, resolveReplacements, inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
Opcode ReadUnchecked_Instruction(u32 address, bool resolveReplacements)
|
|
|
|
{
|
|
|
|
Opcode inst = Opcode(ReadUnchecked_U32(address));
|
|
|
|
return Read_Instruction(address, resolveReplacements, inst);
|
|
|
|
}
|
|
|
|
|
2013-08-24 21:43:49 +00:00
|
|
|
Opcode Read_Opcode_JIT(u32 address)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-12-18 15:27:23 +00:00
|
|
|
Opcode inst = Opcode(Read_U32(address));
|
2021-11-28 00:11:51 +00:00
|
|
|
// No mutex around jit access here, but we assume caller has if necessary.
|
2013-12-18 15:27:23 +00:00
|
|
|
if (MIPS_IS_RUNBLOCK(inst.encoding) && MIPSComp::jit) {
|
2016-05-07 15:37:19 +00:00
|
|
|
return MIPSComp::jit->GetOriginalOp(inst);
|
2013-12-18 15:27:23 +00:00
|
|
|
} else {
|
|
|
|
return inst;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WARNING! No checks!
|
|
|
|
// We assume that _Address is cached
|
2014-12-25 02:42:41 +00:00
|
|
|
void Write_Opcode_JIT(const u32 _Address, const Opcode& _Value)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-08-24 21:43:49 +00:00
|
|
|
Memory::WriteUnchecked_U32(_Value.encoding, _Address);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 16:12:30 +00:00
|
|
|
void Memset(const u32 _Address, const u8 _iValue, const u32 _iLength, const char *tag) {
|
2017-11-30 22:17:55 +00:00
|
|
|
if (IsValidRange(_Address, _iLength)) {
|
2022-07-20 10:40:22 +00:00
|
|
|
uint8_t *ptr = GetPointerWriteUnchecked(_Address);
|
2013-12-18 10:42:19 +00:00
|
|
|
memset(ptr, _iValue, _iLength);
|
2017-11-30 15:49:14 +00:00
|
|
|
} else {
|
2012-11-01 15:19:01 +00:00
|
|
|
for (size_t i = 0; i < _iLength; i++)
|
2012-11-05 14:41:37 +00:00
|
|
|
Write_U8(_iValue, (u32)(_Address + i));
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2018-05-02 01:11:57 +00:00
|
|
|
|
2021-03-13 16:12:30 +00:00
|
|
|
NotifyMemInfo(MemBlockFlags::WRITE, _Address, _iLength, tag, strlen(tag));
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2022-09-03 14:48:52 +00:00
|
|
|
|
|
|
|
void PSPPointerNotifyRW(int rw, uint32_t ptr, uint32_t bytes, const char * tag, size_t tagLen) {
|
|
|
|
if (MemBlockInfoDetailed(bytes)) {
|
|
|
|
if (rw & 1)
|
|
|
|
NotifyMemInfo(MemBlockFlags::WRITE, ptr, bytes, tag, tagLen);
|
|
|
|
if (rw & 2)
|
|
|
|
NotifyMemInfo(MemBlockFlags::READ, ptr, bytes, tag, tagLen);
|
|
|
|
}
|
|
|
|
}
|