1 /* $Id: timer.c,v 1.31 2000/07/04 08:52:40 dwelch 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>
23 #include <internal/string.h>
25 #include <internal/ke.h>
26 #include <internal/id.h>
27 #include <internal/ps.h>
30 #include <internal/debug.h>
32 /* TYPES *****************************************************************/
36 /* GLOBALS ****************************************************************/
38 #define IDMAP_BASE (0xd0000000)
41 * Return a linear address which can be used to access the physical memory
44 extern inline unsigned int physical_to_linear(unsigned int x
)
49 extern inline unsigned int linear_to_physical(unsigned int x
)
57 static unsigned long long boot_time
= 0;
58 static unsigned long long system_time
= 0;
61 * Number of timer interrupts since initialisation
63 volatile ULONGLONG KiTimerTicks
;
66 * The increment in the system clock every timer tick (in system time units)
72 #define CLOCK_INCREMENT (549450)
75 * PURPOSE: List of timers
77 static LIST_ENTRY TimerListHead
;
78 static KSPIN_LOCK TimerListLock
;
80 /* must raise IRQL to HIGH_LEVEL and grab spin lock there, to sync with ISR */
82 extern ULONG PiNrRunnableThreads
;
84 #define MICROSECONDS_PER_TICK (54945)
85 #define TICKS_TO_CALIBRATE (1)
86 #define CALIBRATE_PERIOD (MICROSECONDS_PER_TICK * TICKS_TO_CALIBRATE)
87 #define SYSTEM_TIME_UNITS_PER_MSEC (10000)
89 static BOOLEAN TimerInitDone
= FALSE
;
91 /* FUNCTIONS **************************************************************/
94 NTSTATUS STDCALL
NtQueryTimerResolution(OUT PULONG MinimumResolution
,
95 OUT PULONG MaximumResolution
,
96 OUT PULONG ActualResolution
)
102 NTSTATUS STDCALL
NtSetTimerResolution(IN ULONG RequestedResolution
,
104 OUT PULONG ActualResolution
)
110 NTSTATUS STDCALL
NtQueryPerformanceCounter (IN PLARGE_INTEGER Counter
,
111 IN PLARGE_INTEGER Frequency
)
117 NTSTATUS
KeAddThreadTimeout(PKTHREAD Thread
, PLARGE_INTEGER Interval
)
119 assert(Thread
!= NULL
);
120 assert(Interval
!= NULL
);
122 DPRINT("KeAddThreadTimeout(Thread %x, Interval %x)\n",Thread
,Interval
);
124 KeInitializeTimer(&(Thread
->Timer
));
125 KeSetTimer( &(Thread
->Timer
), *Interval
, &Thread
->TimerDpc
);
127 DPRINT("Thread->Timer.entry.Flink %x\n",
128 Thread
->Timer
.TimerListEntry
.Flink
);
130 return STATUS_SUCCESS
;
134 NTSTATUS STDCALL
NtDelayExecution(IN ULONG Alertable
,
138 PLARGE_INTEGER IntervalP
;
140 IntervalP
= (PLARGE_INTEGER
)Interval
;
142 DPRINT("NtDelayExecution(Alertable %d, Internal %x) IntervalP %x\n",
143 Alertable
, Internal
, IntervalP
);
145 Status
= KeDelayExecutionThread(UserMode
, Alertable
, IntervalP
);
152 KeDelayExecutionThread (
153 KPROCESSOR_MODE WaitMode
,
155 PLARGE_INTEGER Interval
158 * FUNCTION: Puts the current thread into an alertable or nonalertable
159 * wait state for a given internal
161 * WaitMode = Processor mode in which the caller is waiting
162 * Altertable = Specifies if the wait is alertable
163 * Interval = Specifies the interval to wait
167 PKTHREAD CurrentThread
= KeGetCurrentThread();
168 KeAddThreadTimeout(CurrentThread
, Interval
);
169 return (KeWaitForSingleObject(&(CurrentThread
->Timer
),
178 KeQueryTimeIncrement (
182 * FUNCTION: Gets the increment (in 100-nanosecond units) that is added to
183 * the system clock every time the clock interrupts
184 * RETURNS: The increment
187 return(CLOCK_INCREMENT
);
193 PLARGE_INTEGER CurrentTime
196 * FUNCTION: Gets the current system time
198 * CurrentTime (OUT) = The routine stores the current time here
199 * NOTE: The time is the number of 100-nanosecond intervals since the
200 * 1st of January, 1601.
203 CurrentTime
->QuadPart
= system_time
;
213 LARGE_INTEGER TickCount
;
214 if ( UpTime
== NULL
)
215 return(STATUS_INVALID_PARAMETER
);
216 KeQueryTickCount(&TickCount
);
217 *UpTime
= TickCount
.u
.LowPart
;
218 return (STATUS_SUCCESS
);
226 LARGE_INTEGER DueTime
,
230 * FUNCTION: Sets the absolute or relative interval at which a timer object
231 * is to be set to the signaled state and optionally supplies a
232 * CustomTimerDpc to be executed when the timer expires.
234 * Timer = Points to a previously initialized timer object
235 * DueTimer = If positive then absolute time to expire at
236 * If negative then the relative time to expire at
237 * Dpc = If non-NULL then a dpc to be called when the timer expires
238 * RETURNS: True if the timer was already in the system timer queue
242 return(KeSetTimerEx(Timer
, DueTime
, 0, Dpc
));
249 LARGE_INTEGER DueTime
,
254 * FUNCTION: Sets the absolute or relative interval at which a timer object
255 * is to be set to the signaled state and optionally supplies a
256 * CustomTimerDpc to be executed when the timer expires.
258 * Timer = Points to a previously initialized timer object
259 * DueTimer = If positive then absolute time to expire at
260 * If negative then the relative time to expire at
261 * Dpc = If non-NULL then a dpc to be called when the timer expires
262 * RETURNS: True if the timer was already in the system timer queue
268 DPRINT("KeSetTimerEx(Timer %x)\n",Timer
);
270 KeRaiseIrql( HIGH_LEVEL
, &oldlvl
);
271 KeAcquireSpinLockAtDpcLevel(&TimerListLock
);
274 if (DueTime
.QuadPart
< 0)
276 Timer
->DueTime
.QuadPart
= system_time
+ (-(DueTime
.QuadPart
));
280 Timer
->DueTime
.QuadPart
= DueTime
.QuadPart
;
282 Timer
->Period
= Period
;
283 Timer
->Header
.SignalState
= FALSE
;
284 if (Timer
->TimerListEntry
.Flink
!= NULL
)
286 KeReleaseSpinLock(&TimerListLock
, oldlvl
);
289 InsertTailList(&TimerListHead
,&Timer
->TimerListEntry
);
290 KeReleaseSpinLock(&TimerListLock
, oldlvl
);
301 * FUNCTION: Removes a timer from the system timer list
303 * Timer = timer to cancel
304 * RETURNS: True if the timer was running
310 DPRINT("KeCancelTimer(Timer %x)\n",Timer
);
312 KeRaiseIrql( HIGH_LEVEL
, &oldlvl
);
313 KeAcquireSpinLockAtDpcLevel( &TimerListLock
);
315 if (Timer
->TimerListEntry
.Flink
== NULL
)
317 KeReleaseSpinLock(&TimerListLock
, oldlvl
);
320 RemoveEntryList(&Timer
->TimerListEntry
);
321 Timer
->TimerListEntry
.Flink
= Timer
->TimerListEntry
.Blink
= NULL
;
322 KeReleaseSpinLock(&TimerListLock
, oldlvl
);
333 return(Timer
->Header
.SignalState
);
342 * FUNCTION: Initalizes a kernel timer object
344 * Timer = caller supplied storage for the timer
345 * NOTE: This function initializes a notification timer
348 KeInitializeTimerEx(Timer
,NotificationTimer
);
353 KeInitializeTimerEx (
358 * FUNCTION: Initializes a kernel timer object
360 * Timer = caller supplied storage for the timer
361 * Type = the type of timer (notification or synchronization)
362 * NOTE: When a notification type expires all waiting threads are released
363 * and the timer remains signalled until it is explicitly reset. When a
364 * syncrhonization timer expires its state is set to signalled until a
365 * single waiting thread is released and then the timer is reset.
370 if (Type
== NotificationTimer
)
372 IType
= InternalNotificationTimer
;
374 else if (Type
== SynchronizationTimer
)
376 IType
= InternalSynchronizationTimer
;
384 KeInitializeDispatcherHeader(&Timer
->Header
,
386 sizeof(KTIMER
) / sizeof(ULONG
),
388 Timer
->TimerListEntry
.Flink
= Timer
->TimerListEntry
.Blink
= NULL
;
394 PLARGE_INTEGER TickCount
397 * FUNCTION: Returns the number of ticks since the system was booted
399 * TickCount (OUT) = Points to storage for the number of ticks
402 TickCount
->QuadPart
= KiTimerTicks
;
405 static void HandleExpiredTimer(PKTIMER current
)
407 DPRINT("HandleExpiredTime(current %x)\n",current
);
408 if (current
->Dpc
!= NULL
)
410 DPRINT("current->Dpc %x current->Dpc->DeferredRoutine %x\n",
411 current
->Dpc
, current
->Dpc
->DeferredRoutine
);
412 KeInsertQueueDpc(current
->Dpc
,
415 DPRINT("Finished dpc routine\n");
417 current
->Header
.SignalState
= TRUE
;
418 if (current
->Period
!= 0)
420 current
->DueTime
.QuadPart
+=
421 current
->Period
* SYSTEM_TIME_UNITS_PER_MSEC
;
425 RemoveEntryList(¤t
->TimerListEntry
);
426 current
->TimerListEntry
.Flink
= current
->TimerListEntry
.Blink
= NULL
;
430 VOID
KeExpireTimers(VOID
)
432 PLIST_ENTRY current_entry
= NULL
;
433 PKTIMER current
= NULL
;
436 DPRINT("KeExpireTimers()\n");
438 current_entry
= TimerListHead
.Flink
;
440 // DPRINT("&TimerListHead %x\n",&TimerListHead);
441 // DPRINT("current_entry %x\n",current_entry);
442 // DPRINT("current_entry->Flink %x\n",current_entry->Flink);
443 // DPRINT("current_entry->Flink->Flink %x\n",current_entry->Flink->Flink);
445 KeRaiseIrql(HIGH_LEVEL
, &oldlvl
);
446 KeAcquireSpinLockAtDpcLevel(&TimerListLock
);
448 while (current_entry
!=(&TimerListHead
))
450 current
= CONTAINING_RECORD(current_entry
, KTIMER
, TimerListEntry
);
452 current_entry
= current_entry
->Flink
;
454 if (system_time
>= current
->DueTime
.QuadPart
)
456 HandleExpiredTimer(current
);
460 KeReleaseSpinLock(&TimerListLock
, oldlvl
);
461 // DPRINT("Finished KeExpireTimers()\n");
465 VOID
KiUpdateSystemTime (VOID
)
467 * FUNCTION: Handles a timer interrupt
471 char* vidmem
=(char *)physical_to_linear(0xb8000 + 160 - 36);
474 // extern ULONG EiNrUsedBlocks;
475 extern unsigned int EiFreeNonPagedPool
;
476 extern unsigned int EiUsedNonPagedPool
;
477 extern ULONG PiNrThreads
;
478 // extern ULONG MiNrFreePages;
480 if (TimerInitDone
== FALSE
)
485 * Increment the number of timers ticks
488 system_time
= system_time
+ CLOCK_INCREMENT
;
491 * Display the tick count in the top left of the screen as a debugging
494 // sprintf(str,"%.8u %.8u",nr_used_blocks,ticks);
495 if ((EiFreeNonPagedPool
+ EiUsedNonPagedPool
) == 0)
501 x
= (EiFreeNonPagedPool
* 100) /
502 (EiFreeNonPagedPool
+ EiUsedNonPagedPool
);
503 y
= (EiUsedNonPagedPool
* 100) /
504 (EiFreeNonPagedPool
+ EiUsedNonPagedPool
);
506 // sprintf(str,"%.8u %.8u",EiFreeNonPagedPool,ticks);
507 memset(str
, 0, sizeof(str
));
508 // sprintf(str,"%.8u %.8u",(unsigned int)EiNrUsedBlocks,
509 // (unsigned int)EiFreeNonPagedPool);
510 // sprintf(str,"%.8u %.8u",EiFreeNonPagedPool,EiUsedNonPagedPool);
511 sprintf(str
,"%.8u %.8u",(unsigned int)PiNrRunnableThreads
,
512 (unsigned int)PiNrThreads
);
513 // sprintf(str,"%.8u %.8u", (unsigned int)PiNrRunnableThreads,
514 // (unsigned int)MiNrFreePages);
526 VOID
KeInitializeTimerImpl(VOID
)
528 * FUNCTION: Initializes timer irq handling
529 * NOTE: This is only called once from main()
532 TIME_FIELDS TimeFields
;
533 LARGE_INTEGER SystemBootTime
;
535 DPRINT("KeInitializeTimerImpl()\n");
537 InitializeListHead(&TimerListHead
);
538 KeInitializeSpinLock(&TimerListLock
);
540 TimerInitDone
= TRUE
;
543 * Calculate the starting time for the system clock
545 HalQueryRealTimeClock(&TimeFields
);
546 RtlTimeFieldsToTime(&TimeFields
, &SystemBootTime
);
547 boot_time
=SystemBootTime
.QuadPart
;
548 system_time
=boot_time
;
550 DPRINT("Finished KeInitializeTimerImpl()\n");