2020-05-27 09:59:17 -07:00
|
|
|
/**
|
|
|
|
|
* @file wrmsr.cpp
|
2022-01-18 22:38:56 +03:30
|
|
|
* @author Sina Karvandi (sina@hyperdbg.org)
|
2020-05-27 09:59:17 -07:00
|
|
|
* @brief wrmsr command
|
|
|
|
|
* @details
|
|
|
|
|
* @version 0.1
|
|
|
|
|
* @date 2020-05-27
|
|
|
|
|
*
|
|
|
|
|
* @copyright This project is released under the GNU Public License v3.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2022-05-04 16:38:21 -07:00
|
|
|
#include "pch.h"
|
2020-05-27 09:59:17 -07:00
|
|
|
|
2026-06-08 23:45:43 +02:00
|
|
|
//
|
|
|
|
|
// 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 wrmsr 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
|
|
|
|
|
CommandWrmsrHelp()
|
|
|
|
|
{
|
2022-04-10 22:24:56 +04:30
|
|
|
ShowMessages("wrmsr : writes on a model-specific register (MSR).\n\n");
|
2022-04-17 23:05:24 +04:30
|
|
|
|
2022-02-08 16:06:26 +03:30
|
|
|
ShowMessages("syntax : \twrmsr [Msr (hex)] [Value (hex)] [core CoreNumber (hex)]\n");
|
2022-04-17 23:05:24 +04:30
|
|
|
|
|
|
|
|
ShowMessages("\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
ShowMessages("\t\te.g : wrmsr c0000082 fffff8077356f010\n");
|
2021-09-24 03:24:38 +03:30
|
|
|
ShowMessages("\t\te.g : wrmsr c0000082 @rcx\n");
|
|
|
|
|
ShowMessages("\t\te.g : wrmsr c0000082 @rcx+@rdx+12\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
ShowMessages("\t\te.g : wrmsr c0000082 fffff8077356f010 core 2\n");
|
2020-05-27 09:59:17 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-28 04:03:12 -07:00
|
|
|
/**
|
|
|
|
|
* @brief wrmsr command handler
|
2021-02-10 15:19:01 -08:00
|
|
|
*
|
2024-07-30 14:17:06 +09:00
|
|
|
* @param CommandTokens
|
2024-07-31 14:08:14 +09:00
|
|
|
* @param Command
|
2023-07-26 16:53:27 +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
|
2024-07-31 14:08:14 +09:00
|
|
|
CommandWrmsr(vector<CommandToken> CommandTokens, string Command)
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
BOOL Status;
|
|
|
|
|
UINT64 Msr;
|
2026-05-17 20:18:36 +02:00
|
|
|
ULONG ReturnedLength;
|
2023-07-26 15:15:16 +09:00
|
|
|
DEBUGGER_READ_AND_WRITE_ON_MSR MsrWriteRequest = {0};
|
|
|
|
|
BOOL IsNextCoreId = FALSE;
|
|
|
|
|
BOOL SetMsr = FALSE;
|
|
|
|
|
BOOL SetValue = FALSE;
|
|
|
|
|
UINT64 Value = 0;
|
|
|
|
|
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() >= 6)
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
2024-08-10 23:11:59 +09:00
|
|
|
ShowMessages("incorrect use of the '%s'\n\n",
|
|
|
|
|
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
|
2020-05-27 09:59:17 -07:00
|
|
|
CommandWrmsrHelp();
|
|
|
|
|
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");
|
|
|
|
|
CommandWrmsrHelp();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-26 16:53:27 +09:00
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!SetMsr)
|
|
|
|
|
{
|
2024-07-30 14:17:06 +09:00
|
|
|
if (!ConvertTokenToUInt64(Section, &Msr))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
ShowMessages("please specify a correct hex value to be read\n\n");
|
|
|
|
|
CommandWrmsrHelp();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// Means that the MSR is set, next we should read value
|
|
|
|
|
//
|
|
|
|
|
SetMsr = TRUE;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SetMsr)
|
|
|
|
|
{
|
2024-07-30 14:17:06 +09:00
|
|
|
if (!SymbolConvertNameOrExprToAddress(GetCaseSensitiveStringFromCommandToken(Section), &Value))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
ShowMessages(
|
2021-09-24 20:56:46 +03:30
|
|
|
"please specify a correct hex value or an expression to put on the msr\n\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
CommandWrmsrHelp();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetValue = TRUE;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-27 09:59:17 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
|
|
|
|
// Check if msr is set or not
|
|
|
|
|
//
|
|
|
|
|
if (!SetMsr)
|
|
|
|
|
{
|
|
|
|
|
ShowMessages("please specify a correct hex value to write\n\n");
|
2020-05-27 09:59:17 -07:00
|
|
|
CommandWrmsrHelp();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-26 16:53:27 +09:00
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
if (!SetValue)
|
|
|
|
|
{
|
|
|
|
|
ShowMessages("please specify a correct hex value to put on msr\n\n");
|
|
|
|
|
CommandWrmsrHelp();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-26 16:53:27 +09:00
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
if (IsNextCoreId)
|
|
|
|
|
{
|
|
|
|
|
ShowMessages("please specify a correct hex value for core\n\n");
|
2020-05-27 09:59:17 -07:00
|
|
|
CommandWrmsrHelp();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-08 23:45:43 +02:00
|
|
|
AssertShowMessageReturnStmt(g_IsKdModuleLoaded, g_DeviceHandle, ASSERT_MESSAGE_KD_NOT_LOADED, ASSERT_MESSAGE_DRIVER_NOT_LOADED, AssertReturn);
|
2020-05-27 09:59:17 -07:00
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
MsrWriteRequest.ActionType = DEBUGGER_MSR_WRITE;
|
|
|
|
|
MsrWriteRequest.Msr = Msr;
|
|
|
|
|
MsrWriteRequest.CoreNumber = CoreNumer;
|
|
|
|
|
MsrWriteRequest.Value = Value;
|
|
|
|
|
|
2026-07-20 12:27:14 +02:00
|
|
|
Status = PlatformDeviceIoControl(
|
2021-03-22 18:19:39 +04:30
|
|
|
g_DeviceHandle, // Handle to device
|
2023-10-25 15:05:29 +09:00
|
|
|
IOCTL_DEBUGGER_READ_OR_WRITE_MSR, // IO Control Code (IOCTL)
|
2021-03-22 18:19:39 +04:30
|
|
|
&MsrWriteRequest, // Input Buffer to driver.
|
|
|
|
|
SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR, // Input buffer length
|
2026-05-17 20:18:36 +02:00
|
|
|
&MsrWriteRequest, // Output Buffer from driver.
|
|
|
|
|
SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR, // Length of output buffer in bytes.
|
|
|
|
|
&ReturnedLength, // Bytes placed in buffer.
|
2021-03-22 18:19:39 +04:30
|
|
|
NULL // synchronous call
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!Status)
|
|
|
|
|
{
|
2022-03-24 15:13:22 +04:30
|
|
|
ShowMessages("ioctl failed with code (%x), either msr index or core id is invalid\n",
|
2026-07-20 12:27:14 +02:00
|
|
|
PlatformGetLastError());
|
2021-03-22 18:19:39 +04:30
|
|
|
return;
|
|
|
|
|
}
|
2020-05-27 09:59:17 -07:00
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
ShowMessages("\n");
|
2020-05-27 09:59:17 -07:00
|
|
|
}
|