HyperDbg Debugger
Loading...
Searching...
No Matches
rdmsr.cpp File Reference

rdmsr command More...

#include "pch.h"

Typedefs

typedef BOOL(WINAPI * glpie_t) (LOGICAL_PROCESSOR_RELATIONSHIP, PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD)
 defines the GetLogicalProcessorInformationEx function

Functions

VOID CommandRdmsrHelp ()
 help of the rdmsr command
VOID CommandRdmsr (vector< CommandToken > CommandTokens, string Command)
 rdmsr command handler

Variables

BOOLEAN g_IsKdModuleLoaded
 shows whether the kernel debugger (KD) module is loaded or not

Detailed Description

rdmsr command

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-05-27

Typedef Documentation

◆ glpie_t

typedef BOOL(WINAPI * glpie_t) (LOGICAL_PROCESSOR_RELATIONSHIP, PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD)

defines the GetLogicalProcessorInformationEx function

Function Documentation

◆ CommandRdmsr()

VOID CommandRdmsr ( vector< CommandToken > CommandTokens,
string Command )

rdmsr command handler

Parameters
CommandTokens
Command
Returns
VOID
121{
122 BOOL Status;
123 SIZE_T NumCPU;
124 DEBUGGER_READ_AND_WRITE_ON_MSR MsrReadRequest;
125 ULONG ReturnedLength;
126 UINT64 Msr;
127 BOOL IsNextCoreId = FALSE;
128 BOOL SetMsr = FALSE;
130 BOOLEAN IsFirstCommand = TRUE;
131
132 if (CommandTokens.size() >= 5)
133 {
134 ShowMessages("incorrect use of the '%s'\n\n",
135 GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
137 return;
138 }
139
140 for (auto Section : CommandTokens)
141 {
142 if (IsFirstCommand == TRUE)
143 {
144 IsFirstCommand = FALSE;
145 continue;
146 }
147
148 if (IsNextCoreId)
149 {
150 if (!ConvertTokenToUInt32(Section, &CoreNumer))
151 {
152 ShowMessages("please specify a correct hex value for core id\n\n");
154 return;
155 }
156 IsNextCoreId = FALSE;
157 continue;
158 }
159
160 if (CompareLowerCaseStrings(Section, "core"))
161 {
162 IsNextCoreId = TRUE;
163 continue;
164 }
165
166 if (SetMsr || !ConvertTokenToUInt64(Section, &Msr))
167 {
168 ShowMessages("please specify a correct hex value to be read\n\n");
170 return;
171 }
172 SetMsr = TRUE;
173 }
174
175 //
176 // Check if msr is set or not
177 //
178 if (!SetMsr)
179 {
180 ShowMessages("please specify a correct hex value to be read\n\n");
182 return;
183 }
184 if (IsNextCoreId)
185 {
186 ShowMessages("please specify a correct hex value for core\n\n");
188 return;
189 }
190
192
193 MsrReadRequest.ActionType = DEBUGGER_MSR_READ;
194 MsrReadRequest.Msr = Msr;
195 MsrReadRequest.CoreNumber = CoreNumer;
196
197 //
198 // Find logical cores count
199 //
200 SIZE_T NumCores = GetWindowsNumaNumberOfCores();
201 NumCPU = NumCores > 0 ? NumCores : GetWindowsCompatibleNumberOfCores();
202
203 //
204 // allocate buffer for transferring messages
205 //
206 UINT64 * OutputBuffer = (UINT64 *)malloc(sizeof(UINT64) * NumCPU);
207
208 ZeroMemory(OutputBuffer, sizeof(UINT64) * NumCPU);
209
210 Status = DeviceIoControl(
211 g_DeviceHandle, // Handle to device
212 IOCTL_DEBUGGER_READ_OR_WRITE_MSR, // IO Control Code (IOCTL)
213 &MsrReadRequest, // Input Buffer to driver.
214 SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR, // Input buffer length
215 OutputBuffer, // Output Buffer from driver.
216 (DWORD)(sizeof(UINT64) * NumCPU), // Length of output buffer in bytes.
217 &ReturnedLength, // Bytes placed in buffer.
218 NULL // synchronous call
219 );
220
221 if (!Status)
222 {
223 ShowMessages("ioctl failed with code (%x), either msr index or core id is invalid\n",
224 GetLastError());
225 free(OutputBuffer);
226 return;
227 }
228
229 //
230 // btw, %x is enough, no need to %llx
231 //
233 {
234 //
235 // Show all cores
236 //
237 for (SIZE_T i = 0; i < NumCPU; i++)
238 {
239 ShowMessages("core : 0x%x - msr[%llx] = %s\n", i, Msr, SeparateTo64BitValue((OutputBuffer[i])).c_str());
240 }
241 }
242 else
243 {
244 //
245 // Show for a single-core
246 //
247 ShowMessages("core : 0x%x - msr[%llx] = %s\n", CoreNumer, Msr, SeparateTo64BitValue((OutputBuffer[0])).c_str());
248 }
249
250 //
251 // Free the buffer
252 //
253 free(OutputBuffer);
254}
int BOOL
Definition BasicTypes.h:25
UCHAR BOOLEAN
Definition BasicTypes.h:35
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
unsigned long DWORD
Definition BasicTypes.h:38
unsigned int UINT32
Definition BasicTypes.h:54
unsigned long ULONG
Definition BasicTypes.h:31
#define DEBUGGER_READ_AND_WRITE_ON_MSR_APPLY_ALL_CORES
Read and write MSRs to all cores.
Definition Constants.h:727
#define IOCTL_DEBUGGER_READ_OR_WRITE_MSR
ioctl, request to read or write on a special MSR
Definition Ioctls.h:150
struct _DEBUGGER_READ_AND_WRITE_ON_MSR DEBUGGER_READ_AND_WRITE_ON_MSR
request to read or write on MSRs
#define SIZEOF_DEBUGGER_READ_AND_WRITE_ON_MSR
Definition RequestStructures.h:441
@ DEBUGGER_MSR_READ
Definition RequestStructures.h:450
string SeparateTo64BitValue(UINT64 Value)
std::string GetCaseSensitiveStringFromCommandToken(CommandToken TargetToken)
Get case sensitive string from command token.
Definition common.cpp:467
BOOLEAN CompareLowerCaseStrings(CommandToken TargetToken, const CHAR *StringToCompare)
Compare lower case strings.
Definition common.cpp:503
BOOLEAN ConvertTokenToUInt64(CommandToken TargetToken, PUINT64 Result)
add ` between 64 bit values and convert them to string
Definition common.cpp:447
BOOLEAN ConvertTokenToUInt32(CommandToken TargetToken, PUINT32 Result)
check and convert command token to a 32 bit unsigned integer
Definition common.cpp:546
#define ASSERT_MESSAGE_KD_NOT_LOADED
Definition common.h:29
#define AssertShowMessageReturnStmt(expr1, expr2, message1, message2, rc)
Definition common.h:59
#define AssertReturn
Definition common.h:19
#define ASSERT_MESSAGE_DRIVER_NOT_LOADED
Definition common.h:27
HANDLE g_DeviceHandle
Holds the global handle of device which is used to send the request to the kernel by IOCTL,...
Definition globals.h:481
BOOLEAN g_IsKdModuleLoaded
shows whether the kernel debugger (KD) module is loaded or not
Definition globals.h:22
VOID CommandRdmsrHelp()
help of the rdmsr command
Definition rdmsr.cpp:25
UINT32 CoreNumber
Definition RequestStructures.h:461
DEBUGGER_MSR_ACTION_TYPE ActionType
Definition RequestStructures.h:464
UINT64 Msr
Definition RequestStructures.h:460

◆ CommandRdmsrHelp()

VOID CommandRdmsrHelp ( )

help of the rdmsr command

Returns
VOID
26{
27 ShowMessages("rdmsr : reads a model-specific register (MSR).\n\n");
28
29 ShowMessages("syntax : \trdmsr [Msr (hex)] [core CoreNumber (hex)]\n");
30
31 ShowMessages("\n");
32 ShowMessages("\t\te.g : rdmsr c0000082\n");
33 ShowMessages("\t\te.g : rdmsr c0000082 core 2\n");
34}

Variable Documentation

◆ g_IsKdModuleLoaded

BOOLEAN g_IsKdModuleLoaded
extern

shows whether the kernel debugger (KD) module is loaded or not