diff --git a/CMakeLists.txt b/CMakeLists.txt index c7098f37..697f3da5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,10 +2,11 @@ cmake_minimum_required(VERSION 2.8) project(xmrig) option(WITH_LIBCPUID "Use Libcpuid" ON) -option(WITH_AEON "CryptoNight-Lite support" ON) -option(WITH_SUMO "CryptoNight-Heavy support" ON) +option(WITH_CN_LITE "CryptoNight-Lite support" ON) +option(WITH_CN_HEAVY "CryptoNight-Heavy support" ON) option(WITH_CN_PICO "CryptoNight-Pico support" ON) option(WITH_CN_GPU "CryptoNight-GPU support" ON) +option(WITH_RANDOMX "RandomX support" ON) option(WITH_HTTP "HTTP protocol support (client/server)" ON) option(WITH_DEBUG_LOG "Enable debug log output" OFF) option(WITH_TLS "Enable OpenSSL support" ON) @@ -25,11 +26,9 @@ set(HEADERS src/api/interfaces/IApiListener.h src/App.h src/common/cpu/Cpu.h - src/common/crypto/Algorithm.h src/common/crypto/keccak.h src/common/interfaces/ICpuInfo.h src/common/Platform.h - src/common/utils/mm_malloc.h src/common/xmrig.h src/core/config/Config_default.h src/core/config/Config_platform.h @@ -56,32 +55,34 @@ set(HEADERS ) set(HEADERS_CRYPTO - src/crypto/c_blake256.h - src/crypto/c_groestl.h - src/crypto/c_jh.h - src/crypto/c_skein.h - src/crypto/CryptoNight.h - src/crypto/CryptoNight_constants.h - src/crypto/CryptoNight_monero.h - src/crypto/CryptoNight_test.h - src/crypto/groestl_tables.h - src/crypto/hash.h - src/crypto/skein_port.h - src/crypto/soft_aes.h - src/crypto/asm/CryptonightR_template.h + src/crypto/cn/asm/CryptonightR_template.h + src/crypto/cn/c_blake256.h + src/crypto/cn/c_groestl.h + src/crypto/cn/c_jh.h + src/crypto/cn/c_skein.h + src/crypto/cn/CryptoNight_constants.h + src/crypto/cn/CryptoNight_monero.h + src/crypto/cn/CryptoNight_test.h + src/crypto/cn/CryptoNight.h + src/crypto/cn/groestl_tables.h + src/crypto/cn/hash.h + src/crypto/cn/skein_port.h + src/crypto/cn/soft_aes.h + src/crypto/common/Algorithm.h + src/crypto/common/portable/mm_malloc.h + src/crypto/common/VirtualMemory.h ) if (XMRIG_ARM) - set(HEADERS_CRYPTO "${HEADERS_CRYPTO}" src/crypto/CryptoNight_arm.h) + set(HEADERS_CRYPTO "${HEADERS_CRYPTO}" src/crypto/cn/CryptoNight_arm.h) else() - set(HEADERS_CRYPTO "${HEADERS_CRYPTO}" src/crypto/CryptoNight_x86.h) + set(HEADERS_CRYPTO "${HEADERS_CRYPTO}" src/crypto/cn/CryptoNight_x86.h) endif() set(SOURCES "${SOURCES_BASE}" "${SOURCES_BASE_HTTP}" src/App.cpp - src/common/crypto/Algorithm.cpp src/common/crypto/keccak.cpp src/common/Platform.cpp src/core/config/Config.cpp @@ -102,10 +103,11 @@ set(SOURCES ) set(SOURCES_CRYPTO - src/crypto/c_groestl.c - src/crypto/c_blake256.c - src/crypto/c_jh.c - src/crypto/c_skein.c + src/crypto/cn/c_groestl.c + src/crypto/cn/c_blake256.c + src/crypto/cn/c_jh.c + src/crypto/cn/c_skein.c + src/crypto/common/Algorithm.cpp ) if (WIN32) @@ -115,6 +117,7 @@ if (WIN32) src/App_win.cpp src/common/Platform_win.cpp src/Mem_win.cpp + src/crypto/common/VirtualMemory_win.cpp ) add_definitions(/DWIN32) @@ -125,6 +128,7 @@ elseif (APPLE) src/App_unix.cpp src/common/Platform_mac.cpp src/Mem_unix.cpp + src/crypto/common/VirtualMemory_unix.cpp ) else() set(SOURCES_OS @@ -132,6 +136,7 @@ else() src/App_unix.cpp src/common/Platform_unix.cpp src/Mem_unix.cpp + src/crypto/common/VirtualMemory_unix.cpp ) if (CMAKE_SYSTEM_NAME STREQUAL FreeBSD) @@ -155,6 +160,17 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") find_package(UV REQUIRED) +if (WITH_RANDOMX) + find_package(RandomX REQUIRED) + include_directories(${RANDOMX_INCLUDE_DIR}) + + add_definitions(/DXMRIG_ALGO_RANDOMX) +else() + set(RANDOMX_LIBRARIES "") + + remove_definitions(/DXMRIG_ALGO_RANDOMX) +endif() + include(cmake/flags.cmake) if (WITH_LIBCPUID) @@ -178,20 +194,16 @@ include(cmake/OpenSSL.cmake) include(cmake/asm.cmake) include(cmake/cn-gpu.cmake) -if (NOT WITH_AEON) - add_definitions(/DXMRIG_NO_AEON) +if (WITH_CN_LITE) + add_definitions(/DXMRIG_ALGO_CN_LITE) endif() -if (NOT WITH_SUMO) - add_definitions(/DXMRIG_NO_SUMO) +if (WITH_CN_HEAVY) + add_definitions(/DXMRIG_ALGO_CN_HEAVY) endif() -if (NOT WITH_IPBC) - add_definitions(/DXMRIG_NO_IPBC) -endif() - -if (NOT WITH_CN_PICO) - add_definitions(/DXMRIG_NO_CN_PICO) +if (WITH_CN_PICO) + add_definitions(/DXMRIG_ALGO_CN_PICO) endif() if (WITH_EMBEDDED_CONFIG) @@ -229,4 +241,4 @@ if (WITH_DEBUG_LOG) endif() add_executable(${CMAKE_PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTP_SOURCES} ${TLS_SOURCES} ${XMRIG_ASM_SOURCES} ${CN_GPU_SOURCES}) -target_link_libraries(${CMAKE_PROJECT_NAME} ${XMRIG_ASM_LIBRARY} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${EXTRA_LIBS} ${CPUID_LIB}) +target_link_libraries(${CMAKE_PROJECT_NAME} ${XMRIG_ASM_LIBRARY} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${RANDOMX_LIBRARIES} ${EXTRA_LIBS} ${CPUID_LIB}) diff --git a/cmake/FindRandomX.cmake b/cmake/FindRandomX.cmake new file mode 100644 index 00000000..5696d563 --- /dev/null +++ b/cmake/FindRandomX.cmake @@ -0,0 +1,25 @@ +find_path( + RANDOMX_INCLUDE_DIR + NAMES randomx.h + PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" + PATH_SUFFIXES "include" + NO_DEFAULT_PATH +) + +find_path(RANDOMX_INCLUDE_DIR NAMES randomx.h) + +find_library( + RANDOMX_LIBRARY + NAMES librandomx.a randomx librandomx + PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" + PATH_SUFFIXES "lib" + NO_DEFAULT_PATH +) + +find_library(RANDOMX_LIBRARY NAMES librandomx.a randomx librandomx) + +set(RANDOMX_LIBRARIES ${RANDOMX_LIBRARY}) +set(RANDOMX_INCLUDE_DIRS ${RANDOMX_INCLUDE_DIR}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(RANDOMX DEFAULT_MSG RANDOMX_LIBRARY RANDOMX_INCLUDE_DIR) diff --git a/cmake/asm.cmake b/cmake/asm.cmake index 389f6723..25cccead 100644 --- a/cmake/asm.cmake +++ b/cmake/asm.cmake @@ -6,13 +6,13 @@ if (WITH_ASM AND NOT XMRIG_ARM AND CMAKE_SIZEOF_VOID_P EQUAL 8) if (MSVC_TOOLSET_VERSION GREATER_EQUAL 141) set(XMRIG_ASM_FILES - "src/crypto/asm/cn_main_loop.asm" - "src/crypto/asm/CryptonightR_template.asm" + "src/crypto/cn/asm/cn_main_loop.asm" + "src/crypto/cn/asm/CryptonightR_template.asm" ) else() set(XMRIG_ASM_FILES - "src/crypto/asm/win64/cn_main_loop.asm" - "src/crypto/asm/win64/CryptonightR_template.asm" + "src/crypto/cn/asm/win64/cn_main_loop.asm" + "src/crypto/cn/asm/win64/CryptonightR_template.asm" ) endif() @@ -22,13 +22,13 @@ if (WITH_ASM AND NOT XMRIG_ARM AND CMAKE_SIZEOF_VOID_P EQUAL 8) if (WIN32 AND CMAKE_C_COMPILER_ID MATCHES GNU) set(XMRIG_ASM_FILES - "src/crypto/asm/win64/cn_main_loop.S" - "src/crypto/asm/CryptonightR_template.S" + "src/crypto/cn/asm/win64/cn_main_loop.S" + "src/crypto/cn/asm/CryptonightR_template.S" ) else() set(XMRIG_ASM_FILES - "src/crypto/asm/cn_main_loop.S" - "src/crypto/asm/CryptonightR_template.S" + "src/crypto/cn/asm/cn_main_loop.S" + "src/crypto/cn/asm/CryptonightR_template.S" ) endif() @@ -36,7 +36,7 @@ if (WITH_ASM AND NOT XMRIG_ARM AND CMAKE_SIZEOF_VOID_P EQUAL 8) endif() add_library(${XMRIG_ASM_LIBRARY} STATIC ${XMRIG_ASM_FILES}) - set(XMRIG_ASM_SOURCES src/crypto/Asm.h src/crypto/Asm.cpp src/crypto/CryptonightR_gen.cpp) + set(XMRIG_ASM_SOURCES src/crypto/cn/Asm.h src/crypto/cn/Asm.cpp src/crypto/cn/r/CryptonightR_gen.cpp) set_property(TARGET ${XMRIG_ASM_LIBRARY} PROPERTY LINKER_LANGUAGE C) else() set(XMRIG_ASM_SOURCES "") diff --git a/cmake/cn-gpu.cmake b/cmake/cn-gpu.cmake index b529f0b2..7aa489e6 100644 --- a/cmake/cn-gpu.cmake +++ b/cmake/cn-gpu.cmake @@ -1,23 +1,25 @@ if (WITH_CN_GPU AND CMAKE_SIZEOF_VOID_P EQUAL 8) if (XMRIG_ARM) - set(CN_GPU_SOURCES src/crypto/cn_gpu_arm.cpp) + set(CN_GPU_SOURCES src/crypto/cn/gpu/cn_gpu_arm.cpp) if (CMAKE_CXX_COMPILER_ID MATCHES GNU OR CMAKE_CXX_COMPILER_ID MATCHES Clang) - set_source_files_properties(src/crypto/cn_gpu_arm.cpp PROPERTIES COMPILE_FLAGS "-O3") + set_source_files_properties(src/crypto/cn/gpu/cn_gpu_arm.cpp PROPERTIES COMPILE_FLAGS "-O3") endif() else() - set(CN_GPU_SOURCES src/crypto/cn_gpu_avx.cpp src/crypto/cn_gpu_ssse3.cpp) + set(CN_GPU_SOURCES src/crypto/cn/gpu/cn_gpu_avx.cpp src/crypto/cn/gpu/cn_gpu_ssse3.cpp) if (CMAKE_CXX_COMPILER_ID MATCHES GNU OR CMAKE_CXX_COMPILER_ID MATCHES Clang) - set_source_files_properties(src/crypto/cn_gpu_avx.cpp PROPERTIES COMPILE_FLAGS "-O3 -mavx2") - set_source_files_properties(src/crypto/cn_gpu_ssse3.cpp PROPERTIES COMPILE_FLAGS "-O3") + set_source_files_properties(src/crypto/cn/gpu/cn_gpu_avx.cpp PROPERTIES COMPILE_FLAGS "-O3 -mavx2") + set_source_files_properties(src/crypto/cn/gpu/cn_gpu_ssse3.cpp PROPERTIES COMPILE_FLAGS "-O3") elseif (CMAKE_CXX_COMPILER_ID MATCHES MSVC) - set_source_files_properties(src/crypto/cn_gpu_avx.cpp PROPERTIES COMPILE_FLAGS "/arch:AVX") + set_source_files_properties(src/crypto/cn/gpu/cn_gpu_avx.cpp PROPERTIES COMPILE_FLAGS "/arch:AVX") endif() endif() + + add_definitions(/DXMRIG_ALGO_CN_GPU) else() set(CN_GPU_SOURCES "") - add_definitions(/DXMRIG_NO_CN_GPU) + remove_definitions(/DXMRIG_ALGO_CN_GPU) endif() diff --git a/cmake/flags.cmake b/cmake/flags.cmake index d50b5c84..d0036628 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -81,3 +81,10 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang) endif() endif() + +if (NOT WIN32) + check_symbol_exists("__builtin___clear_cache" "stdlib.h" HAVE_BUILTIN_CLEAR_CACHE) + if (HAVE_BUILTIN_CLEAR_CACHE) + add_definitions(/DHAVE_BUILTIN_CLEAR_CACHE) + endif() +endif() diff --git a/src/App.cpp b/src/App.cpp index 38e335c2..66662eb1 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -37,7 +37,6 @@ #include "common/Platform.h" #include "core/config/Config.h" #include "core/Controller.h" -#include "crypto/CryptoNight.h" #include "Mem.h" #include "net/Network.h" #include "Summary.h" diff --git a/src/Mem.cpp b/src/Mem.cpp index 01a2157b..4ae1971f 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -24,9 +24,13 @@ */ -#include "common/utils/mm_malloc.h" -#include "crypto/CryptoNight.h" -#include "crypto/CryptoNight_constants.h" +#include + + +#include "crypto/cn/CryptoNight_constants.h" +#include "crypto/cn/CryptoNight.h" +#include "crypto/common/portable/mm_malloc.h" +#include "crypto/common/VirtualMemory.h" #include "Mem.h" @@ -51,13 +55,9 @@ MemInfo Mem::create(cryptonight_ctx **ctx, xmrig::Algo algorithm, size_t count) cryptonight_ctx *c = static_cast(_mm_malloc(sizeof(cryptonight_ctx), 4096)); c->memory = info.memory + (i * cn_select_memory(algorithm)); - uint8_t* p = reinterpret_cast(allocateExecutableMemory(0x4000)); - c->generated_code = reinterpret_cast(p); - c->generated_code_double = reinterpret_cast(p + 0x2000); - + c->generated_code = reinterpret_cast(xmrig::VirtualMemory::allocateExecutableMemory(0x4000)); c->generated_code_data.variant = xmrig::VARIANT_MAX; - c->generated_code_data.height = (uint64_t)(-1); - c->generated_code_double_data = c->generated_code_data; + c->generated_code_data.height = std::numeric_limits::max(); ctx[i] = c; } @@ -68,6 +68,10 @@ MemInfo Mem::create(cryptonight_ctx **ctx, xmrig::Algo algorithm, size_t count) void Mem::release(cryptonight_ctx **ctx, size_t count, MemInfo &info) { + if (info.memory == nullptr) { + return; + } + release(info); for (size_t i = 0; i < count; ++i) { diff --git a/src/Mem.h b/src/Mem.h index 9e39e963..629f5baa 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -39,11 +39,11 @@ struct cryptonight_ctx; struct MemInfo { - alignas(16) uint8_t *memory; + alignas(16) uint8_t *memory = nullptr; - size_t hugePages; - size_t pages; - size_t size; + size_t hugePages = 0; + size_t pages = 0; + size_t size = 0; }; @@ -60,10 +60,6 @@ public: static void init(bool enabled); static void release(cryptonight_ctx **ctx, size_t count, MemInfo &info); - static void *allocateExecutableMemory(size_t size); - static void protectExecutableMemory(void *p, size_t size); - static void flushInstructionCache(void *p, size_t size); - static inline bool isHugepagesAvailable() { return (m_flags & HugepagesAvailable) != 0; } private: diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 3506c2d1..9bdce0f5 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -29,9 +29,10 @@ #include "base/io/log/Log.h" -#include "common/utils/mm_malloc.h" #include "common/xmrig.h" -#include "crypto/CryptoNight.h" +#include "crypto/common/portable/mm_malloc.h" +#include "crypto/common/VirtualMemory.h" +#include "crypto/cn/CryptoNight.h" #include "Mem.h" @@ -56,15 +57,8 @@ void Mem::allocate(MemInfo &info, bool enabled) return; } -# if defined(__APPLE__) - info.memory = static_cast(mmap(0, info.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); -# elif defined(__FreeBSD__) - info.memory = static_cast(mmap(0, info.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); -# else - info.memory = static_cast(mmap(0, info.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); -# endif - - if (info.memory == MAP_FAILED) { + info.memory = static_cast(xmrig::VirtualMemory::allocateLargePagesMemory(info.size)); + if (!info.memory) { return allocate(info, false);; } @@ -87,33 +81,9 @@ void Mem::release(MemInfo &info) munlock(info.memory, info.size); } - munmap(info.memory, info.size); + xmrig::VirtualMemory::freeLargePagesMemory(info.memory, info.size); } else { _mm_free(info.memory); } } - - -void *Mem::allocateExecutableMemory(size_t size) -{ -# if defined(__APPLE__) - return mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON, -1, 0); -# else - return mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); -# endif -} - - -void Mem::protectExecutableMemory(void *p, size_t size) -{ - mprotect(p, size, PROT_READ | PROT_EXEC); -} - - -void Mem::flushInstructionCache(void *p, size_t size) -{ -# ifndef __FreeBSD__ - __builtin___clear_cache(reinterpret_cast(p), reinterpret_cast(p) + size); -# endif -} diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index e7afb5d3..76cbf434 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -31,10 +31,11 @@ #include "base/io/log/Log.h" -#include "common/utils/mm_malloc.h" #include "common/xmrig.h" -#include "crypto/CryptoNight.h" -#include "crypto/CryptoNight_constants.h" +#include "crypto/common/portable/mm_malloc.h" +#include "crypto/common/VirtualMemory.h" +#include "crypto/cn/CryptoNight_constants.h" +#include "crypto/cn/CryptoNight.h" #include "Mem.h" @@ -163,7 +164,7 @@ void Mem::allocate(MemInfo &info, bool enabled) return; } - info.memory = static_cast(VirtualAlloc(nullptr, info.size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); + info.memory = static_cast(xmrig::VirtualMemory::allocateLargePagesMemory(info.size)); if (info.memory) { info.hugePages = info.pages; @@ -177,28 +178,9 @@ void Mem::allocate(MemInfo &info, bool enabled) void Mem::release(MemInfo &info) { if (info.hugePages) { - VirtualFree(info.memory, 0, MEM_RELEASE); + xmrig::VirtualMemory::freeLargePagesMemory(info.memory, info.size); } else { _mm_free(info.memory); } } - - -void *Mem::allocateExecutableMemory(size_t size) -{ - return VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); -} - - -void Mem::protectExecutableMemory(void *p, size_t size) -{ - DWORD oldProtect; - VirtualProtect(p, size, PAGE_EXECUTE_READ, &oldProtect); -} - - -void Mem::flushInstructionCache(void *p, size_t size) -{ - ::FlushInstructionCache(GetCurrentProcess(), p, size); -} diff --git a/src/Summary.cpp b/src/Summary.cpp index 697e2373..2b28f98d 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -33,7 +33,7 @@ #include "common/cpu/Cpu.h" #include "core/config/Config.h" #include "core/Controller.h" -#include "crypto/Asm.h" +#include "crypto/cn/Asm.h" #include "Mem.h" #include "Summary.h" #include "version.h" diff --git a/src/base/kernel/interfaces/IConfig.h b/src/base/kernel/interfaces/IConfig.h index 07849e35..3d0407e6 100644 --- a/src/base/kernel/interfaces/IConfig.h +++ b/src/base/kernel/interfaces/IConfig.h @@ -26,7 +26,7 @@ #define XMRIG_ICONFIG_H -#include "common/crypto/Algorithm.h" +#include "crypto/common/Algorithm.h" #include "rapidjson/fwd.h" diff --git a/src/base/kernel/interfaces/IConfigTransform.h b/src/base/kernel/interfaces/IConfigTransform.h index 37ceaba1..f8854388 100644 --- a/src/base/kernel/interfaces/IConfigTransform.h +++ b/src/base/kernel/interfaces/IConfigTransform.h @@ -26,7 +26,7 @@ #define XMRIG_ICONFIGTRANSFORM_H -#include "common/crypto/Algorithm.h" +#include "crypto/common/Algorithm.h" #include "rapidjson/fwd.h" diff --git a/src/base/net/stratum/Client.cpp b/src/base/net/stratum/Client.cpp index 1d448ddf..7ee0228b 100644 --- a/src/base/net/stratum/Client.cpp +++ b/src/base/net/stratum/Client.cpp @@ -37,6 +37,7 @@ #endif +#include "base/io/json/Json.h" #include "base/io/json/JsonRequest.h" #include "base/io/log/Log.h" #include "base/kernel/interfaces/IClientListener.h" @@ -344,13 +345,8 @@ bool xmrig::Client::parseJob(const rapidjson::Value ¶ms, int *code) } } - if (params.HasMember("height")) { - const rapidjson::Value &variant = params["height"]; - - if (variant.IsUint64()) { - job.setHeight(variant.GetUint64()); - } - } + job.setSeedHash(Json::getString(params, "seed_hash")); + job.setHeight(Json::getUint64(params, "height")); if (!verifyAlgorithm(job.algorithm())) { *code = 6; diff --git a/src/base/net/stratum/Client.h b/src/base/net/stratum/Client.h index c7aeabfe..841e0e0b 100644 --- a/src/base/net/stratum/Client.h +++ b/src/base/net/stratum/Client.h @@ -40,7 +40,7 @@ #include "base/net/stratum/SubmitResult.h" #include "base/net/tools/RecvBuf.h" #include "base/net/tools/Storage.h" -#include "common/crypto/Algorithm.h" +#include "crypto/common/Algorithm.h" typedef struct bio_st BIO; diff --git a/src/base/net/stratum/DaemonClient.cpp b/src/base/net/stratum/DaemonClient.cpp index 769e2116..70cc9151 100644 --- a/src/base/net/stratum/DaemonClient.cpp +++ b/src/base/net/stratum/DaemonClient.cpp @@ -220,6 +220,7 @@ bool xmrig::DaemonClient::parseJob(const rapidjson::Value ¶ms, int *code) return false; } + job.setSeedHash(Json::getString(params, "seed_hash")); job.setHeight(Json::getUint64(params, kHeight)); job.setDiff(Json::getUint64(params, "difficulty")); job.setId(blocktemplate.data() + blocktemplate.size() - 32); diff --git a/src/base/net/stratum/Job.cpp b/src/base/net/stratum/Job.cpp index 1f1cd413..b4ebb2c0 100644 --- a/src/base/net/stratum/Job.cpp +++ b/src/base/net/stratum/Job.cpp @@ -42,7 +42,8 @@ xmrig::Job::Job() : m_diff(0), m_height(0), m_target(0), - m_blob() + m_blob(), + m_seedHash() { } @@ -58,7 +59,8 @@ xmrig::Job::Job(int poolId, bool nicehash, const Algorithm &algorithm, const Str m_diff(0), m_height(0), m_target(0), - m_blob() + m_blob(), + m_seedHash() { } @@ -111,6 +113,20 @@ bool xmrig::Job::setBlob(const char *blob) } +bool xmrig::Job::setSeedHash(const char *hash) +{ + if (!hash || (strlen(hash) != sizeof(m_seedHash) * 2)) { + return false; + } + +# ifdef XMRIG_PROXY_PROJECT + m_rawSeedHash = hash; +# endif + + return Buffer::fromHex(hash, sizeof(m_seedHash) * 2, m_seedHash); +} + + bool xmrig::Job::setTarget(const char *target) { if (!target) { diff --git a/src/base/net/stratum/Job.h b/src/base/net/stratum/Job.h index 16e9a861..bc0ec2eb 100644 --- a/src/base/net/stratum/Job.h +++ b/src/base/net/stratum/Job.h @@ -33,7 +33,7 @@ #include "base/tools/String.h" -#include "common/crypto/Algorithm.h" +#include "crypto/common/Algorithm.h" namespace xmrig { @@ -52,6 +52,7 @@ public: bool isEqual(const Job &other) const; bool setBlob(const char *blob); + bool setSeedHash(const char *hash); bool setTarget(const char *target); void setAlgorithm(const char *algo); void setDiff(uint64_t diff); @@ -64,6 +65,7 @@ public: inline const String &id() const { return m_id; } inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } inline const uint8_t *blob() const { return m_blob; } + inline const uint8_t *seedHash() const { return m_seedHash; } inline int poolId() const { return m_poolId; } inline int threadId() const { return m_threadId; } inline size_t size() const { return m_size; } @@ -81,9 +83,10 @@ public: inline void setVariant(int variant) { m_algorithm.parseVariant(variant); } # ifdef XMRIG_PROXY_PROJECT - inline char *rawBlob() { return m_rawBlob; } - inline const char *rawBlob() const { return m_rawBlob; } - inline const char *rawTarget() const { return m_rawTarget; } + inline char *rawBlob() { return m_rawBlob; } + inline const char *rawBlob() const { return m_rawBlob; } + inline const char *rawTarget() const { return m_rawTarget; } + inline const String &rawSeedHash() const { return m_rawSeedHash; } # endif static inline uint32_t *nonce(uint8_t *blob) { return reinterpret_cast(blob + 39); } @@ -107,10 +110,12 @@ private: uint64_t m_height; uint64_t m_target; uint8_t m_blob[kMaxBlobSize]; + uint8_t m_seedHash[32]; # ifdef XMRIG_PROXY_PROJECT char m_rawBlob[kMaxBlobSize * 2 + 8]; char m_rawTarget[24]; + String m_rawSeedHash; # endif }; diff --git a/src/base/net/stratum/Pool.cpp b/src/base/net/stratum/Pool.cpp index f441ba63..d3b4b4a3 100644 --- a/src/base/net/stratum/Pool.cpp +++ b/src/base/net/stratum/Pool.cpp @@ -517,6 +517,7 @@ void xmrig::Pool::rebuild() addVariant(VARIANT_RWZ); addVariant(VARIANT_ZLS); addVariant(VARIANT_DOUBLE); + addVariant(VARIANT_RX_WOW); addVariant(VARIANT_AUTO); # endif } diff --git a/src/base/net/stratum/Pool.h b/src/base/net/stratum/Pool.h index f7987707..5348271a 100644 --- a/src/base/net/stratum/Pool.h +++ b/src/base/net/stratum/Pool.h @@ -32,7 +32,7 @@ #include "base/tools/String.h" -#include "common/crypto/Algorithm.h" +#include "crypto/common/Algorithm.h" #include "rapidjson/fwd.h" diff --git a/src/common/cpu/BasicCpuInfo_arm.cpp b/src/common/cpu/BasicCpuInfo_arm.cpp index 33961346..dea8de73 100644 --- a/src/common/cpu/BasicCpuInfo_arm.cpp +++ b/src/common/cpu/BasicCpuInfo_arm.cpp @@ -25,7 +25,8 @@ #include #include -#if __ARM_FEATURE_CRYPTO + +#if __ARM_FEATURE_CRYPTO && !defined(__APPLE__) # include # include #endif @@ -47,7 +48,11 @@ xmrig::BasicCpuInfo::BasicCpuInfo() : # endif # if __ARM_FEATURE_CRYPTO +# if !defined(__APPLE__) m_aes = getauxval(AT_HWCAP) & HWCAP_AES; +# else + m_aes = true; +# endif # endif } diff --git a/src/common/xmrig.h b/src/common/xmrig.h index e8ca8857..e8aa505a 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -36,6 +36,7 @@ enum Algo { CRYPTONIGHT_LITE, /* CryptoNight (1 MB) */ CRYPTONIGHT_HEAVY, /* CryptoNight (4 MB) */ CRYPTONIGHT_PICO, /* CryptoNight (256 KB) */ + RANDOM_X, /* RandomX */ ALGO_MAX }; @@ -79,6 +80,7 @@ enum Variant { VARIANT_RWZ = 14, // CryptoNight variant 2 with 3/4 iterations and reversed shuffle operation (Graft) VARIANT_ZLS = 15, // CryptoNight variant 2 with 3/4 iterations (Zelerius) VARIANT_DOUBLE = 16, // CryptoNight variant 2 with double iterations (X-CASH) + VARIANT_RX_WOW = 17, // RandomX (Wownero) VARIANT_MAX }; diff --git a/src/core/config/Config.cpp b/src/core/config/Config.cpp index 88ddbb72..450f587e 100644 --- a/src/core/config/Config.cpp +++ b/src/core/config/Config.cpp @@ -32,8 +32,8 @@ #include "base/kernel/interfaces/IJsonReader.h" #include "common/cpu/Cpu.h" #include "core/config/Config.h" -#include "crypto/Asm.h" -#include "crypto/CryptoNight_constants.h" +#include "crypto/cn/Asm.h" +#include "crypto/cn/CryptoNight_constants.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" @@ -56,6 +56,12 @@ xmrig::Config::Config() : } +bool xmrig::Config::isHwAES() const +{ + return (m_aesMode == AES_AUTO ? (Cpu::info()->hasAES() ? AES_HW : AES_SOFT) : m_aesMode) == AES_HW; +} + + bool xmrig::Config::read(const IJsonReader &reader, const char *fileName) { if (!BaseConfig::read(reader, fileName)) { @@ -147,11 +153,10 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const bool xmrig::Config::finalize() { if (!m_threads.cpu.empty()) { - m_threads.mode = Advanced; - const bool softAES = (m_aesMode == AES_AUTO ? (Cpu::info()->hasAES() ? AES_HW : AES_SOFT) : m_aesMode) == AES_SOFT; + m_threads.mode = Advanced; for (size_t i = 0; i < m_threads.cpu.size(); ++i) { - m_threads.list.push_back(CpuThread::createFromData(i, m_algorithm.algo(), m_threads.cpu[i], m_priority, softAES)); + m_threads.list.push_back(CpuThread::createFromData(i, m_algorithm.algo(), m_threads.cpu[i], m_priority, !isHwAES())); } return true; @@ -160,7 +165,8 @@ bool xmrig::Config::finalize() const AlgoVariant av = getAlgoVariant(); m_threads.mode = m_threads.count ? Simple : Automatic; - const size_t size = CpuThread::multiway(av) * cn_select_memory(m_algorithm.algo()) / 1024; + const Variant v = m_algorithm.variant(); + const size_t size = CpuThread::multiway(av) * cn_select_memory(m_algorithm.algo(), v) / 1024; if (!m_threads.count) { m_threads.count = Cpu::info()->optimalThreadsCount(size, m_maxCpuUsage); @@ -244,7 +250,7 @@ void xmrig::Config::setThreads(const rapidjson::Value &threads) xmrig::AlgoVariant xmrig::Config::getAlgoVariant() const { -# ifndef XMRIG_NO_AEON +# ifdef XMRIG_ALGO_CN_LITE if (m_algorithm.algo() == xmrig::CRYPTONIGHT_LITE) { return getAlgoVariantLite(); } @@ -262,7 +268,7 @@ xmrig::AlgoVariant xmrig::Config::getAlgoVariant() const } -#ifndef XMRIG_NO_AEON +#ifdef XMRIG_ALGO_CN_LITE xmrig::AlgoVariant xmrig::Config::getAlgoVariantLite() const { if (m_algoVariant <= AV_AUTO || m_algoVariant >= AV_MAX) { diff --git a/src/core/config/Config.h b/src/core/config/Config.h index 861840c7..1d8a42d9 100644 --- a/src/core/config/Config.h +++ b/src/core/config/Config.h @@ -67,10 +67,10 @@ public: Config(); + bool isHwAES() const; bool read(const IJsonReader &reader, const char *fileName) override; void getJSON(rapidjson::Document &doc) const override; - inline AesMode aesMode() const { return m_aesMode; } inline AlgoVariant algoVariant() const { return m_algoVariant; } inline Assembly assembly() const { return m_assembly; } inline bool isHugePages() const { return m_hugePages; } @@ -90,7 +90,7 @@ private: void setThreads(const rapidjson::Value &threads); AlgoVariant getAlgoVariant() const; -# ifndef XMRIG_NO_AEON +# ifdef XMRIG_ALGO_CN_LITE AlgoVariant getAlgoVariantLite() const; # endif diff --git a/src/core/config/usage.h b/src/core/config/usage.h index ce172778..42cbc24a 100644 --- a/src/core/config/usage.h +++ b/src/core/config/usage.h @@ -37,15 +37,15 @@ Usage: " APP_ID " [OPTIONS]\n\ Options:\n\ -a, --algo=ALGO specify the algorithm to use\n\ cryptonight\n" -#ifndef XMRIG_NO_AEON +#ifdef XMRIG_ALGO_CN_LITE "\ cryptonight-lite\n" #endif -#ifndef XMRIG_NO_SUMO +#ifdef XMRIG_ALGO_CN_HEAVY "\ cryptonight-heavy\n" #endif -#ifndef XMRIG_NO_CN_PICO +#ifdef XMRIG_ALGO_CN_PICO "\ cryptonight-pico\n" #endif diff --git a/src/crypto/Asm.cpp b/src/crypto/cn/Asm.cpp similarity index 98% rename from src/crypto/Asm.cpp rename to src/crypto/cn/Asm.cpp index 88812c6c..331c133d 100644 --- a/src/crypto/Asm.cpp +++ b/src/crypto/cn/Asm.cpp @@ -33,7 +33,7 @@ #endif -#include "crypto/Asm.h" +#include "crypto/cn/Asm.h" #include "rapidjson/document.h" diff --git a/src/crypto/Asm.h b/src/crypto/cn/Asm.h similarity index 100% rename from src/crypto/Asm.h rename to src/crypto/cn/Asm.h diff --git a/src/crypto/CryptoNight.h b/src/crypto/cn/CryptoNight.h similarity index 90% rename from src/crypto/CryptoNight.h rename to src/crypto/cn/CryptoNight.h index b1ec2371..f50966ed 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/cn/CryptoNight.h @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett + * Copyright 2018-2019 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -30,14 +31,16 @@ #include #if defined _MSC_VER || defined XMRIG_ARM -#define ABI_ATTRIBUTE +# define ABI_ATTRIBUTE #else -#define ABI_ATTRIBUTE __attribute__((ms_abi)) +# define ABI_ATTRIBUTE __attribute__((ms_abi)) #endif + struct cryptonight_ctx; typedef void(*cn_mainloop_fun_ms_abi)(cryptonight_ctx**) ABI_ATTRIBUTE; + struct cryptonight_r_data { int variant; uint64_t height; @@ -45,17 +48,16 @@ struct cryptonight_r_data { bool match(const int v, const uint64_t h) const { return (v == variant) && (h == height); } }; + struct cryptonight_ctx { alignas(16) uint8_t state[224]; alignas(16) uint8_t *memory; uint8_t unused[40]; - const uint32_t* saes_table; + const uint32_t *saes_table; cn_mainloop_fun_ms_abi generated_code; - cn_mainloop_fun_ms_abi generated_code_double; cryptonight_r_data generated_code_data; - cryptonight_r_data generated_code_double_data; }; diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/cn/CryptoNight_arm.h similarity index 98% rename from src/crypto/CryptoNight_arm.h rename to src/crypto/cn/CryptoNight_arm.h index d762929c..d9be454b 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/cn/CryptoNight_arm.h @@ -29,19 +29,19 @@ #include "common/crypto/keccak.h" -#include "common/utils/mm_malloc.h" -#include "crypto/CryptoNight.h" -#include "crypto/CryptoNight_constants.h" -#include "crypto/CryptoNight_monero.h" -#include "crypto/soft_aes.h" +#include "crypto/common/portable/mm_malloc.h" +#include "crypto/cn/CryptoNight_constants.h" +#include "crypto/cn/CryptoNight_monero.h" +#include "crypto/cn/CryptoNight.h" +#include "crypto/cn/soft_aes.h" extern "C" { -#include "crypto/c_groestl.h" -#include "crypto/c_blake256.h" -#include "crypto/c_jh.h" -#include "crypto/c_skein.h" +#include "crypto/cn/c_groestl.h" +#include "crypto/cn/c_blake256.h" +#include "crypto/cn/c_jh.h" +#include "crypto/cn/c_skein.h" } @@ -284,7 +284,7 @@ static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) } -#ifndef XMRIG_NO_CN_GPU +#ifdef XMRIG_ALGO_CN_GPU template void cn_explode_scratchpad_gpu(const uint8_t *input, uint8_t *output) { @@ -583,7 +583,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } -#ifndef XMRIG_NO_CN_GPU +#ifdef XMRIG_ALGO_CN_GPU template void cn_gpu_inner_arm(const uint8_t *spad, uint8_t *lpad); diff --git a/src/crypto/CryptoNight_constants.h b/src/crypto/cn/CryptoNight_constants.h similarity index 98% rename from src/crypto/CryptoNight_constants.h rename to src/crypto/cn/CryptoNight_constants.h index 1bc06a3b..d06369b4 100644 --- a/src/crypto/CryptoNight_constants.h +++ b/src/crypto/cn/CryptoNight_constants.h @@ -70,7 +70,7 @@ template<> inline constexpr size_t cn_select_memory() { retur template<> inline constexpr size_t cn_select_memory() { return CRYPTONIGHT_PICO_MEMORY; } -inline size_t cn_select_memory(Algo algorithm) +inline size_t cn_select_memory(Algo algorithm, Variant v = VARIANT_AUTO) { switch(algorithm) { @@ -86,6 +86,9 @@ inline size_t cn_select_memory(Algo algorithm) case CRYPTONIGHT_PICO: return CRYPTONIGHT_PICO_MEMORY; + case RANDOM_X: + return (v == VARIANT_RX_WOW) ? CRYPTONIGHT_LITE_MEMORY : CRYPTONIGHT_MEMORY; + default: break; } diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/cn/CryptoNight_monero.h similarity index 99% rename from src/crypto/CryptoNight_monero.h rename to src/crypto/cn/CryptoNight_monero.h index 4e84ac5d..94a18c45 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/cn/CryptoNight_monero.h @@ -179,7 +179,7 @@ #endif #include "common/xmrig.h" -#include "variant4_random_math.h" +#include "crypto/cn/r/variant4_random_math.h" #define VARIANT4_RANDOM_MATH_INIT(part) \ uint32_t r##part[9]; \ diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/cn/CryptoNight_test.h similarity index 99% rename from src/crypto/CryptoNight_test.h rename to src/crypto/cn/CryptoNight_test.h index 6fa9dd28..2429fc17 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/cn/CryptoNight_test.h @@ -272,7 +272,7 @@ const static uint8_t test_output_double[160] = { 0x5E, 0x2E, 0xC1, 0x80, 0x89, 0x39, 0xB3, 0x54, 0x39, 0x52, 0x0E, 0x69, 0x3D, 0xF6, 0xC5, 0x4A }; -#ifndef XMRIG_NO_AEON +#ifdef XMRIG_ALGO_CN_LITE // "cn-lite/0" const static uint8_t test_output_v0_lite[160] = { 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, @@ -304,7 +304,7 @@ const static uint8_t test_output_v1_lite[160] = { #endif -#ifndef XMRIG_NO_SUMO +#ifdef XMRIG_ALGO_CN_HEAVY // "cn-heavy/0" const static uint8_t test_output_v0_heavy[160] = { 0x99, 0x83, 0xF2, 0x1B, 0xDF, 0x20, 0x10, 0xA8, 0xD7, 0x07, 0xBB, 0x2F, 0x14, 0xD7, 0x86, 0x64, @@ -351,7 +351,7 @@ const static uint8_t test_output_tube_heavy[160] = { #endif -#ifndef XMRIG_NO_CN_PICO +#ifdef XMRIG_ALGO_CN_PICO // "cn-pico/trtl" const static uint8_t test_output_pico_trtl[160] = { 0x08, 0xF4, 0x21, 0xD7, 0x83, 0x31, 0x17, 0x30, 0x0E, 0xDA, 0x66, 0xE9, 0x8F, 0x4A, 0x25, 0x69, @@ -368,7 +368,7 @@ const static uint8_t test_output_pico_trtl[160] = { #endif -#ifndef XMRIG_NO_CN_GPU +#ifdef XMRIG_ALGO_CN_GPU // "cn/gpu" const static uint8_t test_output_gpu[160] = { 0xE5, 0x5C, 0xB2, 0x3E, 0x51, 0x64, 0x9A, 0x59, 0xB1, 0x27, 0xB9, 0x6B, 0x51, 0x5F, 0x2B, 0xF7, diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/cn/CryptoNight_x86.h similarity index 99% rename from src/crypto/CryptoNight_x86.h rename to src/crypto/cn/CryptoNight_x86.h index 202b662a..8d6792d2 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/cn/CryptoNight_x86.h @@ -37,18 +37,18 @@ #include "common/cpu/Cpu.h" #include "common/crypto/keccak.h" -#include "crypto/CryptoNight.h" -#include "crypto/CryptoNight_constants.h" -#include "crypto/CryptoNight_monero.h" -#include "crypto/soft_aes.h" +#include "crypto/cn/CryptoNight.h" +#include "crypto/cn/CryptoNight_constants.h" +#include "crypto/cn/CryptoNight_monero.h" +#include "crypto/cn/soft_aes.h" extern "C" { -#include "crypto/c_groestl.h" -#include "crypto/c_blake256.h" -#include "crypto/c_jh.h" -#include "crypto/c_skein.h" +#include "crypto/cn/c_groestl.h" +#include "crypto/cn/c_blake256.h" +#include "crypto/cn/c_jh.h" +#include "crypto/cn/c_skein.h" } @@ -361,7 +361,7 @@ static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) } -#ifndef XMRIG_NO_CN_GPU +#ifdef XMRIG_ALGO_CN_GPU template void cn_explode_scratchpad_gpu(const uint8_t *input, uint8_t *output) { @@ -708,7 +708,7 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si } -#ifndef XMRIG_NO_CN_GPU +#ifdef XMRIG_ALGO_CN_GPU template void cn_gpu_inner_avx(const uint8_t *spad, uint8_t *lpad); @@ -895,12 +895,12 @@ inline void cryptonight_double_hash_asm(const uint8_t *__restrict__ input, size_ { constexpr size_t MEM = xmrig::cn_select_memory(); - if (xmrig::cn_is_cryptonight_r() && !ctx[0]->generated_code_double_data.match(VARIANT, height)) { + if (xmrig::cn_is_cryptonight_r() && !ctx[0]->generated_code_data.match(VARIANT, height)) { V4_Instruction code[256]; const int code_size = v4_random_math_init(code, height); - cn_r_compile_code_double(code, code_size, reinterpret_cast(ctx[0]->generated_code_double), ASM); - ctx[0]->generated_code_double_data.variant = VARIANT; - ctx[0]->generated_code_double_data.height = height; + cn_r_compile_code_double(code, code_size, reinterpret_cast(ctx[0]->generated_code), ASM); + ctx[0]->generated_code_data.variant = VARIANT; + ctx[0]->generated_code_data.height = height; } xmrig::keccak(input, size, ctx[0]->state); @@ -928,7 +928,7 @@ inline void cryptonight_double_hash_asm(const uint8_t *__restrict__ input, size_ cn_double_double_mainloop_sandybridge_asm(ctx); } else if (xmrig::cn_is_cryptonight_r()) { - ctx[0]->generated_code_double(ctx); + ctx[0]->generated_code(ctx); } cn_implode_scratchpad(reinterpret_cast<__m128i*>(ctx[0]->memory), reinterpret_cast<__m128i*>(ctx[0]->state)); diff --git a/src/crypto/SSE2NEON.h b/src/crypto/cn/SSE2NEON.h similarity index 100% rename from src/crypto/SSE2NEON.h rename to src/crypto/cn/SSE2NEON.h diff --git a/src/crypto/asm/CryptonightR_soft_aes_template.inc b/src/crypto/cn/asm/CryptonightR_soft_aes_template.inc similarity index 100% rename from src/crypto/asm/CryptonightR_soft_aes_template.inc rename to src/crypto/cn/asm/CryptonightR_soft_aes_template.inc diff --git a/src/crypto/asm/CryptonightR_soft_aes_template_win.inc b/src/crypto/cn/asm/CryptonightR_soft_aes_template_win.inc similarity index 100% rename from src/crypto/asm/CryptonightR_soft_aes_template_win.inc rename to src/crypto/cn/asm/CryptonightR_soft_aes_template_win.inc diff --git a/src/crypto/asm/CryptonightR_template.S b/src/crypto/cn/asm/CryptonightR_template.S similarity index 100% rename from src/crypto/asm/CryptonightR_template.S rename to src/crypto/cn/asm/CryptonightR_template.S diff --git a/src/crypto/asm/CryptonightR_template.asm b/src/crypto/cn/asm/CryptonightR_template.asm similarity index 100% rename from src/crypto/asm/CryptonightR_template.asm rename to src/crypto/cn/asm/CryptonightR_template.asm diff --git a/src/crypto/asm/CryptonightR_template.h b/src/crypto/cn/asm/CryptonightR_template.h similarity index 100% rename from src/crypto/asm/CryptonightR_template.h rename to src/crypto/cn/asm/CryptonightR_template.h diff --git a/src/crypto/asm/CryptonightR_template.inc b/src/crypto/cn/asm/CryptonightR_template.inc similarity index 100% rename from src/crypto/asm/CryptonightR_template.inc rename to src/crypto/cn/asm/CryptonightR_template.inc diff --git a/src/crypto/asm/CryptonightR_template_win.inc b/src/crypto/cn/asm/CryptonightR_template_win.inc similarity index 100% rename from src/crypto/asm/CryptonightR_template_win.inc rename to src/crypto/cn/asm/CryptonightR_template_win.inc diff --git a/src/crypto/asm/CryptonightWOW_soft_aes_template.inc b/src/crypto/cn/asm/CryptonightWOW_soft_aes_template.inc similarity index 100% rename from src/crypto/asm/CryptonightWOW_soft_aes_template.inc rename to src/crypto/cn/asm/CryptonightWOW_soft_aes_template.inc diff --git a/src/crypto/asm/CryptonightWOW_soft_aes_template_win.inc b/src/crypto/cn/asm/CryptonightWOW_soft_aes_template_win.inc similarity index 100% rename from src/crypto/asm/CryptonightWOW_soft_aes_template_win.inc rename to src/crypto/cn/asm/CryptonightWOW_soft_aes_template_win.inc diff --git a/src/crypto/asm/CryptonightWOW_template.inc b/src/crypto/cn/asm/CryptonightWOW_template.inc similarity index 100% rename from src/crypto/asm/CryptonightWOW_template.inc rename to src/crypto/cn/asm/CryptonightWOW_template.inc diff --git a/src/crypto/asm/CryptonightWOW_template_win.inc b/src/crypto/cn/asm/CryptonightWOW_template_win.inc similarity index 100% rename from src/crypto/asm/CryptonightWOW_template_win.inc rename to src/crypto/cn/asm/CryptonightWOW_template_win.inc diff --git a/src/crypto/asm/cn2/cnv2_double_main_loop_sandybridge.inc b/src/crypto/cn/asm/cn2/cnv2_double_main_loop_sandybridge.inc similarity index 100% rename from src/crypto/asm/cn2/cnv2_double_main_loop_sandybridge.inc rename to src/crypto/cn/asm/cn2/cnv2_double_main_loop_sandybridge.inc diff --git a/src/crypto/asm/cn2/cnv2_main_loop_bulldozer.inc b/src/crypto/cn/asm/cn2/cnv2_main_loop_bulldozer.inc similarity index 100% rename from src/crypto/asm/cn2/cnv2_main_loop_bulldozer.inc rename to src/crypto/cn/asm/cn2/cnv2_main_loop_bulldozer.inc diff --git a/src/crypto/asm/cn2/cnv2_main_loop_ivybridge.inc b/src/crypto/cn/asm/cn2/cnv2_main_loop_ivybridge.inc similarity index 100% rename from src/crypto/asm/cn2/cnv2_main_loop_ivybridge.inc rename to src/crypto/cn/asm/cn2/cnv2_main_loop_ivybridge.inc diff --git a/src/crypto/asm/cn2/cnv2_main_loop_ryzen.inc b/src/crypto/cn/asm/cn2/cnv2_main_loop_ryzen.inc similarity index 100% rename from src/crypto/asm/cn2/cnv2_main_loop_ryzen.inc rename to src/crypto/cn/asm/cn2/cnv2_main_loop_ryzen.inc diff --git a/src/crypto/asm/cn2/cnv2_rwz_double_main_loop.inc b/src/crypto/cn/asm/cn2/cnv2_rwz_double_main_loop.inc similarity index 100% rename from src/crypto/asm/cn2/cnv2_rwz_double_main_loop.inc rename to src/crypto/cn/asm/cn2/cnv2_rwz_double_main_loop.inc diff --git a/src/crypto/asm/cn2/cnv2_rwz_main_loop.inc b/src/crypto/cn/asm/cn2/cnv2_rwz_main_loop.inc similarity index 100% rename from src/crypto/asm/cn2/cnv2_rwz_main_loop.inc rename to src/crypto/cn/asm/cn2/cnv2_rwz_main_loop.inc diff --git a/src/crypto/asm/cn_main_loop.S b/src/crypto/cn/asm/cn_main_loop.S similarity index 100% rename from src/crypto/asm/cn_main_loop.S rename to src/crypto/cn/asm/cn_main_loop.S diff --git a/src/crypto/asm/cn_main_loop.asm b/src/crypto/cn/asm/cn_main_loop.asm similarity index 100% rename from src/crypto/asm/cn_main_loop.asm rename to src/crypto/cn/asm/cn_main_loop.asm diff --git a/src/crypto/asm/win64/CryptonightR_soft_aes_template_win.inc b/src/crypto/cn/asm/win64/CryptonightR_soft_aes_template_win.inc similarity index 100% rename from src/crypto/asm/win64/CryptonightR_soft_aes_template_win.inc rename to src/crypto/cn/asm/win64/CryptonightR_soft_aes_template_win.inc diff --git a/src/crypto/asm/win64/CryptonightR_template.asm b/src/crypto/cn/asm/win64/CryptonightR_template.asm similarity index 100% rename from src/crypto/asm/win64/CryptonightR_template.asm rename to src/crypto/cn/asm/win64/CryptonightR_template.asm diff --git a/src/crypto/asm/win64/CryptonightR_template_win.inc b/src/crypto/cn/asm/win64/CryptonightR_template_win.inc similarity index 100% rename from src/crypto/asm/win64/CryptonightR_template_win.inc rename to src/crypto/cn/asm/win64/CryptonightR_template_win.inc diff --git a/src/crypto/asm/win64/CryptonightWOW_soft_aes_template_win.inc b/src/crypto/cn/asm/win64/CryptonightWOW_soft_aes_template_win.inc similarity index 100% rename from src/crypto/asm/win64/CryptonightWOW_soft_aes_template_win.inc rename to src/crypto/cn/asm/win64/CryptonightWOW_soft_aes_template_win.inc diff --git a/src/crypto/asm/win64/CryptonightWOW_template_win.inc b/src/crypto/cn/asm/win64/CryptonightWOW_template_win.inc similarity index 100% rename from src/crypto/asm/win64/CryptonightWOW_template_win.inc rename to src/crypto/cn/asm/win64/CryptonightWOW_template_win.inc diff --git a/src/crypto/asm/win64/cn2/cnv2_double_main_loop_sandybridge.inc b/src/crypto/cn/asm/win64/cn2/cnv2_double_main_loop_sandybridge.inc similarity index 100% rename from src/crypto/asm/win64/cn2/cnv2_double_main_loop_sandybridge.inc rename to src/crypto/cn/asm/win64/cn2/cnv2_double_main_loop_sandybridge.inc diff --git a/src/crypto/asm/win64/cn2/cnv2_main_loop_bulldozer.inc b/src/crypto/cn/asm/win64/cn2/cnv2_main_loop_bulldozer.inc similarity index 100% rename from src/crypto/asm/win64/cn2/cnv2_main_loop_bulldozer.inc rename to src/crypto/cn/asm/win64/cn2/cnv2_main_loop_bulldozer.inc diff --git a/src/crypto/asm/win64/cn2/cnv2_main_loop_ivybridge.inc b/src/crypto/cn/asm/win64/cn2/cnv2_main_loop_ivybridge.inc similarity index 100% rename from src/crypto/asm/win64/cn2/cnv2_main_loop_ivybridge.inc rename to src/crypto/cn/asm/win64/cn2/cnv2_main_loop_ivybridge.inc diff --git a/src/crypto/asm/win64/cn2/cnv2_main_loop_ryzen.inc b/src/crypto/cn/asm/win64/cn2/cnv2_main_loop_ryzen.inc similarity index 100% rename from src/crypto/asm/win64/cn2/cnv2_main_loop_ryzen.inc rename to src/crypto/cn/asm/win64/cn2/cnv2_main_loop_ryzen.inc diff --git a/src/crypto/asm/win64/cn2/cnv2_rwz_double_main_loop.inc b/src/crypto/cn/asm/win64/cn2/cnv2_rwz_double_main_loop.inc similarity index 100% rename from src/crypto/asm/win64/cn2/cnv2_rwz_double_main_loop.inc rename to src/crypto/cn/asm/win64/cn2/cnv2_rwz_double_main_loop.inc diff --git a/src/crypto/asm/win64/cn2/cnv2_rwz_main_loop.inc b/src/crypto/cn/asm/win64/cn2/cnv2_rwz_main_loop.inc similarity index 100% rename from src/crypto/asm/win64/cn2/cnv2_rwz_main_loop.inc rename to src/crypto/cn/asm/win64/cn2/cnv2_rwz_main_loop.inc diff --git a/src/crypto/asm/win64/cn_main_loop.S b/src/crypto/cn/asm/win64/cn_main_loop.S similarity index 100% rename from src/crypto/asm/win64/cn_main_loop.S rename to src/crypto/cn/asm/win64/cn_main_loop.S diff --git a/src/crypto/asm/win64/cn_main_loop.asm b/src/crypto/cn/asm/win64/cn_main_loop.asm similarity index 100% rename from src/crypto/asm/win64/cn_main_loop.asm rename to src/crypto/cn/asm/win64/cn_main_loop.asm diff --git a/src/crypto/c_blake256.c b/src/crypto/cn/c_blake256.c similarity index 100% rename from src/crypto/c_blake256.c rename to src/crypto/cn/c_blake256.c diff --git a/src/crypto/c_blake256.h b/src/crypto/cn/c_blake256.h similarity index 100% rename from src/crypto/c_blake256.h rename to src/crypto/cn/c_blake256.h diff --git a/src/crypto/c_groestl.c b/src/crypto/cn/c_groestl.c similarity index 100% rename from src/crypto/c_groestl.c rename to src/crypto/cn/c_groestl.c diff --git a/src/crypto/c_groestl.h b/src/crypto/cn/c_groestl.h similarity index 100% rename from src/crypto/c_groestl.h rename to src/crypto/cn/c_groestl.h diff --git a/src/crypto/c_jh.c b/src/crypto/cn/c_jh.c similarity index 100% rename from src/crypto/c_jh.c rename to src/crypto/cn/c_jh.c diff --git a/src/crypto/c_jh.h b/src/crypto/cn/c_jh.h similarity index 100% rename from src/crypto/c_jh.h rename to src/crypto/cn/c_jh.h diff --git a/src/crypto/c_skein.c b/src/crypto/cn/c_skein.c similarity index 100% rename from src/crypto/c_skein.c rename to src/crypto/cn/c_skein.c diff --git a/src/crypto/c_skein.h b/src/crypto/cn/c_skein.h similarity index 100% rename from src/crypto/c_skein.h rename to src/crypto/cn/c_skein.h diff --git a/src/crypto/cn_gpu_arm.cpp b/src/crypto/cn/gpu/cn_gpu_arm.cpp similarity index 99% rename from src/crypto/cn_gpu_arm.cpp rename to src/crypto/cn/gpu/cn_gpu_arm.cpp index b463dd2e..a1df0cc7 100644 --- a/src/crypto/cn_gpu_arm.cpp +++ b/src/crypto/cn/gpu/cn_gpu_arm.cpp @@ -26,7 +26,7 @@ #include -#include "crypto/CryptoNight_constants.h" +#include "crypto/cn/CryptoNight_constants.h" inline void vandq_f32(float32x4_t &v, uint32_t v2) diff --git a/src/crypto/cn_gpu_avx.cpp b/src/crypto/cn/gpu/cn_gpu_avx.cpp similarity index 99% rename from src/crypto/cn_gpu_avx.cpp rename to src/crypto/cn/gpu/cn_gpu_avx.cpp index 9f801c80..382be570 100644 --- a/src/crypto/cn_gpu_avx.cpp +++ b/src/crypto/cn/gpu/cn_gpu_avx.cpp @@ -22,7 +22,7 @@ * along with this program. If not, see . */ -#include "crypto/CryptoNight_constants.h" +#include "crypto/cn/CryptoNight_constants.h" #ifdef __GNUC__ # include diff --git a/src/crypto/cn_gpu_ssse3.cpp b/src/crypto/cn/gpu/cn_gpu_ssse3.cpp similarity index 99% rename from src/crypto/cn_gpu_ssse3.cpp rename to src/crypto/cn/gpu/cn_gpu_ssse3.cpp index ce3d19ad..42a11a1d 100644 --- a/src/crypto/cn_gpu_ssse3.cpp +++ b/src/crypto/cn/gpu/cn_gpu_ssse3.cpp @@ -22,7 +22,7 @@ * along with this program. If not, see . */ -#include "crypto/CryptoNight_constants.h" +#include "crypto/cn/CryptoNight_constants.h" #ifdef __GNUC__ # include diff --git a/src/crypto/groestl_tables.h b/src/crypto/cn/groestl_tables.h similarity index 100% rename from src/crypto/groestl_tables.h rename to src/crypto/cn/groestl_tables.h diff --git a/src/crypto/hash.h b/src/crypto/cn/hash.h similarity index 100% rename from src/crypto/hash.h rename to src/crypto/cn/hash.h diff --git a/src/crypto/CryptonightR_gen.cpp b/src/crypto/cn/r/CryptonightR_gen.cpp similarity index 93% rename from src/crypto/CryptonightR_gen.cpp rename to src/crypto/cn/r/CryptonightR_gen.cpp index 3fba49cd..8491a33b 100644 --- a/src/crypto/CryptonightR_gen.cpp +++ b/src/crypto/cn/r/CryptonightR_gen.cpp @@ -24,11 +24,12 @@ */ #include -#include "crypto/CryptoNight_monero.h" +#include "crypto/cn/CryptoNight_monero.h" typedef void(*void_func)(); -#include "crypto/asm/CryptonightR_template.h" +#include "crypto/cn/asm/CryptonightR_template.h" +#include "crypto/common/VirtualMemory.h" #include "Mem.h" @@ -109,7 +110,7 @@ void wow_compile_code(const V4_Instruction* code, int code_size, void* machine_c *(int*)(p - 4) = static_cast((((const uint8_t*)CryptonightWOW_template_mainloop) - ((const uint8_t*)CryptonightWOW_template_part1)) - (p - p0)); add_code(p, CryptonightWOW_template_part3, CryptonightWOW_template_end); - Mem::flushInstructionCache(machine_code, p - p0); + xmrig::VirtualMemory::flushInstructionCache(machine_code, p - p0); } void v4_compile_code(const V4_Instruction* code, int code_size, void* machine_code, xmrig::Assembly ASM) @@ -123,7 +124,7 @@ void v4_compile_code(const V4_Instruction* code, int code_size, void* machine_co *(int*)(p - 4) = static_cast((((const uint8_t*)CryptonightR_template_mainloop) - ((const uint8_t*)CryptonightR_template_part1)) - (p - p0)); add_code(p, CryptonightR_template_part3, CryptonightR_template_end); - Mem::flushInstructionCache(machine_code, p - p0); + xmrig::VirtualMemory::flushInstructionCache(machine_code, p - p0); } void wow_compile_code_double(const V4_Instruction* code, int code_size, void* machine_code, xmrig::Assembly ASM) @@ -139,7 +140,7 @@ void wow_compile_code_double(const V4_Instruction* code, int code_size, void* ma *(int*)(p - 4) = static_cast((((const uint8_t*)CryptonightWOW_template_double_mainloop) - ((const uint8_t*)CryptonightWOW_template_double_part1)) - (p - p0)); add_code(p, CryptonightWOW_template_double_part4, CryptonightWOW_template_double_end); - Mem::flushInstructionCache(machine_code, p - p0); + xmrig::VirtualMemory::flushInstructionCache(machine_code, p - p0); } void v4_compile_code_double(const V4_Instruction* code, int code_size, void* machine_code, xmrig::Assembly ASM) @@ -155,7 +156,7 @@ void v4_compile_code_double(const V4_Instruction* code, int code_size, void* mac *(int*)(p - 4) = static_cast((((const uint8_t*)CryptonightR_template_double_mainloop) - ((const uint8_t*)CryptonightR_template_double_part1)) - (p - p0)); add_code(p, CryptonightR_template_double_part4, CryptonightR_template_double_end); - Mem::flushInstructionCache(machine_code, p - p0); + xmrig::VirtualMemory::flushInstructionCache(machine_code, p - p0); } void wow_soft_aes_compile_code(const V4_Instruction* code, int code_size, void* machine_code, xmrig::Assembly ASM) @@ -169,7 +170,7 @@ void wow_soft_aes_compile_code(const V4_Instruction* code, int code_size, void* *(int*)(p - 4) = static_cast((((const uint8_t*)CryptonightWOW_soft_aes_template_mainloop) - ((const uint8_t*)CryptonightWOW_soft_aes_template_part1)) - (p - p0)); add_code(p, CryptonightWOW_soft_aes_template_part3, CryptonightWOW_soft_aes_template_end); - Mem::flushInstructionCache(machine_code, p - p0); + xmrig::VirtualMemory::flushInstructionCache(machine_code, p - p0); } void v4_soft_aes_compile_code(const V4_Instruction* code, int code_size, void* machine_code, xmrig::Assembly ASM) @@ -183,5 +184,5 @@ void v4_soft_aes_compile_code(const V4_Instruction* code, int code_size, void* m *(int*)(p - 4) = static_cast((((const uint8_t*)CryptonightR_soft_aes_template_mainloop) - ((const uint8_t*)CryptonightR_soft_aes_template_part1)) - (p - p0)); add_code(p, CryptonightR_soft_aes_template_part3, CryptonightR_soft_aes_template_end); - Mem::flushInstructionCache(machine_code, p - p0); + xmrig::VirtualMemory::flushInstructionCache(machine_code, p - p0); } diff --git a/src/crypto/variant4_random_math.h b/src/crypto/cn/r/variant4_random_math.h similarity index 99% rename from src/crypto/variant4_random_math.h rename to src/crypto/cn/r/variant4_random_math.h index 1f3ea0ac..c384df7a 100644 --- a/src/crypto/variant4_random_math.h +++ b/src/crypto/cn/r/variant4_random_math.h @@ -3,7 +3,7 @@ extern "C" { - #include "c_blake256.h" + #include "crypto/cn/c_blake256.h" } enum V4_Settings diff --git a/src/crypto/skein_port.h b/src/crypto/cn/skein_port.h similarity index 100% rename from src/crypto/skein_port.h rename to src/crypto/cn/skein_port.h diff --git a/src/crypto/soft_aes.h b/src/crypto/cn/soft_aes.h similarity index 99% rename from src/crypto/soft_aes.h rename to src/crypto/cn/soft_aes.h index 4ad9bdd9..fca31d1c 100644 --- a/src/crypto/soft_aes.h +++ b/src/crypto/cn/soft_aes.h @@ -28,7 +28,7 @@ #if defined(XMRIG_ARM) -# include "crypto/SSE2NEON.h" +# include "crypto/cn/SSE2NEON.h" #elif defined(__GNUC__) # include #else diff --git a/src/common/crypto/Algorithm.cpp b/src/crypto/common/Algorithm.cpp similarity index 95% rename from src/common/crypto/Algorithm.cpp rename to src/crypto/common/Algorithm.cpp index d9d3ead9..c706ae0c 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/crypto/common/Algorithm.cpp @@ -30,7 +30,7 @@ #include -#include "common/crypto/Algorithm.h" +#include "crypto/common/Algorithm.h" #ifdef _MSC_VER @@ -70,21 +70,26 @@ static AlgoData const algorithms[] = { { "cryptonight/zls", "cn/zls", xmrig::CRYPTONIGHT, xmrig::VARIANT_ZLS }, { "cryptonight/double", "cn/double", xmrig::CRYPTONIGHT, xmrig::VARIANT_DOUBLE }, -# ifndef XMRIG_NO_AEON +# ifdef XMRIG_ALGO_RANDOMX + { "randomx/wow", "rx/wow", xmrig::RANDOM_X, xmrig::VARIANT_RX_WOW }, + { "randomx", "rx", xmrig::RANDOM_X, xmrig::VARIANT_RX_WOW }, +# endif + +# ifdef XMRIG_ALGO_CN_LITE { "cryptonight-lite", "cn-lite", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, { "cryptonight-light", "cn-light", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, { "cryptonight-lite/0", "cn-lite/0", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_0 }, { "cryptonight-lite/1", "cn-lite/1", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_1 }, # endif -# ifndef XMRIG_NO_SUMO +# ifdef XMRIG_ALGO_CN_HEAVY { "cryptonight-heavy", "cn-heavy", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_AUTO }, { "cryptonight-heavy/0", "cn-heavy/0", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_0 }, { "cryptonight-heavy/xhv", "cn-heavy/xhv", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_XHV }, { "cryptonight-heavy/tube", "cn-heavy/tube", xmrig::CRYPTONIGHT_HEAVY, xmrig::VARIANT_TUBE }, # endif -# ifndef XMRIG_NO_CN_PICO +# ifdef XMRIG_ALGO_CN_PICO { "cryptonight-pico/trtl", "cn-pico/trtl", xmrig::CRYPTONIGHT_PICO, xmrig::VARIANT_TRTL }, { "cryptonight-pico", "cn-pico", xmrig::CRYPTONIGHT_PICO, xmrig::VARIANT_TRTL }, { "cryptonight-turtle", "cn-trtl", xmrig::CRYPTONIGHT_PICO, xmrig::VARIANT_TRTL }, @@ -92,7 +97,7 @@ static AlgoData const algorithms[] = { { "cryptonight_turtle", "cn_turtle", xmrig::CRYPTONIGHT_PICO, xmrig::VARIANT_TRTL }, # endif -# ifndef XMRIG_NO_CN_GPU +# ifdef XMRIG_ALGO_CN_GPU { "cryptonight/gpu", "cn/gpu", xmrig::CRYPTONIGHT, xmrig::VARIANT_GPU }, # endif }; @@ -138,7 +143,8 @@ static const char *variants[] = { "r", "rwz", "zls", - "double" + "double", + "rx/wow", }; diff --git a/src/common/crypto/Algorithm.h b/src/crypto/common/Algorithm.h similarity index 100% rename from src/common/crypto/Algorithm.h rename to src/crypto/common/Algorithm.h diff --git a/src/common/utils/mm_malloc.h b/src/crypto/common/VirtualMemory.h similarity index 57% rename from src/common/utils/mm_malloc.h rename to src/crypto/common/VirtualMemory.h index 30c721a3..e8acb017 100644 --- a/src/common/utils/mm_malloc.h +++ b/src/crypto/common/VirtualMemory.h @@ -5,7 +5,10 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 SChernykh + * Copyright 2018-2019 tevador + * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,23 +24,31 @@ * along with this program. If not, see . */ -#ifndef __MM_MALLOC_PORTABLE_H__ -#define __MM_MALLOC_PORTABLE_H__ +#ifndef XMRIG_VIRTUALMEMORY_H +#define XMRIG_VIRTUALMEMORY_H -#ifdef _WIN32 -# ifdef __GNUC__ -# include -# else -# include -# endif -#else -# if defined(XMRIG_ARM) && !defined(__clang__) -# include "aligned_malloc.h" -# else -# include -# endif -#endif +#include +#include -#endif /* __MM_MALLOC_PORTABLE_H__ */ +namespace xmrig { + + +class VirtualMemory +{ +public: + static void *allocateExecutableMemory(size_t size); + static void *allocateLargePagesMemory(size_t size); + static void flushInstructionCache(void *p, size_t size); + static void freeLargePagesMemory(void *p, size_t size); + static void protectExecutableMemory(void *p, size_t size); + static void unprotectExecutableMemory(void *p, size_t size); +}; + + +} /* namespace xmrig */ + + + +#endif /* XMRIG_VIRTUALMEMORY_H */ diff --git a/src/crypto/common/VirtualMemory_unix.cpp b/src/crypto/common/VirtualMemory_unix.cpp new file mode 100644 index 00000000..beac976d --- /dev/null +++ b/src/crypto/common/VirtualMemory_unix.cpp @@ -0,0 +1,90 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 SChernykh + * Copyright 2018-2019 tevador + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include +#include + + +#include "crypto/common/VirtualMemory.h" + + +#if defined(__APPLE__) +# include +#endif + + + +void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size) +{ +# if defined(__APPLE__) + void *mem = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON, -1, 0); +# else + void *mem = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); +# endif + + return mem == MAP_FAILED ? nullptr : mem; +} + + +void *xmrig::VirtualMemory::allocateLargePagesMemory(size_t size) +{ +# if defined(__APPLE__) + void *mem = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0); +# elif defined(__FreeBSD__) + void *mem = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0); +# else + void *mem = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0); +# endif + + return mem == MAP_FAILED ? nullptr : mem; +} + + +void xmrig::VirtualMemory::flushInstructionCache(void *p, size_t size) +{ +# ifdef HAVE_BUILTIN_CLEAR_CACHE + __builtin___clear_cache(reinterpret_cast(p), reinterpret_cast(p) + size); +# endif +} + + +void xmrig::VirtualMemory::freeLargePagesMemory(void *p, size_t size) +{ + munmap(p, size); +} + + +void xmrig::VirtualMemory::protectExecutableMemory(void *p, size_t size) +{ + mprotect(p, size, PROT_READ | PROT_EXEC); +} + + +void xmrig::VirtualMemory::unprotectExecutableMemory(void *p, size_t size) +{ + mprotect(p, size, PROT_WRITE | PROT_EXEC); +} diff --git a/src/crypto/common/VirtualMemory_win.cpp b/src/crypto/common/VirtualMemory_win.cpp new file mode 100644 index 00000000..dd6be14f --- /dev/null +++ b/src/crypto/common/VirtualMemory_win.cpp @@ -0,0 +1,86 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 SChernykh + * Copyright 2018-2019 tevador + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include +#include + + +#include "crypto/common/VirtualMemory.h" + + +namespace xmrig { + +constexpr size_t align(size_t pos, size_t align) { + return ((pos - 1) / align + 1) * align; +} + +} + + +void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size) +{ + return VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); +} + + +void *xmrig::VirtualMemory::allocateLargePagesMemory(size_t size) +{ + const size_t min = GetLargePageMinimum(); + void *mem = nullptr; + + if (min > 0) { + mem = VirtualAlloc(nullptr, align(size, min), MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE); + } + + return mem; +} + + +void xmrig::VirtualMemory::flushInstructionCache(void *p, size_t size) +{ + ::FlushInstructionCache(GetCurrentProcess(), p, size); +} + + +void xmrig::VirtualMemory::freeLargePagesMemory(void *p, size_t) +{ + VirtualFree(p, 0, MEM_RELEASE); +} + + +void xmrig::VirtualMemory::protectExecutableMemory(void *p, size_t size) +{ + DWORD oldProtect; + VirtualProtect(p, size, PAGE_EXECUTE_READ, &oldProtect); +} + + +void xmrig::VirtualMemory::unprotectExecutableMemory(void *p, size_t size) +{ + DWORD oldProtect; + VirtualProtect(p, size, PAGE_EXECUTE_READWRITE, &oldProtect); +} diff --git a/src/3rdparty/aligned_malloc.h b/src/crypto/common/portable/mm_malloc.h similarity index 57% rename from src/3rdparty/aligned_malloc.h rename to src/crypto/common/portable/mm_malloc.h index 0b74b17e..34ca7d48 100644 --- a/src/3rdparty/aligned_malloc.h +++ b/src/crypto/common/portable/mm_malloc.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,45 +22,50 @@ * along with this program. If not, see . */ -#ifndef __ALIGNED_MALLOC_H__ -#define __ALIGNED_MALLOC_H__ +#ifndef XMRIG_MM_MALLOC_PORTABLE_H +#define XMRIG_MM_MALLOC_PORTABLE_H +#if defined(XMRIG_ARM) && !defined(__clang__) #include #ifndef __cplusplus -extern int posix_memalign(void **__memptr, size_t __alignment, size_t __size); +extern #else -// Some systems (e.g. those with GNU libc) declare posix_memalign with an -// exception specifier. Via an "egregious workaround" in -// Sema::CheckEquivalentExceptionSpec, Clang accepts the following as a valid -// redeclaration of glibc's declaration. -extern "C" int posix_memalign(void **__memptr, size_t __alignment, size_t __size); +extern "C" #endif +int posix_memalign(void **__memptr, size_t __alignment, size_t __size); static __inline__ void *__attribute__((__always_inline__, __malloc__)) _mm_malloc(size_t __size, size_t __align) { - if (__align == 1) { - return malloc(__size); - } + if (__align == 1) { + return malloc(__size); + } - if (!(__align & (__align - 1)) && __align < sizeof(void *)) - __align = sizeof(void *); + if (!(__align & (__align - 1)) && __align < sizeof(void *)) { + __align = sizeof(void *); + } - void *__mallocedMemory; - if (posix_memalign(&__mallocedMemory, __align, __size)) { - return 0; - } + void *__mallocedMemory; + if (posix_memalign(&__mallocedMemory, __align, __size)) { + return nullptr; + } - return __mallocedMemory; + return __mallocedMemory; } static __inline__ void __attribute__((__always_inline__)) _mm_free(void *__p) { - free(__p); + free(__p); } +#elif defined(_WIN32) && !defined(__GNUC__) +# include +#else +# include +#endif -#endif /* __ALIGNED_MALLOC_H__ */ + +#endif /* XMRIG_MM_MALLOC_PORTABLE_H */ diff --git a/src/version.h b/src/version.h index 35831361..3efea229 100644 --- a/src/version.h +++ b/src/version.h @@ -28,15 +28,15 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.15.4-beta" +#define APP_VERSION "2.16.0-evo" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2019 xmrig.com" #define APP_KIND "cpu" #define APP_VER_MAJOR 2 -#define APP_VER_MINOR 15 -#define APP_VER_PATCH 4 +#define APP_VER_MINOR 16 +#define APP_VER_PATCH 0 #ifdef _MSC_VER # if (_MSC_VER >= 1920) diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 2481162c..de99a0cd 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -27,16 +27,17 @@ #include "base/io/log/Log.h" #include "common/cpu/Cpu.h" -#include "crypto/Asm.h" +#include "crypto/cn/Asm.h" +#include "crypto/common/VirtualMemory.h" #include "Mem.h" #include "rapidjson/document.h" #include "workers/CpuThread.h" #if defined(XMRIG_ARM) -# include "crypto/CryptoNight_arm.h" +# include "crypto/cn/CryptoNight_arm.h" #else -# include "crypto/CryptoNight_x86.h" +# include "crypto/cn/CryptoNight_x86.h" #endif @@ -120,7 +121,7 @@ xmrig::CpuThread::cn_mainloop_fun cn_double_double_mainloop_sandybridge_a void xmrig::CpuThread::patchAsmVariants() { const int allocation_size = 65536; - uint8_t *base = static_cast(Mem::allocateExecutableMemory(allocation_size)); + uint8_t *base = static_cast(VirtualMemory::allocateExecutableMemory(allocation_size)); cn_half_mainloop_ivybridge_asm = reinterpret_cast (base + 0x0000); cn_half_mainloop_ryzen_asm = reinterpret_cast (base + 0x1000); @@ -162,8 +163,8 @@ void xmrig::CpuThread::patchAsmVariants() patchCode(cn_double_mainloop_bulldozer_asm, cnv2_mainloop_bulldozer_asm, xmrig::CRYPTONIGHT_DOUBLE_ITER, xmrig::CRYPTONIGHT_MASK); patchCode(cn_double_double_mainloop_sandybridge_asm, cnv2_double_mainloop_sandybridge_asm, xmrig::CRYPTONIGHT_DOUBLE_ITER, xmrig::CRYPTONIGHT_MASK); - Mem::protectExecutableMemory(base, allocation_size); - Mem::flushInstructionCache(base, allocation_size); + VirtualMemory::protectExecutableMemory(base, allocation_size); + VirtualMemory::flushInstructionCache(base, allocation_size); } #endif @@ -206,7 +207,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a add_asm_func(asm_func_map); add_asm_func(asm_func_map); -# ifndef XMRIG_NO_CN_PICO +# ifdef XMRIG_ALGO_CN_PICO add_asm_func(asm_func_map); # endif @@ -320,7 +321,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_TRTL -# ifndef XMRIG_NO_CN_GPU +# ifdef XMRIG_ALGO_CN_GPU cryptonight_single_hash_gpu, nullptr, cryptonight_single_hash_gpu, @@ -389,8 +390,9 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a cryptonight_triple_hash, cryptonight_quad_hash, cryptonight_penta_hash, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW -# ifndef XMRIG_NO_AEON +# ifdef XMRIG_ALGO_CN_LITE cryptonight_single_hash, cryptonight_double_hash, cryptonight_single_hash, @@ -428,6 +430,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_0 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1 @@ -446,9 +449,10 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW # endif -# ifndef XMRIG_NO_SUMO +# ifdef XMRIG_ALGO_CN_HEAVY cryptonight_single_hash, cryptonight_double_hash, cryptonight_single_hash, @@ -498,6 +502,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_0 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1 @@ -516,9 +521,10 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW # endif -# ifndef XMRIG_NO_CN_PICO +# ifdef XMRIG_ALGO_CN_PICO nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_0 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_TUBE @@ -547,6 +553,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW # else nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_0 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1 @@ -565,7 +572,26 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW # endif + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_0 + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1 + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_TUBE + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XTL + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_MSR + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XHV + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_XAO + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RTO + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_2 + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_HALF + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_TRTL + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_GPU + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_WOW + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_4 + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW }; static_assert(count == sizeof(func_table) / sizeof(func_table[0]), "func_table size mismatch"); diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 8b9e0881..d17a2c2e 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -27,7 +27,7 @@ #include -#include "crypto/CryptoNight_test.h" +#include "crypto/cn/CryptoNight_test.h" #include "workers/CpuThread.h" #include "workers/MultiWorker.h" #include "workers/Workers.h" @@ -37,7 +37,9 @@ template MultiWorker::MultiWorker(ThreadHandle *handle) : Worker(handle) { - m_memory = Mem::create(m_ctx, m_thread->algorithm(), N); + if (m_thread->algorithm() != xmrig::RANDOM_X) { + m_memory = Mem::create(m_ctx, m_thread->algorithm(), N); + } } @@ -45,9 +47,34 @@ template MultiWorker::~MultiWorker() { Mem::release(m_ctx, N, m_memory); + +# ifdef XMRIG_ALGO_RANDOMX + if (m_rx_vm) { + randomx_destroy_vm(m_rx_vm); + } +# endif } +#ifdef XMRIG_ALGO_RANDOMX +template +void MultiWorker::allocateRandomX_VM() +{ + if (!m_rx_vm) { + int flags = RANDOMX_FLAG_LARGE_PAGES | RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT; + if (!m_thread->isSoftAES()) { + flags |= RANDOMX_FLAG_HARD_AES; + } + + m_rx_vm = randomx_create_vm(static_cast(flags), nullptr, Workers::getDataset()); + if (!m_rx_vm) { + m_rx_vm = randomx_create_vm(static_cast(flags - RANDOMX_FLAG_LARGE_PAGES), nullptr, Workers::getDataset()); + } + } +} +#endif + + template bool MultiWorker::selfTest() { @@ -68,7 +95,7 @@ bool MultiWorker::selfTest() verify(VARIANT_ZLS, test_output_zls) && verify(VARIANT_DOUBLE, test_output_double); -# ifndef XMRIG_NO_CN_GPU +# ifdef XMRIG_ALGO_CN_GPU if (!rc || N > 1) { return rc; } @@ -79,14 +106,14 @@ bool MultiWorker::selfTest() # endif } -# ifndef XMRIG_NO_AEON +# ifdef XMRIG_ALGO_CN_LITE if (m_thread->algorithm() == CRYPTONIGHT_LITE) { return verify(VARIANT_0, test_output_v0_lite) && verify(VARIANT_1, test_output_v1_lite); } # endif -# ifndef XMRIG_NO_SUMO +# ifdef XMRIG_ALGO_CN_HEAVY if (m_thread->algorithm() == CRYPTONIGHT_HEAVY) { return verify(VARIANT_0, test_output_v0_heavy) && verify(VARIANT_XHV, test_output_xhv_heavy) && @@ -94,12 +121,18 @@ bool MultiWorker::selfTest() } # endif -# ifndef XMRIG_NO_CN_PICO +# ifdef XMRIG_ALGO_CN_PICO if (m_thread->algorithm() == CRYPTONIGHT_PICO) { return verify(VARIANT_TRTL, test_output_pico_trtl); } # endif +# ifdef XMRIG_ALGO_RANDOMX + if (m_thread->algorithm() == RANDOM_X) { + return true; + } +# endif + return false; } @@ -126,7 +159,19 @@ void MultiWorker::start() storeStats(); } - m_thread->fn(m_state.job.algorithm().variant())(m_state.blob, m_state.job.size(), m_hash, m_ctx, m_state.job.height()); + const xmrig::Variant v = m_state.job.algorithm().variant(); + +# ifdef XMRIG_ALGO_RANDOMX + if (v == xmrig::VARIANT_RX_WOW) { + allocateRandomX_VM(); + Workers::updateDataset(m_state.job.seedHash(), m_totalWays); + randomx_calculate_hash(m_rx_vm, m_state.blob, m_state.job.size(), m_hash); + } + else +# endif + { + m_thread->fn(v)(m_state.blob, m_state.job.size(), m_hash, m_ctx, m_state.job.height()); + } for (size_t i = 0; i < N; ++i) { if (*reinterpret_cast(m_hash + (i * 32) + 24) < m_state.job.target()) { diff --git a/src/workers/MultiWorker.h b/src/workers/MultiWorker.h index 99d37e44..bfdf97eb 100644 --- a/src/workers/MultiWorker.h +++ b/src/workers/MultiWorker.h @@ -27,6 +27,11 @@ #define XMRIG_MULTIWORKER_H +#ifdef XMRIG_ALGO_RANDOMX +# include +#endif + + #include "base/net/stratum/Job.h" #include "Mem.h" #include "net/JobResult.h" @@ -48,6 +53,10 @@ protected: void start() override; private: +# ifdef XMRIG_ALGO_RANDOMX + void allocateRandomX_VM(); +# endif + bool resume(const xmrig::Job &job); bool verify(xmrig::Variant variant, const uint8_t *referenceValue); bool verify2(xmrig::Variant variant, const uint8_t *referenceValue); @@ -70,6 +79,10 @@ private: State m_pausedState; State m_state; uint8_t m_hash[N * 32]; + +# ifdef XMRIG_ALGO_RANDOMX + randomx_vm *m_rx_vm = nullptr; +# endif }; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 53990135..458db4a5 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -32,7 +32,7 @@ #include "base/tools/Handle.h" #include "core/config/Config.h" #include "core/Controller.h" -#include "crypto/CryptoNight_constants.h" +#include "crypto/cn/CryptoNight_constants.h" #include "interfaces/IJobResultListener.h" #include "interfaces/IThread.h" #include "Mem.h" @@ -60,6 +60,14 @@ uv_rwlock_t Workers::m_rwlock; uv_timer_t *Workers::m_timer = nullptr; xmrig::Controller *Workers::m_controller = nullptr; +#ifdef XMRIG_ALGO_RANDOMX +uv_rwlock_t Workers::m_rx_dataset_lock; +randomx_cache *Workers::m_rx_cache = nullptr; +randomx_dataset *Workers::m_rx_dataset = nullptr; +uint8_t Workers::m_rx_seed_hash[32] = {}; +std::atomic Workers::m_rx_dataset_init_thread_counter = {}; +#endif + xmrig::Job Workers::job() { @@ -177,6 +185,7 @@ void Workers::start(xmrig::Controller *controller) const std::vector &threads = controller->config()->threads(); m_status.algo = controller->config()->algorithm().algo(); + m_status.variant = controller->config()->algorithm().variant(); m_status.threads = threads.size(); for (const xmrig::IThread *thread : threads) { @@ -188,6 +197,10 @@ void Workers::start(xmrig::Controller *controller) uv_mutex_init(&m_mutex); uv_rwlock_init(&m_rwlock); +# ifdef XMRIG_ALGO_RANDOMX + uv_rwlock_init(&m_rx_dataset_lock); +# endif + m_sequence = 1; m_paused = 1; @@ -240,7 +253,7 @@ void Workers::threadsSummary(rapidjson::Document &doc) { uv_mutex_lock(&m_mutex); const uint64_t pages[2] = { m_status.hugePages, m_status.pages }; - const uint64_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo); + const uint64_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo, m_status.variant); uv_mutex_unlock(&m_mutex); auto &allocator = doc.GetAllocator(); @@ -344,15 +357,92 @@ void Workers::start(IWorker *worker) if (m_status.started == m_status.threads) { const double percent = (double) m_status.hugePages / m_status.pages * 100.0; - const size_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo) / 1024; + const size_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo, m_status.variant) / 1024; - LOG_INFO(GREEN_BOLD("READY (CPU)") " threads " CYAN_BOLD("%zu(%zu)") " huge pages %s%zu/%zu %1.0f%%\x1B[0m memory " CYAN_BOLD("%zu KB") "", - m_status.threads, m_status.ways, - (m_status.hugePages == m_status.pages ? GREEN_BOLD_S : (m_status.hugePages == 0 ? RED_BOLD_S : YELLOW_BOLD_S)), - m_status.hugePages, m_status.pages, percent, memory); +# ifdef XMRIG_ALGO_RANDOMX + if (m_status.algo == xmrig::RANDOM_X) { + LOG_INFO(GREEN_BOLD("READY (CPU)") " threads " CYAN_BOLD("%zu(%zu)") " memory " CYAN_BOLD("%zu KB") "", + m_status.threads, m_status.ways, memory); + } else +# endif + { + LOG_INFO(GREEN_BOLD("READY (CPU)") " threads " CYAN_BOLD("%zu(%zu)") " huge pages %s%zu/%zu %1.0f%%\x1B[0m memory " CYAN_BOLD("%zu KB") "", + m_status.threads, m_status.ways, + (m_status.hugePages == m_status.pages ? GREEN_BOLD_S : (m_status.hugePages == 0 ? RED_BOLD_S : YELLOW_BOLD_S)), + m_status.hugePages, m_status.pages, percent, memory); + } } uv_mutex_unlock(&m_mutex); worker->start(); } + + +#ifdef XMRIG_ALGO_RANDOMX +void Workers::updateDataset(const uint8_t* seed_hash, const uint32_t num_threads) +{ + // Check if we need to update cache and dataset + if (memcmp(m_rx_seed_hash, seed_hash, sizeof(m_rx_seed_hash)) == 0) + return; + + const uint32_t thread_id = m_rx_dataset_init_thread_counter++; + LOG_DEBUG("Thread %u started updating RandomX dataset", thread_id); + + // Wait for all threads to get here + do { + if (m_sequence.load(std::memory_order_relaxed) == 0) { + // Exit immediately if workers were stopped + return; + } + std::this_thread::yield(); + } while (m_rx_dataset_init_thread_counter.load() != num_threads); + + // One of the threads updates cache + uv_rwlock_wrlock(&m_rx_dataset_lock); + if (memcmp(m_rx_seed_hash, seed_hash, sizeof(m_rx_seed_hash)) != 0) { + memcpy(m_rx_seed_hash, seed_hash, sizeof(m_rx_seed_hash)); + randomx_init_cache(m_rx_cache, m_rx_seed_hash, sizeof(m_rx_seed_hash)); + } + uv_rwlock_wrunlock(&m_rx_dataset_lock); + + // All threads update dataset + const uint32_t a = (randomx_dataset_item_count() * thread_id) / num_threads; + const uint32_t b = (randomx_dataset_item_count() * (thread_id + 1)) / num_threads; + randomx_init_dataset(m_rx_dataset, m_rx_cache, a, b - a); + + LOG_DEBUG("Thread %u finished updating RandomX dataset", thread_id); + + // Wait for all threads to complete + --m_rx_dataset_init_thread_counter; + do { + if (m_sequence.load(std::memory_order_relaxed) == 0) { + // Exit immediately if workers were stopped + return; + } + std::this_thread::yield(); + } while (m_rx_dataset_init_thread_counter.load() != 0); +} + +randomx_dataset* Workers::getDataset() +{ + if (m_rx_dataset) + return m_rx_dataset; + + uv_rwlock_wrlock(&m_rx_dataset_lock); + if (!m_rx_dataset) { + randomx_dataset* dataset = randomx_alloc_dataset(RANDOMX_FLAG_LARGE_PAGES); + if (!dataset) { + dataset = randomx_alloc_dataset(RANDOMX_FLAG_DEFAULT); + } + m_rx_cache = randomx_alloc_cache(static_cast(RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES)); + if (!m_rx_cache) { + m_rx_cache = randomx_alloc_cache(RANDOMX_FLAG_JIT); + } + m_rx_dataset = dataset; + } + uv_rwlock_wrunlock(&m_rx_dataset_lock); + + return m_rx_dataset; +} +#endif diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 5b084fc2..1ea11960 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -31,6 +31,10 @@ #include #include +#ifdef XMRIG_ALGO_RANDOMX +# include +#endif + #include "base/net/stratum/Job.h" #include "net/JobResult.h" #include "rapidjson/fwd.h" @@ -72,6 +76,11 @@ public: static void threadsSummary(rapidjson::Document &doc); # endif +# ifdef XMRIG_ALGO_RANDOMX + static void updateDataset(const uint8_t* seed_hash, uint32_t num_threads); + static randomx_dataset* getDataset(); +# endif + private: static void onReady(void *arg); static void onResult(uv_async_t *handle); @@ -87,7 +96,8 @@ private: started(0), threads(0), ways(0), - algo(xmrig::CRYPTONIGHT) + algo(xmrig::CRYPTONIGHT), + variant(xmrig::VARIANT_AUTO) {} size_t hugePages; @@ -96,6 +106,7 @@ private: size_t threads; size_t ways; xmrig::Algo algo; + xmrig::Variant variant; }; static bool m_active; @@ -114,6 +125,14 @@ private: static uv_rwlock_t m_rwlock; static uv_timer_t *m_timer; static xmrig::Controller *m_controller; + +# ifdef XMRIG_ALGO_RANDOMX + static uv_rwlock_t m_rx_dataset_lock; + static randomx_cache *m_rx_cache; + static randomx_dataset *m_rx_dataset; + static uint8_t m_rx_seed_hash[32]; + static std::atomic m_rx_dataset_init_thread_counter; +# endif };