Commit 20e73d73 authored by Alexandre Julliard's avatar Alexandre Julliard

Moved the constructor for the initial TEB to ntdll so that it runs as

early as possible on startup.
parent 36a6c740
...@@ -96,9 +96,7 @@ typedef struct _PDB ...@@ -96,9 +96,7 @@ typedef struct _PDB
LCID locale; /* c4 Locale to be queried by GetThreadLocale (NT) */ LCID locale; /* c4 Locale to be queried by GetThreadLocale (NT) */
} PDB; } PDB;
PDB current_process; static PDB *current_process;
static PEB_LDR_DATA process_ldr;
static HANDLE main_exe_file; static HANDLE main_exe_file;
static DWORD shutdown_flags = 0; static DWORD shutdown_flags = 0;
...@@ -660,11 +658,9 @@ static RTL_USER_PROCESS_PARAMETERS *init_user_process_params( size_t info_size ) ...@@ -660,11 +658,9 @@ static RTL_USER_PROCESS_PARAMETERS *init_user_process_params( size_t info_size )
*/ */
static BOOL process_init( char *argv[] ) static BOOL process_init( char *argv[] )
{ {
static RTL_USER_PROCESS_PARAMETERS default_params; /* default parameters if no parent */
BOOL ret; BOOL ret;
size_t info_size = 0; size_t info_size = 0;
RTL_USER_PROCESS_PARAMETERS *params = &default_params; RTL_USER_PROCESS_PARAMETERS *params;
HANDLE hstdin, hstdout, hstderr; HANDLE hstdin, hstdout, hstderr;
setbuf(stdout,NULL); setbuf(stdout,NULL);
...@@ -672,16 +668,13 @@ static BOOL process_init( char *argv[] ) ...@@ -672,16 +668,13 @@ static BOOL process_init( char *argv[] )
setlocale(LC_CTYPE,""); setlocale(LC_CTYPE,"");
/* Fill the initial process structure */ /* Fill the initial process structure */
current_process.threads = 1; current_process = (PDB *)NtCurrentTeb()->Peb; /* FIXME: should be a PEB */
current_process.running_threads = 1; params = current_process->ProcessParameters;
current_process.ring0_threads = 1; current_process->threads = 1;
current_process.group = &current_process; current_process->running_threads = 1;
current_process.priority = 8; /* Normal */ current_process->ring0_threads = 1;
current_process.ProcessParameters = &default_params; current_process->group = current_process;
current_process.LdrData = &process_ldr; current_process->priority = 8; /* Normal */
InitializeListHead(&process_ldr.InLoadOrderModuleList);
InitializeListHead(&process_ldr.InMemoryOrderModuleList);
InitializeListHead(&process_ldr.InInitializationOrderModuleList);
/* Setup the server connection */ /* Setup the server connection */
wine_server_init_thread(); wine_server_init_thread();
...@@ -705,7 +698,7 @@ static BOOL process_init( char *argv[] ) ...@@ -705,7 +698,7 @@ static BOOL process_init( char *argv[] )
if (!ret) return FALSE; if (!ret) return FALSE;
/* Create the process heap */ /* Create the process heap */
current_process.heap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL ); current_process->heap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL );
if (info_size == 0) if (info_size == 0)
{ {
...@@ -730,7 +723,7 @@ static BOOL process_init( char *argv[] ) ...@@ -730,7 +723,7 @@ static BOOL process_init( char *argv[] )
else else
{ {
if (!(params = init_user_process_params( info_size ))) return FALSE; if (!(params = init_user_process_params( info_size ))) return FALSE;
current_process.ProcessParameters = params; current_process->ProcessParameters = params;
/* convert value from server: /* convert value from server:
* + 0 => INVALID_HANDLE_VALUE * + 0 => INVALID_HANDLE_VALUE
...@@ -813,13 +806,14 @@ void __wine_process_init( int argc, char *argv[] ) ...@@ -813,13 +806,14 @@ void __wine_process_init( int argc, char *argv[] )
char error[1024]; char error[1024];
DWORD stack_size = 0; DWORD stack_size = 0;
int file_exists; int file_exists;
PEB *peb = NtCurrentTeb()->Peb;
/* Initialize everything */ /* Initialize everything */
if (!process_init( argv )) exit(1); if (!process_init( argv )) exit(1);
argv++; /* remove argv[0] (wine itself) */ argv++; /* remove argv[0] (wine itself) */
if (!(main_exe_name = NtCurrentTeb()->Peb->ProcessParameters->ImagePathName.Buffer)) if (!(main_exe_name = peb->ProcessParameters->ImagePathName.Buffer))
{ {
WCHAR buffer[MAX_PATH]; WCHAR buffer[MAX_PATH];
WCHAR exe_nameW[MAX_PATH]; WCHAR exe_nameW[MAX_PATH];
...@@ -838,8 +832,8 @@ void __wine_process_init( int argc, char *argv[] ) ...@@ -838,8 +832,8 @@ void __wine_process_init( int argc, char *argv[] )
MESSAGE( "wine: cannot open %s\n", debugstr_w(main_exe_name) ); MESSAGE( "wine: cannot open %s\n", debugstr_w(main_exe_name) );
ExitProcess(1); ExitProcess(1);
} }
RtlCreateUnicodeString( &NtCurrentTeb()->Peb->ProcessParameters->ImagePathName, buffer ); RtlCreateUnicodeString( &peb->ProcessParameters->ImagePathName, buffer );
main_exe_name = NtCurrentTeb()->Peb->ProcessParameters->ImagePathName.Buffer; main_exe_name = peb->ProcessParameters->ImagePathName.Buffer;
} }
TRACE( "starting process name=%s file=%p argv[0]=%s\n", TRACE( "starting process name=%s file=%p argv[0]=%s\n",
...@@ -862,7 +856,8 @@ void __wine_process_init( int argc, char *argv[] ) ...@@ -862,7 +856,8 @@ void __wine_process_init( int argc, char *argv[] )
{ {
case BINARY_PE_EXE: case BINARY_PE_EXE:
TRACE( "starting Win32 binary %s\n", debugstr_w(main_exe_name) ); TRACE( "starting Win32 binary %s\n", debugstr_w(main_exe_name) );
if ((current_process.module = load_pe_exe( main_exe_name, main_exe_file ))) goto found; if ((peb->ImageBaseAddress = load_pe_exe( main_exe_name, main_exe_file )))
goto found;
MESSAGE( "wine: could not load %s as Win32 binary\n", debugstr_w(main_exe_name) ); MESSAGE( "wine: could not load %s as Win32 binary\n", debugstr_w(main_exe_name) );
ExitProcess(1); ExitProcess(1);
case BINARY_PE_DLL: case BINARY_PE_DLL:
...@@ -910,8 +905,7 @@ void __wine_process_init( int argc, char *argv[] ) ...@@ -910,8 +905,7 @@ void __wine_process_init( int argc, char *argv[] )
{ {
*p = 0; *p = 0;
/* update the unicode string */ /* update the unicode string */
RtlInitUnicodeString( &NtCurrentTeb()->Peb->ProcessParameters->ImagePathName, RtlInitUnicodeString( &peb->ProcessParameters->ImagePathName, main_exe_name );
main_exe_name );
} }
goto found; goto found;
} }
...@@ -926,9 +920,9 @@ void __wine_process_init( int argc, char *argv[] ) ...@@ -926,9 +920,9 @@ void __wine_process_init( int argc, char *argv[] )
if (!build_command_line( __wine_main_wargv )) goto error; if (!build_command_line( __wine_main_wargv )) goto error;
/* create 32-bit module for main exe */ /* create 32-bit module for main exe */
if (!(current_process.module = BUILTIN32_LoadExeModule( current_process.module, CreateFileW ))) if (!(peb->ImageBaseAddress = BUILTIN32_LoadExeModule( peb->ImageBaseAddress, CreateFileW )))
goto error; goto error;
stack_size = RtlImageNtHeader(current_process.module)->OptionalHeader.SizeOfStackReserve; stack_size = RtlImageNtHeader(peb->ImageBaseAddress)->OptionalHeader.SizeOfStackReserve;
/* allocate main thread stack */ /* allocate main thread stack */
if (!THREAD_InitStack( NtCurrentTeb(), stack_size )) goto error; if (!THREAD_InitStack( NtCurrentTeb(), stack_size )) goto error;
...@@ -1948,8 +1942,8 @@ BOOL WINAPI GetExitCodeProcess( ...@@ -1948,8 +1942,8 @@ BOOL WINAPI GetExitCodeProcess(
*/ */
UINT WINAPI SetErrorMode( UINT mode ) UINT WINAPI SetErrorMode( UINT mode )
{ {
UINT old = current_process.error_mode; UINT old = current_process->error_mode;
current_process.error_mode = mode; current_process->error_mode = mode;
return old; return old;
} }
...@@ -1966,7 +1960,7 @@ UINT WINAPI SetErrorMode( UINT mode ) ...@@ -1966,7 +1960,7 @@ UINT WINAPI SetErrorMode( UINT mode )
DWORD WINAPI TlsAlloc( void ) DWORD WINAPI TlsAlloc( void )
{ {
DWORD i, mask, ret = 0; DWORD i, mask, ret = 0;
DWORD *bits = current_process.tls_bits; DWORD *bits = current_process->tls_bits;
RtlAcquirePebLock(); RtlAcquirePebLock();
if (*bits == 0xffffffff) if (*bits == 0xffffffff)
{ {
...@@ -2000,7 +1994,7 @@ BOOL WINAPI TlsFree( ...@@ -2000,7 +1994,7 @@ BOOL WINAPI TlsFree(
DWORD index) /* [in] TLS Index to free */ DWORD index) /* [in] TLS Index to free */
{ {
DWORD mask = (1 << (index & 31)); DWORD mask = (1 << (index & 31));
DWORD *bits = current_process.tls_bits; DWORD *bits = current_process->tls_bits;
if (index >= 64) if (index >= 64)
{ {
SetLastError( ERROR_INVALID_PARAMETER ); SetLastError( ERROR_INVALID_PARAMETER );
......
...@@ -48,11 +48,6 @@ WINE_DEFAULT_DEBUG_CHANNEL(thread); ...@@ -48,11 +48,6 @@ WINE_DEFAULT_DEBUG_CHANNEL(thread);
WINE_DECLARE_DEBUG_CHANNEL(relay); WINE_DECLARE_DEBUG_CHANNEL(relay);
/* TEB of the initial thread */
static TEB initial_teb;
extern struct _PDB current_process;
/*********************************************************************** /***********************************************************************
* THREAD_InitTEB * THREAD_InitTEB
* *
...@@ -148,32 +143,6 @@ TEB *THREAD_InitStack( TEB *teb, DWORD stack_size ) ...@@ -148,32 +143,6 @@ TEB *THREAD_InitStack( TEB *teb, DWORD stack_size )
/*********************************************************************** /***********************************************************************
* THREAD_Init
*
* Setup the initial thread.
*
* NOTES: The first allocated TEB on NT is at 0x7ffde000.
*/
void THREAD_Init(void)
{
static struct debug_info info; /* debug info for initial thread */
if (!initial_teb.Tib.Self) /* do it only once */
{
THREAD_InitTEB( &initial_teb );
assert( initial_teb.teb_sel );
info.str_pos = info.strings;
info.out_pos = info.output;
initial_teb.debug_info = &info;
initial_teb.Peb = (PEB *)&current_process; /* FIXME */
SYSDEPS_SetCurThread( &initial_teb );
}
}
DECL_GLOBAL_CONSTRUCTOR(thread_init) { THREAD_Init(); }
/***********************************************************************
* THREAD_Start * THREAD_Start
* *
* Start execution of a newly created thread. Does not return. * Start execution of a newly created thread. Does not return.
......
...@@ -18,9 +18,13 @@ ...@@ -18,9 +18,13 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#include "config.h"
#include "wine/port.h"
#include "ntstatus.h" #include "ntstatus.h"
#include "thread.h" #include "thread.h"
#include "winternl.h" #include "winternl.h"
#include "wine/library.h"
#include "wine/server.h" #include "wine/server.h"
#include "wine/debug.h" #include "wine/debug.h"
...@@ -28,6 +32,51 @@ WINE_DEFAULT_DEBUG_CHANNEL(thread); ...@@ -28,6 +32,51 @@ WINE_DEFAULT_DEBUG_CHANNEL(thread);
/*********************************************************************** /***********************************************************************
* thread_init
*
* Setup the initial thread.
*
* NOTES: The first allocated TEB on NT is at 0x7ffde000.
*/
DECL_GLOBAL_CONSTRUCTOR(thread_init)
{
static TEB teb;
static PEB peb;
static PEB_LDR_DATA ldr;
static RTL_USER_PROCESS_PARAMETERS params; /* default parameters if no parent */
static struct debug_info info; /* debug info for initial thread */
if (teb.Tib.Self) return; /* do it only once */
info.str_pos = info.strings;
info.out_pos = info.output;
teb.Tib.ExceptionList = (void *)~0UL;
teb.Tib.StackBase = (void *)~0UL;
teb.Tib.Self = &teb.Tib;
teb.Peb = &peb;
teb.tibflags = TEBF_WIN32;
teb.request_fd = -1;
teb.reply_fd = -1;
teb.wait_fd[0] = -1;
teb.wait_fd[1] = -1;
teb.teb_sel = wine_ldt_alloc_fs();
teb.debug_info = &info;
teb.StaticUnicodeString.MaximumLength = sizeof(teb.StaticUnicodeBuffer);
teb.StaticUnicodeString.Buffer = teb.StaticUnicodeBuffer;
InitializeListHead( &teb.TlsLinks );
peb.ProcessParameters = &params;
peb.LdrData = &ldr;
InitializeListHead( &ldr.InLoadOrderModuleList );
InitializeListHead( &ldr.InMemoryOrderModuleList );
InitializeListHead( &ldr.InInitializationOrderModuleList );
SYSDEPS_SetCurThread( &teb );
}
/***********************************************************************
* NtOpenThread (NTDLL.@) * NtOpenThread (NTDLL.@)
* ZwOpenThread (NTDLL.@) * ZwOpenThread (NTDLL.@)
*/ */
......
...@@ -125,7 +125,7 @@ typedef struct _TEB ...@@ -125,7 +125,7 @@ typedef struct _TEB
/* the following are nt specific fields */ /* the following are nt specific fields */
DWORD pad6[624]; /* --n 238 */ DWORD pad6[624]; /* --n 238 */
UNICODE_STRING StaticUnicodeString; /* -2- bf8 used by advapi32 */ UNICODE_STRING StaticUnicodeString; /* -2- bf8 used by advapi32 */
USHORT StaticUnicodeBuffer[261]; /* -2- c00 used by advapi32 */ WCHAR StaticUnicodeBuffer[261]; /* -2- c00 used by advapi32 */
PVOID DeallocationStack; /* -2- e0c Base of the stack */ PVOID DeallocationStack; /* -2- e0c Base of the stack */
LPVOID TlsSlots[64]; /* -2- e10 Thread local storage */ LPVOID TlsSlots[64]; /* -2- e10 Thread local storage */
LIST_ENTRY TlsLinks; /* -2- f10 */ LIST_ENTRY TlsLinks; /* -2- f10 */
...@@ -145,7 +145,6 @@ typedef struct _TEB ...@@ -145,7 +145,6 @@ typedef struct _TEB
/* scheduler/thread.c */ /* scheduler/thread.c */
extern void THREAD_Init(void);
extern TEB *THREAD_InitStack( TEB *teb, DWORD stack_size ); extern TEB *THREAD_InitStack( TEB *teb, DWORD stack_size );
/* scheduler/sysdeps.c */ /* scheduler/sysdeps.c */
......
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