2020-06-01 12:52:05 +02:00
|
|
|
/*
|
|
|
|
* NT threads support
|
|
|
|
*
|
|
|
|
* Copyright 1996, 2003 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#pragma makedep unix
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
2020-06-03 16:32:02 +02:00
|
|
|
#include <errno.h>
|
2020-06-01 12:52:05 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TIMES_H
|
|
|
|
#include <sys/times.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SYSCALL_H
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define NONAMELESSUNION
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "winternl.h"
|
|
|
|
#include "ddk/wdm.h"
|
|
|
|
#include "wine/server.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/exception.h"
|
|
|
|
#include "unix_private.h"
|
|
|
|
|
|
|
|
#ifndef PTHREAD_STACK_MIN
|
|
|
|
#define PTHREAD_STACK_MIN 16384
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int *nb_threads;
|
|
|
|
|
|
|
|
static inline int get_unix_exit_code( NTSTATUS status )
|
|
|
|
{
|
|
|
|
/* prevent a nonzero exit code to end up truncated to zero in unix */
|
|
|
|
if (status && !(status & 0xff)) return 1;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* pthread_exit_wrapper
|
|
|
|
*/
|
|
|
|
static void pthread_exit_wrapper( int status )
|
|
|
|
{
|
|
|
|
close( ntdll_get_thread_data()->wait_fd[0] );
|
|
|
|
close( ntdll_get_thread_data()->wait_fd[1] );
|
|
|
|
close( ntdll_get_thread_data()->reply_fd );
|
|
|
|
close( ntdll_get_thread_data()->request_fd );
|
|
|
|
pthread_exit( UIntToPtr(status) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* init_threading
|
|
|
|
*/
|
2020-06-04 19:37:35 +02:00
|
|
|
TEB * CDECL init_threading( int *nb_threads_ptr, struct ldt_copy **ldt_copy, SIZE_T *size, BOOL *suspend,
|
|
|
|
unsigned int *cpus, BOOL *wow64, timeout_t *start_time )
|
2020-06-01 12:52:05 +02:00
|
|
|
{
|
2020-06-04 19:37:35 +02:00
|
|
|
TEB *teb;
|
|
|
|
SIZE_T info_size;
|
|
|
|
struct ntdll_thread_data *thread_data;
|
2020-06-01 12:52:05 +02:00
|
|
|
#ifdef __i386__
|
|
|
|
extern struct ldt_copy __wine_ldt_copy;
|
|
|
|
*ldt_copy = &__wine_ldt_copy;
|
|
|
|
#endif
|
|
|
|
nb_threads = nb_threads_ptr;
|
2020-06-04 19:37:35 +02:00
|
|
|
|
|
|
|
teb = virtual_alloc_first_teb();
|
|
|
|
thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
|
|
|
|
thread_data->request_fd = -1;
|
|
|
|
thread_data->reply_fd = -1;
|
|
|
|
thread_data->wait_fd[0] = -1;
|
|
|
|
thread_data->wait_fd[1] = -1;
|
|
|
|
|
|
|
|
signal_init_threading();
|
|
|
|
signal_alloc_thread( teb );
|
|
|
|
signal_init_thread( teb );
|
|
|
|
dbg_init();
|
|
|
|
server_init_process();
|
|
|
|
info_size = server_init_thread( teb->Peb, suspend );
|
2020-06-04 19:48:26 +02:00
|
|
|
virtual_map_user_shared_data();
|
2020-06-04 21:27:22 +02:00
|
|
|
NtCreateKeyedEvent( &keyed_event, GENERIC_READ | GENERIC_WRITE, NULL, 0 );
|
2020-06-04 19:48:26 +02:00
|
|
|
|
2020-06-04 19:37:35 +02:00
|
|
|
if (size) *size = info_size;
|
|
|
|
if (cpus) *cpus = server_cpus;
|
|
|
|
if (wow64) *wow64 = is_wow64;
|
|
|
|
if (start_time) *start_time = server_start_time;
|
|
|
|
return teb;
|
2020-06-01 12:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-03 21:09:13 +02:00
|
|
|
/* info passed to a starting thread */
|
|
|
|
struct startup_info
|
|
|
|
{
|
|
|
|
PRTL_THREAD_START_ROUTINE entry;
|
|
|
|
void *arg;
|
|
|
|
void *relay;
|
|
|
|
HANDLE actctx;
|
|
|
|
};
|
|
|
|
|
2020-06-01 12:52:05 +02:00
|
|
|
/***********************************************************************
|
2020-06-03 16:32:02 +02:00
|
|
|
* start_thread
|
2020-06-03 21:09:13 +02:00
|
|
|
*
|
|
|
|
* Startup routine for a newly created thread.
|
2020-06-01 12:52:05 +02:00
|
|
|
*/
|
2020-06-03 21:09:13 +02:00
|
|
|
static void start_thread( TEB *teb )
|
2020-06-01 12:52:05 +02:00
|
|
|
{
|
2020-06-03 21:09:13 +02:00
|
|
|
struct startup_info *info = (struct startup_info *)(teb + 1);
|
|
|
|
struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
|
|
|
|
struct debug_info debug_info;
|
2020-06-03 16:32:02 +02:00
|
|
|
BOOL suspend;
|
2020-06-03 21:09:13 +02:00
|
|
|
ULONG_PTR cookie;
|
2020-06-03 16:32:02 +02:00
|
|
|
|
2020-06-03 21:09:13 +02:00
|
|
|
debug_info.str_pos = debug_info.out_pos = 0;
|
|
|
|
thread_data->debug_info = &debug_info;
|
|
|
|
thread_data->pthread_id = pthread_self();
|
2020-06-01 12:52:05 +02:00
|
|
|
signal_init_thread( teb );
|
2020-06-04 19:37:35 +02:00
|
|
|
server_init_thread( info->entry, &suspend );
|
2020-06-03 21:09:13 +02:00
|
|
|
if (info->actctx)
|
|
|
|
{
|
|
|
|
RtlActivateActivationContext( 0, info->actctx, &cookie );
|
|
|
|
RtlReleaseActivationContext( info->actctx );
|
|
|
|
}
|
|
|
|
signal_start_thread( info->entry, info->arg, suspend, info->relay, teb );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* create_thread
|
|
|
|
*/
|
2020-06-05 14:09:19 +02:00
|
|
|
NTSTATUS CDECL create_thread( HANDLE *handle, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr,
|
|
|
|
HANDLE process, PRTL_THREAD_START_ROUTINE start, void *param, void *relay,
|
|
|
|
ULONG flags, SIZE_T stack_commit, SIZE_T stack_reserve,
|
|
|
|
CLIENT_ID *id )
|
2020-06-03 21:09:13 +02:00
|
|
|
{
|
|
|
|
sigset_t sigset;
|
|
|
|
pthread_t pthread_id;
|
2020-06-05 14:09:19 +02:00
|
|
|
pthread_attr_t pthread_attr;
|
|
|
|
data_size_t len;
|
|
|
|
struct object_attributes *objattr;
|
2020-06-03 21:09:13 +02:00
|
|
|
struct ntdll_thread_data *thread_data;
|
|
|
|
struct startup_info *info;
|
2020-06-05 14:09:19 +02:00
|
|
|
DWORD tid = 0;
|
|
|
|
int request_pipe[2];
|
2020-06-03 21:09:13 +02:00
|
|
|
SIZE_T extra_stack = PTHREAD_STACK_MIN;
|
2020-06-05 14:09:19 +02:00
|
|
|
HANDLE actctx;
|
2020-06-03 21:09:13 +02:00
|
|
|
TEB *teb;
|
|
|
|
INITIAL_TEB stack;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
2020-06-05 14:09:19 +02:00
|
|
|
if (process != NtCurrentProcess())
|
|
|
|
{
|
|
|
|
apc_call_t call;
|
|
|
|
apc_result_t result;
|
|
|
|
|
|
|
|
memset( &call, 0, sizeof(call) );
|
|
|
|
|
|
|
|
call.create_thread.type = APC_CREATE_THREAD;
|
2020-06-05 15:23:37 +02:00
|
|
|
call.create_thread.flags = flags;
|
2020-06-05 14:09:19 +02:00
|
|
|
call.create_thread.func = wine_server_client_ptr( start );
|
|
|
|
call.create_thread.arg = wine_server_client_ptr( param );
|
|
|
|
call.create_thread.reserve = stack_reserve;
|
|
|
|
call.create_thread.commit = stack_commit;
|
|
|
|
status = server_queue_process_apc( process, &call, &result );
|
|
|
|
if (status != STATUS_SUCCESS) return status;
|
|
|
|
|
|
|
|
if (result.create_thread.status == STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
*handle = wine_server_ptr_handle( result.create_thread.handle );
|
2020-06-05 15:23:37 +02:00
|
|
|
if (id)
|
|
|
|
{
|
|
|
|
id->UniqueProcess = ULongToHandle( result.create_thread.pid );
|
|
|
|
id->UniqueThread = ULongToHandle( result.create_thread.tid );
|
|
|
|
}
|
2020-06-05 14:09:19 +02:00
|
|
|
}
|
|
|
|
return result.create_thread.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((status = alloc_object_attributes( attr, &objattr, &len ))) return status;
|
|
|
|
|
|
|
|
if (server_pipe( request_pipe ) == -1)
|
|
|
|
{
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, objattr );
|
|
|
|
return STATUS_TOO_MANY_OPENED_FILES;
|
|
|
|
}
|
|
|
|
server_send_fd( request_pipe[0] );
|
|
|
|
|
|
|
|
SERVER_START_REQ( new_thread )
|
|
|
|
{
|
|
|
|
req->process = wine_server_obj_handle( process );
|
|
|
|
req->access = access;
|
|
|
|
req->suspend = flags & THREAD_CREATE_FLAGS_CREATE_SUSPENDED;
|
|
|
|
req->request_fd = request_pipe[0];
|
|
|
|
wine_server_add_data( req, objattr, len );
|
|
|
|
if (!(status = wine_server_call( req )))
|
|
|
|
{
|
|
|
|
*handle = wine_server_ptr_handle( reply->handle );
|
|
|
|
tid = reply->tid;
|
|
|
|
}
|
|
|
|
close( request_pipe[0] );
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
|
|
|
|
RtlFreeHeap( GetProcessHeap(), 0, objattr );
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
close( request_pipe[1] );
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
RtlGetActiveActivationContext( &actctx );
|
|
|
|
|
2020-06-03 21:09:13 +02:00
|
|
|
pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
|
|
|
|
|
|
|
|
if ((status = virtual_alloc_teb( &teb ))) goto done;
|
|
|
|
|
|
|
|
if ((status = virtual_alloc_thread_stack( &stack, stack_reserve, stack_commit, &extra_stack )))
|
|
|
|
{
|
|
|
|
virtual_free_teb( teb );
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
teb->ClientId.UniqueProcess = ULongToHandle( GetCurrentProcessId() );
|
|
|
|
teb->ClientId.UniqueThread = ULongToHandle( tid );
|
2020-06-05 14:09:19 +02:00
|
|
|
if (id) *id = teb->ClientId;
|
2020-06-03 21:09:13 +02:00
|
|
|
|
|
|
|
info = (struct startup_info *)(teb + 1);
|
|
|
|
info->entry = start;
|
|
|
|
info->arg = param;
|
|
|
|
info->relay = relay;
|
|
|
|
info->actctx = actctx;
|
|
|
|
|
|
|
|
teb->Tib.StackBase = stack.StackBase;
|
|
|
|
teb->Tib.StackLimit = stack.StackLimit;
|
|
|
|
teb->DeallocationStack = stack.DeallocationStack;
|
|
|
|
|
|
|
|
thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
|
2020-06-05 14:09:19 +02:00
|
|
|
thread_data->request_fd = request_pipe[1];
|
2020-06-03 21:09:13 +02:00
|
|
|
thread_data->reply_fd = -1;
|
|
|
|
thread_data->wait_fd[0] = -1;
|
|
|
|
thread_data->wait_fd[1] = -1;
|
|
|
|
thread_data->start_stack = (char *)teb->Tib.StackBase;
|
|
|
|
|
2020-06-05 14:09:19 +02:00
|
|
|
pthread_attr_init( &pthread_attr );
|
|
|
|
pthread_attr_setstack( &pthread_attr, teb->DeallocationStack,
|
2020-06-03 21:09:13 +02:00
|
|
|
(char *)teb->Tib.StackBase + extra_stack - (char *)teb->DeallocationStack );
|
2020-06-05 14:09:19 +02:00
|
|
|
pthread_attr_setguardsize( &pthread_attr, 0 );
|
|
|
|
pthread_attr_setscope( &pthread_attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread */
|
2020-06-03 21:09:13 +02:00
|
|
|
InterlockedIncrement( nb_threads );
|
2020-06-05 14:09:19 +02:00
|
|
|
if (pthread_create( &pthread_id, &pthread_attr, (void * (*)(void *))start_thread, teb ))
|
2020-06-03 21:09:13 +02:00
|
|
|
{
|
|
|
|
InterlockedDecrement( nb_threads );
|
|
|
|
virtual_free_teb( teb );
|
|
|
|
status = STATUS_NO_MEMORY;
|
|
|
|
}
|
2020-06-05 14:09:19 +02:00
|
|
|
pthread_attr_destroy( &pthread_attr );
|
|
|
|
|
2020-06-03 21:09:13 +02:00
|
|
|
done:
|
|
|
|
pthread_sigmask( SIG_SETMASK, &sigset, NULL );
|
2020-06-05 14:09:19 +02:00
|
|
|
if (!status) return STATUS_SUCCESS;
|
|
|
|
NtClose( *handle );
|
|
|
|
RtlReleaseActivationContext( actctx );
|
|
|
|
close( request_pipe[1] );
|
2020-06-03 21:09:13 +02:00
|
|
|
return status;
|
2020-06-03 16:32:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* start_process
|
|
|
|
*/
|
|
|
|
void CDECL start_process( PRTL_THREAD_START_ROUTINE entry, BOOL suspend, void *relay )
|
|
|
|
{
|
|
|
|
signal_start_thread( entry, NtCurrentTeb()->Peb, suspend, relay, NtCurrentTeb() );
|
2020-06-01 12:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* abort_thread
|
|
|
|
*/
|
|
|
|
void CDECL abort_thread( int status )
|
|
|
|
{
|
|
|
|
pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
|
|
|
|
if (InterlockedDecrement( nb_threads ) <= 0) _exit( get_unix_exit_code( status ));
|
|
|
|
signal_exit_thread( status, pthread_exit_wrapper );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* exit_thread
|
|
|
|
*/
|
|
|
|
void CDECL exit_thread( int status )
|
|
|
|
{
|
2020-06-03 21:26:47 +02:00
|
|
|
static void *prev_teb;
|
|
|
|
TEB *teb;
|
|
|
|
|
2020-06-01 12:52:05 +02:00
|
|
|
pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
|
2020-06-03 21:26:47 +02:00
|
|
|
|
|
|
|
if ((teb = InterlockedExchangePointer( &prev_teb, NtCurrentTeb() )))
|
|
|
|
{
|
|
|
|
struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
|
|
|
|
|
|
|
|
if (thread_data->pthread_id)
|
|
|
|
{
|
|
|
|
pthread_join( thread_data->pthread_id, NULL );
|
|
|
|
virtual_free_teb( teb );
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 12:52:05 +02:00
|
|
|
signal_exit_thread( status, pthread_exit_wrapper );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* exit_process
|
|
|
|
*/
|
|
|
|
void CDECL exit_process( int status )
|
|
|
|
{
|
|
|
|
pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
|
|
|
|
signal_exit_thread( get_unix_exit_code( status ), exit );
|
|
|
|
}
|
2020-06-02 13:11:54 +02:00
|
|
|
|
|
|
|
|
2020-06-03 16:32:02 +02:00
|
|
|
/**********************************************************************
|
|
|
|
* wait_suspend
|
|
|
|
*
|
|
|
|
* Wait until the thread is no longer suspended.
|
|
|
|
*/
|
|
|
|
void wait_suspend( CONTEXT *context )
|
|
|
|
{
|
|
|
|
int saved_errno = errno;
|
|
|
|
|
|
|
|
/* wait with 0 timeout, will only return once the thread is no longer suspended */
|
|
|
|
server_select( NULL, 0, SELECT_INTERRUPTIBLE, 0, context, NULL, NULL );
|
|
|
|
errno = saved_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-02 13:11:54 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* set_thread_context
|
|
|
|
*/
|
|
|
|
NTSTATUS set_thread_context( HANDLE handle, const context_t *context, BOOL *self )
|
|
|
|
{
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
SERVER_START_REQ( set_thread_context )
|
|
|
|
{
|
|
|
|
req->handle = wine_server_obj_handle( handle );
|
|
|
|
wine_server_add_data( req, context, sizeof(*context) );
|
|
|
|
ret = wine_server_call( req );
|
|
|
|
*self = reply->self;
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2020-06-02 14:05:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* get_thread_context
|
|
|
|
*/
|
|
|
|
NTSTATUS get_thread_context( HANDLE handle, context_t *context, unsigned int flags, BOOL *self )
|
|
|
|
{
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
SERVER_START_REQ( get_thread_context )
|
|
|
|
{
|
|
|
|
req->handle = wine_server_obj_handle( handle );
|
|
|
|
req->flags = flags;
|
|
|
|
wine_server_set_reply( req, context, sizeof(*context) );
|
|
|
|
ret = wine_server_call( req );
|
|
|
|
*self = reply->self;
|
|
|
|
handle = wine_server_ptr_handle( reply->handle );
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
|
|
|
|
if (ret == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER timeout;
|
|
|
|
timeout.QuadPart = -1000000;
|
|
|
|
if (NtWaitForSingleObject( handle, FALSE, &timeout ))
|
|
|
|
{
|
|
|
|
NtClose( handle );
|
|
|
|
return STATUS_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
SERVER_START_REQ( get_thread_context )
|
|
|
|
{
|
|
|
|
req->handle = wine_server_obj_handle( handle );
|
|
|
|
req->flags = flags;
|
|
|
|
wine_server_set_reply( req, context, sizeof(*context) );
|
|
|
|
ret = wine_server_call( req );
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|