ntdll: Move signal stack size computation to the common code.

Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Alexandre Julliard 2020-04-28 13:26:34 +02:00
parent ffb7c595c6
commit 9650f1d3a1
7 changed files with 25 additions and 57 deletions

View File

@ -77,6 +77,9 @@ extern LPCSTR debugstr_us( const UNICODE_STRING *str ) DECLSPEC_HIDDEN;
extern LPCSTR debugstr_ObjectAttributes(const OBJECT_ATTRIBUTES *oa) DECLSPEC_HIDDEN;
/* init routines */
extern SIZE_T signal_stack_size DECLSPEC_HIDDEN;
extern SIZE_T signal_stack_mask DECLSPEC_HIDDEN;
extern SIZE_T signal_stack_align DECLSPEC_HIDDEN;
extern void signal_init_threading(void) DECLSPEC_HIDDEN;
extern NTSTATUS signal_alloc_thread( TEB **teb ) DECLSPEC_HIDDEN;
extern void signal_free_thread( TEB *teb ) DECLSPEC_HIDDEN;

View File

@ -966,22 +966,12 @@ void signal_init_threading(void)
*/
NTSTATUS signal_alloc_thread( TEB **teb )
{
static size_t sigstack_alignment;
SIZE_T size;
SIZE_T size = signal_stack_mask + 1;
NTSTATUS status;
if (!sigstack_alignment)
{
size_t min_size = page_size;
/* find the first power of two not smaller than min_size */
while ((1u << sigstack_alignment) < min_size) sigstack_alignment++;
assert( sizeof(TEB) <= min_size );
}
size = 1 << sigstack_alignment;
*teb = NULL;
if (!(status = virtual_alloc_aligned( (void **)teb, 0, &size, MEM_COMMIT | MEM_TOP_DOWN,
PAGE_READWRITE, sigstack_alignment )))
PAGE_READWRITE, signal_stack_align )))
{
(*teb)->Tib.Self = &(*teb)->Tib;
(*teb)->Tib.ExceptionList = (void *)~0UL;

View File

@ -111,7 +111,6 @@ static DWORD64 get_fault_esr( ucontext_t *sigcontext )
#endif /* linux */
static const size_t teb_size = 0x2000; /* we reserve two pages for the TEB */
static const size_t signal_stack_size = max( MINSIGSTKSZ, 8192 );
/* stack layout when calling an exception raise function */
struct stack_layout

View File

@ -474,8 +474,6 @@ struct stack_layout
typedef int (*wine_signal_handler)(unsigned int sig);
static const size_t teb_size = 4096; /* we reserve one page for the TEB */
static size_t signal_stack_mask;
static size_t signal_stack_size;
static ULONG first_ldt_entry = 32;
@ -2592,26 +2590,14 @@ void signal_init_threading(void)
*/
NTSTATUS signal_alloc_thread( TEB **teb )
{
static size_t sigstack_alignment;
struct x86_thread_data *thread_data;
SIZE_T size;
SIZE_T size = signal_stack_mask + 1;
void *addr = NULL;
NTSTATUS status;
int first_thread = !sigstack_alignment;
static int first_thread = 1;
if (!sigstack_alignment)
{
size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
/* find the first power of two not smaller than min_size */
sigstack_alignment = 12;
while ((1u << sigstack_alignment) < min_size) sigstack_alignment++;
signal_stack_mask = (1 << sigstack_alignment) - 1;
signal_stack_size = (1 << sigstack_alignment) - teb_size;
}
size = signal_stack_mask + 1;
if (!(status = virtual_alloc_aligned( &addr, 0, &size, MEM_COMMIT | MEM_TOP_DOWN,
PAGE_READWRITE, sigstack_alignment )))
PAGE_READWRITE, signal_stack_align )))
{
*teb = addr;
(*teb)->Tib.Self = &(*teb)->Tib;
@ -2623,6 +2609,7 @@ NTSTATUS signal_alloc_thread( TEB **teb )
NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
status = STATUS_TOO_MANY_THREADS;
}
first_thread = 0;
}
return status;
}

View File

@ -1027,22 +1027,12 @@ void signal_init_threading(void)
*/
NTSTATUS signal_alloc_thread( TEB **teb )
{
static size_t sigstack_alignment;
SIZE_T size;
SIZE_T size = signal_stack_mask + 1;
NTSTATUS status;
if (!sigstack_alignment)
{
size_t min_size = page_size; /* this is just for the TEB, we don't use a signal stack yet */
/* find the first power of two not smaller than min_size */
while ((1u << sigstack_alignment) < min_size) sigstack_alignment++;
assert( sizeof(TEB) <= min_size );
}
size = 1 << sigstack_alignment;
*teb = NULL;
if (!(status = virtual_alloc_aligned( (void **)teb, 0, &size, MEM_COMMIT | MEM_TOP_DOWN,
PAGE_READWRITE, sigstack_alignment )))
PAGE_READWRITE, signal_stack_align )))
{
(*teb)->Tib.Self = &(*teb)->Tib;
(*teb)->Tib.ExceptionList = (void *)~0UL;

View File

@ -282,7 +282,6 @@ enum i386_trap_code
};
static const size_t teb_size = 0x2000; /* we reserve two pages for the TEB */
static size_t signal_stack_size;
typedef void (*raise_func)( EXCEPTION_RECORD *rec, CONTEXT *context );
@ -3120,24 +3119,12 @@ void signal_init_threading(void)
*/
NTSTATUS signal_alloc_thread( TEB **teb )
{
static size_t sigstack_alignment;
SIZE_T size;
SIZE_T size = signal_stack_mask + 1;
NTSTATUS status;
if (!sigstack_alignment)
{
size_t min_size = teb_size + max( MINSIGSTKSZ, 8192 );
/* find the first power of two not smaller than min_size */
sigstack_alignment = 12;
while ((1u << sigstack_alignment) < min_size) sigstack_alignment++;
signal_stack_size = (1 << sigstack_alignment) - teb_size;
assert( sizeof(TEB) <= teb_size );
}
size = 1 << sigstack_alignment;
*teb = NULL;
if (!(status = virtual_alloc_aligned( (void **)teb, 0, &size, MEM_COMMIT | MEM_TOP_DOWN,
PAGE_READWRITE, sigstack_alignment )))
PAGE_READWRITE, signal_stack_align )))
{
(*teb)->Tib.Self = &(*teb)->Tib;
(*teb)->Tib.ExceptionList = (void *)~0UL;

View File

@ -31,6 +31,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
@ -159,6 +160,10 @@ static void *address_space_start = (void *)0x10000;
#endif /* __i386__ */
static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
SIZE_T signal_stack_size = 0;
SIZE_T signal_stack_mask = 0;
SIZE_T signal_stack_align = 0;
#define ROUND_ADDR(addr,mask) \
((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
@ -1974,6 +1979,13 @@ void virtual_init(void)
}
}
size = ROUND_SIZE( 0, sizeof(TEB) ) + max( MINSIGSTKSZ, 8192 );
/* find the first power of two not smaller than size */
signal_stack_align = page_shift;
while ((1u << signal_stack_align) < size) signal_stack_align++;
signal_stack_mask = (1 << signal_stack_align) - 1;
signal_stack_size = (1 << signal_stack_align) - ROUND_SIZE( 0, sizeof(TEB) );
/* try to find space in a reserved area for the views and pages protection table */
#ifdef _WIN64
pages_vprot_size = ((size_t)address_space_limit >> page_shift >> pages_vprot_shift) + 1;