From b2798c7adacc3663e8bfeb5f2ad11ceec04f0614 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 20 Nov 2022 09:35:05 -0800 Subject: [PATCH] jit: Add more reasonable estimates for RX protect. --- Common/CodeBlock.h | 2 +- Common/Thunk.cpp | 4 ++-- Core/MIPS/ARM/ArmAsm.cpp | 2 +- Core/MIPS/ARM/ArmJit.cpp | 2 +- Core/MIPS/ARM64/Arm64Asm.cpp | 2 +- Core/MIPS/ARM64/Arm64Jit.cpp | 2 +- Core/MIPS/x86/Asm.cpp | 2 +- Core/MIPS/x86/Jit.cpp | 3 ++- Core/MIPS/x86/JitSafeMem.cpp | 2 +- GPU/Common/VertexDecoderArm.cpp | 2 +- GPU/Common/VertexDecoderArm64.cpp | 2 +- GPU/Common/VertexDecoderX86.cpp | 2 +- GPU/Software/DrawPixelX86.cpp | 3 ++- GPU/Software/RasterizerRegCache.cpp | 2 +- GPU/Software/SamplerX86.cpp | 6 +++--- 15 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Common/CodeBlock.h b/Common/CodeBlock.h index 82e989c4d1..e76bbf40ce 100644 --- a/Common/CodeBlock.h +++ b/Common/CodeBlock.h @@ -80,7 +80,7 @@ public: // If not WX Exclusive, no need to call ProtectMemoryPages because we never change the protection from RWX. PoisonMemory(offset); ResetCodePtr(offset); - if (PlatformIsWXExclusive()) { + if (PlatformIsWXExclusive() && offset != 0) { // Need to re-protect the part we didn't clear. ProtectMemoryPages(region, offset, MEM_PROT_READ | MEM_PROT_EXEC); } diff --git a/Common/Thunk.cpp b/Common/Thunk.cpp index 6248d087a0..581ce94b94 100644 --- a/Common/Thunk.cpp +++ b/Common/Thunk.cpp @@ -43,7 +43,7 @@ void ThunkManager::Init() #endif AllocCodeSpace(THUNK_ARENA_SIZE); - BeginWrite(); + BeginWrite(512); save_regs = GetCodePtr(); #if PPSSPP_ARCH(AMD64) for (int i = 2; i < ABI_GetNumXMMRegs(); i++) @@ -151,7 +151,7 @@ const void *ThunkManager::ProtectFunction(const void *function, int num_params) _assert_msg_(region != nullptr, "Can't protect functions before the emu is started."); - BeginWrite(); + BeginWrite(128); const u8 *call_point = GetCodePtr(); Enter(this, true); diff --git a/Core/MIPS/ARM/ArmAsm.cpp b/Core/MIPS/ARM/ArmAsm.cpp index 008d24eefe..f90dd237ec 100644 --- a/Core/MIPS/ARM/ArmAsm.cpp +++ b/Core/MIPS/ARM/ArmAsm.cpp @@ -71,8 +71,8 @@ namespace MIPSComp { using namespace ArmJitConstants; void ArmJit::GenerateFixedCode() { + BeginWrite(GetMemoryProtectPageSize()); const u8 *start = AlignCodePage(); - BeginWrite(); // LR == SCRATCHREG2 on ARM32 so it needs to be pushed. restoreRoundingMode = AlignCode16(); { diff --git a/Core/MIPS/ARM/ArmJit.cpp b/Core/MIPS/ARM/ArmJit.cpp index 3048773bb6..238ce2e94d 100644 --- a/Core/MIPS/ARM/ArmJit.cpp +++ b/Core/MIPS/ARM/ArmJit.cpp @@ -234,7 +234,7 @@ void ArmJit::Compile(u32 em_address) { ClearCache(); } - BeginWrite(); + BeginWrite(JitBlockCache::MAX_BLOCK_INSTRUCTIONS * 16); int block_num = blocks.AllocateBlock(em_address); JitBlock *b = blocks.GetBlock(block_num); diff --git a/Core/MIPS/ARM64/Arm64Asm.cpp b/Core/MIPS/ARM64/Arm64Asm.cpp index bca3f535e1..9f70435a78 100644 --- a/Core/MIPS/ARM64/Arm64Asm.cpp +++ b/Core/MIPS/ARM64/Arm64Asm.cpp @@ -97,8 +97,8 @@ namespace MIPSComp { using namespace Arm64JitConstants; void Arm64Jit::GenerateFixedCode(const JitOptions &jo) { + BeginWrite(GetMemoryProtectPageSize()); const u8 *start = AlignCodePage(); - BeginWrite(); if (jo.useStaticAlloc) { saveStaticRegisters = AlignCode16(); diff --git a/Core/MIPS/ARM64/Arm64Jit.cpp b/Core/MIPS/ARM64/Arm64Jit.cpp index f9d7e2d684..b4df4f942a 100644 --- a/Core/MIPS/ARM64/Arm64Jit.cpp +++ b/Core/MIPS/ARM64/Arm64Jit.cpp @@ -230,7 +230,7 @@ void Arm64Jit::Compile(u32 em_address) { ClearCache(); } - BeginWrite(4); + BeginWrite(JitBlockCache::MAX_BLOCK_INSTRUCTIONS * 16); int block_num = blocks.AllocateBlock(em_address); JitBlock *b = blocks.GetBlock(block_num); diff --git a/Core/MIPS/x86/Asm.cpp b/Core/MIPS/x86/Asm.cpp index fef6f0487c..471414e453 100644 --- a/Core/MIPS/x86/Asm.cpp +++ b/Core/MIPS/x86/Asm.cpp @@ -66,8 +66,8 @@ void ImHere() { } void Jit::GenerateFixedCode(JitOptions &jo) { + BeginWrite(GetMemoryProtectPageSize()); AlignCodePage(); - BeginWrite(); restoreRoundingMode = AlignCode16(); { STMXCSR(MIPSSTATE_VAR(temp)); diff --git a/Core/MIPS/x86/Jit.cpp b/Core/MIPS/x86/Jit.cpp index 97f32b7b72..ec74bcfb5d 100644 --- a/Core/MIPS/x86/Jit.cpp +++ b/Core/MIPS/x86/Jit.cpp @@ -281,7 +281,8 @@ void Jit::Compile(u32 em_address) { return; } - BeginWrite(); + // Sometimes we compile fairly large blocks, although it's uncommon. + BeginWrite(JitBlockCache::MAX_BLOCK_INSTRUCTIONS * 16); int block_num = blocks.AllocateBlock(em_address); JitBlock *b = blocks.GetBlock(block_num); diff --git a/Core/MIPS/x86/JitSafeMem.cpp b/Core/MIPS/x86/JitSafeMem.cpp index 0fedb39882..8a6e8a6788 100644 --- a/Core/MIPS/x86/JitSafeMem.cpp +++ b/Core/MIPS/x86/JitSafeMem.cpp @@ -462,7 +462,7 @@ void JitSafeMemFuncs::Init(ThunkManager *thunks) { AllocCodeSpace(FUNCS_ARENA_SIZE); thunks_ = thunks; - BeginWrite(); + BeginWrite(1024); readU32 = GetCodePtr(); CreateReadFunc(32, (const void *)&Memory::Read_U32); readU16 = GetCodePtr(); diff --git a/GPU/Common/VertexDecoderArm.cpp b/GPU/Common/VertexDecoderArm.cpp index 2b93b563f8..e5eea51a3e 100644 --- a/GPU/Common/VertexDecoderArm.cpp +++ b/GPU/Common/VertexDecoderArm.cpp @@ -161,7 +161,7 @@ static const JitLookup jitLookup[] = { JittedVertexDecoder VertexDecoderJitCache::Compile(const VertexDecoder &dec, int32_t *jittedSize) { dec_ = &dec; - BeginWrite(); + BeginWrite(4096); const u8 *start = AlignCode16(); bool prescaleStep = false; diff --git a/GPU/Common/VertexDecoderArm64.cpp b/GPU/Common/VertexDecoderArm64.cpp index 0ad04dbe5c..4e4385a224 100644 --- a/GPU/Common/VertexDecoderArm64.cpp +++ b/GPU/Common/VertexDecoderArm64.cpp @@ -143,7 +143,7 @@ static const JitLookup jitLookup[] = { JittedVertexDecoder VertexDecoderJitCache::Compile(const VertexDecoder &dec, int32_t *jittedSize) { dec_ = &dec; - BeginWrite(); + BeginWrite(4096); const u8 *start = AlignCode16(); bool prescaleStep = false; diff --git a/GPU/Common/VertexDecoderX86.cpp b/GPU/Common/VertexDecoderX86.cpp index 3618071c76..728fafc313 100644 --- a/GPU/Common/VertexDecoderX86.cpp +++ b/GPU/Common/VertexDecoderX86.cpp @@ -164,7 +164,7 @@ static const JitLookup jitLookup[] = { JittedVertexDecoder VertexDecoderJitCache::Compile(const VertexDecoder &dec, int32_t *jittedSize) { dec_ = &dec; - BeginWrite(); + BeginWrite(4096); const u8 *start = this->AlignCode16(); #if PPSSPP_ARCH(X86) diff --git a/GPU/Software/DrawPixelX86.cpp b/GPU/Software/DrawPixelX86.cpp index c2c7ce9493..2ba334df94 100644 --- a/GPU/Software/DrawPixelX86.cpp +++ b/GPU/Software/DrawPixelX86.cpp @@ -42,11 +42,12 @@ SingleFunc PixelJitCache::CompileSingle(const PixelFuncID &id) { RegCache::GEN_ARG_ID, }); - BeginWrite(); + BeginWrite(64); Describe("Init"); WriteConstantPool(id); const u8 *resetPos = AlignCode16(); + EndWrite(); bool success = true; #if PPSSPP_PLATFORM(WINDOWS) diff --git a/GPU/Software/RasterizerRegCache.cpp b/GPU/Software/RasterizerRegCache.cpp index dbd0310fe4..7806b43e03 100644 --- a/GPU/Software/RasterizerRegCache.cpp +++ b/GPU/Software/RasterizerRegCache.cpp @@ -420,7 +420,7 @@ int CodeBlock::WriteProlog(int extraStack, const std::vector &vec #if PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64) using namespace Gen; - BeginWrite(); + BeginWrite(32768); AlignCode16(); lastPrologStart_ = GetWritableCodePtr(); diff --git a/GPU/Software/SamplerX86.cpp b/GPU/Software/SamplerX86.cpp index 7b05debdb5..290f8587f4 100644 --- a/GPU/Software/SamplerX86.cpp +++ b/GPU/Software/SamplerX86.cpp @@ -45,7 +45,7 @@ FetchFunc SamplerJitCache::CompileFetch(const SamplerID &id) { regCache_.ForceRetain(RegCache::GEN_RESULT); regCache_.ChangeReg(XMM0, RegCache::VEC_RESULT); - BeginWrite(); + BeginWrite(2048); Describe("Init"); const u8 *start = AlignCode16(); @@ -122,7 +122,7 @@ FetchFunc SamplerJitCache::CompileFetch(const SamplerID &id) { NearestFunc SamplerJitCache::CompileNearest(const SamplerID &id) { _assert_msg_(!id.fetch && !id.linear, "Fetch and linear should be cleared on sampler id"); - BeginWrite(); + BeginWrite(2048); Describe("Init"); // Let's drop some helpful constants here. @@ -438,7 +438,7 @@ NearestFunc SamplerJitCache::CompileNearest(const SamplerID &id) { LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { _assert_msg_(id.linear && !id.fetch, "Only linear should be set on sampler id"); - BeginWrite(); + BeginWrite(2048); Describe("Init"); // We don't use stackArgPos_ here, this is just for DXT.