MiAddPagesToEnclave

NTSTATUS __stdcall MiAddPagesToEnclave(
        INT64 rcx0,
        UINT64 rdx0,
        _MI_PARTITION *Partition,
        MMPTE *StartingPte,
        MMPTE *EndingPte,
        UINT64 ProtectionMask){
  _MI_PARTITION *v6; 
  int v7; 
  NTSTATUS result; 
  INT64 v11; 
  INT64 v12; 
  _MI_PARTITION *v13; 
  UINT64 v14; 
  _QWORD *v15; 
  INT64 v16; 
  int v17; 
  __int64 v18; 
  __int64 v19; 
  UINT64 v20; 
  int v21; 
  unsigned __int8 v22; 
  unsigned __int8 v23; 
  unsigned __int8 v24; 
  int v25; 
  unsigned __int8 v26; 
  __int64 v27; 
  char *v28; 
  _MI_PARTITION *v29; 
  unsigned __int64 v30; 
  INT64 v31; 
  INT64 v32; 
  int v33; 
  INT64 v34; 
  INT64 v35; 
  INT64 ValidPte; 
  UINT64 PageType; 
  UINT64 *ReturnedError; 
  CHAR v39; 
  unsigned __int8 v40; 
  UINT64 v41; 
  PVOID EnclaveAddress; 
  UINT64 PteFromCopyList; 
  __int64 v44; 
  __int64 v45; 
  ULONG_PTR BugCheckParameter2; 
  UINT64 v47; 
  __int128 v48; 
  _MI_PARTITION *v49; 
  PVOID EnclaveControlAddress; 
  UINT8 v51; 
  UINT64 v52; 
  v52 = rdx0;
  v48 = 0i64;
  v47 = 0i64;
  v6 = Partition;
  v7 = *(_DWORD *)(rcx0 + 64);
  LODWORD(v41) = 0;
  if( (v7 & 2) != 0 && (*(_DWORD *)(rcx0 + 72) & 1) == 0 )
    return -1073741800;
  v11 = 0i64;
  v12 = *((_QWORD *)KeGetCurrentThread() + 23) + 1664i64;
  v13 = *(_MI_PARTITION **)(qword_140C4E388 + 8i64 * *(unsigned __int16 *)(v12 + 174));
  v14 = *(_QWORD *)(rcx0 + 104);
  v49 = v13;
  if( v14 >= rdx0 || (v11 = rdx0 - v14, result = MiReserveEnclavePages(rcx0, (UINT64 *)v13, rdx0 - v14), result >= 0) )
  {
    if( (*(_DWORD *)(rcx0 + 64) & 2) != 0 )
    {
      v19 = 0i64;
      v45 = 0i64;
      v16 = 0i64;
      v18 = (_QWORD)MmGetPteBase() << 25;
    }
    else
    {
      v15 = (_QWORD *)MiReservePtes((INT64)&qword_140C4EC80, 1ui64);
      v16 = (INT64)v15;
      if( !v15 )
      {
LABEL_8:
        v17 = -1073741670;
        goto LABEL_49;
      }
      *v15 = MiMakeValidPte((UINT64)v15, qword_140C4EAC0, 536870913i64);
      v6 = Partition;
      v18 = (_QWORD)MmGetPteBase() << 25;
      v19 = ((v16 << 25) - v18) >> 16;
      v45 = v19;
    }
    v20 = 20i64;
    v44 = v18;
    if( rdx0 <= 0x14 )
      v20 = rdx0;
    MiCreatePteCopyList(v20 + 1, v20 + 1, (__int64)&v47);
    if( !HIDWORD(v47) )
      goto LABEL_8;
    v21 = 4;
    v22 = ((unsigned int)EndingPte >> 1) & 2 | 0x35;
    if( ((unsigned __int8)EndingPte & 2) == 0 )
      v22 = ((unsigned int)EndingPte >> 1) & 2 | 0x31;
    v23 = v22;
    v24 = v22;
    v25 = *(_DWORD *)(rcx0 + 64) & 2;
    if( !v25 )
      v21 = (int)EndingPte;
    v26 = v24 | 0x40;
    HIDWORD(v41) = v21;
    if( !v25 )
      v26 = v23;
    v40 = v26;
    EnclaveControlAddress = (PVOID)(((*(_QWORD *)(rcx0 + 80) << 25) - v18) >> 16);
    v27 = (_QWORD)v6 << 25;
    v28 = 0i64;
    EnclaveAddress = (PVOID)((v27 - v18) >> 16);
    v51 = MiLockWorkingSetShared(v12);
    v29 = Partition;
    v30 = (unsigned __int64)Partition;
    if( Partition > (_MI_PARTITION *)StartingPte )
    {
LABEL_48:
      MiUnlockWorkingSetShared(v12, v51);
      v17 = 0;
      goto LABEL_49;
    }
    while( 1 )
    {
      if( (v30 & 0x78) == 0 && (_MI_PARTITION *)v30 != v29 )
      {
        if( MiWorkingSetIsContended(v12) || KeShouldYieldProcessor() )
          goto LABEL_27;
        if( !v28 )
          goto LABEL_33;
        MiPageTableLockIsContended(v12, (UINT64)v28, v31, v32, PageType, (INT64)ReturnedError, v39);
        if( v33 )
        {
LABEL_27:
          if( v28 )
          {
            MiUnlockPageTableInternal(v12, (UINT64)v28);
            v28 = 0i64;
          }
          MiUnlockWorkingSetShared(v12, v51);
          MiLockWorkingSetShared(v12);
        }
      }
      if( v28 )
      {
        if( (v30 & 0xFFF) != 0 )
          goto LABEL_34;
        MiUnlockPageTableInternal(v12, (UINT64)v28);
      }
LABEL_33:
      v28 = (char *)MmGetPteBase() + ((v30 >> 9) & 0x7FFFFFFFF8i64);
      MiMakeSystemAddressValid(v30, 0i64, (*(_DWORD *)(rcx0 + 48) >> 12) & 0x3F, v51, 0);
LABEL_34:
      if( (MI_READ_PTE_LOCK_FREE(v30) & 1) == 0 )
      {
        v34 = v11 - 1;
        if( v11 != v52 )
          v34 = v11;
        --v52;
        v11 = v34;
        BugCheckParameter2 = MiGetPageForEnclave(rcx0, v49);
        PteFromCopyList = (UINT64)MiGetPteFromCopyList(&v47, BugCheckParameter2, -1i64);
        LODWORD(PageType) = v40;
        v17 = KeAddEnclavePage(
                EnclaveControlAddress,
                (PVOID)v19,
                (PVOID)((__int64)((PteFromCopyList << 25) - v18) >> 16),
                EnclaveAddress,
                PageType,
                &v41);
        v39 = 0;
        *(_QWORD *)PteFromCopyList = ZeroPte;
        if( v17 < 0 )
        {
          MiReturnEnclavePage(BugCheckParameter2);
          if( v17 == -1073740657 )
            v17 = -1073741823;
          if( v28 )
            MiUnlockPageTableInternal(v12, (UINT64)v28);
          MiUnlockWorkingSetShared(v12, v51);
LABEL_49:
          MiReleasePteCopyList((INT64)&v47);
          if( v16 )
            MiReleasePtes((INT64)&qword_140C4EC80, v16, 1ui64);
          if( v11 )
            MiReturnReservedEnclavePages(rcx0, v11);
          return v17;
        }
        MiInitializeEnclavePfn(BugCheckParameter2, (MMPTE *)v30, HIDWORD(v41));
        v35 = HIDWORD(v41);
        LODWORD(v35) = HIDWORD(v41) | 0x80000000;
        ValidPte = MiMakeValidPte(v30, BugCheckParameter2, v35);
        LODWORD(PageType) = 0;
        MiWriteEnclavePte((PVOID)v30, ValidPte);
        v18 = v44;
      }
      EnclaveAddress = (char *)EnclaveAddress + 4096;
      v30 += 8i64;
      if( v30 > (unsigned __int64)StartingPte )
      {
        if( v28 )
          MiUnlockPageTableInternal(v12, (UINT64)v28);
        goto LABEL_48;
      }
      v19 = v45;
      v29 = Partition;
    }
  }
  return result;
}

Referenced by:

MiCommitEnclavePages