Author: ion
Date: Sat Jul 23 16:26:03 2011
New Revision: 52809
URL: http://svn.reactos.org/svn/reactos?rev=52809&view=rev
Log:
Thanks to Timo Kreuzer for discovering what led to these:
[KERNEL32]: BasepInitializeContext was not creating a correct CONTEXT record for fibers: the stack return address was not set (EIP was being used instead), and support for FPU-compatible Fibers was non-existent.
[KERNEL32]: CreateFiberEx was not passing the correct context flags to BasepInitializeContext to notify it that this is an FPU-fiber.
[KERNEL32]: SwitchToFiber was using some weird "FXSR" constant that maps to checking of PowerPC 64-bit Move instructions are available. We actually want to check for XMMI.
Modified:
trunk/reactos/dll/win32/kernel32/client/fiber.c
trunk/reactos/dll/win32/kernel32/client/i386/fiber.S
trunk/reactos/dll/win32/kernel32/client/utils.c
Modified: trunk/reactos/dll/win32/kernel32/client/fiber.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/client/…
==============================================================================
--- trunk/reactos/dll/win32/kernel32/client/fiber.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/kernel32/client/fiber.c [iso-8859-1] Sat Jul 23 16:26:03 2011
@@ -223,11 +223,8 @@
Fiber->ActivationContextStack = ActivationContextStack;
Fiber->Context.ContextFlags = CONTEXT_FULL;
- /* Save FPU State if requsted */
- if (dwFlags & FIBER_FLAG_FLOAT_SWITCH)
- {
- Fiber->Context.ContextFlags |= CONTEXT_FLOATING_POINT;
- }
+ /* Save FPU State if requested */
+ Fiber->Context.ContextFlags = (dwFlags & FIBER_FLAG_FLOAT_SWITCH) ? CONTEXT_FLOATING_POINT : 0;
/* initialize the context for the fiber */
BasepInitializeContext(&Fiber->Context,
Modified: trunk/reactos/dll/win32/kernel32/client/i386/fiber.S
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/client/…
==============================================================================
--- trunk/reactos/dll/win32/kernel32/client/i386/fiber.S [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/kernel32/client/i386/fiber.S [iso-8859-1] Sat Jul 23 16:26:03 2011
@@ -35,7 +35,7 @@
fnstcw [eax+FIBER_CONTEXT_FLOAT_SAVE_CONTROL_WORD]
/* Check if the CPU supports SIMD MXCSR State Save */
- cmp byte ptr ds:[PROCESSOR_FEATURE_FXSR], 1
+ cmp byte ptr ds:[PF_XMMI_INSTRUCTIONS_AVAILABLE], 1
jnz NoFpuStateSave
stmxcsr [eax+FIBER_CONTEXT_DR6]
@@ -99,7 +99,7 @@
ControlWordEqual:
/* Load the new one */
- cmp byte ptr ds:[PROCESSOR_FEATURE_FXSR], 1
+ cmp byte ptr ds:[PF_XMMI_INSTRUCTIONS_AVAILABLE], 1
jnz NoFpuStateRestore
ldmxcsr [ecx+FIBER_CONTEXT_DR6]
Modified: trunk/reactos/dll/win32/kernel32/client/utils.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/client/…
==============================================================================
--- trunk/reactos/dll/win32/kernel32/client/utils.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/kernel32/client/utils.c [iso-8859-1] Sat Jul 23 16:26:03 2011
@@ -336,8 +336,9 @@
IN ULONG ContextType)
{
#ifdef _M_IX86
+ ULONG ContextFlags;
DPRINT("BasepInitializeContext: %p\n", Context);
-
+
/* Setup the Initial Win32 Thread Context */
Context->Eax = (ULONG)StartAddress;
Context->Ebx = (ULONG)Parameter;
@@ -352,30 +353,53 @@
Context->SegSs = KGDT_R3_DATA | RPL_MASK;
Context->SegGs = 0;
+ /* Set the Context Flags */
+ ContextFlags = Context->ContextFlags;
+ Context->ContextFlags = CONTEXT_FULL;
+
+ /* Give it some room for the Parameter */
+ Context->Esp -= sizeof(PVOID);
+
/* Set the EFLAGS */
Context->EFlags = 0x3000; /* IOPL 3 */
- if (ContextType == 1) /* For Threads */
- {
+ /* What kind of context is being created? */
+ if (ContextType == 1)
+ {
+ /* For Threads */
Context->Eip = (ULONG)BaseThreadStartupThunk;
}
- else if (ContextType == 2) /* For Fibers */
- {
- Context->Eip = (ULONG)BaseFiberStartup;
- }
- else /* For first thread in a Process */
- {
+ else if (ContextType == 2)
+ {
+ /* This is a fiber: make space for the return address */
+ Context->Esp -= sizeof(PVOID);
+ *((PVOID*)Context->Esp) = BaseFiberStartup;
+
+ /* Is FPU state required? */
+ Context->ContextFlags |= ContextFlags;
+ if (ContextFlags == CONTEXT_FLOATING_POINT)
+ {
+ /* Set an initial state */
+ Context->FloatSave.ControlWord = 0x27F;
+ Context->FloatSave.StatusWord = 0;
+ Context->FloatSave.TagWord = 0xFFFF;
+ Context->FloatSave.ErrorOffset = 0;
+ Context->FloatSave.ErrorSelector = 0;
+ Context->FloatSave.DataOffset = 0;
+ Context->FloatSave.DataSelector = 0;
+ if (SharedUserData->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE])
+ Context->Dr6 = 0x1F80;
+ }
+ }
+ else
+ {
+ /* For first thread in a Process */
Context->Eip = (ULONG)BaseProcessStartThunk;
}
-
- /* Set the Context Flags */
- Context->ContextFlags = CONTEXT_FULL;
-
- /* Give it some room for the Parameter */
- Context->Esp -= sizeof(PVOID);
+
#elif defined(_M_AMD64)
DPRINT("BasepInitializeContext: %p\n", Context);
-
+
/* Setup the Initial Win32 Thread Context */
Context->Rax = (ULONG_PTR)StartAddress;
Context->Rbx = (ULONG_PTR)Parameter;
@@ -405,10 +429,10 @@
{
Context->Rip = (ULONG_PTR)BaseProcessStartThunk;
}
-
+
/* Set the Context Flags */
Context->ContextFlags = CONTEXT_FULL;
-
+
/* Give it some room for the Parameter */
Context->Rsp -= sizeof(PVOID);
#else
Author: ion
Date: Sat Jul 23 12:08:36 2011
New Revision: 52807
URL: http://svn.reactos.org/svn/reactos?rev=52807&view=rev
Log:
[KERNEL32]: Optimize SwitchToFiber to simply use "ret" to jump between fibers, instead of saving EIP and doing a JMP.
Bug #50: SwitchToFiber needs to check if FXSR is *NOT* present in order to skip using ldmxcsr/stmxcsr. Previously, it would check if it's unsupported, and jump past the instruction if it was (resulting in invalid opcode instructions on older systems)
50 bugs. Penance has been paid.
Modified:
trunk/reactos/dll/win32/kernel32/client/i386/fiber.S
Modified: trunk/reactos/dll/win32/kernel32/client/i386/fiber.S
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/client/…
==============================================================================
--- trunk/reactos/dll/win32/kernel32/client/i386/fiber.S [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/kernel32/client/i386/fiber.S [iso-8859-1] Sat Jul 23 12:08:36 2011
@@ -26,20 +26,16 @@
mov [eax+FIBER_CONTEXT_EDI], edi
mov [eax+FIBER_CONTEXT_EBP], ebp
- /* Save the return address */
- mov ebx, [esp]
- mov [eax+FIBER_CONTEXT_EIP], ebx
-
/* Check if we're to save FPU State */
cmp dword ptr [eax+FIBER_CONTEXT_FLAGS], CONTEXT_FULL OR CONTEXT_FLOATING_POINT
jnz NoFpuStateSave
/* Save the FPU State (Status and Control)*/
fstsw [eax+FIBER_CONTEXT_FLOAT_SAVE_STATUS_WORD]
- fstcw [eax+FIBER_CONTEXT_FLOAT_SAVE_CONTROL_WORD]
+ fnstcw [eax+FIBER_CONTEXT_FLOAT_SAVE_CONTROL_WORD]
/* Check if the CPU supports SIMD MXCSR State Save */
- cmp byte ptr ds:[PROCESSOR_FEATURE_FXSR], 0
+ cmp byte ptr ds:[PROCESSOR_FEATURE_FXSR], 1
jnz NoFpuStateSave
stmxcsr [eax+FIBER_CONTEXT_DR6]
@@ -103,7 +99,7 @@
ControlWordEqual:
/* Load the new one */
- cmp byte ptr ds:[PROCESSOR_FEATURE_FXSR], 0
+ cmp byte ptr ds:[PROCESSOR_FEATURE_FXSR], 1
jnz NoFpuStateRestore
ldmxcsr [ecx+FIBER_CONTEXT_DR6]
@@ -121,7 +117,8 @@
mov [edx+TEB_FLS_DATA], eax
/* Jump to new fiber */
- jmp dword ptr [ecx+FIBER_CONTEXT_EIP]
+ mov esp, [ecx+FIBER_CONTEXT_ESP]
+ ret 4
+END
-END
/* EOF */
Author: ion
Date: Sat Jul 23 11:43:57 2011
New Revision: 52802
URL: http://svn.reactos.org/svn/reactos?rev=52802&view=rev
Log:
[KERNEL32]: Bug #42: GetThreadPriority was only returning -2, -1, 0, 1, 2 or 15 and -15. For realtime threads, priorities of 3, 4, 5, 6, 7, and their negatives, are also valid. Also, GetThreadPriority was returning -15/15 for any priorty outside the -2/2 range, instead of just the special saturation values (I should count this as a separate bug, really...)
Modified:
trunk/reactos/dll/win32/kernel32/client/thread.c
Modified: trunk/reactos/dll/win32/kernel32/client/thread.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/client/…
==============================================================================
--- trunk/reactos/dll/win32/kernel32/client/thread.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/kernel32/client/thread.c [iso-8859-1] Sat Jul 23 11:43:57 2011
@@ -621,13 +621,15 @@
return THREAD_PRIORITY_ERROR_RETURN;
}
- /* Do some conversions for out of boundary values */
- if (ThreadBasic.BasePriority > THREAD_BASE_PRIORITY_MAX)
- {
+ /* Do some conversions for saturation values */
+ if (ThreadBasic.BasePriority == ((HIGH_PRIORITY + 1) / 2))
+ {
+ /* Win32 calls this "time critical" */
ThreadBasic.BasePriority = THREAD_PRIORITY_TIME_CRITICAL;
}
- else if (ThreadBasic.BasePriority < THREAD_BASE_PRIORITY_MIN)
- {
+ else if (ThreadBasic.BasePriority == -((HIGH_PRIORITY + 1) / 2))
+ {
+ /* Win32 calls this "idle" */
ThreadBasic.BasePriority = THREAD_PRIORITY_IDLE;
}
Author: ion
Date: Sat Jul 23 11:38:58 2011
New Revision: 52801
URL: http://svn.reactos.org/svn/reactos?rev=52801&view=rev
Log:
[KERNEL32]:
Bug #39: BaseThreadStartup needs to call CsrNewThread (which registers the termination port with CSRSS) for Win32 threads.
Bug #40: If a crash happens during thread execution, ExitProcess should be called instead of ExitThread (unless this is a CSR thread).
Bug #41: The ExitThread/Process APIs should be called still from within the context of the SEH frame, not outside of it.
Use BaseRunningInServerProcess global instead of local IsServer local when connecting to CSRSS. This is needed for things such as the above.
Modified:
trunk/reactos/dll/win32/kernel32/client/dllmain.c
trunk/reactos/dll/win32/kernel32/client/thread.c
trunk/reactos/dll/win32/kernel32/include/kernel32.h
Modified: trunk/reactos/dll/win32/kernel32/client/dllmain.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/client/…
==============================================================================
--- trunk/reactos/dll/win32/kernel32/client/dllmain.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/kernel32/client/dllmain.c [iso-8859-1] Sat Jul 23 11:38:58 2011
@@ -21,6 +21,8 @@
extern UNICODE_STRING SystemDirectory;
extern UNICODE_STRING WindowsDirectory;
+
+BOOLEAN BaseRunningInServerProcess;
WCHAR BaseDefaultPathBuffer[6140];
@@ -258,7 +260,6 @@
LPVOID lpReserved)
{
NTSTATUS Status;
- BOOLEAN IsServer;
ULONG Dummy;
ULONG DummySize = sizeof(Dummy);
WCHAR SessionDir[256];
@@ -304,7 +305,7 @@
InWindows ? 1 : 0,
&Dummy,
&DummySize,
- &IsServer);
+ &BaseRunningInServerProcess);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to connect to CSR (Status %lx)\n", Status);
@@ -313,7 +314,7 @@
}
/* Check if we are running a CSR Server */
- if (!IsServer)
+ if (!BaseRunningInServerProcess)
{
/* Set the termination port for the thread */
DPRINT("Creating new thread for CSR\n");
Modified: trunk/reactos/dll/win32/kernel32/client/thread.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/client/…
==============================================================================
--- trunk/reactos/dll/win32/kernel32/client/thread.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/kernel32/client/thread.c [iso-8859-1] Sat Jul 23 11:38:58 2011
@@ -52,22 +52,34 @@
BaseThreadStartup(LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter)
{
- volatile UINT uExitCode = 0;
-
/* Attempt to call the Thread Start Address */
_SEH2_TRY
{
+ /* Legacy check which is still used today for Win32 threads */
+ if (NtCurrentTeb()->NtTib.Version == (30 << 8)) // OS/2 V3.0 ("Cruiser")
+ {
+ /* This registers the termination port with CSRSS */
+ if (!BaseRunningInServerProcess) CsrNewThread();
+ }
+
/* Get the exit code from the Thread Start */
- uExitCode = (lpStartAddress)((PVOID)lpParameter);
+ ExitThread((lpStartAddress)((PVOID)lpParameter));
}
_SEH2_EXCEPT(BaseThreadExceptionFilter(_SEH2_GetExceptionInformation()))
{
/* Get the Exit code from the SEH Handler */
- uExitCode = _SEH2_GetExceptionCode();
- } _SEH2_END;
-
- /* Exit the Thread */
- ExitThread(uExitCode);
+ if (!BaseRunningInServerProcess)
+ {
+ /* Kill the whole process, usually */
+ ExitProcess(_SEH2_GetExceptionCode());
+ }
+ else
+ {
+ /* If running inside CSRSS, kill just this thread */
+ ExitThread(_SEH2_GetExceptionCode());
+ }
+ }
+ _SEH2_END;
}
/*
Modified: trunk/reactos/dll/win32/kernel32/include/kernel32.h
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/include…
==============================================================================
--- trunk/reactos/dll/win32/kernel32/include/kernel32.h [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/kernel32/include/kernel32.h [iso-8859-1] Sat Jul 23 11:38:58 2011
@@ -106,6 +106,8 @@
extern SYSTEM_BASIC_INFORMATION BaseCachedSysInfo;
+extern BOOLEAN BaseRunningInServerProcess;
+
/* FUNCTION PROTOTYPES *******************************************************/
BOOL WINAPI VerifyConsoleIoHandle(HANDLE Handle);