Skip to content

Commit

Permalink
Added inline page table optimization (fastmem-like) and backpatching …
Browse files Browse the repository at this point in the history
…of offending instructions. Added option to build an xbox-specific version of lib86cpu. Added optimization where segment base sums are optimized away when the base is zero
  • Loading branch information
ergo720 committed Jan 9, 2025
1 parent bce9ebe commit 373fa43
Show file tree
Hide file tree
Showing 20 changed files with 1,645 additions and 498 deletions.
50 changes: 36 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ else ()
message(FATAL_ERROR "Unsupported compiler")
endif()

if (${LIB86CPU_X64_EMITTER})
add_definitions(-DLIB86CPU_X64_EMITTER)
set(LIB86CPU_EMITTER "X64")
else ()
set(LIB86CPU_EMITTER "Autodetect (X64)")
set(LIB86CPU_X64_EMITTER TRUE) # we only support x64 for now
endif()

if (${GENERATOR_IS_UMAKE})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Debug")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Release")
Expand All @@ -45,7 +53,7 @@ endif()

if (${CMAKE_HOST_WIN32})
message("Building for Windows")
include_directories(${LIB86CPU_ROOT_DIR}/lib86cpu/core/windows)
include_directories(${LIB86CPU_ROOT_DIR}/lib86cpu/core/windows ${LIB86CPU_ROOT_DIR}/lib86cpu/core/emitter/x64/windows)
set(PLATFORM_HEADERS
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/windows/clock.h"
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/windows/os_exceptions.h"
Expand All @@ -56,6 +64,20 @@ set(PLATFORM_SRC
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/windows/os_exceptions.cpp"
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/windows/os_mem.cpp"
)
if (${LIB86CPU_XBOX_BUILD})
add_definitions(-DXBOX_CPU)
if (${LIB86CPU_X64_EMITTER})
set(PLATFORM_HEADERS ${PLATFORM_HEADERS}
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/emitter/x64/windows/ipt.h"
)
set(PLATFORM_SRC ${PLATFORM_SRC}
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/emitter/x64/windows/ipt.cpp"
)
endif()
add_compile_options(/EHa)
else ()
add_compile_options(/EHs)
endif()
elseif (${CMAKE_HOST_LINUX})
message("Building for Linux")
include_directories(${LIB86CPU_ROOT_DIR}/lib86cpu/core/linux)
Expand All @@ -69,20 +91,25 @@ set(PLATFORM_SRC
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/linux/os_exceptions.cpp"
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/linux/os_mem.cpp"
)
if (${LIB86CPU_XBOX_BUILD})
message(FATAL_ERROR "Inline page tables are not supported on Linux (yet)")
endif()
endif()

if (${LIB86CPU_X64_EMITTER})
add_definitions(-DLIB86CPU_X64_EMITTER)
set(LIB86CPU_EMITTER "X64")
else ()
set(LIB86CPU_X64_EMITTER FALSE)
set(LIB86CPU_EMITTER "Autodetect")
set(PLATFORM_HEADERS ${PLATFORM_HEADERS}
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/emitter/x64/jit.h"
)
set(PLATFORM_SRC ${PLATFORM_SRC}
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/emitter/x64/jit.cpp"
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/emitter/x64/support.cpp"
)
endif()

message("** Lib86cpu Summary **")
message(" LIB86CPU_EMITTER=${LIB86CPU_EMITTER}")
message(" LIB86CPU_X64_EMITTER=${LIB86CPU_X64_EMITTER}")
message(" LIB86CPU_BUILD_TEST=${LIB86CPU_BUILD_TEST}")
message(" LIB86CPU_XBOX_BUILD=${LIB86CPU_XBOX_BUILD}")

message("Building lib86cpu")
include(BuildConfigH.cmake)
Expand Down Expand Up @@ -135,9 +162,7 @@ set(HEADERS
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/internal.h"
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/memory_management.h"
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/registers.h"

"${LIB86CPU_ROOT_DIR}/lib86cpu/core/emitter/emitter_common.h"
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/emitter/x64/jit.h"

${PLATFORM_HEADERS}
)
Expand All @@ -158,10 +183,7 @@ set(SOURCES
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/instructions.cpp"
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/memory_management.cpp"
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/translate.cpp"

"${LIB86CPU_ROOT_DIR}/lib86cpu/core/emitter/emitter_common.cpp"
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/emitter/x64/jit.cpp"
"${LIB86CPU_ROOT_DIR}/lib86cpu/core/emitter/x64/support.cpp"

${PLATFORM_SRC}
)
Expand All @@ -170,7 +192,7 @@ source_group(TREE ${LIB86CPU_ROOT_DIR} PREFIX header FILES ${HEADERS})
source_group(TREE ${LIB86CPU_ROOT_DIR} PREFIX source FILES ${SOURCES})

if (${COMPILER_IS_MSVC})
set(CMAKE_CXX_FLAGS "/EHs /fp:strict /Zc:preprocessor")
add_compile_options(/fp:strict /Zc:preprocessor)
add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS -D__STDC_FORMAT_MACROS -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS)
add_definitions(-wd4146)
add_definitions(-wd4800)
Expand All @@ -186,7 +208,7 @@ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} \
"
)
elseif (${COMPILER_IS_GNU})
set(CMAKE_CXX_FLAGS "-m64 -msse2 -fexceptions -funwind-tables")
add_compile_options(-m64 -msse2 -fexceptions -funwind-tables)
endif()

