[ntoskrnl]
[reactos.git] / reactos / ntoskrnl / ps / kill.c
index d5e05d0..b1ea95f 100644 (file)
 
 #include <ntoskrnl.h>
 #define NDEBUG
-#include <internal/debug.h>
+#include <debug.h>
 
 /* GLOBALS *******************************************************************/
 
-LIST_ENTRY PspReaperListHead = {0};
+LIST_ENTRY PspReaperListHead = { NULL, NULL };
 WORK_QUEUE_ITEM PspReaperWorkItem;
 LARGE_INTEGER ShortTime = {{-10 * 100 * 1000, -1}};
 
@@ -188,7 +188,7 @@ PspReapRoutine(IN PVOID Context)
             Thread = CONTAINING_RECORD(NextEntry, ETHREAD, ReaperLink);
 
             /* Delete this entry's kernel stack */
-            MmDeleteKernelStack((PVOID)Thread->Tcb.StackLimit,
+            MmDeleteKernelStack((PVOID)Thread->Tcb.StackBase,
                                 Thread->Tcb.LargeStack);
             Thread->Tcb.InitialStack = NULL;
 
@@ -270,9 +270,11 @@ PspDeleteProcess(IN PVOID ObjectBody)
         Process->SectionObject = NULL;
     }
 
-    /* Clean LDT and VDM_OBJECTS */
+#if defined(_X86_)
+    /* Clean Ldt and Vdm objects */
     PspDeleteLdt(Process);
     PspDeleteVdmObjects(Process);
+#endif
 
     /* Delete the Object Table */
     if (Process->ObjectTable)
@@ -287,9 +289,6 @@ PspDeleteProcess(IN PVOID ObjectBody)
         KeUnstackDetachProcess(&ApcState);
     }
 
-    /* KDB hook */
-    KDB_DELETEPROCESS_HOOK(Process);
-
     /* Check if we have an address space, and clean it */
     if (Process->HasAddressSpace)
     {
@@ -313,7 +312,7 @@ PspDeleteProcess(IN PVOID ObjectBody)
         if (!(ExDestroyHandle(PspCidTable, Process->UniqueProcessId, NULL)))
         {
             /* Something wrong happened, bugcheck */
-            KEBUGCHECK(CID_HANDLE_DELETION);
+            KeBugCheck(CID_HANDLE_DELETION);
         }
     }
 
@@ -352,7 +351,7 @@ PspDeleteThread(IN PVOID ObjectBody)
     if (Thread->Tcb.InitialStack)
     {
         /* Release it */
-        MmDeleteKernelStack((PVOID)Thread->Tcb.StackLimit,
+        MmDeleteKernelStack((PVOID)Thread->Tcb.StackBase,
                             Thread->Tcb.LargeStack);
     }
 
@@ -363,7 +362,7 @@ PspDeleteThread(IN PVOID ObjectBody)
         if (!(ExDestroyHandle(PspCidTable, Thread->Cid.UniqueThread, NULL)))
         {
             /* Something wrong happened, bugcheck */
-            KEBUGCHECK(CID_HANDLE_DELETION);
+            KeBugCheck(CID_HANDLE_DELETION);
         }
     }
 
