MiUnlinkBadPages

NTSTATUS __stdcall MiUnlinkBadPages(PVOID BugCheckParameter2){
  unsigned __int64 v1; 
  NTSTATUS v2; 
  char *v3; 
  char *v4; 
  unsigned __int8 CurrentIrql; 
  volatile signed __int32 *v6; 
  _QWORD *v7; 
  int v8; 
  __int64 v9; 
  __int64 v10; 
  unsigned __int64 v11; 
  struct _KPRCB *CurrentPrcb; 
  __int64 v13; 
  bool v14; 
  signed __int32 v15; 
  _RTL_ATOM_TABLE *v16; 
  char v17; 
  signed __int32 v18; 
  UINT64 SpinCount; 
  unsigned __int64 v21; 
  __int64 v22; 
  v21 = v1;
  v2 = 0;
  v22 = 0i64;
  v3 = (char *)BugCheckParameter2;
  v4 = (char *)MmGetPfnDb() + 48 * (_QWORD)BugCheckParameter2;
  CurrentIrql = KeGetCurrentIrql();
  __writecr8(2ui64);
  if( (unsigned __int64)BugCheckParameter2 < v1 )
  {
    v6 = (volatile signed __int32 *)(v4 + 24);
    v7 = (_QWORD *)((char *)MmGetPfnDb() + 48 * (_QWORD)BugCheckParameter2 + 40);
    do
    {
      if( MiIsPageInHugePfn((UINT64)v3) )
      {
        __writecr8(CurrentIrql);
        v8 = MiMarkHugePfnGood((UINT64)v3);
        CurrentIrql = KeGetCurrentIrql();
        __writecr8(2ui64);
        v9 = v22;
        if( v8 < 0 )
        {
          --v3;
          v7 -= 6;
          v4 -= 48;
          v6 -= 12;
          v9 = v22 - 1;
        }
        goto LABEL_41;
      }
      if( (unsigned __int64)v3 > 0xFFFFFFFFFi64 || (*v7 & 0x4000000000000i64) == 0 )
        goto LABEL_40;
      LODWORD(SpinCount) = 0;
      while( _interlockedbittestandset64(v6, 0x3Fui64) )
      {
        do
          KeYieldProcessorEx(&SpinCount);
        while( *(__int64 *)v6 < 0 );
      }
      v10 = *(_QWORD *)(qword_140C4E388 + 8 * ((*((_QWORD *)v4 + 5) >> 39) & 0x3FFi64));
      if( (v4[35] & 0x40) != 0 )
      {
        MiSetPfnRemovalRequested((INT64)v4, 0i64);
        v4[35] &= ~0x80u;
        if( (*((_QWORD *)v4 + 5) & 0x2000000000000i64) != 0 )
        {
          if( (v4[35] & 0x10) != 0 )
            goto LABEL_36;
          goto LABEL_35;
        }
        MiReturnCommit((_MI_PARTITION *)v10, 1ui64);
        v11 = 1i64;
        if( (ULONG_PTR *)v10 == &MiSystemPartition )
        {
          CurrentPrcb = KeGetCurrentPrcb();
          v13 = *((int *)CurrentPrcb + 8391);
          if( (_DWORD)v13 != -1 )
          {
            if( (unsigned __int64)(v13 + 1) <= 0x100 )
            {
              do
              {
                v15 = _InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, v13 + 1, v13);
                v14 = (_DWORD)v13 == v15;
                LODWORD(v13) = v15;
                if( v14 )
                  goto LABEL_35;
              }
              while( v15 != -1 && (unsigned __int64)(v15 + 1i64) <= 0x100 );
            }
LABEL_29:
            if( (int)v13 > 192
              && (_DWORD)v13 != -1
              && (_DWORD)v13 == _InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, 192, v13) )
            {
              v11 = (int)v13 - 192 + 1i64;
            }
            if( !v11 )
              goto LABEL_35;
          }
        }
      }
      else
      {
        if( !MiIsPageOnBadList((INT64)v4) )
          goto LABEL_36;
        v4[35] = v17 & 0x7F;
        MiUnlinkPageFromBadList(v16);
        *(_QWORD *)v6 |= 0x4000000000000000ui64;
        MiInsertPageInFreeOrZeroedList((UINT64)v3, 2ui64);
        MiReturnCommit((_MI_PARTITION *)v10, 1ui64);
        v11 = 1i64;
        if( (ULONG_PTR *)v10 == &MiSystemPartition )
        {
          CurrentPrcb = KeGetCurrentPrcb();
          v13 = *((int *)CurrentPrcb + 8391);
          if( (_DWORD)v13 != -1 )
          {
            if( (unsigned __int64)(v13 + 1) <= 0x100 )
            {
              while( 1 )
              {
                v18 = _InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, v13 + 1, v13);
                v14 = (_DWORD)v13 == v18;
                LODWORD(v13) = v18;
                if( v14 )
                  goto LABEL_35;
                if( v18 == -1 || (unsigned __int64)(v18 + 1i64) > 0x100 )
                  goto LABEL_29;
              }
            }
            goto LABEL_29;
          }
        }
      }
      _InterlockedExchangeAdd64((volatile signed __int64 *)(v10 + 7168), v11);
LABEL_35:
      ++v2;
LABEL_36:
      _InterlockedAnd64((volatile signed __int64 *)v6, 0x7FFFFFFFFFFFFFFFui64);
      v9 = v22;
      if( CurrentIrql >= 2u || (v22 & 0xF) != 0 )
        goto LABEL_41;
      if( KeShouldYieldProcessor() )
      {
        __writecr8(CurrentIrql);
        CurrentIrql = KeGetCurrentIrql();
        __writecr8(2ui64);
      }
LABEL_40:
      v9 = v22;
LABEL_41:
      ++v3;
      v7 += 6;
      v22 = v9 + 1;
      v4 += 48;
      v6 += 12;
    }
    while( (unsigned __int64)v3 < v21 );
  }
  __writecr8(CurrentIrql);
  return v2;
}

Referenced by:

MmMarkPhysicalMemoryAsGood