if (${LIB86CPU_BUILD_SHARED_LIB})
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ NOTE: there is a known bug in a version of Visual Studio 2022 after 17.1.5, that
4. `cmake .. -G "Unix Makefiles"`
5. Build the resulting Makefile with make, or use Visual Studio Code

**NOTE:** use `-DLIB86CPU_BUILD_TEST=ON` if you want to also build the test app.
### Additional options:
- `-DLIB86CPU_BUILD_TEST=ON` build the test app
- `-LIB86CPU_XBOX_BUILD=ON` build an xbox-customized version
29 changes: 29 additions & 0 deletions lib86cpu/core/emitter/emitter_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,35 @@ get_seg_prfx_offset(decoded_instr *instr)
}
}

int
get_seg_prfx_idx(decoded_instr *instr)
{
if (!(instr->i.attributes & ZYDIS_ATTRIB_HAS_SEGMENT)) {
return DS_idx;
}
else if (instr->i.attributes & ZYDIS_ATTRIB_HAS_SEGMENT_CS) {
return CS_idx;
}
else if (instr->i.attributes & ZYDIS_ATTRIB_HAS_SEGMENT_SS) {
return SS_idx;
}
else if (instr->i.attributes & ZYDIS_ATTRIB_HAS_SEGMENT_DS) {
return DS_idx;
}
else if (instr->i.attributes & ZYDIS_ATTRIB_HAS_SEGMENT_ES) {
return ES_idx;
}
else if (instr->i.attributes & ZYDIS_ATTRIB_HAS_SEGMENT_FS) {
return FS_idx;
}
else if (instr->i.attributes & ZYDIS_ATTRIB_HAS_SEGMENT_GS) {
return GS_idx;
}
else {
LIB86CPU_ABORT();
}
}

uint128_t::uint128_t()
{
this->low = 0;
Expand Down
6 changes: 6 additions & 0 deletions lib86cpu/core/emitter/emitter_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@

size_t get_reg_offset(ZydisRegister reg);
size_t get_seg_prfx_offset(decoded_instr *instr);
int get_seg_prfx_idx(decoded_instr *instr);
int get_reg_idx(ZydisRegister reg);
const std::pair<int, size_t> get_reg_pair(ZydisRegister reg);
#ifdef LIB86CPU_X64_EMITTER
Expand Down Expand Up @@ -183,6 +184,11 @@ inline constexpr auto all_callable_funcs = std::make_tuple(
update_crN_helper<1>,
update_crN_helper<2>,
update_drN_helper,
mov_sel_real_helper<SS_idx>,
mov_sel_real_helper<DS_idx>,
mov_sel_real_helper<ES_idx>,
mov_sel_real_helper<FS_idx>,
mov_sel_real_helper<GS_idx>,
mov_sel_pe_helper<SS_idx>,
mov_sel_pe_helper<DS_idx>,
mov_sel_pe_helper<ES_idx>,
Expand Down
Loading

0 comments on commit 373fa43

Please sign in to comment.