[REACTX] Fix 64 bit issues
[reactos.git] / ntoskrnl / ps / process.c
index 7207ee3..c696276 100644 (file)
@@ -24,7 +24,7 @@ KGUARDED_MUTEX PspActiveProcessMutex;
 
 LARGE_INTEGER ShortPsLockDelay;
 
-ULONG PsRawPrioritySeparation = 0;
+ULONG PsRawPrioritySeparation;
 ULONG PsPrioritySeparation;
 CHAR PspForegroundQuantum[3];
 
@@ -255,11 +255,16 @@ PsChangeQuantumTable(IN BOOLEAN Immediate,
         /* Use a variable table */
         QuantumTable = PspVariableQuantums;
     }
-    else
+    else if (PspQuantumTypeFromMask(PrioritySeparation) == PSP_FIXED_QUANTUMS)
     {
         /* Use fixed table */
         QuantumTable = PspFixedQuantums;
     }
+    else
+    {
+        /* Use default for the type of system we're on */
+        QuantumTable = MmIsThisAnNtAsSystem() ? PspFixedQuantums : PspVariableQuantums;
+    }
 
     /* Now check if we should use long or short */
     if (PspQuantumLengthFromMask(PrioritySeparation) == PSP_LONG_QUANTUMS)
@@ -267,6 +272,16 @@ PsChangeQuantumTable(IN BOOLEAN Immediate,
         /* Use long quantums */
         QuantumTable += 3;
     }
+    else if (PspQuantumLengthFromMask(PrioritySeparation) == PSP_SHORT_QUANTUMS)
+    {
+        /* Keep existing table */
+        NOTHING;
+    }
+    else
+    {
+        /* Use default for the type of system we're on */
+        QuantumTable += MmIsThisAnNtAsSystem() ? 3 : 0;
+    }
 
     /* Check if we're using long fixed quantums */
     if (QuantumTable == &PspFixedQuantums[3])
