KePrepareClockTimerForIdle

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

Referenced by:

PpmIdleExecuteTransition