Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIPSHooks for the Interpreter core #18848

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -2269,6 +2269,8 @@ add_library(${CoreLibName} ${CoreLinkType}
Core/MIPS/MIPSStackWalk.h
Core/MIPS/MIPSTables.cpp
Core/MIPS/MIPSTables.h
Core/MIPS/MIPSHooks.cpp
Core/MIPS/MIPSHooks.h
Core/MIPS/MIPSVFPUUtils.cpp
Core/MIPS/MIPSVFPUUtils.h
Core/MIPS/MIPSVFPUFallbacks.cpp
Expand Down
2 changes: 2 additions & 0 deletions Core/Core.vcxproj
Expand Up @@ -989,6 +989,7 @@
<ClCompile Include="MIPS\MIPSIntVFPU.cpp" />
<ClCompile Include="MIPS\MIPSTables.cpp" />
<ClCompile Include="MIPS\MIPSVFPUUtils.cpp" />
<ClCompile Include="MIPS\MIPSHooks.cpp" />
<ClCompile Include="MIPS\MIPS\MipsJit.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
Expand Down Expand Up @@ -1400,6 +1401,7 @@
<ClInclude Include="MIPS\MIPSIntVFPU.h" />
<ClInclude Include="MIPS\MIPSTables.h" />
<ClInclude Include="MIPS\MIPSVFPUUtils.h" />
<ClInclude Include="MIPS\MIPSHooks.h" />
<ClInclude Include="MIPS\MIPS\MipsJit.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
Expand Down
6 changes: 6 additions & 0 deletions Core/Core.vcxproj.filters
Expand Up @@ -129,6 +129,9 @@
<ClCompile Include="MIPS\MIPSCodeUtils.cpp">
<Filter>MIPS</Filter>
</ClCompile>
<ClCompile Include="MIPS\MIPSHooks.cpp">
<Filter>MIPS</Filter>
</ClCompile>
<ClCompile Include="MIPS\x86\CompALU.cpp">
<Filter>MIPS\x86</Filter>
</ClCompile>
Expand Down Expand Up @@ -1344,6 +1347,9 @@
<ClInclude Include="MIPS\MIPSCodeUtils.h">
<Filter>MIPS</Filter>
</ClInclude>
<ClInclude Include="MIPS\MIPSHooks.h">
<Filter>MIPS</Filter>
</ClInclude>
<ClInclude Include="MIPS\x86\Jit.h">
<Filter>MIPS\x86</Filter>
</ClInclude>
Expand Down
49 changes: 49 additions & 0 deletions Core/MIPS/MIPSHooks.cpp
@@ -0,0 +1,49 @@
// Copyright (c) 2024- 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
// the Free Software Foundation, version 2.0 or later versions.

// 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/.

#include "Core/MIPS/MIPSHooks.h"

#include <map>
#include <vector>


MIPSNames MIPSNameLookupTable;
static std::vector<std::pair<MIPSInstruction*, MIPSInterpretFunc>> stack;

namespace MIPSHooks {

void Reset() {
INFO_LOG(CPU, "Resetting the interpreter hooks");
for (auto&[instr, func] : stack) {
instr->interpret = func;
VERBOSE_LOG(CPU, "Resetting %s", instr->name);
}
stack.clear();
}

void Hook(const char* name, MIPSInterpretFunc func) {
auto current_handler = MIPSNameLookupTable.GetInstructionByName(name);
if (!current_handler) {
WARN_LOG(CPU, "Cannot setup a hook: unknown instruction '%s'", name);
return;
}

stack.emplace_back(current_handler, current_handler->interpret);
current_handler->interpret = func;
INFO_LOG(CPU, "Enabled a hook for %s", name);
}
}
59 changes: 59 additions & 0 deletions Core/MIPS/MIPSHooks.h
@@ -0,0 +1,59 @@
// Copyright (c) 2024- 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
// the Free Software Foundation, version 2.0 or later versions.

// 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/.

#pragma once

#include <map>

#include "Core/MIPS/MIPSTables.h"
#include "Common/Log.h"

class MIPSNames {
private:
std::map<std::string, MIPSInstruction*> name_lookup_table;
public:
template <size_t size>
void RegisterInstructions(MIPSInstruction (&table)[size]) {
for (uint32_t i = 0; i < size; ++i) {
// register the name if it's valid
if (table[i].name) {
name_lookup_table.emplace(table[i].name, &table[i]);
VERBOSE_LOG(CPU, "Registered %s", table[i].name);
}
}
}

MIPSInstruction* GetInstructionByName(std::string name) {
auto it = name_lookup_table.find(name);
if (it == name_lookup_table.end()) {
return nullptr;
}
return it->second;
}
};

extern MIPSNames MIPSNameLookupTable;

namespace MIPSHooks {
// Fills the MIPSNameLookupTable
void Init();
// Disables the hooks
void Reset();
// Tries to register a hook
void Hook(const char* name, MIPSInterpretFunc func);
}