-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsysinfo.c
502 lines (444 loc) · 14.4 KB
/
sysinfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*++
Copyright (c) Alex Ionescu. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED UNDER THE LESSER GNU PUBLIC LICENSE.
PLEASE READ THE FILE "COPYING" IN THE TOP LEVEL DIRECTORY.
Module Name:
sysinfo.c
Abstract:
The Native Command Line Interface (NCLI) is the command shell for the
TinyKRNL OS.
This module implements commands for displaying system information.
Environment:
Native mode
Revision History:
Alex Ionescu - Started Implementation - 23-Mar-06
--*/
#include "precomp.h"
/*++
* @name RtlCliShutdown
*
* The RtlCliShutdown routine FILLMEIN
*
* @param None.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliShutdown(VOID)
{
BOOLEAN Old;
//
// Get the shutdown privilege and shutdown the system
//
RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, TRUE, FALSE, &Old);
return ZwShutdownSystem(ShutdownNoReboot);
}
/*++
* @name RtlCliReboot
*
* The RtlCliReboot routine FILLMEIN
*
* @param None.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliReboot(VOID)
{
BOOLEAN Old;
//
// Get the shutdown privilege and shutdown the system
//
RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, TRUE, FALSE, &Old);
return ZwShutdownSystem(ShutdownReboot);
}
/*++
* @name RtlCliPowerOff
*
* The RtlCliPowerOff routine FILLMEIN
*
* @param None.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliPowerOff(VOID)
{
BOOLEAN Old;
//
// Get the shutdown privilege and shutdown the system
//
RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, TRUE, FALSE, &Old);
return ZwShutdownSystem(ShutdownPowerOff);
}
/*++
* @name RtlCliListDrivers
*
* The RtlCliListDrivers routine FILLMEIN
*
* @param None.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliListDrivers(VOID)
{
PRTL_PROCESS_MODULES ModuleInfo;
PRTL_PROCESS_MODULE_INFORMATION ModuleEntry;
NTSTATUS Status;
ULONG Size = 0;
ULONG i;
//
// Get the count first
//
Status = NtQuerySystemInformation(SystemModuleInformation,
&Size,
sizeof(Size),
NULL);
//
// Get the total buffer size
//
Size = sizeof(*ModuleInfo) + (Size * sizeof(*ModuleInfo));
//
// Allocate it
//
ModuleInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, Size);
//
// Query the buffer
//
Status = NtQuerySystemInformation(SystemModuleInformation,
ModuleInfo,
Size,
NULL);
//
// Display Header
//
RtlCliDisplayString("*** ACTIVE MODULE LIST - DUMPING %d MODULES\n",
ModuleInfo->NumberOfModules);
//
// Now walk every module in it
//
for (i = 0; i < ModuleInfo->NumberOfModules; i++)
{
//
// Check if we've displayed 20
// BUGBUG: Should be natively handled by our display routines
//
if (i && !(i % 20))
{
//
// Hold for more input
//
RtlCliDisplayString("--- PRESS SPACE TO CONTINUE ---\n");
while (RtlCliGetChar(hKeyboard) != ' ');
}
//
// Get this entry
//
ModuleEntry = &ModuleInfo->Modules[i];
//
// Display basic data
//
RtlCliDisplayString("%s - Base: %p Size: 0x%lx\n",
ModuleEntry->FullPathName,
ModuleEntry->ImageBase,
ModuleEntry->ImageSize);
}
//
// Return error code
//
return Status;
}
/*++
* @name RtlCliListProcesses
*
* The RtlCliListProcesses routine provides a way to list the current
* processes.
*
* @param None.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliListProcesses(VOID)
{
PSYSTEM_PROCESS_INFORMATION ModuleInfo;
NTSTATUS Status;
ULONG Size = 0x10000;
//
// Allocate a static buffer that should be large enough
//
ModuleInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, Size);
if (!ModuleInfo) return STATUS_INSUFFICIENT_RESOURCES;
//
// Query the buffer
//
Status = NtQuerySystemInformation(SystemProcessInformation,
ModuleInfo,
Size,
NULL);
if (!NT_SUCCESS(Status)) return Status;
//
// Display Header
//
RtlCliDisplayString("*** ACTIVE PROCESS LIST\n");
//
// Now walk every module in it
//
while (TRUE)
{
//
// Display basic data
//
RtlCliDisplayString("[%lx] %S - WS/PF/V:[%dK/%dK/%dK] Threads: %d\n",
ModuleInfo->UniqueProcessId,
ModuleInfo->ImageName.Buffer,
ModuleInfo->WorkingSetSize / 1024,
ModuleInfo->PagefileUsage / 1024,
ModuleInfo->VirtualSize / 1024,
ModuleInfo->NumberOfThreads);
//
// Break out if we're done
//
if (!ModuleInfo->NextEntryOffset) break;
//
// Get next entry
//
ModuleInfo = (PSYSTEM_PROCESS_INFORMATION)((ULONG_PTR)ModuleInfo +
ModuleInfo->NextEntryOffset);
}
//
// Return error code
//
return Status;
}
/*++
* @name RtlCliDumpSysInfo
*
* The RtlCliDumpSysInfo routine queries a large amount of system information
* and displays it on screen.
*
* @param None.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliDumpSysInfo(VOID)
{
NTSTATUS Status;
SYSTEM_BASIC_INFORMATION BasicInfo;
SYSTEM_PROCESSOR_INFORMATION ProcInfo;
SYSTEM_PERFORMANCE_INFORMATION PerfInfo;
SYSTEM_TIMEOFDAY_INFORMATION TimeInfo;
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION ProcPerfInfo[2];
SYSTEM_FILECACHE_INFORMATION CacheInfo;
PKUSER_SHARED_DATA SharedData = (PKUSER_SHARED_DATA)USER_SHARED_DATA;
TIME_FIELDS BootTime, IdleTime, KernelTime, UserTime, DpcTime;
//
// Query basic system information
//
Status = NtQuerySystemInformation(SystemBasicInformation,
&BasicInfo,
sizeof(BasicInfo),
NULL);
if (!NT_SUCCESS(Status)) return Status;
//
// Query basic processor information
//
Status = NtQuerySystemInformation(SystemProcessorInformation,
&ProcInfo,
sizeof(ProcInfo),
NULL);
if (!NT_SUCCESS(Status)) return Status;
//
// Query basic system information
//
Status = NtQuerySystemInformation(SystemPerformanceInformation,
&PerfInfo,
sizeof(PerfInfo),
NULL);
if (!NT_SUCCESS(Status)) return Status;
//
// Query basic system information
//
Status = NtQuerySystemInformation(SystemTimeOfDayInformation,
&TimeInfo,
sizeof(TimeInfo),
NULL);
if (!NT_SUCCESS(Status)) return Status;
//
// Query basic system information
//
Status = NtQuerySystemInformation(SystemProcessorPerformanceInformation,
&ProcPerfInfo,
sizeof(ProcPerfInfo),
NULL);
if (!NT_SUCCESS(Status)) return Status;
//
// Query basic system information
//
Status = NtQuerySystemInformation(SystemFileCacheInformation,
&CacheInfo,
sizeof(CacheInfo),
NULL);
if (!NT_SUCCESS(Status)) return Status;
//
// Display Header
// FIXME: Center it
//
RtlTimeToTimeFields(&TimeInfo.BootTime, &BootTime);
RtlCliDisplayString("Native shell running in %S booted on %02d-%02d-%02d "
"at %02d:%02d. CPUs: %d\n",
SharedData->NtSystemRoot,
BootTime.Day,
BootTime.Month,
BootTime.Year,
BootTime.Hour,
BootTime.Minute,
BasicInfo.NumberOfProcessors);
//
// Display System Flags
//
RtlCliDisplayString("Version: %x.%x. Debug Mode: %x. Safe Mode: %x "
"Product Type: %x. Suite Mask: %x\n",
SharedData->NtMajorVersion,
SharedData->NtMinorVersion,
SharedData->KdDebuggerEnabled,
SharedData->SafeBootMode,
SharedData->NtProductType,
SharedData->SuiteMask);
RtlCliDisplayString("-------------------------------------"
"-------------------------------------\n");
//
// Display CPU Information
//
RtlCliDisplayString("[CPU] %s Family %d Model %x Stepping %x. "
"Feature Bits: 0x%X NX: 0x%x\n",
(ProcInfo.ProcessorArchitecture ==
PROCESSOR_ARCHITECTURE_INTEL) ? "x86" : "Unknown",
ProcInfo.ProcessorLevel,
ProcInfo.ProcessorRevision >> 8,
ProcInfo.ProcessorRevision & 0xFF,
ProcInfo.ProcessorFeatureBits,
SharedData->NXSupportPolicy);
//
// Display RAM Information
//
RtlCliDisplayString("[RAM] Page Size: %dKB. Physical Pages: 0x%X. "
"Total Physical RAM: %dKB\n",
BasicInfo.PageSize / 1024,
BasicInfo.NumberOfPhysicalPages,
BasicInfo.NumberOfPhysicalPages * PAGE_SIZE / 1024);
//
// Display User-Mode Virtual Memory Information
//
RtlCliDisplayString("[USR] User-Mode Range: 0x%08X-0x%X. "
"Allocation Granularity: %dKB\n",
BasicInfo.MinimumUserModeAddress,
BasicInfo.MaximumUserModeAddress,
BasicInfo.AllocationGranularity / 1024);
//
// Display System Virtual Memory Information
//
RtlCliDisplayString("[VRAM] Free: %dKB. Committed: %dKB. "
"Total: %dKB. Peak: %dKB\n",
PerfInfo.AvailablePages * PAGE_SIZE / 1024,
PerfInfo.CommittedPages * PAGE_SIZE / 1024,
PerfInfo.CommitLimit * PAGE_SIZE / 1024,
PerfInfo.PeakCommitment * PAGE_SIZE / 1024);
//
// Display Kernel Memory/Pool Information
//
RtlCliDisplayString("[KRNL] Paged: %dKB. Non-Paged: %dKB. "
"Drivers: %dKB Code: %dKB\n",
PerfInfo.PagedPoolPages * PAGE_SIZE / 1024,
PerfInfo.NonPagedPoolPages * PAGE_SIZE / 1024,
PerfInfo.TotalSystemDriverPages * PAGE_SIZE / 1024,
PerfInfo.TotalSystemCodePages * PAGE_SIZE/ 1024);
//
// Check if we have two CPUs
//
if (BasicInfo.NumberOfProcessors > 1)
{
//
// Handle two CPU case by adding all of CPU 2's times into CPU 1's
// FIXME: This should be improved to support 2+ CPUs later
//
ProcPerfInfo[0].IdleTime.QuadPart +=
ProcPerfInfo[1].IdleTime.QuadPart;
ProcPerfInfo[0].KernelTime.QuadPart +=
ProcPerfInfo[1].KernelTime.QuadPart;
ProcPerfInfo[0].UserTime.QuadPart +=
ProcPerfInfo[1].UserTime.QuadPart;
ProcPerfInfo[0].DpcTime.QuadPart +=
ProcPerfInfo[1].DpcTime.QuadPart;
ProcPerfInfo[0].InterruptCount += ProcPerfInfo[1].InterruptCount;
}
//
// Convert all 64-bit times into a readable format
//
RtlTimeToTimeFields(&ProcPerfInfo[0].IdleTime, &IdleTime);
RtlTimeToTimeFields(&ProcPerfInfo[0].KernelTime, &KernelTime);
RtlTimeToTimeFields(&ProcPerfInfo[0].UserTime, &UserTime);
RtlTimeToTimeFields(&ProcPerfInfo[0].DpcTime, &DpcTime);
//
// Display System Times
//
RtlCliDisplayString("[TIME] Kernel: %02d:%02d:%02d. User: %02d:%02d:%02d. "
"DPC: %02d:%02d:%02d. Idle: %02d:%02d:%02d.\n",
KernelTime.Hour, KernelTime.Minute, KernelTime.Second,
UserTime.Hour, UserTime.Minute, UserTime.Second,
DpcTime.Hour, DpcTime.Minute, DpcTime.Second,
IdleTime.Hour, IdleTime.Minute, IdleTime.Second);
//
// Display Core Performance Information
//
RtlCliDisplayString("[PERF] INTs: %d. SysCalls: %d. PFs: %d. "
"Ctx Switches: %d\n",
ProcPerfInfo[0].InterruptCount,
PerfInfo.SystemCalls,
PerfInfo.PageFaultCount,
PerfInfo.ContextSwitches);
//
// Display I/O Information
//
RtlCliDisplayString("[I/O] Reads: %d/%I64dKB. Writes: %d/%I64dKB. "
"Others: %d/%I64dKB\n",
PerfInfo.IoReadOperationCount,
PerfInfo.IoReadTransferCount.QuadPart / 1024,
PerfInfo.IoWriteOperationCount,
PerfInfo.IoWriteTransferCount.QuadPart / 1024,
PerfInfo.IoOtherOperationCount,
PerfInfo.IoOtherTransferCount.QuadPart / 1024);
//
// Display FileSystem Cache Information
//
RtlCliDisplayString("[CACHE] Size: %dKB. Peak: %dKB. "
"Min WS: %dKB. Max WS: %dKB\n",
CacheInfo.CurrentSize / 1024,
CacheInfo.PeakSize / 1024,
CacheInfo.MinimumWorkingSet,
CacheInfo.MaximumWorkingSet);
//
// Return success
//
return STATUS_SUCCESS;
}