- Last patch was incomplete, apologies. SVN @ 5AM = bad idea.
- Do not report that ROS is running on 0 active processors, that's a bad idea.
- Actually check the affinity mask set by NtSetInformationThread
- Fix the check in KeSetAffinityThread
- Give a valid affinity to the system thread (corresponding to the active cpu affinity set).

This removes all bugchecks from the kernel32 thread winetest.
Modified: trunk/reactos/ntoskrnl/include/internal/ke.h
Modified: trunk/reactos/ntoskrnl/ke/kthread.c
Modified: trunk/reactos/ntoskrnl/ke/main.c
Modified: trunk/reactos/ntoskrnl/ps/psmgr.c
Modified: trunk/reactos/ntoskrnl/ps/query.c

Modified: trunk/reactos/ntoskrnl/include/internal/ke.h
--- trunk/reactos/ntoskrnl/include/internal/ke.h	2005-08-09 08:02:05 UTC (rev 17234)
+++ trunk/reactos/ntoskrnl/include/internal/ke.h	2005-08-09 08:50:57 UTC (rev 17235)
@@ -228,6 +228,12 @@
 
 VOID
 STDCALL
+KiSetPriorityThread(PKTHREAD Thread,
+                    KPRIORITY Priority,
+                    PBOOLEAN Released);
+
+VOID
+STDCALL
 KeStackAttachProcess (
     IN struct _KPROCESS* Process,
     OUT PKAPC_STATE ApcState

Modified: trunk/reactos/ntoskrnl/ke/kthread.c
--- trunk/reactos/ntoskrnl/ke/kthread.c	2005-08-09 08:02:05 UTC (rev 17234)
+++ trunk/reactos/ntoskrnl/ke/kthread.c	2005-08-09 08:50:57 UTC (rev 17235)
@@ -1299,7 +1299,12 @@
 
     DPRINT("KeSetAffinityThread(Thread %x, Affinity %x)\n", Thread, Affinity);
 
-    ASSERT(Affinity & ((1 << KeNumberProcessors) - 1));
+    /* Verify correct affinity */
+    if ((Affinity & Thread->ApcStatePointer[0]->Process->Affinity) !=
+        Affinity || !Affinity)
+    {
+        KEBUGCHECK(INVALID_AFFINITY_SET);
+    }
 
     OldIrql = KeAcquireDispatcherDatabaseLock();
 

Modified: trunk/reactos/ntoskrnl/ke/main.c
--- trunk/reactos/ntoskrnl/ke/main.c	2005-08-09 08:02:05 UTC (rev 17234)
+++ trunk/reactos/ntoskrnl/ke/main.c	2005-08-09 08:50:57 UTC (rev 17235)
@@ -25,7 +25,7 @@
 EXPORTED ULONG NtBuildNumber = KERNEL_VERSION_BUILD;
 EXPORTED ULONG NtGlobalFlag = 0;
 EXPORTED CHAR  KeNumberProcessors;
-EXPORTED KAFFINITY KeActiveProcessors;
+EXPORTED KAFFINITY KeActiveProcessors = 1;
 EXPORTED LOADER_PARAMETER_BLOCK KeLoaderBlock;
 EXPORTED ULONG KeDcacheFlushCount = 0;
 EXPORTED ULONG KeIcacheFlushCount = 0;

Modified: trunk/reactos/ntoskrnl/ps/psmgr.c
--- trunk/reactos/ntoskrnl/ps/psmgr.c	2005-08-09 08:02:05 UTC (rev 17234)
+++ trunk/reactos/ntoskrnl/ps/psmgr.c	2005-08-09 08:50:57 UTC (rev 17235)
@@ -213,7 +213,7 @@
 
    /* System threads may run on any processor. */
    RtlZeroMemory(PsInitialSystemProcess, sizeof(EPROCESS));
-   PsInitialSystemProcess->Pcb.Affinity = 0xFFFFFFFF;
+   PsInitialSystemProcess->Pcb.Affinity = KeActiveProcessors;
    PsInitialSystemProcess->Pcb.IopmOffset = 0xffff;
    PsInitialSystemProcess->Pcb.BasePriority = PROCESS_PRIO_NORMAL;
    PsInitialSystemProcess->Pcb.QuantumReset = 6;

Modified: trunk/reactos/ntoskrnl/ps/query.c
--- trunk/reactos/ntoskrnl/ps/query.c	2005-08-09 08:02:05 UTC (rev 17234)
+++ trunk/reactos/ntoskrnl/ps/query.c	2005-08-09 08:50:57 UTC (rev 17235)
@@ -1194,6 +1194,8 @@
                     SetInformationData[ThreadInformationClass].Size);
     }
 
+  /* FIXME: This is REALLY wrong. Some types don't need THREAD_SET_INFORMATION */
+  /* FIXME: We should also check for certain things before doing the reference */
   Status = ObReferenceObjectByHandle (ThreadHandle,
 				      THREAD_SET_INFORMATION,
 				      PsThreadType,
@@ -1218,7 +1220,19 @@
 	     break;
 
            case ThreadAffinityMask:
-	     Status = KeSetAffinityThread(&Thread->Tcb, u.Affinity);
+               
+               /* Check if this is valid */
+               DPRINT1("%lx, %lx\n", Thread->ThreadsProcess->Pcb.Affinity, u.Affinity);
+               if ((Thread->ThreadsProcess->Pcb.Affinity & u.Affinity) !=
+                   u.Affinity)
+               {
+                   DPRINT1("Wrong affinity given\n");
+                   Status = STATUS_INVALID_PARAMETER;
+               }
+               else
+               {
+	                Status = KeSetAffinityThread(&Thread->Tcb, u.Affinity);
+               }
 	     break;
 
            case ThreadImpersonationToken: