mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-14 23:10:33 +02:00
The JITs used to find a block by overwriting its first instruction with an "emuhack" holding the offset into the code space, and replacement functions installed their own emuhack the same way. Anything that read code (savestates, the debugger, memory dumps, cheats, the function hasher, games reading their own code) had to translate them back. Blocks now live in a block shadow: one u32 per guest instruction, indexed by the PC masked with BLOCK_SHADOW_MASK (1GB on 64-bit, folding the kernel and uncached mirrors; 256MB on 32-bit, iOS and Switch, where address space is short), with the low bits cleared so a misaligned PC can't read across two entries. Only the ranges under memory views are committed, so a bad PC still faults in the dispatcher, and the shadow is reserved before the memory map so it can't land in a gap of the 4GB window. Entries hold full 32-bit offsets, lifting the 16MB limit. The dispatchers on every backend, and the IR interpreter's loop, look blocks up there. A hooked instruction without a compiled block gets the JIT's hook value in its entry: the offset of the dispatcher's compile path (1 for the interpreters, the IR arena keeping that offset empty), so dispatching there compiles a block, and the frontends substitute the hook. Compiling replaces the entry with the block's, and destroying the block puts the hook value back. The hook value is rewritten when the JIT changes or regenerates its dispatcher. Hooks live in a map keyed by the masked address, and are dropped when the game overwrites the hooked instruction, or when an icache invalidation finds the function's hash (immediates masked, as when matching it) changed. Those checks run when queued invalidations are processed, since cheats and the debugger queue them before writing. Installing and removing hooks invalidates deferred, as it happens in HLE calls that can run inside a block. The cache instruction goes through MIPSState, so it drops changed hooks too. With clean memory, the save/clear/restore around savestates, memory reads and dumps goes away, as do the source-range invalidations in the memcpy replacements, sceDmac and the cheats. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
263 lines
7.3 KiB
C++
263 lines
7.3 KiB
C++
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <mutex>
|
|
#include "Common/Data/Encoding/Utf8.h"
|
|
#include "Core/Core.h"
|
|
#include "Core/System.h"
|
|
#include "Core/HLE/ReplaceTables.h"
|
|
#include "Core/MemMap.h"
|
|
#include "Core/MIPS/MIPSDebugInterface.h"
|
|
#include "Windows/Debugger/DumpMemoryWindow.h"
|
|
#include "Windows/resource.h"
|
|
#include "Windows/W32Util/ShellUtil.h"
|
|
|
|
DumpMemoryWindow* DumpMemoryWindow::bp;
|
|
|
|
void DumpMemoryWindow::HandleBrowseClick(HWND hwnd) {
|
|
std::wstring buffer;
|
|
HWND filenameWnd = GetDlgItem(hwnd, IDC_DUMP_FILENAME);
|
|
buffer.resize(GetWindowTextLengthW(filenameWnd) + 1);
|
|
GetWindowTextW(filenameWnd, &buffer[0], (int)buffer.size());
|
|
std::string fn = ConvertWStringToUTF8(buffer);
|
|
|
|
bool result = W32Util::BrowseForFileName(false, hwnd, L"Select filename", nullptr, L"dump.bin", nullptr, nullptr, fn);
|
|
if (result) {
|
|
filenameChosen_ = true;
|
|
buffer = ConvertUTF8ToWString(fn);
|
|
SetWindowTextW(filenameWnd, buffer.c_str());
|
|
}
|
|
}
|
|
|
|
INT_PTR CALLBACK DumpMemoryWindow::dlgFunc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
|
|
{
|
|
switch (iMsg)
|
|
{
|
|
case WM_INITDIALOG:
|
|
bp->changeMode(hwnd,bp->selectedMode);
|
|
return TRUE;
|
|
case WM_COMMAND:
|
|
switch (LOWORD(wParam))
|
|
{
|
|
case IDC_DUMP_USERMEMORY:
|
|
switch (HIWORD(wParam))
|
|
{
|
|
case BN_CLICKED:
|
|
bp->changeMode(hwnd,MODE_RAM);
|
|
break;
|
|
}
|
|
break;
|
|
case IDC_DUMP_VRAM:
|
|
switch (HIWORD(wParam))
|
|
{
|
|
case BN_CLICKED:
|
|
bp->changeMode(hwnd,MODE_VRAM);
|
|
break;
|
|
}
|
|
break;
|
|
case IDC_DUMP_SCRATCHPAD:
|
|
switch (HIWORD(wParam))
|
|
{
|
|
case BN_CLICKED:
|
|
bp->changeMode(hwnd,MODE_SCRATCHPAD);
|
|
break;
|
|
}
|
|
break;
|
|
case IDC_DUMP_CUSTOMRANGE:
|
|
switch (HIWORD(wParam))
|
|
{
|
|
case BN_CLICKED:
|
|
bp->changeMode(hwnd,MODE_CUSTOM);
|
|
break;
|
|
}
|
|
break;
|
|
case IDC_DUMP_BROWSEFILENAME:
|
|
switch (HIWORD(wParam))
|
|
{
|
|
case BN_CLICKED:
|
|
bp->HandleBrowseClick(hwnd);
|
|
break;
|
|
}
|
|
break;
|
|
case IDOK:
|
|
if (bp->fetchDialogData(hwnd)) {
|
|
|
|
// Route the actual memory dump to the CPU thread instead of forcing the emulator
|
|
// to pause and poking at it directly from this GUI thread - see
|
|
// Core_RunOnCPUThread() in Core.h. MessageBoxA() is modal, so it stays outside the
|
|
// queued callback.
|
|
enum class Outcome { NotInited, OpenFailed, Success } outcome = Outcome::NotInited;
|
|
Core_RunOnCPUThread([&] {
|
|
CoreShutdownLock coreLock = Core_LockAgainstShutdown();
|
|
if (!PSP_IsInited())
|
|
return;
|
|
|
|
FILE *output = _wfopen(bp->fileName_.c_str(), L"wb");
|
|
if (output == nullptr) {
|
|
outcome = Outcome::OpenFailed;
|
|
return;
|
|
}
|
|
|
|
fwrite(Memory::GetPointerOrException(bp->start), 1, bp->size, output);
|
|
|
|
fclose(output);
|
|
outcome = Outcome::Success;
|
|
});
|
|
|
|
if (outcome == Outcome::OpenFailed) {
|
|
char errorMessage[2048];
|
|
snprintf(errorMessage, sizeof(errorMessage), "Could not open file \"%S\".", bp->fileName_.c_str());
|
|
MessageBoxA(hwnd, errorMessage, "Error", MB_OK);
|
|
} else if (outcome == Outcome::Success) {
|
|
MessageBoxA(hwnd, "Done.", "Information", MB_OK);
|
|
EndDialog(hwnd, true);
|
|
}
|
|
}
|
|
break;
|
|
case IDCANCEL:
|
|
EndDialog(hwnd,false);
|
|
break;
|
|
}
|
|
break;
|
|
case WM_KEYDOWN:
|
|
|
|
break;
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
static bool isInInterval(u32 start, u32 end, u32 value)
|
|
{
|
|
return start <= value && value < end;
|
|
}
|
|
|
|
bool DumpMemoryWindow::fetchDialogData(HWND hwnd)
|
|
{
|
|
char str[256],errorMessage[256];
|
|
PostfixExpression exp;
|
|
|
|
// parse start address
|
|
GetWindowTextA(GetDlgItem(hwnd,IDC_DUMP_STARTADDRESS),str,256);
|
|
if (initExpression(cpu, str,exp) == false
|
|
|| parseExpression(cpu, exp,start) == false)
|
|
{
|
|
snprintf(errorMessage, sizeof(errorMessage), "Invalid address expression \"%s\".",str);
|
|
MessageBoxA(hwnd,errorMessage,"Error",MB_OK);
|
|
return false;
|
|
}
|
|
|
|
// parse size
|
|
GetWindowTextA(GetDlgItem(hwnd,IDC_DUMP_SIZE),str,256);
|
|
if (initExpression(cpu, str,exp) == false
|
|
|| parseExpression(cpu, exp,size) == false)
|
|
{
|
|
snprintf(errorMessage, sizeof(errorMessage), "Invalid size expression \"%s\".",str);
|
|
MessageBoxA(hwnd,errorMessage,"Error",MB_OK);
|
|
return false;
|
|
}
|
|
|
|
if (size == 0)
|
|
{
|
|
MessageBoxA(hwnd,"Invalid size 0.","Error",MB_OK);
|
|
return false;
|
|
}
|
|
|
|
// get filename
|
|
fileName_.resize(GetWindowTextLengthW(GetDlgItem(hwnd, IDC_DUMP_FILENAME)) + 1);
|
|
GetWindowTextW(GetDlgItem(hwnd, IDC_DUMP_FILENAME), &fileName_[0], (int)fileName_.size());
|
|
if (fileName_.size() == 0)
|
|
return false;
|
|
|
|
// now check if data makes sense...
|
|
bool invalidSize = false;
|
|
bool invalidAddress = false;
|
|
if (isInInterval(PSP_GetScratchpadMemoryBase(),PSP_GetScratchpadMemoryEnd(),start))
|
|
{
|
|
invalidSize = !isInInterval(PSP_GetScratchpadMemoryBase(),PSP_GetScratchpadMemoryEnd(),start+size-1);
|
|
} else if (isInInterval(PSP_GetVidMemBase(),PSP_GetVidMemEnd(),start))
|
|
{
|
|
invalidSize = !isInInterval(PSP_GetVidMemBase(),PSP_GetVidMemEnd(),start+size-1);
|
|
} else if (isInInterval(PSP_GetKernelMemoryBase(),PSP_GetUserMemoryEnd(),start))
|
|
{
|
|
invalidSize = !isInInterval(PSP_GetKernelMemoryBase(),PSP_GetUserMemoryEnd(),start+size-1);
|
|
} else
|
|
{
|
|
invalidAddress = true;
|
|
}
|
|
|
|
if (invalidAddress)
|
|
{
|
|
snprintf(errorMessage, sizeof(errorMessage), "Invalid address 0x%08X.",start);
|
|
MessageBoxA(hwnd,errorMessage,"Error",MB_OK);
|
|
return false;
|
|
} else if (invalidSize)
|
|
{
|
|
snprintf(errorMessage, sizeof(errorMessage), "Invalid end address 0x%08X.",start+size);
|
|
MessageBoxA(hwnd,errorMessage,"Error",MB_OK);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void DumpMemoryWindow::changeMode(HWND hwnd, Mode newMode)
|
|
{
|
|
char buffer[128];
|
|
selectedMode = newMode;
|
|
|
|
SendMessage(GetDlgItem(hwnd,IDC_DUMP_USERMEMORY),BM_SETCHECK,selectedMode == MODE_RAM ? BST_CHECKED : BST_UNCHECKED,0);
|
|
SendMessage(GetDlgItem(hwnd,IDC_DUMP_VRAM),BM_SETCHECK,selectedMode == MODE_VRAM ? BST_CHECKED : BST_UNCHECKED,0);
|
|
SendMessage(GetDlgItem(hwnd,IDC_DUMP_SCRATCHPAD),BM_SETCHECK,selectedMode == MODE_SCRATCHPAD ? BST_CHECKED : BST_UNCHECKED,0);
|
|
SendMessage(GetDlgItem(hwnd,IDC_DUMP_CUSTOMRANGE),BM_SETCHECK,selectedMode == MODE_CUSTOM ? BST_CHECKED : BST_UNCHECKED,0);
|
|
|
|
if (selectedMode == MODE_CUSTOM)
|
|
{
|
|
EnableWindow(GetDlgItem(hwnd,IDC_DUMP_STARTADDRESS),TRUE);
|
|
EnableWindow(GetDlgItem(hwnd,IDC_DUMP_SIZE),TRUE);
|
|
|
|
if (filenameChosen_ == false)
|
|
SetWindowTextA(GetDlgItem(hwnd,IDC_DUMP_FILENAME),"Custom.dump");
|
|
} else {
|
|
u32 start = 0, size = 0;
|
|
const char *defaultFileName = "";
|
|
|
|
switch (selectedMode) {
|
|
case MODE_RAM:
|
|
start = PSP_GetUserMemoryBase();
|
|
size = PSP_GetUserMemoryEnd()-start;
|
|
defaultFileName = "RAM.dump";
|
|
break;
|
|
case MODE_VRAM:
|
|
start = PSP_GetVidMemBase();
|
|
size = PSP_GetVidMemEnd()-start;
|
|
defaultFileName = "VRAM.dump";
|
|
break;
|
|
case MODE_SCRATCHPAD:
|
|
start = PSP_GetScratchpadMemoryBase();
|
|
size = PSP_GetScratchpadMemoryEnd()-start;
|
|
defaultFileName = "Scratchpad.dump";
|
|
break;
|
|
case MODE_CUSTOM:
|
|
break;
|
|
}
|
|
|
|
snprintf(buffer, sizeof(buffer), "0x%08X", start);
|
|
SetWindowTextA(GetDlgItem(hwnd,IDC_DUMP_STARTADDRESS),buffer);
|
|
EnableWindow(GetDlgItem(hwnd,IDC_DUMP_STARTADDRESS),FALSE);
|
|
|
|
snprintf(buffer, sizeof(buffer), "0x%08X", size);
|
|
SetWindowTextA(GetDlgItem(hwnd,IDC_DUMP_SIZE),buffer);
|
|
EnableWindow(GetDlgItem(hwnd,IDC_DUMP_SIZE),FALSE);
|
|
|
|
if (filenameChosen_ == false)
|
|
SetWindowTextA(GetDlgItem(hwnd,IDC_DUMP_FILENAME),defaultFileName);
|
|
}
|
|
}
|
|
|
|
bool DumpMemoryWindow::exec()
|
|
{
|
|
bp = this;
|
|
bool result = DialogBoxParam(GetModuleHandle(0),MAKEINTRESOURCE(IDD_DUMPMEMORY),parentHwnd,dlgFunc,(LPARAM)this) != 0;
|
|
return result;
|
|
}
|