Unified Linux/Windows MSR log messages.

This commit is contained in:
XMRig 2019-12-15 01:32:41 +07:00
parent 1ad6b5504c
commit 5d0fd2dc8e
No known key found for this signature in database
GPG Key ID: 446A53638BE94409
3 changed files with 141 additions and 63 deletions

View File

@ -83,6 +83,7 @@ private:
#define WHITE_BOLD_S CSI "1;37m" // actually white #define WHITE_BOLD_S CSI "1;37m" // actually white
#define GREEN_BG_BOLD_S CSI "42;1m" #define GREEN_BG_BOLD_S CSI "42;1m"
#define YELLOW_BG_BOLD_S CSI "43;1m"
#define BLUE_BG_S CSI "44m" #define BLUE_BG_S CSI "44m"
#define BLUE_BG_BOLD_S CSI "44;1m" #define BLUE_BG_BOLD_S CSI "44;1m"
#define MAGENTA_BG_S CSI "45m" #define MAGENTA_BG_S CSI "45m"
@ -109,6 +110,7 @@ private:
#define WHITE_BOLD(x) WHITE_BOLD_S x CLEAR #define WHITE_BOLD(x) WHITE_BOLD_S x CLEAR
#define GREEN_BG_BOLD(x) GREEN_BG_BOLD_S x CLEAR #define GREEN_BG_BOLD(x) GREEN_BG_BOLD_S x CLEAR
#define YELLOW_BG_BOLD(x) YELLOW_BG_BOLD_S x CLEAR
#define BLUE_BG(x) BLUE_BG_S x CLEAR #define BLUE_BG(x) BLUE_BG_S x CLEAR
#define BLUE_BG_BOLD(x) BLUE_BG_BOLD_S x CLEAR #define BLUE_BG_BOLD(x) BLUE_BG_BOLD_S x CLEAR
#define MAGENTA_BG(x) MAGENTA_BG_S x CLEAR #define MAGENTA_BG(x) MAGENTA_BG_S x CLEAR

View File

@ -28,12 +28,13 @@
#include "crypto/rx/Rx.h" #include "crypto/rx/Rx.h"
#include "backend/common/Tags.h"
#include "backend/cpu/Cpu.h" #include "backend/cpu/Cpu.h"
#include "base/io/log/Log.h" #include "base/io/log/Log.h"
#include "base/tools/Chrono.h"
#include "crypto/rx/RxConfig.h" #include "crypto/rx/RxConfig.h"
#include <array>
#include <cctype> #include <cctype>
#include <cinttypes> #include <cinttypes>
#include <cstdio> #include <cstdio>
@ -47,6 +48,18 @@
namespace xmrig { 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 inline int dir_filter(const struct dirent *dirp) static inline int dir_filter(const struct dirent *dirp)
{ {
return isdigit(dirp->d_name[0]) ? 1 : 0; return isdigit(dirp->d_name[0]) ? 1 : 0;
@ -88,7 +101,7 @@ static bool wrmsr_on_all_cpus(uint32_t reg, uint64_t value)
free(namelist); free(namelist);
if (errors) { if (errors) {
LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "cannot set MSR 0x%08" PRIx32 " to 0x%08" PRIx64, rx_tag(), reg, value); LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "cannot set MSR 0x%08" PRIx32 " to 0x%08" PRIx64, tag, reg, value);
} }
return errors == 0; return errors == 0;
@ -98,7 +111,7 @@ static bool wrmsr_on_all_cpus(uint32_t reg, uint64_t value)
static bool wrmsr_modprobe() static bool wrmsr_modprobe()
{ {
if (system("/sbin/modprobe msr > /dev/null 2>&1") != 0) { if (system("/sbin/modprobe msr > /dev/null 2>&1") != 0) {
LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "msr kernel module is not available", rx_tag()); LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "msr kernel module is not available", tag);
return false; return false;
} }
@ -116,16 +129,33 @@ void xmrig::Rx::osInit(const RxConfig &config)
return; return;
} }
if (Cpu::info()->assembly() == Assembly::RYZEN && wrmsr_modprobe()) { MsrMod mod = MSR_MOD_NONE;
wrmsr_on_all_cpus(0xC0011022, 0x510000); if (Cpu::info()->assembly() == Assembly::RYZEN) {
wrmsr_on_all_cpus(0xC001102b, 0x1808cc16); mod = MSR_MOD_RYZEN;
wrmsr_on_all_cpus(0xC0011020, 0); }
wrmsr_on_all_cpus(0xC0011021, 0x40); else if (Cpu::info()->vendor() == ICpuInfo::VENDOR_INTEL) {
mod = MSR_MOD_INTEL;
}
if (mod == MSR_MOD_NONE) {
return; return;
} }
if (Cpu::info()->vendor() == ICpuInfo::VENDOR_INTEL && wrmsr_modprobe()) { const uint64_t ts = Chrono::steadyMSecs();
if (!wrmsr_modprobe()) {
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()); wrmsr_on_all_cpus(0x1a4, config.wrmsr());
} }
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);
} }

