Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@
[submodule "External/range-v3"]
path = External/range-v3
url = https://github.com/ericniebler/range-v3.git
[submodule "External/zydis"]
shallow = true
path = External/zydis
url = https://github.com/zyantific/zydis.git
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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
Copy link
Member

@neobrain neobrain Dec 18, 2025

Choose a reason for hiding this comment

The 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
find_package(Zydis QUIET)
if (Zydis_FOUND)
message(STATUS "Using system Zydis")
else()
message(STATUS "Using bundled Zydis")
pkg_search_module(Zydis QUIET IMPORTED_TARGET zydis)
pkg_search_module(Zycore QUIET IMPORTED_TARGET zycore)
if (TARGET PkgConfig::Zydis AND TARGET PkgConfig::Zycore AND NOT CMAKE_CROSSCOMPILING)
add_library(Zydis::Zydis ALIAS PkgConfig::Zydis)
add_library(Zycore::Zycore ALIAS PkgConfig::Zycore)
message(STATUS "Using system Zydis")
else()
message(STATUS "Using bundled Zydis")

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()
Expand Down
1 change: 1 addition & 0 deletions External/zydis
Submodule zydis added at 9bfadd
8 changes: 8 additions & 0 deletions FEXCore/Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -108,6 +112,10 @@ if (ENABLE_VIXL_DISASSEMBLER OR ENABLE_VIXL_SIMULATOR)
list (APPEND LIBS vixl)
endif()

if (ENABLE_ZYDIS)
list (APPEND LIBS Zydis)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use the namespaced variant: Zydis::Zydis or Zycore::Zycore (they are robust against typos and unintentionally undeclared build targets; "Zydis" may fall back to purely linking against libZydis.so without propagating include directories etc)

endif()

if (NOT MINGW_BUILD)
list (APPEND LIBS dl)
else()
Expand Down
8 changes: 8 additions & 0 deletions FEXCore/Source/Interface/Config/Config.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,14 @@
"\tstats: Will print stats when disassembling the code"
]
},
"X86Disassemble": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also briefly update the description of the Disassemble option to distinguish from this option / make it clear it only covers generated arm code?

"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",
Expand Down
38 changes: 38 additions & 0 deletions FEXCore/Source/Interface/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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})) {
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LogMan::Msg::IFmt("{:#x}: {}", InstAddress, ZydisInst.text);
} else {
LogMan::Msg::IFmt("{:#x}: (decode failed, {} bytes)", InstAddress, DecodedInfo->InstSize);
LogMan::Msg::IFmt(" {:#x}: {}", InstAddress, ZydisInst.text);
} else {
LogMan::Msg::IFmt(" {:#x}: (decode failed, {} bytes)", InstAddress, DecodedInfo->InstSize);

}
}
#endif

bool IsLocked = DecodedInfo->Flags & FEXCore::X86Tables::DecodeFlags::FLAG_LOCK;

// Do a partial register cache flush before every instruction. This
Expand Down Expand Up @@ -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();
Expand Down
Loading