2010-01-19 06:16:47 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* LICENSE: BSD - See COPYING.ARM in the top level directory
|
|
|
|
* FILE: ntoskrnl/ke/time.c
|
|
|
|
* PURPOSE: Implements timebase functionality
|
|
|
|
* PROGRAMMERS: ReactOS Portable Systems Group
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *******************************************************************/
|
|
|
|
|
|
|
|
#include <ntoskrnl.h>
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/* GLOBALS ********************************************************************/
|
|
|
|
|
|
|
|
LONG KiTickOffset;
|
|
|
|
ULONG KeTimeAdjustment;
|
2011-10-11 16:11:59 +00:00
|
|
|
BOOLEAN KiTimeAdjustmentEnabled = FALSE;
|
2010-01-19 06:16:47 +00:00
|
|
|
|
|
|
|
/* FUNCTIONS ******************************************************************/
|
|
|
|
|
2012-01-25 16:56:06 +00:00
|
|
|
FORCEINLINE
|
2013-11-26 13:45:33 +00:00
|
|
|
VOID
|
2012-01-25 16:56:06 +00:00
|
|
|
KiWriteSystemTime(volatile KSYSTEM_TIME *SystemTime, ULARGE_INTEGER NewTime)
|
|
|
|
{
|
|
|
|
#ifdef _WIN64
|
|
|
|
/* Do a single atomic write */
|
|
|
|
*(ULONGLONG*)SystemTime = NewTime.QuadPart;
|
|
|
|
#else
|
|
|
|
/* Update in 3 steps, so that a reader can recognize partial updates */
|
|
|
|
SystemTime->High1Time = NewTime.HighPart;
|
|
|
|
SystemTime->LowPart = NewTime.LowPart;
|
|
|
|
SystemTime->High2Time = NewTime.HighPart;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE
|
2013-11-26 13:45:33 +00:00
|
|
|
VOID
|
2012-01-25 16:56:06 +00:00
|
|
|
KiCheckForTimerExpiration(
|
|
|
|
PKPRCB Prcb,
|
|
|
|
PKTRAP_FRAME TrapFrame,
|
|
|
|
ULARGE_INTEGER InterruptTime)
|
|
|
|
{
|
|
|
|
ULONG Hand;
|
|
|
|
|
|
|
|
/* Check for timer expiration */
|
|
|
|
Hand = KeTickCount.LowPart & (TIMER_TABLE_SIZE - 1);
|
|
|
|
if (KiTimerTableListHead[Hand].Time.QuadPart <= InterruptTime.QuadPart)
|
|
|
|
{
|
|
|
|
/* Check if we are already doing expiration */
|
|
|
|
if (!Prcb->TimerRequest)
|
|
|
|
{
|
|
|
|
/* Request a DPC to handle this */
|
|
|
|
Prcb->TimerRequest = (ULONG_PTR)TrapFrame;
|
|
|
|
Prcb->TimerHand = Hand;
|
|
|
|
HalRequestSoftwareInterrupt(DISPATCH_LEVEL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-19 06:16:47 +00:00
|
|
|
VOID
|
2010-01-26 21:41:05 +00:00
|
|
|
FASTCALL
|
2010-01-19 06:16:47 +00:00
|
|
|
KeUpdateSystemTime(IN PKTRAP_FRAME TrapFrame,
|
|
|
|
IN ULONG Increment,
|
2011-10-11 16:11:59 +00:00
|
|
|
IN KIRQL Irql)
|
2010-01-19 06:16:47 +00:00
|
|
|
{
|
|
|
|
PKPRCB Prcb = KeGetCurrentPrcb();
|
|
|
|
ULARGE_INTEGER CurrentTime, InterruptTime;
|
2012-01-25 16:56:06 +00:00
|
|
|
LONG OldTickOffset;
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2012-03-20 13:39:25 +00:00
|
|
|
/* Check if this tick is being skipped */
|
|
|
|
if (Prcb->SkipTick)
|
|
|
|
{
|
|
|
|
/* Handle it next time */
|
|
|
|
Prcb->SkipTick = FALSE;
|
|
|
|
|
|
|
|
/* Increase interrupt count and end the interrupt */
|
|
|
|
Prcb->InterruptCount++;
|
|
|
|
KiEndInterrupt(Irql, TrapFrame);
|
2014-05-10 14:33:37 +00:00
|
|
|
|
|
|
|
/* Note: non-x86 return back to the caller! */
|
|
|
|
return;
|
2012-03-20 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 06:16:47 +00:00
|
|
|
/* Add the increment time to the shared data */
|
2012-01-25 16:56:06 +00:00
|
|
|
InterruptTime.QuadPart = *(ULONGLONG*)&SharedUserData->InterruptTime;
|
2010-01-19 06:16:47 +00:00
|
|
|
InterruptTime.QuadPart += Increment;
|
2012-01-25 16:56:06 +00:00
|
|
|
KiWriteSystemTime(&SharedUserData->InterruptTime, InterruptTime);
|
|
|
|
|
|
|
|
/* Check for timer expiration */
|
|
|
|
KiCheckForTimerExpiration(Prcb, TrapFrame, InterruptTime);
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2012-01-25 16:56:06 +00:00
|
|
|
/* Update the tick offset */
|
|
|
|
OldTickOffset = InterlockedExchangeAdd(&KiTickOffset, -(LONG)Increment);
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2012-03-20 13:39:25 +00:00
|
|
|
/* If the debugger is enabled, check for break-in request */
|
|
|
|
if (KdDebuggerEnabled && KdPollBreakIn())
|
|
|
|
{
|
|
|
|
/* Break-in requested! */
|
|
|
|
DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C);
|
|
|
|
}
|
|
|
|
|
2012-01-25 16:56:06 +00:00
|
|
|
/* Check for full tick */
|
|
|
|
if (OldTickOffset <= (LONG)Increment)
|
2010-01-19 06:16:47 +00:00
|
|
|
{
|
|
|
|
/* Update the system time */
|
2012-01-25 16:56:06 +00:00
|
|
|
CurrentTime.QuadPart = *(ULONGLONG*)&SharedUserData->SystemTime;
|
2010-01-19 06:16:47 +00:00
|
|
|
CurrentTime.QuadPart += KeTimeAdjustment;
|
2012-01-25 16:56:06 +00:00
|
|
|
KiWriteSystemTime(&SharedUserData->SystemTime, CurrentTime);
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2010-01-19 06:16:47 +00:00
|
|
|
/* Update the tick count */
|
2012-01-25 16:56:06 +00:00
|
|
|
CurrentTime.QuadPart = (*(ULONGLONG*)&KeTickCount) + 1;
|
|
|
|
KiWriteSystemTime(&KeTickCount, CurrentTime);
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2010-01-19 06:16:47 +00:00
|
|
|
/* Update it in the shared user data */
|
2012-01-25 16:56:06 +00:00
|
|
|
KiWriteSystemTime(&SharedUserData->TickCount, CurrentTime);
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2010-01-19 06:16:47 +00:00
|
|
|
/* Check for expiration with the new tick count as well */
|
2012-01-25 16:56:06 +00:00
|
|
|
KiCheckForTimerExpiration(Prcb, TrapFrame, InterruptTime);
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2012-01-25 16:56:06 +00:00
|
|
|
/* Reset the tick offset */
|
2010-01-19 06:16:47 +00:00
|
|
|
KiTickOffset += KeMaximumIncrement;
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2012-01-25 16:56:06 +00:00
|
|
|
/* Update processor/thread runtime */
|
2010-01-19 06:16:47 +00:00
|
|
|
KeUpdateRunTime(TrapFrame, Irql);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-25 16:56:06 +00:00
|
|
|
/* Increase interrupt count only */
|
2010-01-19 06:16:47 +00:00
|
|
|
Prcb->InterruptCount++;
|
|
|
|
}
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2010-01-26 21:41:05 +00:00
|
|
|
/* Disable interrupts and end the interrupt */
|
2010-02-01 03:51:45 +00:00
|
|
|
KiEndInterrupt(Irql, TrapFrame);
|
2010-01-19 06:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
KeUpdateRunTime(IN PKTRAP_FRAME TrapFrame,
|
|
|
|
IN KIRQL Irql)
|
|
|
|
{
|
|
|
|
PKTHREAD Thread = KeGetCurrentThread();
|
|
|
|
PKPRCB Prcb = KeGetCurrentPrcb();
|
|
|
|
|
2012-03-20 13:39:25 +00:00
|
|
|
/* Check if this tick is being skipped */
|
|
|
|
if (Prcb->SkipTick)
|
|
|
|
{
|
|
|
|
/* Handle it next time */
|
|
|
|
Prcb->SkipTick = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-19 06:16:47 +00:00
|
|
|
/* Increase interrupt count */
|
|
|
|
Prcb->InterruptCount++;
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2010-01-19 06:16:47 +00:00
|
|
|
/* Check if we came from user mode */
|
|
|
|
#ifndef _M_ARM
|
2013-10-19 11:33:34 +00:00
|
|
|
if (KiUserTrap(TrapFrame) || (TrapFrame->EFlags & EFLAGS_V86_MASK))
|
2010-01-19 06:16:47 +00:00
|
|
|
#else
|
|
|
|
if (TrapFrame->PreviousMode == UserMode)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Increase thread user time */
|
|
|
|
Prcb->UserTime++;
|
|
|
|
Thread->UserTime++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* See if we were in an ISR */
|
|
|
|
Prcb->KernelTime++;
|
|
|
|
if (Irql > DISPATCH_LEVEL)
|
|
|
|
{
|
|
|
|
/* Handle that */
|
|
|
|
Prcb->InterruptTime++;
|
|
|
|
}
|
|
|
|
else if ((Irql < DISPATCH_LEVEL) || !(Prcb->DpcRoutineActive))
|
|
|
|
{
|
|
|
|
/* Handle being in kernel mode */
|
|
|
|
Thread->KernelTime++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Handle being in a DPC */
|
|
|
|
Prcb->DpcTime++;
|
2012-03-20 13:39:25 +00:00
|
|
|
|
2012-03-20 13:53:01 +00:00
|
|
|
#if 0 //DBG
|
2012-03-20 13:39:25 +00:00
|
|
|
/* Update the DPC time */
|
|
|
|
Prcb->DebugDpcTime++;
|
|
|
|
|
|
|
|
/* Check if we have timed out */
|
|
|
|
if (Prcb->DebugDpcTime == KiDPCTimeout);
|
|
|
|
{
|
|
|
|
/* We did! */
|
|
|
|
DbgPrint("*** DPC routine > 1 sec --- This is not a break in KeUpdateSystemTime\n");
|
|
|
|
|
|
|
|
/* Break if debugger is enabled */
|
|
|
|
if (KdDebuggerEnabled) DbgBreakPoint();
|
|
|
|
|
|
|
|
/* Clear state */
|
|
|
|
Prcb->DebugDpcTime = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2010-01-19 06:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2010-01-19 06:16:47 +00:00
|
|
|
/* Update DPC rates */
|
|
|
|
Prcb->DpcRequestRate = ((Prcb->DpcData[0].DpcCount - Prcb->DpcLastCount) +
|
|
|
|
Prcb->DpcRequestRate) >> 1;
|
|
|
|
Prcb->DpcLastCount = Prcb->DpcData[0].DpcCount;
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2010-01-19 06:16:47 +00:00
|
|
|
/* Check if the queue is large enough */
|
|
|
|
if ((Prcb->DpcData[0].DpcQueueDepth) && !(Prcb->DpcRoutineActive))
|
|
|
|
{
|
|
|
|
/* Request a DPC */
|
|
|
|
Prcb->AdjustDpcThreshold = KiAdjustDpcThreshold;
|
|
|
|
HalRequestSoftwareInterrupt(DISPATCH_LEVEL);
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2010-01-19 06:16:47 +00:00
|
|
|
/* Fix the maximum queue depth */
|
|
|
|
if ((Prcb->DpcRequestRate < KiIdealDpcRate) &&
|
|
|
|
(Prcb->MaximumDpcQueueDepth > 1))
|
|
|
|
{
|
|
|
|
/* Make it smaller */
|
|
|
|
Prcb->MaximumDpcQueueDepth--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check if we've reached the adjustment limit */
|
|
|
|
if (!(--Prcb->AdjustDpcThreshold))
|
|
|
|
{
|
|
|
|
/* Reset it, and check the queue maximum */
|
|
|
|
Prcb->AdjustDpcThreshold = KiAdjustDpcThreshold;
|
|
|
|
if (KiMaximumDpcQueueDepth != Prcb->MaximumDpcQueueDepth)
|
|
|
|
{
|
|
|
|
/* Increase it */
|
|
|
|
Prcb->MaximumDpcQueueDepth++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-11 16:11:59 +00:00
|
|
|
|
2010-01-19 06:16:47 +00:00
|
|
|
/* Decrement the thread quantum */
|
|
|
|
Thread->Quantum -= CLOCK_QUANTUM_DECREMENT;
|
|
|
|
|
|
|
|
/* Check if the time expired */
|
|
|
|
if ((Thread->Quantum <= 0) && (Thread != Prcb->IdleThread))
|
|
|
|
{
|
|
|
|
/* Schedule a quantum end */
|
|
|
|
Prcb->QuantumEnd = 1;
|
|
|
|
HalRequestSoftwareInterrupt(DISPATCH_LEVEL);
|
|
|
|
}
|
|
|
|
}
|