Module: wine Branch: master Commit: 7a7ef6a64c5d69376c86303e7ce6bfc74ea48cfb URL: http://source.winehq.org/git/wine.git/?a=commit;h=7a7ef6a64c5d69376c86303e7c...
Author: Alexandre Julliard julliard@winehq.org Date: Wed Mar 26 17:08:38 2008 +0100
kernel32: Moved the allocation of the process stack to ntdll.
---
dlls/kernel32/process.c | 47 +------------------------- dlls/ntdll/loader.c | 82 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 71 insertions(+), 58 deletions(-)
diff --git a/dlls/kernel32/process.c b/dlls/kernel32/process.c index 054fbc0..8d040bd 100644 --- a/dlls/kernel32/process.c +++ b/dlls/kernel32/process.c @@ -52,10 +52,6 @@ #include "wine/unicode.h" #include "wine/debug.h"
-#ifdef HAVE_VALGRIND_MEMCHECK_H -#include <valgrind/memcheck.h> -#endif - WINE_DEFAULT_DEBUG_CHANNEL(process); WINE_DECLARE_DEBUG_CHANNEL(file); WINE_DECLARE_DEBUG_CHANNEL(relay); @@ -818,44 +814,6 @@ static HANDLE start_wineboot(void)
/*********************************************************************** - * init_stack - * - * Allocate the stack of new process. - */ -static void *init_stack(void) -{ - void *base; - SIZE_T stack_size, page_size = getpagesize(); - IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress ); - - stack_size = max( nt->OptionalHeader.SizeOfStackReserve, nt->OptionalHeader.SizeOfStackCommit ); - stack_size += page_size; /* for the guard page */ - stack_size = (stack_size + 0xffff) & ~0xffff; /* round to 64K boundary */ - if (stack_size < 1024 * 1024) stack_size = 1024 * 1024; /* Xlib needs a large stack */ - - if (!(base = VirtualAlloc( NULL, stack_size, MEM_COMMIT, PAGE_READWRITE ))) - { - ERR( "failed to allocate main process stack\n" ); - ExitProcess( 1 ); - } - - /* note: limit is lower than base since the stack grows down */ - NtCurrentTeb()->DeallocationStack = base; - NtCurrentTeb()->Tib.StackBase = (char *)base + stack_size; - NtCurrentTeb()->Tib.StackLimit = (char *)base + page_size; - -#ifdef VALGRIND_STACK_REGISTER - /* no need to de-register the stack as it's the one of the main thread */ - VALGRIND_STACK_REGISTER(NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase); -#endif - - /* setup guard page */ - VirtualProtect( base, page_size, PAGE_NOACCESS, NULL ); - return NtCurrentTeb()->Tib.StackBase; -} - - -/*********************************************************************** * start_process * * Startup routine of a new process. Runs on the new process stack. @@ -868,8 +826,6 @@ static void start_process( void *arg ) IMAGE_NT_HEADERS *nt; LPTHREAD_START_ROUTINE entry;
- LdrInitializeThunk( 0, 0, 0, 0 ); - nt = RtlImageNtHeader( peb->ImageBaseAddress ); entry = (LPTHREAD_START_ROUTINE)((char *)peb->ImageBaseAddress + nt->OptionalHeader.AddressOfEntryPoint); @@ -1000,8 +956,9 @@ void __wine_kernel_init(void) CloseHandle( boot_event ); }
+ LdrInitializeThunk( 0, 0, 0, 0 ); /* switch to the new stack */ - wine_switch_to_stack( start_process, NULL, init_stack() ); + wine_switch_to_stack( start_process, NULL, NtCurrentTeb()->Tib.StackBase );
error: ExitProcess( GetLastError() ); diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c index 811540b..f17c0c0 100644 --- a/dlls/ntdll/loader.c +++ b/dlls/ntdll/loader.c @@ -24,6 +24,12 @@
#include <assert.h> #include <stdarg.h> +#ifdef HAVE_SYS_MMAN_H +# include <sys/mman.h> +#endif +#ifdef HAVE_VALGRIND_MEMCHECK_H +# include <valgrind/memcheck.h> +#endif
#define NONAMELESSUNION #define NONAMELESSSTRUCT @@ -2313,6 +2319,65 @@ PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE hModule) }
+/*********************************************************************** + * alloc_process_stack + * + * Allocate the stack of new process. + */ +static NTSTATUS alloc_process_stack( IMAGE_NT_HEADERS *nt ) +{ + NTSTATUS status; + void *base = NULL; + SIZE_T stack_size, page_size = getpagesize(); + + stack_size = max( nt->OptionalHeader.SizeOfStackReserve, nt->OptionalHeader.SizeOfStackCommit ); + stack_size += page_size; /* for the guard page */ + stack_size = (stack_size + 0xffff) & ~0xffff; /* round to 64K boundary */ + if (stack_size < 1024 * 1024) stack_size = 1024 * 1024; /* Xlib needs a large stack */ + + if ((status = NtAllocateVirtualMemory( GetCurrentProcess(), &base, 16, &stack_size, + MEM_COMMIT, PAGE_READWRITE ))) + return status; + + /* note: limit is lower than base since the stack grows down */ + NtCurrentTeb()->DeallocationStack = base; + NtCurrentTeb()->Tib.StackBase = (char *)base + stack_size; + NtCurrentTeb()->Tib.StackLimit = (char *)base + page_size; + +#ifdef VALGRIND_STACK_REGISTER + /* no need to de-register the stack as it's the one of the main thread */ + VALGRIND_STACK_REGISTER(NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase); +#endif + + /* setup guard page */ + NtProtectVirtualMemory( GetCurrentProcess(), &base, &page_size, PAGE_NOACCESS, NULL ); + return STATUS_SUCCESS; +} + + +/*********************************************************************** + * attach_process_dlls + * + * Initial attach to all the dlls loaded by the process. + */ +static NTSTATUS attach_process_dlls( void *wm ) +{ + NTSTATUS status; + + RtlEnterCriticalSection( &loader_section ); + if ((status = process_attach( wm, (LPVOID)1 )) != STATUS_SUCCESS) + { + if (last_failed_modref) + ERR( "%s failed to initialize, aborting\n", + debugstr_w(last_failed_modref->ldr.BaseDllName.Buffer) + 1 ); + return status; + } + attach_implicitly_loaded_dlls( (LPVOID)1 ); + RtlLeaveCriticalSection( &loader_section ); + return status; +} + + /****************************************************************** * LdrInitializeThunk (NTDLL.@) * @@ -2344,8 +2409,8 @@ void WINAPI LdrInitializeThunk( ULONG unknown1, ULONG unknown2, ULONG unknown3, RemoveEntryList( &wm->ldr.InLoadOrderModuleList ); InsertHeadList( &peb->LdrData->InLoadOrderModuleList, &wm->ldr.InLoadOrderModuleList );
- status = server_init_process_done(); - if (status != STATUS_SUCCESS) goto error; + if ((status = alloc_process_stack( nt )) != STATUS_SUCCESS) goto error; + if ((status = server_init_process_done()) != STATUS_SUCCESS) goto error;
actctx_init(); load_path = NtCurrentTeb()->Peb->ProcessParameters->DllPath.Buffer; @@ -2355,17 +2420,8 @@ void WINAPI LdrInitializeThunk( ULONG unknown1, ULONG unknown2, ULONG unknown3,
pthread_functions.sigprocmask( SIG_UNBLOCK, &server_block_set, NULL );
- RtlEnterCriticalSection( &loader_section ); - - if ((status = process_attach( wm, (LPVOID)1 )) != STATUS_SUCCESS) - { - if (last_failed_modref) - ERR( "%s failed to initialize, aborting\n", debugstr_w(last_failed_modref->ldr.BaseDllName.Buffer) + 1 ); - goto error; - } - attach_implicitly_loaded_dlls( (LPVOID)1 ); - - RtlLeaveCriticalSection( &loader_section ); + status = wine_call_on_stack( attach_process_dlls, wm, NtCurrentTeb()->Tib.StackBase ); + if (status != STATUS_SUCCESS) goto error;
if (nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE) VIRTUAL_UseLargeAddressSpace(); return;