KePrepareClockTimerForIdle

VOID __stdcall KePrepareClockTimerForIdle(UINT8 DeepSleepEnabled, UINT64 MaxDuration, UINT8 MeasureLatency){
  char v3; 
  char v5; 
  unsigned __int64 v6; 
  struct _KPRCB *CurrentPrcb; 
  __int32 v8; 
  char v9; 
  __int64 v10; 
  _KAFFINITY_EX *v11; 
  UINT64 v12; 
  unsigned __int64 v13; 
  __int64 v14; 
  __int64 v15; 
  unsigned __int64 v16; 
  bool v17; 
  bool v18; 
  _KPRCB **v19; 
  __int64 v20; 
  volatile signed __int32 *SchedulerAssist; 
  __int64 v22; 
  UINT64 *NextDueTime; 
  UINT64 RequestedIncrement; 
  UINT64 DeliveredIncrement; 
  _KEXPECTED_WAKE_REASON ExpectedWakeReason[2]; 
  INT64 CurrentQpc; 

  v5 = v3;
  v6 = MeasureLatency;
  DeliveredIncrement = 0i64;
  CurrentPrcb = KeGetCurrentPrcb();
  *(_QWORD *)ExpectedWakeReason = 0i64;
  if( KiDynamicTickInitialized
    && !(_BYTE)KiDynamicTickDisableReason
    && MeasureLatency > (unsigned __int64)(unsigned int)KiLastRequestedTimeIncrement )
  {
    if( MeasureLatency > (unsigned __int64)KiMaxDynamicTickDuration )
    {
      ++dword_140C3134C;
      v6 = KiMaxDynamicTickDuration;
    }
    v8 = _InterlockedExchange((volatile __int32 *)&KiClockState, 3);
    if( (unsigned __int8)PoAllProcessorsDeepIdle() && (DeepSleepEnabled || !KeIsForceIdleEngaged()) )
    {
      v10 = *(_QWORD *)&KUSER_SHARED_DATA.InterruptTime.LowPart;
      LOBYTE(NextDueTime) = v9;
      KiGetNextTimerExpirationDueTime(
        CurrentPrcb,
        1u,
        *(UINT64 *)&KUSER_SHARED_DATA.InterruptTime.LowPart,
        DeepSleepEnabled,
        NextDueTime,
        ExpectedWakeReason);
      v13 = *(_QWORD *)ExpectedWakeReason;
      if( v5 )
      {
        LODWORD(v22) = RtlGetInterruptTimePrecise(&CurrentQpc);
        v15 = v22;
      }
      else
      {
        if( *(_QWORD *)ExpectedWakeReason <= v10 + (unsigned __int64)(unsigned int)KiLastRequestedTimeIncrement
          || !DeepSleepEnabled && !(unsigned __int8)KiClockTimerOneShotReady((_IPI_TYPE)v10, v11, v12) )
        {
          goto LABEL_35;
        }
        LODWORD(v14) = RtlGetInterruptTimePrecise(&CurrentQpc);
        v15 = v14;
        if( v13 <= v14 + (unsigned __int64)(unsigned int)KiLastRequestedTimeIncrement )
          goto LABEL_35;
      }
      if( v13 > v15 + (unsigned __int64)(unsigned int)KiMinDynamicTickDuration )
      {
        v16 = v13 - v15;
        if( v16 > v6 )
          v16 = v6;
        RequestedIncrement = v16;
        if( v5 )
        {
          if( !DeepSleepEnabled )
          {
            v16 = RequestedIncrement;
            if( RequestedIncrement > (unsigned int)KiClockLatencyMaxDynamicTickDuration )
            {
              v16 = (unsigned int)KiClockLatencyMaxDynamicTickDuration;
              RequestedIncrement = (unsigned int)KiClockLatencyMaxDynamicTickDuration;
            }
          }
        }
        ((void(__fastcall *)(__int64, unsigned __int64, UINT64 *))off_140C00890[0])(1i64, v16, &DeliveredIncrement);
        KiSetPendingTick(1u);
        KiClockTimerOneShotStartTime = v15;
        KiEventClockStateChange(ClockStateOneShot, (_KI_CLOCK_STATE)v8, &DeliveredIncrement, &RequestedIncrement);
        if( v5 )
          KiClockLatencyMeasurementEnabled = 1;
        if( KeIsForceIdleEngaged() )
          KiForceIdleReset = 1;
        ++qword_140C31350;
        v8 = 1;
        v17 = v16 < qword_140C31388;
        CurrentPrcb->ClockOwner = 0;
        if( v17 )
          qword_140C31388 = v16;
        if( v16 > qword_140C31380 )
          qword_140C31380 = v16;
        if( DeepSleepEnabled )
          KiConsiderTimerRebasing = 1;
        v18 = KiHrTimerActiveCount > 0;
        if( (_DWORD)KeNumberProcessors_0 )
        {
          v19 = &KiProcessorBlock;
          v20 = (unsigned int)KeNumberProcessors_0;
          do
          {
            SchedulerAssist = (volatile signed __int32 *)(*v19)->SchedulerAssist;
            if( SchedulerAssist && (KiVelocityFlags & 0x40) != 0 )
            {
              if( v18 )
                _InterlockedOr(SchedulerAssist, 0x80000u);
              else
                _InterlockedAnd(SchedulerAssist, 0xFFF7FFFF);
            }
            ++v19;
            --v20;
          }
          while( v20 );
        }
        KiClockTimerNextTickTime = v15 + DeliveredIncrement;
        goto LABEL_34;
      }
    }
LABEL_35:
    if( v8 == 4 )
      return;
LABEL_34:
    _InterlockedExchange((volatile __int32 *)&KiClockState, v8);
  }
}

Referenced by:

PpmIdleExecuteTransition