KiChooseTargetProcessor

_KPRCB *__stdcall KiChooseTargetProcessor(
        _KPRCB *CurrentPrcb,
        _ETHREAD *Thread,
        _GROUP_AFFINITY *Affinity,
        UINT64 *Idle){
  CHAR v4; 
  unsigned __int64 Mask; 
  bool v6; 
  unsigned __int16 Group; 
  _KPRCB *v9; 
  __int64 v10; 
  INT64 v11; 
  INT64 v12; 
  __int64 v13; 
  _KNODE *v14; 
  INT64 v15; 
  _KNODE *v16; 
  unsigned __int64 IdleNonParkedCpuSet; 
  unsigned __int64 v18; 
  unsigned __int64 v19; 
  struct _KPRCB *v20; 
  char v21; 
  unsigned __int64 v23; 
  unsigned __int64 v24; 
  unsigned __int64 v25; 
  char v26; 
  unsigned __int64 v27; 
  int v28; 
  __int64 v29; 
  unsigned __int64 v30; 
  unsigned __int64 v31; 
  _KNODE *v32; 
  unsigned __int64 v33; 
  __int64 v34; 
  unsigned int v35; 
  UINT64 v36; 
  UINT8 *v37; 
  __int64 v38; 
  int v39; 
  char *v40; 
  char v41; 
  unsigned int v42; 
  char v43; 
  int v44; 
  __int64 v45; 
  __int64 v46; 
  struct _KPRCB *v47; 
  _QWORD *v48; 
  __int64 v49; 
  __int64 v50; 
  UINT64 *v51; 
  __int64 v52; 
  struct _KPRCB *v53; 
  __int64 v54; 
  unsigned int NextNode; 
  __int64 v56; 
  __int64 v57; 
  struct _KPRCB *v58; 
  __int64 v59; 
  CHAR RankBiasedIdleSmtSet; 
  bool v61; 
  unsigned int v62; 
  unsigned int v63; 
  char *v64; 
  __int64 v65; 
  int v66; 
  __int64 v67; 
  __int64 v68; 
  unsigned __int16 v69; 
  bool v70; 
  UINT64 AffinityMask; 
  unsigned __int64 a2; 
  _KNODE *PreferredNode; 
  UINT64 LookupIndex; 
  unsigned __int64 v76; 
  int v77; 
  UINT64 SpinCount; 
  __int128 v79; 
  __int64 a3; 
  __int64 a5; 
  UINT64 *v82; 
  UINT8 v83[64]; 
  CHAR v84; 
  v4 = v84;
  Mask = Affinity->Mask;
  v6 = 0;
  v82 = Idle;
  Group = Affinity->Group;
  v9 = CurrentPrcb;
  v69 = Group;
  LODWORD(LookupIndex) = 0;
  v79 = 0i64;
  AffinityMask = Mask;
  v70 = 0;
  while( 1 )
  {
    while( 1 )
    {
      while( 1 )
      {
        v10 = *((unsigned int *)Thread + 134);
        LODWORD(v10) = v10 & 0x7FFFFFFF;
        WORD4(v79) = Group;
        v11 = (INT64)*(&KiProcessorBlock + v10);
        if( Group == *((unsigned __int8 *)v9 + 208) )
        {
          v12 = KiTryLocalThreadSchedule((INT64)v9, v11, (INT64)Thread, Mask, v4, v6);
          if( v12 )
            goto LABEL_18;
          Mask = AffinityMask;
          v9 = CurrentPrcb;
        }
        v13 = *((unsigned int *)Thread + 147);
        v12 = 0i64;
        v14 = *(_KNODE **)(v11 + 192);
        PreferredNode = v14;
        v15 = (INT64)*(&KiProcessorBlock + v13);
        *(_QWORD *)&v79 = Mask;
        v16 = *(_KNODE **)(v15 + 192);
        if( v14 == v16 )
          v12 = v11;
        IdleNonParkedCpuSet = v16->IdleNonParkedCpuSet;
        v18 = Mask & v16->IdleNonParkedCpuSet;
        a2 = v18;
        v19 = v18;
        if( v70 )
        {
          if( v18 )
            goto LABEL_9;
          IdleNonParkedCpuSet = v16->IdleCpuSet & v16->SoftParkedSet;
          v18 = Mask & IdleNonParkedCpuSet;
          a2 = Mask & IdleNonParkedCpuSet;
          v19 = Mask & IdleNonParkedCpuSet;
        }
        if( !v19 )
          goto LABEL_34;
LABEL_9:
        if( (*(_BYTE *)&v16->Flags & 0x20) == 0 )
          goto LABEL_14;
        if( v4 )
        {
          RankBiasedIdleSmtSet = KiFindRankBiasedIdleSmtSet(v15, &a2);
          Mask = AffinityMask;
          v14 = PreferredNode;
          if( RankBiasedIdleSmtSet )
          {
            v18 = a2;
            goto LABEL_14;
          }
          v9 = CurrentPrcb;
        }
        v18 = a2;
        if( (a2 & v16->IdleSmtSet) != 0 )
        {
          if( v4
            && (unsigned int)((0x101010101010101i64
                             * ((((IdleNonParkedCpuSet - ((IdleNonParkedCpuSet >> 1) & 0x5555555555555555i64)) & 0x3333333333333333i64)
                               + (((IdleNonParkedCpuSet - ((IdleNonParkedCpuSet >> 1) & 0x5555555555555555i64)) >> 2) & 0x3333333333333333i64)
                               + ((((IdleNonParkedCpuSet - ((IdleNonParkedCpuSet >> 1) & 0x5555555555555555i64)) & 0x3333333333333333i64)
                                 + (((IdleNonParkedCpuSet - ((IdleNonParkedCpuSet >> 1) & 0x5555555555555555i64)) >> 2) & 0x3333333333333333i64)) >> 4)) & 0xF0F0F0F0F0F0F0Fi64)) >> 32) >> 24 < KiPerfIsoEnabled )
          {
            goto LABEL_34;
          }
          v18 = a2 & v16->IdleSmtSet;
          a2 = v18;
        }
        else
        {
          if( v4 )
            goto LABEL_34;
          if( *((_KNODE **)v9 + 24) == v16 )
          {
            KiReduceByEffectiveIdleSmtSet(v9, &a2);
            v18 = a2;
            Mask = AffinityMask;
            v14 = PreferredNode;
          }
        }
LABEL_14:
        if( (v18 & *(_QWORD *)(v15 + 200)) != 0 )
        {
          v12 = v15;
        }
        else if( !v12 || (v18 & *(_QWORD *)(v12 + 200)) == 0 )
        {
          v23 = v18 & *(_QWORD *)(v15 + 33880);
          if( v23 )
          {
            v18 &= *(_QWORD *)(v15 + 33880);
            a2 = v23;
          }
          else if( v12 && (v24 = v18 & *(_QWORD *)(v12 + 33880)) != 0 )
          {
            v18 &= *(_QWORD *)(v12 + 33880);
            a2 = v24;
          }
          else if( (KiCacheAwareScheduling & 1) != 0 )
          {
            v25 = v18 & *(_QWORD *)(v15 + 33896);
            if( v25 )
            {
              v18 &= *(_QWORD *)(v15 + 33896);
              a2 = v25;
            }
            else if( v12 )
            {
              if( (v18 & *(_QWORD *)(v12 + 33896)) != 0 )
                v18 &= *(_QWORD *)(v12 + 33896);
              a2 = v18;
            }
          }
          v26 = *(_BYTE *)(v15 + 209);
          _BitScanForward64(&v27, __ROR8__(v18, v26));
          v28 = ((_BYTE)v27 + v26) & 0x3F;
          v29 = v28 + (*(unsigned __int8 *)(v15 + 208) << 6);
          v77 = v28;
          v12 = (INT64)*(&KiProcessorBlock + (unsigned int)KiProcessorNumberToIndexMappingTable[v29]);
        }
        if( v12 )
          goto LABEL_17;
LABEL_34:
        v76 = v16->Affinity.Mask;
        v30 = v76;
        v31 = Mask & ~v76;
        *(_QWORD *)&v79 = v31;
        if( v14 == v16 || !v31 || v14->Affinity.Group != v69 )
          break;
        v12 = (INT64)KiSelectIdleProcessor(Mask, CurrentPrcb, PreferredNode, (_KPRCB *)v11);
        if( !v12 )
        {
          v30 = v76;
          Mask = AffinityMask;
          v14 = PreferredNode;
          break;
        }
LABEL_17:
        v6 = v70;
LABEL_18:
        v20 = KeGetCurrentPrcb();
        LODWORD(SpinCount) = 0;
        while( _interlockedbittestandset64((volatile signed __int32 *)(v12 + 48), 0i64) )
        {
          do
            KeYieldProcessorEx(&SpinCount);
          while( *(_QWORD *)(v12 + 48) );
          v52 = *((_QWORD *)v20 + 4247);
          if( v52 && *((_BYTE *)v20 + 32) <= 1u )
            ++*(_DWORD *)(v52 + 24);
        }
        v21 = *(_BYTE *)(v12 + 35);
        if( !v21 || v6 && v21 == 6 )
        {
          *(_DWORD *)v82 = 1;
          return(_KPRCB *)v12;
        }
        _InterlockedAnd64((volatile signed __int64 *)(v12 + 48), 0i64);
        v53 = KeGetCurrentPrcb();
        Mask = AffinityMask;
        v9 = CurrentPrcb;
        Group = v69;
        v54 = *((_QWORD *)v53 + 4247);
        if( v54 && *((_BYTE *)v53 + 32) <= 1u )
        {
          v61 = (*(_DWORD *)(v54 + 24))-- == 1;
LABEL_95:
          if( v61 )
          {
            v9 = CurrentPrcb;
            Group = v69;
            goto LABEL_97;
          }
        }
      }
      if( v69 == v14->Affinity.Group )
        v31 = Mask & ~(v30 | v14->Affinity.Mask);
      v9 = CurrentPrcb;
      *(_QWORD *)&v79 = v31;
      v32 = (_KNODE *)*((_QWORD *)CurrentPrcb + 24);
      v76 = (unsigned __int64)v32;
      if( v32 != v16 && v32 != v14 && v31 )
      {
        if( v69 != v32->Affinity.Group )
        {
LABEL_94:
          v33 = v31;
          goto LABEL_40;
        }
        v12 = (INT64)KiSelectIdleProcessor(Mask, CurrentPrcb, v32, CurrentPrcb);
        if( !v12 )
        {
          v32 = (_KNODE *)v76;
          v9 = CurrentPrcb;
          v14 = PreferredNode;
          goto LABEL_38;
        }
        goto LABEL_17;
      }
LABEL_38:
      if( v69 != v32->Affinity.Group )
        goto LABEL_94;
      v33 = v31 & ~v32->Affinity.Mask;
LABEL_40:
      v34 = (1i64 << LOBYTE(v16->NodeNumber)) | (1i64 << v32->Affinity.Reserved[0]);
      LODWORD(LookupIndex) = 0;
      v35 = v16->SiblingMask & ~(v34 | (1 << v14->Affinity.Reserved[0]));
LABEL_41:
      *(_QWORD *)&v79 = v33;
      while( v33 )
      {
        NextNode = MmGetNextNode(v16->Affinity.Reserved[0], &LookupIndex);
        if( NextNode == -1 )
          break;
        v56 = v35;
        if( _bittest64(&v56, NextNode) )
        {
          v76 = *((_QWORD *)&KeNodeBlock + NextNode);
          v12 = (INT64)KiSelectIdleProcessor(AffinityMask, v9, (_KNODE *)v76, 0i64);
          if( v12 )
          {
            v4 = v84;
            goto LABEL_17;
          }
          v9 = CurrentPrcb;
          v33 &= ~*(_QWORD *)(v76 + 136);
          goto LABEL_41;
        }
      }
      if( v70 )
        break;
      v6 = 0;
      a3 = 0i64;
      a5 = 0i64;
      HIDWORD(LookupIndex) = 0;
      v70 = 0;
      if( KeSoftParkedQueueThreshold )
      {
        KeQueryReadyQueueStatsProcessor(v15, (_DWORD *)&LookupIndex + 1, &a3, (_DWORD *)&LookupIndex + 1, &a5);
        v9 = CurrentPrcb;
        v6 = a3 + a5 > (unsigned __int64)(unsigned int)KeSoftParkedQueueThreshold;
        v70 = v6;
      }
      v4 = v84;
      Group = v69;
      if( !v6 )
        break;
LABEL_97:
      Mask = AffinityMask;
    }
    v36 = AffinityMask & *(_QWORD *)(v15 + 33856);
    if( ((v36 - 1) & v36) == 0 )
      goto LABEL_59;
    v37 = *(UINT8 **)(v15 + 33864);
    v38 = v37[593];
    v39 = v37[592];
    v40 = (char *)&v83[v38];
    v41 = v37[593];
    memmove(&v83[v38], v37 + 528, v37[592]);
    v42 = v38 + v39;
    v43 = *((_BYTE *)Thread + 195);
    v44 = -1;
    v45 = *(_QWORD *)(*(_QWORD *)(v15 + 192) + 360i64);
    if( (KiHeteroSchedulerOptions & 1) != 0 )
    {
      if( (KiHeteroSchedulerOptions & 2) == 0 )
        goto LABEL_51;
    }
    else if( (KiVelocityFlags & 0x800) == 0 || !KeHeteroSystemQos )
    {
      goto LABEL_51;
    }
    if( (v45 & v36) != 0 )
    {
      v62 = (unsigned __int8)*((_DWORD *)Thread + 128) - 1;
      LOBYTE(v62) = v62 <= 1;
      if( (_BYTE)v62 )
      {
        v63 = v38;
        if( (unsigned int)v38 >= v42 )
          goto LABEL_57;
        v64 = (char *)&v83[v38];
        do
        {
          v65 = (1i64 << v63) & v45;
          if( (v65 & v36) != 0 && *v64 < v43 )
          {
            v43 = *v64;
            v44 = v63;
          }
          ++v63;
          ++v64;
        }
        while( v63 < v42 );
        v41 = v38;
        if( v44 >= 0 )
        {
LABEL_58:
          v15 = (INT64)*(&KiProcessorBlock
                       + (unsigned int)KiProcessorNumberToIndexMappingTable[64 * *(unsigned __int8 *)(v15 + 208) + v44]);
          goto LABEL_59;
        }
      }
    }
LABEL_51:
    if( (unsigned int)v38 < v42 )
    {
      v46 = __ROL8__(1i64, v41);
      do
      {
        if( (v46 & v36) != 0 && *v40 < v43 )
        {
          v43 = *v40;
          v44 = v38;
        }
        LODWORD(v38) = v38 + 1;
        v46 = __ROL8__(v46, 1);
        ++v40;
      }
      while( (unsigned int)v38 < v42 );
    }
LABEL_57:
    if( v44 >= 0 )
      goto LABEL_58;
LABEL_59:
    v47 = KeGetCurrentPrcb();
    v48 = *(_QWORD **)(v15 + 192);
    HIDWORD(SpinCount) = 0;
    while( _interlockedbittestandset64((volatile signed __int32 *)(v15 + 48), 0i64) )
    {
      do
        KeYieldProcessorEx((UINT64 *)((char *)&SpinCount + 4));
      while( *(_QWORD *)(v15 + 48) );
      v57 = *((_QWORD *)v47 + 4247);
      if( v57 && *((_BYTE *)v47 + 32) <= 1u )
      {
        v66 = *(_DWORD *)(v57 + 24) + 1;
        *(_DWORD *)(v57 + 24) = v66;
      }
    }
    v6 = v70;
    if( v70 )
    {
      v67 = v48[3];
      v49 = v67 & v48[11];
    }
    else
    {
      v49 = 0i64;
    }
    v4 = v84;
    if( v84 )
      break;
    Mask = AffinityMask;
    v50 = v49 | *v48;
    if( (v50 & AffinityMask) == 0 )
      break;
    _InterlockedAnd64((volatile signed __int64 *)(v15 + 48), 0i64);
    v58 = KeGetCurrentPrcb();
    v9 = CurrentPrcb;
    Group = v69;
    v59 = *((_QWORD *)v58 + 4247);
    if( v59 && *((_BYTE *)v58 + 32) <= 1u )
    {
      v61 = (*(_DWORD *)(v59 + 24))-- == 1;
      goto LABEL_95;
    }
  }
  v51 = v82;
  *(_DWORD *)v82 = 0;
  if( (*(_BYTE *)(v15 + 35) & 2) != 0 )
  {
    _InterlockedAnd64((volatile signed __int64 *)(v15 + 48), 0i64);
    v68 = KiSelectCandidateProcessor(v15, (__int64)Thread, -1i64);
    v15 = v68;
    if( !v84 && (*(_BYTE *)(v68 + 35) & 1) == 0 )
      *(_DWORD *)v51 = 1;
  }
  return(_KPRCB *)v15;
}

Referenced by:

KiDeferredReadySingleThread