From 331c5bfeda4fca4e37995afbba17d8b4ce968d7b Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Sat, 20 Apr 2024 23:15:57 +0200 Subject: [PATCH] Rework initial timer support --- sdk/xtdk/kefuncs.h | 12 +++---- xtoskrnl/includes/kei.h | 12 +++++++ xtoskrnl/ke/kthread.c | 22 +++++++++++- xtoskrnl/ke/timer.c | 79 +++++++++++++++++++++++++++++++++++------ xtoskrnl/po/idle.c | 2 +- xtoskrnl/xtoskrnl.spec | 4 +-- 6 files changed, 111 insertions(+), 20 deletions(-) diff --git a/sdk/xtdk/kefuncs.h b/sdk/xtdk/kefuncs.h index bb9ba46..ac2abe7 100644 --- a/sdk/xtdk/kefuncs.h +++ b/sdk/xtdk/kefuncs.h @@ -24,6 +24,10 @@ XTFASTCALL VOID KeAcquireSpinLock(IN OUT PKSPIN_LOCK SpinLock); +XTAPI +BOOLEAN +KeCancelTimer(IN PKTIMER Timer); + XTFASTCALL KRUNLEVEL KeGetCurrentRunLevel(VOID); @@ -63,12 +67,8 @@ KeInitializeThreadedDpc(IN PKDPC Dpc, XTAPI VOID -KeInitializeTimer(OUT PKTIMER Timer); - -XTAPI -VOID -KeInitializeTimerEx(OUT PKTIMER Timer, - IN KTIMER_TYPE Type); +KeInitializeTimer(OUT PKTIMER Timer, + IN KTIMER_TYPE Type); XTFASTCALL VOID diff --git a/xtoskrnl/includes/kei.h b/xtoskrnl/includes/kei.h index 39bddfd..c452ea7 100644 --- a/xtoskrnl/includes/kei.h +++ b/xtoskrnl/includes/kei.h @@ -17,6 +17,10 @@ XTAPI VOID KeClearEvent(IN PKEVENT Event); +XTAPI +VOID +KeClearTimer(IN PKTIMER Timer); + XTAPI VOID KeHaltSystem(VOID); @@ -78,6 +82,14 @@ XTAPI VOID KeStartXtSystem(IN PKERNEL_INITIALIZATION_BLOCK Parameters); +XTFASTCALL +VOID +KepExitDispatcher(IN KRUNLEVEL OldRunLevel); + +XTAPI +VOID +KepRemoveTimer(IN OUT PKTIMER Timer); + XTFASTCALL VOID KepRetireDpcList(IN PKPROCESSOR_CONTROL_BLOCK Prcb); diff --git a/xtoskrnl/ke/kthread.c b/xtoskrnl/ke/kthread.c index 8485d0b..d5c6385 100644 --- a/xtoskrnl/ke/kthread.c +++ b/xtoskrnl/ke/kthread.c @@ -113,7 +113,7 @@ KeInitializeThread(IN PKPROCESS Process, KeInitializeSemaphore(&Thread->SuspendSemaphore, 0, 2); /* Initialize the builtin timer */ - KeInitializeTimer(&Thread->Timer); + KeInitializeTimer(&Thread->Timer, NotificationTimer); TimerWaitBlock = &Thread->WaitBlock[KTIMER_WAIT_BLOCK]; TimerWaitBlock->Object = &Thread->Timer; TimerWaitBlock->WaitKey = STATUS_TIMEOUT; @@ -194,6 +194,26 @@ KeStartThread(IN PKTHREAD Thread) UNIMPLEMENTED; } +/** + * Exits the dispatcher, switches context to a new thread and lowers runlevel to its original state. + * + * @param OldRunLevel + * Supplies the original runlevel state. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTFASTCALL +VOID +KepExitDispatcher(IN KRUNLEVEL OldRunLevel) +{ + UNIMPLEMENTED; + + /* Lower runlevel */ + KeLowerRunLevel(OldRunLevel); +} + /** * Suspend APC-built thread NOP routine. It takes no actions. * diff --git a/xtoskrnl/ke/timer.c b/xtoskrnl/ke/timer.c index 2ae2a96..f6b408c 100644 --- a/xtoskrnl/ke/timer.c +++ b/xtoskrnl/ke/timer.c @@ -10,21 +10,61 @@ /** - * Initializes a kernel timer. + * Cancels the timer. * * @param Timer - * Supplies a pointer to a time object. + * Supplies a pointer to a timer object. * - * @return This routine does not return any value. + * @return This routine returns TRUE if the cancelled timer was set, or FALSE otherwise. * * @since NT 3.5 */ XTAPI -VOID -KeInitializeTimer(OUT PKTIMER Timer) +BOOLEAN +KeCancelTimer(IN PKTIMER Timer) { - /* Initialize the timer */ - KeInitializeTimerEx(Timer, NotificationTimer); + BOOLEAN Result; + KRUNLEVEL OldRunLevel; + + /* Set default result value */ + Result = FALSE; + + /* Raise run level and acquire dispatcher lock */ + OldRunLevel = KeRaiseRunLevel(SYNC_LEVEL); + KeAcquireQueuedSpinLock(DispatcherLock); + + /* Check timer status */ + if(Timer->Header.Inserted) + { + /* Remove the timer from the list */ + KepRemoveTimer(Timer); + Result = TRUE; + } + + /* Release dispatcher lock and processes the deferred ready list */ + KeReleaseQueuedSpinLock(DispatcherLock); + KepExitDispatcher(OldRunLevel); + + /* Return result */ + return Result; +} + +/** + * Clears the signal state of the timer. + * + * @param Timer + * Supplies a pointer to a timer object. + * + * @return This routine does not return any value. + * + * @since NT 4.0 + */ +XTAPI +VOID +KeClearTimer(IN PKTIMER Timer) +{ + /* Clear signal state */ + Timer->Header.SignalState = 0; } /** @@ -38,12 +78,12 @@ KeInitializeTimer(OUT PKTIMER Timer) * * @return This routine does not return any value. * - * @since NT 3.5 + * @since XT 1.0 */ XTAPI VOID -KeInitializeTimerEx(OUT PKTIMER Timer, - IN KTIMER_TYPE Type) +KeInitializeTimer(OUT PKTIMER Timer, + IN KTIMER_TYPE Type) { /* Initialize the header */ Timer->Header.Type = TimerNotificationObject + Type; @@ -58,3 +98,22 @@ KeInitializeTimerEx(OUT PKTIMER Timer, RtlInitializeListHead(&Timer->Header.WaitListHead); RtlInitializeListHead(&Timer->TimerListEntry); } + +/** + * Removes a specified timer from the timer list. + * + * @param Timer + * Supplies a pointer to a timer object. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +KepRemoveTimer(IN OUT PKTIMER Timer) +{ + /* Remove the timer from the list */ + Timer->Header.Inserted = FALSE; + RtlRemoveEntryList(&Timer->TimerListEntry); +} diff --git a/xtoskrnl/po/idle.c b/xtoskrnl/po/idle.c index cb4d1b8..31d3796 100644 --- a/xtoskrnl/po/idle.c +++ b/xtoskrnl/po/idle.c @@ -34,7 +34,7 @@ PoInitializeProcessorControlBlock(IN OUT PKPROCESSOR_CONTROL_BLOCK Prcb) /* Initialize DPC and Timer */ KeInitializeDpc(&Prcb->PowerState.PerfDpc, PopPerfIdleDpc, Prcb); KeSetTargetProcessorDpc(&Prcb->PowerState.PerfDpc, Prcb->Number); - KeInitializeTimerEx(&Prcb->PowerState.PerfTimer, SynchronizationTimer); + KeInitializeTimer(&Prcb->PowerState.PerfTimer, SynchronizationTimer); } /** diff --git a/xtoskrnl/xtoskrnl.spec b/xtoskrnl/xtoskrnl.spec index 1cc1c1a..c256988 100644 --- a/xtoskrnl/xtoskrnl.spec +++ b/xtoskrnl/xtoskrnl.spec @@ -13,14 +13,14 @@ @ cdecl HlIoPortOutShort(ptr long) @ fastcall KeAcquireQueuedSpinLock(long) @ fastcall KeAcquireSpinLock(ptr) +@ stdcall KeCancelTimer(ptr) @ fastcall KeGetCurrentRunLevel() @ stdcall KeInitializeApc(ptr ptr long ptr ptr ptr long ptr) @ stdcall KeInitializeDpc(ptr ptr ptr) @ stdcall KeInitializeSemaphore(ptr long long) @ stdcall KeInitializeSpinLock(ptr) @ stdcall KeInitializeThreadedDpc(ptr ptr ptr) -@ stdcall KeInitializeTimer(ptr) -@ stdcall KeInitializeTimerEx(ptr long) +@ stdcall KeInitializeTimer(ptr long) @ fastcall KeLowerRunLevel(long) @ fastcall KeRaiseRunLevel(long) @ stdcall KeReadSemaphoreState(ptr)