1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528 |
- /*++
- Copyright (c) 2012 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- init.c
- Abstract:
- This module implements the kernel system startup.
- Author:
- Evan Green 2-Jul-2012
- Environment:
- Kernel Initialization
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/kernel/kernel.h>
- #include <minoca/lib/bconf.h>
- #include <minoca/kernel/bootload.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // The first row has at max 11 * 4 = 44 characters of value.
- // The second row has at max 13 + (4 * 4) + 11 + 11 = 51 characters of value.
- //
- #define KE_BANNER_FULL_WIDTH 116
- #define KE_BANNER_FULL_MEMORY_FORMAT \
- "Memory Used/Total: %s Paged Pool: %s Non-Paged Pool: %s Cache: %s"
- #define KE_BANNER_FULL_TIME_FORMAT \
- "Uptime: %s CPU User: %s Kernel: %s Interrupt: %s Idle: %s IO: %s%s"
- #define KE_BANNER_FULL_PAGING_FORMAT " Pg: %s"
- #define KE_BANNER_SHORT_WIDTH 80
- #define KE_BANNER_SHORT_MEMORY_FORMAT \
- "Memory: %s Paged: %s Non-paged: %s Cache: %s"
- #define KE_BANNER_SHORT_TIME_FORMAT \
- "%s U: %s K: %s In: %s Id: %s IO: %s%s"
- #define KE_BANNER_SHORT_PAGING_FORMAT " Pg: %s"
- #define KE_BANNER_TINY_WIDTH 40
- #define KE_BANNER_TINY_MEMORY_FORMAT "Memory: %s Cache: %s"
- #define KE_BANNER_TINY_TIME_FORMAT \
- "%s U%s K%s IO:%s"
- #define KE_BANNER_TINY_PAGING_FORMAT ""
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- typedef enum _KERNEL_SUBSYSTEM {
- KernelSubsystemInvalid,
- KernelSubsystemKernelDebugger,
- KernelSubsystemKernelExecutive,
- KernelSubsystemMemoryManager,
- KernelSubsystemObjectManager,
- KernelSubsystemAcpi,
- KernelSubsystemHardwareLayer,
- KernelSubsystemProcess,
- KernelSubsystemInputOutput,
- KernelSubsystemProfiler
- } KERNEL_SUBSYSTEM, *PKERNEL_SUBSYSTEM;
- typedef struct _SYSTEM_USAGE_CONTEXT {
- ULONGLONG TimeCounter;
- ULONGLONG TimeCounterFrequency;
- ULONGLONG CycleCounterFrequency;
- ULONGLONG UserCycles;
- ULONGLONG KernelCycles;
- ULONGLONG InterruptCycles;
- ULONGLONG IdleCycles;
- ULONGLONG TotalCycles;
- ULONG UserPercent;
- ULONG KernelPercent;
- ULONG InterruptPercent;
- ULONG IdlePercent;
- } SYSTEM_USAGE_CONTEXT, *PSYSTEM_USAGE_CONTEXT;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- VOID
- KepApplicationProcessorStartup (
- PPROCESSOR_START_BLOCK StartBlock
- );
- VOID
- KepCompleteSystemInitialization (
- PVOID Parameter
- );
- VOID
- KepAcquireProcessorStartLock (
- VOID
- );
- VOID
- KepReleaseProcessorStartLock (
- VOID
- );
- VOID
- KepBannerThread (
- PVOID Context
- );
- VOID
- KepUpdateSystemUsage (
- PSYSTEM_USAGE_CONTEXT Context
- );
- VOID
- KepPrintFormattedMemoryUsage (
- PCHAR String,
- ULONG StringSize,
- ULONGLONG UsedValue,
- ULONGLONG TotalValue
- );
- ULONG
- KepPrintFormattedSize (
- PCHAR String,
- ULONG StringSize,
- ULONGLONG Value
- );
- ULONG
- KepPrintFormattedPercent (
- PCHAR String,
- ULONG StringSize,
- ULONG PercentTimesTen
- );
- VOID
- KepQueueDistributionTimer (
- VOID
- );
- VOID
- KepDistributionTimerDpcRoutine (
- PDPC Dpc
- );
- VOID
- KepDistributionTimerWorkRoutine (
- PVOID Parameter
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Define a lock used to serializes parts of the AP startup execution.
- //
- volatile ULONG KeProcessorStartLock = 0;
- volatile ULONG KeProcessorsReady = 0;
- volatile BOOL KeAllProcessorsInitialize = FALSE;
- volatile BOOL KeAllProcessorsGo = FALSE;
- //
- // ------------------------------------------------------------------ Functions
- //
- VOID
- KepStartSystem (
- PKERNEL_INITIALIZATION_BLOCK Parameters
- )
- /*++
- Routine Description:
- This routine implements the first function called in the kernel from the
- boot loader.
- Arguments:
- Parameters - Supplies information about the system and memory layout as
- set up by the loader.
- Return Value:
- This function does not return.
- --*/
- {
- PBOOT_ENTRY BootEntry;
- PDEBUG_DEVICE_DESCRIPTION DebugDevice;
- KERNEL_SUBSYSTEM FailingSubsystem;
- ULONG ProcessorCount;
- KSTATUS Status;
- DebugDevice = NULL;
- FailingSubsystem = KernelSubsystemInvalid;
- //
- // Perform very basic processor initialization, preparing it to take
- // exceptions and use the serial port.
- //
- ArInitializeProcessor(FALSE, NULL);
- AcpiInitializePreDebugger(Parameters);
- Status = MmInitialize(Parameters, NULL, 0);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemMemoryManager;
- goto StartSystemEnd;
- }
- HlInitializePreDebugger(Parameters, 0, &DebugDevice);
- //
- // Initialize the debugging subsystem.
- //
- BootEntry = Parameters->BootEntry;
- if ((BootEntry != NULL) &&
- ((BootEntry->Flags & BOOT_ENTRY_FLAG_DEBUG) != 0)) {
- Status = KdInitialize(DebugDevice, Parameters->KernelModule);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemKernelDebugger;
- goto StartSystemEnd;
- }
- }
- //
- // Initialize the kernel executive.
- //
- Status = KeInitialize(0, Parameters);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemKernelExecutive;
- goto StartSystemEnd;
- }
- //
- // Perform phase 1 MM Initialization.
- //
- Status = MmInitialize(Parameters, NULL, 1);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemMemoryManager;
- goto StartSystemEnd;
- }
- //
- // Initialize the Object Manager.
- //
- Status = ObInitialize();
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemObjectManager;
- goto StartSystemEnd;
- }
- //
- // Perform phase 1 executive initialization, which sets up primitives like
- // queued locks and events.
- //
- Status = KeInitialize(1, Parameters);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemKernelExecutive;
- goto StartSystemEnd;
- }
- //
- // Initialize ACPI.
- //
- Status = AcpiInitialize(Parameters);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemAcpi;
- goto StartSystemEnd;
- }
- //
- // Initialize the hardware layer.
- //
- Status = HlInitialize(Parameters, 0);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemHardwareLayer;
- goto StartSystemEnd;
- }
- //
- // Initialize the process and thread subsystem.
- //
- Status = PsInitialize(0,
- Parameters,
- Parameters->KernelStack.Buffer,
- Parameters->KernelStack.Size);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemProcess;
- goto StartSystemEnd;
- }
- //
- // Perform phase 1 hardware layer initialization. The scheduler becomes
- // active at this point.
- //
- Status = HlInitialize(Parameters, 1);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemHardwareLayer;
- goto StartSystemEnd;
- }
- //
- // Now that the system is multithreaded, lock down MM.
- //
- Status = MmInitialize(Parameters, NULL, 2);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemMemoryManager;
- goto StartSystemEnd;
- }
- //
- // Perform additional process initialization now that MM is fully up.
- //
- Status = PsInitialize(1, Parameters, NULL, 0);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemProcess;
- goto StartSystemEnd;
- }
- //
- // Start all processors. Wait for all processors to initialize before
- // allowing the debugger to start broadcasting NMIs.
- //
- Status = HlStartAllProcessors(KepApplicationProcessorStartup,
- &ProcessorCount);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemHardwareLayer;
- goto StartSystemEnd;
- }
- KeAllProcessorsInitialize = TRUE;
- RtlAtomicAdd32(&KeProcessorsReady, 1);
- while (KeProcessorsReady != ProcessorCount) {
- ArProcessorYield();
- }
- KdEnableNmiBroadcast(TRUE);
- //
- // Perform phase 2 executive initialization, which creates things like the
- // worker threads.
- //
- Status = KeInitialize(2, Parameters);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemKernelExecutive;
- goto StartSystemEnd;
- }
- //
- // Initialize the system profiler subsystem, which will start profiling
- // only if early profiling is enabled.
- //
- Status = SpInitializeProfiler();
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemProfiler;
- goto StartSystemEnd;
- }
- //
- // Create a thread to continue system initialization that may involve
- // blocking, letting this thread become the idle thread. After this point,
- // the idle thread really is the idle thread.
- //
- Status = PsCreateKernelThread(KepCompleteSystemInitialization,
- Parameters,
- "Init");
- if (!KSUCCESS(Status)) {
- goto StartSystemEnd;
- }
- //
- // Boot mappings will be freed by the thread just kicked off, so the
- // parameters are now untouchable.
- //
- Parameters = NULL;
- StartSystemEnd:
- if (!KSUCCESS(Status)) {
- KeVideoPrintString(0, 14, "Kernel Failure: 0x");
- KeVideoPrintHexInteger(18, 14, Status);
- KeVideoPrintString(0, 15, "Subsystem: ");
- KeVideoPrintInteger(11, 15, FailingSubsystem);
- KeCrashSystem(CRASH_SYSTEM_INITIALIZATION_FAILURE,
- FailingSubsystem,
- Status,
- 0,
- 0);
- }
- //
- // Drop into the idle loop.
- //
- KeIdleLoop();
- return;
- }
- VOID
- KepApplicationProcessorStartup (
- PPROCESSOR_START_BLOCK StartBlock
- )
- /*++
- Routine Description:
- This routine implements the main initialization routine for processors
- other than P0.
- Arguments:
- StartBlock - Supplies a pointer to the processor start block that contains
- this processor's initialization information.
- Return Value:
- This function does not return, this thread eventually becomes the idle
- thread.
- --*/
- {
- KSTATUS Status;
- //
- // Mark the core as started.
- //
- StartBlock->Started = TRUE;
- RtlMemoryBarrier();
- //
- // Wait here until P0 says it's okay to initialize. This barrier allows
- // all processors to get out of the stub code as quickly as possible and
- // not have to worry about contending for non-paged pool locks while
- // allocating an idle stack.
- //
- while (KeAllProcessorsInitialize == FALSE) {
- ArProcessorYield();
- }
- KepAcquireProcessorStartLock();
- ArInitializeProcessor(FALSE, StartBlock->ProcessorStructures);
- //
- // Initialize the kernel executive.
- //
- Status = KeInitialize(0, NULL);
- if (!KSUCCESS(Status)) {
- goto ApplicationProcessorStartupEnd;
- }
- //
- // Perform phase 1 MM Initialization.
- //
- Status = MmInitialize(NULL, StartBlock, 1);
- if (!KSUCCESS(Status)) {
- goto ApplicationProcessorStartupEnd;
- }
- //
- // Perform phase 1 executive initialization.
- //
- Status = KeInitialize(1, NULL);
- if (!KSUCCESS(Status)) {
- goto ApplicationProcessorStartupEnd;
- }
- //
- // Initialize the hardware layer. The clock interrupt becomes active at
- // this point. As a result, this routine raises the run level from low to
- // dispatch to prevent the scheduler from becoming active before the
- // process and thread subsystem is initialized.
- //
- Status = HlInitialize(NULL, 0);
- if (!KSUCCESS(Status)) {
- goto ApplicationProcessorStartupEnd;
- }
- //
- // Initialize the process and thread subsystem.
- //
- Status = PsInitialize(0,
- NULL,
- StartBlock->StackBase,
- StartBlock->StackSize);
- if (!KSUCCESS(Status)) {
- goto ApplicationProcessorStartupEnd;
- }
- //
- // Perform phase 1 hardware layer initialization.
- //
- Status = HlInitialize(NULL, 1);
- if (!KSUCCESS(Status)) {
- goto ApplicationProcessorStartupEnd;
- }
- ApplicationProcessorStartupEnd:
- KeFreeProcessorStartBlock(StartBlock, FALSE);
- KepReleaseProcessorStartLock();
- StartBlock = NULL;
- //
- // On failure, take the system down.
- //
- if (!KSUCCESS(Status)) {
- KeCrashSystem(CRASH_SYSTEM_INITIALIZATION_FAILURE,
- KeGetCurrentProcessorNumber(),
- Status,
- 0,
- 0);
- }
- //
- // Wait until all processors are ready, and drop down to low level.
- //
- RtlAtomicAdd32(&KeProcessorsReady, 1);
- while (KeAllProcessorsGo == FALSE) {
- ArProcessorYield();
- }
- KeLowerRunLevel(RunLevelLow);
- KeIdleLoop();
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- VOID
- KepCompleteSystemInitialization (
- PVOID Parameter
- )
- /*++
- Routine Description:
- This routine completes initial kernel startup. It is performed on a
- separate thread to allow the startup thread to mature into the idle thread
- before blocking work starts. There is no guarantee that this routine will
- be executed exclusively on any one processor, the scheduler and all
- processors are active at this point.
- Arguments:
- Parameter - Supplies information about the system and memory layout as
- set up by the loader, the kernel initialization block.
- Return Value:
- None.
- --*/
- {
- KERNEL_SUBSYSTEM FailingSubsystem;
- PKERNEL_INITIALIZATION_BLOCK Parameters;
- KSTATUS Status;
- FailingSubsystem = KernelSubsystemInvalid;
- Parameters = (PKERNEL_INITIALIZATION_BLOCK)Parameter;
- //
- // Let all processors idle.
- //
- KeAllProcessorsGo = TRUE;
- //
- // Perform phase 0 initialization of the I/O subsystem, which will
- // initialize boot start drivers.
- //
- Status = IoInitialize(0, Parameters);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemInputOutput;
- goto CompleteSystemInitializationEnd;
- }
- //
- // Perform phase 3 executive initialization, which signs up for entropy
- // interface notifications.
- //
- Status = KeInitialize(3, NULL);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemKernelExecutive;
- goto CompleteSystemInitializationEnd;
- }
- //
- // Perform phase 3 initialization of the memory manager, which completes
- // initialization by freeing all boot allocations. From here on out, the
- // parameters pointer is inaccessible.
- //
- Status = MmInitialize(Parameters, NULL, 3);
- if (!KSUCCESS(Status)) {
- FailingSubsystem = KernelSubsystemMemoryManager;
- goto CompleteSystemInitializationEnd;
- }
- //
- // Fire up the banner thread.
- //
- PsCreateKernelThread(KepBannerThread, NULL, "KepBannerThread");
- //
- // Enable this for the free public builds.
- //
- #if 0
- KepQueueDistributionTimer();
- #endif
- CompleteSystemInitializationEnd:
- if (!KSUCCESS(Status)) {
- KeVideoPrintString(0, 24, "Failure: 0x");
- KeVideoPrintHexInteger(11, 24, Status);
- KeCrashSystem(CRASH_SYSTEM_INITIALIZATION_FAILURE,
- FailingSubsystem,
- Status,
- 0,
- 0);
- }
- return;
- }
- VOID
- KepAcquireProcessorStartLock (
- VOID
- )
- /*++
- Routine Description:
- This routine acquires the processor start lock.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- ULONG LockValue;
- while (TRUE) {
- LockValue = RtlAtomicCompareExchange32(&KeProcessorStartLock, 1, 0);
- if (LockValue == 0) {
- break;
- }
- ArProcessorYield();
- }
- return;
- }
- VOID
- KepReleaseProcessorStartLock (
- VOID
- )
- /*++
- Routine Description:
- This routine releases the processor start lock.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- ULONG LockValue;
- LockValue = RtlAtomicExchange32(&KeProcessorStartLock, 0);
- //
- // Assert if the lock was not held.
- //
- ASSERT(LockValue != 0);
- return;
- }
- VOID
- KepBannerThread (
- PVOID Context
- )
- /*++
- Routine Description:
- This routine prints an updated banner at the top of the screen.
- Arguments:
- Context - Supplies an unused context pointer.
- Return Value:
- None.
- --*/
- {
- CHAR BannerString[120];
- IO_CACHE_STATISTICS Cache;
- CHAR CacheString[16];
- CHAR CpuIdleString[16];
- CHAR CpuInterruptString[16];
- CHAR CpuKernelString[16];
- CHAR CpuUserString[16];
- ULONGLONG Days;
- ULONGLONG Frequency;
- ULONGLONG Hours;
- IO_GLOBAL_STATISTICS IoStatistics;
- CHAR IoString[16];
- MM_STATISTICS Memory;
- PSTR MemoryFormat;
- ULONGLONG Minutes;
- CHAR NonPagedPoolString[16];
- CHAR PagedPoolString[16];
- ULONG PageShift;
- PSTR PagingFormat;
- CHAR PagingString[24];
- CHAR PagingValueString[16];
- IO_GLOBAL_STATISTICS PreviousIoStatistics;
- ULONGLONG ReadDifference;
- ULONGLONG Seconds;
- ULONG Size;
- KSTATUS Status;
- ULONGLONG TimeCounter;
- PSTR TimeFormat;
- PKTIMER Timer;
- TIMER_QUEUE_TYPE TimerQueueType;
- CHAR TotalMemoryString[16];
- CHAR UptimeString[16];
- SYSTEM_USAGE_CONTEXT UsageContext;
- UINTN UsedSize;
- ULONG Width;
- ULONGLONG WriteDifference;
- Frequency = HlQueryTimeCounterFrequency();
- PageShift = MmPageShift();
- RtlZeroMemory(&Memory, sizeof(MM_STATISTICS));
- RtlZeroMemory(&Cache, sizeof(IO_CACHE_STATISTICS));
- RtlZeroMemory(&UsageContext, sizeof(SYSTEM_USAGE_CONTEXT));
- RtlZeroMemory(&PreviousIoStatistics, sizeof(IO_GLOBAL_STATISTICS));
- IoStatistics.Version = IO_GLOBAL_STATISTICS_VERSION;
- Memory.Version = MM_STATISTICS_VERSION;
- Cache.Version = IO_CACHE_STATISTICS_VERSION;
- if (!KSUCCESS(KeVideoGetDimensions(&Width, NULL))) {
- return;
- }
- if (Width > sizeof(BannerString) - 1) {
- Width = sizeof(BannerString) - 1;
- }
- //
- // Determine the right format given the width of the console.
- //
- if (Width >= KE_BANNER_FULL_WIDTH) {
- MemoryFormat = KE_BANNER_FULL_MEMORY_FORMAT;
- TimeFormat = KE_BANNER_FULL_TIME_FORMAT;
- PagingFormat = KE_BANNER_FULL_PAGING_FORMAT;
- } else if (Width >= KE_BANNER_SHORT_WIDTH) {
- MemoryFormat = KE_BANNER_SHORT_MEMORY_FORMAT;
- TimeFormat = KE_BANNER_SHORT_TIME_FORMAT;
- PagingFormat = KE_BANNER_SHORT_PAGING_FORMAT;
- } else if (Width >= KE_BANNER_TINY_WIDTH) {
- MemoryFormat = KE_BANNER_TINY_MEMORY_FORMAT;
- TimeFormat = KE_BANNER_TINY_TIME_FORMAT;
- PagingFormat = KE_BANNER_TINY_PAGING_FORMAT;
- } else {
- return;
- }
- Timer = KeCreateTimer(KE_ALLOCATION_TAG);
- if (Timer == NULL) {
- return;
- }
- while (TRUE) {
- Status = MmGetMemoryStatistics(&Memory);
- if (!KSUCCESS(Status)) {
- RtlDebugPrint("Failed to get MM statistics.\n");
- break;
- }
- Status = IoGetCacheStatistics(&Cache);
- if (!KSUCCESS(Status)) {
- RtlDebugPrint("Failed to get IO cache statistics.\n");
- }
- IoGetGlobalStatistics(&IoStatistics);
- TimeCounter = KeGetRecentTimeCounter();
- Seconds = TimeCounter / Frequency;
- Minutes = Seconds / 60;
- Seconds %= 60;
- Hours = Minutes / 60;
- Minutes %= 60;
- Days = Hours / 24;
- Hours %= 24;
- KepPrintFormattedMemoryUsage(TotalMemoryString,
- sizeof(TotalMemoryString),
- Memory.AllocatedPhysicalPages << PageShift,
- Memory.PhysicalPages << PageShift);
- UsedSize = Memory.PagedPool.TotalHeapSize -
- Memory.PagedPool.FreeListSize;
- KepPrintFormattedMemoryUsage(PagedPoolString,
- sizeof(PagedPoolString),
- UsedSize,
- Memory.PagedPool.TotalHeapSize);
- UsedSize = Memory.NonPagedPool.TotalHeapSize -
- Memory.NonPagedPool.FreeListSize;
- KepPrintFormattedMemoryUsage(NonPagedPoolString,
- sizeof(NonPagedPoolString),
- UsedSize,
- Memory.NonPagedPool.TotalHeapSize);
- KepPrintFormattedMemoryUsage(CacheString,
- sizeof(CacheString),
- Cache.DirtyPageCount << PageShift,
- Cache.PhysicalPageCount << PageShift);
- if (Width >= KE_BANNER_SHORT_WIDTH) {
- Size = RtlPrintToString(BannerString,
- Width + 1,
- CharacterEncodingDefault,
- MemoryFormat,
- TotalMemoryString,
- PagedPoolString,
- NonPagedPoolString,
- CacheString);
- } else {
- Size = RtlPrintToString(BannerString,
- Width + 1,
- CharacterEncodingDefault,
- MemoryFormat,
- TotalMemoryString,
- CacheString);
- }
- Size -= 1;
- while (Size < Width) {
- BannerString[Size] = ' ';
- Size += 1;
- }
- BannerString[Size] = '\0';
- KeVideoPrintString(0, 0, BannerString);
- //
- // Also update the second line, which contains the system usage.
- //
- KepUpdateSystemUsage(&UsageContext);
- if (Days == 0) {
- RtlPrintToString(UptimeString,
- sizeof(UptimeString),
- CharacterEncodingAscii,
- "%02I64d:%02I64d:%02I64d",
- Hours,
- Minutes,
- Seconds);
- } else {
- RtlPrintToString(UptimeString,
- sizeof(UptimeString),
- CharacterEncodingAscii,
- "%02I64d:%02I64d:%02I64d:%02I64d",
- Days,
- Hours,
- Minutes,
- Seconds);
- }
- KepPrintFormattedPercent(CpuUserString,
- sizeof(CpuUserString),
- UsageContext.UserPercent);
- KepPrintFormattedPercent(CpuKernelString,
- sizeof(CpuKernelString),
- UsageContext.KernelPercent);
- KepPrintFormattedPercent(CpuInterruptString,
- sizeof(CpuInterruptString),
- UsageContext.InterruptPercent);
- KepPrintFormattedPercent(CpuIdleString,
- sizeof(CpuIdleString),
- UsageContext.IdlePercent);
- ReadDifference = IoStatistics.BytesRead -
- PreviousIoStatistics.BytesRead;
- WriteDifference = IoStatistics.BytesWritten -
- PreviousIoStatistics.BytesWritten;
- KepPrintFormattedMemoryUsage(IoString,
- sizeof(IoString),
- ReadDifference,
- WriteDifference);
- ReadDifference = IoStatistics.PagingBytesRead -
- PreviousIoStatistics.PagingBytesRead;
- WriteDifference = IoStatistics.PagingBytesWritten -
- PreviousIoStatistics.PagingBytesWritten;
- PagingString[0] = '\0';
- if ((ReadDifference != 0) || (WriteDifference != 0)) {
- KepPrintFormattedMemoryUsage(PagingValueString,
- sizeof(PagingValueString),
- ReadDifference,
- WriteDifference);
- RtlPrintToString(PagingString,
- sizeof(PagingString),
- CharacterEncodingAscii,
- PagingFormat,
- PagingValueString);
- }
- RtlCopyMemory(&PreviousIoStatistics,
- &IoStatistics,
- sizeof(IO_GLOBAL_STATISTICS));
- if (Width >= KE_BANNER_SHORT_WIDTH) {
- Size = RtlPrintToString(BannerString,
- Width + 1,
- CharacterEncodingDefault,
- TimeFormat,
- UptimeString,
- CpuUserString,
- CpuKernelString,
- CpuInterruptString,
- CpuIdleString,
- IoString,
- PagingString);
- } else {
- Size = RtlPrintToString(BannerString,
- Width + 1,
- CharacterEncodingDefault,
- TimeFormat,
- UptimeString,
- CpuUserString,
- CpuKernelString,
- IoString);
- }
- Size -= 1;
- while (Size < Width) {
- BannerString[Size] = ' ';
- Size += 1;
- }
- BannerString[Size] = '\0';
- KeVideoPrintString(0, 1, BannerString);
- TimerQueueType = TimerQueueSoftWake;
- if ((Seconds % 5) == 0) {
- TimerQueueType = TimerQueueSoft;
- }
- KeQueueTimer(Timer,
- TimerQueueType,
- TimeCounter + Frequency,
- 0,
- 0,
- NULL);
- ObWaitOnObject(Timer, 0, WAIT_TIME_INDEFINITE);
- }
- KeDestroyTimer(Timer);
- return;
- }
- VOID
- KepUpdateSystemUsage (
- PSYSTEM_USAGE_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine updates the system usage information.
- Arguments:
- Context - Supplies a pointer to the context information.
- Return Value:
- None.
- --*/
- {
- PROCESSOR_CYCLE_ACCOUNTING Cycles;
- ULONGLONG DeltaTotal;
- ULONGLONG ExpectedTotalDelta;
- ULONGLONG IdleDelta;
- ULONGLONG InterruptDelta;
- ULONGLONG KernelDelta;
- ULONGLONG StoppedCycles;
- ULONGLONG TimeCounter;
- ULONGLONG TimeCounterDelta;
- ULONGLONG TotalCycles;
- ULONGLONG TotalDelta;
- ULONGLONG UserDelta;
- if (Context->TimeCounterFrequency == 0) {
- Context->TimeCounterFrequency = HlQueryTimeCounterFrequency();
- }
- if (Context->CycleCounterFrequency == 0) {
- Context->CycleCounterFrequency = HlQueryProcessorCounterFrequency();
- }
- //
- // Snap the time counter and cycle counters.
- //
- TimeCounter = HlQueryTimeCounter();
- KeGetTotalProcessorCycleAccounting(&Cycles);
- //
- // The cycle counter may not count while the processor is idle. Use the
- // time counter to figure out how many cycles there should have been, and
- // compare to how many there actually are. Any difference gets added to the
- // idle cycles.
- //
- TimeCounterDelta = TimeCounter - Context->TimeCounter;
- if (TimeCounterDelta == 0) {
- return;
- }
- //
- // TcTicks * CcTicks/ * s/ = CcTicks.
- // s TcTicks
- //
- ExpectedTotalDelta = TimeCounterDelta * Context->CycleCounterFrequency *
- KeGetActiveProcessorCount() /
- Context->TimeCounterFrequency;
- TotalCycles = Cycles.UserCycles + Cycles.KernelCycles +
- Cycles.InterruptCycles + Cycles.IdleCycles;
- TotalDelta = TotalCycles - Context->TotalCycles;
- StoppedCycles = 0;
- if (ExpectedTotalDelta > TotalDelta) {
- StoppedCycles = ExpectedTotalDelta - TotalDelta;
- }
- //
- // Compute the differences between this time and last time.
- //
- UserDelta = Cycles.UserCycles - Context->UserCycles;
- KernelDelta = Cycles.KernelCycles - Context->KernelCycles;
- InterruptDelta = Cycles.InterruptCycles - Context->InterruptCycles;
- IdleDelta = Cycles.IdleCycles - Context->IdleCycles + StoppedCycles;
- DeltaTotal = UserDelta + KernelDelta + InterruptDelta + IdleDelta;
- //
- // Save this snapshot into the context as the new previous snapshot.
- //
- Context->TimeCounter = TimeCounter;
- Context->UserCycles = Cycles.UserCycles;
- Context->KernelCycles = Cycles.KernelCycles;
- Context->InterruptCycles = Cycles.InterruptCycles;
- Context->IdleCycles = Cycles.IdleCycles;
- Context->TotalCycles = TotalCycles;
- //
- // Finally, update the percent (times ten) values.
- //
- Context->UserPercent = UserDelta * 1000 / DeltaTotal;
- Context->KernelPercent = KernelDelta * 1000 / DeltaTotal;
- Context->InterruptPercent = InterruptDelta * 1000 / DeltaTotal;
- Context->IdlePercent = IdleDelta * 1000 / DeltaTotal;
- return;
- }
- VOID
- KepPrintFormattedMemoryUsage (
- PCHAR String,
- ULONG StringSize,
- ULONGLONG UsedValue,
- ULONGLONG TotalValue
- )
- /*++
- Routine Description:
- This routine prints two formatted sizes a la 5.8M/64M.
- Arguments:
- String - Supplies a pointer to the string buffer to print to.
- StringSize - Supplies the total size of the string buffer in bytes.
- UsedValue - Supplies the first value to print.
- TotalValue - Supplies the second value to print.
- Return Value:
- None.
- --*/
- {
- ULONG Size;
- Size = KepPrintFormattedSize(String, StringSize, UsedValue);
- if (Size != 0) {
- Size -= 1;
- }
- String += Size;
- StringSize -= Size;
- if (StringSize > 1) {
- *String = '/';
- String += 1;
- StringSize -= 1;
- }
- if (StringSize > 1) {
- KepPrintFormattedSize(String, StringSize, TotalValue);
- }
- return;
- }
- ULONG
- KepPrintFormattedSize (
- PCHAR String,
- ULONG StringSize,
- ULONGLONG Value
- )
- /*++
- Routine Description:
- This routine prints a formatted size a la 5.8M (M for megabytes).
- Arguments:
- String - Supplies a pointer to the string buffer to print to.
- StringSize - Supplies the total size of the string buffer in bytes.
- Value - Supplies the value in bytes to print.
- Return Value:
- Returns the length of the final string after all formatting has been
- completed.
- --*/
- {
- ULONG Size;
- CHAR Suffix;
- Suffix = 'B';
- if (Value > 1024) {
- Suffix = 'K';
- Value = (Value * 10) / 1024;
- if (Value / 10 >= 1024) {
- Suffix = 'M';
- Value /= 1024;
- if (Value / 10 >= 1024) {
- Suffix = 'G';
- Value /= 1024;
- }
- }
- }
- ASSERT(Value < 1024 * 10);
- if (Suffix == 'B') {
- Size = RtlPrintToString(String,
- StringSize,
- CharacterEncodingAscii,
- "%d",
- (ULONG)Value);
- } else {
- if (Value < 100) {
- Size = RtlPrintToString(String,
- StringSize,
- CharacterEncodingAscii,
- "%d.%d%c",
- (ULONG)Value / 10,
- (ULONG)Value % 10,
- Suffix);
- } else {
- Size = RtlPrintToString(String,
- StringSize,
- CharacterEncodingAscii,
- "%d%c",
- (ULONG)Value / 10,
- Suffix);
- }
- }
- return Size;
- }
- ULONG
- KepPrintFormattedPercent (
- PCHAR String,
- ULONG StringSize,
- ULONG PercentTimesTen
- )
- /*++
- Routine Description:
- This routine prints a formatted percentage a la 5.8% or 99%. The field
- width is always 4.
- Arguments:
- String - Supplies a pointer to the string buffer to print to.
- StringSize - Supplies the total size of the string buffer in bytes.
- PercentTimesTen - Supplies ten times the percentage value. So 54.8% would
- have a value of 548. This value will be rounded to the precision that
- is printed.
- Offset - Supplies a pointer that on input supplies the offset within the
- string to print. This value will be updated to the new end of the
- string.
- Return Value:
- Returns the length of the final string after all formatting has been
- completed.
- --*/
- {
- ULONG Size;
- //
- // For values less than 10%, print the single digit and first decimal
- // point.
- //
- if (PercentTimesTen < 100) {
- Size = RtlPrintToString(String,
- StringSize,
- CharacterEncodingAscii,
- "%d.%d%%",
- PercentTimesTen / 10,
- PercentTimesTen % 10);
- } else {
- PercentTimesTen += 5;
- Size = RtlPrintToString(String,
- StringSize,
- CharacterEncodingAscii,
- "%3d%%",
- PercentTimesTen / 10);
- }
- return Size;
- }
- VOID
- KepQueueDistributionTimer (
- VOID
- )
- /*++
- Routine Description:
- This routine queues the distribution timer.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- PDPC Dpc;
- ULONGLONG DueTime;
- ULONGLONG Interval;
- KSTATUS Status;
- PKTIMER Timer;
- Dpc = NULL;
- //
- // Create the reset timer that reboots the system every few days.
- //
- Timer = KeCreateTimer(MM_ALLOCATION_TAG);
- if (Timer == NULL) {
- goto QueueDistributionTimerEnd;
- }
- Dpc = KeCreateDpc(KepDistributionTimerDpcRoutine, Timer);
- if (Dpc == NULL) {
- goto QueueDistributionTimerEnd;
- }
- Interval = MICROSECONDS_PER_SECOND * SECONDS_PER_DAY * 3;
- DueTime = KeGetRecentTimeCounter() +
- KeConvertMicrosecondsToTimeTicks(Interval);
- Status = KeQueueTimer(Timer, TimerQueueSoftWake, DueTime, 0, 0, Dpc);
- if (!KSUCCESS(Status)) {
- goto QueueDistributionTimerEnd;
- }
- Timer = NULL;
- Dpc = NULL;
- QueueDistributionTimerEnd:
- if (Timer != NULL) {
- KeDestroyTimer(Timer);
- }
- if (Dpc != NULL) {
- KeDestroyDpc(Dpc);
- }
- return;
- }
- VOID
- KepDistributionTimerDpcRoutine (
- PDPC Dpc
- )
- /*++
- Routine Description:
- This routine implements the distribution DPC routine. It is called when the
- distribution timer fires.
- Arguments:
- Dpc - Supplies a pointer to the DPC.
- Return Value:
- None.
- --*/
- {
- KeCreateAndQueueWorkItem(NULL,
- WorkPriorityNormal,
- KepDistributionTimerWorkRoutine,
- Dpc);
- return;
- }
- VOID
- KepDistributionTimerWorkRoutine (
- PVOID Parameter
- )
- /*++
- Routine Description:
- This routine prototype represents a work item.
- Arguments:
- Parameter - Supplies an optional parameter passed in by the creator of the
- work item.
- Return Value:
- None.
- --*/
- {
- KSTATUS (*ActionRoutine)(SYSTEM_RESET_TYPE);
- PDPC Dpc;
- PKTIMER Timer;
- Dpc = Parameter;
- Timer = Dpc->UserData;
- KeDestroyTimer(Timer);
- KeDestroyDpc(Dpc);
- //
- // Reset the system. Be casually tricky by not just calling the routine
- // directly. Really it's not that tricky.
- //
- ActionRoutine = KeResetSystem;
- ActionRoutine(SystemResetShutdown);
- return;
- }
|