From db5fa233a8862c48d5481711119a7d1063c3b529 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Fri, 25 Jan 2013 11:38:09 -0800 Subject: [PATCH 1/8] Make sure we don't mark a reg dirty on noop. --- Core/MIPS/x86/CompALU.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Core/MIPS/x86/CompALU.cpp b/Core/MIPS/x86/CompALU.cpp index 104d14b64b..da90e46ea4 100644 --- a/Core/MIPS/x86/CompALU.cpp +++ b/Core/MIPS/x86/CompALU.cpp @@ -184,10 +184,12 @@ namespace MIPSComp // Optimize out + 0 and | 0. if ((doImm == &RType3_ImmAdd || doImm == &RType3_ImmOr) && (rs == 0 || rt == 0)) { - gpr.BindToRegister(rd, rd == rs || rd == rt, true); int rsource = rt == 0 ? rs : rt; if (rsource != rd) + { + gpr.BindToRegister(rd, false, true); MOV(32, gpr.R(rd), gpr.R(rsource)); + } } else { From 4bb515cab033b9f04486907718d0f2143163ebb4 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Fri, 25 Jan 2013 22:34:08 -0800 Subject: [PATCH 2/8] Don't log an incorrect pc for bad jit mem access. --- Core/MemMapFunctions.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Core/MemMapFunctions.cpp b/Core/MemMapFunctions.cpp index 1f9b17bef7..ee2370c0da 100644 --- a/Core/MemMapFunctions.cpp +++ b/Core/MemMapFunctions.cpp @@ -83,7 +83,11 @@ inline void ReadFromHardware(T &var, const u32 address) } else { - WARN_LOG(MEMMAP, "ReadFromHardware: Invalid address %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); + if (g_Config.iCpuCore == CPU_JIT) { + WARN_LOG(MEMMAP, "ReadFromHardware: Invalid address %08x", address); + } else { + WARN_LOG(MEMMAP, "ReadFromHardware: Invalid address %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); + } if (!g_Config.bIgnoreBadMemAccess) { Core_EnableStepping(true); host->SetDebugMode(true); @@ -111,7 +115,11 @@ inline void WriteToHardware(u32 address, const T data) } else { - WARN_LOG(MEMMAP, "WriteToHardware: Invalid address %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); + if (g_Config.iCpuCore == CPU_JIT) { + WARN_LOG(MEMMAP, "WriteToHardware: Invalid address %08x", address); + } else { + WARN_LOG(MEMMAP, "WriteToHardware: Invalid address %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); + } if (!g_Config.bIgnoreBadMemAccess) { Core_EnableStepping(true); host->SetDebugMode(true); From 34183839179f6afbfdba5c427f8ea3d788bf2163 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Fri, 25 Jan 2013 22:52:51 -0800 Subject: [PATCH 3/8] Immediately break on bad mem access in jit slowmem. --- Core/MIPS/x86/CompFPU.cpp | 20 ++++++++++++++++++++ Core/MIPS/x86/CompLoadStore.cpp | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/Core/MIPS/x86/CompFPU.cpp b/Core/MIPS/x86/CompFPU.cpp index 59e4f28eb5..a9edeb3c17 100644 --- a/Core/MIPS/x86/CompFPU.cpp +++ b/Core/MIPS/x86/CompFPU.cpp @@ -164,6 +164,16 @@ void Jit::Comp_FPULS(u32 op) MOV(32, M((void *)&ssLoadStoreTemp), R(EAX)); MOVSS(fpr.RX(ft), M((void *)&ssLoadStoreTemp)); + // Should we check the core state? + if (!g_Config.bIgnoreBadMemAccess) + { + CMP(32, M((void*)&coreState), Imm32(0)); + FixupBranch skip2 = J_CC(CC_E); + MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); + WriteSyscallExit(); + SetJumpTarget(skip2); + } + SetJumpTarget(skip); } else @@ -246,6 +256,16 @@ void Jit::Comp_FPULS(u32 op) MOVSS(M((void *)&ssLoadStoreTemp), fpr.RX(ft)); ABI_CallFunctionAA(thunks.ProtectFunction((void *) &Memory::Write_U32, 2), M((void *)&ssLoadStoreTemp), R(EAX)); + // Should we check the core state? + if (!g_Config.bIgnoreBadMemAccess) + { + CMP(32, M((void*)&coreState), Imm32(0)); + FixupBranch skip2 = J_CC(CC_E); + MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); + WriteSyscallExit(); + SetJumpTarget(skip2); + } + SetJumpTarget(skip); } else diff --git a/Core/MIPS/x86/CompLoadStore.cpp b/Core/MIPS/x86/CompLoadStore.cpp index eec20c9539..0623d4fcde 100644 --- a/Core/MIPS/x86/CompLoadStore.cpp +++ b/Core/MIPS/x86/CompLoadStore.cpp @@ -112,6 +112,16 @@ namespace MIPSComp ABI_CallFunctionA(thunks.ProtectFunction(safeFunc, 1), R(EAX)); (this->*mov)(32, bits, gpr.RX(rt), R(EAX)); + // Should we check the core state? + if (!g_Config.bIgnoreBadMemAccess) + { + CMP(32, M((void*)&coreState), Imm32(0)); + FixupBranch skip2 = J_CC(CC_E); + MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); + WriteSyscallExit(); + SetJumpTarget(skip2); + } + SetJumpTarget(skip); } else @@ -218,6 +228,16 @@ namespace MIPSComp LEA(32, EAX, MDisp(addr, offset)); ABI_CallFunctionAA(thunks.ProtectFunction(safeFunc, 2), gpr.R(rt), R(EAX)); + // Should we check the core state? + if (!g_Config.bIgnoreBadMemAccess) + { + CMP(32, M((void*)&coreState), Imm32(0)); + FixupBranch skip2 = J_CC(CC_E); + MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); + WriteSyscallExit(); + SetJumpTarget(skip2); + } + SetJumpTarget(skip); } else From b7ef3e7bef0ce7b5ca601177565ffe8ee2e14364 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Fri, 25 Jan 2013 23:06:43 -0800 Subject: [PATCH 4/8] Make sure to log / check bad immediate mem access. Although, theoretically, this should never happen. Also, definitely time to refactor. --- Core/MIPS/x86/CompFPU.cpp | 32 ++++++++++++++++++++++++++++++-- Core/MIPS/x86/CompLoadStore.cpp | 31 ++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Core/MIPS/x86/CompFPU.cpp b/Core/MIPS/x86/CompFPU.cpp index a9edeb3c17..68df48e328 100644 --- a/Core/MIPS/x86/CompFPU.cpp +++ b/Core/MIPS/x86/CompFPU.cpp @@ -113,8 +113,20 @@ void Jit::Comp_FPULS(u32 op) } else { - MOV(32, M((void *)ssLoadStoreTemp), Imm32(0)); - MOVSS(fpr.RX(ft), M((void *)ssLoadStoreTemp)); + MOV(32, R(EAX), Imm32(gpr.R(rs).GetImmValue() + offset)); + ABI_CallFunctionA(thunks.ProtectFunction((void *) &Memory::Read_U32, 1), R(EAX)); + MOV(32, M((void *)&ssLoadStoreTemp), R(EAX)); + MOVSS(fpr.RX(ft), M((void *)&ssLoadStoreTemp)); + + // Should we check the core state? + if (!g_Config.bIgnoreBadMemAccess) + { + CMP(32, M((void*)&coreState), Imm32(0)); + FixupBranch skip2 = J_CC(CC_E); + MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); + WriteSyscallExit(); + SetJumpTarget(skip2); + } } } else @@ -209,6 +221,22 @@ void Jit::Comp_FPULS(u32 op) MOVSS(MDisp(RBX, gpr.R(rs).GetImmValue() + offset), fpr.RX(ft)); #endif } + else + { + MOV(32, R(EAX), Imm32(gpr.R(rs).GetImmValue() + offset)); + MOVSS(M((void *)&ssLoadStoreTemp), fpr.RX(ft)); + ABI_CallFunctionAA(thunks.ProtectFunction((void *) &Memory::Write_U32, 2), M((void *)&ssLoadStoreTemp), R(EAX)); + + // Should we check the core state? + if (!g_Config.bIgnoreBadMemAccess) + { + CMP(32, M((void*)&coreState), Imm32(0)); + FixupBranch skip2 = J_CC(CC_E); + MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); + WriteSyscallExit(); + SetJumpTarget(skip2); + } + } } else { diff --git a/Core/MIPS/x86/CompLoadStore.cpp b/Core/MIPS/x86/CompLoadStore.cpp index 0623d4fcde..2dcead88cd 100644 --- a/Core/MIPS/x86/CompLoadStore.cpp +++ b/Core/MIPS/x86/CompLoadStore.cpp @@ -63,7 +63,21 @@ namespace MIPSComp #endif } else - gpr.SetImmediate32(rt, 0); + { + MOV(32, R(EAX), Imm32(gpr.R(rs).GetImmValue() + offset)); + ABI_CallFunctionA(thunks.ProtectFunction(safeFunc, 1), R(EAX)); + (this->*mov)(32, bits, gpr.RX(rt), R(EAX)); + + // Should we check the core state? + if (!g_Config.bIgnoreBadMemAccess) + { + CMP(32, M((void*)&coreState), Imm32(0)); + FixupBranch skip2 = J_CC(CC_E); + MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); + WriteSyscallExit(); + SetJumpTarget(skip2); + } + } } else { @@ -175,6 +189,21 @@ namespace MIPSComp MOV(bits, MDisp(RBX, gpr.R(rs).GetImmValue() + offset), gpr.R(rt)); #endif } + else + { + MOV(32, R(EAX), Imm32(gpr.R(rs).GetImmValue() + offset)); + ABI_CallFunctionAA(thunks.ProtectFunction(safeFunc, 2), gpr.R(rt), R(EAX)); + + // Should we check the core state? + if (!g_Config.bIgnoreBadMemAccess) + { + CMP(32, M((void*)&coreState), Imm32(0)); + FixupBranch skip2 = J_CC(CC_E); + MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); + WriteSyscallExit(); + SetJumpTarget(skip2); + } + } } else { From 3e419f513a9693dd11c2fe71a7b612cac17db138 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 26 Jan 2013 08:42:34 -0800 Subject: [PATCH 5/8] Refactor jit safe memory reads without dup code. But, maybe too automagical... --- Core/MIPS/x86/CompFPU.cpp | 230 ++++--------------------------- Core/MIPS/x86/CompLoadStore.cpp | 232 +++----------------------------- Core/MIPS/x86/Jit.cpp | 194 ++++++++++++++++++++++++++ Core/MIPS/x86/Jit.h | 35 +++++ Core/MemMap.h | 2 + 5 files changed, 277 insertions(+), 416 deletions(-) diff --git a/Core/MIPS/x86/CompFPU.cpp b/Core/MIPS/x86/CompFPU.cpp index 68df48e328..1a60b6d611 100644 --- a/Core/MIPS/x86/CompFPU.cpp +++ b/Core/MIPS/x86/CompFPU.cpp @@ -96,222 +96,46 @@ void Jit::Comp_FPULS(u32 op) switch(op >> 26) { case 49: //FI(ft) = Memory::Read_U32(addr); break; //lwc1 - gpr.Lock(rs); - fpr.SpillLock(ft); - fpr.BindToRegister(ft, false, true); - - if (gpr.R(rs).IsImm()) { - void *data = Memory::GetPointer(gpr.R(rs).GetImmValue() + offset); - if (data) + gpr.Lock(rs); + fpr.SpillLock(ft); + fpr.BindToRegister(ft, false, true); + + JitSafeMem safe(this, rs, offset); + OpArg src; + if (safe.PrepareRead(src, (void *) &Memory::Read_U32)) + MOVSS(fpr.RX(ft), src); + if (safe.PrepareSlowRead((void *) &Memory::Read_U32)) { -#ifdef _M_IX86 - MOVSS(fpr.RX(ft), M(data)); -#else - MOVSS(fpr.RX(ft), MDisp(RBX, gpr.R(rs).GetImmValue() + offset)); -#endif - } - else - { - MOV(32, R(EAX), Imm32(gpr.R(rs).GetImmValue() + offset)); - ABI_CallFunctionA(thunks.ProtectFunction((void *) &Memory::Read_U32, 1), R(EAX)); MOV(32, M((void *)&ssLoadStoreTemp), R(EAX)); MOVSS(fpr.RX(ft), M((void *)&ssLoadStoreTemp)); - - // Should we check the core state? - if (!g_Config.bIgnoreBadMemAccess) - { - CMP(32, M((void*)&coreState), Imm32(0)); - FixupBranch skip2 = J_CC(CC_E); - MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); - WriteSyscallExit(); - SetJumpTarget(skip2); - } } + safe.WriteFinish(); + + gpr.UnlockAll(); + fpr.ReleaseSpillLocks(); } - else - { - // We may not even need to move into EAX as a temporary. - X64Reg addr; - if (gpr.R(rs).IsSimpleReg()) - { - // TODO: Maybe just add a check if it's away, don't mind copying to EAX instead... - gpr.BindToRegister(rs, true, false); - addr = gpr.RX(rs); - } - else - { - MOV(32, R(EAX), gpr.R(rs)); - addr = EAX; - } - - if (!g_Config.bFastMemory) - { - // Is it in physical ram? - CMP(32, R(addr), Imm32(0x08000000)); - FixupBranch tooLow = J_CC(CC_L); - CMP(32, R(addr), Imm32(0x0A000000)); - FixupBranch tooHigh = J_CC(CC_GE); - - const u8* safe = GetCodePtr(); -#ifdef _M_IX86 - MOVSS(fpr.RX(ft), MDisp(addr, (u32)Memory::base + offset)); -#else - MOVSS(fpr.RX(ft), MComplex(RBX, addr, SCALE_1, offset)); -#endif - - FixupBranch skip = J(); - SetJumpTarget(tooLow); - SetJumpTarget(tooHigh); - - // Might also be the scratchpad. - CMP(32, R(addr), Imm32(0x00010000)); - FixupBranch tooLow2 = J_CC(CC_L); - CMP(32, R(addr), Imm32(0x00014000)); - J_CC(CC_L, safe); - SetJumpTarget(tooLow2); - - LEA(32, EAX, MDisp(addr, offset)); - ABI_CallFunctionA(thunks.ProtectFunction((void *) &Memory::Read_U32, 1), R(EAX)); - MOV(32, M((void *)&ssLoadStoreTemp), R(EAX)); - MOVSS(fpr.RX(ft), M((void *)&ssLoadStoreTemp)); - - // Should we check the core state? - if (!g_Config.bIgnoreBadMemAccess) - { - CMP(32, M((void*)&coreState), Imm32(0)); - FixupBranch skip2 = J_CC(CC_E); - MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); - WriteSyscallExit(); - SetJumpTarget(skip2); - } - - SetJumpTarget(skip); - } - else - { -#ifdef _M_IX86 - // Need to modify it, too bad. - if (addr != EAX) - MOV(32, R(EAX), gpr.R(rs)); - AND(32, R(EAX), Imm32(Memory::MEMVIEW32_MASK)); - MOVSS(fpr.RX(ft), MDisp(EAX, (u32)Memory::base + offset)); -#else - MOVSS(fpr.RX(ft), MComplex(RBX, addr, SCALE_1, offset)); -#endif - } - } - - gpr.UnlockAll(); - fpr.ReleaseSpillLocks(); break; case 57: //Memory::Write_U32(FI(ft), addr); break; //swc1 - gpr.Lock(rs); - fpr.SpillLock(ft); - fpr.BindToRegister(ft, true, false); - - if (gpr.R(rs).IsImm()) { - void *data = Memory::GetPointer(gpr.R(rs).GetImmValue() + offset); - if (data) + gpr.Lock(rs); + fpr.SpillLock(ft); + fpr.BindToRegister(ft, true, false); + + JitSafeMem safe(this, rs, offset); + OpArg dest; + if (safe.PrepareWrite(dest)) + MOVSS(dest, fpr.RX(ft)); + if (safe.PrepareSlowWrite()) { -#ifdef _M_IX86 - MOVSS(M(data), fpr.RX(ft)); -#else - MOVSS(MDisp(RBX, gpr.R(rs).GetImmValue() + offset), fpr.RX(ft)); -#endif - } - else - { - MOV(32, R(EAX), Imm32(gpr.R(rs).GetImmValue() + offset)); MOVSS(M((void *)&ssLoadStoreTemp), fpr.RX(ft)); - ABI_CallFunctionAA(thunks.ProtectFunction((void *) &Memory::Write_U32, 2), M((void *)&ssLoadStoreTemp), R(EAX)); - - // Should we check the core state? - if (!g_Config.bIgnoreBadMemAccess) - { - CMP(32, M((void*)&coreState), Imm32(0)); - FixupBranch skip2 = J_CC(CC_E); - MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); - WriteSyscallExit(); - SetJumpTarget(skip2); - } + safe.DoSlowWrite((void *) &Memory::Write_U32, M((void *)&ssLoadStoreTemp)); } + safe.WriteFinish(); + + gpr.UnlockAll(); + fpr.ReleaseSpillLocks(); } - else - { - // We may not even need to move into EAX as a temporary. - X64Reg addr; - if (gpr.R(rs).IsSimpleReg()) - { - // TODO: Maybe just add a check if it's away, don't mind copying to EAX instead... - gpr.BindToRegister(rs, true, false); - addr = gpr.RX(rs); - } - else - { - MOV(32, R(EAX), gpr.R(rs)); - addr = EAX; - } - - if (!g_Config.bFastMemory) - { - // Is it in physical ram? - CMP(32, R(addr), Imm32(0x08000000)); - FixupBranch tooLow = J_CC(CC_L); - CMP(32, R(addr), Imm32(0x0A000000)); - FixupBranch tooHigh = J_CC(CC_GE); - - const u8* safe = GetCodePtr(); -#ifdef _M_IX86 - MOVSS(MDisp(addr, (u32)Memory::base + offset), fpr.RX(ft)); -#else - MOVSS(MComplex(RBX, addr, SCALE_1, offset), fpr.RX(ft)); -#endif - - FixupBranch skip = J(); - SetJumpTarget(tooLow); - SetJumpTarget(tooHigh); - - // Might also be the scratchpad. - CMP(32, R(addr), Imm32(0x00010000)); - FixupBranch tooLow2 = J_CC(CC_L); - CMP(32, R(addr), Imm32(0x00014000)); - J_CC(CC_L, safe); - SetJumpTarget(tooLow2); - - LEA(32, EAX, MDisp(addr, offset)); - MOVSS(M((void *)&ssLoadStoreTemp), fpr.RX(ft)); - ABI_CallFunctionAA(thunks.ProtectFunction((void *) &Memory::Write_U32, 2), M((void *)&ssLoadStoreTemp), R(EAX)); - - // Should we check the core state? - if (!g_Config.bIgnoreBadMemAccess) - { - CMP(32, M((void*)&coreState), Imm32(0)); - FixupBranch skip2 = J_CC(CC_E); - MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); - WriteSyscallExit(); - SetJumpTarget(skip2); - } - - SetJumpTarget(skip); - } - else - { -#ifdef _M_IX86 - // Need to modify it, too bad. - if (addr != EAX) - MOV(32, R(EAX), gpr.R(rs)); - AND(32, R(EAX), Imm32(Memory::MEMVIEW32_MASK)); - MOVSS(MDisp(EAX, (u32)Memory::base + offset), fpr.RX(ft)); -#else - MOVSS(MComplex(RBX, addr, SCALE_1, offset), fpr.RX(ft)); -#endif - } - } - - gpr.UnlockAll(); - fpr.ReleaseSpillLocks(); break; default: diff --git a/Core/MIPS/x86/CompLoadStore.cpp b/Core/MIPS/x86/CompLoadStore.cpp index 2dcead88cd..799a884f4f 100644 --- a/Core/MIPS/x86/CompLoadStore.cpp +++ b/Core/MIPS/x86/CompLoadStore.cpp @@ -51,106 +51,13 @@ namespace MIPSComp gpr.Lock(rt, rs); gpr.BindToRegister(rt, rt == rs, true); - if (gpr.R(rs).IsImm()) - { - void *data = Memory::GetPointer(gpr.R(rs).GetImmValue() + offset); - if (data) - { -#ifdef _M_IX86 - (this->*mov)(32, bits, gpr.RX(rt), M(data)); -#else - (this->*mov)(32, bits, gpr.RX(rt), MDisp(RBX, gpr.R(rs).GetImmValue() + offset)); -#endif - } - else - { - MOV(32, R(EAX), Imm32(gpr.R(rs).GetImmValue() + offset)); - ABI_CallFunctionA(thunks.ProtectFunction(safeFunc, 1), R(EAX)); - (this->*mov)(32, bits, gpr.RX(rt), R(EAX)); - - // Should we check the core state? - if (!g_Config.bIgnoreBadMemAccess) - { - CMP(32, M((void*)&coreState), Imm32(0)); - FixupBranch skip2 = J_CC(CC_E); - MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); - WriteSyscallExit(); - SetJumpTarget(skip2); - } - } - } - else - { - // We may not even need to move into EAX as a temporary. - X64Reg addr; - if (gpr.R(rs).IsSimpleReg()) - { - // TODO: Maybe just add a check if it's away, don't mind copying to EAX instead... - if (rs != rt) - gpr.BindToRegister(rs, true, false); - addr = gpr.RX(rs); - } - else - { - MOV(32, R(EAX), gpr.R(rs)); - addr = EAX; - } - - if (!g_Config.bFastMemory) - { - // Is it in physical ram? - CMP(32, R(addr), Imm32(0x08000000)); - FixupBranch tooLow = J_CC(CC_L); - CMP(32, R(addr), Imm32(0x0A000000)); - FixupBranch tooHigh = J_CC(CC_GE); - - const u8* safe = GetCodePtr(); -#ifdef _M_IX86 - (this->*mov)(32, bits, gpr.RX(rt), MDisp(addr, (u32)Memory::base + offset)); -#else - (this->*mov)(32, bits, gpr.RX(rt), MComplex(RBX, addr, SCALE_1, offset)); -#endif - - FixupBranch skip = J(); - SetJumpTarget(tooLow); - SetJumpTarget(tooHigh); - - // Might also be the scratchpad. - CMP(32, R(addr), Imm32(0x00010000)); - FixupBranch tooLow2 = J_CC(CC_L); - CMP(32, R(addr), Imm32(0x00014000)); - J_CC(CC_L, safe); - SetJumpTarget(tooLow2); - - LEA(32, EAX, MDisp(addr, offset)); - ABI_CallFunctionA(thunks.ProtectFunction(safeFunc, 1), R(EAX)); - (this->*mov)(32, bits, gpr.RX(rt), R(EAX)); - - // Should we check the core state? - if (!g_Config.bIgnoreBadMemAccess) - { - CMP(32, M((void*)&coreState), Imm32(0)); - FixupBranch skip2 = J_CC(CC_E); - MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); - WriteSyscallExit(); - SetJumpTarget(skip2); - } - - SetJumpTarget(skip); - } - else - { -#ifdef _M_IX86 - // Need to modify it, too bad. - if (addr != EAX) - MOV(32, R(EAX), gpr.R(rs)); - AND(32, R(EAX), Imm32(Memory::MEMVIEW32_MASK)); - (this->*mov)(32, bits, gpr.RX(rt), MDisp(EAX, (u32)Memory::base + offset)); -#else - (this->*mov)(32, bits, gpr.RX(rt), MComplex(RBX, addr, SCALE_1, offset)); -#endif - } - } + JitSafeMem safe(this, rs, offset); + OpArg src; + if (safe.PrepareRead(src, safeFunc)) + (this->*mov)(32, bits, gpr.RX(rt), src); + if (safe.PrepareSlowRead(safeFunc)) + (this->*mov)(32, bits, gpr.RX(rt), R(EAX)); + safe.WriteFinish(); gpr.UnlockAll(); } @@ -170,129 +77,28 @@ namespace MIPSComp const bool needSwap = bits == 8 && !gpr.R(rt).IsSimpleReg(EDX) && !gpr.R(rt).IsSimpleReg(ECX); if (needSwap) gpr.FlushLockX(EDX); +#else + const bool needSwap = false; #endif - if (gpr.R(rs).IsImm()) + JitSafeMem safe(this, rs, offset); + OpArg dest; + if (safe.PrepareWrite(dest)) { - void *data = Memory::GetPointer(gpr.R(rs).GetImmValue() + offset); - if (data) + if (needSwap) { -#ifdef _M_IX86 - if (needSwap) - { - MOV(32, R(EDX), gpr.R(rt)); - MOV(bits, M(data), R(EDX)); - } - else - MOV(bits, M(data), gpr.R(rt)); -#else - MOV(bits, MDisp(RBX, gpr.R(rs).GetImmValue() + offset), gpr.R(rt)); -#endif + MOV(32, R(EDX), gpr.R(rt)); + MOV(bits, dest, R(EDX)); } else - { - MOV(32, R(EAX), Imm32(gpr.R(rs).GetImmValue() + offset)); - ABI_CallFunctionAA(thunks.ProtectFunction(safeFunc, 2), gpr.R(rt), R(EAX)); - - // Should we check the core state? - if (!g_Config.bIgnoreBadMemAccess) - { - CMP(32, M((void*)&coreState), Imm32(0)); - FixupBranch skip2 = J_CC(CC_E); - MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); - WriteSyscallExit(); - SetJumpTarget(skip2); - } - } + MOV(bits, dest, gpr.R(rt)); } - else - { - // We may not even need to move into EAX as a temporary. - X64Reg addr; - if (gpr.R(rs).IsSimpleReg()) - { - // TODO: Maybe just add a check if it's away, don't mind copying to EAX instead... - if (rs != rt) - gpr.BindToRegister(rs, true, false); - addr = gpr.RX(rs); - } - else - { - MOV(32, R(EAX), gpr.R(rs)); - addr = EAX; - } + if (safe.PrepareSlowWrite()) + safe.DoSlowWrite(safeFunc, gpr.R(rt)); + safe.WriteFinish(); - if (!g_Config.bFastMemory) - { - // Is it in physical ram? - CMP(32, R(addr), Imm32(0x08000000)); - FixupBranch tooLow = J_CC(CC_L); - CMP(32, R(addr), Imm32(0x0A000000)); - FixupBranch tooHigh = J_CC(CC_GE); - - const u8* safe = GetCodePtr(); -#ifdef _M_IX86 - if (needSwap) - { - MOV(32, R(EDX), gpr.R(rt)); - MOV(bits, MDisp(addr, (u32)Memory::base + offset), R(EDX)); - } - else - MOV(bits, MDisp(addr, (u32)Memory::base + offset), gpr.R(rt)); -#else - MOV(bits, MComplex(RBX, addr, SCALE_1, offset), gpr.R(rt)); -#endif - - FixupBranch skip = J(); - SetJumpTarget(tooLow); - SetJumpTarget(tooHigh); - - // Might also be the scratchpad. - CMP(32, R(addr), Imm32(0x00010000)); - FixupBranch tooLow2 = J_CC(CC_L); - CMP(32, R(addr), Imm32(0x00014000)); - J_CC(CC_L, safe); - SetJumpTarget(tooLow2); - - LEA(32, EAX, MDisp(addr, offset)); - ABI_CallFunctionAA(thunks.ProtectFunction(safeFunc, 2), gpr.R(rt), R(EAX)); - - // Should we check the core state? - if (!g_Config.bIgnoreBadMemAccess) - { - CMP(32, M((void*)&coreState), Imm32(0)); - FixupBranch skip2 = J_CC(CC_E); - MOV(32, M(¤tMIPS->pc), Imm32(js.compilerPC + 4)); - WriteSyscallExit(); - SetJumpTarget(skip2); - } - - SetJumpTarget(skip); - } - else - { -#ifdef _M_IX86 - // Need to modify it, too bad. - if (addr != EAX) - MOV(32, R(EAX), gpr.R(rs)); - AND(32, R(EAX), Imm32(Memory::MEMVIEW32_MASK)); - if (needSwap) - { - MOV(32, R(EDX), gpr.R(rt)); - MOV(bits, MDisp(EAX, (u32)Memory::base + offset), R(EDX)); - } - else - MOV(bits, MDisp(EAX, (u32)Memory::base + offset), gpr.R(rt)); -#else - MOV(bits, MComplex(RBX, addr, SCALE_1, offset), gpr.R(rt)); -#endif - } - } - -#ifdef _M_IX86 if (needSwap) gpr.UnlockAllX(); -#endif gpr.UnlockAll(); } diff --git a/Core/MIPS/x86/Jit.cpp b/Core/MIPS/x86/Jit.cpp index 3134d5cbae..a09d3dc22c 100644 --- a/Core/MIPS/x86/Jit.cpp +++ b/Core/MIPS/x86/Jit.cpp @@ -19,6 +19,7 @@ #include #include "../../Core.h" #include "../../CoreTiming.h" +#include "../../Config.h" #include "../MIPS.h" #include "../MIPSCodeUtils.h" #include "../MIPSInt.h" @@ -301,4 +302,197 @@ bool Jit::CheckJitBreakpoint(u32 addr, int downcountOffset) return false; } +Jit::JitSafeMem::JitSafeMem(Jit *jit, int raddr, s32 offset) + : jit_(jit), raddr_(raddr), offset_(offset), needsCheck_(false), needsSkip_(false) +{ +} + +bool Jit::JitSafeMem::PrepareWrite(OpArg &dest) +{ + // If it's an immediate, we can do the write if valid. + if (jit_->gpr.IsImmediate(raddr_)) + { + u32 addr = jit_->gpr.GetImmediate32(raddr_) + offset_; + if (Memory::IsValidAddress(addr)) + { +#ifdef _M_IX86 + dest = M(Memory::base + addr); +#else + dest = MDisp(RBX, addr); +#endif + return true; + } + else + return false; + } + // Otherwise, we always can do the write (conditionally.) + else + dest = PrepareMemoryOpArg(); + return true; +} + +bool Jit::JitSafeMem::PrepareRead(OpArg &src, void *safeFunc) +{ + if (jit_->gpr.IsImmediate(raddr_)) + { + u32 addr = jit_->gpr.GetImmediate32(raddr_) + offset_; + if (Memory::IsValidAddress(addr)) + { +#ifdef _M_IX86 + src = M(Memory::base + addr); +#else + src = MDisp(RBX, addr); +#endif + return true; + } + else + return false; + } + else + src = PrepareMemoryOpArg(); + return true; +} + +OpArg Jit::JitSafeMem::PrepareMemoryOpArg() +{ + // We may not even need to move into EAX as a temporary. + // TODO: Except on x86 in fastmem mode. + if (jit_->gpr.R(raddr_).IsSimpleReg()) + { + jit_->gpr.BindToRegister(raddr_, true, false); + xaddr_ = jit_->gpr.RX(raddr_); + } + else + { + jit_->MOV(32, R(EAX), jit_->gpr.R(raddr_)); + xaddr_ = EAX; + } + + X64Reg xaddrResult = xaddr_; + if (!g_Config.bFastMemory) + { + // Is it in physical ram? + jit_->CMP(32, R(xaddr_), Imm32(PSP_GetKernelMemoryBase())); + tooLow_ = jit_->J_CC(CC_L); + jit_->CMP(32, R(xaddr_), Imm32(PSP_GetUserMemoryEnd())); + tooHigh_ = jit_->J_CC(CC_GE); + + // We may need to jump back up here. + safe_ = jit_->GetCodePtr(); + } + else + { +#ifdef _M_IX86 + // Need to modify it, too bad. + if (xaddr_ != EAX) + jit_->MOV(32, R(EAX), R(xaddr_)); + jit_->AND(32, R(EAX), Imm32(Memory::MEMVIEW32_MASK)); + xaddrResult = EAX; +#endif + } + +#ifdef _M_IX86 + return MDisp(xaddrResult, (u32) Memory::base + offset_); +#else + return MComplex(RBX, xaddrResult, SCALE_1, offset_); +#endif +} + +void Jit::JitSafeMem::PrepareSlowAccess() +{ + // Skip the fast path (which the caller wrote just now.) + skip_ = jit_->J(); + needsSkip_ = true; + jit_->SetJumpTarget(tooLow_); + jit_->SetJumpTarget(tooHigh_); + + // Might also be the scratchpad. + jit_->CMP(32, R(xaddr_), Imm32(PSP_GetScratchpadMemoryBase())); + FixupBranch tooLow = jit_->J_CC(CC_L); + jit_->CMP(32, R(xaddr_), Imm32(PSP_GetScratchpadMemoryEnd())); + jit_->J_CC(CC_L, safe_); + jit_->SetJumpTarget(tooLow); +} + +bool Jit::JitSafeMem::PrepareSlowWrite() +{ + // If it's immediate, we only need a slow write on invalid. + if (jit_->gpr.IsImmediate(raddr_)) + { + u32 addr = jit_->gpr.GetImmediate32(raddr_) + offset_; + return !g_Config.bFastMemory && !Memory::IsValidAddress(addr); + } + + if (!g_Config.bFastMemory) + { + PrepareSlowAccess(); + return true; + } + else + return false; +} + +void Jit::JitSafeMem::DoSlowWrite(void *safeFunc, const OpArg src) +{ + if (jit_->gpr.IsImmediate(raddr_)) + { + u32 addr = jit_->gpr.GetImmediate32(raddr_) + offset_; + jit_->MOV(32, R(EAX), Imm32(addr)); + } + else + jit_->LEA(32, EAX, MDisp(xaddr_, offset_)); + + jit_->ABI_CallFunctionAA(jit_->thunks.ProtectFunction(safeFunc, 2), src, R(EAX)); + needsCheck_ = true; +} + +bool Jit::JitSafeMem::PrepareSlowRead(void *safeFunc) +{ + if (jit_->gpr.IsImmediate(raddr_)) + { + u32 addr = jit_->gpr.GetImmediate32(raddr_) + offset_; + if (!g_Config.bFastMemory) + { + jit_->MOV(32, R(EAX), Imm32(addr)); + jit_->ABI_CallFunctionA(jit_->thunks.ProtectFunction(safeFunc, 1), R(EAX)); + needsCheck_ = true; + return true; + } + // Can't read a bad immediate in fast memory mode. + else + return false; + } + + if (!g_Config.bFastMemory) + { + PrepareSlowAccess(); + + jit_->LEA(32, EAX, MDisp(xaddr_, offset_)); + jit_->ABI_CallFunctionA(jit_->thunks.ProtectFunction(safeFunc, 1), R(EAX)); + needsCheck_ = true; + + return true; + } + else + return false; +} + +void Jit::JitSafeMem::WriteFinish() +{ + if (needsCheck_) + { + // Memory::Read_U32/etc. may have tripped coreState. + if (!g_Config.bIgnoreBadMemAccess) + { + jit_->CMP(32, M((void*)&coreState), Imm32(0)); + FixupBranch skipCheck = jit_->J_CC(CC_E); + jit_->MOV(32, M(¤tMIPS->pc), Imm32(jit_->js.compilerPC + 4)); + jit_->WriteSyscallExit(); + jit_->SetJumpTarget(skipCheck); + } + } + if (needsSkip_) + jit_->SetJumpTarget(skip_); +} + } // namespace diff --git a/Core/MIPS/x86/Jit.h b/Core/MIPS/x86/Jit.h index 620fa64e5a..1b499b8a5a 100644 --- a/Core/MIPS/x86/Jit.h +++ b/Core/MIPS/x86/Jit.h @@ -183,6 +183,41 @@ private: ThunkManager thunks; MIPSState *mips_; + + class JitSafeMem + { + public: + JitSafeMem(Jit *jit, int raddr, s32 offset); + + // Emit code necessary for a memory write, returns true if MOV to dest is needed. + bool PrepareWrite(OpArg &dest); + // Emit code proceeding a slow write call, returns true if slow write is needed. + bool PrepareSlowWrite(); + // Emit a slow write from src. + void DoSlowWrite(void *safeFunc, const OpArg src); + + // Emit code necessary for a memory read, returns true if MOV from src is needed. + bool PrepareRead(OpArg &src, void *safeFunc); + // Emit code for a slow read call, and returns true if result is in EAX. + bool PrepareSlowRead(void *safeFunc); + + // Cleans up final code for the memory access. + void WriteFinish(); + + private: + OpArg PrepareMemoryOpArg(); + void PrepareSlowAccess(); + + Jit *jit_; + int raddr_; + s32 offset_; + bool needsCheck_; + bool needsSkip_; + X64Reg xaddr_; + FixupBranch tooLow_, tooHigh_, skip_; + const u8 *safe_; + }; + friend class JitSafeMem; }; typedef void (Jit::*MIPSCompileFunc)(u32 opcode); diff --git a/Core/MemMap.h b/Core/MemMap.h index 0c92469880..bf77692018 100644 --- a/Core/MemMap.h +++ b/Core/MemMap.h @@ -55,6 +55,8 @@ typedef void (*readFn16)(u16&, const u32); typedef void (*readFn32)(u32&, const u32); typedef void (*readFn64)(u64&, const u32); +inline u32 PSP_GetScratchpadMemoryBase() { return 0x00010000;} +inline u32 PSP_GetScratchpadMemoryEnd() { return 0x00014000;} inline u32 PSP_GetKernelMemoryBase() { return 0x08000000;} inline u32 PSP_GetKernelMemoryEnd() { return 0x08400000;} From 9cd5836b85316683be280ff1f4ec0bc464c810be Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 26 Jan 2013 09:09:47 -0800 Subject: [PATCH 6/8] Rename WriteFinish() to Finish() is safe mem. It's nothing to do with writing. --- Core/MIPS/x86/CompFPU.cpp | 4 ++-- Core/MIPS/x86/CompLoadStore.cpp | 4 ++-- Core/MIPS/x86/Jit.cpp | 2 +- Core/MIPS/x86/Jit.h | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Core/MIPS/x86/CompFPU.cpp b/Core/MIPS/x86/CompFPU.cpp index 1a60b6d611..19adaec74a 100644 --- a/Core/MIPS/x86/CompFPU.cpp +++ b/Core/MIPS/x86/CompFPU.cpp @@ -110,7 +110,7 @@ void Jit::Comp_FPULS(u32 op) MOV(32, M((void *)&ssLoadStoreTemp), R(EAX)); MOVSS(fpr.RX(ft), M((void *)&ssLoadStoreTemp)); } - safe.WriteFinish(); + safe.Finish(); gpr.UnlockAll(); fpr.ReleaseSpillLocks(); @@ -131,7 +131,7 @@ void Jit::Comp_FPULS(u32 op) MOVSS(M((void *)&ssLoadStoreTemp), fpr.RX(ft)); safe.DoSlowWrite((void *) &Memory::Write_U32, M((void *)&ssLoadStoreTemp)); } - safe.WriteFinish(); + safe.Finish(); gpr.UnlockAll(); fpr.ReleaseSpillLocks(); diff --git a/Core/MIPS/x86/CompLoadStore.cpp b/Core/MIPS/x86/CompLoadStore.cpp index 799a884f4f..eab1d3b5bb 100644 --- a/Core/MIPS/x86/CompLoadStore.cpp +++ b/Core/MIPS/x86/CompLoadStore.cpp @@ -57,7 +57,7 @@ namespace MIPSComp (this->*mov)(32, bits, gpr.RX(rt), src); if (safe.PrepareSlowRead(safeFunc)) (this->*mov)(32, bits, gpr.RX(rt), R(EAX)); - safe.WriteFinish(); + safe.Finish(); gpr.UnlockAll(); } @@ -95,7 +95,7 @@ namespace MIPSComp } if (safe.PrepareSlowWrite()) safe.DoSlowWrite(safeFunc, gpr.R(rt)); - safe.WriteFinish(); + safe.Finish(); if (needSwap) gpr.UnlockAllX(); diff --git a/Core/MIPS/x86/Jit.cpp b/Core/MIPS/x86/Jit.cpp index a09d3dc22c..400824cc48 100644 --- a/Core/MIPS/x86/Jit.cpp +++ b/Core/MIPS/x86/Jit.cpp @@ -477,7 +477,7 @@ bool Jit::JitSafeMem::PrepareSlowRead(void *safeFunc) return false; } -void Jit::JitSafeMem::WriteFinish() +void Jit::JitSafeMem::Finish() { if (needsCheck_) { diff --git a/Core/MIPS/x86/Jit.h b/Core/MIPS/x86/Jit.h index 1b499b8a5a..cd04fae248 100644 --- a/Core/MIPS/x86/Jit.h +++ b/Core/MIPS/x86/Jit.h @@ -202,7 +202,7 @@ private: bool PrepareSlowRead(void *safeFunc); // Cleans up final code for the memory access. - void WriteFinish(); + void Finish(); private: OpArg PrepareMemoryOpArg(); From b77ce99d01710024449c2eca2b1ad954502c1001 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 26 Jan 2013 09:23:43 -0800 Subject: [PATCH 7/8] Oops, no slow read for immediates usually. --- Core/MIPS/x86/CompFPU.cpp | 2 +- Core/MIPS/x86/CompLoadStore.cpp | 2 +- Core/MIPS/x86/Jit.cpp | 34 +++++++++++++++------------------ Core/MIPS/x86/Jit.h | 2 +- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/Core/MIPS/x86/CompFPU.cpp b/Core/MIPS/x86/CompFPU.cpp index 19adaec74a..c7981b8d3b 100644 --- a/Core/MIPS/x86/CompFPU.cpp +++ b/Core/MIPS/x86/CompFPU.cpp @@ -103,7 +103,7 @@ void Jit::Comp_FPULS(u32 op) JitSafeMem safe(this, rs, offset); OpArg src; - if (safe.PrepareRead(src, (void *) &Memory::Read_U32)) + if (safe.PrepareRead(src)) MOVSS(fpr.RX(ft), src); if (safe.PrepareSlowRead((void *) &Memory::Read_U32)) { diff --git a/Core/MIPS/x86/CompLoadStore.cpp b/Core/MIPS/x86/CompLoadStore.cpp index eab1d3b5bb..37a977d99c 100644 --- a/Core/MIPS/x86/CompLoadStore.cpp +++ b/Core/MIPS/x86/CompLoadStore.cpp @@ -53,7 +53,7 @@ namespace MIPSComp JitSafeMem safe(this, rs, offset); OpArg src; - if (safe.PrepareRead(src, safeFunc)) + if (safe.PrepareRead(src)) (this->*mov)(32, bits, gpr.RX(rt), src); if (safe.PrepareSlowRead(safeFunc)) (this->*mov)(32, bits, gpr.RX(rt), R(EAX)); diff --git a/Core/MIPS/x86/Jit.cpp b/Core/MIPS/x86/Jit.cpp index 400824cc48..b03cf71836 100644 --- a/Core/MIPS/x86/Jit.cpp +++ b/Core/MIPS/x86/Jit.cpp @@ -331,7 +331,7 @@ bool Jit::JitSafeMem::PrepareWrite(OpArg &dest) return true; } -bool Jit::JitSafeMem::PrepareRead(OpArg &src, void *safeFunc) +bool Jit::JitSafeMem::PrepareRead(OpArg &src) { if (jit_->gpr.IsImmediate(raddr_)) { @@ -448,29 +448,25 @@ void Jit::JitSafeMem::DoSlowWrite(void *safeFunc, const OpArg src) bool Jit::JitSafeMem::PrepareSlowRead(void *safeFunc) { - if (jit_->gpr.IsImmediate(raddr_)) - { - u32 addr = jit_->gpr.GetImmediate32(raddr_) + offset_; - if (!g_Config.bFastMemory) - { - jit_->MOV(32, R(EAX), Imm32(addr)); - jit_->ABI_CallFunctionA(jit_->thunks.ProtectFunction(safeFunc, 1), R(EAX)); - needsCheck_ = true; - return true; - } - // Can't read a bad immediate in fast memory mode. - else - return false; - } - if (!g_Config.bFastMemory) { - PrepareSlowAccess(); + if (jit_->gpr.IsImmediate(raddr_)) + { + u32 addr = jit_->gpr.GetImmediate32(raddr_) + offset_; + + // No slow read necessary. + if (Memory::IsValidAddress(addr)) + return false; + jit_->MOV(32, R(EAX), Imm32(addr)); + } + else + { + PrepareSlowAccess(); + jit_->LEA(32, EAX, MDisp(xaddr_, offset_)); + } - jit_->LEA(32, EAX, MDisp(xaddr_, offset_)); jit_->ABI_CallFunctionA(jit_->thunks.ProtectFunction(safeFunc, 1), R(EAX)); needsCheck_ = true; - return true; } else diff --git a/Core/MIPS/x86/Jit.h b/Core/MIPS/x86/Jit.h index cd04fae248..528db41d1f 100644 --- a/Core/MIPS/x86/Jit.h +++ b/Core/MIPS/x86/Jit.h @@ -197,7 +197,7 @@ private: void DoSlowWrite(void *safeFunc, const OpArg src); // Emit code necessary for a memory read, returns true if MOV from src is needed. - bool PrepareRead(OpArg &src, void *safeFunc); + bool PrepareRead(OpArg &src); // Emit code for a slow read call, and returns true if result is in EAX. bool PrepareSlowRead(void *safeFunc); From 0e8e9697c5726d4c1c5f128e977f3cc267edc107 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 26 Jan 2013 10:07:05 -0800 Subject: [PATCH 8/8] Add lv.q/sv.q support to the x86 jit. --- Core/MIPS/x86/CompVFPU.cpp | 68 ++++++++++++++++++-------------------- Core/MIPS/x86/Jit.cpp | 55 ++++++++++++++++++++++++++---- Core/MIPS/x86/Jit.h | 7 +++- 3 files changed, 87 insertions(+), 43 deletions(-) diff --git a/Core/MIPS/x86/CompVFPU.cpp b/Core/MIPS/x86/CompVFPU.cpp index 99002ce44b..383638fc67 100644 --- a/Core/MIPS/x86/CompVFPU.cpp +++ b/Core/MIPS/x86/CompVFPU.cpp @@ -139,6 +139,7 @@ void Jit::ApplyPrefixD(const u8 *vregs, u32 prefix, VectorSize sz, bool onlyWrit } } +static u32 GC_ALIGNED16(ssLoadStoreTemp[1]); void Jit::Comp_SVQ(u32 op) { @@ -150,28 +151,30 @@ void Jit::Comp_SVQ(u32 op) { case 54: //lv.q { - if (!g_Config.bFastMemory) { - DISABLE; - } gpr.BindToRegister(rs, true, true); u8 vregs[4]; GetVectorRegs(vregs, V_Quad, vt); - MOV(32, R(EAX), gpr.R(rs)); - // Just copy 4 words the easiest way while not wasting registers. -#ifndef _M_X64 - AND(32, R(EAX), Imm32(0x3FFFFFFF)); -#endif fpr.MapRegsV(vregs, V_Quad, MAP_DIRTY | MAP_NOINIT); - // MOVSS to prime any crazy cache mechanism that might assume that there's a float somewhere... - for (int i = 0; i < 4; i++) { -#ifdef _M_X64 - MOVSS(fpr.VX(vregs[i]), MComplex(RBX, EAX, 1, i * 4 + imm)); -#else - MOVSS(fpr.VX(vregs[i]), MDisp(EAX, (u32)(Memory::base + i * 4 + imm))); -#endif + JitSafeMem safe(this, rs, imm); + OpArg src; + if (safe.PrepareRead(src)) + { + // Just copy 4 words the easiest way while not wasting registers. + for (int i = 0; i < 4; i++) + MOVSS(fpr.VX(vregs[i]), safe.NextFastAddress(i * 4)); } + if (safe.PrepareSlowRead((void *) &Memory::Read_U32)) + { + for (int i = 0; i < 4; i++) + { + safe.NextSlowRead((void *) &Memory::Read_U32, i * 4); + MOV(32, M((void *)&ssLoadStoreTemp), R(EAX)); + MOVSS(fpr.VX(vregs[i]), M((void *)&ssLoadStoreTemp)); + } + } + safe.Finish(); gpr.UnlockAll(); fpr.ReleaseSpillLocks(); @@ -180,34 +183,29 @@ void Jit::Comp_SVQ(u32 op) case 62: //sv.q { - if (!g_Config.bFastMemory) { - DISABLE; - } - fpr.Flush(); gpr.BindToRegister(rs, true, true); u8 vregs[4]; GetVectorRegs(vregs, V_Quad, vt); - - MOV(32, R(EAX), gpr.R(rs)); - // Just copy 4 words the easiest way while not wasting registers. -#ifndef _M_X64 - AND(32, R(EAX), Imm32(0x3FFFFFFF)); -#endif - // MOVSS to prime any crazy cache mechanism that might assume that there's a float somewhere... - - // It would be pretty nice to have these in registers for the next instruction... // Even if we don't use real SIMD there's still 8 or 16 scalar float registers. - fpr.MapRegsV(vregs, V_Quad, 0); - for (int i = 0; i < 4; i++) { -#ifdef _M_X64 - MOVSS(MComplex(RBX, EAX, 1, i * 4 + imm), fpr.VX(vregs[i])); -#else - MOVSS(MDisp(EAX, (u32)(Memory::base + i * 4 + imm)), fpr.VX(vregs[i])); -#endif + JitSafeMem safe(this, rs, imm); + OpArg dest; + if (safe.PrepareWrite(dest)) + { + for (int i = 0; i < 4; i++) + MOVSS(safe.NextFastAddress(i * 4), fpr.VX(vregs[i])); } + if (safe.PrepareSlowWrite()) + { + for (int i = 0; i < 4; i++) + { + MOVSS(M((void *)&ssLoadStoreTemp), fpr.VX(vregs[i])); + safe.DoSlowWrite((void *) &Memory::Write_U32, M((void *)&ssLoadStoreTemp), i * 4); + } + } + safe.Finish(); fpr.ReleaseSpillLocks(); gpr.UnlockAll(); diff --git a/Core/MIPS/x86/Jit.cpp b/Core/MIPS/x86/Jit.cpp index b03cf71836..f486a4a53d 100644 --- a/Core/MIPS/x86/Jit.cpp +++ b/Core/MIPS/x86/Jit.cpp @@ -353,6 +353,26 @@ bool Jit::JitSafeMem::PrepareRead(OpArg &src) return true; } +OpArg Jit::JitSafeMem::NextFastAddress(int suboffset) +{ + if (jit_->gpr.IsImmediate(raddr_)) + { + u32 addr = jit_->gpr.GetImmediate32(raddr_) + offset_ + suboffset; + +#ifdef _M_IX86 + return M(Memory::base + addr); +#else + return MDisp(RBX, addr); +#endif + } + +#ifdef _M_IX86 + return MDisp(xaddr_, (u32) Memory::base + offset_ + suboffset); +#else + return MComplex(RBX, xaddr_, SCALE_1, offset_ + suboffset); +#endif +} + OpArg Jit::JitSafeMem::PrepareMemoryOpArg() { // We may not even need to move into EAX as a temporary. @@ -368,7 +388,6 @@ OpArg Jit::JitSafeMem::PrepareMemoryOpArg() xaddr_ = EAX; } - X64Reg xaddrResult = xaddr_; if (!g_Config.bFastMemory) { // Is it in physical ram? @@ -387,14 +406,14 @@ OpArg Jit::JitSafeMem::PrepareMemoryOpArg() if (xaddr_ != EAX) jit_->MOV(32, R(EAX), R(xaddr_)); jit_->AND(32, R(EAX), Imm32(Memory::MEMVIEW32_MASK)); - xaddrResult = EAX; + xaddr_ = EAX; #endif } #ifdef _M_IX86 - return MDisp(xaddrResult, (u32) Memory::base + offset_); + return MDisp(xaddr_, (u32) Memory::base + offset_); #else - return MComplex(RBX, xaddrResult, SCALE_1, offset_); + return MComplex(RBX, xaddr_, SCALE_1, offset_); #endif } @@ -432,15 +451,15 @@ bool Jit::JitSafeMem::PrepareSlowWrite() return false; } -void Jit::JitSafeMem::DoSlowWrite(void *safeFunc, const OpArg src) +void Jit::JitSafeMem::DoSlowWrite(void *safeFunc, const OpArg src, int suboffset) { if (jit_->gpr.IsImmediate(raddr_)) { u32 addr = jit_->gpr.GetImmediate32(raddr_) + offset_; - jit_->MOV(32, R(EAX), Imm32(addr)); + jit_->MOV(32, R(EAX), Imm32(addr + suboffset)); } else - jit_->LEA(32, EAX, MDisp(xaddr_, offset_)); + jit_->LEA(32, EAX, MDisp(xaddr_, offset_ + suboffset)); jit_->ABI_CallFunctionAA(jit_->thunks.ProtectFunction(safeFunc, 2), src, R(EAX)); needsCheck_ = true; @@ -473,6 +492,28 @@ bool Jit::JitSafeMem::PrepareSlowRead(void *safeFunc) return false; } +void Jit::JitSafeMem::NextSlowRead(void *safeFunc, int suboffset) +{ + _dbg_assert_msg_(JIT, !g_Config.bFastMemory, "NextSlowRead() called in fast memory mode?"); + + // For simplicity, do nothing for 0. We already read in PrepareSlowRead(). + if (suboffset == 0) + return; + + if (jit_->gpr.IsImmediate(raddr_)) + { + u32 addr = jit_->gpr.GetImmediate32(raddr_) + offset_; + _dbg_assert_msg_(JIT, !Memory::IsValidAddress(addr), "NextSlowRead() for a valid immediate address?"); + + jit_->MOV(32, R(EAX), Imm32(addr + suboffset)); + } + // For GPR, if xaddr_ was the dest register, this will be wrong. Don't use in GPR. + else + jit_->LEA(32, EAX, MDisp(xaddr_, offset_ + suboffset)); + + jit_->ABI_CallFunctionA(jit_->thunks.ProtectFunction(safeFunc, 1), R(EAX)); +} + void Jit::JitSafeMem::Finish() { if (needsCheck_) diff --git a/Core/MIPS/x86/Jit.h b/Core/MIPS/x86/Jit.h index 528db41d1f..25b3d24cac 100644 --- a/Core/MIPS/x86/Jit.h +++ b/Core/MIPS/x86/Jit.h @@ -194,12 +194,17 @@ private: // Emit code proceeding a slow write call, returns true if slow write is needed. bool PrepareSlowWrite(); // Emit a slow write from src. - void DoSlowWrite(void *safeFunc, const OpArg src); + void DoSlowWrite(void *safeFunc, const OpArg src, int suboffset = 0); // Emit code necessary for a memory read, returns true if MOV from src is needed. bool PrepareRead(OpArg &src); // Emit code for a slow read call, and returns true if result is in EAX. bool PrepareSlowRead(void *safeFunc); + + // WARNING: Only works for non-GPR. Do not use for reads into GPR. + OpArg NextFastAddress(int suboffset); + // WARNING: Only works for non-GPR. Do not use for reads into GPR. + void NextSlowRead(void *safeFunc, int suboffset); // Cleans up final code for the memory access. void Finish();