본문 바로가기
코드/kernel

[ReactOs Source Code] KeAttachProcess, KeDetachProcess

by WeZZ 2009. 1. 20.


NTKERNELAPI VOID NTAPI KeAttachProcess ( IN PKPROCESS  Process  ) 

Definition at line 443 of file procobj.c.

00444 {
00445     KLOCK_QUEUE_HANDLE ApcLock;
00446     PKTHREAD Thread = KeGetCurrentThread();
00447     ASSERT_PROCESS(Process);
00448     ASSERT_IRQL_LESS_OR_EQUAL(DISPATCH_LEVEL);
00449 
00450     /* Make sure that we are in the right page directory (ReactOS Mm Hack) */
00451     MiSyncForProcessAttach(Thread, (PEPROCESS)Process);
00452 
00453     /* Check if we're already in that process */
00454     if (Thread->ApcState.Process == Process) return;
00455 
00456     /* Check if a DPC is executing or if we're already attached */
00457     if ((Thread->ApcStateIndex != OriginalApcEnvironment) ||
00458         (KeIsExecutingDpc()))
00459     {
00460         /* Invalid attempt */
00461         KeBugCheckEx(INVALID_PROCESS_ATTACH_ATTEMPT,
00462                      (ULONG_PTR)Process,
00463                      (ULONG_PTR)Thread->ApcState.Process,
00464                      Thread->ApcStateIndex,
00465                      KeIsExecutingDpc());
00466     }
00467     else
00468     {
00469         /* Acquire APC Lock */
00470         KiAcquireApcLock(Thread, &ApcLock);
00471 
00472         /* Acquire the dispatcher lock */
00473         KiAcquireDispatcherLockAtDpcLevel();
00474 
00475         /* Legit attach attempt: do it! */
00476         KiAttachProcess(Thread, Process, &ApcLock, &Thread->SavedApcState);
00477     }
00478 }

NTKERNELAPI VOID NTAPI KeDetachProcess ( VOID   ) 

Definition at line 485 of file procobj.c.

00486 {
00487     PKTHREAD Thread = KeGetCurrentThread();
00488     KLOCK_QUEUE_HANDLE ApcLock;
00489     PKPROCESS Process;
00490     ASSERT_IRQL_LESS_OR_EQUAL(DISPATCH_LEVEL);
00491 
00492     /* Check if it's attached */
00493     if (Thread->ApcStateIndex == OriginalApcEnvironment) return;
00494 
00495     /* Acquire APC Lock */
00496     KiAcquireApcLock(Thread, &ApcLock);
00497 
00498     /* Check for invalid attach attempts */
00499     if ((Thread->ApcState.KernelApcInProgress) ||
00500         !(IsListEmpty(&Thread->ApcState.ApcListHead[KernelMode])) ||
00501         !(IsListEmpty(&Thread->ApcState.ApcListHead[UserMode])))
00502     {
00503         /* Crash the system */
00504         KeBugCheck(INVALID_PROCESS_DETACH_ATTEMPT);
00505     }
00506 
00507     /* Get the process */
00508     Process = Thread->ApcState.Process;
00509 
00510     /* Acquire dispatcher lock */
00511     KiAcquireDispatcherLockAtDpcLevel();
00512 
00513     /* Decrease the stack count */
00514     ASSERT(Process->StackCount != 0);
00515     ASSERT(Process->State == ProcessInMemory);
00516     Process->StackCount--;
00517 
00518     /* Check if we can swap the process out */
00519     if (!Process->StackCount)
00520     {
00521         /* FIXME: Swap the process out */
00522     }
00523 
00524     /* Release dispatcher lock */
00525     KiReleaseDispatcherLockFromDpcLevel();
00526 
00527     /* Restore the APC State */
00528     KiMoveApcState(&Thread->SavedApcState, &Thread->ApcState);
00529     Thread->SavedApcState.Process = NULL;
00530     Thread->ApcStatePointer[OriginalApcEnvironment] = &Thread->ApcState;
00531     Thread->ApcStatePointer[AttachedApcEnvironment] = &Thread->SavedApcState;
00532     Thread->ApcStateIndex = OriginalApcEnvironment;
00533 
00534     /* Release lock */
00535     KiReleaseApcLockFromDpcLevel(&ApcLock);
00536 
00537     /* Swap Processes */
00538     KiSwapProcess(Thread->ApcState.Process, Thread->ApcState.Process);
00539 
00540     /* Exit the dispatcher */
00541     KiExitDispatcher(ApcLock.OldIrql);
00542 
00543     /* Check if we have pending APCs */
00544     if (IsListEmpty(&Thread->ApcState.ApcListHead[KernelMode]))
00545     {
00546         /* What do you know, we do! Request them to be delivered */
00547         Thread->ApcState.KernelApcPending = TRUE;
00548         HalRequestSoftwareInterrupt(APC_LEVEL);
00549     }
00550 }


http://www.reactos.org/
http://www.reactos.org/pipermail/ros-diffs/2005-December/010224.html