init.c 36 KB

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