Skip to content

Commit

Permalink
PPCCache: Avoid Global System Accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
mitaclaw committed Apr 9, 2024
1 parent db0cd82 commit cf74c0d
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 104 deletions.
8 changes: 8 additions & 0 deletions Source/Core/Common/x64Emitter.h
Expand Up @@ -1082,6 +1082,14 @@ class XEmitter
ABI_CallFunction(func);
}

template <typename FunctionPointer>
void ABI_CallFunctionPP(FunctionPointer func, const void* param1, const void* param2)
{
MOV(64, R(ABI_PARAM1), Imm64(reinterpret_cast<u64>(param1)));
MOV(64, R(ABI_PARAM2), Imm64(reinterpret_cast<u64>(param2)));
ABI_CallFunction(func);
}

template <typename FunctionPointer>
void ABI_CallFunctionPC(FunctionPointer func, const void* param1, u32 param2)
{
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/Core/GeckoCode.cpp
Expand Up @@ -208,9 +208,11 @@ static Installation InstallCodeHandlerLocked(const Core::CPUThreadGuard& guard)

// Invalidate the icache and any asm codes
auto& ppc_state = guard.GetSystem().GetPPCState();
auto& memory = guard.GetSystem().GetMemory();
auto& jit_interface = guard.GetSystem().GetJitInterface();
for (u32 j = 0; j < (INSTALLER_END_ADDRESS - INSTALLER_BASE_ADDRESS); j += 32)
{
ppc_state.iCache.Invalidate(INSTALLER_BASE_ADDRESS + j);
ppc_state.iCache.Invalidate(memory, jit_interface, INSTALLER_BASE_ADDRESS + j);
}
return Installation::Installed;
}
Expand Down
20 changes: 14 additions & 6 deletions Source/Core/Core/HLE/HLE.cpp
Expand Up @@ -66,12 +66,14 @@ constexpr std::array<Hook, 23> os_patches{{
void Patch(Core::System& system, u32 addr, std::string_view func_name)
{
auto& ppc_state = system.GetPPCState();
auto& memory = system.GetMemory();
auto& jit_interface = system.GetJitInterface();
for (u32 i = 1; i < os_patches.size(); ++i)
{
if (os_patches[i].name == func_name)
{
s_hooked_addresses[addr] = i;
ppc_state.iCache.Invalidate(addr);
ppc_state.iCache.Invalidate(memory, jit_interface, addr);
return;
}
}
Expand Down Expand Up @@ -108,14 +110,16 @@ void PatchFunctions(Core::System& system)
{
auto& power_pc = system.GetPowerPC();
auto& ppc_state = power_pc.GetPPCState();
auto& memory = system.GetMemory();
auto& jit_interface = system.GetJitInterface();
auto& ppc_symbol_db = power_pc.GetSymbolDB();

// Remove all hooks that aren't fixed address hooks
for (auto i = s_hooked_addresses.begin(); i != s_hooked_addresses.end();)
{
if (os_patches[i->second].flags != HookFlag::Fixed)
{
ppc_state.iCache.Invalidate(i->first);
ppc_state.iCache.Invalidate(memory, jit_interface, i->first);
i = s_hooked_addresses.erase(i);
}
else
Expand All @@ -135,7 +139,7 @@ void PatchFunctions(Core::System& system)
for (u32 addr = symbol->address; addr < symbol->address + symbol->size; addr += 4)
{
s_hooked_addresses[addr] = i;
ppc_state.iCache.Invalidate(addr);
ppc_state.iCache.Invalidate(memory, jit_interface, addr);
}
INFO_LOG_FMT(OSHLE, "Patching {} {:08x}", os_patches[i].name, symbol->address);
}
Expand Down Expand Up @@ -234,6 +238,8 @@ u32 UnPatch(Core::System& system, std::string_view patch_name)

auto& power_pc = system.GetPowerPC();
auto& ppc_state = power_pc.GetPPCState();
auto& memory = system.GetMemory();
auto& jit_interface = system.GetJitInterface();

if (patch->flags == HookFlag::Fixed)
{
Expand All @@ -245,7 +251,7 @@ u32 UnPatch(Core::System& system, std::string_view patch_name)
if (i->second == patch_idx)
{
addr = i->first;
ppc_state.iCache.Invalidate(i->first);
ppc_state.iCache.Invalidate(memory, jit_interface, i->first);
i = s_hooked_addresses.erase(i);
}
else
Expand All @@ -263,7 +269,7 @@ u32 UnPatch(Core::System& system, std::string_view patch_name)
for (u32 addr = symbol->address; addr < symbol->address + symbol->size; addr += 4)
{
s_hooked_addresses.erase(addr);
ppc_state.iCache.Invalidate(addr);
ppc_state.iCache.Invalidate(memory, jit_interface, addr);
}
return symbol->address;
}
Expand All @@ -274,6 +280,8 @@ u32 UnPatch(Core::System& system, std::string_view patch_name)
u32 UnpatchRange(Core::System& system, u32 start_addr, u32 end_addr)
{
auto& ppc_state = system.GetPPCState();
auto& memory = system.GetMemory();
auto& jit_interface = system.GetJitInterface();

u32 count = 0;

Expand All @@ -282,7 +290,7 @@ u32 UnpatchRange(Core::System& system, u32 start_addr, u32 end_addr)
{
INFO_LOG_FMT(OSHLE, "Unpatch HLE hooks [{:08x};{:08x}): {} at {:08x}", start_addr, end_addr,
os_patches[i->second].name, i->first);
ppc_state.iCache.Invalidate(i->first);
ppc_state.iCache.Invalidate(memory, jit_interface, i->first);
i = s_hooked_addresses.erase(i);
count += 1;
}
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/Core/HLE/HLE_Misc.cpp
Expand Up @@ -36,6 +36,7 @@ void GeckoCodeHandlerICacheFlush(const Core::CPUThreadGuard& guard)
{
auto& system = guard.GetSystem();
auto& ppc_state = system.GetPPCState();
auto& jit_interface = system.GetJitInterface();

// Work around the codehandler not properly invalidating the icache, but
// only the first few frames.
Expand All @@ -54,7 +55,7 @@ void GeckoCodeHandlerICacheFlush(const Core::CPUThreadGuard& guard)
}
PowerPC::MMU::HostWrite_U32(guard, gch_gameid + 1, Gecko::INSTALLER_BASE_ADDRESS);

ppc_state.iCache.Reset();
ppc_state.iCache.Reset(jit_interface);
}

// Because Dolphin messes around with the CPU state instead of patching the game binary, we
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/Core/PowerPC/GDBStub.cpp
Expand Up @@ -1016,7 +1016,8 @@ void ProcessCommands(bool loop_until_continue)

WriteMemory(guard);
auto& ppc_state = system.GetPPCState();
ppc_state.iCache.Reset();
auto& jit_interface = system.GetJitInterface();
ppc_state.iCache.Reset(jit_interface);
Host_UpdateDisasmDialog();
break;
}
Expand Down
Expand Up @@ -631,7 +631,9 @@ void Interpreter::icbi(Interpreter& interpreter, UGeckoInstruction inst)
// TODO: Raise DSI if translation fails (except for direct-store segments).
auto& ppc_state = interpreter.m_ppc_state;
const u32 address = Helper_Get_EA_X(ppc_state, inst);
ppc_state.iCache.Invalidate(address);
auto& memory = interpreter.m_system.GetMemory();
auto& jit_interface = interpreter.m_system.GetJitInterface();
ppc_state.iCache.Invalidate(memory, jit_interface, address);
}

void Interpreter::lbzux(Interpreter& interpreter, UGeckoInstruction inst)
Expand Down
Expand Up @@ -358,7 +358,8 @@ void Interpreter::mtspr(Interpreter& interpreter, UGeckoInstruction inst)
INFO_LOG_FMT(POWERPC, "Flush Instruction Cache! ICE={}", HID0(ppc_state).ICE);
// this is rather slow
// most games do it only once during initialization
ppc_state.iCache.Reset();
auto& jit_interface = interpreter.m_system.GetJitInterface();
ppc_state.iCache.Reset(jit_interface);
}
}
break;
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Core/PowerPC/Jit64/Jit_SystemRegisters.cpp
Expand Up @@ -216,9 +216,9 @@ void Jit64::UpdateFPExceptionSummary(X64Reg fpscr, X64Reg tmp1, X64Reg tmp2)
OR(32, R(fpscr), R(tmp1));
}

