Commit 7370f601 authored by Alexandre Julliard's avatar Alexandre Julliard

Implemented RtlCreateUserThread, and use it to move thread creation to

ntdll.
parent ef65d2a6
...@@ -49,30 +49,6 @@ WINE_DECLARE_DEBUG_CHANNEL(relay); ...@@ -49,30 +49,6 @@ WINE_DECLARE_DEBUG_CHANNEL(relay);
/*********************************************************************** /***********************************************************************
* THREAD_InitTEB
*
* Initialization of a newly created TEB.
*/
static BOOL THREAD_InitTEB( TEB *teb )
{
teb->Tib.ExceptionList = (void *)~0UL;
teb->Tib.StackBase = (void *)~0UL;
teb->Tib.Self = &teb->Tib;
teb->tibflags = TEBF_WIN32;
teb->exit_code = STILL_ACTIVE;
teb->request_fd = -1;
teb->reply_fd = -1;
teb->wait_fd[0] = -1;
teb->wait_fd[1] = -1;
teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
teb->StaticUnicodeString.Buffer = (PWSTR)teb->StaticUnicodeBuffer;
InitializeListHead(&teb->TlsLinks);
teb->teb_sel = wine_ldt_alloc_fs();
return (teb->teb_sel != 0);
}
/***********************************************************************
* THREAD_InitStack * THREAD_InitStack
* *
* Allocate the stack of a thread. * Allocate the stack of a thread.
...@@ -83,22 +59,6 @@ TEB *THREAD_InitStack( TEB *teb, DWORD stack_size ) ...@@ -83,22 +59,6 @@ TEB *THREAD_InitStack( TEB *teb, DWORD stack_size )
DWORD page_size = getpagesize(); DWORD page_size = getpagesize();
void *base; void *base;
/* Allocate the stack */
/* if size is smaller than default, get stack size from parent */
if (stack_size < 1024 * 1024)
{
if (teb)
stack_size = 1024 * 1024; /* no parent */
else
stack_size = ((char *)NtCurrentTeb()->Tib.StackBase
- (char *)NtCurrentTeb()->DeallocationStack
- SIGNAL_STACK_SIZE);
}
/* FIXME: some Wine functions use a lot of stack, so we add 64Kb here */
stack_size += 64 * 1024;
/* Memory layout in allocated block: /* Memory layout in allocated block:
* *
* size contents * size contents
...@@ -109,21 +69,10 @@ TEB *THREAD_InitStack( TEB *teb, DWORD stack_size ) ...@@ -109,21 +69,10 @@ TEB *THREAD_InitStack( TEB *teb, DWORD stack_size )
stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1); stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
total_size = stack_size + SIGNAL_STACK_SIZE; total_size = stack_size + SIGNAL_STACK_SIZE;
if (!teb) total_size += page_size;
if (!(base = VirtualAlloc( NULL, total_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE ))) if (!(base = VirtualAlloc( NULL, total_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE )))
return NULL; return NULL;
if (!teb)
{
teb = (TEB *)((char *)base + total_size - page_size);
if (!THREAD_InitTEB( teb ))
{
VirtualFree( base, 0, MEM_RELEASE );
return NULL;
}
}
teb->DeallocationStack = base; teb->DeallocationStack = base;
teb->Tib.StackBase = (char *)base + SIGNAL_STACK_SIZE + stack_size; teb->Tib.StackBase = (char *)base + SIGNAL_STACK_SIZE + stack_size;
teb->Tib.StackLimit = base; /* note: limit is lower than base since the stack grows down */ teb->Tib.StackLimit = base; /* note: limit is lower than base since the stack grows down */
...@@ -136,23 +85,24 @@ TEB *THREAD_InitStack( TEB *teb, DWORD stack_size ) ...@@ -136,23 +85,24 @@ TEB *THREAD_InitStack( TEB *teb, DWORD stack_size )
} }
struct new_thread_info
{
LPTHREAD_START_ROUTINE func;
void *arg;
};
/*********************************************************************** /***********************************************************************
* THREAD_Start * THREAD_Start
* *
* Start execution of a newly created thread. Does not return. * Start execution of a newly created thread. Does not return.
*/ */
static void THREAD_Start( TEB *teb ) static void CALLBACK THREAD_Start( void *ptr )
{ {
LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)teb->entry_point; struct new_thread_info *info = ptr;
struct debug_info info; LPTHREAD_START_ROUTINE func = info->func;
void *arg = info->arg;
info.str_pos = info.strings;
info.out_pos = info.output;
teb->debug_info = &info;
SYSDEPS_SetCurThread( teb ); RtlFreeHeap( GetProcessHeap(), 0, info );
SIGNAL_Init();
wine_server_init_thread();
if (TRACE_ON(relay)) if (TRACE_ON(relay))
DPRINTF("%04lx:Starting thread (entryproc=%p)\n", GetCurrentThreadId(), func ); DPRINTF("%04lx:Starting thread (entryproc=%p)\n", GetCurrentThreadId(), func );
...@@ -160,7 +110,7 @@ static void THREAD_Start( TEB *teb ) ...@@ -160,7 +110,7 @@ static void THREAD_Start( TEB *teb )
__TRY __TRY
{ {
MODULE_DllThreadAttach( NULL ); MODULE_DllThreadAttach( NULL );
ExitThread( func( NtCurrentTeb()->entry_arg ) ); ExitThread( func( arg ) );
} }
__EXCEPT(UnhandledExceptionFilter) __EXCEPT(UnhandledExceptionFilter)
{ {
...@@ -177,60 +127,37 @@ HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack, ...@@ -177,60 +127,37 @@ HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack,
LPTHREAD_START_ROUTINE start, LPVOID param, LPTHREAD_START_ROUTINE start, LPVOID param,
DWORD flags, LPDWORD id ) DWORD flags, LPDWORD id )
{ {
HANDLE handle = 0; HANDLE handle;
TEB *teb; CLIENT_ID client_id;
DWORD tid = 0; NTSTATUS status;
int request_pipe[2]; SIZE_T stack_reserve = 0, stack_commit = 0;
struct new_thread_info *info;
if (pipe( request_pipe ) == -1) if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info) )))
{ {
SetLastError( ERROR_TOO_MANY_OPEN_FILES ); SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return 0; return 0;
} }
fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */ info->func = start;
wine_server_send_fd( request_pipe[0] ); info->arg = param;
SERVER_START_REQ( new_thread ) if (flags & STACK_SIZE_PARAM_IS_A_RESERVATION) stack_reserve = stack;
{ else stack_commit = stack;
req->suspend = ((flags & CREATE_SUSPENDED) != 0);
req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
req->request_fd = request_pipe[0];
if (!wine_server_call_err( req ))
{
handle = reply->handle;
tid = reply->tid;
}
close( request_pipe[0] );
}
SERVER_END_REQ;
if (!handle || !(teb = THREAD_InitStack( NULL, stack ))) status = RtlCreateUserThread( GetCurrentProcess(), NULL, (flags & CREATE_SUSPENDED) != 0,
NULL, stack_reserve, stack_commit,
THREAD_Start, info, &handle, &client_id );
if (status == STATUS_SUCCESS)
{ {
close( request_pipe[1] ); if (id) *id = (DWORD)client_id.UniqueThread;
return 0; if (sa && (sa->nLength >= sizeof(*sa)) && sa->bInheritHandle)
SetHandleInformation( handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT );
} }
else
teb->Peb = NtCurrentTeb()->Peb;
teb->ClientId.UniqueThread = (HANDLE)tid;
teb->request_fd = request_pipe[1];
teb->entry_point = start;
teb->entry_arg = param;
teb->htask16 = GetCurrentTask();
RtlAcquirePebLock();
InsertHeadList( &NtCurrentTeb()->TlsLinks, &teb->TlsLinks );
RtlReleasePebLock();
if (id) *id = tid;
if (SYSDEPS_SpawnThread( THREAD_Start, teb ) == -1)
{ {
CloseHandle( handle ); RtlFreeHeap( GetProcessHeap(), 0, info );
close( request_pipe[1] ); SetLastError( RtlNtStatusToDosError(status) );
RtlAcquirePebLock(); handle = 0;
RemoveEntryList( &teb->TlsLinks );
RtlReleasePebLock();
wine_ldt_free_fs( teb->teb_sel );
VirtualFree( teb->DeallocationStack, 0, MEM_RELEASE );
return 0;
} }
return handle; return handle;
} }
......
...@@ -333,7 +333,7 @@ ...@@ -333,7 +333,7 @@
@ stdcall RtlCreateUnicodeStringFromAsciiz(ptr str) @ stdcall RtlCreateUnicodeStringFromAsciiz(ptr str)
@ stub RtlCreateUserProcess @ stub RtlCreateUserProcess
@ stub RtlCreateUserSecurityObject @ stub RtlCreateUserSecurityObject
@ stub RtlCreateUserThread @ stdcall RtlCreateUserThread(long ptr long ptr long long ptr ptr ptr ptr)
@ stub RtlCustomCPToUnicodeN @ stub RtlCustomCPToUnicodeN
@ stub RtlCutoverTimeToSystemTime @ stub RtlCutoverTimeToSystemTime
@ stdcall RtlDeNormalizeProcessParams(ptr) @ stdcall RtlDeNormalizeProcessParams(ptr)
...@@ -1065,7 +1065,6 @@ ...@@ -1065,7 +1065,6 @@
# Server interface # Server interface
@ cdecl -norelay wine_server_call(ptr) @ cdecl -norelay wine_server_call(ptr)
@ cdecl wine_server_init_thread()
@ cdecl wine_server_fd_to_handle(long long long ptr) @ cdecl wine_server_fd_to_handle(long long long ptr)
@ cdecl wine_server_handle_to_fd(long long ptr ptr ptr) @ cdecl wine_server_handle_to_fd(long long ptr ptr ptr)
@ cdecl wine_server_send_fd(long) @ cdecl wine_server_send_fd(long)
...@@ -1084,9 +1083,6 @@ ...@@ -1084,9 +1083,6 @@
@ cdecl MODULE_DllThreadAttach(ptr) @ cdecl MODULE_DllThreadAttach(ptr)
@ cdecl MODULE_GetLoadOrderA(ptr str str long) @ cdecl MODULE_GetLoadOrderA(ptr str str long)
@ cdecl MODULE_GetLoadOrderW(ptr wstr wstr long) @ cdecl MODULE_GetLoadOrderW(ptr wstr wstr long)
@ cdecl SIGNAL_Init()
@ cdecl SYSDEPS_ExitThread(long) @ cdecl SYSDEPS_ExitThread(long)
@ cdecl SYSDEPS_SetCurThread(ptr)
@ cdecl SYSDEPS_SpawnThread(ptr ptr)
@ cdecl VERSION_Init(wstr) @ cdecl VERSION_Init(wstr)
@ cdecl VIRTUAL_SetFaultHandler(ptr ptr ptr) @ cdecl VIRTUAL_SetFaultHandler(ptr ptr ptr)
...@@ -79,6 +79,149 @@ DECL_GLOBAL_CONSTRUCTOR(thread_init) ...@@ -79,6 +79,149 @@ DECL_GLOBAL_CONSTRUCTOR(thread_init)
} }
/* startup routine for a newly created thread */
static void start_thread( TEB *teb )
{
LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)teb->entry_point;
struct debug_info info;
info.str_pos = info.strings;
info.out_pos = info.output;
teb->debug_info = &info;
SYSDEPS_SetCurThread( teb );
SIGNAL_Init();
wine_server_init_thread();
NtTerminateThread( GetCurrentThread(), func( NtCurrentTeb()->entry_arg ) );
}
/***********************************************************************
* RtlCreateUserThread (NTDLL.@)
*/
NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr,
BOOLEAN suspended, PVOID stack_addr,
SIZE_T stack_reserve, SIZE_T stack_commit,
PRTL_THREAD_START_ROUTINE start, void *param,
HANDLE *handle_ptr, CLIENT_ID *id )
{
HANDLE handle = 0;
TEB *teb;
DWORD tid = 0;
SIZE_T total_size;
SIZE_T page_size = getpagesize();
void *ptr, *base = NULL;
int request_pipe[2];
NTSTATUS status;
if (pipe( request_pipe ) == -1) return STATUS_TOO_MANY_OPENED_FILES;
fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
wine_server_send_fd( request_pipe[0] );
SERVER_START_REQ( new_thread )
{
req->suspend = suspended;
req->inherit = 0; /* FIXME */
req->request_fd = request_pipe[0];
if (!(status = wine_server_call( req )))
{
handle = reply->handle;
tid = reply->tid;
}
close( request_pipe[0] );
}
SERVER_END_REQ;
if (status) goto error;
if (!stack_reserve || !stack_commit)
{
IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
if (!stack_reserve) stack_reserve = nt->OptionalHeader.SizeOfStackReserve;
if (!stack_commit) stack_commit = nt->OptionalHeader.SizeOfStackCommit;
}
if (stack_reserve < stack_commit) stack_reserve = stack_commit;
stack_reserve = (stack_reserve + 0xffff) & ~0xffff; /* round to 64K boundary */
/* Memory layout in allocated block:
*
* size contents
* SIGNAL_STACK_SIZE signal stack
* stack_size normal stack (including a PAGE_GUARD page at the bottom)
* 1 page TEB (except for initial thread)
*/
total_size = stack_reserve + SIGNAL_STACK_SIZE + page_size;
if ((status = NtAllocateVirtualMemory( GetCurrentProcess(), &base, NULL, &total_size,
MEM_COMMIT, PAGE_EXECUTE_READWRITE )) != STATUS_SUCCESS)
goto error;
teb = (TEB *)((char *)base + total_size - page_size);
if (!(teb->teb_sel = wine_ldt_alloc_fs()))
{
status = STATUS_TOO_MANY_THREADS;
goto error;
}
teb->Tib.ExceptionList = (void *)~0UL;
teb->Tib.StackBase = (char *)base + SIGNAL_STACK_SIZE + stack_reserve;
teb->Tib.StackLimit = base; /* limit is lower than base since the stack grows down */
teb->Tib.Self = &teb->Tib;
teb->ClientId.UniqueProcess = (HANDLE)GetCurrentProcessId();
teb->ClientId.UniqueThread = (HANDLE)tid;
teb->Peb = NtCurrentTeb()->Peb;
teb->DeallocationStack = base;
teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer;
teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
RtlAcquirePebLock();
InsertHeadList( &NtCurrentTeb()->TlsLinks, &teb->TlsLinks );
RtlReleasePebLock();
teb->tibflags = TEBF_WIN32;
teb->exit_code = STILL_ACTIVE;
teb->request_fd = request_pipe[1];
teb->reply_fd = -1;
teb->wait_fd[0] = -1;
teb->wait_fd[1] = -1;
teb->entry_point = start;
teb->entry_arg = param;
teb->htask16 = NtCurrentTeb()->htask16;
/* setup the guard page */
ptr = (char *)base + SIGNAL_STACK_SIZE;
NtProtectVirtualMemory( GetCurrentProcess(), &ptr, &page_size,
PAGE_EXECUTE_READWRITE | PAGE_GUARD, NULL );
if (SYSDEPS_SpawnThread( start_thread, teb ) == -1)
{
RtlAcquirePebLock();
RemoveEntryList( &teb->TlsLinks );
RtlReleasePebLock();
wine_ldt_free_fs( teb->teb_sel );
status = STATUS_TOO_MANY_THREADS;
goto error;
}
if (id) id->UniqueThread = (HANDLE)tid;
if (handle_ptr) *handle_ptr = handle;
else NtClose( handle );
return STATUS_SUCCESS;
error:
if (base)
{
total_size = 0;
NtFreeVirtualMemory( GetCurrentProcess(), &base, &total_size, MEM_RELEASE );
}
if (handle) NtClose( handle );
close( request_pipe[1] );
return status;
}
/*********************************************************************** /***********************************************************************
* NtOpenThread (NTDLL.@) * NtOpenThread (NTDLL.@)
* ZwOpenThread (NTDLL.@) * ZwOpenThread (NTDLL.@)
...@@ -159,13 +302,7 @@ NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code ) ...@@ -159,13 +302,7 @@ NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
if (self) if (self)
{ {
if (last) exit( exit_code ); if (last) exit( exit_code );
else else SYSDEPS_AbortThread( exit_code );
{
RtlAcquirePebLock();
RemoveEntryList( &NtCurrentTeb()->TlsLinks );
RtlReleasePebLock();
SYSDEPS_ExitThread( exit_code );
}
} }
return ret; return ret;
} }
......
...@@ -589,6 +589,7 @@ typedef struct _TIME_ZONE_INFORMATION{ ...@@ -589,6 +589,7 @@ typedef struct _TIME_ZONE_INFORMATION{
#define CREATE_UNICODE_ENVIRONMENT 0x00000400 #define CREATE_UNICODE_ENVIRONMENT 0x00000400
#define CREATE_SEPARATE_WOW_VDM 0x00000800 #define CREATE_SEPARATE_WOW_VDM 0x00000800
#define CREATE_SHARED_WOW_VDM 0x00001000 #define CREATE_SHARED_WOW_VDM 0x00001000
#define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
#define CREATE_DEFAULT_ERROR_MODE 0x04000000 #define CREATE_DEFAULT_ERROR_MODE 0x04000000
#define CREATE_NO_WINDOW 0x08000000 #define CREATE_NO_WINDOW 0x08000000
#define PROFILE_USER 0x10000000 #define PROFILE_USER 0x10000000
......
...@@ -935,6 +935,7 @@ typedef struct _LDR_RESOURCE_INFO ...@@ -935,6 +935,7 @@ typedef struct _LDR_RESOURCE_INFO
#define SERVERNAME_CURRENT ((HANDLE)NULL) #define SERVERNAME_CURRENT ((HANDLE)NULL)
typedef void (CALLBACK *PNTAPCFUNC)(ULONG_PTR,ULONG_PTR,ULONG_PTR); /* FIXME: not the right name */ typedef void (CALLBACK *PNTAPCFUNC)(ULONG_PTR,ULONG_PTR,ULONG_PTR); /* FIXME: not the right name */
typedef void (CALLBACK *PRTL_THREAD_START_ROUTINE)(LPVOID); /* FIXME: not the right name */
/*********************************************************************** /***********************************************************************
* Function declarations * Function declarations
...@@ -1095,6 +1096,7 @@ NTSTATUS WINAPI RtlCreateProcessParameters(RTL_USER_PROCESS_PARAMETERS**,const ...@@ -1095,6 +1096,7 @@ NTSTATUS WINAPI RtlCreateProcessParameters(RTL_USER_PROCESS_PARAMETERS**,const
NTSTATUS WINAPI RtlCreateSecurityDescriptor(PSECURITY_DESCRIPTOR,DWORD); NTSTATUS WINAPI RtlCreateSecurityDescriptor(PSECURITY_DESCRIPTOR,DWORD);
BOOLEAN WINAPI RtlCreateUnicodeString(PUNICODE_STRING,LPCWSTR); BOOLEAN WINAPI RtlCreateUnicodeString(PUNICODE_STRING,LPCWSTR);
BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz(PUNICODE_STRING,LPCSTR); BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz(PUNICODE_STRING,LPCSTR);
NTSTATUS WINAPI RtlCreateUserThread(HANDLE,const SECURITY_DESCRIPTOR*,BOOLEAN,PVOID,SIZE_T,SIZE_T,PRTL_THREAD_START_ROUTINE,void*,HANDLE*,CLIENT_ID*);
NTSTATUS WINAPI RtlDeleteCriticalSection(RTL_CRITICAL_SECTION *); NTSTATUS WINAPI RtlDeleteCriticalSection(RTL_CRITICAL_SECTION *);
void WINAPI RtlDeleteResource(LPRTL_RWLOCK); void WINAPI RtlDeleteResource(LPRTL_RWLOCK);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment