HyperDbg/hyperdbg/libhyperdbg/code/debugger/commands/debugging-commands/rdmsr.cpp

261 lines
7 KiB
C++
Raw Permalink Normal View History

/**
* @file rdmsr.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
* @brief rdmsr command
* @details
* @version 0.1
* @date 2020-05-27
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
//
// Global Variables
//
extern BOOLEAN g_IsKdModuleLoaded;
2020-08-28 04:03:12 -07:00
/**
2023-07-13 16:05:42 +09:00
* @brief help of the rdmsr command
2021-02-10 15:19:01 -08:00
*
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandRdmsrHelp()
{
2022-04-10 22:24:56 +04:30
ShowMessages("rdmsr : reads a model-specific register (MSR).\n\n");
ShowMessages("syntax : \trdmsr [Msr (hex)] [core CoreNumber (hex)]\n");
ShowMessages("\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : rdmsr c0000082\n");
ShowMessages("\t\te.g : rdmsr c0000082 core 2\n");
}
2020-05-27 14:06:27 -07:00
#ifdef _WIN32
/// defines the GetLogicalProcessorInformationEx function
2024-04-07 16:05:43 +09:00
typedef BOOL(WINAPI * glpie_t)(
LOGICAL_PROCESSOR_RELATIONSHIP,
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX,
PDWORD);
/**
* @brief classic way to get number of cores in windows
2024-04-07 16:05:43 +09:00
*
* @return SIZE_T
*/
2024-04-07 16:05:43 +09:00
static SIZE_T
GetWindowsCompatibleNumberOfCores()
{
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
2024-04-07 16:05:43 +09:00
return (SIZE_T)SysInfo.dwNumberOfProcessors;
}
/*
2024-04-07 16:05:43 +09:00
* @brief Windows NUMA support is particular
* https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getlogicalprocessorinformationex#remarks
*
* New api here:
* https://learn.microsoft.com/en-us/windows/win32/procthread/numa-support
2024-04-07 16:05:43 +09:00
*
* @return SIZE_T
*/
2024-04-07 16:05:43 +09:00
static SIZE_T
GetWindowsNumaNumberOfCores()
{
2024-04-07 16:05:43 +09:00
uint8_t * Buffer = NULL;
SIZE_T NumCores = 0;
DWORD Length = 0;
HMODULE Kernel32 = GetModuleHandleW(L"kernel32.dll");
2024-04-07 16:05:43 +09:00
glpie_t GetLogicalProcessorInformationEx = (glpie_t)GetProcAddress(Kernel32, "GetLogicalProcessorInformationEx");
if (!GetLogicalProcessorInformationEx)
{
return 0;
}
2024-04-07 16:05:43 +09:00
GetLogicalProcessorInformationEx(RelationAll, NULL, &Length);
if (Length < 1 || PlatformGetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
return 0;
}
2024-04-07 16:05:43 +09:00
Buffer = (uint8_t *)malloc(Length);
if (!Buffer || !GetLogicalProcessorInformationEx(RelationAll, (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)Buffer, &Length))
{
2024-04-07 16:05:43 +09:00
free(Buffer);
return 0;
}
2024-04-07 16:05:43 +09:00
for (DWORD Offset = 0; Offset < Length;)
{
2024-04-07 16:05:43 +09:00
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX Info = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)(Buffer + Offset);
Offset += Info->Size;
if (Info->Relationship != RelationProcessorCore)
{
continue;
}
2024-04-07 16:05:43 +09:00
for (WORD group = 0; group < Info->Processor.GroupCount; ++group)
{
2024-04-07 16:05:43 +09:00
for (KAFFINITY Mask = Info->Processor.GroupMask[group].Mask; Mask != 0; Mask >>= 1)
{
2024-04-07 16:05:43 +09:00
NumCores += Mask & 1;
}
}
}
2024-04-07 16:05:43 +09:00
free(Buffer);
return NumCores;
}
2026-07-21 15:57:23 +02:00
#endif // _WIN32
2020-08-28 04:03:12 -07:00
/**
* @brief rdmsr command handler
2021-02-10 15:19:01 -08:00
*
2024-07-30 14:17:06 +09:00
* @param CommandTokens
* @param Command
2024-07-30 14:17:06 +09:00
*
2021-02-10 15:19:01 -08:00
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandRdmsr(vector<CommandToken> CommandTokens, string Command)
2021-03-22 18:19:39 +04:30
{
BOOL Status;
2024-04-07 16:05:43 +09:00
SIZE_T NumCPU;
2021-03-22 18:19:39 +04:30
DEBUGGER_READ_AND_WRITE_ON_MSR MsrReadRequest;
ULONG ReturnedLength;
UINT64 Msr;
2023-07-26 15:15:16 +09:00
BOOL IsNextCoreId = FALSE;
BOOL SetMsr = FALSE;
UINT32 CoreNumer = DEBUGGER_READ_AND_WRITE_ON_MSR_APPLY_ALL_CORES;
BOOLEAN IsFirstCommand = TRUE;
2021-03-22 18:19:39 +04:30
2024-07-30 14:17:06 +09:00
if (CommandTokens.size() >= 5)
2021-03-22 18:19:39 +04:30
{
2024-07-30 14:17:06 +09:00
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2021-03-22 18:19:39 +04:30
CommandRdmsrHelp();
return;
}
2024-07-30 14:17:06 +09:00
for (auto Section : CommandTokens)
2021-03-22 18:19:39 +04:30
{
2023-07-26 15:15:16 +09:00
if (IsFirstCommand == TRUE)
2021-03-22 18:19:39 +04:30
{
2023-07-26 15:15:16 +09:00
IsFirstCommand = FALSE;
2021-03-22 18:19:39 +04:30
continue;
}
if (IsNextCoreId)
{
2024-07-30 14:17:06 +09:00
if (!ConvertTokenToUInt32(Section, &CoreNumer))
2021-03-22 18:19:39 +04:30
{
ShowMessages("please specify a correct hex value for core id\n\n");
CommandRdmsrHelp();
return;
}
IsNextCoreId = FALSE;
continue;
}
2024-07-30 14:17:06 +09:00
if (CompareLowerCaseStrings(Section, "core"))
2021-03-22 18:19:39 +04:30
{
IsNextCoreId = TRUE;
continue;
}
2024-07-30 14:17:06 +09:00
if (SetMsr || !ConvertTokenToUInt64(Section, &Msr))
2021-03-22 18:19:39 +04:30
{
ShowMessages("please specify a correct hex value to be read\n\n");
CommandRdmsrHelp();
return;
}
SetMsr = TRUE;
}
//
// Check if msr is set or not
//
if (!SetMsr)
{
ShowMessages("please specify a correct hex value to be read\n\n");
CommandRdmsrHelp();
return;
}
2021-03-22 18:19:39 +04:30
if (IsNextCoreId)
{
ShowMessages("please specify a correct hex value for core\n\n");
CommandRdmsrHelp();
return;
}
AssertShowMessageReturnStmt(g_IsKdModuleLoaded, g_DeviceHandle, ASSERT_MESSAGE_KD_NOT_LOADED, ASSERT_MESSAGE_DRIVER_NOT_LOADED, AssertReturn);
2020-05-27 12:09:57 -07:00
2021-03-22 18:19:39 +04:30
MsrReadRequest.ActionType = DEBUGGER_MSR_READ;
MsrReadRequest.Msr = Msr;
MsrReadRequest.CoreNumber = CoreNumer;
2020-08-28 04:03:12 -07:00
//
2021-03-22 18:19:39 +04:30
// Find logical cores count
//
2026-07-22 10:02:29 +02:00
#ifdef _WIN32
2024-04-07 16:05:43 +09:00
SIZE_T NumCores = GetWindowsNumaNumberOfCores();
NumCPU = NumCores > 0 ? NumCores : GetWindowsCompatibleNumberOfCores();
2026-07-22 10:02:29 +02:00
#else
NumCPU = PlatformGetActiveProcessorCount();
#endif
2021-03-22 18:19:39 +04:30
//
2024-03-17 01:01:22 +09:00
// allocate buffer for transferring messages
2021-03-22 18:19:39 +04:30
//
UINT64 * OutputBuffer = (UINT64 *)malloc(sizeof(UINT64) * NumCPU);
PlatformZeroMemory(OutputBuffer, sizeof(UINT64) * NumCPU);
2021-03-22 18:19:39 +04:30
Status = PlatformDeviceIoControl(
2021-03-22 18:19:39 +04:30
g_DeviceHandle, // Handle to device
IOCTL_DEBUGGER_READ_OR_WRITE_MSR, // IO Control Code (IOCTL)
2021-03-22 18:19:39 +04:30
&MsrReadRequest, // Input Buffer to driver.
SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR, // Input buffer length
OutputBuffer, // Output Buffer from driver.
2024-04-07 16:05:43 +09:00
(DWORD)(sizeof(UINT64) * NumCPU), // Length of output buffer in bytes.
2021-03-22 18:19:39 +04:30
&ReturnedLength, // Bytes placed in buffer.
NULL // synchronous call
);
if (!Status)
{
ShowMessages("ioctl failed with code (%x), either msr index or core id is invalid\n",
PlatformGetLastError());
2021-03-22 18:19:39 +04:30
free(OutputBuffer);
return;
}
2021-03-22 18:19:39 +04:30
//
// btw, %x is enough, no need to %llx
//
if (CoreNumer == DEBUGGER_READ_AND_WRITE_ON_MSR_APPLY_ALL_CORES)
{
//
// Show all cores
//
2024-04-07 16:05:43 +09:00
for (SIZE_T i = 0; i < NumCPU; i++)
2021-03-22 18:19:39 +04:30
{
ShowMessages("core : 0x%x - msr[%llx] = %s\n", i, Msr, SeparateTo64BitValue((OutputBuffer[i])).c_str());
}
}
else
{
//
// Show for a single-core
//
ShowMessages("core : 0x%x - msr[%llx] = %s\n", CoreNumer, Msr, SeparateTo64BitValue((OutputBuffer[0])).c_str());
}
2021-02-10 15:19:01 -08:00
//
2021-03-22 18:19:39 +04:30
// Free the buffer
//
2021-03-22 18:19:39 +04:30
free(OutputBuffer);
2020-05-27 12:09:57 -07:00
}