-
Notifications
You must be signed in to change notification settings - Fork 222
Add Zydis x86/x86-64 disassembler support #5128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,6 +28,7 @@ option(ENABLE_LIBCXX "Enables LLVM libc++" FALSE) | |||||||||||||||||||||||||||
| option(ENABLE_CCACHE "Enables ccache for compile caching" TRUE) | ||||||||||||||||||||||||||||
| option(ENABLE_VIXL_SIMULATOR "Enable use of VIXL simulator for emulation (only useful for CI testing)" FALSE) | ||||||||||||||||||||||||||||
| option(ENABLE_VIXL_DISASSEMBLER "Enables debug disassembler output with VIXL" FALSE) | ||||||||||||||||||||||||||||
| option(ENABLE_ZYDIS "Enables x86/x86-64 guest disassembler output with Zydis" FALSE) | ||||||||||||||||||||||||||||
| option(USE_LEGACY_BINFMTMISC "Uses legacy method of setting up binfmt_misc" FALSE) | ||||||||||||||||||||||||||||
| option(ENABLE_FEXCORE_PROFILER "Enables use of the FEXCore timeline profiling capabilities" FALSE) | ||||||||||||||||||||||||||||
| set (FEXCORE_PROFILER_BACKEND "gpuvis" CACHE STRING "Set which backend to use for the FEXCore profiler (gpuvis, tracy)") | ||||||||||||||||||||||||||||
|
|
@@ -315,6 +316,18 @@ if (BUILD_TESTING OR ENABLE_VIXL_DISASSEMBLER OR ENABLE_VIXL_SIMULATOR) | |||||||||||||||||||||||||||
| include_directories(SYSTEM External/vixl/src/) | ||||||||||||||||||||||||||||
| endif() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (ENABLE_ZYDIS) | ||||||||||||||||||||||||||||
| find_package(Zydis QUIET) | ||||||||||||||||||||||||||||
| if (Zydis_FOUND) | ||||||||||||||||||||||||||||
| message(STATUS "Using system Zydis") | ||||||||||||||||||||||||||||
| else() | ||||||||||||||||||||||||||||
| message(STATUS "Using bundled Zydis") | ||||||||||||||||||||||||||||
|
Comment on lines
+320
to
+324
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately I realized Fedora does not actually ship a CMake config for Zydis, but only pkgconfig files. To add insult to injury, zycore uses a separate pkgconfig file, so this must be used instead:
Suggested change
None of the other distros that I use have that package to begin with so I don't know if CMake files are shipped anywhere. Might be interesting to check quickly on your systems, otherwise I wouldn't spend too much energy researching this. One day we'll have nice things :( |
||||||||||||||||||||||||||||
| set(ZYDIS_BUILD_TOOLS OFF CACHE BOOL "" FORCE) | ||||||||||||||||||||||||||||
| set(ZYDIS_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) | ||||||||||||||||||||||||||||
| add_subdirectory(External/zydis/) | ||||||||||||||||||||||||||||
| endif() | ||||||||||||||||||||||||||||
| endif() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (ENABLE_FEXCORE_PROFILER AND FEXCORE_PROFILER_BACKEND STREQUAL "TRACY") | ||||||||||||||||||||||||||||
| add_subdirectory(External/tracy) | ||||||||||||||||||||||||||||
| endif() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,10 @@ if (ENABLE_VIXL_DISASSEMBLER) | |
| list(APPEND DEFINES -DVIXL_DISASSEMBLER=1) | ||
| endif() | ||
|
|
||
| if (ENABLE_ZYDIS) | ||
| list(APPEND DEFINES -DZYDIS_DISASSEMBLER=1) | ||
| endif() | ||
|
|
||
| if (_M_ARM_64 AND HAS_CLANG_PRESERVE_ALL) | ||
| list(APPEND DEFINES "-DFEXCORE_PRESERVE_ALL_ATTR=__attribute__((preserve_all));-DFEXCORE_HAS_PRESERVE_ALL_ATTR=1") | ||
| else() | ||
|
|
@@ -108,6 +112,10 @@ if (ENABLE_VIXL_DISASSEMBLER OR ENABLE_VIXL_SIMULATOR) | |
| list (APPEND LIBS vixl) | ||
| endif() | ||
|
|
||
| if (ENABLE_ZYDIS) | ||
| list (APPEND LIBS Zydis) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use the namespaced variant: |
||
| endif() | ||
|
|
||
| if (NOT MINGW_BUILD) | ||
| list (APPEND LIBS dl) | ||
| else() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -334,6 +334,14 @@ | |
| "\tstats: Will print stats when disassembling the code" | ||
| ] | ||
| }, | ||
| "X86Disassemble": { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also briefly update the description of the |
||
| "Type": "bool", | ||
| "Default": "false", | ||
| "Desc": [ | ||
| "Enables x86/x86-64 guest disassembly output for compiled blocks.", | ||
| "Requires FEX to be built with -DENABLE_ZYDIS=TRUE" | ||
| ] | ||
| }, | ||
| "ForceSVEWidth": { | ||
| "Type": "uint32", | ||
| "Default": "0", | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,9 @@ desc: Glues Frontend, OpDispatcher and IR Opts & Compilation, LookupCache, Dispa | |||||||||||||
| */ | ||||||||||||||
|
|
||||||||||||||
| #include <cstdint> | ||||||||||||||
| #ifdef ZYDIS_DISASSEMBLER | ||||||||||||||
| #include <Zydis/Zydis.h> | ||||||||||||||
| #endif | ||||||||||||||
| #include "Interface/Core/ArchHelpers/Arm64Emitter.h" | ||||||||||||||
| #include "Interface/Core/LookupCache.h" | ||||||||||||||
| #include "Interface/Core/CPUBackend.h" | ||||||||||||||
|
|
@@ -536,9 +539,25 @@ ContextImpl::GenerateIR(FEXCore::Core::InternalThreadState* Thread, uint64_t Gue | |||||||||||||
|
|
||||||||||||||
| const auto GPRSize = Thread->OpDispatcher->GetGPROpSize(); | ||||||||||||||
|
|
||||||||||||||
| #ifdef ZYDIS_DISASSEMBLER | ||||||||||||||
| FEX_CONFIG_OPT(X86Disassemble, X86DISASSEMBLE); | ||||||||||||||
| const auto ZydisMachineMode = Config.Is64BitMode ? ZYDIS_MACHINE_MODE_LONG_64 : ZYDIS_MACHINE_MODE_LEGACY_32; | ||||||||||||||
| if (X86Disassemble()) { | ||||||||||||||
| const uint64_t DecodedMin = Thread->FrontendDecoder->DecodedMinAddress; | ||||||||||||||
| const uint64_t DecodedMax = Thread->FrontendDecoder->DecodedMaxAddress; | ||||||||||||||
| LogMan::Msg::IFmt("Guest x86 Begin (RIP={:#x}, {:#x}-{:#x})", GuestRIP, DecodedMin, DecodedMax); | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
|
|
||||||||||||||
| for (size_t j = 0; j < CodeBlocks->size(); ++j) { | ||||||||||||||
| const FEXCore::Frontend::Decoder::DecodedBlocks& Block = CodeBlocks->at(j); | ||||||||||||||
|
|
||||||||||||||
| #ifdef ZYDIS_DISASSEMBLER | ||||||||||||||
| if (X86Disassemble() && CodeBlocks->size() > 1) { | ||||||||||||||
| LogMan::Msg::IFmt(" Block {} Entry={:#x} NumInsts={}", j, Block.Entry, Block.NumInstructions); | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
|
|
||||||||||||||
| bool BlockInForceTSOValidRange = false; | ||||||||||||||
| auto InstForceTSOIt = ForceTSOInstructions.end(); | ||||||||||||||
| if (ForceTSOValidRanges.Contains({Block.Entry, Block.Entry + Block.Size})) { | ||||||||||||||
|
|
@@ -570,6 +589,19 @@ ContextImpl::GenerateIR(FEXCore::Core::InternalThreadState* Thread, uint64_t Gue | |||||||||||||
|
|
||||||||||||||
| TableInfo = Block.DecodedInstructions[i].TableInfo; | ||||||||||||||
| DecodedInfo = &Block.DecodedInstructions[i]; | ||||||||||||||
|
|
||||||||||||||
| #ifdef ZYDIS_DISASSEMBLER | ||||||||||||||
| if (X86Disassemble()) { | ||||||||||||||
| const uint8_t* InstBytes = reinterpret_cast<const uint8_t*>(InstAddress); | ||||||||||||||
| ZydisDisassembledInstruction ZydisInst; | ||||||||||||||
| if (ZYAN_SUCCESS(ZydisDisassembleIntel(ZydisMachineMode, InstAddress, InstBytes, DecodedInfo->InstSize, &ZydisInst))) { | ||||||||||||||
| LogMan::Msg::IFmt("{:#x}: {}", InstAddress, ZydisInst.text); | ||||||||||||||
| } else { | ||||||||||||||
| LogMan::Msg::IFmt("{:#x}: (decode failed, {} bytes)", InstAddress, DecodedInfo->InstSize); | ||||||||||||||
|
Comment on lines
+598
to
+600
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
|
|
||||||||||||||
| bool IsLocked = DecodedInfo->Flags & FEXCore::X86Tables::DecodeFlags::FLAG_LOCK; | ||||||||||||||
|
|
||||||||||||||
| // Do a partial register cache flush before every instruction. This | ||||||||||||||
|
|
@@ -689,6 +721,12 @@ ContextImpl::GenerateIR(FEXCore::Core::InternalThreadState* Thread, uint64_t Gue | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #ifdef ZYDIS_DISASSEMBLER | ||||||||||||||
| if (X86Disassemble()) { | ||||||||||||||
| LogMan::Msg::IFmt("Guest x86 End"); | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
|
|
||||||||||||||
| Thread->OpDispatcher->Finalize(); | ||||||||||||||
|
|
||||||||||||||
| Thread->FrontendDecoder->DelayedDisownBuffer(); | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.