[CMAKE]
[reactos.git] / ntoskrnl / ps / process.c
index 33767f4..01f9a77 100644 (file)
@@ -70,22 +70,6 @@ KPRIORITY PspPriorityTable[PROCESS_PRIORITY_CLASS_ABOVE_NORMAL + 1] =
 
 /* PRIVATE FUNCTIONS *********************************************************/
 
-NTSTATUS
-NTAPI
-PspDeleteLdt(PEPROCESS Process)
-{
-    /* FIXME */
-    return STATUS_SUCCESS;
-}
-
-NTSTATUS
-NTAPI
-PspDeleteVdmObjects(PEPROCESS Process)
-{
-    /* FIXME */
-    return STATUS_SUCCESS;
-}
-
 PETHREAD
 NTAPI
 PsGetNextProcessThread(IN PEPROCESS Process,
@@ -201,13 +185,13 @@ PspComputeQuantumAndPriority(IN PEPROCESS Process,
     if (Mode == PsProcessPriorityForeground)
     {
         /* Set the memory priority and use priority separation */
-        MemoryPriority = 2;
+        MemoryPriority = MEMORY_PRIORITY_FOREGROUND;
         i = PsPrioritySeparation;
     }
     else
     {
         /* Set the background memory priority and no separation */
-        MemoryPriority = 0;
+        MemoryPriority = MEMORY_PRIORITY_BACKGROUND;
         i = 0;
     }
 
@@ -379,6 +363,8 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
     BOOLEAN Result, SdAllocated;
     PSECURITY_DESCRIPTOR SecurityDescriptor;
     SECURITY_SUBJECT_CONTEXT SubjectContext;
+    BOOLEAN NeedsPeb = FALSE;
+    INITIAL_PEB InitialPeb;
     PAGED_CODE();
     PSTRACE(PS_PROCESS_DEBUG,
             "ProcessHandle: %p Parent: %p\n", ProcessHandle, ParentProcess);
@@ -451,7 +437,7 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
     /* Check if we have a parent */
     if (Parent)
     {
-        /* Ineherit PID and Hard Error Processing */
+        /* Inherit PID and Hard Error Processing */
         Process->InheritedFromUniqueProcessId = Parent->UniqueProcessId;
         Process->DefaultHardErrorProcessing = Parent->
                                               DefaultHardErrorProcessing;
@@ -483,14 +469,14 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
         if (Parent != PsInitialSystemProcess)
         {
             /* It's not, so acquire the process rundown */
-            if (ExAcquireRundownProtection(&Process->RundownProtect))
+            if (ExAcquireRundownProtection(&Parent->RundownProtect))
             {
                 /* If the parent has a section, use it */
                 SectionObject = Parent->SectionObject;
                 if (SectionObject) ObReferenceObject(SectionObject);
 
                 /* Release process rundown */
-                ExReleaseRundownProtection(&Process->RundownProtect);
+                ExReleaseRundownProtection(&Parent->RundownProtect);
             }
 
             /* If we don't have a section object */
@@ -554,7 +540,7 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
     Process->SectionObject = SectionObject;
 
     /* Set default exit code */
-    Process->ExitStatus = STATUS_TIMEOUT;
+    Process->ExitStatus = STATUS_PENDING;
 
     /* Check if this is the initial process being built */
     if (Parent)
@@ -635,17 +621,27 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
                                                  SeAuditProcessCreationInfo.
                                                  ImageFileName);
         if (!NT_SUCCESS(Status)) goto CleanupWithRef;
+        
+        //
+        // We need a PEB
+        //
+        NeedsPeb = TRUE;
     }
     else if (Parent)
     {
         /* Check if this is a child of the system process */
         if (Parent != PsInitialSystemProcess)
         {
+            //
+            // We need a PEB
+            //
+            NeedsPeb = TRUE;
+
             /* This is a clone! */
             ASSERTMSG("No support for cloning yet\n", FALSE);
         }
         else
-        {
+        {           
             /* This is the initial system process */
             Flags &= ~PS_LARGE_PAGES;
             Status = MmInitializeProcessAddressSpace(Process,
@@ -659,7 +655,7 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
             Process->SeAuditProcessCreationInfo.ImageFileName =
                 ExAllocatePoolWithTag(PagedPool,
                                       sizeof(OBJECT_NAME_INFORMATION),
-                                      TAG('S', 'e', 'P', 'a'));
+                                      'aPeS');
             if (!Process->SeAuditProcessCreationInfo.ImageFileName)
             {
                 /* Fail */
@@ -673,6 +669,13 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
         }
     }
 
+#if MI_TRACE_PFNS
+    /* Copy the process name now that we have it */
+    memcpy(MiGetPfnEntry(Process->Pcb.DirectoryTableBase[0] >> PAGE_SHIFT)->ProcessName, Process->ImageFileName, 16);
+    if (Process->Pcb.DirectoryTableBase[1]) memcpy(MiGetPfnEntry(Process->Pcb.DirectoryTableBase[1] >> PAGE_SHIFT)->ProcessName, Process->ImageFileName, 16);
+    if (Process->WorkingSetPage) memcpy(MiGetPfnEntry(Process->WorkingSetPage)->ProcessName, Process->ImageFileName, 16);
+#endif
+
     /* Check if we have a section object and map the system DLL */
     if (SectionObject) PspMapSystemDll(Process, NULL, FALSE);
 
@@ -702,11 +705,34 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
     }
 
     /* Create PEB only for User-Mode Processes */
-    if (Parent)
+    if ((Parent) && (NeedsPeb))
     {
-        /* Create it */
-        Status = MmCreatePeb(Process);
-        if (!NT_SUCCESS(Status)) goto CleanupWithRef;
+        //
+        // Set up the initial PEB
+        //
+        RtlZeroMemory(&InitialPeb, sizeof(INITIAL_PEB));
+        InitialPeb.Mutant = (HANDLE)-1;
+        InitialPeb.ImageUsesLargePages = 0; // FIXME: Not yet supported
+        
+        //
+        // Create it only if we have an image section
+        //
+        if (SectionHandle)
+        {
+            //
+            // Create it
+            //
+            Status = MmCreatePeb(Process, &InitialPeb, &Process->Peb);
+            if (!NT_SUCCESS(Status)) goto CleanupWithRef;
+        }
+        else
+        {
+            //
+            // We have to clone it
+            //
+            ASSERTMSG("No support for cloning yet\n", FALSE);
+        }
+
     }
 
     /* The process can now be activated */
@@ -810,17 +836,23 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
     KeQuerySystemTime(&Process->CreateTime);
 
     /* Protect against bad user-mode pointer */
-    _SEH_TRY
+    _SEH2_TRY
     {
         /* Save the process handle */
        *ProcessHandle = hProcess;
     }
-    _SEH_HANDLE
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
         /* Get the exception code */
-       Status = _SEH_GetExceptionCode();
+       Status = _SEH2_GetExceptionCode();
     }
-    _SEH_END;
+    _SEH2_END;
+
+    /* Run the Notification Routines */
+    PspRunCreateProcessNotifyRoutines(Process, TRUE);
+    
+    /* If 12 processes have been created, enough of user-mode is ready */
+    if (++ProcessCount == 12) Ki386PerfEnd();
 
 CleanupWithRef:
     /*
@@ -1244,27 +1276,26 @@ NtCreateProcessEx(OUT PHANDLE ProcessHandle,
                   IN HANDLE ExceptionPort OPTIONAL,
                   IN BOOLEAN InJob)
 {
-    KPROCESSOR_MODE PreviousMode  = ExGetPreviousMode();
-    NTSTATUS Status = STATUS_SUCCESS;
+    KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
+    NTSTATUS Status;
     PAGED_CODE();
     PSTRACE(PS_PROCESS_DEBUG,
             "ParentProcess: %p Flags: %lx\n", ParentProcess, Flags);
 
     /* Check if we came from user mode */
-    if(PreviousMode != KernelMode)
+    if (PreviousMode != KernelMode)
     {
-        _SEH_TRY
+        _SEH2_TRY
         {
             /* Probe process handle */
             ProbeForWriteHandle(ProcessHandle);
         }
-        _SEH_HANDLE
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
-            /* Get exception code */
-            Status = _SEH_GetExceptionCode();
+            /* Return the exception code */
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
         }
-        _SEH_END;
-        if (!NT_SUCCESS(Status)) return Status;
+        _SEH2_END;
     }
 
     /* Make sure there's a parent process */
@@ -1343,7 +1374,7 @@ NtOpenProcess(OUT PHANDLE ProcessHandle,
     BOOLEAN HasObjectName = FALSE;
     PETHREAD Thread = NULL;
     PEPROCESS Process = NULL;
-    NTSTATUS Status = STATUS_SUCCESS;
+    NTSTATUS Status;
     ACCESS_STATE AccessState;
     AUX_ACCESS_DATA AuxData;
     PAGED_CODE();
@@ -1354,7 +1385,7 @@ NtOpenProcess(OUT PHANDLE ProcessHandle,
     if (PreviousMode != KernelMode)
     {
         /* Enter SEH for probing */
-        _SEH_TRY
+        _SEH2_TRY
         {
             /* Probe the thread handle */
             ProbeForWriteHandle(ProcessHandle);
@@ -1378,13 +1409,12 @@ NtOpenProcess(OUT PHANDLE ProcessHandle,
             HasObjectName = (ObjectAttributes->ObjectName != NULL);
             Attributes = ObjectAttributes->Attributes;
         }
-        _SEH_HANDLE
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
-            /* Get the exception code */
-            Status = _SEH_GetExceptionCode();
+            /* Return the exception code */
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
         }
-        _SEH_END;
-        if (!NT_SUCCESS(Status)) return Status;
+        _SEH2_END;
     }
     else
     {
@@ -1489,17 +1519,17 @@ NtOpenProcess(OUT PHANDLE ProcessHandle,
     if (NT_SUCCESS(Status))
     {
         /* Use SEH for write back */
-        _SEH_TRY
+        _SEH2_TRY
         {
             /* Write back the handle */
             *ProcessHandle = hProcess;
         }
-        _SEH_HANDLE
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
             /* Get the exception code */
-            Status = _SEH_GetExceptionCode();
+            Status = _SEH2_GetExceptionCode();
         }
-        _SEH_END;
+        _SEH2_END;
     }
 
     /* Return status */