From 26e0fc6ad505248e4c47daead6d3903348b7e8d5 Mon Sep 17 00:00:00 2001 From: squidbus <175574877+squidbus@users.noreply.github.com> Date: Tue, 16 Jul 2024 04:04:44 -0700 Subject: [PATCH] Only patch BMI1 instructions under Rosetta 2. --- src/core/cpu_patches.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/core/cpu_patches.cpp b/src/core/cpu_patches.cpp index eb062f1e..39cfc239 100644 --- a/src/core/cpu_patches.cpp +++ b/src/core/cpu_patches.cpp @@ -14,6 +14,9 @@ #include #else #include +#ifdef __APPLE__ +#include +#endif #endif using namespace Xbyak::util; @@ -251,6 +254,15 @@ static void GenerateBLSR(const ZydisDecodedOperand* operands, Xbyak::CodeGenerat RestoreRegisters(c, {scratch}); } +bool FilterRosetta2Only(const ZydisDecodedOperand*) { + int ret = 0; + size_t size = sizeof(ret); + if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) != 0) { + return false; + } + return ret; +} + #endif // __APPLE__ static bool FilterTcbAccess(const ZydisDecodedOperand* operands) { @@ -297,10 +309,6 @@ static void GenerateTcbAccess(const ZydisDecodedOperand* operands, Xbyak::CodeGe #endif } -bool FilterAlwaysTrue(const ZydisDecodedOperand* operands) { - return true; -} - using PatchFilter = bool (*)(const ZydisDecodedOperand*); using InstructionGenerator = void (*)(const ZydisDecodedOperand*, Xbyak::CodeGenerator&); struct PatchInfo { @@ -324,11 +332,11 @@ static const std::unordered_map Patches = { #ifdef __APPLE__ // BMI1 instructions that are not supported by Rosetta 2 on Apple Silicon. - {ZYDIS_MNEMONIC_ANDN, {FilterAlwaysTrue, GenerateANDN, true}}, - {ZYDIS_MNEMONIC_BEXTR, {FilterAlwaysTrue, GenerateBEXTR, true}}, - {ZYDIS_MNEMONIC_BLSI, {FilterAlwaysTrue, GenerateBLSI, true}}, - {ZYDIS_MNEMONIC_BLSMSK, {FilterAlwaysTrue, GenerateBLSMSK, true}}, - {ZYDIS_MNEMONIC_BLSR, {FilterAlwaysTrue, GenerateBLSR, true}}, + {ZYDIS_MNEMONIC_ANDN, {FilterRosetta2Only, GenerateANDN, true}}, + {ZYDIS_MNEMONIC_BEXTR, {FilterRosetta2Only, GenerateBEXTR, true}}, + {ZYDIS_MNEMONIC_BLSI, {FilterRosetta2Only, GenerateBLSI, true}}, + {ZYDIS_MNEMONIC_BLSMSK, {FilterRosetta2Only, GenerateBLSMSK, true}}, + {ZYDIS_MNEMONIC_BLSR, {FilterRosetta2Only, GenerateBLSR, true}}, #endif };