KeInitSystem

UINT8 __stdcall KeInitSystem(UINT64 Phase){
  _LOADER_PARAMETER_BLOCK *v1; 
  unsigned int ActiveProcessorCount; 
  UINT64 v3; 
  unsigned __int64 v4; 
  unsigned int v5; 
  unsigned int v6; 
  int v7; 
  unsigned __int64 v8; 
  unsigned int v9; 
  _KPRCB **v10; 
  _KPRCB *v11; 
  UINT64 v12; 
  INT8 *v13; 
  int v14; 
  __int64 v15; 
  __int64 v16; 
  unsigned __int64 v17; 
  int v18; 
  struct _KPRCB *CurrentPrcb; 
  INT64 v21; 
  _BYTE *v22; 
  __int64 v23; 
  int v24; 
  __int64 v25; 
  UINT64 v26; 
  unsigned int v27; 
  _QWORD *PoolWithTag; 
  __int64 v29; 
  unsigned __int8 CurrentIrql; 
  int v31[2]; 
  __int64 v32; 
  __int64(__fastcall **v33)(); 
  __int64 v34; 
  __int64 v35; 
  int a2; 
  __int64 a1; 
  if( !(_DWORD)Phase )
  {
    CurrentPrcb = KeGetCurrentPrcb();
    if( (KeFeatureBits2 & 0x8000) != 0 && KiDisableTsx && (*((_QWORD *)CurrentPrcb + 1440) & 3) != 3 )
    {
      v25 = *((_QWORD *)CurrentPrcb + 1440) | 3i64;
      *((_QWORD *)CurrentPrcb + 1440) = v25;
      __writemsr(0x122u, v25);
    }
    KiTsxSupported = KiDetectTsx();
    KeInitializeSchedulerAssist(v21);
    KeInitializeCatRegisters();
    v24 = KeInitializeTimerTable(v23, v22);
    if( v24 < 0 )
      KeBugCheckEx(0x31u, v24, 1ui64, 0i64, 0i64);
    if( KiHrIncrement < (unsigned int)KeMinimumIncrement || KiHrIncrement > KeMaximumIncrement )
      KiHrIncrement = KeMinimumIncrement;
    return 1;
  }
  if( (_DWORD)Phase != 1 )
  {
    if( (_DWORD)Phase == 2 )
    {
      TraceLoggingRegisterEx_EtwRegister_EtwSetInformation(&dword_140C01A70);
    }
    else if( (_DWORD)Phase == 3 )
    {
      KiInitializeReservedCpuSets();
    }
    else
    {
      ExAcquireFastMutexUnsafe(&KiDynamicProcessorLock);
      MmFreeLoaderBlock(v1);
      KeLoaderBlock_0 = 0i64;
      ExReleaseFastMutexUnsafe(&KiDynamicProcessorLock);
      KiInitializeVelocity();
      KiRegisterForDisableFgBoostDecayRegistryNotification();
      if( !(unsigned __int8)HviIsAnyHypervisorPresent() )
        KeEnableWatchdogTimeout = 1;
      a1 = 0i64;
      a2 = 0;
      if( (HvlpFlags & 0x100000) != 0 )
      {
        PoolWithTag = ExAllocatePoolWithTag(NonPagedPoolNx, 0x4A0ui64, 0x4850654Bui64);
        KiEpfHashTable = (__int64)PoolWithTag;
        if( PoolWithTag )
        {
          v29 = 37i64;
          do
          {
            PoolWithTag[1] = PoolWithTag;
            *PoolWithTag = PoolWithTag;
            PoolWithTag[2] = 0i64;
            *((_DWORD *)PoolWithTag + 6) = 0;
            PoolWithTag += 4;
            --v29;
          }
          while( v29 );
          LODWORD(KiEpfCompletionDpc) = 275;
          qword_140C2AE98 = (__int64)KiEpfCompletionDpcRoutine;
          qword_140C2AEA0 = 0i64;
          qword_140C2AEB8 = 0i64;
          qword_140C2AE90 = 0i64;
          if( (int)HvlSetupPhysicalFaultNotificationQueue(&a1, &a2) >= 0 )
          {
            KiEpfCompletionQueueSize = (unsigned int)(a2 - 16) >> 3;
            KiEpfCompletionQueue = a1;
            CurrentIrql = KeGetCurrentIrql();
            __writecr8(2ui64);
            KiEpfDrainCompletionQueue();
            __writecr8(CurrentIrql);
          }
        }
      }
      KeFreeInitializationCode();
    }
    return 1;
  }
  _InterlockedOr(&dword_140D23E60, 0x8000u);
  _InterlockedOr(&dword_140D2427C, 0x400000u);
  KiInitDynamicTraceSupport();
  ActiveProcessorCount = KeQueryActiveProcessorCountEx(0xFFFFu);
  KiClockKeepAliveCycle = (ActiveProcessorCount + (KeMaximumIncrement + 29999999) / KeMaximumIncrement - 1)
                        / ActiveProcessorCount;
  if( (unsigned __int8)HviIsAnyHypervisorPresent() )
  {
    if( (_BYTE)HvlHypervisorConnected )
    {
      v26 = 0i64;
    }
    else
    {
      KeDpcWatchdogPeriod = 0;
      v26 = 1i64;
      KiDPCTimeout = 0;
    }
    KeRelaxTimingConstraints(v26);
  }
  v4 = (unsigned int)KeDpcWatchdogPeriod;
  if( KeDpcWatchdogPeriod )
  {
    if( (unsigned int)KeDpcWatchdogPeriod < 0x7D0 )
    {
      v4 = 2000i64;
      KeDpcWatchdogPeriod = 2000;
    }
    if( (unsigned int)v4 > 0x3A980 )
    {
      v4 = 240000i64;
      KeDpcWatchdogPeriod = 240000;
    }
  }
  v5 = KiDPCTimeout;
  if( KiDPCTimeout )
  {
    if( (unsigned int)KiDPCTimeout < 0x14 )
    {
      v5 = 20;
      KiDPCTimeout = 20;
    }
    if( v5 > 0x4E20 )
    {
      v5 = 20000;
      KiDPCTimeout = 20000;
    }
  }
  if( ViVerifierEnabled )
  {
    v27 = KeVerifierDpcScalingFactor;
    if( !KeVerifierDpcScalingFactor )
    {
      v27 = 1;
      KeVerifierDpcScalingFactor = 1;
    }
    if( v27 > 0x64 )
    {
      v27 = 100;
      KeVerifierDpcScalingFactor = 100;
    }
    v4 = v27 * (unsigned int)v4;
    v5 *= v27;
    KeDpcWatchdogPeriod = v4;
    KiDPCTimeout = v5;
  }
  if( (_DWORD)v4 )
  {
    if( v5 )
    {
      if( v5 < (unsigned int)v4 )
      {
        v6 = KeDpcWatchdogProfileOffset;
        if( KeDpcWatchdogProfileOffset )
        {
          if( (unsigned int)KeDpcWatchdogProfileOffset <= 0x3E8 )
          {
            v6 = 1000;
            KeDpcWatchdogProfileOffset = 1000;
          }
          if( v6 > (unsigned int)v4 )
          {
            v6 = 10000;
            if( (unsigned int)v4 <= 0x2710 )
              v6 = 1000;
            KeDpcWatchdogProfileOffset = v6;
          }
          v7 = v4 - v6;
          KiDpcWatchdogProfileCumulativeDpcThreshold = v4 - v6;
          if( (unsigned int)v4 - v6 < 0x3E8 )
          {
            KiDpcWatchdogProfileCumulativeDpcThreshold = 1000;
            v7 = 1000;
          }
          v8 = v5 * v7 / v4;
          v4 = 0xFFFFFFFFi64;
          if( v8 > 0xFFFFFFFF )
            LODWORD(v8) = -1;
          KiDpcWatchdogProfileSingleDpcThreshold = v8;
          if( v5 - (unsigned int)v8 > v6 )
            KiDpcWatchdogProfileSingleDpcThreshold = 0;
          v3 = v6 / 0x3E8;
          KiDpcWatchdogProfileArrayLength = 3328 * v3;
          dword_140CFA32C = 3328 * v3;
        }
      }
    }
  }
  v9 = 0;
  if( ActiveProcessorCount )
  {
    v10 = &KiProcessorBlock;
    while( 1 )
    {
      v11 = *v10;
      KiInitializeProcessor(*v10);
      if( KeThreadDpcEnable )
      {
        KiStartDpcThread((INT64)v11, v3, v12, v13);
        if( v14 < 0 )
          break;
      }
      ++v9;
      ++v10;
      if( v9 >= ActiveProcessorCount )
        goto LABEL_40;
    }
  }
  else
  {
LABEL_40:
    if( (unsigned __int8)KiComputeNumaCosts((PVOID)v4, v3) )
    {
      SymCryptInitEnvWindowsKernelmodeWin8_1nLater(0x640005u);
      KiIntSteerInit();
      if( (KeFeatureBits & 0x800000) != 0 && KUSER_SHARED_DATA.XState.EnabledSupervisorFeatures || (_DWORD)KiIptMsrMask )
      {
        v31[1] = 6;
        v33 = off_140008CD8;
        v32 = 512i64;
        v31[0] = 65548;
        v34 = 0i64;
        v35 = 0i64;
        if( (int)ExRegisterHost(&KiSupervisorStateExtensionHost, v15, (__int64)v31) < 0 )
          KiSupervisorStateExtensionHost = 0i64;
      }
      if( KiInitMachineDependent() )
      {
        v16 = *((unsigned int *)KiProcessorBlock + 17);
        v17 = v16 * (unsigned __int64)KeMaximumIncrement / 0xA;
        KiShortExecutionCycles = v17 / 0xF0;
        KiCyclesPerClockQuantum = v17 / 3;
        KiDirectQuantumTarget = v17 / 3;
        KiLockQuantumTarget = 3 * (v17 / 3);
        _BitScanReverse64(&v17, 6000000 * v16);
        KiFavoredCoreCycleTimeBits = v17;
        KdEncodeDataBlock();
        v18 = KeExpandKernelStackAndCalloutInternal(KiVerifyScopesExecute, 0i64, 49152i64);
        if( v18 < 0 )
          KeBugCheckEx(0x32u, v18, 0x5356694Bui64, 0i64, 0i64);
        return 1;
      }
    }
  }
  return 0;
}

Referenced by:

InitBootProcessor
Phase1InitializationDiscard
Phase1InitializationIoReady