MmMarkPhysicalMemoryAsBad

NTSTATUS __stdcall MmMarkPhysicalMemoryAsBad(_LARGE_INTEGER *StartAddress, _LARGE_INTEGER *a2){
  unsigned __int64 v3; 
  unsigned __int64 v4; 
  _ETHREAD *CurrentThread; 
  unsigned int v6; 
  NTSTATUS v7; 
  char *v8; 
  __int64 v9; 
  __int64 v10; 
  NTSTATUS v11; 
  unsigned __int8 v12; 
  unsigned __int64 v13; 
  unsigned __int64 v14; 
  __int64 v15; 
  CHAR v16; 
  unsigned __int64 v17; 
  unsigned __int64 v18; 
  struct _KPRCB *v19; 
  __int64 v20; 
  bool v21; 
  signed __int32 v22; 
  char v23; 
  unsigned __int64 v24; 
  struct _KPRCB *v25; 
  __int64 v26; 
  signed __int32 v27; 
  unsigned __int64 v28; 
  struct _KPRCB *v29; 
  __int64 v30; 
  signed __int32 v31; 
  int ContiguousPages; 
  unsigned __int64 v33; 
  UINT64 v34; 
  unsigned __int64 v35; 
  struct _KPRCB *CurrentPrcb; 
  __int64 v37; 
  signed __int32 v38; 
  UINT64 PreferredNode; 
  UINT64 PreferredChannel; 
  UINT64 Flags; 
  INT64 v42; 
  unsigned __int64 v43; 
  unsigned __int64 v44; 
  int v45; 
  if( (StartAddress->LowPart & 0xFFF) > 1 )
    return -1073741585;
  if( (a2->LowPart & 0xFFF) != 0 )
    return -1073741584;
  if( (MiFlags & 0x8000000) == 0 )
    return -1073741822;
  v3 = (unsigned __int64)StartAddress->QuadPart >> 12;
  v44 = (unsigned __int64)a2->QuadPart >> 12;
  v4 = v3 + v44;
  v43 = v3 + v44;
  if( v3 >= v3 + v44 )
    return -1073741585;
  CurrentThread = (_ETHREAD *)KeGetCurrentThread();
  v45 = 0;
  v42 = (INT64)CurrentThread;
  v6 = (((StartAddress->LowPart & 1) == 0) << 28) + 739246080;
  v7 = 0;
  MiLockDynamicMemoryShared((INT64)&MiSystemPartition, (INT64)CurrentThread);
  v8 = (char *)MmGetPfnDb() + 48 * v3;
  while( 1 )
  {
    if( MiIsPageInHugePfn(v3) )
    {
      MiUnlockDynamicMemoryShared((INT64)&MiSystemPartition, (INT64)CurrentThread);
      v7 = MiMarkHugePfnBad(v3, v6);
      MiLockDynamicMemoryShared((INT64)&MiSystemPartition, (INT64)CurrentThread);
      if( v7 == -1073740748 )
      {
        --v3;
        v8 -= 48;
      }
      goto LABEL_16;
    }
    if( v3 > 0xFFFFFFFFFi64 || (*((_QWORD *)MmGetPfnDb() + 6 * v3 + 5) & v10) == 0 )
    {
      v7 = -1073741584;
      goto LABEL_16;
    }
    if( (v9 & *((_QWORD *)v8 + 5)) != 0 )
    {
      v7 = MiMarkFileOnlyPfnBad((UINT64)v8, v6);
      if( v7 == -1073740748 )
      {
        --v3;
        v8 -= 48;
      }
      goto LABEL_16;
    }
    v12 = MiLockPageInline((INT64)v8);
    v13 = *((_QWORD *)v8 + 5);
    v14 = v12;
    if( (v13 & 0x2000000000000i64) == 0 )
      break;
    _InterlockedAnd64((volatile signed __int64 *)v8 + 3, 0x7FFFFFFFFFFFFFFFui64);
    __writecr8(v12);
    --v3;
    v8 -= 48;
LABEL_25:
    CurrentThread = (_ETHREAD *)v42;
LABEL_16:
    ++v3;
    v8 += 48;
    if( v3 >= v4 )
    {
      v11 = 0;
      goto LABEL_18;
    }
  }
  v15 = *(_QWORD *)(qword_140C4E388 + 8 * ((v13 >> 39) & 0x3FF));
  v16 = PsReferencePartitionSafe(*(_QWORD *)(v15 + 176));
  _InterlockedAnd64((volatile signed __int64 *)v8 + 3, 0x7FFFFFFFFFFFFFFFui64);
  if( !v16 )
  {
    __writecr8(v14);
    v7 = -1073741558;
    goto LABEL_25;
  }
  __writecr8(v14);
  if( (MiAcquireNonPagedResources((_MI_PARTITION *)v15, 2ui64) & 0x80000000) == 0i64 )
  {
    v17 = (unsigned __int8)MiLockPageInline((INT64)v8);
    if( *(_QWORD *)(qword_140C4E388 + 8 * ((*((_QWORD *)v8 + 5) >> 39) & 0x3FFi64)) == v15 )
    {
      if( (v6 & 0x10000000) != 0 )
      {
        v23 = v8[35];
        if( v23 >= 0 )
          v8[35] = v23 | 0x80;
      }
      if( (v8[35] & 0x40) == 0 )
      {
        if( (v8[34] & 7) != 5 || (*((_QWORD *)v8 + 5) & 0x2000000000000i64) == 0 && !MiIsPageOnBadList((INT64)v8) )
        {
          MiSetPfnRemovalRequested((INT64)v8, 1i64);
          _InterlockedAnd64((volatile signed __int64 *)v8 + 3, 0x7FFFFFFFFFFFFFFFui64);
          __writecr8(v17);
          MiUnlockDynamicMemoryShared((INT64)&MiSystemPartition, v42);
          LODWORD(Flags) = v6;
          LODWORD(PreferredChannel) = 0x80000000;
          LODWORD(PreferredNode) = 0x80000000;
          ContiguousPages = MiFindContiguousPages(
                              (_MI_PARTITION *)v15,
                              v3,
                              v3,
                              0i64,
                              1ui64,
                              MiCached,
                              PreferredNode,
                              PreferredChannel,
                              Flags,
                              0i64);
          v7 = 0;
          MiLockDynamicMemoryShared((INT64)&MiSystemPartition, v42);
          if( (*((_QWORD *)MmGetPfnDb() + 6 * v3 + 5) & 0x4000000000000i64) == 0 )
          {
            v7 = -1073741584;
LABEL_87:
            MiReturnCommit((_MI_PARTITION *)v15, 1ui64);
            v35 = 1i64;
            if( (ULONG_PTR *)v15 != &MiSystemPartition )
              goto LABEL_98;
            CurrentPrcb = KeGetCurrentPrcb();
            v37 = *((int *)CurrentPrcb + 8391);
            if( (_DWORD)v37 == -1 )
              goto LABEL_98;
            if( (unsigned __int64)(v37 + 1) <= 0x100 )
            {
              do
              {
                v38 = _InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, v37 + 1, v37);
                v21 = (_DWORD)v37 == v38;
                LODWORD(v37) = v38;
                if( v21 )
                  goto LABEL_99;
              }
              while( v38 != -1 && (unsigned __int64)(v38 + 1i64) <= 0x100 );
            }
            if( (int)v37 > 192
              && (_DWORD)v37 != -1
              && (_DWORD)v37 == _InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, 192, v37) )
            {
              v35 = (int)v37 - 192 + 1i64;
            }
            if( v35 )
LABEL_98:
              _InterlockedExchangeAdd64((volatile signed __int64 *)(v15 + 7168), v35);
LABEL_99:
            PsDereferencePartition(*(_QWORD *)(v15 + 176));
            if( ContiguousPages == -1073741523 || ContiguousPages == -1073741670 )
            {
              v7 = ContiguousPages;
              v11 = ContiguousPages;
              v4 = v43;
              goto LABEL_18;
            }
            v4 = v43;
            goto LABEL_25;
          }
          v33 = (unsigned __int8)MiLockPageInline((INT64)v8);
          if( ContiguousPages >= 0 )
          {
            if( (v8[35] & 0x40) == 0 )
            {
              _InterlockedAnd64((volatile signed __int64 *)v8 + 3, 0x7FFFFFFFFFFFFFFFui64);
              __writecr8(v33);
              MiFreeContiguousPages((_MI_PARTITION *)v3, 1ui64, v34);
              goto LABEL_99;
            }
            *((_WORD *)v8 + 16) = 0;
            *((_QWORD *)v8 + 5) &= 0xFFFFFFF000000000ui64;
            MiInsertPageInList((_MMPFN *)v8, 0x20ui64);
LABEL_86:
            _InterlockedAnd64((volatile signed __int64 *)v8 + 3, 0x7FFFFFFFFFFFFFFFui64);
            __writecr8(v33);
            goto LABEL_87;
          }
          if( !MiIsPageOnBadList((INT64)v8) )
          {
            if( (v8[35] & 0x40) == 0 )
              goto LABEL_86;
            byte_140C4E538 = 1;
            v7 = 259;
            v45 = 1;
          }
          ContiguousPages = 0;
          goto LABEL_86;
        }
        _InterlockedAnd64((volatile signed __int64 *)v8 + 3, 0x7FFFFFFFFFFFFFFFui64);
        __writecr8(v17);
        MiReturnCommit((_MI_PARTITION *)v15, 2ui64);
        v28 = 2i64;
        if( (ULONG_PTR *)v15 != &MiSystemPartition )
          goto LABEL_74;
        v29 = KeGetCurrentPrcb();
        v30 = *((int *)v29 + 8391);
        if( (_DWORD)v30 == -1 )
          goto LABEL_74;
        if( (unsigned __int64)(v30 + 2) <= 0x100 )
        {
          do
          {
            v31 = _InterlockedCompareExchange((volatile signed __int32 *)v29 + 8391, v30 + 2, v30);
            v21 = (_DWORD)v30 == v31;
            LODWORD(v30) = v31;
            if( v21 )
              goto LABEL_75;
          }
          while( v31 != -1 && (unsigned __int64)(v31 + 2i64) <= 0x100 );
        }
        if( (int)v30 > 192
          && (_DWORD)v30 != -1
          && (_DWORD)v30 == _InterlockedCompareExchange((volatile signed __int32 *)v29 + 8391, 192, v30) )
        {
          v28 = (int)v30 - 192 + 2i64;
        }
        if( v28 )
LABEL_74:
          _InterlockedExchangeAdd64((volatile signed __int64 *)(v15 + 7168), v28);
LABEL_75:
        PsDereferencePartition(*(_QWORD *)(v15 + 176));
        goto LABEL_25;
      }
      _InterlockedAnd64((volatile signed __int64 *)v8 + 3, 0x7FFFFFFFFFFFFFFFui64);
      __writecr8(v17);
      MiReturnCommit((_MI_PARTITION *)v15, 2ui64);
      v24 = 2i64;
      if( (ULONG_PTR *)v15 != &MiSystemPartition )
        goto LABEL_58;
      v25 = KeGetCurrentPrcb();
      v26 = *((int *)v25 + 8391);
      if( (_DWORD)v26 == -1 )
        goto LABEL_58;
      if( (unsigned __int64)(v26 + 2) <= 0x100 )
      {
        do
        {
          v27 = _InterlockedCompareExchange((volatile signed __int32 *)v25 + 8391, v26 + 2, v26);
          v21 = (_DWORD)v26 == v27;
          LODWORD(v26) = v27;
          if( v21 )
            goto LABEL_59;
        }
        while( v27 != -1 && (unsigned __int64)(v27 + 2i64) <= 0x100 );
      }
      if( (int)v26 > 192
        && (_DWORD)v26 != -1
        && (_DWORD)v26 == _InterlockedCompareExchange((volatile signed __int32 *)v25 + 8391, 192, v26) )
      {
        v24 = (int)v26 - 192 + 2i64;
      }
      if( v24 )
LABEL_58:
        _InterlockedExchangeAdd64((volatile signed __int64 *)(v15 + 7168), v24);
LABEL_59:
      PsDereferencePartition(*(_QWORD *)(v15 + 176));
      v7 = 259;
      goto LABEL_25;
    }
    _InterlockedAnd64((volatile signed __int64 *)v8 + 3, 0x7FFFFFFFFFFFFFFFui64);
    __writecr8(v17);
    MiReturnCommit((_MI_PARTITION *)v15, 2ui64);
    v18 = 2i64;
    if( (ULONG_PTR *)v15 != &MiSystemPartition )
      goto LABEL_41;
    v19 = KeGetCurrentPrcb();
    v20 = *((int *)v19 + 8391);
    if( (_DWORD)v20 == -1 )
      goto LABEL_41;
    if( (unsigned __int64)(v20 + 2) <= 0x100 )
    {
      do
      {
        v22 = _InterlockedCompareExchange((volatile signed __int32 *)v19 + 8391, v20 + 2, v20);
        v21 = (_DWORD)v20 == v22;
        LODWORD(v20) = v22;
        if( v21 )
          goto LABEL_42;
      }
      while( v22 != -1 && (unsigned __int64)(v22 + 2i64) <= 0x100 );
    }
    if( (int)v20 > 192
      && (_DWORD)v20 != -1
      && (_DWORD)v20 == _InterlockedCompareExchange((volatile signed __int32 *)v19 + 8391, 192, v20) )
    {
      v18 = (int)v20 - 192 + 2i64;
    }
    if( v18 )
LABEL_41:
      _InterlockedExchangeAdd64((volatile signed __int64 *)(v15 + 7168), v18);
LABEL_42:
    PsDereferencePartition(*(_QWORD *)(v15 + 176));
    --v3;
    v8 -= 48;
    goto LABEL_25;
  }
  v7 = -1073741670;
  v11 = -1073741670;
  PsDereferencePartition(*(_QWORD *)(v15 + 176));
LABEL_18:
  MiUnlockDynamicMemoryShared((INT64)&MiSystemPartition, v42);
  if( v45 == 1 )
    ZwUpdateWnfStateData();
  a2->QuadPart = (v44 + v3 - v4) << 12;
  if( v44 == 1 )
    return v7;
  return v11;
}

Referenced by:

MiScrubNodeLargePageList
WheapAttemptPhysicalPageOffline