static void DoICacheReset(PowerPC::PowerPCState& ppc_state)
static void DoICacheReset(PowerPC::PowerPCState& ppc_state, JitInterface& jit_interface)
{
ppc_state.iCache.Reset();
ppc_state.iCache.Reset(jit_interface);
}

void Jit64::mtspr(UGeckoInstruction inst)
Expand Down Expand Up @@ -286,7 +286,7 @@ void Jit64::mtspr(UGeckoInstruction inst)
FixupBranch dont_reset_icache = J_CC(CC_NC);
BitSet32 regs = CallerSavedRegistersInUse();
ABI_PushRegistersAndAdjustStack(regs, 0);
ABI_CallFunctionP(DoICacheReset, &m_ppc_state);
ABI_CallFunctionPP(DoICacheReset, &m_ppc_state, &m_system.GetJitInterface());
ABI_PopRegistersAndAdjustStack(regs, 0);
SetJumpTarget(dont_reset_icache);
return;
Expand Down
18 changes: 9 additions & 9 deletions Source/Core/Core/PowerPC/MMU.cpp
Expand Up @@ -220,7 +220,7 @@ T MMU::ReadFromHardware(u32 em_address)
}
else
{
m_ppc_state.dCache.Read(em_address, &value, sizeof(T),
m_ppc_state.dCache.Read(m_memory, em_address, &value, sizeof(T),
HID0(m_ppc_state).DLOCK || flag != XCheckTLBFlag::Read);
}

Expand All @@ -239,7 +239,7 @@ T MMU::ReadFromHardware(u32 em_address)
}
else
{
m_ppc_state.dCache.Read(em_address + 0x10000000, &value, sizeof(T),
m_ppc_state.dCache.Read(m_memory, em_address + 0x10000000, &value, sizeof(T),
HID0(m_ppc_state).DLOCK || flag != XCheckTLBFlag::Read);
}

Expand Down Expand Up @@ -412,7 +412,7 @@ void MMU::WriteToHardware(u32 em_address, const u32 data, const u32 size)
em_address &= m_memory.GetRamMask();

if (m_ppc_state.m_enable_dcache && !wi)
m_ppc_state.dCache.Write(em_address, &swapped_data, size, HID0(m_ppc_state).DLOCK);
m_ppc_state.dCache.Write(m_memory, em_address, &swapped_data, size, HID0(m_ppc_state).DLOCK);

if (!m_ppc_state.m_enable_dcache || wi || flag != XCheckTLBFlag::Write)
std::memcpy(&m_memory.GetRAM()[em_address], &swapped_data, size);
Expand All @@ -427,7 +427,7 @@ void MMU::WriteToHardware(u32 em_address, const u32 data, const u32 size)

if (m_ppc_state.m_enable_dcache && !wi)
{
m_ppc_state.dCache.Write(em_address + 0x10000000, &swapped_data, size,
m_ppc_state.dCache.Write(m_memory, em_address + 0x10000000, &swapped_data, size,
HID0(m_ppc_state).DLOCK);
}

Expand Down Expand Up @@ -497,7 +497,7 @@ TryReadInstResult MMU::TryReadInstruction(u32 address)
}
else
{
hex = m_ppc_state.iCache.ReadInstruction(address);
hex = m_ppc_state.iCache.ReadInstruction(m_memory, m_ppc_state, address);
}
return TryReadInstResult{true, from_bat, hex, address};
}
Expand Down Expand Up @@ -1137,7 +1137,7 @@ void MMU::StoreDCacheLine(u32 address)
}

