[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / ps / thread.c
index 068d383..d05fc85 100644 (file)
@@ -56,7 +56,7 @@ PspUserThreadStartup(IN PKSTART_ROUTINE StartRoutine,
     if (!(Thread->DeadThread) && !(Thread->HideFromDebugger))
     {
         /* We're not, so notify the debugger */
-        DbgkCreateThread(StartContext);
+        DbgkCreateThread(Thread, StartContext);
     }
 
     /* Make sure we're not already dead */
@@ -72,10 +72,8 @@ PspUserThreadStartup(IN PKSTART_ROUTINE StartRoutine,
         KeRaiseIrql(APC_LEVEL, &OldIrql);
 
         /* Queue the User APC */
-        KiInitializeUserApc(NULL,
-                            (PVOID)((ULONG_PTR)Thread->Tcb.InitialStack -
-                            sizeof(KTRAP_FRAME) -
-                            SIZEOF_FX_SAVE_AREA),
+        KiInitializeUserApc(KeGetExceptionFrame(&Thread->Tcb),
+                            KeGetTrapFrame(&Thread->Tcb),
                             PspSystemDllEntryPoint,
                             NULL,
                             PspSystemDllBase,
@@ -113,8 +111,7 @@ PspUserThreadStartup(IN PKSTART_ROUTINE StartRoutine,
     }
 }
 
-static
-int
+LONG
 PspUnhandledExceptionInSystemThread(PEXCEPTION_POINTERS ExceptionPointers)
 {
     /* Print debugging information */
@@ -311,34 +308,18 @@ PspCreateThread(OUT PHANDLE ThreadHandle,
     if (ThreadContext)
     {
         /* User-mode Thread, create Teb */
-        TebBase = MmCreateTeb(Process, &Thread->Cid, InitialTeb);
-        if (!TebBase)
+        Status = MmCreateTeb(Process, &Thread->Cid, InitialTeb, &TebBase);
+        if (!NT_SUCCESS(Status))
         {
             /* Failed to create the TEB. Release rundown and dereference */
             ExReleaseRundownProtection(&Process->RundownProtect);
             ObDereferenceObject(Thread);
-            return STATUS_INSUFFICIENT_RESOURCES;
+            return Status;
         }
 
         /* Set the Start Addresses */
-#if defined(_M_IX86)
-        Thread->StartAddress = (PVOID)ThreadContext->Eip;
-        Thread->Win32StartAddress = (PVOID)ThreadContext->Eax;
-#elif defined(_M_PPC)
-        Thread->StartAddress = (PVOID)ThreadContext->Dr0;
-        Thread->Win32StartAddress = (PVOID)ThreadContext->Gpr3;
-#elif defined(_M_MIPS)
-        Thread->StartAddress = (PVOID)ThreadContext->Psr;
-        Thread->Win32StartAddress = (PVOID)ThreadContext->IntA0;
-#elif defined(_M_ARM)
-        Thread->StartAddress = (PVOID)ThreadContext->Pc;
-        Thread->Win32StartAddress = (PVOID)ThreadContext->R0;
-#elif defined(_M_AMD64)
-        Thread->StartAddress = (PVOID)ThreadContext->Rip;
-        Thread->Win32StartAddress = (PVOID)ThreadContext->Rax;
-#else
-#error Unknown architecture
-#endif
+        Thread->StartAddress = (PVOID)KeGetContextPc(ThreadContext);
+        Thread->Win32StartAddress = (PVOID)KeGetContextReturnRegister(ThreadContext);
 
         /* Let the kernel intialize the Thread */
         Status = KeInitThread(&Thread->Tcb,
@@ -476,9 +457,6 @@ PspCreateThread(OUT PHANDLE ThreadHandle,
         }
         _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
-            /* Get the exception code */
-            Status = _SEH2_GetExceptionCode();
-
             /* Thread insertion failed, thread is dead */
             PspSetCrossThreadFlag(Thread, CT_DEAD_THREAD_BIT);
 
@@ -493,9 +471,11 @@ PspCreateThread(OUT PHANDLE ThreadHandle,
 
             /* Close its handle, killing it */
             ObCloseHandle(ThreadHandle, PreviousMode);
+
+            /* Return the exception code */
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
         }
         _SEH2_END;
-        if (!NT_SUCCESS(Status)) return Status;
     }
     else
     {
@@ -880,7 +860,6 @@ NtCreateThread(OUT PHANDLE ThreadHandle,
                IN BOOLEAN CreateSuspended)
 {
     INITIAL_TEB SafeInitialTeb;
-    NTSTATUS Status = STATUS_SUCCESS;
     PAGED_CODE();
     PSTRACE(PS_THREAD_DEBUG,
             "ProcessHandle: %p Context: %p\n", ProcessHandle, ThreadContext);
@@ -898,7 +877,7 @@ NtCreateThread(OUT PHANDLE ThreadHandle,
             ProbeForWriteHandle(ThreadHandle);
 
             /* Check if the caller wants a client id */
-            if(ClientId)
+            if (ClientId)
             {
                 /* Make sure we can write to it */
                 ProbeForWrite(ClientId, sizeof(CLIENT_ID), sizeof(ULONG));
@@ -913,10 +892,10 @@ NtCreateThread(OUT PHANDLE ThreadHandle,
         }
         _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
-            Status = _SEH2_GetExceptionCode();
+            /* Return the exception code */
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
         }
         _SEH2_END;
-        if (!NT_SUCCESS(Status)) return Status;
     }
     else
     {
@@ -952,7 +931,7 @@ NtOpenThread(OUT PHANDLE ThreadHandle,
     CLIENT_ID SafeClientId;
     ULONG Attributes = 0;
     HANDLE hThread = NULL;
-    NTSTATUS Status = STATUS_SUCCESS;
+    NTSTATUS Status;
     PETHREAD Thread;
     BOOLEAN HasObjectName = FALSE;
     ACCESS_STATE AccessState;
@@ -991,11 +970,10 @@ NtOpenThread(OUT PHANDLE ThreadHandle,
         }
         _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
         {
-            /* Get the exception code */
-            Status = _SEH2_GetExceptionCode();
+            /* Return the exception code */
+            _SEH2_YIELD(return _SEH2_GetExceptionCode());
         }
         _SEH2_END;
-        if (!NT_SUCCESS(Status)) return Status;
     }
     else
     {