Only patch BMI1 instructions under Rosetta 2.

This commit is contained in:
squidbus 2024-07-16 04:04:44 -07:00 committed by TheTurtle
parent fadf03909c
commit 26e0fc6ad5
1 changed files with 17 additions and 9 deletions

View File

@ -14,6 +14,9 @@
#include <windows.h> #include <windows.h>
#else #else
#include <pthread.h> #include <pthread.h>
#ifdef __APPLE__
#include <sys/sysctl.h>
#endif
#endif #endif
using namespace Xbyak::util; using namespace Xbyak::util;
@ -251,6 +254,15 @@ static void GenerateBLSR(const ZydisDecodedOperand* operands, Xbyak::CodeGenerat
RestoreRegisters(c, {scratch}); 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__ #endif // __APPLE__
static bool FilterTcbAccess(const ZydisDecodedOperand* operands) { static bool FilterTcbAccess(const ZydisDecodedOperand* operands) {
@ -297,10 +309,6 @@ static void GenerateTcbAccess(const ZydisDecodedOperand* operands, Xbyak::CodeGe
#endif #endif
} }
bool FilterAlwaysTrue(const ZydisDecodedOperand* operands) {
return true;
}
using PatchFilter = bool (*)(const ZydisDecodedOperand*); using PatchFilter = bool (*)(const ZydisDecodedOperand*);
using InstructionGenerator = void (*)(const ZydisDecodedOperand*, Xbyak::CodeGenerator&); using InstructionGenerator = void (*)(const ZydisDecodedOperand*, Xbyak::CodeGenerator&);
struct PatchInfo { struct PatchInfo {
@ -324,11 +332,11 @@ static const std::unordered_map<ZydisMnemonic, PatchInfo> Patches = {
#ifdef __APPLE__ #ifdef __APPLE__
// BMI1 instructions that are not supported by Rosetta 2 on Apple Silicon. // BMI1 instructions that are not supported by Rosetta 2 on Apple Silicon.
{ZYDIS_MNEMONIC_ANDN, {FilterAlwaysTrue, GenerateANDN, true}}, {ZYDIS_MNEMONIC_ANDN, {FilterRosetta2Only, GenerateANDN, true}},
{ZYDIS_MNEMONIC_BEXTR, {FilterAlwaysTrue, GenerateBEXTR, true}}, {ZYDIS_MNEMONIC_BEXTR, {FilterRosetta2Only, GenerateBEXTR, true}},
{ZYDIS_MNEMONIC_BLSI, {FilterAlwaysTrue, GenerateBLSI, true}}, {ZYDIS_MNEMONIC_BLSI, {FilterRosetta2Only, GenerateBLSI, true}},
{ZYDIS_MNEMONIC_BLSMSK, {FilterAlwaysTrue, GenerateBLSMSK, true}}, {ZYDIS_MNEMONIC_BLSMSK, {FilterRosetta2Only, GenerateBLSMSK, true}},
{ZYDIS_MNEMONIC_BLSR, {FilterAlwaysTrue, GenerateBLSR, true}}, {ZYDIS_MNEMONIC_BLSR, {FilterRosetta2Only, GenerateBLSR, true}},
#endif #endif
}; };