mirror of
https://github.com/xmrig/xmrig.git
synced 2024-11-18 00:37:46 +00:00
Merge branch 'feature-custom-msr' into dev
This commit is contained in:
commit
969821296f
16 changed files with 468 additions and 111 deletions
|
@ -13,6 +13,7 @@ option(WITH_HTTP "Enable HTTP protocol support (client/server)" ON)
|
|||
option(WITH_DEBUG_LOG "Enable debug log output" OFF)
|
||||
option(WITH_TLS "Enable OpenSSL support" ON)
|
||||
option(WITH_ASM "Enable ASM PoW implementations" ON)
|
||||
option(WITH_MSR "Enable MSR support" ON)
|
||||
option(WITH_EMBEDDED_CONFIG "Enable internal embedded JSON config" OFF)
|
||||
option(WITH_OPENCL "Enable OpenCL backend" ON)
|
||||
option(WITH_CUDA "Enable CUDA backend" ON)
|
||||
|
|
|
@ -79,10 +79,21 @@ if (WITH_RANDOMX)
|
|||
)
|
||||
endif()
|
||||
|
||||
if (WIN32)
|
||||
list(APPEND SOURCES_CRYPTO src/crypto/rx/Rx_windows.cpp)
|
||||
elseif (XMRIG_OS_LINUX AND NOT XMRIG_ARM)
|
||||
list(APPEND SOURCES_CRYPTO src/crypto/rx/Rx_linux.cpp)
|
||||
if (WITH_MSR AND NOT XMRIG_ARM AND CMAKE_SIZEOF_VOID_P EQUAL 8 AND (XMRIG_OS_WIN OR XMRIG_OS_LINUX))
|
||||
add_definitions(/DXMRIG_FEATURE_MSR)
|
||||
message("-- WITH_MSR=ON")
|
||||
|
||||
if (XMRIG_OS_WIN)
|
||||
list(APPEND SOURCES_CRYPTO src/crypto/rx/Rx_win.cpp)
|
||||
elseif (XMRIG_OS_LINUX)
|
||||
list(APPEND SOURCES_CRYPTO src/crypto/rx/Rx_linux.cpp)
|
||||
endif()
|
||||
|
||||
list(APPEND HEADERS_CRYPTO src/crypto/rx/msr/MsrItem.h)
|
||||
list(APPEND SOURCES_CRYPTO src/crypto/rx/msr/MsrItem.cpp)
|
||||
else()
|
||||
remove_definitions(/DXMRIG_FEATURE_MSR)
|
||||
message("-- WITH_MSR=OFF")
|
||||
endif()
|
||||
else()
|
||||
remove_definitions(/DXMRIG_ALGO_RANDOMX)
|
||||
|
|
|
@ -37,12 +37,20 @@ namespace xmrig {
|
|||
class ICpuInfo
|
||||
{
|
||||
public:
|
||||
enum Vendor {
|
||||
enum Vendor : uint32_t {
|
||||
VENDOR_UNKNOWN,
|
||||
VENDOR_INTEL,
|
||||
VENDOR_AMD
|
||||
};
|
||||
|
||||
enum MsrMod : uint32_t {
|
||||
MSR_MOD_NONE,
|
||||
MSR_MOD_RYZEN,
|
||||
MSR_MOD_INTEL,
|
||||
MSR_MOD_CUSTOM,
|
||||
MSR_MOD_MAX
|
||||
};
|
||||
|
||||
virtual ~ICpuInfo() = default;
|
||||
|
||||
# if defined(__x86_64__) || defined(_M_AMD64) || defined (__arm64__) || defined (__aarch64__)
|
||||
|
@ -58,6 +66,7 @@ public:
|
|||
virtual const char *backend() const = 0;
|
||||
virtual const char *brand() const = 0;
|
||||
virtual CpuThreads threads(const Algorithm &algorithm, uint32_t limit) const = 0;
|
||||
virtual MsrMod msrMod() const = 0;
|
||||
virtual size_t cores() const = 0;
|
||||
virtual size_t L2() const = 0;
|
||||
virtual size_t L3() const = 0;
|
||||
|
|
|
@ -139,7 +139,13 @@ xmrig::AdvancedCpuInfo::AdvancedCpuInfo() :
|
|||
m_aes = true;
|
||||
|
||||
if (m_vendor == VENDOR_AMD) {
|
||||
m_assembly = (data.ext_family >= 23) ? Assembly::RYZEN : Assembly::BULLDOZER;
|
||||
if (data.ext_family >= 23) {
|
||||
m_assembly = Assembly::RYZEN;
|
||||
m_msrMod = MSR_MOD_RYZEN;
|
||||
}
|
||||
else {
|
||||
m_assembly = Assembly::BULLDOZER;
|
||||
}
|
||||
}
|
||||
else if (m_vendor == VENDOR_INTEL) {
|
||||
m_assembly = Assembly::INTEL;
|
||||
|
|
|
@ -46,6 +46,7 @@ protected:
|
|||
inline bool hasOneGbPages() const override { return m_pdpe1gb; }
|
||||
inline const char *backend() const override { return m_backend; }
|
||||
inline const char *brand() const override { return m_brand; }
|
||||
inline MsrMod msrMod() const override { return m_msrMod; }
|
||||
inline size_t cores() const override { return m_cores; }
|
||||
inline size_t L2() const override { return m_L2; }
|
||||
inline size_t L3() const override { return m_L3; }
|
||||
|
@ -62,6 +63,7 @@ private:
|
|||
char m_backend[32]{};
|
||||
char m_brand[64 + 5]{};
|
||||
const bool m_pdpe1gb = false;
|
||||
MsrMod m_msrMod = MSR_MOD_NONE;
|
||||
size_t m_cores = 0;
|
||||
size_t m_L2 = 0;
|
||||
size_t m_L3 = 0;
|
||||
|
|
|
@ -175,11 +175,18 @@ xmrig::BasicCpuInfo::BasicCpuInfo() :
|
|||
cpuid(PROCESSOR_INFO, data);
|
||||
const int32_t family = get_masked(data[EAX_Reg], 12, 8) + get_masked(data[EAX_Reg], 28, 20);
|
||||
|
||||
m_assembly = family >= 23 ? Assembly::RYZEN : Assembly::BULLDOZER;
|
||||
if (family >= 23) {
|
||||
m_assembly = Assembly::RYZEN;
|
||||
m_msrMod = MSR_MOD_RYZEN;
|
||||
}
|
||||
else {
|
||||
m_assembly = Assembly::BULLDOZER;
|
||||
}
|
||||
}
|
||||
else if (memcmp(vendor, "GenuineIntel", 12) == 0) {
|
||||
m_vendor = VENDOR_INTEL;
|
||||
m_assembly = Assembly::INTEL;
|
||||
m_msrMod = MSR_MOD_INTEL;
|
||||
}
|
||||
}
|
||||
# endif
|
||||
|
|
|
@ -46,6 +46,7 @@ protected:
|
|||
inline bool hasAVX2() const override { return m_avx2; }
|
||||
inline bool hasOneGbPages() const override { return m_pdpe1gb; }
|
||||
inline const char *brand() const override { return m_brand; }
|
||||
inline MsrMod msrMod() const override { return m_msrMod; }
|
||||
inline size_t cores() const override { return 0; }
|
||||
inline size_t L2() const override { return 0; }
|
||||
inline size_t L3() const override { return 0; }
|
||||
|
@ -63,6 +64,7 @@ private:
|
|||
bool m_aes = false;
|
||||
const bool m_avx2 = false;
|
||||
const bool m_pdpe1gb = false;
|
||||
MsrMod m_msrMod = MSR_MOD_NONE;
|
||||
Vendor m_vendor = VENDOR_UNKNOWN;
|
||||
};
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
"init": -1,
|
||||
"mode": "auto",
|
||||
"1gb-pages": false,
|
||||
"rdmsr": true,
|
||||
"wrmsr": true,
|
||||
"numa": true
|
||||
},
|
||||
|
|
|
@ -73,7 +73,7 @@ bool xmrig::Rx::init(const Job &job, const RxConfig &config, const CpuConfig &cp
|
|||
}
|
||||
|
||||
if (!osInitialized) {
|
||||
osInit(config);
|
||||
msrInit(config);
|
||||
osInitialized = true;
|
||||
}
|
||||
|
||||
|
@ -103,6 +103,10 @@ xmrig::RxDataset *xmrig::Rx::dataset(const Job &job, uint32_t nodeId)
|
|||
|
||||
void xmrig::Rx::destroy()
|
||||
{
|
||||
if (osInitialized) {
|
||||
msrDestroy();
|
||||
}
|
||||
|
||||
delete d_ptr;
|
||||
|
||||
d_ptr = nullptr;
|
||||
|
@ -115,8 +119,16 @@ void xmrig::Rx::init(IRxListener *listener)
|
|||
}
|
||||
|
||||
|
||||
#if (!defined(XMRIG_OS_LINUX) && !defined(_WIN32)) || defined(XMRIG_ARM)
|
||||
void xmrig::Rx::osInit(const RxConfig &)
|
||||
#ifndef XMRIG_FEATURE_MSR
|
||||
void xmrig::Rx::msrInit(const RxConfig &)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Rx::msrDestroy()
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -58,7 +58,8 @@ public:
|
|||
static void init(IRxListener *listener);
|
||||
|
||||
private:
|
||||
static void osInit(const RxConfig &config);
|
||||
static void msrInit(const RxConfig &config);
|
||||
static void msrDestroy();
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -49,6 +49,7 @@ namespace xmrig {
|
|||
static const char *kInit = "init";
|
||||
static const char *kMode = "mode";
|
||||
static const char *kOneGbPages = "1gb-pages";
|
||||
static const char *kRdmsr = "rdmsr";
|
||||
static const char *kWrmsr = "wrmsr";
|
||||
|
||||
#ifdef XMRIG_FEATURE_HWLOC
|
||||
|
@ -57,6 +58,23 @@ static const char *kNUMA = "numa";
|
|||
|
||||
static const std::array<const char *, RxConfig::ModeMax> modeNames = { "auto", "fast", "light" };
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_MSR
|
||||
constexpr size_t kMsrArraySize = 4;
|
||||
|
||||
static const std::array<MsrItems, kMsrArraySize> msrPresets = {
|
||||
MsrItems(),
|
||||
MsrItems{{ 0xC0011020, 0x0 }, { 0xC0011021, 0x40 }, { 0xC0011022, 0x510000 }, { 0xC001102b, 0x1808cc16 }},
|
||||
MsrItems{{ 0x1a4, 0x6 }},
|
||||
MsrItems()
|
||||
};
|
||||
|
||||
static const std::array<const char *, kMsrArraySize> modNames = { "none", "ryzen", "intel", "custom" };
|
||||
|
||||
static_assert (kMsrArraySize == ICpuInfo::MSR_MOD_MAX, "kMsrArraySize and MSR_MOD_MAX mismatch");
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -65,7 +83,11 @@ bool xmrig::RxConfig::read(const rapidjson::Value &value)
|
|||
if (value.IsObject()) {
|
||||
m_threads = Json::getInt(value, kInit, m_threads);
|
||||
m_mode = readMode(Json::getValue(value, kMode));
|
||||
m_wrmsr = readMSR(Json::getValue(value, kWrmsr));
|
||||
m_rdmsr = Json::getBool(value, kRdmsr, m_rdmsr);
|
||||
|
||||
# ifdef XMRIG_FEATURE_MSR
|
||||
readMSR(Json::getValue(value, kWrmsr));
|
||||
# endif
|
||||
|
||||
# ifdef XMRIG_OS_LINUX
|
||||
m_oneGbPages = Json::getBool(value, kOneGbPages, m_oneGbPages);
|
||||
|
@ -109,13 +131,25 @@ rapidjson::Value xmrig::RxConfig::toJSON(rapidjson::Document &doc) const
|
|||
obj.AddMember(StringRef(kInit), m_threads, allocator);
|
||||
obj.AddMember(StringRef(kMode), StringRef(modeName()), allocator);
|
||||
obj.AddMember(StringRef(kOneGbPages), m_oneGbPages, allocator);
|
||||
obj.AddMember(StringRef(kRdmsr), m_rdmsr, allocator);
|
||||
|
||||
if (m_wrmsr < 0 || m_wrmsr == 6) {
|
||||
obj.AddMember(StringRef(kWrmsr), m_wrmsr == 6, allocator);
|
||||
# ifdef XMRIG_FEATURE_MSR
|
||||
if (!m_msrPreset.empty()) {
|
||||
Value wrmsr(kArrayType);
|
||||
wrmsr.Reserve(m_msrPreset.size(), allocator);
|
||||
|
||||
for (const auto &i : m_msrPreset) {
|
||||
wrmsr.PushBack(i.toJSON(doc), allocator);
|
||||
}
|
||||
|
||||
obj.AddMember(StringRef(kWrmsr), wrmsr, allocator);
|
||||
}
|
||||
else {
|
||||
obj.AddMember(StringRef(kWrmsr), m_wrmsr, allocator);
|
||||
}
|
||||
# else
|
||||
obj.AddMember(StringRef(kWrmsr), false, allocator);
|
||||
# endif
|
||||
|
||||
# ifdef XMRIG_FEATURE_HWLOC
|
||||
if (!m_nodeset.empty()) {
|
||||
|
@ -168,20 +202,71 @@ uint32_t xmrig::RxConfig::threads(uint32_t limit) const
|
|||
}
|
||||
|
||||
|
||||
int xmrig::RxConfig::readMSR(const rapidjson::Value &value) const
|
||||
#ifdef XMRIG_FEATURE_MSR
|
||||
const char *xmrig::RxConfig::msrPresetName() const
|
||||
{
|
||||
if (value.IsInt()) {
|
||||
return std::min(value.GetInt(), 15);
|
||||
}
|
||||
|
||||
if (value.IsBool() && !value.GetBool()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return m_wrmsr;
|
||||
return modNames[msrMod()];
|
||||
}
|
||||
|
||||
|
||||
const xmrig::MsrItems &xmrig::RxConfig::msrPreset() const
|
||||
{
|
||||
const auto mod = msrMod();
|
||||
|
||||
if (mod == ICpuInfo::MSR_MOD_CUSTOM) {
|
||||
return m_msrPreset;
|
||||
}
|
||||
|
||||
return msrPresets[mod];
|
||||
}
|
||||
|
||||
|
||||
uint32_t xmrig::RxConfig::msrMod() const
|
||||
{
|
||||
if (!wrmsr()) {
|
||||
return ICpuInfo::MSR_MOD_NONE;
|
||||
}
|
||||
|
||||
if (!m_msrPreset.empty()) {
|
||||
return ICpuInfo::MSR_MOD_CUSTOM;
|
||||
}
|
||||
|
||||
return Cpu::info()->msrMod();
|
||||
}
|
||||
|
||||
|
||||
void xmrig::RxConfig::readMSR(const rapidjson::Value &value)
|
||||
{
|
||||
if (value.IsBool()) {
|
||||
m_wrmsr = value.GetBool();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (value.IsInt()) {
|
||||
const int i = std::min(value.GetInt(), 15);
|
||||
if (i >= 0) {
|
||||
m_msrPreset.emplace_back(0x1a4, i);
|
||||
}
|
||||
else {
|
||||
m_wrmsr = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (value.IsArray()) {
|
||||
for (const auto &i : value.GetArray()) {
|
||||
MsrItem item(i);
|
||||
if (item.isValid()) {
|
||||
m_msrPreset.emplace_back(item);
|
||||
}
|
||||
}
|
||||
|
||||
m_wrmsr = !m_msrPreset.empty();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
xmrig::RxConfig::Mode xmrig::RxConfig::readMode(const rapidjson::Value &value) const
|
||||
{
|
||||
if (value.IsUint()) {
|
||||
|
|
|
@ -29,6 +29,11 @@
|
|||
#include "rapidjson/fwd.h"
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_MSR
|
||||
# include "crypto/rx/msr/MsrItem.h"
|
||||
#endif
|
||||
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
|
@ -58,17 +63,32 @@ public:
|
|||
uint32_t threads(uint32_t limit = 100) const;
|
||||
|
||||
inline bool isOneGbPages() const { return m_oneGbPages; }
|
||||
inline int wrmsr() const { return m_wrmsr; }
|
||||
inline bool rdmsr() const { return m_rdmsr; }
|
||||
inline bool wrmsr() const { return m_wrmsr; }
|
||||
inline Mode mode() const { return m_mode; }
|
||||
|
||||
# ifdef XMRIG_FEATURE_MSR
|
||||
const char *msrPresetName() const;
|
||||
const MsrItems &msrPreset() const;
|
||||
# endif
|
||||
|
||||
private:
|
||||
int readMSR(const rapidjson::Value &value) const;
|
||||
# ifdef XMRIG_FEATURE_MSR
|
||||
uint32_t msrMod() const;
|
||||
void readMSR(const rapidjson::Value &value);
|
||||
|
||||
bool m_wrmsr = true;
|
||||
MsrItems m_msrPreset;
|
||||
# else
|
||||
bool m_wrmsr = false;
|
||||
# endif
|
||||
|
||||
Mode readMode(const rapidjson::Value &value) const;
|
||||
|
||||
bool m_numa = true;
|
||||
bool m_oneGbPages = false;
|
||||
bool m_rdmsr = true;
|
||||
int m_threads = -1;
|
||||
int m_wrmsr = 6;
|
||||
Mode m_mode = AutoMode;
|
||||
|
||||
# ifdef XMRIG_FEATURE_HWLOC
|
||||
|
|
|
@ -48,16 +48,8 @@
|
|||
namespace xmrig {
|
||||
|
||||
|
||||
enum MsrMod : uint32_t {
|
||||
MSR_MOD_NONE,
|
||||
MSR_MOD_RYZEN,
|
||||
MSR_MOD_INTEL,
|
||||
MSR_MOD_MAX
|
||||
};
|
||||
|
||||
|
||||
static const char *tag = YELLOW_BG_BOLD(WHITE_BOLD_S " msr ") " ";
|
||||
static const std::array<const char *, MSR_MOD_MAX> modNames = { nullptr, "Ryzen", "Intel" };
|
||||
static const char *tag = YELLOW_BG_BOLD(WHITE_BOLD_S " msr ") " ";
|
||||
static MsrItems savedState;
|
||||
|
||||
|
||||
static inline int dir_filter(const struct dirent *dirp)
|
||||
|
@ -108,6 +100,37 @@ static bool wrmsr_on_all_cpus(uint32_t reg, uint64_t value)
|
|||
}
|
||||
|
||||
|
||||
bool rdmsr_on_cpu(uint32_t reg, uint32_t cpu, uint64_t &value)
|
||||
{
|
||||
char msr_file_name[64]{};
|
||||
|
||||
sprintf(msr_file_name, "/dev/cpu/%u/msr", cpu);
|
||||
int fd = open(msr_file_name, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool success = pread(fd, &value, sizeof value, reg) == sizeof value;
|
||||
|
||||
close(fd);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
static MsrItem rdmsr(uint32_t reg)
|
||||
{
|
||||
uint64_t value = 0;
|
||||
if (!rdmsr_on_cpu(reg, 0, value)) {
|
||||
LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "cannot read MSR 0x%08" PRIx32, tag, reg);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
return { reg, value };
|
||||
}
|
||||
|
||||
|
||||
static bool wrmsr_modprobe()
|
||||
{
|
||||
if (system("/sbin/modprobe msr > /dev/null 2>&1") != 0) {
|
||||
|
@ -120,42 +143,58 @@ static bool wrmsr_modprobe()
|
|||
}
|
||||
|
||||
|
||||
static bool wrmsr(const MsrItems &preset, bool save)
|
||||
{
|
||||
if (!wrmsr_modprobe()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (save) {
|
||||
for (const auto &i : preset) {
|
||||
auto item = rdmsr(i.reg());
|
||||
if (item.isValid()) {
|
||||
savedState.emplace_back(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &i : preset) {
|
||||
if (!wrmsr_on_all_cpus(i.reg(), i.value())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
void xmrig::Rx::osInit(const RxConfig &config)
|
||||
void xmrig::Rx::msrInit(const RxConfig &config)
|
||||
{
|
||||
if (config.wrmsr() < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
MsrMod mod = MSR_MOD_NONE;
|
||||
if (Cpu::info()->assembly() == Assembly::RYZEN) {
|
||||
mod = MSR_MOD_RYZEN;
|
||||
}
|
||||
else if (Cpu::info()->vendor() == ICpuInfo::VENDOR_INTEL) {
|
||||
mod = MSR_MOD_INTEL;
|
||||
}
|
||||
|
||||
if (mod == MSR_MOD_NONE) {
|
||||
const auto &preset = config.msrPreset();
|
||||
if (preset.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uint64_t ts = Chrono::steadyMSecs();
|
||||
|
||||
if (!wrmsr_modprobe()) {
|
||||
if (wrmsr(preset, config.rdmsr())) {
|
||||
LOG_NOTICE(CLEAR "%s" GREEN_BOLD_S "register values for \"%s\" preset has been set successfully" BLACK_BOLD(" (%" PRIu64 " ms)"), tag, config.msrPresetName(), Chrono::steadyMSecs() - ts);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Rx::msrDestroy()
|
||||
{
|
||||
if (savedState.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mod == MSR_MOD_RYZEN) {
|
||||
wrmsr_on_all_cpus(0xC0011020, 0);
|
||||
wrmsr_on_all_cpus(0xC0011021, 0x40);
|
||||
wrmsr_on_all_cpus(0xC0011022, 0x510000);
|
||||
wrmsr_on_all_cpus(0xC001102b, 0x1808cc16);
|
||||
}
|
||||
else if (mod == MSR_MOD_INTEL) {
|
||||
wrmsr_on_all_cpus(0x1a4, config.wrmsr());
|
||||
}
|
||||
const uint64_t ts = Chrono::steadyMSecs();
|
||||
|
||||
LOG_NOTICE(CLEAR "%s" GREEN_BOLD_S "register values for %s has been set successfully" BLACK_BOLD(" (%" PRIu64 " ms)"), tag, modNames[mod], Chrono::steadyMSecs() - ts);
|
||||
if (!wrmsr(savedState, false)) {
|
||||
LOG_ERR(CLEAR "%s" RED_BOLD_S "failed to restore initial state" BLACK_BOLD(" (%" PRIu64 " ms)"), tag, Chrono::steadyMSecs() - ts);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,17 +48,9 @@
|
|||
namespace xmrig {
|
||||
|
||||
|
||||
enum MsrMod : uint32_t {
|
||||
MSR_MOD_NONE,
|
||||
MSR_MOD_RYZEN,
|
||||
MSR_MOD_INTEL,
|
||||
MSR_MOD_MAX
|
||||
};
|
||||
|
||||
|
||||
static const char *tag = YELLOW_BG_BOLD(WHITE_BOLD_S " msr ") " ";
|
||||
static const std::array<const char *, MSR_MOD_MAX> modNames = { nullptr, "Ryzen", "Intel" };
|
||||
static bool reuseDriver = false;
|
||||
static bool reuseDriver = false;
|
||||
static const char *tag = YELLOW_BG_BOLD(WHITE_BOLD_S " msr ") " ";
|
||||
static MsrItems savedState;
|
||||
|
||||
|
||||
static SC_HANDLE hManager;
|
||||
|
@ -186,10 +178,12 @@ static HANDLE wrmsr_install_driver()
|
|||
}
|
||||
|
||||
|
||||
#define IOCTL_READ_MSR CTL_CODE(40000, 0x821, METHOD_BUFFERED, FILE_ANY_ACCESS)
|
||||
#define IOCTL_WRITE_MSR CTL_CODE(40000, 0x822, METHOD_BUFFERED, FILE_ANY_ACCESS)
|
||||
|
||||
|
||||
static bool wrmsr(HANDLE hDriver, uint32_t reg, uint64_t value) {
|
||||
static bool wrmsr(HANDLE driver, uint32_t reg, uint64_t value)
|
||||
{
|
||||
struct {
|
||||
uint32_t reg = 0;
|
||||
uint32_t value[2]{};
|
||||
|
@ -198,12 +192,12 @@ static bool wrmsr(HANDLE hDriver, uint32_t reg, uint64_t value) {
|
|||
static_assert(sizeof(input) == 12, "Invalid struct size for WinRing0 driver");
|
||||
|
||||
input.reg = reg;
|
||||
*((uint64_t*)input.value) = value;
|
||||
*(reinterpret_cast<uint64_t*>(input.value)) = value;
|
||||
|
||||
DWORD output;
|
||||
DWORD k;
|
||||
|
||||
if (!DeviceIoControl(hDriver, IOCTL_WRITE_MSR, &input, sizeof(input), &output, sizeof(output), &k, nullptr)) {
|
||||
if (!DeviceIoControl(driver, IOCTL_WRITE_MSR, &input, sizeof(input), &output, sizeof(output), &k, nullptr)) {
|
||||
LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "cannot set MSR 0x%08" PRIx32 " to 0x%08" PRIx64, tag, reg, value);
|
||||
|
||||
return false;
|
||||
|
@ -213,55 +207,59 @@ static bool wrmsr(HANDLE hDriver, uint32_t reg, uint64_t value) {
|
|||
}
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
void xmrig::Rx::osInit(const RxConfig &config)
|
||||
static bool rdmsr(HANDLE driver, uint32_t reg, uint64_t &value)
|
||||
{
|
||||
if ((config.wrmsr() < 0) || !ICpuInfo::isX64()) {
|
||||
return;
|
||||
DWORD size = 0;
|
||||
|
||||
return DeviceIoControl(driver, IOCTL_READ_MSR, ®, sizeof(reg), &value, sizeof(value), &size, nullptr) != 0;
|
||||
}
|
||||
|
||||
|
||||
static MsrItem rdmsr(HANDLE driver, uint32_t reg)
|
||||
{
|
||||
uint64_t value = 0;
|
||||
if (!rdmsr(driver, reg, value)) {
|
||||
LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "cannot read MSR 0x%08" PRIx32, tag, reg);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
MsrMod mod = MSR_MOD_NONE;
|
||||
if (Cpu::info()->assembly() == Assembly::RYZEN) {
|
||||
mod = MSR_MOD_RYZEN;
|
||||
}
|
||||
else if (Cpu::info()->vendor() == ICpuInfo::VENDOR_INTEL) {
|
||||
mod = MSR_MOD_INTEL;
|
||||
}
|
||||
return { reg, value };
|
||||
}
|
||||
|
||||
if (mod == MSR_MOD_NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uint64_t ts = Chrono::steadyMSecs();
|
||||
bool success = true;
|
||||
static bool wrmsr(const MsrItems &preset, bool save)
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
HANDLE hDriver = wrmsr_install_driver();
|
||||
if (!hDriver) {
|
||||
HANDLE driver = wrmsr_install_driver();
|
||||
if (!driver) {
|
||||
wrmsr_uninstall_driver();
|
||||
|
||||
if (hManager) {
|
||||
CloseServiceHandle(hManager);
|
||||
}
|
||||
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::thread wrmsr_thread([hDriver, mod, &config, &success]() {
|
||||
if (save) {
|
||||
for (const auto &i : preset) {
|
||||
auto item = rdmsr(driver, i.reg());
|
||||
if (item.isValid()) {
|
||||
savedState.emplace_back(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::thread wrmsr_thread([driver, &preset, &success]() {
|
||||
for (uint32_t i = 0, n = Cpu::info()->threads(); i < n; ++i) {
|
||||
if (!Platform::setThreadAffinity(i)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mod == MSR_MOD_RYZEN) {
|
||||
success = wrmsr(hDriver, 0xC0011020, 0) &&
|
||||
wrmsr(hDriver, 0xC0011021, 0x40) &&
|
||||
wrmsr(hDriver, 0xC0011022, 0x510000) &&
|
||||
wrmsr(hDriver, 0xC001102b, 0x1808cc16);
|
||||
}
|
||||
else if (mod == MSR_MOD_INTEL) {
|
||||
success = wrmsr(hDriver, 0x1a4, config.wrmsr());
|
||||
for (const auto &i : preset) {
|
||||
success = wrmsr(driver, i.reg(), i.value());
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
|
@ -272,15 +270,42 @@ void xmrig::Rx::osInit(const RxConfig &config)
|
|||
|
||||
wrmsr_thread.join();
|
||||
|
||||
CloseHandle(hDriver);
|
||||
CloseHandle(driver);
|
||||
|
||||
wrmsr_uninstall_driver();
|
||||
CloseServiceHandle(hManager);
|
||||
|
||||
if (success) {
|
||||
LOG_NOTICE(CLEAR "%s" GREEN_BOLD_S "register values for %s has been set successfully" BLACK_BOLD(" (%" PRIu64 " ms)"), tag, modNames[mod], Chrono::steadyMSecs() - ts);
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
void xmrig::Rx::msrInit(const RxConfig &config)
|
||||
{
|
||||
const auto &preset = config.msrPreset();
|
||||
if (preset.empty()) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
LOG_ERR(CLEAR "%s" RED_BOLD_S "failed to write MSR registers" BLACK_BOLD(" (%" PRIu64 " ms)"), tag, Chrono::steadyMSecs() - ts);
|
||||
|
||||
const uint64_t ts = Chrono::steadyMSecs();
|
||||
|
||||
if (wrmsr(preset, config.rdmsr())) {
|
||||
LOG_NOTICE(CLEAR "%s" GREEN_BOLD_S "register values for \"%s\" preset has been set successfully" BLACK_BOLD(" (%" PRIu64 " ms)"), tag, config.msrPresetName(), Chrono::steadyMSecs() - ts);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Rx::msrDestroy()
|
||||
{
|
||||
if (savedState.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uint64_t ts = Chrono::steadyMSecs();
|
||||
|
||||
if (!wrmsr(savedState, false)) {
|
||||
LOG_ERR(CLEAR "%s" RED_BOLD_S "failed to restore initial state" BLACK_BOLD(" (%" PRIu64 " ms)"), tag, Chrono::steadyMSecs() - ts);
|
||||
}
|
||||
}
|
65
src/crypto/rx/msr/MsrItem.cpp
Normal file
65
src/crypto/rx/msr/MsrItem.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
|
||||
* Copyright 2018-2019 tevador <tevador@gmail.com>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "crypto/rx/msr/MsrItem.h"
|
||||
#include "rapidjson/document.h"
|
||||
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
xmrig::MsrItem::MsrItem(const rapidjson::Value &value)
|
||||
{
|
||||
if (!value.IsString()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto kv = String(value.GetString()).split(':');
|
||||
if (kv.size() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_reg = strtoul(kv[0], nullptr, 0);
|
||||
m_value = strtoul(kv[1], nullptr, 0);
|
||||
}
|
||||
|
||||
|
||||
rapidjson::Value xmrig::MsrItem::toJSON(rapidjson::Document &doc) const
|
||||
{
|
||||
return toString().toJSON(doc);
|
||||
}
|
||||
|
||||
|
||||
xmrig::String xmrig::MsrItem::toString() const
|
||||
{
|
||||
constexpr size_t size = 32;
|
||||
|
||||
auto buf = new char[size]();
|
||||
snprintf(buf, size, "0x%" PRIx32 ":0x%" PRIx64, m_reg, m_value);
|
||||
|
||||
return buf;
|
||||
}
|
71
src/crypto/rx/msr/MsrItem.h
Normal file
71
src/crypto/rx/msr/MsrItem.h
Normal file
|
@ -0,0 +1,71 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
|
||||
* Copyright 2018-2019 tevador <tevador@gmail.com>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_MSRITEM_H
|
||||
#define XMRIG_MSRITEM_H
|
||||
|
||||
|
||||
#include "base/tools/String.h"
|
||||
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace xmrig
|
||||
{
|
||||
|
||||
|
||||
class RxDataset;
|
||||
|
||||
|
||||
class MsrItem
|
||||
{
|
||||
public:
|
||||
inline MsrItem() = default;
|
||||
inline MsrItem(uint32_t reg, uint64_t value) : m_reg(reg), m_value(value) {}
|
||||
|
||||
MsrItem(const rapidjson::Value &value);
|
||||
|
||||
inline bool isValid() const { return m_reg > 0; }
|
||||
inline uint32_t reg() const { return m_reg; }
|
||||
inline uint64_t value() const { return m_value; }
|
||||
|
||||
rapidjson::Value toJSON(rapidjson::Document &doc) const;
|
||||
String toString() const;
|
||||
|
||||
private:
|
||||
uint32_t m_reg = 0;
|
||||
uint64_t m_value = 0;
|
||||
};
|
||||
|
||||
|
||||
using MsrItems = std::vector<MsrItem>;
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_MSRITEM_H */
|
Loading…
Reference in a new issue