2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ke/timer.c
5 * PURPOSE: Handle timers
6 * PROGRAMMER: David Welch (welch@mcmail.com)
11 /* NOTES ******************************************************************/
13 * System time units are 100-nanosecond intervals
16 /* INCLUDES ***************************************************************/
19 #include <ddk/ntddk.h>
21 #include <internal/string.h>
25 #include <internal/debug.h>
27 /* TYPES *****************************************************************/
32 /* GLOBALS ****************************************************************/
37 static unsigned long long system_time
= 0;
40 * Number of timer interrupts since initialisation
42 volatile ULONGLONG KiTimerTicks
;
45 * The increment in the system clock every timer tick (in system time units)
51 #define CLOCK_INCREMENT (549450)
54 * PURPOSE: List of timers
56 static LIST_ENTRY timer_list_head
= {NULL
,NULL
};
57 static KSPIN_LOCK timer_list_lock
= {0,};
61 #define MICROSECONDS_PER_TICK (54945)
62 #define TICKS_TO_CALIBRATE (1)
63 #define CALIBRATE_PERIOD (MICROSECONDS_PER_TICK * TICKS_TO_CALIBRATE)
64 #define SYSTEM_TIME_UNITS_PER_MSEC (10000)
66 static unsigned int loops_per_microsecond
= 100;
68 /* FUNCTIONS **************************************************************/
70 VOID
KeCalibrateTimerLoop(VOID
)
72 unsigned int start_tick
;
73 // unsigned int end_tick;
74 // unsigned int nr_ticks;
76 unsigned int microseconds
;
81 start_tick
= KiTimerTicks
;
83 while (start_tick
== KiTimerTicks
);
84 while (KiTimerTicks
== (start_tick
+TICKS_TO_CALIBRATE
))
86 KeStallExecutionProcessor(1);
90 // DbgPrint("microseconds %d\n",microseconds);
92 if (microseconds
> (CALIBRATE_PERIOD
+1000))
94 loops_per_microsecond
= loops_per_microsecond
+ 1;
96 if (microseconds
< (CALIBRATE_PERIOD
-1000))
98 loops_per_microsecond
= loops_per_microsecond
- 1;
100 // DbgPrint("loops_per_microsecond %d\n",loops_per_microsecond);
106 NTSTATUS STDCALL
NtQueryTimerResolution (OUT PULONG MinimumResolution
,
107 OUT PULONG MaximumResolution
,
108 OUT PULONG ActualResolution
)
110 return(ZwQueryTimerResolution(MinimumResolution
,MaximumResolution
,
114 NTSTATUS STDCALL
ZwQueryTimerResolution (OUT PULONG MinimumResolution
,
115 OUT PULONG MaximumResolution
,
116 OUT PULONG ActualResolution
)
121 NTSTATUS STDCALL
NtSetTimerResolution(IN ULONG RequestedResolution
,
123 OUT PULONG ActualResolution
)
125 return(ZwSetTimerResolution(RequestedResolution
,
130 NTSTATUS STDCALL
ZwSetTimerResolution(IN ULONG RequestedResolution
,
132 OUT PULONG ActualResolution
)
137 NTSTATUS STDCALL
NtQueryPerformanceCounter(IN PLARGE_INTEGER Counter
,
138 IN PLARGE_INTEGER Frequency
)
140 return(ZwQueryPerformanceCounter(Counter
,
144 NTSTATUS STDCALL
ZwQueryPerformanceCounter(IN PLARGE_INTEGER Counter
,
145 IN PLARGE_INTEGER Frequency
)
151 NTSTATUS
KeAddThreadTimeout(PKTHREAD Thread
, PLARGE_INTEGER Interval
)
153 assert(Thread
!= NULL
);
154 assert(Interval
!= NULL
);
156 DPRINT("KeAddThreadTimeout(Thread %x, Interval %x)\n",Thread
,Interval
);
158 KeInitializeTimer(&(Thread
->TimerBlock
));
159 KeSetTimer(&(Thread
->TimerBlock
),*Interval
,NULL
);
161 DPRINT("Thread->TimerBlock.entry.Flink %x\n",
162 Thread
->TimerBlock
.entry
.Flink
);
164 return STATUS_SUCCESS
;
168 NTSTATUS STDCALL
NtDelayExecution(IN BOOLEAN Alertable
,
171 return(ZwDelayExecution(Alertable
,Interval
));
174 NTSTATUS STDCALL
ZwDelayExecution(IN BOOLEAN Alertable
,
178 return(STATUS_UNSUCCESSFUL
);
181 NTSTATUS
KeDelayExecutionThread(KPROCESSOR_MODE WaitMode
,
183 PLARGE_INTEGER Interval
)
185 * FUNCTION: Puts the current thread into an alertable or nonalertable
186 * wait state for a given internal
188 * WaitMode = Processor mode in which the caller is waiting
189 * Altertable = Specifies if the wait is alertable
190 * Interval = Specifies the interval to wait
194 PKTHREAD CurrentThread
= KeGetCurrentThread();
195 KeAddThreadTimeout(CurrentThread
,Interval
);
196 return(KeWaitForSingleObject(&(CurrentThread
->TimerBlock
),Executive
,
197 KernelMode
,Alertable
,NULL
));
200 VOID
KeStallExecutionProcessor(ULONG MicroSeconds
)
203 for (i
=0; i
<(loops_per_microsecond
*MicroSeconds
) ;i
++)
210 static inline void ULLToLargeInteger(unsigned long long src
,
213 dest
->LowPart
= src
& 0xffffffff;
214 dest
->HighPart
= (src
>>32);
217 static inline void SLLToLargeInteger(signed long long src
,
222 dest
->LowPart
= src
& 0xffffffff;
223 dest
->HighPart
= (src
>>32);
228 dest
->LowPart
= src
& 0xffffffff;
229 dest
->HighPart
= -(src
>>32);
233 static inline signed long long LargeIntegerToSLL(PLARGE_INTEGER src
)
238 if (src
->HighPart
>= 0)
240 r
= r
| (((unsigned long long)src
->HighPart
)<<32);
244 r
= r
| (((unsigned long long)(-(src
->HighPart
)))<<32);
253 LARGE_INTEGER
KeQueryPerformanceCounter(PLARGE_INTEGER PerformanceFreq
)
255 * FUNCTION: Queries the finest grained running count avaiable in the system
257 * PerformanceFreq (OUT) = The routine stores the number of
258 * performance counters tick per second here
259 * RETURNS: The performance counter value in HERTZ
260 * NOTE: Returns the system tick count or the time-stamp on the pentium
263 if (PerformanceFreq
!= NULL
)
265 LARGE_INTEGER_QUAD_PART(*PerformanceFreq
) = 0;
268 return *PerformanceFreq
;
271 ULONG
KeQueryTimeIncrement(VOID
)
273 * FUNCTION: Gets the increment (in 100-nanosecond units) that is added to
274 * the system clock every time the clock interrupts
275 * RETURNS: The increment
278 return(CLOCK_INCREMENT
);
281 VOID
KeQuerySystemTime(PLARGE_INTEGER CurrentTime
)
283 * FUNCTION: Gets the current system time
285 * CurrentTime (OUT) = The routine stores the current time here
286 * NOTE: The time is the number of 100-nanosecond intervals since the
287 * 1st of January, 1601.
290 LARGE_INTEGER_QUAD_PART(*CurrentTime
) = system_time
;
293 NTSTATUS STDCALL
NtGetTickCount(PULONG UpTime
)
295 return(ZwGetTickCount(UpTime
));
298 NTSTATUS STDCALL
ZwGetTickCount(PULONG UpTime
)
303 BOOLEAN
KeSetTimer(PKTIMER Timer
, LARGE_INTEGER DueTime
, PKDPC Dpc
)
305 * FUNCTION: Sets the absolute or relative interval at which a timer object
306 * is to be set to the signaled state and optionally supplies a
307 * CustomTimerDpc to be executed when the timer expires.
309 * Timer = Points to a previously initialized timer object
310 * DueTimer = If positive then absolute time to expire at
311 * If negative then the relative time to expire at
312 * Dpc = If non-NULL then a dpc to be called when the timer expires
313 * RETURNS: True if the timer was already in the system timer queue
317 return(KeSetTimerEx(Timer
,DueTime
,0,Dpc
));
320 BOOLEAN
KeSetTimerEx(PKTIMER Timer
, LARGE_INTEGER DueTime
, LONG Period
,
323 * FUNCTION: Sets the absolute or relative interval at which a timer object
324 * is to be set to the signaled state and optionally supplies a
325 * CustomTimerDpc to be executed when the timer expires.
327 * Timer = Points to a previously initialized timer object
328 * DueTimer = If positive then absolute time to expire at
329 * If negative then the relative time to expire at
330 * Dpc = If non-NULL then a dpc to be called when the timer expires
331 * RETURNS: True if the timer was already in the system timer queue
337 DPRINT("KeSetTimerEx(Timer %x)\n",Timer
);
339 KeAcquireSpinLock(&timer_list_lock
,&oldlvl
);
342 Timer
->period
=Period
;
343 Timer
->expire_time
= (*(long long int *)&DueTime
);
344 if (Timer
->expire_time
< 0)
346 Timer
->expire_time
= system_time
+ (-Timer
->expire_time
);
348 DPRINT("System:%ld:%ld Expire:%d:%d Period:%d\n",
349 (unsigned long) (system_time
& 0xffffffff),
350 (unsigned long) ((system_time
>> 32) & 0xffffffff),
351 (unsigned long) (Timer
->expire_time
& 0xffffffff),
352 (unsigned long) ((Timer
->expire_time
>> 32) & 0xffffffff),
354 Timer
->signaled
= FALSE
;
357 KeReleaseSpinLock(&timer_list_lock
,oldlvl
);
360 DPRINT("Inserting %x in list\n",&Timer
->entry
);
361 DPRINT("Timer->entry.Flink %x\n",Timer
->entry
.Flink
);
363 InsertTailList(&timer_list_head
,&Timer
->entry
);
364 KeReleaseSpinLock(&timer_list_lock
,oldlvl
);
369 BOOLEAN
KeCancelTimer(PKTIMER Timer
)
371 * FUNCTION: Removes a timer from the system timer list
373 * Timer = timer to cancel
374 * RETURNS: True if the timer was running
380 DPRINT("KeCancelTimer(Timer %x)\n",Timer
);
382 KeAcquireSpinLock(&timer_list_lock
,&oldlvl
);
386 KeReleaseSpinLock(&timer_list_lock
,oldlvl
);
389 RemoveEntryList(&Timer
->entry
);
390 Timer
->running
= FALSE
;
391 KeReleaseSpinLock(&timer_list_lock
,oldlvl
);
396 BOOLEAN
KeReadStateTimer(PKTIMER Timer
)
398 return Timer
->signaled
;
401 VOID
KeInitializeTimer(PKTIMER Timer
)
403 * FUNCTION: Initalizes a kernel timer object
405 * Timer = caller supplied storage for the timer
406 * NOTE: This function initializes a notification timer
409 KeInitializeTimerEx(Timer
,NotificationTimer
);
412 VOID
KeInitializeTimerEx(PKTIMER Timer
, TIMER_TYPE Type
)
414 * FUNCTION: Initializes a kernel timer object
416 * Timer = caller supplied storage for the timer
417 * Type = the type of timer (notification or synchronization)
418 * NOTE: When a notification type expires all waiting threads are released
419 * and the timer remains signalled until it is explicitly reset. When a
420 * syncrhonization timer expires its state is set to signalled until a
421 * single waiting thread is released and then the timer is reset.
424 Timer
->running
=FALSE
;
426 Timer
->signaled
=FALSE
;
429 VOID
KeQueryTickCount(PLARGE_INTEGER TickCount
)
431 * FUNCTION: Returns the number of ticks since the system was booted
433 * TickCount (OUT) = Points to storage for the number of ticks
436 LARGE_INTEGER_QUAD_PART(*TickCount
) = KiTimerTicks
;
439 static void HandleExpiredTimer(PKTIMER current
)
441 DPRINT("HandleExpiredTime(current %x)\n",current
);
442 if (current
->dpc
!=NULL
)
444 DPRINT("current->dpc->DeferredRoutine %x\n",
445 current
->dpc
->DeferredRoutine
);
446 current
->dpc
->DeferredRoutine(current
->dpc
,
447 current
->dpc
->DeferredContext
,
448 current
->dpc
->SystemArgument1
,
449 current
->dpc
->SystemArgument2
);
450 DPRINT("Finished dpc routine\n");
452 current
->signaled
=TRUE
;
453 if (current
->period
!= 0)
455 DPRINT("System:%ld:%ld Expire:%d:%d Period:%d\n",
456 (unsigned long) (system_time
& 0xffffffff),
457 (unsigned long) ((system_time
>> 32) & 0xffffffff),
458 (unsigned long) (current
->expire_time
& 0xffffffff),
459 (unsigned long) ((current
->expire_time
>> 32) & 0xffffffff),
461 current
->expire_time
+= current
->period
* SYSTEM_TIME_UNITS_PER_MSEC
;
465 RemoveEntryList(¤t
->entry
);
466 current
->running
=FALSE
;
470 void KeExpireTimers(void)
472 PLIST_ENTRY current_entry
= timer_list_head
.Flink
;
473 PKTIMER current
= CONTAINING_RECORD(current_entry
,KTIMER
,entry
);
476 DPRINT("KeExpireTimers()\n");
477 DPRINT("&timer_list_head %x\n",&timer_list_head
);
478 DPRINT("current_entry %x\n",current_entry
);
479 DPRINT("current_entry->Flink %x\n",current_entry
->Flink
);
480 DPRINT("current_entry->Flink->Flink %x\n",current_entry
->Flink
->Flink
);
482 KeAcquireSpinLock(&timer_list_lock
,&oldlvl
);
484 while (current_entry
!=(&timer_list_head
))
486 if (system_time
>= current
->expire_time
)
488 HandleExpiredTimer(current
);
491 current_entry
= current_entry
->Flink
;
492 current
= CONTAINING_RECORD(current_entry
,KTIMER
,entry
);
494 KeReleaseSpinLock(&timer_list_lock
,oldlvl
);
495 DPRINT("Finished KeExpireTimers()\n");
499 BOOLEAN
KiTimerInterrupt(VOID
)
501 * FUNCTION: Handles a timer interrupt
505 char* vidmem
=(char *)physical_to_linear(0xb8000 + 160 - 36);
508 extern ULONG EiNrUsedBlocks
;
509 extern unsigned int EiFreeNonPagedPool
;
510 extern unsigned int EiUsedNonPagedPool
;
513 * Increment the number of timers ticks
516 system_time
= system_time
+ CLOCK_INCREMENT
;
519 * Display the tick count in the top left of the screen as a debugging
522 // sprintf(str,"%.8u %.8u",nr_used_blocks,ticks);
523 if ((EiFreeNonPagedPool
+ EiUsedNonPagedPool
) == 0)
529 x
= (EiFreeNonPagedPool
* 100) /
530 (EiFreeNonPagedPool
+ EiUsedNonPagedPool
);
531 y
= (EiUsedNonPagedPool
* 100) /
532 (EiFreeNonPagedPool
+ EiUsedNonPagedPool
);
534 // sprintf(str,"%.8u %.8u",EiFreeNonPagedPool,ticks);
535 memset(str
, 0, sizeof(str
));
536 sprintf(str
,"%.8u %.8u",(unsigned int)EiNrUsedBlocks
,
537 (unsigned int)EiFreeNonPagedPool
);
538 // sprintf(str,"%.8u %.8u",EiFreeNonPagedPool,EiUsedNonPagedPool);
539 // sprintf(str,"%.8u %.8u",PiNrThreads,KiTimerTicks);
552 VOID
KeInitializeTimerImpl(VOID
)
554 * FUNCTION: Initializes timer irq handling
555 * NOTE: This is only called once from main()
558 InitializeListHead(&timer_list_head
);
559 KeInitializeSpinLock(&timer_list_lock
);
562 * Calculate the starting time for the system clock