KeInitSystem

UINT8 __stdcall KeInitSystem(UINT64 Phase){
  _LOADER_PARAMETER_BLOCK *v1; 
  unsigned int ActiveProcessorCount; 
  unsigned __int64 v3; 
  unsigned int v4; 
  unsigned int v5; 
  int v6; 
  unsigned __int64 v7; 
  unsigned int v8; 
  _KPRCB **v9; 
  _KPRCB *v10; 
  char v11; 
  UINT64 v12; 
  __int64 MHz; 
  unsigned __int64 v14; 
  NTSTATUS v15; 
  struct _KPRCB *CurrentPrcb; 
  INT64 v18; 
  _KPRCB *v19; 
  NTSTATUS v20; 
  __int64 v21; 
  UINT64 v22; 
  unsigned int v23; 
  VOID **PoolWithTag; 
  __int64 v25; 
  unsigned __int8 CurrentIrql; 
  int RegistrationInfo[2]; 
  __int64 v28; 
  __int64(__fastcall **v29)(); 
  __int64 v30; 
  __int64 v31; 
  int v32; 
  __int64 v33; 

  if( !(_DWORD)Phase )
  {
    CurrentPrcb = KeGetCurrentPrcb();
    if( ((unsigned __int16)KeFeatureBits2 & 0x8000) != 0 && KiDisableTsx && (CurrentPrcb->MsrIa32TsxCtrl & 3) != 3 )
    {
      v21 = CurrentPrcb->MsrIa32TsxCtrl | 3;
      CurrentPrcb->MsrIa32TsxCtrl = v21;
      __writemsr(0x122u, v21);
    }
    KiTsxSupported = KiDetectTsx();
    KeInitializeSchedulerAssist(v18);
    KeInitializeCatRegisters();
    v20 = KeInitializeTimerTable(v19);
    if( v20 < 0 )
      KeBugCheckEx(0x31u, (PVOID)v20, (PVOID)1, 0i64, 0i64);
    if( KiHrIncrement < (unsigned int)KeMinimumIncrement || KiHrIncrement > (unsigned int)KeMaximumIncrement )
      KiHrIncrement = KeMinimumIncrement;
    return 1;
  }
  if( (_DWORD)Phase != 1 )
  {
    if( (_DWORD)Phase == 2 )
    {
      TraceLoggingRegisterEx_EtwRegister_EtwSetInformation((char *)&stru_140C00F40 + 2864);
    }
    else if( (_DWORD)Phase == 3 )
    {
      KiInitializeReservedCpuSets();
    }
    else
    {
      ExAcquireFastMutexUnsafe(&KiDynamicProcessorLock);
      MmFreeLoaderBlock(v1);
      KeLoaderBlock_0 = 0i64;
      ExReleaseFastMutexUnsafe(&KiDynamicProcessorLock);
      KiInitializeVelocity();
      KiRegisterForDisableFgBoostDecayRegistryNotification();
      if( !HviIsAnyHypervisorPresent() )
        KeEnableWatchdogTimeout = 1;
      v33 = 0i64;
      v32 = 0;
      if( (HvlpFlags & 0x100000) != 0 )
      {
        PoolWithTag = ExAllocatePoolWithTag(0x200ui64, 0x4A0ui64, 1213228363i64);
        KiEpfHashTable = (__int64)PoolWithTag;
        if( PoolWithTag )
        {
          v25 = 37i64;
          do
          {
            PoolWithTag[1] = PoolWithTag;
            *PoolWithTag = PoolWithTag;
            PoolWithTag[2] = 0i64;
            *((_DWORD *)PoolWithTag + 6) = 0;
            PoolWithTag += 4;
            --v25;
          }
          while( v25 );
          KiEpfCompletionDpc.TargetInfoAsUlong = 275;
          KiEpfCompletionDpc.DeferredRoutine = (void(__fastcall *)(_KDPC *, void *, void *, void *))KiEpfCompletionDpcRoutine;
          KiEpfCompletionDpc.DeferredContext = 0i64;
          KiEpfCompletionDpc.DpcData = 0i64;
          KiEpfCompletionDpc.ProcessorHistory = 0i64;
          if( (int)HvlSetupPhysicalFaultNotificationQueue(&v33, &v32) >= 0 )
          {
            KiEpfCompletionQueueSize = (unsigned int)(v32 - 16) >> 3;
            KiEpfCompletionQueue = v33;
            CurrentIrql = KeGetCurrentIrql();
            __writecr8(2ui64);
            KiEpfDrainCompletionQueue();
            __writecr8(CurrentIrql);
          }
        }
      }
      KeFreeInitializationCode();
    }
    return 1;
  }
  _InterlockedOr(dword_140D23E60, 0x8000u);
  _InterlockedOr(dword_140D2427C, 0x400000u);
  KiInitDynamicTraceSupport();
  ActiveProcessorCount = KeQueryActiveProcessorCountEx(0xFFFFu);
  KiClockKeepAliveCycle = (ActiveProcessorCount
                         + ((int)KeMaximumIncrement + 29999999) / (unsigned int)KeMaximumIncrement
                         - 1)
                        / ActiveProcessorCount;
  if( HviIsAnyHypervisorPresent() )
  {
    if( (_BYTE)HvlHypervisorConnected )
    {
      v22 = 0i64;
    }
    else
    {
      KeDpcWatchdogPeriod = 0;
      v22 = 1i64;
      KiDPCTimeout = 0;
    }
    KeRelaxTimingConstraints(v22);
  }
  v3 = (unsigned int)KeDpcWatchdogPeriod;
  if( KeDpcWatchdogPeriod )
  {
    if( (unsigned int)KeDpcWatchdogPeriod < 0x7D0 )
    {
      v3 = 2000i64;
      KeDpcWatchdogPeriod = 2000;
    }
    if( (unsigned int)v3 > 0x3A980 )
    {
      v3 = 240000i64;
      KeDpcWatchdogPeriod = 240000;
    }
  }
  v4 = KiDPCTimeout;
  if( KiDPCTimeout )
  {
    if( (unsigned int)KiDPCTimeout < 0x14 )
    {
      v4 = 20;
      KiDPCTimeout = 20;
    }
    if( v4 > 0x4E20 )
    {
      v4 = 20000;
      KiDPCTimeout = 20000;
    }
  }
  if( ViVerifierEnabled )
  {
    v23 = KeVerifierDpcScalingFactor;
    if( !KeVerifierDpcScalingFactor )
    {
      v23 = 1;
      KeVerifierDpcScalingFactor = 1;
    }
    if( v23 > 0x64 )
    {
      v23 = 100;
      KeVerifierDpcScalingFactor = 100;
    }
    v3 = v23 * (unsigned int)v3;
    v4 *= v23;
    KeDpcWatchdogPeriod = v3;
    KiDPCTimeout = v4;
  }
  if( (_DWORD)v3 )
  {
    if( v4 )
    {
      if( v4 < (unsigned int)v3 )
      {
        v5 = KeDpcWatchdogProfileOffset;
        if( KeDpcWatchdogProfileOffset )
        {
          if( (unsigned int)KeDpcWatchdogProfileOffset <= 0x3E8 )
          {
            v5 = 1000;
            KeDpcWatchdogProfileOffset = 1000;
          }
          if( v5 > (unsigned int)v3 )
          {
            v5 = 10000;
            if( (unsigned int)v3 <= 0x2710 )
              v5 = 1000;
            KeDpcWatchdogProfileOffset = v5;
          }
          v6 = v3 - v5;
          dword_140C50A7C = v3 - v5;
          if( (unsigned int)v3 - v5 < 0x3E8 )
          {
            dword_140C50A7C = 1000;
            v6 = 1000;
          }
          v7 = v4 * v6 / v3;
          if( v7 > 0xFFFFFFFF )
            LODWORD(v7) = -1;
          dword_140C50A80 = v7;
          if( v4 - (unsigned int)v7 > v5 )
            dword_140C50A80 = 0;
          dword_140C50A78 = 3328 * (v5 / 0x3E8);
          dword_140CFA32C = 3328 * (v5 / 0x3E8);
        }
      }
    }
  }
  v8 = 0;
  if( ActiveProcessorCount )
  {
    v9 = &KiProcessorBlock;
    while( 1 )
    {
      v10 = *v9;
      KiInitializeProcessor(*v9);
      if( KeThreadDpcEnable )
      {
        if( KiStartDpcThread(v10) < 0 )
          break;
      }
      ++v8;
      ++v9;
      if( v8 >= ActiveProcessorCount )
        goto LABEL_40;
    }
  }
  else
  {
LABEL_40:
    KiComputeNumaCosts();
    if( v11 )
    {
      SymCryptInitEnvWindowsKernelmodeWin8_1nLater(0x640005u);
      KiIntSteerInit();
      if( ((unsigned __int64)KeFeatureBits & 0x800000) != 0 && KUSER_SHARED_DATA.XState.EnabledSupervisorFeatures
        || (_DWORD)KiIptMsrMask )
      {
        RegistrationInfo[1] = 6;
        v29 = off_140008CD8;
        v28 = 512i64;
        RegistrationInfo[0] = 65548;
        v30 = 0i64;
        v31 = 0i64;
        if( ExRegisterHost(&KiSupervisorStateExtensionHost, v12, RegistrationInfo) < 0 )
          KiSupervisorStateExtensionHost = 0i64;
      }
      if( KiInitMachineDependent() )
      {
        MHz = KiProcessorBlock->MHz;
        v14 = MHz * (unsigned __int64)(unsigned int)KeMaximumIncrement / 0xA;
        KiShortExecutionCycles = v14 / 0xF0;
        KiCyclesPerClockQuantum = v14 / 3;
        KiDirectQuantumTarget = v14 / 3;
        KiLockQuantumTarget = 3 * (v14 / 3);
        _BitScanReverse64(&v14, 6000000 * MHz);
        KiFavoredCoreCycleTimeBits = v14;
        KdEncodeDataBlock();
        v15 = KeExpandKernelStackAndCalloutInternal(KiVerifyScopesExecute, 0i64, 0xC000ui64, 2, 0i64);
        if( v15 < 0 )
          KeBugCheckEx(0x32u, (PVOID)v15, (PVOID)0x5356694B, 0i64, 0i64);
        return 1;
      }
    }
  }
  return 0;
}

Referenced by:

InitBootProcessor
Phase1InitializationIoReady