2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ke/apc.c
5 * PURPOSE: NT Implementation of APCs
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
11 /* INCLUDES *****************************************************************/
15 #include <internal/debug.h>
17 /* FUNCTIONS *****************************************************************/
20 * KiKernelApcDeliveryCheck
23 * The KiKernelApcDeliveryCheck routine is called whenever APCs have just
24 * been re-enabled in Kernel Mode, such as after leaving a Critical or
25 * Guarded Region. It delivers APCs if the environment is right.
34 * This routine allows KeLeave/EnterCritical/GuardedRegion to be used as a
35 * macro from inside WIN32K or other Drivers, which will then only have to
36 * do an Import API call in the case where APCs are enabled again.
41 KiKernelApcDeliveryCheck(VOID
)
43 /* We should only deliver at passive */
44 if (KeGetCurrentIrql() == PASSIVE_LEVEL
)
46 /* Raise to APC and Deliver APCs, then lower back to Passive */
47 KfRaiseIrql(APC_LEVEL
);
48 KiDeliverApc(KernelMode
, 0, 0);
49 KfLowerIrql(PASSIVE_LEVEL
);
54 * If we're not at passive level it means someone raised IRQL
55 * to APC level before the a critical or guarded section was entered
56 * (e.g) by a fast mutex). This implies that the APCs shouldn't
57 * be delivered now, but after the IRQL is lowered to passive
60 HalRequestSoftwareInterrupt(APC_LEVEL
);
65 * KeEnterCriticalRegion
68 * The KeEnterCriticalRegion routine temporarily disables the delivery of
69 * normal kernel APCs; special kernel-mode APCs are still delivered.
78 * Highest-level drivers can call this routine while running in the context
79 * of the thread that requested the current I/O operation. Any caller of
80 * this routine should call KeLeaveCriticalRegion as quickly as possible.
82 * Callers of KeEnterCriticalRegion must be running at IRQL <= APC_LEVEL.
85 #undef KeEnterCriticalRegion
88 KeEnterCriticalRegion(VOID
)
90 /* Disable Kernel APCs */
91 PKTHREAD Thread
= KeGetCurrentThread();
92 if (Thread
) Thread
->KernelApcDisable
--;
96 * KeLeaveCriticalRegion
99 * The KeLeaveCriticalRegion routine reenables the delivery of normal
100 * kernel-mode APCs that were disabled by a call to KeEnterCriticalRegion.
109 * Highest-level drivers can call this routine while running in the context
110 * of the thread that requested the current I/O operation.
112 * Callers of KeLeaveCriticalRegion must be running at IRQL <= DISPATCH_LEVEL.
115 #undef KeLeaveCriticalRegion
118 KeLeaveCriticalRegion (VOID
)
120 PKTHREAD Thread
= KeGetCurrentThread();
122 /* Check if Kernel APCs are now enabled */
123 if((Thread
) && (++Thread
->KernelApcDisable
== 0))
125 /* Check if we need to request an APC Delivery */
126 if (!IsListEmpty(&Thread
->ApcState
.ApcListHead
[KernelMode
]))
128 /* Check for the right environment */
129 KiKernelApcDeliveryCheck();
138 * The The KeInitializeApc routine initializes an APC object, and registers
139 * the Kernel, Rundown and Normal routines for that object.
142 * Apc - Pointer to a KAPC structure that represents the APC object to
143 * initialize. The caller must allocate storage for the structure
144 * from resident memory.
146 * Thread - Thread to which to deliver the APC.
148 * TargetEnvironment - APC Environment to be used.
150 * KernelRoutine - Points to the KernelRoutine to associate with the APC.
151 * This routine is executed for all APCs.
153 * RundownRoutine - Points to the RundownRoutine to associate with the APC.
154 * This routine is executed when the Thread exists with
157 * NormalRoutine - Points to the NormalRoutine to associate with the APC.
158 * This routine is executed at PASSIVE_LEVEL. If this is
159 * not specifed, the APC becomes a Special APC and the
160 * Mode and Context parameters are ignored.
162 * Mode - Specifies the processor mode at which to run the Normal Routine.
164 * Context - Specifices the value to pass as Context parameter to the
165 * registered routines.
171 * The caller can queue an initialized APC with KeInsertQueueApc.
173 * Storage for the APC object must be resident, such as nonpaged pool
174 * allocated by the caller.
176 * Callers of this routine must be running at IRQL = PASSIVE_LEVEL.
181 KeInitializeApc(IN PKAPC Apc
,
183 IN KAPC_ENVIRONMENT TargetEnvironment
,
184 IN PKKERNEL_ROUTINE KernelRoutine
,
185 IN PKRUNDOWN_ROUTINE RundownRoutine OPTIONAL
,
186 IN PKNORMAL_ROUTINE NormalRoutine
,
187 IN KPROCESSOR_MODE Mode
,
190 DPRINT("KeInitializeApc(Apc %x, Thread %x, Environment %d, "
191 "KernelRoutine %x, RundownRoutine %x, NormalRoutine %x, Mode %d, "
192 "Context %x)\n",Apc
,Thread
,TargetEnvironment
,KernelRoutine
,RundownRoutine
,
193 NormalRoutine
,Mode
,Context
);
195 /* Set up the basic APC Structure Data */
196 RtlZeroMemory(Apc
, sizeof(KAPC
));
197 Apc
->Type
= ApcObject
;
198 Apc
->Size
= sizeof(KAPC
);
200 /* Set the Environment */
201 if (TargetEnvironment
== CurrentApcEnvironment
) {
203 Apc
->ApcStateIndex
= Thread
->ApcStateIndex
;
207 Apc
->ApcStateIndex
= TargetEnvironment
;
210 /* Set the Thread and Routines */
211 Apc
->Thread
= Thread
;
212 Apc
->KernelRoutine
= KernelRoutine
;
213 Apc
->RundownRoutine
= RundownRoutine
;
214 Apc
->NormalRoutine
= NormalRoutine
;
216 /* Check if this is a Special APC, in which case we use KernelMode and no Context */
217 if (ARGUMENT_PRESENT(NormalRoutine
)) {
220 Apc
->NormalContext
= Context
;
224 Apc
->ApcMode
= KernelMode
;
231 * The KiInsertQueueApc routine queues a APC for execution when the right
232 * scheduler environment exists.
235 * Apc - Pointer to an initialized control object of type DPC for which the
236 * caller provides the storage.
238 * PriorityBoost - Priority Boost to apply to the Thread.
241 * If the APC is already inserted or APC queueing is disabled, FALSE.
245 * The APC will execute at APC_LEVEL for the KernelRoutine registered, and
246 * at PASSIVE_LEVEL for the NormalRoutine registered.
248 * Callers of this routine must be running at IRQL = PASSIVE_LEVEL.
253 KiInsertQueueApc(PKAPC Apc
,
254 KPRIORITY PriorityBoost
)
256 PKTHREAD Thread
= Apc
->Thread
;
257 PLIST_ENTRY ApcListEntry
;
260 /* Don't do anything if the APC is already inserted */
267 1) Kernel APC with Normal Routine or User APC = Put it at the end of the List
268 2) User APC which is PsExitSpecialApc = Put it at the front of the List
269 3) Kernel APC without Normal Routine = Put it at the end of the No-Normal Routine Kernel APC list
271 if ((Apc
->ApcMode
!= KernelMode
) && (Apc
->KernelRoutine
== (PKKERNEL_ROUTINE
)PsExitSpecialApc
)) {
273 DPRINT ("Inserting the Process Exit APC into the Queue\n");
274 Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->UserApcPending
= TRUE
;
275 InsertHeadList(&Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->ApcListHead
[(int)Apc
->ApcMode
],
278 } else if (Apc
->NormalRoutine
== NULL
) {
280 DPRINT ("Inserting Special APC %x into the Queue\n", Apc
);
282 for (ApcListEntry
= Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->ApcListHead
[(int)Apc
->ApcMode
].Flink
;
283 ApcListEntry
!= &Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->ApcListHead
[(int)Apc
->ApcMode
];
284 ApcListEntry
= ApcListEntry
->Flink
) {
286 QueuedApc
= CONTAINING_RECORD(ApcListEntry
, KAPC
, ApcListEntry
);
287 if (Apc
->NormalRoutine
!= NULL
) break;
290 /* We found the first "Normal" APC, so write right before it */
291 ApcListEntry
= ApcListEntry
->Blink
;
292 InsertHeadList(ApcListEntry
, &Apc
->ApcListEntry
);
296 DPRINT ("Inserting Normal APC %x into the %x Queue\n", Apc
, Apc
->ApcMode
);
297 InsertTailList(&Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->ApcListHead
[(int)Apc
->ApcMode
],
301 /* Confirm Insertion */
302 Apc
->Inserted
= TRUE
;
305 * Three possibilites here again:
306 * 1) Kernel APC, The thread is Running: Request an Interrupt
307 * 2) Kernel APC, The Thread is Waiting at PASSIVE_LEVEL and APCs are enabled and not in progress: Unwait the Thread
308 * 3) User APC, Unwait the Thread if it is alertable
310 if (Apc
->ApcMode
== KernelMode
) {
312 /* Set Kernel APC pending */
313 Thread
->ApcState
.KernelApcPending
= TRUE
;
315 /* Check the Thread State */
316 if (Thread
->State
== Running
) {
319 PKPRCB Prcb
, CurrentPrcb
;
324 DPRINT ("Requesting APC Interrupt for Running Thread \n");
327 oldIrql
= KeRaiseIrqlToDpcLevel();
328 CurrentPrcb
= KeGetCurrentPrcb();
329 if (CurrentPrcb
->CurrentThread
== Thread
)
331 HalRequestSoftwareInterrupt(APC_LEVEL
);
335 for (i
= 0; i
< KeNumberProcessors
; i
++)
337 Prcb
= ((PKPCR
)(KPCR_BASE
+ i
* PAGE_SIZE
))->Prcb
;
338 if (Prcb
->CurrentThread
== Thread
)
340 ASSERT (CurrentPrcb
!= Prcb
);
341 KiIpiSendRequest(Prcb
->SetMember
, IPI_REQUEST_APC
);
345 ASSERT (i
< KeNumberProcessors
);
347 KeLowerIrql(oldIrql
);
349 HalRequestSoftwareInterrupt(APC_LEVEL
);
352 } else if ((Thread
->State
== Waiting
) && (Thread
->WaitIrql
== PASSIVE_LEVEL
) &&
353 ((Apc
->NormalRoutine
== NULL
) ||
354 ((!Thread
->KernelApcDisable
) && (!Thread
->ApcState
.KernelApcInProgress
)))) {
356 DPRINT("Waking up Thread for Kernel-Mode APC Delivery \n");
357 KiAbortWaitThread(Thread
, STATUS_KERNEL_APC
, PriorityBoost
);
360 } else if ((Thread
->State
== Waiting
) &&
361 (Thread
->WaitMode
== UserMode
) &&
362 (Thread
->Alertable
)) {
364 DPRINT("Waking up Thread for User-Mode APC Delivery \n");
365 Thread
->ApcState
.UserApcPending
= TRUE
;
366 KiAbortWaitThread(Thread
, STATUS_USER_APC
, PriorityBoost
);
376 * The KeInsertQueueApc routine queues a APC for execution when the right
377 * scheduler environment exists.
380 * Apc - Pointer to an initialized control object of type DPC for which the
381 * caller provides the storage.
383 * SystemArgument[1,2] - Pointer to a set of two parameters that contain
386 * PriorityBoost - Priority Boost to apply to the Thread.
389 * If the APC is already inserted or APC queueing is disabled, FALSE.
393 * The APC will execute at APC_LEVEL for the KernelRoutine registered, and
394 * at PASSIVE_LEVEL for the NormalRoutine registered.
396 * Callers of this routine must be running at IRQL = PASSIVE_LEVEL.
401 KeInsertQueueApc(PKAPC Apc
,
402 PVOID SystemArgument1
,
403 PVOID SystemArgument2
,
404 KPRIORITY PriorityBoost
)
411 ASSERT_IRQL_LESS_OR_EQUAL(DISPATCH_LEVEL
);
412 DPRINT("KeInsertQueueApc(Apc %x, SystemArgument1 %x, "
413 "SystemArgument2 %x)\n",Apc
,SystemArgument1
,
416 /* Lock the Dispatcher Database */
417 OldIrql
= KeAcquireDispatcherDatabaseLock();
419 /* Get the Thread specified in the APC */
420 Thread
= Apc
->Thread
;
422 /* Make sure the thread allows APC Queues.
423 * The thread is not apc queueable, for instance, when it's (about to be) terminated.
425 if (Thread
->ApcQueueable
== FALSE
) {
426 DPRINT("Thread doesn't allow APC Queues\n");
427 KeReleaseDispatcherDatabaseLock(OldIrql
);
431 /* Set the System Arguments */
432 Apc
->SystemArgument1
= SystemArgument1
;
433 Apc
->SystemArgument2
= SystemArgument2
;
435 /* Call the Internal Function */
436 Inserted
= KiInsertQueueApc(Apc
, PriorityBoost
);
438 /* Return Sucess if we are here */
439 KeReleaseDispatcherDatabaseLock(OldIrql
);
446 * The KeRemoveQueueApc routine removes a given APC object from the system
450 * APC - Pointer to an initialized APC object that was queued by calling
454 * TRUE if the APC Object is in the APC Queue. If it isn't, no operation is
455 * performed and FALSE is returned.
458 * If the given APC Object is currently queued, it is removed from the queue
459 * and any calls to the registered routines are cancelled.
461 * Callers of KeLeaveCriticalRegion can be running at any IRQL.
466 KeRemoveQueueApc(PKAPC Apc
)
469 PKTHREAD Thread
= Apc
->Thread
;
471 ASSERT_IRQL_LESS_OR_EQUAL(DISPATCH_LEVEL
);
472 DPRINT("KeRemoveQueueApc called for APC: %x \n", Apc
);
474 OldIrql
= KeAcquireDispatcherDatabaseLock();
475 KeAcquireSpinLock(&Thread
->ApcQueueLock
, &OldIrql
);
477 /* Check if it's inserted */
480 /* Remove it from the Queue*/
481 RemoveEntryList(&Apc
->ApcListEntry
);
482 Apc
->Inserted
= FALSE
;
484 /* If the Queue is completely empty, then no more APCs are pending */
485 if (IsListEmpty(&Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->ApcListHead
[(int)Apc
->ApcMode
])) {
487 /* Set the correct State based on the Apc Mode */
488 if (Apc
->ApcMode
== KernelMode
) {
490 Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->KernelApcPending
= FALSE
;
494 Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->UserApcPending
= FALSE
;
500 /* It's not inserted, fail */
501 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
502 KeReleaseDispatcherDatabaseLock(OldIrql
);
506 /* Restore IRQL and Return */
507 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
508 KeReleaseDispatcherDatabaseLock(OldIrql
);
516 * The KiDeliverApc routine is called from IRQL switching code if the
517 * thread is returning from an IRQL >= APC_LEVEL and Kernel-Mode APCs are
521 * DeliveryMode - Specifies the current processor mode.
523 * Reserved - Pointer to the Exception Frame on non-i386 builds.
525 * TrapFrame - Pointer to the Trap Frame.
531 * First, Special APCs are delivered, followed by Kernel-Mode APCs and
532 * User-Mode APCs. Note that the TrapFrame is only valid if the previous
535 * Upon entry, this routine executes at APC_LEVEL.
540 KiDeliverApc(KPROCESSOR_MODE DeliveryMode
,
542 PKTRAP_FRAME TrapFrame
)
544 PKTHREAD Thread
= KeGetCurrentThread();
545 PLIST_ENTRY ApcListEntry
;
548 PKKERNEL_ROUTINE KernelRoutine
;
550 PKNORMAL_ROUTINE NormalRoutine
;
551 PVOID SystemArgument1
;
552 PVOID SystemArgument2
;
554 ASSERT_IRQL_EQUAL(APC_LEVEL
);
556 /* Lock the APC Queue and Raise IRQL to Synch */
557 KeAcquireSpinLock(&Thread
->ApcQueueLock
, &OldIrql
);
559 /* Clear APC Pending */
560 Thread
->ApcState
.KernelApcPending
= FALSE
;
562 /* Do the Kernel APCs first */
563 while (!IsListEmpty(&Thread
->ApcState
.ApcListHead
[KernelMode
])) {
565 /* Get the next Entry */
566 ApcListEntry
= Thread
->ApcState
.ApcListHead
[KernelMode
].Flink
;
567 Apc
= CONTAINING_RECORD(ApcListEntry
, KAPC
, ApcListEntry
);
569 /* Save Parameters so that it's safe to free the Object in Kernel Routine*/
570 NormalRoutine
= Apc
->NormalRoutine
;
571 KernelRoutine
= Apc
->KernelRoutine
;
572 NormalContext
= Apc
->NormalContext
;
573 SystemArgument1
= Apc
->SystemArgument1
;
574 SystemArgument2
= Apc
->SystemArgument2
;
577 if (NormalRoutine
== NULL
) {
579 /* Remove the APC from the list */
580 RemoveEntryList(ApcListEntry
);
581 Apc
->Inserted
= FALSE
;
583 /* Go back to APC_LEVEL */
584 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
586 /* Call the Special APC */
587 DPRINT("Delivering a Special APC: %x\n", Apc
);
594 /* Raise IRQL and Lock again */
595 KeAcquireSpinLock(&Thread
->ApcQueueLock
, &OldIrql
);
599 /* Normal Kernel APC */
600 if (Thread
->ApcState
.KernelApcInProgress
|| Thread
->KernelApcDisable
) {
603 * DeliveryMode must be KernelMode in this case, since one may not
604 * return to umode while being inside a critical section or while
605 * a regular kmode apc is running (the latter should be impossible btw).
608 ASSERT(DeliveryMode
== KernelMode
);
610 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
614 /* Dequeue the APC */
615 Apc
->Inserted
= FALSE
;
616 RemoveEntryList(ApcListEntry
);
618 /* Go back to APC_LEVEL */
619 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
621 /* Call the Kernel APC */
622 DPRINT("Delivering a Normal APC: %x\n", Apc
);
629 /* If There still is a Normal Routine, then we need to call this at PASSIVE_LEVEL */
630 if (NormalRoutine
!= NULL
) {
632 /* At Passive Level, this APC can be prempted by a Special APC */
633 Thread
->ApcState
.KernelApcInProgress
= TRUE
;
634 KeLowerIrql(PASSIVE_LEVEL
);
636 /* Call and Raise IRQ back to APC_LEVEL */
637 DPRINT("Calling the Normal Routine for a Normal APC: %x\n", Apc
);
638 NormalRoutine(NormalContext
, SystemArgument1
, SystemArgument2
);
639 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
642 /* Raise IRQL and Lock again */
643 KeAcquireSpinLock(&Thread
->ApcQueueLock
, &OldIrql
);
644 Thread
->ApcState
.KernelApcInProgress
= FALSE
;
648 /* Now we do the User APCs */
649 if ((!IsListEmpty(&Thread
->ApcState
.ApcListHead
[UserMode
])) &&
650 (DeliveryMode
== UserMode
) && (Thread
->ApcState
.UserApcPending
== TRUE
)) {
652 /* It's not pending anymore */
653 Thread
->ApcState
.UserApcPending
= FALSE
;
655 /* Get the APC Object */
656 ApcListEntry
= Thread
->ApcState
.ApcListHead
[UserMode
].Flink
;
657 Apc
= CONTAINING_RECORD(ApcListEntry
, KAPC
, ApcListEntry
);
659 /* Save Parameters so that it's safe to free the Object in Kernel Routine*/
660 NormalRoutine
= Apc
->NormalRoutine
;
661 KernelRoutine
= Apc
->KernelRoutine
;
662 NormalContext
= Apc
->NormalContext
;
663 SystemArgument1
= Apc
->SystemArgument1
;
664 SystemArgument2
= Apc
->SystemArgument2
;
666 /* Remove the APC from Queue, restore IRQL and call the APC */
667 RemoveEntryList(ApcListEntry
);
668 Apc
->Inserted
= FALSE
;
669 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
671 DPRINT("Calling the Kernel Routine for for a User APC: %x\n", Apc
);
678 if (NormalRoutine
== NULL
) {
680 /* Check if more User APCs are Pending */
681 KeTestAlertThread(UserMode
);
685 /* Set up the Trap Frame and prepare for Execution in NTDLL.DLL */
686 DPRINT("Delivering a User APC: %x\n", Apc
);
687 KiInitializeUserApc(Reserved
,
697 /* Go back to APC_LEVEL */
698 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
704 KiFreeApcRoutine(PKAPC Apc
,
705 PKNORMAL_ROUTINE
* NormalRoutine
,
706 PVOID
* NormalContext
,
707 PVOID
* SystemArgument1
,
708 PVOID
* SystemArgument2
)
710 /* Free the APC and do nothing else */
715 * KiInitializeUserApc
717 * Prepares the Context for a User-Mode APC called through NTDLL.DLL
720 * Reserved - Pointer to the Exception Frame on non-i386 builds.
722 * TrapFrame - Pointer to the Trap Frame.
724 * NormalRoutine - Pointer to the NormalRoutine to call.
726 * NormalContext - Pointer to the context to send to the Normal Routine.
728 * SystemArgument[1-2] - Pointer to a set of two parameters that contain
740 KiInitializeUserApc(IN PVOID Reserved
,
741 IN PKTRAP_FRAME TrapFrame
,
742 IN PKNORMAL_ROUTINE NormalRoutine
,
743 IN PVOID NormalContext
,
744 IN PVOID SystemArgument1
,
745 IN PVOID SystemArgument2
)
750 DPRINT("KiInitializeUserApc(TrapFrame %x/%x)\n", TrapFrame
, KeGetCurrentThread()->TrapFrame
);
753 * Save the thread's current context (in other words the registers
754 * that will be restored when it returns to user mode) so the
755 * APC dispatcher can restore them later
757 Context
= (PCONTEXT
)(((PUCHAR
)TrapFrame
->Esp
) - sizeof(CONTEXT
));
758 RtlZeroMemory(Context
, sizeof(CONTEXT
));
759 Context
->ContextFlags
= CONTEXT_FULL
;
760 Context
->SegGs
= TrapFrame
->Gs
;
761 Context
->SegFs
= TrapFrame
->Fs
;
762 Context
->SegEs
= TrapFrame
->Es
;
763 Context
->SegDs
= TrapFrame
->Ds
;
764 Context
->Edi
= TrapFrame
->Edi
;
765 Context
->Esi
= TrapFrame
->Esi
;
766 Context
->Ebx
= TrapFrame
->Ebx
;
767 Context
->Edx
= TrapFrame
->Edx
;
768 Context
->Ecx
= TrapFrame
->Ecx
;
769 Context
->Eax
= TrapFrame
->Eax
;
770 Context
->Ebp
= TrapFrame
->Ebp
;
771 Context
->Eip
= TrapFrame
->Eip
;
772 Context
->SegCs
= TrapFrame
->Cs
;
773 Context
->EFlags
= TrapFrame
->Eflags
;
774 Context
->Esp
= TrapFrame
->Esp
;
775 Context
->SegSs
= TrapFrame
->Ss
;
778 * Setup the trap frame so the thread will start executing at the
779 * APC Dispatcher when it returns to user-mode
781 Esp
= (PULONG
)(((PUCHAR
)TrapFrame
->Esp
) - (sizeof(CONTEXT
) + (6 * sizeof(ULONG
))));
783 Esp
[1] = (ULONG
)NormalRoutine
;
784 Esp
[2] = (ULONG
)NormalContext
;
785 Esp
[3] = (ULONG
)SystemArgument1
;
786 Esp
[4] = (ULONG
)SystemArgument2
;
787 Esp
[5] = (ULONG
)Context
;
788 TrapFrame
->Eip
= (ULONG
)LdrpGetSystemDllApcDispatcher();
789 DPRINT("TrapFrame->Eip: %x\n", TrapFrame
->Eip
);
790 TrapFrame
->Esp
= (ULONG
)Esp
;
797 * Prepares the Context for a User-Mode APC called through NTDLL.DLL
803 * KeAreApcsDisabled returns TRUE if the thread is within a critical region
804 * or a guarded region, and FALSE otherwise.
807 * A thread running at IRQL = PASSIVE_LEVEL can use KeAreApcsDisabled to
808 * determine if normal kernel APCs are disabled. A thread that is inside a
809 * critical region has both user APCs and normal kernel APCs disabled, but
810 * not special kernel APCs. A thread that is inside a guarded region has
811 * all APCs disabled, including special kernel APCs.
813 * Callers of this routine must be running at IRQL <= APC_LEVEL.
818 KeAreApcsDisabled(VOID
)
820 /* Return the Kernel APC State */
821 return KeGetCurrentThread()->KernelApcDisable
? TRUE
: FALSE
;
828 * This routine is used to queue an APC from user-mode for the specified
832 * Thread Handle - Handle to the Thread. This handle must have THREAD_SET_CONTEXT privileges.
834 * ApcRoutine - Pointer to the APC Routine to call when the APC executes.
836 * NormalContext - Pointer to the context to send to the Normal Routine.
838 * SystemArgument[1-2] - Pointer to a set of two parameters that contain
842 * STATUS_SUCCESS or failure cute from associated calls.
845 * The thread must enter an alertable wait before the APC will be
851 NtQueueApcThread(HANDLE ThreadHandle
,
852 PKNORMAL_ROUTINE ApcRoutine
,
854 PVOID SystemArgument1
,
855 PVOID SystemArgument2
)
859 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
862 /* Get ETHREAD from Handle */
863 Status
= ObReferenceObjectByHandle(ThreadHandle
,
870 /* Fail if the Handle is invalid for some reason */
871 if (!NT_SUCCESS(Status
)) {
876 /* If this is a Kernel or System Thread, then fail */
877 if (Thread
->Tcb
.Teb
== NULL
) {
879 ObDereferenceObject(Thread
);
880 return STATUS_INVALID_HANDLE
;
883 /* Allocate an APC */
884 Apc
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(KAPC
), TAG('P', 's', 'a', 'p'));
887 ObDereferenceObject(Thread
);
888 return(STATUS_NO_MEMORY
);
891 /* Initialize and Queue a user mode apc (always!) */
894 OriginalApcEnvironment
,
901 if (!KeInsertQueueApc(Apc
, SystemArgument1
, SystemArgument2
, IO_NO_INCREMENT
)) {
903 Status
= STATUS_UNSUCCESSFUL
;
907 Status
= STATUS_SUCCESS
;
910 /* Dereference Thread and Return */
911 ObDereferenceObject(Thread
);
916 VOID
RepairList(PLIST_ENTRY Original
,
918 KPROCESSOR_MODE Mode
)
920 /* Copy Source to Desination */
921 if (IsListEmpty(&Original
[(int)Mode
])) {
923 InitializeListHead(&Copy
[(int)Mode
]);
927 Copy
[(int)Mode
].Flink
= Original
[(int)Mode
].Flink
;
928 Copy
[(int)Mode
].Blink
= Original
[(int)Mode
].Blink
;
929 Original
[(int)Mode
].Flink
->Blink
= &Copy
[(int)Mode
];
930 Original
[(int)Mode
].Blink
->Flink
= &Copy
[(int)Mode
];
936 KiMoveApcState(PKAPC_STATE OldState
,
937 PKAPC_STATE NewState
)
939 /* Restore backup of Original Environment */
940 *NewState
= *OldState
;
943 RepairList(NewState
->ApcListHead
, OldState
->ApcListHead
, KernelMode
);
944 RepairList(NewState
->ApcListHead
, OldState
->ApcListHead
, UserMode
);