From f3fcc98b2dffdb8b031b009770957fb69e02ba75 Mon Sep 17 00:00:00 2001 From: "KJK::Hyperion" Date: Tue, 30 Dec 2003 05:10:32 +0000 Subject: [PATCH] - include/rosrtl/thread.h, lib/rosrtl/makefile, lib/rosrtl/makefile.i386, lib/rosrtl/thread/stack.c, lib/rosrtl/thread/exit.c, lib/rosrtl/thread/i386/stackexit.c: three new RosRtl functions (RtlRosExitUserThread, RtlRosFreeUserThreadStack and RtlRosSwitchStackForExit) - lib/kernel32/makefile, lib/kernel32/thread/thread.c, lib/kernel32/thread/thread/i386/exit.S: kernel32.dll now uses them - lib/ntdll/dbg/debug.c, lib/ntdll/rtl/thread.c: ntdll.dll too - subsys/csrss/api/wapi.c: CSRSS too (should fix regression) svn path=/trunk/; revision=7323 --- reactos/include/rosrtl/thread.h | 21 +++++++- reactos/lib/kernel32/makefile | 5 +- reactos/lib/kernel32/thread/i386/exit.S | 14 ------ reactos/lib/kernel32/thread/thread.c | 31 +----------- reactos/lib/ntdll/dbg/debug.c | 5 +- reactos/lib/ntdll/rtl/thread.c | 58 ++++++---------------- reactos/lib/rosrtl/makefile | 3 +- reactos/lib/rosrtl/makefile.i386 | 5 +- reactos/lib/rosrtl/thread/exit.c | 39 +++++++++++++++ reactos/lib/rosrtl/thread/i386/stackexit.S | 13 +++++ reactos/lib/rosrtl/thread/stack.c | 53 +++++++++++++++++++- reactos/subsys/csrss/api/wapi.c | 6 +-- 12 files changed, 154 insertions(+), 99 deletions(-) delete mode 100644 reactos/lib/kernel32/thread/i386/exit.S create mode 100644 reactos/lib/rosrtl/thread/exit.c create mode 100644 reactos/lib/rosrtl/thread/i386/stackexit.S diff --git a/reactos/include/rosrtl/thread.h b/reactos/include/rosrtl/thread.h index 8801e17c4e3..9ea15c372c3 100644 --- a/reactos/include/rosrtl/thread.h +++ b/reactos/include/rosrtl/thread.h @@ -1,4 +1,4 @@ -/* $Id: thread.h,v 1.3 2003/07/22 20:10:04 hyperion Exp $ +/* $Id: thread.h,v 1.4 2003/12/30 05:10:31 hyperion Exp $ */ #ifdef __cplusplus @@ -36,6 +36,11 @@ NTSTATUS CDECL RtlRosCreateUserThreadVa ... ); +__declspec(noreturn) VOID NTAPI RtlRosExitUserThread +( + IN NTSTATUS Status +); + NTSTATUS NTAPI RtlRosInitializeContext ( IN HANDLE ProcessHandle, @@ -61,6 +66,20 @@ NTSTATUS NTAPI RtlRosDeleteStack IN PUSER_STACK UserStack ); +NTSTATUS NTAPI RtlRosFreeUserThreadStack +( + IN HANDLE ProcessHandle, + IN HANDLE ThreadHandle +); + +NTSTATUS NTAPI RtlRosSwitchStackForExit +( + IN PVOID StackBase, + IN SIZE_T StackSize, + IN VOID (NTAPI * ExitRoutine)(ULONG_PTR Parameter), + IN ULONG_PTR Parameter +); + /* Private functions - for ROSRTL internal use only */ NTSTATUS NTAPI RtlpRosGetStackLimits ( diff --git a/reactos/lib/kernel32/makefile b/reactos/lib/kernel32/makefile index 7904a868c82..6dd69b40a38 100644 --- a/reactos/lib/kernel32/makefile +++ b/reactos/lib/kernel32/makefile @@ -1,4 +1,4 @@ -# $Id: makefile,v 1.72 2003/12/30 00:12:47 hyperion Exp $ +# $Id: makefile,v 1.73 2003/12/30 05:10:31 hyperion Exp $ PATH_TO_TOP = ../.. @@ -51,8 +51,7 @@ THREAD_OBJECTS = \ thread/fls.o THREAD_I386_OBJECTS = \ - thread/i386/fiber.o \ - thread/i386/exit.o + thread/i386/fiber.o PROCESS_OBJECTS = \ process/proc.o \ diff --git a/reactos/lib/kernel32/thread/i386/exit.S b/reactos/lib/kernel32/thread/i386/exit.S deleted file mode 100644 index 813980a53b5..00000000000 --- a/reactos/lib/kernel32/thread/i386/exit.S +++ /dev/null @@ -1,14 +0,0 @@ -.extern _K32ExitThread@4 - -.globl _K32SwitchStackAndExitThread@12 - -_K32SwitchStackAndExitThread@12: - movl 0xC(%esp), %edx /* nExitCode */ - movl 0x8(%esp), %ecx /* nStackSize */ - movl 0x4(%esp), %esp /* pStackBase */ - addl %ecx, %esp - subl $0x4, %esp - pushl %edx - call _K32ExitThread@4 - -/* EOF */ diff --git a/reactos/lib/kernel32/thread/thread.c b/reactos/lib/kernel32/thread/thread.c index a9f8c092656..48ff739501a 100644 --- a/reactos/lib/kernel32/thread/thread.c +++ b/reactos/lib/kernel32/thread/thread.c @@ -1,4 +1,4 @@ -/* $Id: thread.c,v 1.47 2003/12/30 01:08:16 hyperion Exp $ +/* $Id: thread.c,v 1.48 2003/12/30 05:10:32 hyperion Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -281,28 +281,6 @@ GetCurrentThreadId(VOID) return((DWORD)(NtCurrentTeb()->Cid).UniqueThread); } -static VOID FASTCALL K32FreeCurrentStack(VOID) -{ - MEMORY_BASIC_INFORMATION mbiInfo; - - if(VirtualQuery(NtCurrentTeb()->Tib.StackLimit, &mbiInfo, sizeof(mbiInfo))) - VirtualFree(mbiInfo.AllocationBase, 0, MEM_RELEASE); -} - -__declspec(noreturn) VOID STDCALL K32ExitThread(DWORD nExitCode) -{ - K32FreeCurrentStack(); - NtTerminateThread(NtCurrentThread(), nExitCode); - for(;;); -} - -extern __declspec(noreturn) VOID STDCALL K32SwitchStackAndExitThread -( - PVOID pStackBase, - SIZE_T uStackSize, - DWORD uExitCode -); - /* * @implemented */ @@ -330,12 +308,7 @@ ExitThread(DWORD uExitCode) LdrShutdownThread(); - K32SwitchStackAndExitThread - ( - NtCurrentTeb()->StaticUnicodeBuffer, - sizeof(NtCurrentTeb()->StaticUnicodeBuffer), - uExitCode - ); + RtlRosExitUserThread(uExitCode); } diff --git a/reactos/lib/ntdll/dbg/debug.c b/reactos/lib/ntdll/dbg/debug.c index 3360f4585f8..d204ee43f04 100644 --- a/reactos/lib/ntdll/dbg/debug.c +++ b/reactos/lib/ntdll/dbg/debug.c @@ -1,4 +1,4 @@ -/* $Id: debug.c,v 1.12 2003/11/17 02:12:50 hyperion Exp $ +/* $Id: debug.c,v 1.13 2003/12/30 05:10:32 hyperion Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -189,7 +190,7 @@ NTSTATUS STDCALL DbgUiRemoteBreakin(VOID) { DbgBreakPoint(); - RtlExitUserThread(0); + RtlRosExitUserThread(0); DbgBreakPoint(); return STATUS_SUCCESS; diff --git a/reactos/lib/ntdll/rtl/thread.c b/reactos/lib/ntdll/rtl/thread.c index 086e02455e8..fd266fe7c1a 100644 --- a/reactos/lib/ntdll/rtl/thread.c +++ b/reactos/lib/ntdll/rtl/thread.c @@ -26,6 +26,9 @@ /* FUNCTIONS ***************************************************************/ +/* + @implemented +*/ NTSTATUS STDCALL RtlCreateUserThread ( HANDLE ProcessHandle, @@ -67,6 +70,9 @@ NTSTATUS STDCALL RtlCreateUserThread ); } +/* + @implemented +*/ NTSTATUS STDCALL RtlInitializeContext ( HANDLE ProcessHandle, @@ -87,58 +93,24 @@ NTSTATUS STDCALL RtlInitializeContext ); } +/* + @implemented +*/ NTSTATUS STDCALL RtlFreeUserThreadStack ( HANDLE ProcessHandle, HANDLE ThreadHandle ) { - THREAD_BASIC_INFORMATION tbiInfo; - NTSTATUS nErrCode; - ULONG nDummy; - ULONG nSize = 0; - PVOID pStackBase; - PTEB pTeb; - - /* query basic information about the thread */ - nErrCode = NtQueryInformationThread - ( - ThreadHandle, - ThreadBasicInformation, - &tbiInfo, - sizeof(tbiInfo), - NULL - ); - - /* failure */ - if(!NT_SUCCESS(nErrCode)) return nErrCode; - if(tbiInfo.TebBaseAddress == NULL) return STATUS_ACCESS_VIOLATION; - - pTeb = (PTEB)tbiInfo.TebBaseAddress; - - /* read the base address of the stack to be deallocated */ - nErrCode = NtReadVirtualMemory - ( - ProcessHandle, - &pTeb->DeallocationStack, - &pStackBase, - sizeof(pStackBase), - &nDummy - ); - - /* failure */ - if(!NT_SUCCESS(nErrCode)) return nErrCode; - if(pStackBase == NULL) return STATUS_ACCESS_VIOLATION; - - /* deallocate the stack */ - nErrCode = NtFreeVirtualMemory(ProcessHandle, pStackBase, &nSize, MEM_RELEASE); - - return nErrCode; + return RtlRosFreeUserThreadStack(ProcessHandle, ThreadHandle); } -NTSTATUS STDCALL RtlExitUserThread(NTSTATUS nErrCode) +/* + @implemented +*/ +NTSTATUS STDCALL RtlExitUserThread(NTSTATUS Status) { - return NtTerminateThread(NtCurrentThread(), nErrCode); + RtlRosExitUserThread(Status); } /* EOF */ diff --git a/reactos/lib/rosrtl/makefile b/reactos/lib/rosrtl/makefile index 11f2e97b328..6b8013c5a66 100644 --- a/reactos/lib/rosrtl/makefile +++ b/reactos/lib/rosrtl/makefile @@ -1,4 +1,4 @@ -# $Id: makefile,v 1.9 2003/08/07 04:03:23 royce Exp $ +# $Id: makefile,v 1.10 2003/12/30 05:10:32 hyperion Exp $ PATH_TO_TOP = ../.. @@ -12,6 +12,7 @@ TARGET_SDKLIBS = ntdll.a kernel32.a advapi32.a THREAD_OBJECTS = \ thread/create.o \ + thread/exit.o \ thread/stack.o MISC_OBJECTS = \ diff --git a/reactos/lib/rosrtl/makefile.i386 b/reactos/lib/rosrtl/makefile.i386 index 9252556a062..a1ddfdcd8cd 100644 --- a/reactos/lib/rosrtl/makefile.i386 +++ b/reactos/lib/rosrtl/makefile.i386 @@ -1,8 +1,9 @@ -# $Id: makefile.i386,v 1.1 2003/07/22 20:10:04 hyperion Exp $ +# $Id: makefile.i386,v 1.2 2003/12/30 05:10:32 hyperion Exp $ THREAD_OBJECTS := \ $(THREAD_OBJECTS) \ thread/linearstack.o \ - thread/$(ARCH)/context.o + thread/$(ARCH)/context.o \ + thread/$(ARCH)/stackexit.o # EOF diff --git a/reactos/lib/rosrtl/thread/exit.c b/reactos/lib/rosrtl/thread/exit.c new file mode 100644 index 00000000000..5301aeb85ea --- /dev/null +++ b/reactos/lib/rosrtl/thread/exit.c @@ -0,0 +1,39 @@ +/* $Id: exit.c,v 1.1 2003/12/30 05:10:32 hyperion Exp $ +*/ +/* +*/ + +#define NTOS_MODE_USER +#include + +#define NDEBUG +#include + +#include + +static VOID NTAPI RtlRosExitUserThread_Stage2 +( + IN ULONG_PTR Status +) +{ + RtlRosFreeUserThreadStack(NtCurrentProcess(), NtCurrentThread()); + NtTerminateThread(NtCurrentThread(), Status); +} + +__declspec(noreturn) VOID NTAPI RtlRosExitUserThread +( + IN NTSTATUS Status +) +{ + RtlRosSwitchStackForExit + ( + NtCurrentTeb()->StaticUnicodeBuffer, + sizeof(NtCurrentTeb()->StaticUnicodeBuffer), + RtlRosExitUserThread_Stage2, + Status + ); + + for(;;); +} + +/* EOF */ diff --git a/reactos/lib/rosrtl/thread/i386/stackexit.S b/reactos/lib/rosrtl/thread/i386/stackexit.S new file mode 100644 index 00000000000..c2b644f1ef1 --- /dev/null +++ b/reactos/lib/rosrtl/thread/i386/stackexit.S @@ -0,0 +1,13 @@ +.globl _RtlRosSwitchStackForExit@16 + +_RtlRosSwitchStackForExit@16: + movl 0x10(%esp), %edx /* Parameter */ + movl 0xC(%esp), %eax /* ExitRoutine */ + movl 0x8(%esp), %ecx /* StackSize */ + movl 0x4(%esp), %esp /* StackBase */ + addl %ecx, %esp + subl $0x4, %esp + pushl %edx + call *%eax + +/* EOF */ diff --git a/reactos/lib/rosrtl/thread/stack.c b/reactos/lib/rosrtl/thread/stack.c index 0cb49ce7a79..1b1af47e26c 100644 --- a/reactos/lib/rosrtl/thread/stack.c +++ b/reactos/lib/rosrtl/thread/stack.c @@ -1,4 +1,4 @@ -/* $Id: stack.c,v 1.3 2003/07/22 20:10:04 hyperion Exp $ +/* $Id: stack.c,v 1.4 2003/12/30 05:10:32 hyperion Exp $ */ /* */ @@ -184,6 +184,57 @@ NTSTATUS NTAPI RtlRosDeleteStack return STATUS_SUCCESS; } +NTSTATUS NTAPI RtlRosFreeUserThreadStack +( + IN HANDLE ProcessHandle, + IN HANDLE ThreadHandle +) +{ + NTSTATUS nErrCode; + ULONG nSize = 0; + PVOID pStackBase; + + if(ThreadHandle == NtCurrentThread()) + pStackBase = NtCurrentTeb()->DeallocationStack; + else + { + THREAD_BASIC_INFORMATION tbiInfo; + ULONG nDummy; + + /* query basic information about the thread */ + nErrCode = NtQueryInformationThread + ( + ThreadHandle, + ThreadBasicInformation, + &tbiInfo, + sizeof(tbiInfo), + NULL + ); + + /* failure */ + if(!NT_SUCCESS(nErrCode)) return nErrCode; + if(tbiInfo.TebBaseAddress == NULL) return STATUS_ACCESS_VIOLATION; + + /* read the base address of the stack to be deallocated */ + nErrCode = NtReadVirtualMemory + ( + ProcessHandle, + &((PTEB)tbiInfo.TebBaseAddress)->DeallocationStack, + &pStackBase, + sizeof(pStackBase), + &nDummy + ); + + /* failure */ + if(!NT_SUCCESS(nErrCode)) return nErrCode; + if(pStackBase == NULL) return STATUS_ACCESS_VIOLATION; + } + + /* deallocate the stack */ + nErrCode = NtFreeVirtualMemory(ProcessHandle, pStackBase, &nSize, MEM_RELEASE); + + return nErrCode; +} NTSTATUS NTAPI RtlpRosGetStackLimits ( diff --git a/reactos/subsys/csrss/api/wapi.c b/reactos/subsys/csrss/api/wapi.c index 4e1d021263e..8ba8e1a5e24 100644 --- a/reactos/subsys/csrss/api/wapi.c +++ b/reactos/subsys/csrss/api/wapi.c @@ -1,4 +1,4 @@ -/* $Id: wapi.c,v 1.32 2003/12/05 08:43:16 gvg Exp $ +/* $Id: wapi.c,v 1.33 2003/12/30 05:10:32 hyperion Exp $ * * reactos/subsys/csrss/api/wapi.c * @@ -120,7 +120,7 @@ Thread_Api2(HANDLE ServerPort) { DPRINT1("CSR: NtReplyWaitReceivePort failed\n"); NtClose(ServerPort); - NtTerminateThread(NtCurrentThread(), Status); + RtlRosExitUserThread(Status); continue; } @@ -128,7 +128,7 @@ Thread_Api2(HANDLE ServerPort) { CsrFreeProcessData( (ULONG)LpcRequest.Header.ClientId.UniqueProcess ); NtClose(ServerPort); - NtTerminateThread(NtCurrentThread(), STATUS_SUCCESS); + RtlRosExitUserThread(STATUS_SUCCESS); continue; }