init.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. /*++
  2. Copyright (c) 2012 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. init.c
  5. Abstract:
  6. This module implements the kernel system startup.
  7. Author:
  8. Evan Green 2-Jul-2012
  9. Environment:
  10. Kernel Initialization
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <minoca/kernel/kernel.h>
  16. #include <minoca/lib/bconf.h>
  17. #include <minoca/kernel/bootload.h>
  18. //
  19. // ---------------------------------------------------------------- Definitions
  20. //
  21. //
  22. // The first row has at max 11 * 4 = 44 characters of value.
  23. // The second row has at max 13 + (4 * 4) + 11 + 11 = 51 characters of value.
  24. //
  25. #define KE_BANNER_FULL_WIDTH 116
  26. #define KE_BANNER_FULL_MEMORY_FORMAT \
  27. "Memory Used/Total: %s Paged Pool: %s Non-Paged Pool: %s Cache: %s"
  28. #define KE_BANNER_FULL_TIME_FORMAT \
  29. "Uptime: %s CPU User: %s Kernel: %s Interrupt: %s Idle: %s IO: %s%s"
  30. #define KE_BANNER_FULL_PAGING_FORMAT " Pg: %s"
  31. #define KE_BANNER_SHORT_WIDTH 80
  32. #define KE_BANNER_SHORT_MEMORY_FORMAT \
  33. "Memory: %s Paged: %s Non-paged: %s Cache: %s"
  34. #define KE_BANNER_SHORT_TIME_FORMAT \
  35. "%s U: %s K: %s In: %s Id: %s IO: %s%s"
  36. #define KE_BANNER_SHORT_PAGING_FORMAT " Pg: %s"
  37. #define KE_BANNER_TINY_WIDTH 40
  38. #define KE_BANNER_TINY_MEMORY_FORMAT "Memory: %s Cache: %s"
  39. #define KE_BANNER_TINY_TIME_FORMAT \
  40. "%s U%s K%s IO:%s"
  41. #define KE_BANNER_TINY_PAGING_FORMAT ""
  42. //
  43. // ------------------------------------------------------ Data Type Definitions
  44. //
  45. typedef enum _KERNEL_SUBSYSTEM {
  46. KernelSubsystemInvalid,
  47. KernelSubsystemKernelDebugger,
  48. KernelSubsystemKernelExecutive,
  49. KernelSubsystemMemoryManager,
  50. KernelSubsystemObjectManager,
  51. KernelSubsystemAcpi,
  52. KernelSubsystemHardwareLayer,
  53. KernelSubsystemProcess,
  54. KernelSubsystemInputOutput,
  55. KernelSubsystemProfiler
  56. } KERNEL_SUBSYSTEM, *PKERNEL_SUBSYSTEM;
  57. typedef struct _SYSTEM_USAGE_CONTEXT {
  58. ULONGLONG TimeCounter;
  59. ULONGLONG TimeCounterFrequency;
  60. ULONGLONG CycleCounterFrequency;
  61. ULONGLONG UserCycles;
  62. ULONGLONG KernelCycles;
  63. ULONGLONG InterruptCycles;
  64. ULONGLONG IdleCycles;
  65. ULONGLONG TotalCycles;
  66. ULONG UserPercent;
  67. ULONG KernelPercent;
  68. ULONG InterruptPercent;
  69. ULONG IdlePercent;
  70. } SYSTEM_USAGE_CONTEXT, *PSYSTEM_USAGE_CONTEXT;
  71. //
  72. // ----------------------------------------------- Internal Function Prototypes
  73. //
  74. VOID
  75. KepApplicationProcessorStartup (
  76. PPROCESSOR_START_BLOCK StartBlock
  77. );
  78. VOID
  79. KepCompleteSystemInitialization (
  80. PVOID Parameter
  81. );
  82. VOID
  83. KepAcquireProcessorStartLock (
  84. VOID
  85. );
  86. VOID
  87. KepReleaseProcessorStartLock (
  88. VOID
  89. );
  90. VOID
  91. KepBannerThread (
  92. PVOID Context
  93. );
  94. VOID
  95. KepUpdateSystemUsage (
  96. PSYSTEM_USAGE_CONTEXT Context
  97. );
  98. VOID
  99. KepPrintFormattedMemoryUsage (
  100. PCHAR String,
  101. ULONG StringSize,
  102. ULONGLONG UsedValue,
  103. ULONGLONG TotalValue
  104. );
  105. ULONG
  106. KepPrintFormattedSize (
  107. PCHAR String,
  108. ULONG StringSize,
  109. ULONGLONG Value
  110. );
  111. ULONG
  112. KepPrintFormattedPercent (
  113. PCHAR String,
  114. ULONG StringSize,
  115. ULONG PercentTimesTen
  116. );
  117. VOID
  118. KepQueueDistributionTimer (
  119. VOID
  120. );
  121. VOID
  122. KepDistributionTimerDpcRoutine (
  123. PDPC Dpc
  124. );
  125. VOID
  126. KepDistributionTimerWorkRoutine (
  127. PVOID Parameter
  128. );
  129. //
  130. // -------------------------------------------------------------------- Globals
  131. //
  132. //
  133. // Define a lock used to serializes parts of the AP startup execution.
  134. //
  135. volatile ULONG KeProcessorStartLock = 0;
  136. volatile ULONG KeProcessorsReady = 0;
  137. volatile BOOL KeAllProcessorsInitialize = FALSE;
  138. volatile BOOL KeAllProcessorsGo = FALSE;
  139. //
  140. // ------------------------------------------------------------------ Functions
  141. //
  142. VOID
  143. KepStartSystem (
  144. PKERNEL_INITIALIZATION_BLOCK Parameters
  145. )
  146. /*++
  147. Routine Description:
  148. This routine implements the first function called in the kernel from the
  149. boot loader.
  150. Arguments:
  151. Parameters - Supplies information about the system and memory layout as
  152. set up by the loader.
  153. Return Value:
  154. This function does not return.
  155. --*/
  156. {
  157. PBOOT_ENTRY BootEntry;
  158. PDEBUG_DEVICE_DESCRIPTION DebugDevice;
  159. KERNEL_SUBSYSTEM FailingSubsystem;
  160. ULONG ProcessorCount;
  161. KSTATUS Status;
  162. DebugDevice = NULL;
  163. FailingSubsystem = KernelSubsystemInvalid;
  164. //
  165. // Perform very basic processor initialization, preparing it to take
  166. // exceptions and use the serial port.
  167. //
  168. ArInitializeProcessor(FALSE, NULL);
  169. AcpiInitializePreDebugger(Parameters);
  170. Status = MmInitialize(Parameters, NULL, 0);
  171. if (!KSUCCESS(Status)) {
  172. FailingSubsystem = KernelSubsystemMemoryManager;
  173. goto StartSystemEnd;
  174. }
  175. HlInitializePreDebugger(Parameters, 0, &DebugDevice);
  176. //
  177. // Initialize the debugging subsystem.
  178. //
  179. BootEntry = Parameters->BootEntry;
  180. if ((BootEntry != NULL) &&
  181. ((BootEntry->Flags & BOOT_ENTRY_FLAG_DEBUG) != 0)) {
  182. Status = KdInitialize(DebugDevice, Parameters->KernelModule);
  183. if (!KSUCCESS(Status)) {
  184. FailingSubsystem = KernelSubsystemKernelDebugger;
  185. goto StartSystemEnd;
  186. }
  187. }
  188. //
  189. // Initialize the kernel executive.
  190. //
  191. Status = KeInitialize(0, Parameters);
  192. if (!KSUCCESS(Status)) {
  193. FailingSubsystem = KernelSubsystemKernelExecutive;
  194. goto StartSystemEnd;
  195. }
  196. //
  197. // Perform phase 1 MM Initialization.
  198. //
  199. Status = MmInitialize(Parameters, NULL, 1);
  200. if (!KSUCCESS(Status)) {
  201. FailingSubsystem = KernelSubsystemMemoryManager;
  202. goto StartSystemEnd;
  203. }
  204. //
  205. // Initialize the Object Manager.
  206. //
  207. Status = ObInitialize();
  208. if (!KSUCCESS(Status)) {
  209. FailingSubsystem = KernelSubsystemObjectManager;
  210. goto StartSystemEnd;
  211. }
  212. //
  213. // Perform phase 1 executive initialization, which sets up primitives like
  214. // queued locks and events.
  215. //
  216. Status = KeInitialize(1, Parameters);
  217. if (!KSUCCESS(Status)) {
  218. FailingSubsystem = KernelSubsystemKernelExecutive;
  219. goto StartSystemEnd;
  220. }
  221. //
  222. // Initialize ACPI.
  223. //
  224. Status = AcpiInitialize(Parameters);
  225. if (!KSUCCESS(Status)) {
  226. FailingSubsystem = KernelSubsystemAcpi;
  227. goto StartSystemEnd;
  228. }
  229. //
  230. // Initialize the hardware layer.
  231. //
  232. Status = HlInitialize(Parameters, 0);
  233. if (!KSUCCESS(Status)) {
  234. FailingSubsystem = KernelSubsystemHardwareLayer;
  235. goto StartSystemEnd;
  236. }
  237. //
  238. // Initialize the process and thread subsystem.
  239. //
  240. Status = PsInitialize(0,
  241. Parameters,
  242. Parameters->KernelStack.Buffer,
  243. Parameters->KernelStack.Size);
  244. if (!KSUCCESS(Status)) {
  245. FailingSubsystem = KernelSubsystemProcess;
  246. goto StartSystemEnd;
  247. }
  248. //
  249. // Perform phase 1 hardware layer initialization. The scheduler becomes
  250. // active at this point.
  251. //
  252. Status = HlInitialize(Parameters, 1);
  253. if (!KSUCCESS(Status)) {
  254. FailingSubsystem = KernelSubsystemHardwareLayer;
  255. goto StartSystemEnd;
  256. }
  257. //
  258. // Now that the system is multithreaded, lock down MM.
  259. //
  260. Status = MmInitialize(Parameters, NULL, 2);
  261. if (!KSUCCESS(Status)) {
  262. FailingSubsystem = KernelSubsystemMemoryManager;
  263. goto StartSystemEnd;
  264. }
  265. //
  266. // Perform additional process initialization now that MM is fully up.
  267. //
  268. Status = PsInitialize(1, Parameters, NULL, 0);
  269. if (!KSUCCESS(Status)) {
  270. FailingSubsystem = KernelSubsystemProcess;
  271. goto StartSystemEnd;
  272. }
  273. //
  274. // Start all processors. Wait for all processors to initialize before
  275. // allowing the debugger to start broadcasting NMIs.
  276. //
  277. Status = HlStartAllProcessors(KepApplicationProcessorStartup,
  278. &ProcessorCount);
  279. if (!KSUCCESS(Status)) {
  280. FailingSubsystem = KernelSubsystemHardwareLayer;
  281. goto StartSystemEnd;
  282. }
  283. KeAllProcessorsInitialize = TRUE;
  284. RtlAtomicAdd32(&KeProcessorsReady, 1);
  285. while (KeProcessorsReady != ProcessorCount) {
  286. ArProcessorYield();
  287. }
  288. KdEnableNmiBroadcast(TRUE);
  289. //
  290. // Perform phase 2 executive initialization, which creates things like the
  291. // worker threads.
  292. //
  293. Status = KeInitialize(2, Parameters);
  294. if (!KSUCCESS(Status)) {
  295. FailingSubsystem = KernelSubsystemKernelExecutive;
  296. goto StartSystemEnd;
  297. }
  298. //
  299. // Initialize the system profiler subsystem, which will start profiling
  300. // only if early profiling is enabled.
  301. //
  302. Status = SpInitializeProfiler();
  303. if (!KSUCCESS(Status)) {
  304. FailingSubsystem = KernelSubsystemProfiler;
  305. goto StartSystemEnd;
  306. }
  307. //
  308. // Create a thread to continue system initialization that may involve
  309. // blocking, letting this thread become the idle thread. After this point,
  310. // the idle thread really is the idle thread.
  311. //
  312. Status = PsCreateKernelThread(KepCompleteSystemInitialization,
  313. Parameters,
  314. "Init");
  315. if (!KSUCCESS(Status)) {
  316. goto StartSystemEnd;
  317. }
  318. //
  319. // Boot mappings will be freed by the thread just kicked off, so the
  320. // parameters are now untouchable.
  321. //
  322. Parameters = NULL;
  323. StartSystemEnd:
  324. if (!KSUCCESS(Status)) {
  325. KeVideoPrintString(0, 14, "Kernel Failure: 0x");
  326. KeVideoPrintHexInteger(18, 14, Status);
  327. KeVideoPrintString(0, 15, "Subsystem: ");
  328. KeVideoPrintInteger(11, 15, FailingSubsystem);
  329. KeCrashSystem(CRASH_SYSTEM_INITIALIZATION_FAILURE,
  330. FailingSubsystem,
  331. Status,
  332. 0,
  333. 0);
  334. }
  335. //
  336. // Drop into the idle loop.
  337. //
  338. KeIdleLoop();
  339. return;
  340. }
  341. VOID
  342. KepApplicationProcessorStartup (
  343. PPROCESSOR_START_BLOCK StartBlock
  344. )
  345. /*++
  346. Routine Description:
  347. This routine implements the main initialization routine for processors
  348. other than P0.
  349. Arguments:
  350. StartBlock - Supplies a pointer to the processor start block that contains
  351. this processor's initialization information.
  352. Return Value:
  353. This function does not return, this thread eventually becomes the idle
  354. thread.
  355. --*/
  356. {
  357. KSTATUS Status;
  358. //
  359. // Mark the core as started.
  360. //
  361. StartBlock->Started = TRUE;
  362. RtlMemoryBarrier();
  363. //
  364. // Wait here until P0 says it's okay to initialize. This barrier allows
  365. // all processors to get out of the stub code as quickly as possible and
  366. // not have to worry about contending for non-paged pool locks while
  367. // allocating an idle stack.
  368. //
  369. while (KeAllProcessorsInitialize == FALSE) {
  370. ArProcessorYield();
  371. }
  372. KepAcquireProcessorStartLock();
  373. ArInitializeProcessor(FALSE, StartBlock->ProcessorStructures);
  374. //
  375. // Initialize the kernel executive.
  376. //
  377. Status = KeInitialize(0, NULL);
  378. if (!KSUCCESS(Status)) {
  379. goto ApplicationProcessorStartupEnd;
  380. }
  381. //
  382. // Perform phase 1 MM Initialization.
  383. //
  384. Status = MmInitialize(NULL, StartBlock, 1);
  385. if (!KSUCCESS(Status)) {
  386. goto ApplicationProcessorStartupEnd;
  387. }
  388. //
  389. // Perform phase 1 executive initialization.
  390. //
  391. Status = KeInitialize(1, NULL);
  392. if (!KSUCCESS(Status)) {
  393. goto ApplicationProcessorStartupEnd;
  394. }
  395. //
  396. // Initialize the hardware layer. The clock interrupt becomes active at
  397. // this point. As a result, this routine raises the run level from low to
  398. // dispatch to prevent the scheduler from becoming active before the
  399. // process and thread subsystem is initialized.
  400. //
  401. Status = HlInitialize(NULL, 0);
  402. if (!KSUCCESS(Status)) {
  403. goto ApplicationProcessorStartupEnd;
  404. }
  405. //
  406. // Initialize the process and thread subsystem.
  407. //
  408. Status = PsInitialize(0,
  409. NULL,
  410. StartBlock->StackBase,
  411. StartBlock->StackSize);
  412. if (!KSUCCESS(Status)) {
  413. goto ApplicationProcessorStartupEnd;
  414. }
  415. //
  416. // Perform phase 1 hardware layer initialization.
  417. //
  418. Status = HlInitialize(NULL, 1);
  419. if (!KSUCCESS(Status)) {
  420. goto ApplicationProcessorStartupEnd;
  421. }
  422. ApplicationProcessorStartupEnd:
  423. KeFreeProcessorStartBlock(StartBlock, FALSE);
  424. KepReleaseProcessorStartLock();
  425. StartBlock = NULL;
  426. //
  427. // On failure, take the system down.
  428. //
  429. if (!KSUCCESS(Status)) {
  430. KeCrashSystem(CRASH_SYSTEM_INITIALIZATION_FAILURE,
  431. KeGetCurrentProcessorNumber(),
  432. Status,
  433. 0,
  434. 0);
  435. }
  436. //
  437. // Wait until all processors are ready, and drop down to low level.
  438. //
  439. RtlAtomicAdd32(&KeProcessorsReady, 1);
  440. while (KeAllProcessorsGo == FALSE) {
  441. ArProcessorYield();
  442. }
  443. KeLowerRunLevel(RunLevelLow);
  444. KeIdleLoop();
  445. return;
  446. }
  447. //
  448. // --------------------------------------------------------- Internal Functions
  449. //
  450. VOID
  451. KepCompleteSystemInitialization (
  452. PVOID Parameter
  453. )
  454. /*++
  455. Routine Description:
  456. This routine completes initial kernel startup. It is performed on a
  457. separate thread to allow the startup thread to mature into the idle thread
  458. before blocking work starts. There is no guarantee that this routine will
  459. be executed exclusively on any one processor, the scheduler and all
  460. processors are active at this point.
  461. Arguments:
  462. Parameter - Supplies information about the system and memory layout as
  463. set up by the loader, the kernel initialization block.
  464. Return Value:
  465. None.
  466. --*/
  467. {
  468. KERNEL_SUBSYSTEM FailingSubsystem;
  469. PKERNEL_INITIALIZATION_BLOCK Parameters;
  470. KSTATUS Status;
  471. FailingSubsystem = KernelSubsystemInvalid;
  472. Parameters = (PKERNEL_INITIALIZATION_BLOCK)Parameter;
  473. //
  474. // Let all processors idle.
  475. //
  476. KeAllProcessorsGo = TRUE;
  477. //
  478. // Perform phase 0 initialization of the I/O subsystem, which will
  479. // initialize boot start drivers.
  480. //
  481. Status = IoInitialize(0, Parameters);
  482. if (!KSUCCESS(Status)) {
  483. FailingSubsystem = KernelSubsystemInputOutput;
  484. goto CompleteSystemInitializationEnd;
  485. }
  486. //
  487. // Perform phase 3 executive initialization, which signs up for entropy
  488. // interface notifications.
  489. //
  490. Status = KeInitialize(3, NULL);
  491. if (!KSUCCESS(Status)) {
  492. FailingSubsystem = KernelSubsystemKernelExecutive;
  493. goto CompleteSystemInitializationEnd;
  494. }
  495. //
  496. // Perform phase 3 initialization of the memory manager, which completes
  497. // initialization by freeing all boot allocations. From here on out, the
  498. // parameters pointer is inaccessible.
  499. //
  500. Status = MmInitialize(Parameters, NULL, 3);
  501. if (!KSUCCESS(Status)) {
  502. FailingSubsystem = KernelSubsystemMemoryManager;
  503. goto CompleteSystemInitializationEnd;
  504. }
  505. //
  506. // Fire up the banner thread.
  507. //
  508. PsCreateKernelThread(KepBannerThread, NULL, "KepBannerThread");
  509. //
  510. // Enable this for the free public builds.
  511. //
  512. #if 0
  513. KepQueueDistributionTimer();
  514. #endif
  515. CompleteSystemInitializationEnd:
  516. if (!KSUCCESS(Status)) {
  517. KeVideoPrintString(0, 24, "Failure: 0x");
  518. KeVideoPrintHexInteger(11, 24, Status);
  519. KeCrashSystem(CRASH_SYSTEM_INITIALIZATION_FAILURE,
  520. FailingSubsystem,
  521. Status,
  522. 0,
  523. 0);
  524. }
  525. return;
  526. }
  527. VOID
  528. KepAcquireProcessorStartLock (
  529. VOID
  530. )
  531. /*++
  532. Routine Description:
  533. This routine acquires the processor start lock.
  534. Arguments:
  535. None.
  536. Return Value:
  537. None.
  538. --*/
  539. {
  540. ULONG LockValue;
  541. while (TRUE) {
  542. LockValue = RtlAtomicCompareExchange32(&KeProcessorStartLock, 1, 0);
  543. if (LockValue == 0) {
  544. break;
  545. }
  546. ArProcessorYield();
  547. }
  548. return;
  549. }
  550. VOID
  551. KepReleaseProcessorStartLock (
  552. VOID
  553. )
  554. /*++
  555. Routine Description:
  556. This routine releases the processor start lock.
  557. Arguments:
  558. None.
  559. Return Value:
  560. None.
  561. --*/
  562. {
  563. ULONG LockValue;
  564. LockValue = RtlAtomicExchange32(&KeProcessorStartLock, 0);
  565. //
  566. // Assert if the lock was not held.
  567. //
  568. ASSERT(LockValue != 0);
  569. return;
  570. }
  571. VOID
  572. KepBannerThread (
  573. PVOID Context
  574. )
  575. /*++
  576. Routine Description:
  577. This routine prints an updated banner at the top of the screen.
  578. Arguments:
  579. Context - Supplies an unused context pointer.
  580. Return Value:
  581. None.
  582. --*/
  583. {
  584. CHAR BannerString[120];
  585. IO_CACHE_STATISTICS Cache;
  586. CHAR CacheString[16];
  587. CHAR CpuIdleString[16];
  588. CHAR CpuInterruptString[16];
  589. CHAR CpuKernelString[16];
  590. CHAR CpuUserString[16];
  591. ULONGLONG Days;
  592. ULONGLONG Frequency;
  593. ULONGLONG Hours;
  594. IO_GLOBAL_STATISTICS IoStatistics;
  595. CHAR IoString[16];
  596. MM_STATISTICS Memory;
  597. PSTR MemoryFormat;
  598. ULONGLONG Minutes;
  599. CHAR NonPagedPoolString[16];
  600. CHAR PagedPoolString[16];
  601. ULONG PageSize;
  602. PSTR PagingFormat;
  603. CHAR PagingString[24];
  604. CHAR PagingValueString[16];
  605. IO_GLOBAL_STATISTICS PreviousIoStatistics;
  606. ULONGLONG ReadDifference;
  607. ULONGLONG Seconds;
  608. ULONG Size;
  609. KSTATUS Status;
  610. ULONGLONG TimeCounter;
  611. PSTR TimeFormat;
  612. PKTIMER Timer;
  613. TIMER_QUEUE_TYPE TimerQueueType;
  614. CHAR TotalMemoryString[16];
  615. CHAR UptimeString[16];
  616. SYSTEM_USAGE_CONTEXT UsageContext;
  617. UINTN UsedSize;
  618. ULONG Width;
  619. ULONGLONG WriteDifference;
  620. Frequency = HlQueryTimeCounterFrequency();
  621. PageSize = MmPageSize();
  622. RtlZeroMemory(&Memory, sizeof(MM_STATISTICS));
  623. RtlZeroMemory(&Cache, sizeof(IO_CACHE_STATISTICS));
  624. RtlZeroMemory(&UsageContext, sizeof(SYSTEM_USAGE_CONTEXT));
  625. RtlZeroMemory(&PreviousIoStatistics, sizeof(IO_GLOBAL_STATISTICS));
  626. IoStatistics.Version = IO_GLOBAL_STATISTICS_VERSION;
  627. Memory.Version = MM_STATISTICS_VERSION;
  628. Cache.Version = IO_CACHE_STATISTICS_VERSION;
  629. if (!KSUCCESS(KeVideoGetDimensions(&Width, NULL))) {
  630. return;
  631. }
  632. if (Width > sizeof(BannerString) - 1) {
  633. Width = sizeof(BannerString) - 1;
  634. }
  635. //
  636. // Determine the right format given the width of the console.
  637. //
  638. if (Width >= KE_BANNER_FULL_WIDTH) {
  639. MemoryFormat = KE_BANNER_FULL_MEMORY_FORMAT;
  640. TimeFormat = KE_BANNER_FULL_TIME_FORMAT;
  641. PagingFormat = KE_BANNER_FULL_PAGING_FORMAT;
  642. } else if (Width >= KE_BANNER_SHORT_WIDTH) {
  643. MemoryFormat = KE_BANNER_SHORT_MEMORY_FORMAT;
  644. TimeFormat = KE_BANNER_SHORT_TIME_FORMAT;
  645. PagingFormat = KE_BANNER_SHORT_PAGING_FORMAT;
  646. } else if (Width >= KE_BANNER_TINY_WIDTH) {
  647. MemoryFormat = KE_BANNER_TINY_MEMORY_FORMAT;
  648. TimeFormat = KE_BANNER_TINY_TIME_FORMAT;
  649. PagingFormat = KE_BANNER_TINY_PAGING_FORMAT;
  650. } else {
  651. return;
  652. }
  653. Timer = KeCreateTimer(KE_ALLOCATION_TAG);
  654. if (Timer == NULL) {
  655. return;
  656. }
  657. while (TRUE) {
  658. Status = MmGetMemoryStatistics(&Memory);
  659. if (!KSUCCESS(Status)) {
  660. RtlDebugPrint("Failed to get MM statistics.\n");
  661. break;
  662. }
  663. Status = IoGetCacheStatistics(&Cache);
  664. if (!KSUCCESS(Status)) {
  665. RtlDebugPrint("Failed to get IO cache statistics.\n");
  666. }
  667. IoGetGlobalStatistics(&IoStatistics);
  668. TimeCounter = KeGetRecentTimeCounter();
  669. Seconds = TimeCounter / Frequency;
  670. Minutes = Seconds / 60;
  671. Seconds %= 60;
  672. Hours = Minutes / 60;
  673. Minutes %= 60;
  674. Days = Hours / 24;
  675. Hours %= 24;
  676. KepPrintFormattedMemoryUsage(TotalMemoryString,
  677. sizeof(TotalMemoryString),
  678. Memory.AllocatedPhysicalPages * PageSize,
  679. Memory.PhysicalPages * PageSize);
  680. UsedSize = Memory.PagedPool.TotalHeapSize -
  681. Memory.PagedPool.FreeListSize;
  682. KepPrintFormattedMemoryUsage(PagedPoolString,
  683. sizeof(PagedPoolString),
  684. UsedSize,
  685. Memory.PagedPool.TotalHeapSize);
  686. UsedSize = Memory.NonPagedPool.TotalHeapSize -
  687. Memory.NonPagedPool.FreeListSize;
  688. KepPrintFormattedMemoryUsage(NonPagedPoolString,
  689. sizeof(NonPagedPoolString),
  690. UsedSize,
  691. Memory.NonPagedPool.TotalHeapSize);
  692. KepPrintFormattedMemoryUsage(CacheString,
  693. sizeof(CacheString),
  694. Cache.DirtyPageCount * PageSize,
  695. Cache.PhysicalPageCount * PageSize);
  696. if (Width >= KE_BANNER_SHORT_WIDTH) {
  697. Size = RtlPrintToString(BannerString,
  698. Width + 1,
  699. CharacterEncodingDefault,
  700. MemoryFormat,
  701. TotalMemoryString,
  702. PagedPoolString,
  703. NonPagedPoolString,
  704. CacheString);
  705. } else {
  706. Size = RtlPrintToString(BannerString,
  707. Width + 1,
  708. CharacterEncodingDefault,
  709. MemoryFormat,
  710. TotalMemoryString,
  711. CacheString);
  712. }
  713. Size -= 1;
  714. while (Size < Width) {
  715. BannerString[Size] = ' ';
  716. Size += 1;
  717. }
  718. BannerString[Size] = '\0';
  719. KeVideoPrintString(0, 0, BannerString);
  720. //
  721. // Also update the second line, which contains the system usage.
  722. //
  723. KepUpdateSystemUsage(&UsageContext);
  724. if (Days == 0) {
  725. RtlPrintToString(UptimeString,
  726. sizeof(UptimeString),
  727. CharacterEncodingAscii,
  728. "%02I64d:%02I64d:%02I64d",
  729. Hours,
  730. Minutes,
  731. Seconds);
  732. } else {
  733. RtlPrintToString(UptimeString,
  734. sizeof(UptimeString),
  735. CharacterEncodingAscii,
  736. "%02I64d:%02I64d:%02I64d:%02I64d",
  737. Days,
  738. Hours,
  739. Minutes,
  740. Seconds);
  741. }
  742. KepPrintFormattedPercent(CpuUserString,
  743. sizeof(CpuUserString),
  744. UsageContext.UserPercent);
  745. KepPrintFormattedPercent(CpuKernelString,
  746. sizeof(CpuKernelString),
  747. UsageContext.KernelPercent);
  748. KepPrintFormattedPercent(CpuInterruptString,
  749. sizeof(CpuInterruptString),
  750. UsageContext.InterruptPercent);
  751. KepPrintFormattedPercent(CpuIdleString,
  752. sizeof(CpuIdleString),
  753. UsageContext.IdlePercent);
  754. ReadDifference = IoStatistics.BytesRead -
  755. PreviousIoStatistics.BytesRead;
  756. WriteDifference = IoStatistics.BytesWritten -
  757. PreviousIoStatistics.BytesWritten;
  758. KepPrintFormattedMemoryUsage(IoString,
  759. sizeof(IoString),
  760. ReadDifference,
  761. WriteDifference);
  762. ReadDifference = IoStatistics.PagingBytesRead -
  763. PreviousIoStatistics.PagingBytesRead;
  764. WriteDifference = IoStatistics.PagingBytesWritten -
  765. PreviousIoStatistics.PagingBytesWritten;
  766. PagingString[0] = '\0';
  767. if ((ReadDifference != 0) || (WriteDifference != 0)) {
  768. KepPrintFormattedMemoryUsage(PagingValueString,
  769. sizeof(PagingValueString),
  770. ReadDifference,
  771. WriteDifference);
  772. RtlPrintToString(PagingString,
  773. sizeof(PagingString),
  774. CharacterEncodingAscii,
  775. PagingFormat,
  776. PagingValueString);
  777. }
  778. RtlCopyMemory(&PreviousIoStatistics,
  779. &IoStatistics,
  780. sizeof(IO_GLOBAL_STATISTICS));
  781. if (Width >= KE_BANNER_SHORT_WIDTH) {
  782. Size = RtlPrintToString(BannerString,
  783. Width + 1,
  784. CharacterEncodingDefault,
  785. TimeFormat,
  786. UptimeString,
  787. CpuUserString,
  788. CpuKernelString,
  789. CpuInterruptString,
  790. CpuIdleString,
  791. IoString,
  792. PagingString);
  793. } else {
  794. Size = RtlPrintToString(BannerString,
  795. Width + 1,
  796. CharacterEncodingDefault,
  797. TimeFormat,
  798. UptimeString,
  799. CpuUserString,
  800. CpuKernelString,
  801. IoString);
  802. }
  803. Size -= 1;
  804. while (Size < Width) {
  805. BannerString[Size] = ' ';
  806. Size += 1;
  807. }
  808. BannerString[Size] = '\0';
  809. KeVideoPrintString(0, 1, BannerString);
  810. TimerQueueType = TimerQueueSoftWake;
  811. if ((Seconds % 5) == 0) {
  812. TimerQueueType = TimerQueueSoft;
  813. }
  814. KeQueueTimer(Timer,
  815. TimerQueueType,
  816. TimeCounter + Frequency,
  817. 0,
  818. 0,
  819. NULL);
  820. ObWaitOnObject(Timer, 0, WAIT_TIME_INDEFINITE);
  821. }
  822. KeDestroyTimer(Timer);
  823. return;
  824. }
  825. VOID
  826. KepUpdateSystemUsage (
  827. PSYSTEM_USAGE_CONTEXT Context
  828. )
  829. /*++
  830. Routine Description:
  831. This routine updates the system usage information.
  832. Arguments:
  833. Context - Supplies a pointer to the context information.
  834. Return Value:
  835. None.
  836. --*/
  837. {
  838. PROCESSOR_CYCLE_ACCOUNTING Cycles;
  839. ULONGLONG DeltaTotal;
  840. ULONGLONG ExpectedTotalDelta;
  841. ULONGLONG IdleDelta;
  842. ULONGLONG InterruptDelta;
  843. ULONGLONG KernelDelta;
  844. ULONGLONG StoppedCycles;
  845. ULONGLONG TimeCounter;
  846. ULONGLONG TimeCounterDelta;
  847. ULONGLONG TotalCycles;
  848. ULONGLONG TotalDelta;
  849. ULONGLONG UserDelta;
  850. if (Context->TimeCounterFrequency == 0) {
  851. Context->TimeCounterFrequency = HlQueryTimeCounterFrequency();
  852. }
  853. if (Context->CycleCounterFrequency == 0) {
  854. Context->CycleCounterFrequency = HlQueryProcessorCounterFrequency();
  855. }
  856. //
  857. // Snap the time counter and cycle counters.
  858. //
  859. TimeCounter = HlQueryTimeCounter();
  860. KeGetTotalProcessorCycleAccounting(&Cycles);
  861. //
  862. // The cycle counter may not count while the processor is idle. Use the
  863. // time counter to figure out how many cycles there should have been, and
  864. // compare to how many there actually are. Any difference gets added to the
  865. // idle cycles.
  866. //
  867. TimeCounterDelta = TimeCounter - Context->TimeCounter;
  868. if (TimeCounterDelta == 0) {
  869. return;
  870. }
  871. //
  872. // TcTicks * CcTicks/ * s/ = CcTicks.
  873. // s TcTicks
  874. //
  875. ExpectedTotalDelta = TimeCounterDelta * Context->CycleCounterFrequency *
  876. KeGetActiveProcessorCount() /
  877. Context->TimeCounterFrequency;
  878. TotalCycles = Cycles.UserCycles + Cycles.KernelCycles +
  879. Cycles.InterruptCycles + Cycles.IdleCycles;
  880. TotalDelta = TotalCycles - Context->TotalCycles;
  881. StoppedCycles = 0;
  882. if (ExpectedTotalDelta > TotalDelta) {
  883. StoppedCycles = ExpectedTotalDelta - TotalDelta;
  884. }
  885. //
  886. // Compute the differences between this time and last time.
  887. //
  888. UserDelta = Cycles.UserCycles - Context->UserCycles;
  889. KernelDelta = Cycles.KernelCycles - Context->KernelCycles;
  890. InterruptDelta = Cycles.InterruptCycles - Context->InterruptCycles;
  891. IdleDelta = Cycles.IdleCycles - Context->IdleCycles + StoppedCycles;
  892. DeltaTotal = UserDelta + KernelDelta + InterruptDelta + IdleDelta;
  893. //
  894. // Save this snapshot into the context as the new previous snapshot.
  895. //
  896. Context->TimeCounter = TimeCounter;
  897. Context->UserCycles = Cycles.UserCycles;
  898. Context->KernelCycles = Cycles.KernelCycles;
  899. Context->InterruptCycles = Cycles.InterruptCycles;
  900. Context->IdleCycles = Cycles.IdleCycles;
  901. Context->TotalCycles = TotalCycles;
  902. //
  903. // Finally, update the percent (times ten) values.
  904. //
  905. Context->UserPercent = UserDelta * 1000 / DeltaTotal;
  906. Context->KernelPercent = KernelDelta * 1000 / DeltaTotal;
  907. Context->InterruptPercent = InterruptDelta * 1000 / DeltaTotal;
  908. Context->IdlePercent = IdleDelta * 1000 / DeltaTotal;
  909. return;
  910. }
  911. VOID
  912. KepPrintFormattedMemoryUsage (
  913. PCHAR String,
  914. ULONG StringSize,
  915. ULONGLONG UsedValue,
  916. ULONGLONG TotalValue
  917. )
  918. /*++
  919. Routine Description:
  920. This routine prints two formatted sizes a la 5.8M/64M.
  921. Arguments:
  922. String - Supplies a pointer to the string buffer to print to.
  923. StringSize - Supplies the total size of the string buffer in bytes.
  924. UsedValue - Supplies the first value to print.
  925. TotalValue - Supplies the second value to print.
  926. Return Value:
  927. None.
  928. --*/
  929. {
  930. ULONG Size;
  931. Size = KepPrintFormattedSize(String, StringSize, UsedValue);
  932. if (Size != 0) {
  933. Size -= 1;
  934. }
  935. String += Size;
  936. StringSize -= Size;
  937. if (StringSize > 1) {
  938. *String = '/';
  939. String += 1;
  940. StringSize -= 1;
  941. }
  942. if (StringSize > 1) {
  943. KepPrintFormattedSize(String, StringSize, TotalValue);
  944. }
  945. return;
  946. }
  947. ULONG
  948. KepPrintFormattedSize (
  949. PCHAR String,
  950. ULONG StringSize,
  951. ULONGLONG Value
  952. )
  953. /*++
  954. Routine Description:
  955. This routine prints a formatted size a la 5.8M (M for megabytes).
  956. Arguments:
  957. String - Supplies a pointer to the string buffer to print to.
  958. StringSize - Supplies the total size of the string buffer in bytes.
  959. Value - Supplies the value in bytes to print.
  960. Return Value:
  961. Returns the length of the final string after all formatting has been
  962. completed.
  963. --*/
  964. {
  965. ULONG Size;
  966. CHAR Suffix;
  967. Suffix = 'B';
  968. if (Value > 1024) {
  969. Suffix = 'K';
  970. Value = (Value * 10) / 1024;
  971. if (Value / 10 >= 1024) {
  972. Suffix = 'M';
  973. Value /= 1024;
  974. if (Value / 10 >= 1024) {
  975. Suffix = 'G';
  976. Value /= 1024;
  977. }
  978. }
  979. }
  980. ASSERT(Value < 1024 * 10);
  981. if (Suffix == 'B') {
  982. Size = RtlPrintToString(String,
  983. StringSize,
  984. CharacterEncodingAscii,
  985. "%d",
  986. (ULONG)Value);
  987. } else {
  988. if (Value < 100) {
  989. Size = RtlPrintToString(String,
  990. StringSize,
  991. CharacterEncodingAscii,
  992. "%d.%d%c",
  993. (ULONG)Value / 10,
  994. (ULONG)Value % 10,
  995. Suffix);
  996. } else {
  997. Size = RtlPrintToString(String,
  998. StringSize,
  999. CharacterEncodingAscii,
  1000. "%d%c",
  1001. (ULONG)Value / 10,
  1002. Suffix);
  1003. }
  1004. }
  1005. return Size;
  1006. }
  1007. ULONG
  1008. KepPrintFormattedPercent (
  1009. PCHAR String,
  1010. ULONG StringSize,
  1011. ULONG PercentTimesTen
  1012. )
  1013. /*++
  1014. Routine Description:
  1015. This routine prints a formatted percentage a la 5.8% or 99%. The field
  1016. width is always 4.
  1017. Arguments:
  1018. String - Supplies a pointer to the string buffer to print to.
  1019. StringSize - Supplies the total size of the string buffer in bytes.
  1020. PercentTimesTen - Supplies ten times the percentage value. So 54.8% would
  1021. have a value of 548. This value will be rounded to the precision that
  1022. is printed.
  1023. Offset - Supplies a pointer that on input supplies the offset within the
  1024. string to print. This value will be updated to the new end of the
  1025. string.
  1026. Return Value:
  1027. Returns the length of the final string after all formatting has been
  1028. completed.
  1029. --*/
  1030. {
  1031. ULONG Size;
  1032. //
  1033. // For values less than 10%, print the single digit and first decimal
  1034. // point.
  1035. //
  1036. if (PercentTimesTen < 100) {
  1037. Size = RtlPrintToString(String,
  1038. StringSize,
  1039. CharacterEncodingAscii,
  1040. "%d.%d%%",
  1041. PercentTimesTen / 10,
  1042. PercentTimesTen % 10);
  1043. } else {
  1044. PercentTimesTen += 5;
  1045. Size = RtlPrintToString(String,
  1046. StringSize,
  1047. CharacterEncodingAscii,
  1048. "%3d%%",
  1049. PercentTimesTen / 10);
  1050. }
  1051. return Size;
  1052. }
  1053. VOID
  1054. KepQueueDistributionTimer (
  1055. VOID
  1056. )
  1057. /*++
  1058. Routine Description:
  1059. This routine queues the distribution timer.
  1060. Arguments:
  1061. None.
  1062. Return Value:
  1063. None.
  1064. --*/
  1065. {
  1066. PDPC Dpc;
  1067. ULONGLONG DueTime;
  1068. ULONGLONG Interval;
  1069. KSTATUS Status;
  1070. PKTIMER Timer;
  1071. Dpc = NULL;
  1072. //
  1073. // Create the reset timer that reboots the system every few days.
  1074. //
  1075. Timer = KeCreateTimer(MM_ALLOCATION_TAG);
  1076. if (Timer == NULL) {
  1077. goto QueueDistributionTimerEnd;
  1078. }
  1079. Dpc = KeCreateDpc(KepDistributionTimerDpcRoutine, Timer);
  1080. if (Dpc == NULL) {
  1081. goto QueueDistributionTimerEnd;
  1082. }
  1083. Interval = MICROSECONDS_PER_SECOND * SECONDS_PER_DAY * 3;
  1084. DueTime = KeGetRecentTimeCounter() +
  1085. KeConvertMicrosecondsToTimeTicks(Interval);
  1086. Status = KeQueueTimer(Timer, TimerQueueSoftWake, DueTime, 0, 0, Dpc);
  1087. if (!KSUCCESS(Status)) {
  1088. goto QueueDistributionTimerEnd;
  1089. }
  1090. Timer = NULL;
  1091. Dpc = NULL;
  1092. QueueDistributionTimerEnd:
  1093. if (Timer != NULL) {
  1094. KeDestroyTimer(Timer);
  1095. }
  1096. if (Dpc != NULL) {
  1097. KeDestroyDpc(Dpc);
  1098. }
  1099. return;
  1100. }
  1101. VOID
  1102. KepDistributionTimerDpcRoutine (
  1103. PDPC Dpc
  1104. )
  1105. /*++
  1106. Routine Description:
  1107. This routine implements the distribution DPC routine. It is called when the
  1108. distribution timer fires.
  1109. Arguments:
  1110. Dpc - Supplies a pointer to the DPC.
  1111. Return Value:
  1112. None.
  1113. --*/
  1114. {
  1115. KeCreateAndQueueWorkItem(NULL,
  1116. WorkPriorityNormal,
  1117. KepDistributionTimerWorkRoutine,
  1118. Dpc);
  1119. return;
  1120. }
  1121. VOID
  1122. KepDistributionTimerWorkRoutine (
  1123. PVOID Parameter
  1124. )
  1125. /*++
  1126. Routine Description:
  1127. This routine prototype represents a work item.
  1128. Arguments:
  1129. Parameter - Supplies an optional parameter passed in by the creator of the
  1130. work item.
  1131. Return Value:
  1132. None.
  1133. --*/
  1134. {
  1135. KSTATUS (*ActionRoutine)(SYSTEM_RESET_TYPE);
  1136. PDPC Dpc;
  1137. PKTIMER Timer;
  1138. Dpc = Parameter;
  1139. Timer = Dpc->UserData;
  1140. KeDestroyTimer(Timer);
  1141. KeDestroyDpc(Dpc);
  1142. //
  1143. // Reset the system. Be casually tricky by not just calling the routine
  1144. // directly. Really it's not that tricky.
  1145. //
  1146. ActionRoutine = KeResetSystem;
  1147. ActionRoutine(SystemResetShutdown);
  1148. return;
  1149. }