View File

@ -7,9 +7,10 @@
* Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt> * Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd> * Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 tevador <tevador@gmail.com> * Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2000 Transmeta Corporation <https://github.com/intel/msr-tools> * Copyright 2000 Transmeta Corporation <https://github.com/intel/msr-tools>
* Copyright 2004-2008 H. Peter Anvin <https://github.com/intel/msr-tools> * Copyright 2004-2008 H. Peter Anvin <https://github.com/intel/msr-tools>
* Copyright 2007-2009 hiyohiyo <https://openlibsys.org>, <hiyohiyo@crystalmark.info>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -28,61 +29,96 @@
#include "crypto/rx/Rx.h" #include "crypto/rx/Rx.h"
#include "backend/common/Tags.h"
#include "backend/cpu/Cpu.h" #include "backend/cpu/Cpu.h"
#include "base/io/log/Log.h" #include "base/io/log/Log.h"
#include "base/kernel/Platform.h" #include "base/kernel/Platform.h"
#include "base/tools/Chrono.h"
#include "crypto/rx/RxConfig.h" #include "crypto/rx/RxConfig.h"
#include <Windows.h> #include <Windows.h>
#include <array>
#include <string> #include <string>
#include <thread> #include <thread>
#define SERVICE_NAME L"WinRing0_1_2_0" #define SERVICE_NAME L"WinRing0_1_2_0"
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 SC_HANDLE hManager; static SC_HANDLE hManager;
static SC_HANDLE hService; static SC_HANDLE hService;
static bool uninstall_driver()
static bool wrmsr_uninstall_driver()
{ {
if (!hService) {
return true;
}
bool result = true; bool result = true;
DWORD err;
SERVICE_STATUS serviceStatus; SERVICE_STATUS serviceStatus;
if (!ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus)) { if (!ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus)) {
err = GetLastError(); LOG_ERR(CLEAR "%s" RED_S "failed to stop WinRing0 driver, error %u", tag, GetLastError());
LOG_ERR("Failed to stop WinRing0 driver, error %u", err);
result = false; result = false;
} }
if (!DeleteService(hService)) { if (!DeleteService(hService)) {
err = GetLastError(); LOG_ERR(CLEAR "%s" RED_S "failed to remove WinRing0 driver, error %u", tag, GetLastError());
LOG_ERR("Failed to remove WinRing0 driver, error %u", err);
result = false; result = false;
} }
CloseServiceHandle(hService);
hService = nullptr;
return result; return result;
} }
static HANDLE install_driver()
static HANDLE wrmsr_install_driver()
{ {
DWORD err = 0; DWORD err = 0;
hManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_ALL_ACCESS); hManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_ALL_ACCESS);
if (!hManager) { if (!hManager) {
err = GetLastError(); err = GetLastError();
LOG_ERR("Failed to open service control manager, error %u", err);
return 0; if (err == ERROR_ACCESS_DENIED) {
LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "to write MSR registers Administrator privileges required.", tag);
}
else {
LOG_ERR(CLEAR "%s" RED_S "failed to open service control manager, error %u", tag, err);
}
return nullptr;
} }
std::vector<wchar_t> dir; std::vector<wchar_t> dir;
dir.resize(MAX_PATH); dir.resize(MAX_PATH);
do { do {
dir.resize(dir.size() * 2); dir.resize(dir.size() * 2);
DWORD len = GetModuleFileNameW(NULL, dir.data(), dir.size()); GetModuleFileNameW(nullptr, dir.data(), dir.size());
err = GetLastError(); err = GetLastError();
} while (err == ERROR_INSUFFICIENT_BUFFER); } while (err == ERROR_INSUFFICIENT_BUFFER);
if (err != ERROR_SUCCESS) { if (err != ERROR_SUCCESS) {
LOG_ERR("Failed to get path to driver, error %u", err); LOG_ERR(CLEAR "%s" RED_S "failed to get path to driver, error %u", err);
return 0; return nullptr;
} }
for (auto it = dir.end(); it != dir.begin(); --it) { for (auto it = dir.end(); it != dir.begin(); --it) {
@ -97,39 +133,41 @@ static HANDLE install_driver()
driverPath += L"WinRing0x64.sys"; driverPath += L"WinRing0x64.sys";
hService = OpenServiceW(hManager, SERVICE_NAME, SERVICE_ALL_ACCESS); hService = OpenServiceW(hManager, SERVICE_NAME, SERVICE_ALL_ACCESS);
if (hService) { if (hService && !wrmsr_uninstall_driver()) {
if (!uninstall_driver()) { return nullptr;
return 0;
}
CloseServiceHandle(hService);
hService = 0;
} }
else {
err = GetLastError(); err = GetLastError();
if (err != ERROR_SERVICE_DOES_NOT_EXIST) { if (err != ERROR_SERVICE_DOES_NOT_EXIST) {
LOG_ERR("Failed to open WinRing0 driver, error %u", err); LOG_ERR(CLEAR "%s" RED_S "failed to open WinRing0 driver, error %u", tag, err);
return 0;
} return nullptr;
} }
hService = CreateServiceW(hManager, SERVICE_NAME, SERVICE_NAME, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, driverPath.c_str(), nullptr, nullptr, nullptr, nullptr, nullptr); hService = CreateServiceW(hManager, SERVICE_NAME, SERVICE_NAME, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, driverPath.c_str(), nullptr, nullptr, nullptr, nullptr, nullptr);
if (!hService) { if (!hService) {
LOG_ERR("Failed to install WinRing0 driver, error %u", err); LOG_ERR(CLEAR "%s" RED_S "failed to install WinRing0 driver, error %u", tag, GetLastError());
return nullptr;
} }
if (!StartService(hService, 0, nullptr)) { if (!StartService(hService, 0, nullptr)) {
err = GetLastError(); err = GetLastError();
if (err != ERROR_SERVICE_ALREADY_RUNNING) { if (err != ERROR_SERVICE_ALREADY_RUNNING) {
LOG_ERR("Failed to start WinRing0 driver, error %u", err); LOG_ERR(CLEAR "%s" RED_S "failed to start WinRing0 driver, error %u", tag, err);
return 0;
CloseServiceHandle(hService);
hService = nullptr;
return nullptr;
} }
} }
HANDLE hDriver = CreateFileW(L"\\\\.\\" SERVICE_NAME, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); HANDLE hDriver = CreateFileW(L"\\\\.\\" SERVICE_NAME, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (!hDriver) { if (!hDriver) {
err = GetLastError(); LOG_ERR(CLEAR "%s" RED_S "failed to connect to WinRing0 driver, error %u", tag, GetLastError());
LOG_ERR("Failed to connect to WinRing0 driver, error %u", err);
return 0; return nullptr;
} }
return hDriver; return hDriver;
@ -138,11 +176,13 @@ static HANDLE install_driver()
#define IOCTL_WRITE_MSR CTL_CODE(40000, 0x822, 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 hDriver, uint32_t reg, uint64_t value) {
struct { struct {
uint32_t reg; uint32_t reg = 0;
uint32_t value[2]; uint32_t value[2]{};
} input; } input;
static_assert(sizeof(input) == 12, "Invalid struct size for WinRing0 driver"); static_assert(sizeof(input) == 12, "Invalid struct size for WinRing0 driver");
input.reg = reg; input.reg = reg;
@ -150,9 +190,10 @@ static bool wrmsr(HANDLE hDriver, uint32_t reg, uint64_t value) {
DWORD output; DWORD output;
DWORD k; DWORD k;
if (!DeviceIoControl(hDriver, IOCTL_WRITE_MSR, &input, sizeof(input), &output, sizeof(output), &k, nullptr)) { if (!DeviceIoControl(hDriver, IOCTL_WRITE_MSR, &input, sizeof(input), &output, sizeof(output), &k, nullptr)) {
const DWORD err = GetLastError(); LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "cannot set MSR 0x%08" PRIx32 " to 0x%08" PRIx64, tag, reg, value);
LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "cannot set MSR 0x%08" PRIx32 " to 0x%08" PRIx64, xmrig::rx_tag(), reg, value);
return false; return false;
} }
@ -160,57 +201,62 @@ static bool wrmsr(HANDLE hDriver, uint32_t reg, uint64_t value) {
} }
} // namespace xmrig
void xmrig::Rx::osInit(const RxConfig &config) void xmrig::Rx::osInit(const RxConfig &config)
{ {
if ((config.wrmsr() < 0) || !ICpuInfo::isX64()) { if ((config.wrmsr() < 0) || !ICpuInfo::isX64()) {
return; return;
} }
const char* msr_mod_variant = (Cpu::info()->assembly() == Assembly::RYZEN) ? "Ryzen" : MsrMod mod = MSR_MOD_NONE;
((Cpu::info()->vendor() == ICpuInfo::VENDOR_INTEL) ? "Intel" : nullptr); if (Cpu::info()->assembly() == Assembly::RYZEN) {
mod = MSR_MOD_RYZEN;
}
else if (Cpu::info()->vendor() == ICpuInfo::VENDOR_INTEL) {
mod = MSR_MOD_INTEL;
}
if (!msr_mod_variant) { if (mod == MSR_MOD_NONE) {
return; return;
} }
LOG_INFO(CLEAR "%s" GREEN_BOLD_S "MSR mod: loading WinRing0 driver", xmrig::rx_tag()); const uint64_t ts = Chrono::steadyMSecs();
HANDLE hDriver = install_driver(); HANDLE hDriver = wrmsr_install_driver();
if (!hDriver) { if (!hDriver) {
if (hService) { wrmsr_uninstall_driver();
uninstall_driver();
CloseServiceHandle(hService);
}
if (hManager) { if (hManager) {
CloseServiceHandle(hManager); CloseServiceHandle(hManager);
} }
return; return;
} }
LOG_INFO(CLEAR "%s" GREEN_BOLD_S "MSR mod: setting MSR register values for %s", xmrig::rx_tag(), msr_mod_variant); std::thread wrmsr_thread([hDriver, mod, &config]() {
std::thread wrmsr_thread([hDriver, &config]() {
for (uint32_t i = 0, n = Cpu::info()->threads(); i < n; ++i) { for (uint32_t i = 0, n = Cpu::info()->threads(); i < n; ++i) {
Platform::setThreadAffinity(i); Platform::setThreadAffinity(i);
if (Cpu::info()->assembly() == Assembly::RYZEN) {
if (mod == MSR_MOD_RYZEN) {
wrmsr(hDriver, 0xC0011020, 0); wrmsr(hDriver, 0xC0011020, 0);
wrmsr(hDriver, 0xC0011021, 0x40); wrmsr(hDriver, 0xC0011021, 0x40);
wrmsr(hDriver, 0xC0011022, 0x510000); wrmsr(hDriver, 0xC0011022, 0x510000);
wrmsr(hDriver, 0xC001102b, 0x1808cc16); wrmsr(hDriver, 0xC001102b, 0x1808cc16);
} }
else if (Cpu::info()->vendor() == ICpuInfo::VENDOR_INTEL) { else if (mod == MSR_MOD_INTEL) {
wrmsr(hDriver, 0x1a4, config.wrmsr()); wrmsr(hDriver, 0x1a4, config.wrmsr());
} }
} }
}); });
wrmsr_thread.join(); wrmsr_thread.join();
CloseHandle(hDriver); CloseHandle(hDriver);
uninstall_driver(); wrmsr_uninstall_driver();
CloseServiceHandle(hService);
CloseServiceHandle(hManager); CloseServiceHandle(hManager);
LOG_INFO(CLEAR "%s" GREEN_BOLD_S "MSR mod: all done, WinRing0 driver unloaded", xmrig::rx_tag()); 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);
} }