1 /* $Id: timer.c,v 1.47 2001/08/27 01:21:50 ekohl Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ke/timer.c
6 * PURPOSE: Handle timers
7 * PROGRAMMER: David Welch (welch@mcmail.com)
10 * 12/3/99: Phillip Susi: enabled the timers, fixed spin lock
13 /* NOTES ******************************************************************/
15 * System time units are 100-nanosecond intervals
18 /* INCLUDES ***************************************************************/
21 #include <ddk/ntddk.h>
22 #include <internal/ke.h>
23 #include <internal/id.h>
24 #include <internal/ps.h>
27 #include <internal/debug.h>
29 /* TYPES *****************************************************************/
33 /* GLOBALS ****************************************************************/
35 #define IDMAP_BASE (0xd0000000)
38 * Return a linear address which can be used to access the physical memory
41 unsigned int physical_to_linear(unsigned int x
)
46 unsigned int linear_to_physical(unsigned int x
)
54 static unsigned long long boot_time
= 0;
55 static unsigned long long system_time
= 0;
58 * Number of timer interrupts since initialisation
60 volatile ULONGLONG KeTickCount
;
61 volatile ULONG KiRawTicks
= 0;
64 * The increment in the system clock every timer tick (in system time units)
70 #define CLOCK_INCREMENT (100000)
73 * PURPOSE: List of timers
75 static LIST_ENTRY TimerListHead
;
76 static KSPIN_LOCK TimerListLock
;
77 static KDPC ExpireTimerDpc
;
79 /* must raise IRQL to HIGH_LEVEL and grab spin lock there, to sync with ISR */
81 extern ULONG PiNrRunnableThreads
;
83 #define MICROSECONDS_PER_TICK (10000)
84 #define TICKS_TO_CALIBRATE (1)
85 #define CALIBRATE_PERIOD (MICROSECONDS_PER_TICK * TICKS_TO_CALIBRATE)
86 #define SYSTEM_TIME_UNITS_PER_MSEC (10000)
88 static BOOLEAN TimerInitDone
= FALSE
;
90 /* FUNCTIONS **************************************************************/
93 NTSTATUS STDCALL
NtQueryTimerResolution(OUT PULONG MinimumResolution
,
94 OUT PULONG MaximumResolution
,
95 OUT PULONG ActualResolution
)
101 NTSTATUS STDCALL
NtSetTimerResolution(IN ULONG RequestedResolution
,
103 OUT PULONG ActualResolution
)
109 NTSTATUS STDCALL
NtQueryPerformanceCounter (IN PLARGE_INTEGER Counter
,
110 IN PLARGE_INTEGER Frequency
)
117 NTSTATUS STDCALL
NtDelayExecution(IN ULONG Alertable
,
121 LARGE_INTEGER Timeout
;
123 Timeout
= *((PLARGE_INTEGER
)Interval
);
124 DPRINT("NtDelayExecution(Alertable %d, Internal %x) IntervalP %x\n",
125 Alertable
, Internal
, Timeout
);
127 DPRINT("Execution delay is %d/%d\n",
128 Timeout
.u
.HighPart
, Timeout
.u
.LowPart
);
129 Status
= KeDelayExecutionThread(UserMode
, Alertable
, &Timeout
);
135 KeDelayExecutionThread (KPROCESSOR_MODE WaitMode
,
137 PLARGE_INTEGER Interval
)
139 * FUNCTION: Puts the current thread into an alertable or nonalertable
140 * wait state for a given internal
142 * WaitMode = Processor mode in which the caller is waiting
143 * Altertable = Specifies if the wait is alertable
144 * Interval = Specifies the interval to wait
148 PKTHREAD Thread
= KeGetCurrentThread();
150 KeInitializeTimer(&Thread
->Timer
);
151 KeSetTimer(&Thread
->Timer
, *Interval
, NULL
);
152 return (KeWaitForSingleObject(&Thread
->Timer
,
160 KeQueryTimeIncrement (VOID
)
162 * FUNCTION: Gets the increment (in 100-nanosecond units) that is added to
163 * the system clock every time the clock interrupts
164 * RETURNS: The increment
167 return(CLOCK_INCREMENT
);
171 KeQuerySystemTime (PLARGE_INTEGER CurrentTime
)
173 * FUNCTION: Gets the current system time
175 * CurrentTime (OUT) = The routine stores the current time here
176 * NOTE: The time is the number of 100-nanosecond intervals since the
177 * 1st of January, 1601.
180 CurrentTime
->QuadPart
= system_time
;
185 NtGetTickCount (PULONG UpTime
)
187 LARGE_INTEGER TickCount
;
188 if ( UpTime
== NULL
)
189 return(STATUS_INVALID_PARAMETER
);
190 KeQueryTickCount(&TickCount
);
191 *UpTime
= TickCount
.u
.LowPart
;
192 return (STATUS_SUCCESS
);
197 KeSetTimer (PKTIMER Timer
,
198 LARGE_INTEGER DueTime
,
201 * FUNCTION: Sets the absolute or relative interval at which a timer object
202 * is to be set to the signaled state and optionally supplies a
203 * CustomTimerDpc to be executed when the timer expires.
205 * Timer = Points to a previously initialized timer object
206 * DueTimer = If positive then absolute time to expire at
207 * If negative then the relative time to expire at
208 * Dpc = If non-NULL then a dpc to be called when the timer expires
209 * RETURNS: True if the timer was already in the system timer queue
213 return(KeSetTimerEx(Timer
, DueTime
, 0, Dpc
));
217 KeSetTimerEx (PKTIMER Timer
,
218 LARGE_INTEGER DueTime
,
222 * FUNCTION: Sets the absolute or relative interval at which a timer object
223 * is to be set to the signaled state and optionally supplies a
224 * CustomTimerDpc to be executed when the timer expires.
226 * Timer = Points to a previously initialized timer object
227 * DueTimer = If positive then absolute time to expire at
228 * If negative then the relative time to expire at
229 * Dpc = If non-NULL then a dpc to be called when the timer expires
230 * RETURNS: True if the timer was already in the system timer queue
236 DPRINT("KeSetTimerEx(Timer %x), DueTime: \n",Timer
);
237 KeAcquireSpinLock( &TimerListLock
, &oldlvl
);
240 if (DueTime
.QuadPart
< 0)
242 Timer
->DueTime
.QuadPart
= system_time
- DueTime
.QuadPart
;
246 Timer
->DueTime
.QuadPart
= DueTime
.QuadPart
;
248 Timer
->Period
= Period
;
249 Timer
->Header
.SignalState
= FALSE
;
250 if (Timer
->TimerListEntry
.Flink
!= NULL
)
252 KeReleaseSpinLock(&TimerListLock
, oldlvl
);
255 InsertTailList(&TimerListHead
,&Timer
->TimerListEntry
);
256 KeReleaseSpinLock(&TimerListLock
, oldlvl
);
262 KeCancelTimer (PKTIMER Timer
)
264 * FUNCTION: Removes a timer from the system timer list
266 * Timer = timer to cancel
267 * RETURNS: True if the timer was running
273 DPRINT("KeCancelTimer(Timer %x)\n",Timer
);
275 KeRaiseIrql(HIGH_LEVEL
, &oldlvl
);
276 KeAcquireSpinLockAtDpcLevel( &TimerListLock
);
278 if (Timer
->TimerListEntry
.Flink
== NULL
)
280 KeReleaseSpinLock(&TimerListLock
, oldlvl
);
283 RemoveEntryList(&Timer
->TimerListEntry
);
284 Timer
->TimerListEntry
.Flink
= Timer
->TimerListEntry
.Blink
= NULL
;
285 KeReleaseSpinLock(&TimerListLock
, oldlvl
);
291 KeReadStateTimer (PKTIMER Timer
)
293 return(Timer
->Header
.SignalState
);
297 KeInitializeTimer (PKTIMER Timer
)
299 * FUNCTION: Initalizes a kernel timer object
301 * Timer = caller supplied storage for the timer
302 * NOTE: This function initializes a notification timer
305 KeInitializeTimerEx(Timer
,NotificationTimer
);
309 KeInitializeTimerEx (PKTIMER Timer
,
312 * FUNCTION: Initializes a kernel timer object
314 * Timer = caller supplied storage for the timer
315 * Type = the type of timer (notification or synchronization)
316 * NOTE: When a notification type expires all waiting threads are released
317 * and the timer remains signalled until it is explicitly reset. When a
318 * syncrhonization timer expires its state is set to signalled until a
319 * single waiting thread is released and then the timer is reset.
324 if (Type
== NotificationTimer
)
326 IType
= InternalNotificationTimer
;
328 else if (Type
== SynchronizationTimer
)
330 IType
= InternalSynchronizationTimer
;
338 KeInitializeDispatcherHeader(&Timer
->Header
,
340 sizeof(KTIMER
) / sizeof(ULONG
),
342 Timer
->TimerListEntry
.Flink
= Timer
->TimerListEntry
.Blink
= NULL
;
346 KeQueryTickCount (PLARGE_INTEGER TickCount
)
348 * FUNCTION: Returns the number of ticks since the system was booted
350 * TickCount (OUT) = Points to storage for the number of ticks
353 TickCount
->QuadPart
= KeTickCount
;
357 HandleExpiredTimer(PKTIMER current
)
359 DPRINT("HandleExpiredTime(current %x)\n",current
);
360 if (current
->Dpc
!= NULL
)
362 DPRINT("current->Dpc %x current->Dpc->DeferredRoutine %x\n",
363 current
->Dpc
, current
->Dpc
->DeferredRoutine
);
364 KeInsertQueueDpc(current
->Dpc
,
367 DPRINT("Finished dpc routine\n");
369 KeAcquireDispatcherDatabaseLock(FALSE
);
370 current
->Header
.SignalState
= TRUE
;
371 KeDispatcherObjectWake(¤t
->Header
);
372 KeReleaseDispatcherDatabaseLock(FALSE
);
373 if (current
->Period
!= 0)
375 current
->DueTime
.QuadPart
+=
376 current
->Period
* SYSTEM_TIME_UNITS_PER_MSEC
;
380 RemoveEntryList(¤t
->TimerListEntry
);
381 current
->TimerListEntry
.Flink
= current
->TimerListEntry
.Blink
= NULL
;
386 KeExpireTimers(PKDPC Dpc
,
391 PLIST_ENTRY current_entry
= NULL
;
392 PKTIMER current
= NULL
;
393 ULONG Eip
= (ULONG
)Arg1
;
395 DPRINT("KeExpireTimers()\n");
397 current_entry
= TimerListHead
.Flink
;
399 KeAcquireSpinLockAtDpcLevel(&TimerListLock
);
401 while (current_entry
!= &TimerListHead
)
403 current
= CONTAINING_RECORD(current_entry
, KTIMER
, TimerListEntry
);
405 current_entry
= current_entry
->Flink
;
407 if (system_time
>= current
->DueTime
.QuadPart
)
409 HandleExpiredTimer(current
);
413 KiAddProfileEvent(ProfileTime
, Eip
);
415 KeReleaseSpinLockFromDpcLevel(&TimerListLock
);
420 KiUpdateSystemTime (KIRQL oldIrql
, ULONG Eip
)
422 * FUNCTION: Handles a timer interrupt
427 if (TimerInitDone
== FALSE
)
432 * Increment the number of timers ticks
435 system_time
= system_time
+ CLOCK_INCREMENT
;
438 * Queue a DPC that will expire timers
440 KeInsertQueueDpc(&ExpireTimerDpc
, (PVOID
)Eip
, 0);
445 KeInitializeTimerImpl(VOID
)
447 * FUNCTION: Initializes timer irq handling
448 * NOTE: This is only called once from main()
451 TIME_FIELDS TimeFields
;
452 LARGE_INTEGER SystemBootTime
;
454 DPRINT("KeInitializeTimerImpl()\n");
455 InitializeListHead(&TimerListHead
);
456 KeInitializeSpinLock(&TimerListLock
);
457 KeInitializeDpc(&ExpireTimerDpc
, KeExpireTimers
, 0);
458 TimerInitDone
= TRUE
;
460 * Calculate the starting time for the system clock
462 HalQueryRealTimeClock(&TimeFields
);
463 RtlTimeFieldsToTime(&TimeFields
, &SystemBootTime
);
464 boot_time
=SystemBootTime
.QuadPart
;
465 system_time
=boot_time
;
467 DPRINT("Finished KeInitializeTimerImpl()\n");