MiInitSystem

UINT8 __stdcall MiInitSystem(UINT64 Phase, _LOADER_PARAMETER_BLOCK *LoaderBlock){
  VOID *v3; 
  _MMSUPPORT_INSTANCE *AnyMultiplexedVm; 
  UINT64 v5; 
  unsigned int v6; 
  INT64 v7; 
  _MMPTE *v8; 
  INT64 CurrentProcess; 
  int v11; 
  _MI_PARTITION *v12; 
  int v13; 
  __int64 *i; 
  void *ThreadHandle; 
  __int64 v16[4]; 

  ThreadHandle = 0i64;
  v16[0] = (__int64)KeBalanceSetManager;
  v16[1] = (__int64)KeSwapProcessOrStack;
  v16[2] = (__int64)MiRebuildLargePagesThread;
  v16[3] = (__int64)MiZeroPageThread;
  if( (_DWORD)Phase )
  {
    if( (_DWORD)Phase == 1 )
    {
      CurrentProcess = (INT64)PsGetCurrentProcess();
      MiSetPageTablePfnBuddy((INT64)&MmGetPfnDb()[*(_QWORD *)(CurrentProcess + 40) >> 12], CurrentProcess, 0i64);
      _InterlockedOr((volatile signed __int32 *)(CurrentProcess + 1124), 0x400u);
      _InterlockedOr((volatile signed __int32 *)(CurrentProcess + 1124), 0x800u);
      KUSER_SHARED_DATA.LargePageMinimum = 0x200000;
      MiHotAddBootDeferredDescriptors();
      if( (MiFlags & 0x8000) == 0 || (VslInitializeSecurePool(), v11 >= 0) )
      {
        MiMapBBTMemory(LoaderBlock);
        if( MiSectionInitialization() )
        {
          if( MiInitializeCfg() >= 0 )
          {
            if( MiCreateEnclaveRegions(LoaderBlock) )
            {
              *(&stru_140C4DB30 + 235) = 0i64;
              *(&stru_140C4DB30 + 547) = (char *)&stru_140C4DB30 + 4368;
              *(&stru_140C4DB30 + 546) = (char *)&stru_140C4DB30 + 4368;
              MiInitializeSessionIds();
              if( MiInitializePartitions(1ui64) )
              {
                MiInitializeCacheFlushing();
                MiComputeOptimalZeroPath();
                if( MiInitializeMemoryEvents(&Irp) )
                {
                  if( MiStoreChargeReservedPages(v12) )
                  {
                    MiInitializeModifiedWriterParameters();
                    *(&stru_140C50B28 + 792) = MiCreateZeroThreadContext(0i64, 0);
                    if( *(&stru_140C50B28 + 792) )
                    {
                      v13 = 0;
                      for( i = v16;
                            PsCreateSystemThread(&ThreadHandle, 0x1FFFFFu, 0i64, 0, 0i64, (PKSTART_ROUTINE)*i, &Irp) >= 0;
                            ++i )
                      {
                        ObCloseHandle(ThreadHandle, 0);
                        if( (unsigned int)++v13 >= 4 )
                        {
                          if( !MiInitializeMirroring() )
                            return 0;
                          Lock._bf_0 = 0i64;
                          MiWriteProtectSystemImages();
                          _InterlockedDecrement((_DWORD *)&stru_140C4DB30 + 1046);
                          return MiInitializeApiSets(LoaderBlock) >= 0;
                        }
                      }
                    }
                  }
                }
              }
            }
            else
            {
              *(&stru_140C4DB30 + 2570) = 32;
            }
          }
        }
      }
    }
    else if( (_DWORD)Phase == 2 )
    {
      MiUnlockBootPageSections();
      TlgRegisterAggregateProviderEx(&unk_140C04698);
      *(&stru_140C4DB30 + 542) = &unk_140C04698;
      MiFlushStrongCodeDriverLoadFailures();
      MiFlushDeferredRetpolineImageLoadEvents();
      return 1;
    }
  }
  else
  {
    MmTrackLockedPages &= 1u;
    MiInitializeCrashDumpPtes();
    _InterlockedIncrement((_DWORD *)&stru_140C4DB30 + 1046);
    if( MiInitializeWorkingSetManagerParameters(&Irp) )
    {
      if( ExInitializePagedHeaps() >= 0 )
      {
        AnyMultiplexedVm = MiGetAnyMultiplexedVm(MiWorkingSetTypeSystemViews, v3);
        if( MiInitializeSystemWorkingSetList(
               (ULONG_PTR *)&Irp.Core.PartitionId,
               AnyMultiplexedVm,
               WorkingSetTypeSystemViews,
               0i64) )
        {
          if( PsInitializeQuotaSystem(0i64) )
          {
            MiInitializeVadSecuring();
            *(&stru_140C4DB30 + 10) = 305419896;
            if( MiInitializeSharedUserData() )
            {
              if( MiInitializeBootProcess(v5) >= 0 )
              {
                KeFlushCurrentTbOnly(FlushEntire);
                MiAddLoaderHalIoMappings(MiLowHalVa, (VOID *)0xFFFFFFFFFFFFFFFFi64);
                MiAddLoaderHalIoMappings(*(&stru_140C4DB30 + 947), (VOID *)(*(&stru_140C4DB30 + 947) + 0x7FFFFFFFFFi64));
                KdAllocateDynamicMemory();
                MiSetSlabAllocatorPolicy((INT64)&Irp);
                MiInitializeHotPatches((INT64)LoaderBlock);
                if( MiInitializeRetpoline() >= 0 )
                {
                  qword_140D57868 = KeQueryPerformanceCounter(0i64);
                  if( (unsigned int)dword_140CFA1D8 > 2 )
                    dword_140CFA1D8 = 0;
                  if( ((MiFlags & 4) == 0 || MiCreateSlabAllocationsFromKernelHal() >= 0)
                    && MiCreateSlabAllocationsFromLoaderBlock((INT64)LoaderBlock) >= 0 )
                  {
                    qword_140D57870 = KeQueryPerformanceCounter(0i64);
                    if( MiInitializeDriverImages(LoaderBlock) )
                    {
                      qword_140D57878 = KeQueryPerformanceCounter(0i64);
                      v6 = -1;
                      if( *(&stru_140C50B28 + 853) <= 0xFFFFFFFFui64 )
                        v6 = *(&stru_140C50B28 + 1706);
                      KUSER_SHARED_DATA.NumberOfPhysicalPages = v6;
                      KUSER_SHARED_DATA.LargePageMinimum = 0;
                      MiInitializeSystemSpaceMap(&Session);
                      RootPointer.Root = 0i64;
                      *(&stru_140C4DB30 + 239) = 0i64;
                      *(&stru_140C4DB30 + 241) = 0i64;
                      qword_140D57880 = KeQueryPerformanceCounter(0i64);
                      if( MiInitializeLoadedModuleList(LoaderBlock) )
                      {
                        if( MiInitializeKernelCfg() >= 0 )
                        {
                          RtlInitializeHistoryTable();
                          MiFlags |= 0x1000000u;
                          *(&stru_140C50B28 + 117) = 0i64;
                          MmConfigurePrefetchSeekThreshold(0x20ui64);
                          PsSetPagePriorityThread((_ETHREAD *)KeGetCurrentThread(), 5ui64);
                          MiEnablePagingTheExecutive();
                          ExPageLockHandle = MmLockPagableDataSection(MmShutdownSystem);
                          MmUnlockPagableImageSection(ExPageLockHandle);
                          *(&stru_140C4DB30 + 496) = MiAllocateDummyPage(v7);
                          *(&stru_140C4DB30 + 497) = (*(&stru_140C4DB30 + 496) - (_QWORD)MmGetPfnDb()) / 48i64;
                          v8 = MiReservePtes((_MI_SYSTEM_PTE_TYPE *)((char *)&stru_140C4DB30 + 4432), 1ui64);
                          MmBadPointer = v8;
                          if( v8 )
                          {
                            MmBadPointer = (PVOID)((__int64)(((_QWORD)v8 << 25) - ((_QWORD)MmGetPteBase() << 25)) >> 16);
                            if( MiInitializeRelocations() )
                            {
                              qword_140D57888 = KeQueryPerformanceCounter(0i64);
                              MiInitializeTbFlushing();
                              qword_140D57890 = KeQueryPerformanceCounter(0i64);
                              return 1;
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  return 0;
}

Referenced by:

MmInitSystem