@@ -424,7 +423,7 @@ PspExitThread(IN NTSTATUS ExitStatus)
     if (KeIsAttachedProcess())
     {
         /* Bugcheck */
-        KEBUGCHECKEX(INVALID_PROCESS_ATTACH_ATTEMPT,
+        KeBugCheckEx(INVALID_PROCESS_ATTACH_ATTEMPT,
                      (ULONG_PTR)CurrentProcess,
                      (ULONG_PTR)Thread->Tcb.ApcState.Process,
                      (ULONG_PTR)Thread->Tcb.ApcStateIndex,
@@ -438,7 +437,7 @@ PspExitThread(IN NTSTATUS ExitStatus)
     if (Thread->ActiveExWorker)
     {
         /* Bugcheck */
-        KEBUGCHECKEX(ACTIVE_EX_WORKER_THREAD_TERMINATION,
+        KeBugCheckEx(ACTIVE_EX_WORKER_THREAD_TERMINATION,
                      (ULONG_PTR)Thread,
                      0,
                      0,
@@ -449,7 +448,7 @@ PspExitThread(IN NTSTATUS ExitStatus)
     if (Thread->Tcb.CombinedApcDisable != 0)
     {
         /* Bugcheck */
-        KEBUGCHECKEX(KERNEL_APC_PENDING_DURING_EXIT,
+        KeBugCheckEx(KERNEL_APC_PENDING_DURING_EXIT,
                      0,
                      Thread->Tcb.CombinedApcDisable,
                      0,
@@ -581,7 +580,7 @@ PspExitThread(IN NTSTATUS ExitStatus)
         else
         {
             /* Bugcheck, we can't allow this */
-            KEBUGCHECKEX(CRITICAL_PROCESS_DIED,
+            KeBugCheckEx(CRITICAL_PROCESS_DIED,
                          (ULONG_PTR)CurrentProcess,
                          0,
                          0,
@@ -597,6 +596,7 @@ PspExitThread(IN NTSTATUS ExitStatus)
     if (TerminationPort)
     {
         /* Setup the message header */
+        TerminationMsg.h.u2.ZeroInit = 0;
         TerminationMsg.h.u2.s2.Type = LPC_CLIENT_DIED;
         TerminationMsg.h.u1.s1.TotalLength = sizeof(TerminationMsg);
         TerminationMsg.h.u1.s1.DataLength = sizeof(TerminationMsg) -
@@ -700,11 +700,11 @@ PspExitThread(IN NTSTATUS ExitStatus)
         PspW32ProcessCallout(CurrentProcess, FALSE);
     }
 
-    /* Make sure Stack Swap isn't enabled */
-    if (Thread->Tcb.EnableStackSwap)
+    /* Make sure Stack Swap is enabled */
+    if (!Thread->Tcb.EnableStackSwap)
     {
-        /* Stack swap really shouldn't be on during exit !*/
-        KEBUGCHECKEX(KERNEL_STACK_LOCKED_AT_EXIT, 0, 0, 0, 0);
+        /* Stack swap really shouldn't be disabled during exit! */
+        KeBugCheckEx(KERNEL_STACK_LOCKED_AT_EXIT, 0, 0, 0, 0);
     }
 
     /* Cancel I/O for the thread. */
@@ -781,7 +781,7 @@ PspExitThread(IN NTSTATUS ExitStatus)
         ObFastDereferenceObject(&CurrentProcess->Token, PrimaryToken);
 
         /* Check if this is a VDM Process and rundown the VDM DPCs if so */
-        if (CurrentProcess->VdmObjects);// VdmRundownDpcs(CurrentProcess);
+        if (CurrentProcess->VdmObjects) { /* VdmRundownDpcs(CurrentProcess); */ }
 
         /* Kill the process in the Object Manager */
         ObKillProcess(CurrentProcess);
@@ -852,7 +852,7 @@ PspExitThread(IN NTSTATUS ExitStatus)
     if ((FirstEntry) || (Thread->Tcb.CombinedApcDisable != 0))
     {
         /* Bugcheck time */
-        KEBUGCHECKEX(KERNEL_APC_PENDING_DURING_EXIT,
+        KeBugCheckEx(KERNEL_APC_PENDING_DURING_EXIT,
                      (ULONG_PTR)FirstEntry,
                      Thread->Tcb.CombinedApcDisable,
                      KeGetCurrentIrql(),
@@ -972,6 +972,7 @@ PspTerminateThreadByPointer(IN PETHREAD Thread,
 
     /* Allocate the APC */
     Apc = ExAllocatePoolWithTag(NonPagedPool, sizeof(KAPC), TAG_TERMINATE_APC);
+    if (!Apc) return STATUS_INSUFFICIENT_RESOURCES;
 
     /* Set the Terminated Flag */
     Flags = Thread->CrossThreadFlags | CT_TERMINATED_BIT;
@@ -994,7 +995,6 @@ PspTerminateThreadByPointer(IN PETHREAD Thread,
         if (!KeInsertQueueApc(Apc, Apc, NULL, 2))
         {
             /* The APC was already in the queue, fail */
-            ExFreePool(Apc);
             Status = STATUS_UNSUCCESSFUL;
         }
         else
@@ -1089,7 +1089,7 @@ PsTerminateSystemThread(IN NTSTATUS ExitStatus)
     PETHREAD Thread = PsGetCurrentThread();
 
     /* Make sure this is a system thread */
-    if (Thread->SystemThread) return STATUS_INVALID_PARAMETER;
+    if (!Thread->SystemThread) return STATUS_INVALID_PARAMETER;
 
     /* Terminate it for real */
     return PspTerminateThreadByPointer(Thread, ExitStatus, TRUE);
@@ -1111,12 +1111,21 @@ NtTerminateProcess(IN HANDLE ProcessHandle OPTIONAL,
     PSTRACE(PS_KILL_DEBUG,
             "ProcessHandle: %p ExitStatus: %p\n", ProcessHandle, ExitStatus);
 
-    /* Remember how we will kill it */
-    KillByHandle = (ProcessHandle != NULL);
+    /* Were we passed a process handle? */
+    if (ProcessHandle)
+    {
+        /* Yes we were, use it */
+        KillByHandle = TRUE;
+    }
+    else
+    {
+        /* We weren't... we assume this is suicide */
+        KillByHandle = FALSE;
+        ProcessHandle = NtCurrentProcess();
+    }
 
     /* Get the Process Object */
-    Status = ObReferenceObjectByHandle((KillByHandle) ?
-                                       ProcessHandle : NtCurrentProcess(),
+    Status = ObReferenceObjectByHandle(ProcessHandle,
                                        PROCESS_TERMINATE,
                                        PsProcessType,
                                        KeGetPreviousMode(),
@@ -1136,14 +1145,13 @@ NtTerminateProcess(IN HANDLE ProcessHandle OPTIONAL,
     /* Lock the Process */
     if (!ExAcquireRundownProtection(&Process->RundownProtect))
     {
-        /* Failed to lock, fal */
+        /* Failed to lock, fail */
         ObDereferenceObject (Process);
         return STATUS_PROCESS_IS_TERMINATING;
     }
 
-    /* Set the delete flag */
-    if (!KillByHandle) InterlockedOr((PLONG)&Process->Flags,
-                                     PSF_PROCESS_DELETE_BIT);
+    /* Set the delete flag, unless the process is comitting suicide */
+    if (KillByHandle) PspSetProcessFlag(Process, PSF_PROCESS_DELETE_BIT);
 
     /* Get the first thread */
     Status = STATUS_NOTHING_TO_TERMINATE;
@@ -1172,23 +1180,22 @@ NtTerminateProcess(IN HANDLE ProcessHandle OPTIONAL,
     ExReleaseRundownProtection(&Process->RundownProtect);
 
     /* Check if we are killing ourselves */
-    if (Process != CurrentProcess)
+    if (Process == CurrentProcess)
     {
-        /* Check for the DBG_TERMINATE_PROCESS exit code */
-        if (ExitStatus == DBG_TERMINATE_PROCESS)
+        /* Also make sure the caller gave us our handle */
+        if (KillByHandle)
         {
-            /* Disable debugging on this process */
-            DbgkClearProcessDebugObject(Process, NULL);
+            /* Dereference the process */
+            ObDereferenceObject(Process);
+
+            /* Terminate ourselves */
+            PspTerminateThreadByPointer(CurrentThread, ExitStatus, TRUE);
         }
     }
-    /* Make sure that we got a handle */
-    else if (KillByHandle)
+    else if (ExitStatus == DBG_TERMINATE_PROCESS)
     {
-        /* Dereference the project */
-        ObDereferenceObject(Process);
-
-        /* Terminate ourselves */
-        PspTerminateThreadByPointer(CurrentThread, ExitStatus, TRUE);
+        /* Disable debugging on this process */
+        DbgkClearProcessDebugObject(Process, NULL);
     }
 
     /* Check if there was nothing to terminate, or if we have a Debug Port */
@@ -1295,7 +1302,7 @@ NtRegisterThreadTerminatePort(IN HANDLE PortHandle)
     /* Allocate the Port and make sure it suceeded */
     TerminationPort = ExAllocatePoolWithTag(NonPagedPool,
                                             sizeof(TERMINATION_PORT),
-                                            TAG('P', 's', 'T', '='));
+                                            '=TsP');
     if(TerminationPort)
     {
         /* Associate the Port */