From 524839568d8df72bb3d62d64cb1b927bc8dbbbf1 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Wed, 3 Mar 2021 09:23:20 +0100 Subject: score: Ensure stack alignment requirement Make sure that a user-provided stack size is the minimum size allocated for the stack. Make sure we meet the stack alignment requirement also for CPU ports with CPU_STACK_ALIGNMENT > CPU_HEAP_ALIGNMENT. --- cpukit/score/src/threadinitialize.c | 5 +++++ cpukit/score/src/tlsallocsize.c | 30 +++++++++++++++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-) (limited to 'cpukit/score') diff --git a/cpukit/score/src/threadinitialize.c b/cpukit/score/src/threadinitialize.c index 3a331ed269..f11e35dcf3 100644 --- a/cpukit/score/src/threadinitialize.c +++ b/cpukit/score/src/threadinitialize.c @@ -107,6 +107,7 @@ static bool _Thread_Try_initialize( size_t i; char *stack_begin; char *stack_end; + uintptr_t stack_align; Scheduler_Node *scheduler_node; #if defined(RTEMS_SMP) Scheduler_Node *scheduler_node_for_index; @@ -128,8 +129,12 @@ static bool _Thread_Try_initialize( (char *) the_thread + add_on->source_offset; } + /* Set up the properly aligned stack area begin and end */ stack_begin = config->stack_area; stack_end = stack_begin + config->stack_size; + stack_align = CPU_STACK_ALIGNMENT; + stack_begin = (char *) RTEMS_ALIGN_UP( (uintptr_t) stack_begin, stack_align ); + stack_end = (char *) RTEMS_ALIGN_DOWN( (uintptr_t) stack_end, stack_align ); /* Allocate floating-point context in stack area */ #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) diff --git a/cpukit/score/src/tlsallocsize.c b/cpukit/score/src/tlsallocsize.c index e7854c677a..d761f3b6cf 100644 --- a/cpukit/score/src/tlsallocsize.c +++ b/cpukit/score/src/tlsallocsize.c @@ -48,7 +48,6 @@ uintptr_t _TLS_Get_allocation_size( void ) { uintptr_t size; uintptr_t allocation_size; - uintptr_t alignment; size = _TLS_Get_size(); @@ -59,25 +58,34 @@ uintptr_t _TLS_Get_allocation_size( void ) allocation_size = _TLS_Allocation_size; if ( allocation_size == 0 ) { - allocation_size = _TLS_Heap_align_up( size ); - alignment = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment ); + uintptr_t alignment; + + alignment = _TLS_Align_up( (uintptr_t) _TLS_Alignment ); + + allocation_size = size; + allocation_size += _TLS_Get_thread_control_block_area_size( alignment ); +#ifndef __i386__ + allocation_size += sizeof( TLS_Dynamic_thread_vector ); +#endif + + /* + * The TLS area is allocated in the thread storage area. Each allocation + * shall meet the stack alignment requirement. + */ + allocation_size = _TLS_Align_up( allocation_size ); /* * The stack allocator does not support aligned allocations. Allocate * enough to do the alignment manually. */ - if ( alignment > CPU_HEAP_ALIGNMENT ) { - allocation_size += alignment; + if ( alignment > CPU_STACK_ALIGNMENT ) { + _Assert( alignment % CPU_STACK_ALIGNMENT == 0 ); + allocation_size += alignment - CPU_STACK_ALIGNMENT; } - allocation_size += _TLS_Get_thread_control_block_area_size( alignment ); - -#ifndef __i386__ - allocation_size += sizeof(TLS_Dynamic_thread_vector); -#endif - if ( _Thread_Maximum_TLS_size != 0 ) { if ( allocation_size <= _Thread_Maximum_TLS_size ) { + _Assert( _Thread_Maximum_TLS_size % CPU_STACK_ALIGNMENT == 0 ); allocation_size = _Thread_Maximum_TLS_size; } else { _Internal_error( INTERNAL_ERROR_TOO_LARGE_TLS_SIZE ); -- cgit v1.2.3