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 /* GLOBALS *******************************************************************/
19 VOID
PsTerminateCurrentThread(NTSTATUS ExitStatus
);
21 /* FUNCTIONS *****************************************************************/
24 * KeEnterCriticalRegion
27 * The KeEnterCriticalRegion routine temporarily disables the delivery of
28 * normal kernel APCs; special kernel-mode APCs are still delivered.
37 * Highest-level drivers can call this routine while running in the context
38 * of the thread that requested the current I/O operation. Any caller of
39 * this routine should call KeLeaveCriticalRegion as quickly as possible.
41 * Callers of KeEnterCriticalRegion must be running at IRQL <= APC_LEVEL.
46 KeEnterCriticalRegion(VOID
)
48 /* Disable Kernel APCs */
49 PKTHREAD Thread
= KeGetCurrentThread();
50 if (Thread
) Thread
->KernelApcDisable
--;
57 * The The KeInitializeApc routine initializes an APC object, and registers
58 * the Kernel, Rundown and Normal routines for that object.
61 * Apc - Pointer to a KAPC structure that represents the APC object to
62 * initialize. The caller must allocate storage for the structure
63 * from resident memory.
65 * Thread - Thread to which to deliver the APC.
67 * TargetEnvironment - APC Environment to be used.
69 * KernelRoutine - Points to the KernelRoutine to associate with the APC.
70 * This routine is executed for all APCs.
72 * RundownRoutine - Points to the RundownRoutine to associate with the APC.
73 * This routine is executed when the Thread exists with
76 * NormalRoutine - Points to the NormalRoutine to associate with the APC.
77 * This routine is executed at PASSIVE_LEVEL. If this is
78 * not specifed, the APC becomes a Special APC and the
79 * Mode and Context parameters are ignored.
81 * Mode - Specifies the processor mode at which to run the Normal Routine.
83 * Context - Specifices the value to pass as Context parameter to the
84 * registered routines.
90 * The caller can queue an initialized APC with KeInsertQueueApc.
92 * Storage for the APC object must be resident, such as nonpaged pool
93 * allocated by the caller.
95 * Callers of this routine must be running at IRQL = PASSIVE_LEVEL.
100 KeInitializeApc(IN PKAPC Apc
,
102 IN KAPC_ENVIRONMENT TargetEnvironment
,
103 IN PKKERNEL_ROUTINE KernelRoutine
,
104 IN PKRUNDOWN_ROUTINE RundownRoutine OPTIONAL
,
105 IN PKNORMAL_ROUTINE NormalRoutine
,
106 IN KPROCESSOR_MODE Mode
,
109 DPRINT("KeInitializeApc(Apc %x, Thread %x, Environment %d, "
110 "KernelRoutine %x, RundownRoutine %x, NormalRoutine %x, Mode %d, "
111 "Context %x)\n",Apc
,Thread
,TargetEnvironment
,KernelRoutine
,RundownRoutine
,
112 NormalRoutine
,Mode
,Context
);
114 /* Set up the basic APC Structure Data */
115 RtlZeroMemory(Apc
, sizeof(KAPC
));
116 Apc
->Type
= ApcObject
;
117 Apc
->Size
= sizeof(KAPC
);
119 /* Set the Environment */
120 if (TargetEnvironment
== CurrentApcEnvironment
) {
122 Apc
->ApcStateIndex
= Thread
->ApcStateIndex
;
126 Apc
->ApcStateIndex
= TargetEnvironment
;
129 /* Set the Thread and Routines */
130 Apc
->Thread
= Thread
;
131 Apc
->KernelRoutine
= KernelRoutine
;
132 Apc
->RundownRoutine
= RundownRoutine
;
133 Apc
->NormalRoutine
= NormalRoutine
;
135 /* Check if this is a Special APC, in which case we use KernelMode and no Context */
136 if (ARGUMENT_PRESENT(NormalRoutine
)) {
139 Apc
->NormalContext
= Context
;
143 Apc
->ApcMode
= KernelMode
;
151 * The KeInsertQueueApc routine queues a APC for execution when the right
152 * scheduler environment exists.
155 * Apc - Pointer to an initialized control object of type DPC for which the
156 * caller provides the storage.
158 * SystemArgument[1,2] - Pointer to a set of two parameters that contain
161 * PriorityBoost - Priority Boost to apply to the Thread.
164 * If the APC is already inserted or APC queueing is disabled, FALSE.
168 * The APC will execute at APC_LEVEL for the KernelRoutine registered, and
169 * at PASSIVE_LEVEL for the NormalRoutine registered.
171 * Callers of this routine must be running at IRQL = PASSIVE_LEVEL.
176 KeInsertQueueApc(PKAPC Apc
,
177 PVOID SystemArgument1
,
178 PVOID SystemArgument2
,
179 KPRIORITY PriorityBoost
)
184 PLIST_ENTRY ApcListEntry
;
187 ASSERT_IRQL_LESS_OR_EQUAL(DISPATCH_LEVEL
);
188 DPRINT("KeInsertQueueApc(Apc %x, SystemArgument1 %x, "
189 "SystemArgument2 %x)\n",Apc
,SystemArgument1
,
192 /* Lock the Dispatcher Database */
193 OldIrql
= KeAcquireDispatcherDatabaseLock();
195 /* Get the Thread specified in the APC */
196 Thread
= Apc
->Thread
;
198 /* Make sure the thread allows APC Queues.
199 * The thread is not apc queueable, for instance, when it's (about to be) terminated.
201 if (Thread
->ApcQueueable
== FALSE
) {
202 DPRINT("Thread doesn't allow APC Queues\n");
203 KeReleaseDispatcherDatabaseLock(OldIrql
);
207 /* Set the System Arguments */
208 Apc
->SystemArgument1
= SystemArgument1
;
209 Apc
->SystemArgument2
= SystemArgument2
;
211 /* Don't do anything if the APC is already inserted */
214 KeReleaseDispatcherDatabaseLock(OldIrql
);
219 1) Kernel APC with Normal Routine or User APC = Put it at the end of the List
220 2) User APC which is PsExitSpecialApc = Put it at the front of the List
221 3) Kernel APC without Normal Routine = Put it at the end of the No-Normal Routine Kernel APC list
223 if ((Apc
->ApcMode
!= KernelMode
) && (Apc
->KernelRoutine
== (PKKERNEL_ROUTINE
)PsExitSpecialApc
)) {
225 DPRINT ("Inserting the Process Exit APC into the Queue\n");
226 Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->UserApcPending
= TRUE
;
227 InsertHeadList(&Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->ApcListHead
[(int)Apc
->ApcMode
],
230 } else if (Apc
->NormalRoutine
== NULL
) {
232 DPRINT ("Inserting Special APC %x into the Queue\n", Apc
);
234 for (ApcListEntry
= Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->ApcListHead
[(int)Apc
->ApcMode
].Flink
;
235 ApcListEntry
!= &Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->ApcListHead
[(int)Apc
->ApcMode
];
236 ApcListEntry
= ApcListEntry
->Flink
) {
238 QueuedApc
= CONTAINING_RECORD(ApcListEntry
, KAPC
, ApcListEntry
);
239 if (Apc
->NormalRoutine
!= NULL
) break;
242 /* We found the first "Normal" APC, so write right before it */
243 ApcListEntry
= ApcListEntry
->Blink
;
244 InsertHeadList(ApcListEntry
, &Apc
->ApcListEntry
);
248 DPRINT ("Inserting Normal APC %x into the %x Queue\n", Apc
, Apc
->ApcMode
);
249 InsertTailList(&Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->ApcListHead
[(int)Apc
->ApcMode
],
253 /* Confirm Insertion */
254 Apc
->Inserted
= TRUE
;
257 * Three possibilites here again:
258 * 1) Kernel APC, The thread is Running: Request an Interrupt
259 * 2) Kernel APC, The Thread is Waiting at PASSIVE_LEVEL and APCs are enabled and not in progress: Unwait the Thread
260 * 3) User APC, Unwait the Thread if it is alertable
262 if (Apc
->ApcMode
== KernelMode
) {
264 /* Set Kernel APC pending */
265 Thread
->ApcState
.KernelApcPending
= TRUE
;
267 /* Check the Thread State */
268 if (Thread
->State
== THREAD_STATE_RUNNING
) {
271 DPRINT ("Requesting APC Interrupt for Running Thread \n");
272 HalRequestSoftwareInterrupt(APC_LEVEL
);
274 } else if ((Thread
->State
== THREAD_STATE_BLOCKED
) &&
275 (Thread
->WaitIrql
< APC_LEVEL
) &&
276 (Apc
->NormalRoutine
== NULL
)) {
278 DPRINT("Waking up Thread for Kernel-Mode APC Delivery \n");
279 KiAbortWaitThread(Thread
, STATUS_KERNEL_APC
, PriorityBoost
);
282 } else if ((Thread
->State
== THREAD_STATE_BLOCKED
) &&
283 (Thread
->WaitMode
== UserMode
) &&
284 (Thread
->Alertable
)) {
286 DPRINT("Waking up Thread for User-Mode APC Delivery \n");
287 Thread
->ApcState
.UserApcPending
= TRUE
;
288 KiAbortWaitThread(Thread
, STATUS_USER_APC
, PriorityBoost
);
291 /* Return Sucess if we are here */
292 KeReleaseDispatcherDatabaseLock(OldIrql
);
297 * KeLeaveCriticalRegion
300 * The KeLeaveCriticalRegion routine reenables the delivery of normal
301 * kernel-mode APCs that were disabled by a call to KeEnterCriticalRegion.
310 * Highest-level drivers can call this routine while running in the context
311 * of the thread that requested the current I/O operation.
313 * Callers of KeLeaveCriticalRegion must be running at IRQL <= DISPATCH_LEVEL.
318 KeLeaveCriticalRegion (VOID
)
320 PKTHREAD Thread
= KeGetCurrentThread();
322 /* Check if Kernel APCs are now enabled */
323 if((Thread
) && (++Thread
->KernelApcDisable
== 0)) {
325 /* Check if we need to request an APC Delivery */
326 if (!IsListEmpty(&Thread
->ApcState
.ApcListHead
[KernelMode
])) {
328 /* Set APC Pending */
329 Thread
->ApcState
.KernelApcPending
= TRUE
;
330 HalRequestSoftwareInterrupt(APC_LEVEL
);
338 * The KeRemoveQueueApc routine removes a given APC object from the system
342 * APC - Pointer to an initialized APC object that was queued by calling
346 * TRUE if the APC Object is in the APC Queue. If it isn't, no operation is
347 * performed and FALSE is returned.
350 * If the given APC Object is currently queued, it is removed from the queue
351 * and any calls to the registered routines are cancelled.
353 * Callers of KeLeaveCriticalRegion can be running at any IRQL.
358 KeRemoveQueueApc(PKAPC Apc
)
361 PKTHREAD Thread
= Apc
->Thread
;
363 ASSERT_IRQL_LESS_OR_EQUAL(DISPATCH_LEVEL
);
364 DPRINT("KeRemoveQueueApc called for APC: %x \n", Apc
);
366 OldIrql
= KeAcquireDispatcherDatabaseLock();
367 KeAcquireSpinLock(&Thread
->ApcQueueLock
, &OldIrql
);
369 /* Check if it's inserted */
372 /* Remove it from the Queue*/
373 RemoveEntryList(&Apc
->ApcListEntry
);
374 Apc
->Inserted
= FALSE
;
376 /* If the Queue is completely empty, then no more APCs are pending */
377 if (IsListEmpty(&Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->ApcListHead
[(int)Apc
->ApcMode
])) {
379 /* Set the correct State based on the Apc Mode */
380 if (Apc
->ApcMode
== KernelMode
) {
382 Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->KernelApcPending
= FALSE
;
386 Thread
->ApcStatePointer
[(int)Apc
->ApcStateIndex
]->UserApcPending
= FALSE
;
392 /* It's not inserted, fail */
393 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
394 KeReleaseDispatcherDatabaseLock(OldIrql
);
398 /* Restore IRQL and Return */
399 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
400 KeReleaseDispatcherDatabaseLock(OldIrql
);
408 * The KiDeliverApc routine is called from IRQL switching code if the
409 * thread is returning from an IRQL >= APC_LEVEL and Kernel-Mode APCs are
413 * DeliveryMode - Specifies the current processor mode.
415 * Reserved - Pointer to the Exception Frame on non-i386 builds.
417 * TrapFrame - Pointer to the Trap Frame.
423 * First, Special APCs are delivered, followed by Kernel-Mode APCs and
424 * User-Mode APCs. Note that the TrapFrame is only valid if the previous
427 * Upon entry, this routine executes at APC_LEVEL.
432 KiDeliverApc(KPROCESSOR_MODE DeliveryMode
,
434 PKTRAP_FRAME TrapFrame
)
436 PKTHREAD Thread
= KeGetCurrentThread();
437 PLIST_ENTRY ApcListEntry
;
440 PKKERNEL_ROUTINE KernelRoutine
;
442 PKNORMAL_ROUTINE NormalRoutine
;
443 PVOID SystemArgument1
;
444 PVOID SystemArgument2
;
446 ASSERT_IRQL_EQUAL(APC_LEVEL
);
448 /* Lock the APC Queue and Raise IRQL to Synch */
449 KeAcquireSpinLock(&Thread
->ApcQueueLock
, &OldIrql
);
451 /* Clear APC Pending */
452 Thread
->ApcState
.KernelApcPending
= FALSE
;
454 /* Do the Kernel APCs first */
455 while (!IsListEmpty(&Thread
->ApcState
.ApcListHead
[KernelMode
])) {
457 /* Get the next Entry */
458 ApcListEntry
= Thread
->ApcState
.ApcListHead
[KernelMode
].Flink
;
459 Apc
= CONTAINING_RECORD(ApcListEntry
, KAPC
, ApcListEntry
);
461 /* Save Parameters so that it's safe to free the Object in Kernel Routine*/
462 NormalRoutine
= Apc
->NormalRoutine
;
463 KernelRoutine
= Apc
->KernelRoutine
;
464 NormalContext
= Apc
->NormalContext
;
465 SystemArgument1
= Apc
->SystemArgument1
;
466 SystemArgument2
= Apc
->SystemArgument2
;
469 if (NormalRoutine
== NULL
) {
471 /* Remove the APC from the list */
472 Apc
->Inserted
= FALSE
;
473 RemoveEntryList(ApcListEntry
);
475 /* Go back to APC_LEVEL */
476 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
478 /* Call the Special APC */
479 DPRINT("Delivering a Special APC: %x\n", Apc
);
486 /* Raise IRQL and Lock again */
487 KeAcquireSpinLock(&Thread
->ApcQueueLock
, &OldIrql
);
491 /* Normal Kernel APC */
492 if (Thread
->ApcState
.KernelApcInProgress
|| Thread
->KernelApcDisable
) {
495 * DeliveryMode must be KernelMode in this case, since one may not
496 * return to umode while being inside a critical section or while
497 * a regular kmode apc is running (the latter should be impossible btw).
500 ASSERT(DeliveryMode
== KernelMode
);
502 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
506 /* Dequeue the APC */
507 RemoveEntryList(ApcListEntry
);
508 Apc
->Inserted
= FALSE
;
510 /* Go back to APC_LEVEL */
511 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
513 /* Call the Kernel APC */
514 DPRINT("Delivering a Normal APC: %x\n", Apc
);
521 /* If There still is a Normal Routine, then we need to call this at PASSIVE_LEVEL */
522 if (NormalRoutine
!= NULL
) {
524 /* At Passive Level, this APC can be prempted by a Special APC */
525 Thread
->ApcState
.KernelApcInProgress
= TRUE
;
526 KeLowerIrql(PASSIVE_LEVEL
);
528 /* Call and Raise IRQ back to APC_LEVEL */
529 DPRINT("Calling the Normal Routine for a Normal APC: %x\n", Apc
);
530 NormalRoutine(&NormalContext
, &SystemArgument1
, &SystemArgument2
);
531 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
534 /* Raise IRQL and Lock again */
535 KeAcquireSpinLock(&Thread
->ApcQueueLock
, &OldIrql
);
536 Thread
->ApcState
.KernelApcInProgress
= FALSE
;
540 /* Now we do the User APCs */
541 if ((!IsListEmpty(&Thread
->ApcState
.ApcListHead
[UserMode
])) &&
542 (DeliveryMode
== UserMode
) && (Thread
->ApcState
.UserApcPending
== TRUE
)) {
544 /* It's not pending anymore */
545 Thread
->ApcState
.UserApcPending
= FALSE
;
547 /* Get the APC Object */
548 ApcListEntry
= Thread
->ApcState
.ApcListHead
[UserMode
].Flink
;
549 Apc
= CONTAINING_RECORD(ApcListEntry
, KAPC
, ApcListEntry
);
551 /* Save Parameters so that it's safe to free the Object in Kernel Routine*/
552 NormalRoutine
= Apc
->NormalRoutine
;
553 KernelRoutine
= Apc
->KernelRoutine
;
554 NormalContext
= Apc
->NormalContext
;
555 SystemArgument1
= Apc
->SystemArgument1
;
556 SystemArgument2
= Apc
->SystemArgument2
;
558 /* Remove the APC from Queue, restore IRQL and call the APC */
559 RemoveEntryList(ApcListEntry
);
560 Apc
->Inserted
= FALSE
;
562 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
563 DPRINT("Calling the Kernel Routine for for a User APC: %x\n", Apc
);
570 if (NormalRoutine
== NULL
) {
572 /* Check if more User APCs are Pending */
573 KeTestAlertThread(UserMode
);
577 /* Set up the Trap Frame and prepare for Execution in NTDLL.DLL */
578 DPRINT("Delivering a User APC: %x\n", Apc
);
579 KiInitializeUserApc(Reserved
,
589 /* Go back to APC_LEVEL */
590 KeReleaseSpinLock(&Thread
->ApcQueueLock
, OldIrql
);
596 KiFreeApcRoutine(PKAPC Apc
,
597 PKNORMAL_ROUTINE
* NormalRoutine
,
598 PVOID
* NormalContext
,
599 PVOID
* SystemArgument1
,
600 PVOID
* SystemArgument2
)
602 /* Free the APC and do nothing else */
607 * KiInitializeUserApc
609 * Prepares the Context for a User-Mode APC called through NTDLL.DLL
612 * Reserved - Pointer to the Exception Frame on non-i386 builds.
614 * TrapFrame - Pointer to the Trap Frame.
616 * NormalRoutine - Pointer to the NormalRoutine to call.
618 * NormalContext - Pointer to the context to send to the Normal Routine.
620 * SystemArgument[1-2] - Pointer to a set of two parameters that contain
632 KiInitializeUserApc(IN PVOID Reserved
,
633 IN PKTRAP_FRAME TrapFrame
,
634 IN PKNORMAL_ROUTINE NormalRoutine
,
635 IN PVOID NormalContext
,
636 IN PVOID SystemArgument1
,
637 IN PVOID SystemArgument2
)
642 DPRINT("KiInitializeUserApc(TrapFrame %x/%x)\n", TrapFrame
, KeGetCurrentThread()->TrapFrame
);
645 * Save the thread's current context (in other words the registers
646 * that will be restored when it returns to user mode) so the
647 * APC dispatcher can restore them later
649 Context
= (PCONTEXT
)(((PUCHAR
)TrapFrame
->Esp
) - sizeof(CONTEXT
));
650 RtlZeroMemory(Context
, sizeof(CONTEXT
));
651 Context
->ContextFlags
= CONTEXT_FULL
;
652 Context
->SegGs
= TrapFrame
->Gs
;
653 Context
->SegFs
= TrapFrame
->Fs
;
654 Context
->SegEs
= TrapFrame
->Es
;
655 Context
->SegDs
= TrapFrame
->Ds
;
656 Context
->Edi
= TrapFrame
->Edi
;
657 Context
->Esi
= TrapFrame
->Esi
;
658 Context
->Ebx
= TrapFrame
->Ebx
;
659 Context
->Edx
= TrapFrame
->Edx
;
660 Context
->Ecx
= TrapFrame
->Ecx
;
661 Context
->Eax
= TrapFrame
->Eax
;
662 Context
->Ebp
= TrapFrame
->Ebp
;
663 Context
->Eip
= TrapFrame
->Eip
;
664 Context
->SegCs
= TrapFrame
->Cs
;
665 Context
->EFlags
= TrapFrame
->Eflags
;
666 Context
->Esp
= TrapFrame
->Esp
;
667 Context
->SegSs
= TrapFrame
->Ss
;
670 * Setup the trap frame so the thread will start executing at the
671 * APC Dispatcher when it returns to user-mode
673 Esp
= (PULONG
)(((PUCHAR
)TrapFrame
->Esp
) - (sizeof(CONTEXT
) + (6 * sizeof(ULONG
))));
675 Esp
[1] = (ULONG
)NormalRoutine
;
676 Esp
[2] = (ULONG
)NormalContext
;
677 Esp
[3] = (ULONG
)SystemArgument1
;
678 Esp
[4] = (ULONG
)SystemArgument2
;
679 Esp
[5] = (ULONG
)Context
;
680 TrapFrame
->Eip
= (ULONG
)LdrpGetSystemDllApcDispatcher();
681 TrapFrame
->Esp
= (ULONG
)Esp
;
688 * Prepares the Context for a User-Mode APC called through NTDLL.DLL
694 * KeAreApcsDisabled returns TRUE if the thread is within a critical region
695 * or a guarded region, and FALSE otherwise.
698 * A thread running at IRQL = PASSIVE_LEVEL can use KeAreApcsDisabled to
699 * determine if normal kernel APCs are disabled. A thread that is inside a
700 * critical region has both user APCs and normal kernel APCs disabled, but
701 * not special kernel APCs. A thread that is inside a guarded region has
702 * all APCs disabled, including special kernel APCs.
704 * Callers of this routine must be running at IRQL <= APC_LEVEL.
709 KeAreApcsDisabled(VOID
)
711 /* Return the Kernel APC State */
712 return KeGetCurrentThread()->KernelApcDisable
? TRUE
: FALSE
;
719 * This routine is used to queue an APC from user-mode for the specified
723 * Thread Handle - Handle to the Thread. This handle must have THREAD_SET_CONTEXT privileges.
725 * ApcRoutine - Pointer to the APC Routine to call when the APC executes.
727 * NormalContext - Pointer to the context to send to the Normal Routine.
729 * SystemArgument[1-2] - Pointer to a set of two parameters that contain
733 * STATUS_SUCCESS or failure cute from associated calls.
736 * The thread must enter an alertable wait before the APC will be
742 NtQueueApcThread(HANDLE ThreadHandle
,
743 PKNORMAL_ROUTINE ApcRoutine
,
745 PVOID SystemArgument1
,
746 PVOID SystemArgument2
)
750 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
753 /* Get ETHREAD from Handle */
754 Status
= ObReferenceObjectByHandle(ThreadHandle
,
761 /* Fail if the Handle is invalid for some reason */
762 if (!NT_SUCCESS(Status
)) {
767 /* If this is a Kernel or System Thread, then fail */
768 if (Thread
->Tcb
.Teb
== NULL
) {
770 ObDereferenceObject(Thread
);
771 return STATUS_INVALID_HANDLE
;
774 /* Allocate an APC */
775 Apc
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(KAPC
), TAG('P', 's', 'a', 'p'));
778 ObDereferenceObject(Thread
);
779 return(STATUS_NO_MEMORY
);
782 /* Initialize and Queue a user mode apc (always!) */
785 OriginalApcEnvironment
,
792 if (!KeInsertQueueApc(Apc
, SystemArgument1
, SystemArgument2
, IO_NO_INCREMENT
)) {
794 Status
= STATUS_UNSUCCESSFUL
;
798 Status
= STATUS_SUCCESS
;
801 /* Dereference Thread and Return */
802 ObDereferenceObject(Thread
);
807 VOID
RepairList(PLIST_ENTRY Original
,
809 KPROCESSOR_MODE Mode
)
811 /* Copy Source to Desination */
812 if (IsListEmpty(&Original
[(int)Mode
])) {
814 InitializeListHead(&Copy
[(int)Mode
]);
818 Copy
[(int)Mode
].Flink
= Original
[(int)Mode
].Flink
;
819 Copy
[(int)Mode
].Blink
= Original
[(int)Mode
].Blink
;
820 Original
[(int)Mode
].Flink
->Blink
= &Copy
[(int)Mode
];
821 Original
[(int)Mode
].Blink
->Flink
= &Copy
[(int)Mode
];
827 KiMoveApcState(PKAPC_STATE OldState
,
828 PKAPC_STATE NewState
)
830 /* Restore backup of Original Environment */
831 *NewState
= *OldState
;
834 RepairList(NewState
->ApcListHead
, OldState
->ApcListHead
, KernelMode
);
835 RepairList(NewState
->ApcListHead
, OldState
->ApcListHead
, UserMode
);