MiSetReadOnlyOnSectionView

INT64 __fastcall MiSetReadOnlyOnSectionView(INT64 a1, INT64 a2, UINT64 a3, UINT64 a4){
  INT64 v4; 
  UINT64 v6; 
  unsigned __int64 v7; 
  __int64 v8; 
  unsigned int v10; 
  unsigned int updated; 
  unsigned int v12; 
  UINT64 v13; 
  _MMPTE *PteBase; 
  UINT64 v15; 
  unsigned __int64 v16; 
  UINT8 v17; 
  UINT64 v18; 
  UINT64 v19; 
  BOOL v20; 
  WCHAR *v21; 
  __int64 v22; 
  UNICODE_STRING *v23; 
  int v24; 
  _MMPTE *v25; 
  unsigned __int64 v26; 
  unsigned __int64 v27; 
  int WsleProtection; 
  UINT64 v29; 
  UINT64 v30; 
  int v31; 
  int v32; 
  UINT64 v33; 
  UINT64 v34; 
  UINT8 v35; 
  INT64 v36; 
  int v37; 
  UINT64 v38; 
  UINT64 v39; 
  INT64 v40; 
  INT64 v41; 
  __int64 v42; 
  unsigned __int64 v43; 
  unsigned __int64 v44; 
  _SUBSECTION *ContainingSubsection; 
  MMPTE *PointerPte; 
  UINT64 v47; 
  int v48; 
  __int16 v49; 
  __int16 v50; 
  __int64 v51; 
  UINT64 v52; 
  UINT64 v53; 
  __int128 v54; 
  __int128 v55; 
  __int128 v56; 
  __int128 v57; 
  __int128 v58; 
  __int128 v59; 
  __int128 v60; 
  __int128 v61; 
  __int128 v62; 
  __int64 v63; 
  v41 = a2;
  v47 = a3;
  v4 = a2;
  v50 = 0;
  v63 = 0i64;
  ContainingSubsection = 0i64;
  v54 = 0i64;
  v55 = 0i64;
  v56 = 0i64;
  v57 = 0i64;
  v58 = 0i64;
  v59 = 0i64;
  v60 = 0i64;
  v61 = 0i64;
  v62 = 0i64;
  if( (unsigned int)MiVadPureReserve(a2) )
    return 3221225541i64;
  v10 = v6;
  updated = 1;
  v12 = 4;
  HIDWORD(v36) = v6;
  v13 = v6;
  v49 = v6;
  v52 = v6;
  v53 = v6;
  v39 = v6;
  v37 = 4;
  LODWORD(v36) = 1;
  v51 = 20i64;
  v48 = 1;
  PteBase = MmGetPteBase();
  v44 = (unsigned __int64)PteBase + ((v7 >> 9) & 0x7FFFFFFFF8i64);
  v15 = v44;
  v16 = (unsigned __int64)PteBase + ((a4 >> 9) & 0x7FFFFFFFF8i64);
  v43 = v16;
  v40 = v8 + 1664;
  v17 = MiLockWorkingSetShared(v8 + 1664);
  v35 = v17;
  if( v44 <= v16 )
  {
    do
    {
      if( (v15 & 0xFFF) != 0 )
      {
        if( v13 )
          goto LABEL_10;
      }
      else if( v13 )
      {
        MiFlushTbList((__int64)&v48);
        MiUnlockPageTableInternal(v40, v13);
        v17 = v35;
      }
      MiMakeSystemAddressValid(v15, (INT64)&v48, (*(_DWORD *)(v4 + 48) >> 12) & 0x3F, v17, 0);
      v39 = (UINT64)MmGetPteBase() + ((v15 >> 9) & 0x7FFFFFFFF8i64);
LABEL_10:
      v18 = *(_QWORD *)v15;
      v20 = MiPteInShadowRange(v15);
      v38 = v18;
      if( !v18 )
        goto LABEL_49;
      if( (v18 & 1) != 0 )
      {
        v22 = (v18 >> 12) & 0xFFFFFFFFFi64;
        v23 = (UNICODE_STRING *)((char *)MmGetPfnDb() + 48 * v22);
        if( v23[2].Length > 1u && (updated & 7) != 4 )
          goto LABEL_49;
        LOBYTE(v24) = MI_PFN_IS_PROTO(v23, (UNICODE_STRING *)v19, v20, v21);
        if( v24 )
        {
          v25 = MmGetPteBase();
          v26 = (__int64)((v15 << 25) - ((_QWORD)v25 << 25)) >> 16;
          if( (_MMPTE *)((unsigned __int64)v23->Buffer | 0x8000000000000000ui64) != MiGetProtoPteAddress(
                                                                                       (_MMVAD *)v4,
                                                                                       v26 >> 12,
                                                                                       0i64,
                                                                                       &ContainingSubsection) )
            goto LABEL_49;
          PointerPte = (_MMPTE *)((char *)v25 + ((v26 >> 9) & 0x7FFFFFFFF8i64));
          LODWORD(v42) = ((v26 >> 9) & 0xFFFFFFF8) + (_DWORD)v25;
          v27 = *(_QWORD *)PointerPte;
          MiPteInShadowRange((UINT64)PointerPte);
          WsleProtection = MiGetWsleProtection(v26, HIBYTE(v27) & 0xF | (16 * ((v27 >> 60) & 7)));
          v12 = v37;
          if( WsleProtection != v37
            && (WsleProtection || (((unsigned __int64)MI_READ_PTE_LOCK_FREE((INT64)&v23[1]) >> 5) & 0x1F) != v37) )
          {
            v4 = v41;
            goto LABEL_49;
          }
          updated = v36;
          MiWriteValidPteVolatile(PointerPte, 0x80000000ui64);
          v4 = v41;
        }
        else
        {
          if( ((*(_DWORD *)&v23[1].Length >> 5) & 0x1F) != (unsigned __int64)v12 )
            goto LABEL_49;
          updated = MiUpdatePfnProtection(v4, (INT64)v23, updated);
          LODWORD(v36) = updated;
        }
        MiRevertValidPte(v4, v15, updated, v22, (INT64)&v48);
        v16 = v43;
        v15 += 8i64;
        goto LABEL_35;
      }
      if( (v18 & 0x400) != 0 )
      {
        if( (updated & 7) != 4 )
          goto LABEL_49;
        if( !MiIsPrototypePteVadLookup(v18) )
        {
          v29 = MiSwizzleInvalidPte(32 * (updated & 0x1F | 0xFFFFFFFFF8000020ui64));
          v38 = v29;
          if( v31 )
          {
            if( (*(_DWORD *)(*((_QWORD *)KeGetCurrentThread() + 23) + 2172i64) & 0x1000) == 0 )
            {
              *(_QWORD *)v15 = v30;
              goto LABEL_49;
            }
            v29 = v30;
            if( (v30 & 1) != 0 )
              v29 = v30 | 0x8000000000000000ui64;
          }
          *(_QWORD *)v15 = v29;
LABEL_49:
          v34 = v39;
          if( (_MMPTE *)v39 != (_MMPTE *)((char *)MmGetPdeBase() + ((v47 >> 18) & 0x3FFFFFF8)) )
          {
            MiFlushTbList((__int64)&v48);
            if( v34 )
            {
              MiUnlockPageTableInternal(v40, v34);
              v39 = 0i64;
            }
          }
          v16 = v15 - 8;
          v12 = 1;
          v15 = v44;
          updated = 4;
          v10 = -1073741755;
          v43 = v16;
          v37 = 1;
          v36 = 0xC000004500000004ui64;
          goto LABEL_36;
        }
      }
      else
      {
        if( (v18 & 0x800) != 0 )
        {
          v32 = MiSetProtectionOnTransitionPte(v4, (UINT64 *)v15, updated, (updated & 7) == 4);
          if( !v32 )
            goto LABEL_47;
          if( v32 != 2 )
            goto LABEL_49;
          goto LABEL_35;
        }
        if( ((v18 >> 5) & 0x1F) != v12 )
          goto LABEL_49;
      }
      v38 = v18 ^ ((unsigned __int16)v18 ^ (unsigned __int16)(32 * updated)) & 0x3E0;
      v33 = v38;
      if( !v20 )
        goto LABEL_46;
      if( (*(_DWORD *)(*((_QWORD *)KeGetCurrentThread() + 23) + 2172i64) & 0x1000) != 0 )
      {
        v33 = v19;
        if( (v19 & 1) != 0 )
          v33 = v19 | 0x8000000000000000ui64;
LABEL_46:
        *(_QWORD *)v15 = v33;
LABEL_47:
        v15 += 8i64;
        goto LABEL_35;
      }
      *(_QWORD *)v15 = v19;
      v15 += 8i64;
LABEL_35:
      v10 = HIDWORD(v36);
LABEL_36:
      v13 = v39;
      v17 = v35;
    }
    while( v15 <= v16 );
  }
  MiFlushTbList((__int64)&v48);
  if( v13 )
    MiUnlockPageTableInternal(v40, v13);
  MiUnlockWorkingSetShared(v40, v35);
  return v10;
}

Referenced by:

MmSecureVirtualMemoryAgainstWrites