ReactOS 0.4.16-dev-852-gcfcc8d8
time.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/ke/time.c
5 * PURPOSE: Implements timebase functionality
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 */
8
9/* INCLUDES *******************************************************************/
10
11#include <ntoskrnl.h>
12#define NDEBUG
13#include <debug.h>
14
15/* GLOBALS ********************************************************************/
16
20
21/* FUNCTIONS ******************************************************************/
22
24VOID
25KiWriteSystemTime(volatile KSYSTEM_TIME *SystemTime, ULARGE_INTEGER NewTime)
26{
27#ifdef _WIN64
28 /* Do a single atomic write */
29 *(ULONGLONG*)SystemTime = NewTime.QuadPart;
30#else
31 /* Update in 3 steps, so that a reader can recognize partial updates */
32 SystemTime->High1Time = NewTime.HighPart;
33 SystemTime->LowPart = NewTime.LowPart;
34#endif
35 SystemTime->High2Time = NewTime.HighPart;
36}
37
39VOID
41 PKPRCB Prcb,
42 PKTRAP_FRAME TrapFrame,
43 ULARGE_INTEGER InterruptTime)
44{
45 ULONG Hand;
46
47 /* Check for timer expiration */
49 if (KiTimerTableListHead[Hand].Time.QuadPart <= InterruptTime.QuadPart)
50 {
51 /* Check if we are already doing expiration */
52 if (!Prcb->TimerRequest)
53 {
54 /* Request a DPC to handle this */
55 Prcb->TimerRequest = (ULONG_PTR)TrapFrame;
56 Prcb->TimerHand = Hand;
58 }
59 }
60}
61
62VOID
67{
68 PKPRCB Prcb = KeGetCurrentPrcb();
69 ULARGE_INTEGER CurrentTime, InterruptTime;
70 LONG OldTickOffset;
71
72 /* Check if this tick is being skipped */
73 if (Prcb->SkipTick)
74 {
75 /* Handle it next time */
76 Prcb->SkipTick = FALSE;
77
78 /* Increase interrupt count and end the interrupt */
79 Prcb->InterruptCount++;
80
81#ifdef _M_IX86 // x86 optimization
82 KiEndInterrupt(Irql, TrapFrame);
83#endif
84
85 /* Note: non-x86 return back to the caller! */
86 return;
87 }
88
89 /* Add the increment time to the shared data */
90 InterruptTime.QuadPart = *(ULONGLONG*)&SharedUserData->InterruptTime;
91 InterruptTime.QuadPart += Increment;
92 KiWriteSystemTime(&SharedUserData->InterruptTime, InterruptTime);
93
94 /* Check for timer expiration */
95 KiCheckForTimerExpiration(Prcb, TrapFrame, InterruptTime);
96
97 /* Update the tick offset */
99
100 /* If the debugger is enabled, check for break-in request */
102 {
103 /* Break-in requested! */
105 }
106
107 /* Check for full tick */
108 if (OldTickOffset <= (LONG)Increment)
109 {
110 /* Update the system time */
111 CurrentTime.QuadPart = *(ULONGLONG*)&SharedUserData->SystemTime;
112 CurrentTime.QuadPart += KeTimeAdjustment;
113 KiWriteSystemTime(&SharedUserData->SystemTime, CurrentTime);
114
115 /* Update the tick count */
116 CurrentTime.QuadPart = (*(ULONGLONG*)&KeTickCount) + 1;
117 KiWriteSystemTime(&KeTickCount, CurrentTime);
118
119 /* Update it in the shared user data */
120 KiWriteSystemTime(&SharedUserData->TickCount, CurrentTime);
121
122 /* Check for expiration with the new tick count as well */
123 KiCheckForTimerExpiration(Prcb, TrapFrame, InterruptTime);
124
125 /* Reset the tick offset */
127
128 /* Update processor/thread runtime */
129 KeUpdateRunTime(TrapFrame, Irql);
130 }
131 else
132 {
133 /* Increase interrupt count only */
134 Prcb->InterruptCount++;
135 }
136
137#ifdef _M_IX86 // x86 optimization
138 /* Disable interrupts and end the interrupt */
139 KiEndInterrupt(Irql, TrapFrame);
140#endif
141}
142
143VOID
144NTAPI
146 IN KIRQL Irql)
147{
149 PKPRCB Prcb = KeGetCurrentPrcb();
150
151 /* Check if this tick is being skipped */
152 if (Prcb->SkipTick)
153 {
154 /* Handle it next time */
155 Prcb->SkipTick = FALSE;
156 return;
157 }
158
159 /* Increase interrupt count */
160 Prcb->InterruptCount++;
161
162 /* Check if we came from user mode */
163#ifndef _M_ARM
164 if (KiUserTrap(TrapFrame) || (TrapFrame->EFlags & EFLAGS_V86_MASK))
165#else
166 if (TrapFrame->PreviousMode == UserMode)
167#endif
168 {
169 /* Increase thread user time */
170 Prcb->UserTime++;
171 Thread->UserTime++;
172 }
173 else
174 {
175 /* See if we were in an ISR */
176 Prcb->KernelTime++;
177 if (Irql > DISPATCH_LEVEL)
178 {
179 /* Handle that */
180 Prcb->InterruptTime++;
181 }
182 else if ((Irql < DISPATCH_LEVEL) || !(Prcb->DpcRoutineActive))
183 {
184 /* Handle being in kernel mode */
185 Thread->KernelTime++;
186 }
187 else
188 {
189 /* Handle being in a DPC */
190 Prcb->DpcTime++;
191
192#if DBG
193 /* Update the DPC time */
194 Prcb->DebugDpcTime++;
195
196 /* Check if we have timed out */
197 if (Prcb->DebugDpcTime == KiDPCTimeout)
198 {
199 /* We did! */
200 DbgPrint("*** DPC routine > 1 sec --- This is not a break in KeUpdateSystemTime\n");
201
202 /* Break if debugger is enabled */
204
205 /* Clear state */
206 Prcb->DebugDpcTime = 0;
207 }
208#endif
209 }
210 }
211
212 /* Update DPC rates */
213 Prcb->DpcRequestRate = ((Prcb->DpcData[0].DpcCount - Prcb->DpcLastCount) +
214 Prcb->DpcRequestRate) >> 1;
215 Prcb->DpcLastCount = Prcb->DpcData[0].DpcCount;
216
217 /* Check if the queue is large enough */
218 if ((Prcb->DpcData[0].DpcQueueDepth) && !(Prcb->DpcRoutineActive))
219 {
220 /* Request a DPC */
223
224 /* Fix the maximum queue depth */
225 if ((Prcb->DpcRequestRate < KiIdealDpcRate) &&
226 (Prcb->MaximumDpcQueueDepth > 1))
227 {
228 /* Make it smaller */
229 Prcb->MaximumDpcQueueDepth--;
230 }
231 }
232 else
233 {
234 /* Check if we've reached the adjustment limit */
235 if (!(--Prcb->AdjustDpcThreshold))
236 {
237 /* Reset it, and check the queue maximum */
240 {
241 /* Increase it */
242 Prcb->MaximumDpcQueueDepth++;
243 }
244 }
245 }
246
247 /* Decrement the thread quantum */
249
250 /* Check if the time expired */
251 if ((Thread->Quantum <= 0) && (Thread != Prcb->IdleThread))
252 {
253 /* Schedule a quantum end */
254 Prcb->QuantumEnd = 1;
256 }
257}
unsigned char BOOLEAN
_Out_ PKIRQL Irql
Definition: csq.h:179
#define FALSE
Definition: types.h:117
#define ULONG_PTR
Definition: config.h:101
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG Increment
Definition: CrNtStubs.h:46
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
VOID FASTCALL HalRequestSoftwareInterrupt(IN KIRQL Irql)
Definition: pic.c:271
#define DbgPrint
Definition: hal.h:12
#define KeGetCurrentThread
Definition: hal.h:55
NTSYSAPI void WINAPI DbgBreakPoint(void)
#define InterlockedExchangeAdd
Definition: interlocked.h:181
BOOLEAN KdDebuggerEnabled
Definition: kddata.c:82
BOOLEAN NTAPI KdPollBreakIn(VOID)
Definition: kdlock.c:75
static PLARGE_INTEGER Time
Definition: time.c:105
#define EFLAGS_V86_MASK
Definition: ketypes.h:197
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1182
#define UserMode
Definition: asm.h:39
#define DBG_STATUS_CONTROL_C
Definition: kdtypes.h:39
#define CLOCK_QUANTUM_DECREMENT
Definition: ketypes.h:138
#define FASTCALL
Definition: nt_native.h:50
NTKERNELAPI volatile KSYSTEM_TIME KeTickCount
Definition: clock.c:19
FORCEINLINE BOOLEAN KiUserTrap(IN PKTRAP_FRAME TrapFrame)
Definition: ke.h:367
#define KiEndInterrupt(x, y)
Definition: ke.h:175
KTIMER_TABLE_ENTRY KiTimerTableListHead[TIMER_TABLE_SIZE]
Definition: timerobj.c:17
ULONG KiAdjustDpcThreshold
Definition: dpc.c:21
ULONG KiMaximumDpcQueueDepth
Definition: dpc.c:19
ULONG KiDPCTimeout
Definition: dpc.c:27
ULONG KiIdealDpcRate
Definition: dpc.c:22
ULONG KeMaximumIncrement
Definition: clock.c:20
BOOLEAN KiTimeAdjustmentEnabled
Definition: time.c:19
LONG KiTickOffset
Definition: time.c:17
ULONG KeTimeAdjustment
Definition: time.c:18
FORCEINLINE VOID KiCheckForTimerExpiration(PKPRCB Prcb, PKTRAP_FRAME TrapFrame, ULARGE_INTEGER InterruptTime)
Definition: time.c:40
FORCEINLINE VOID KiWriteSystemTime(volatile KSYSTEM_TIME *SystemTime, ULARGE_INTEGER NewTime)
Definition: time.c:25
VOID NTAPI KeUpdateRunTime(IN PKTRAP_FRAME TrapFrame, IN KIRQL Irql)
Definition: time.c:145
VOID FASTCALL KeUpdateSystemTime(IN PKTRAP_FRAME TrapFrame, IN ULONG Increment, IN KIRQL Irql)
Definition: time.c:64
long LONG
Definition: pedump.c:60
#define SharedUserData
ULONG DpcCount
Definition: ketypes.h:860
volatile ULONG DpcQueueDepth
Definition: ketypes.h:858
UINT64 TimerHand
Definition: ketypes.h:783
UINT64 TimerRequest
Definition: ketypes.h:784
ULONG InterruptTime
Definition: ketypes.h:829
struct _KTHREAD * IdleThread
Definition: ketypes.h:661
ULONG UserTime
Definition: ketypes.h:827
ULONG InterruptCount
Definition: ketypes.h:825
LONG MaximumDpcQueueDepth
Definition: ketypes.h:776
ULONG DpcLastCount
Definition: ketypes.h:787
UCHAR SkipTick
Definition: ketypes.h:831
ULONG DebugDpcTime
Definition: ketypes.h:572
KDPC_DATA DpcData[2]
Definition: ketypes.h:769
UCHAR QuantumEnd
Definition: ketypes.h:789
ULONG DpcTime
Definition: ketypes.h:828
ULONG AdjustDpcThreshold
Definition: ketypes.h:830
ULONG KernelTime
Definition: ketypes.h:826
ULONG DpcRequestRate
Definition: ketypes.h:777
UCHAR DpcRoutineActive
Definition: ketypes.h:781
LONG High1Time
Definition: ketypes.h:930
LONG High2Time
Definition: ketypes.h:931
ULONG LowPart
Definition: ketypes.h:929
$ULONG LowPart
Definition: ntbasedef.h:577
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
$ULONG HighPart
Definition: ntbasedef.h:578
#define NTAPI
Definition: typedefs.h:36
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
uint64_t ULONGLONG
Definition: typedefs.h:67
__analysis_noreturn NTSYSAPI VOID NTAPI DbgBreakPointWithStatus(_In_ ULONG Status)
LONGLONG QuadPart
Definition: typedefs.h:114
#define FORCEINLINE
Definition: wdftypes.h:67
#define TIMER_TABLE_SIZE
Definition: ketypes.h:848