if (m_ppc_state.m_enable_dcache)
m_ppc_state.dCache.Store(address);
m_ppc_state.dCache.Store(m_memory, address);
}

void MMU::InvalidateDCacheLine(u32 address)
Expand All @@ -1159,7 +1159,7 @@ void MMU::InvalidateDCacheLine(u32 address)
}

if (m_ppc_state.m_enable_dcache)
m_ppc_state.dCache.Invalidate(address);
m_ppc_state.dCache.Invalidate(m_memory, address);
}

void MMU::FlushDCacheLine(u32 address)
Expand All @@ -1183,7 +1183,7 @@ void MMU::FlushDCacheLine(u32 address)
}

if (m_ppc_state.m_enable_dcache)
m_ppc_state.dCache.Flush(address);
m_ppc_state.dCache.Flush(m_memory, address);
}

void MMU::TouchDCacheLine(u32 address, bool store)
Expand All @@ -1207,7 +1207,7 @@ void MMU::TouchDCacheLine(u32 address, bool store)
}

if (m_ppc_state.m_enable_dcache)
m_ppc_state.dCache.Touch(address, store);
m_ppc_state.dCache.Touch(m_memory, address, store);
}

u32 MMU::IsOptimizableMMIOAccess(u32 address, u32 access_size) const
Expand Down

0 comments on commit cf74c0d

Please sign in to comment.