@@ -292,12 +307,10 @@ PsChangeQuantumTable(IN BOOLEAN Immediate,
         Process = PsGetNextProcess(Process);
         while (Process)
         {
-            /*
-             * Use the priority separation, unless the process has
-             * low memory priority
-             */
-            i = (Process->Vm.Flags.MemoryPriority == 1) ?
-                0: PsPrioritySeparation;
+            /* Use the priority separation if this is a foreground process */
+            i = (Process->Vm.Flags.MemoryPriority ==
+                 MEMORY_PRIORITY_BACKGROUND) ?
+                 0: PsPrioritySeparation;
 
             /* Make sure that the process isn't idle */
             if (Process->PriorityClass != PROCESS_PRIORITY_CLASS_IDLE)
@@ -306,8 +319,7 @@ PsChangeQuantumTable(IN BOOLEAN Immediate,
                 if ((Process->Job) && (PspUseJobSchedulingClasses))
                 {
                     /* Use job quantum */
-                    Quantum = PspJobSchedulingClasses[Process->Job->
-                                                      SchedulingClass];
+                    Quantum = PspJobSchedulingClasses[Process->Job->SchedulingClass];
                 }
                 else
                 {
@@ -369,7 +381,7 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
             "ProcessHandle: %p Parent: %p\n", ProcessHandle, ParentProcess);
 
     /* Validate flags */
-    if (Flags & ~PS_ALL_FLAGS) return STATUS_INVALID_PARAMETER;
+    if (Flags & ~PROCESS_CREATE_FLAGS_LEGAL_MASK) return STATUS_INVALID_PARAMETER;
 
     /* Check for parent */
     if (ParentProcess)
@@ -436,15 +448,14 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
     /* Check if we have a parent */
     if (Parent)
     {
-        /* Inherit PID and Hard Error Processing */
+        /* Inherit PID and hard-error processing */
         Process->InheritedFromUniqueProcessId = Parent->UniqueProcessId;
-        Process->DefaultHardErrorProcessing = Parent->
-                                              DefaultHardErrorProcessing;
+        Process->DefaultHardErrorProcessing = Parent->DefaultHardErrorProcessing;
     }
     else
     {
-        /* Use default hard error processing */
-        Process->DefaultHardErrorProcessing = TRUE;
+        /* Use default hard-error processing */
+        Process->DefaultHardErrorProcessing = SEM_FAILCRITICALERRORS;
     }
 
     /* Check for a section handle */
@@ -508,7 +519,7 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
         Process->DebugPort = DebugObject;
 
         /* Check if the caller doesn't want the debug stuff inherited */
-        if (Flags & PS_NO_DEBUG_INHERIT)
+        if (Flags & PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT)
         {
             /* Set the process flag */
             InterlockedOr((PLONG)&Process->Flags, PSF_NO_DEBUG_INHERIT_BIT);
@@ -574,7 +585,8 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
                         PROCESS_PRIORITY_NORMAL,
                         Affinity,
                         DirectoryTableBase,
-                        (BOOLEAN)(Process->DefaultHardErrorProcessing & 4));
+                        BooleanFlagOn(Process->DefaultHardErrorProcessing,
+                                      SEM_NOALIGNMENTFAULTEXCEPT));
 
     /* Duplicate Parent Token */
     Status = PspInitializeProcessSecurity(Process, Parent);
@@ -595,7 +607,8 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
         }
 
         /* Initialize object manager for the process */
-        Status = ObInitProcess(Flags & PS_INHERIT_HANDLES ? Parent : NULL,
+        Status = ObInitProcess(Flags & PROCESS_CREATE_FLAGS_INHERIT_HANDLES ?
+                               Parent : NULL,
                                Process);
         if (!NT_SUCCESS(Status)) goto CleanupWithRef;
     }
@@ -643,7 +656,7 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
         else
         {
             /* This is the initial system process */
-            Flags &= ~PS_LARGE_PAGES;
+            Flags &= ~PROCESS_CREATE_FLAGS_LARGE_PAGES;
             Status = MmInitializeProcessAddressSpace(Process,
                                                      NULL,
                                                      NULL,
@@ -655,7 +668,7 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
             Process->SeAuditProcessCreationInfo.ImageFileName =
                 ExAllocatePoolWithTag(PagedPool,
                                       sizeof(OBJECT_NAME_INFORMATION),
-                                      'aPeS');
+                                      TAG_SEPA);
             if (!Process->SeAuditProcessCreationInfo.ImageFileName)
             {
                 /* Fail */
@@ -701,7 +714,6 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
     {
         /* FIXME: We need to insert this process */
         DPRINT1("Jobs not yet supported\n");
-        ASSERT(FALSE);
     }
 
     /* Create PEB only for User-Mode Processes */
@@ -838,6 +850,12 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
     /* Protect against bad user-mode pointer */
     _SEH2_TRY
     {
+        /* Hacky way of returning the PEB to the user-mode creator */
+        if ((Process->Peb) && (CurrentThread->Tcb.Teb))
+        {
+            CurrentThread->Tcb.Teb->NtTib.ArbitraryUserPointer = Process->Peb;
+        }
+
         /* Save the process handle */
        *ProcessHandle = hProcess;
     }
@@ -1140,11 +1158,21 @@ PsGetProcessSecurityPort(PEPROCESS Process)
 /*
  * @implemented
  */
-HANDLE
+ULONG
+NTAPI
+PsGetProcessSessionId(IN PEPROCESS Process)
+{
+    return MmGetSessionId(Process);
+}
+
+/*
+ * @implemented
+ */
+ULONG
 NTAPI
-PsGetProcessSessionId(PEPROCESS Process)
+PsGetProcessSessionIdEx(IN PEPROCESS Process)
 {
-    return (HANDLE)Process->Session;
+    return MmGetSessionIdEx(Process);
 }
 
 /*
@@ -1212,23 +1240,70 @@ PsSetProcessPriorityClass(PEPROCESS Process,
 /*
  * @implemented
  */
-VOID
+NTSTATUS
 NTAPI
 PsSetProcessSecurityPort(PEPROCESS Process,
                          PVOID SecurityPort)
 {
     Process->SecurityPort = SecurityPort;
+    return STATUS_SUCCESS;
 }
 
 /*
  * @implemented
  */
-VOID
+NTSTATUS
 NTAPI
-PsSetProcessWin32Process(PEPROCESS Process,
-                         PVOID Win32Process)
+PsSetProcessWin32Process(
+    _Inout_ PEPROCESS Process,
+    _In_opt_ PVOID Win32Process,
+    _In_opt_ PVOID OldWin32Process)
 {
-    Process->Win32Process = Win32Process;
+    NTSTATUS Status;
+
+    /* Assume success */
+    Status = STATUS_SUCCESS;
+
+    /* Lock the process */
+    KeEnterCriticalRegion();
+    ExAcquirePushLockExclusive(&Process->ProcessLock);
+
+    /* Check if we set a new win32 process */
+    if (Win32Process != NULL)
+    {
+        /* Check if the process is in the right state */
+        if (((Process->Flags & PSF_PROCESS_DELETE_BIT) == 0) &&
+            (Process->Win32Process == NULL))
+        {
+            /* Set the new win32 process */
+            Process->Win32Process = Win32Process;
+        }
+        else
+        {
+            /* Otherwise fail */
+            Status = STATUS_PROCESS_IS_TERMINATING;
+        }
+    }
+    else
+    {
+        /* Reset the win32 process, did the caller specify the correct old value? */
+        if (Process->Win32Process == OldWin32Process)
+        {
+            /* Yes, so reset the win32 process to NULL */
+            Process->Win32Process = NULL;
+        }
+        else
+        {
+            /* Otherwise fail */
+            Status = STATUS_UNSUCCESSFUL;
+        }
+    }
+
+    /* Unlock the process */
+    ExReleasePushLockExclusive(&Process->ProcessLock);
+    KeLeaveCriticalRegion();
+
+    return Status;
 }
 
 /*
@@ -1341,9 +1416,9 @@ NtCreateProcess(OUT PHANDLE ProcessHandle,
             "Parent: %p Attributes: %p\n", ParentProcess, ObjectAttributes);
 
     /* Set new-style flags */
-    if ((ULONG)SectionHandle & 1) Flags = PS_REQUEST_BREAKAWAY;
-    if ((ULONG)DebugPort & 1) Flags |= PS_NO_DEBUG_INHERIT;
-    if (InheritObjectTable) Flags |= PS_INHERIT_HANDLES;
+    if ((ULONG_PTR)SectionHandle & 1) Flags |= PROCESS_CREATE_FLAGS_BREAKAWAY;
+    if ((ULONG_PTR)DebugPort & 1) Flags |= PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT;
+    if (InheritObjectTable) Flags |= PROCESS_CREATE_FLAGS_INHERIT_HANDLES;
 
     /* Call the new API */
     return NtCreateProcessEx(ProcessHandle,
@@ -1407,7 +1482,9 @@ NtOpenProcess(OUT PHANDLE ProcessHandle,
                          sizeof(OBJECT_ATTRIBUTES),
                          sizeof(ULONG));
             HasObjectName = (ObjectAttributes->ObjectName != NULL);
-            Attributes = ObjectAttributes->Attributes;
+
+            /* Validate user attributes */
+            Attributes = ObpValidateAttributes(ObjectAttributes->Attributes, PreviousMode);
         }
         _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
@@ -1420,7 +1497,9 @@ NtOpenProcess(OUT PHANDLE ProcessHandle,
     {
         /* Otherwise just get the data directly */
         HasObjectName = (ObjectAttributes->ObjectName != NULL);
-        Attributes = ObjectAttributes->Attributes;
+
+        /* Still have to sanitize attributes */
+        Attributes = ObpValidateAttributes(ObjectAttributes->Attributes, PreviousMode);
     }
 
     /* Can't pass both, fail */