score: Fix CPU context usage on SMP

We must not alter the is executing indicator in
_CPU_Context_Initialize() since this would cause an invalid state during
a self restart.

The is executing indicator must be valid at creation time since
otherwise _Thread_Kill_zombies() uses an undefined value for not started
threads.  This could result in a system life lock.
This commit is contained in:
Sebastian Huber
2014-05-08 10:11:13 +02:00
parent 35a3af7af8
commit 11b05f11d4
14 changed files with 132 additions and 61 deletions

View File

@@ -164,17 +164,17 @@ done_flushing:
nop nop
#if defined(RTEMS_SMP) #if defined(RTEMS_SMP)
! Indicate that this context is no longer executing ! The executing context no longer executes on this processor
stb %g0, [%o0 + SPARC_CONTEXT_CONTROL_IS_EXECUTING_OFFSET] stb %g0, [%o0 + SPARC_CONTEXT_CONTROL_IS_EXECUTING_OFFSET]
! Wait for context to stop execution if necessary ! Wait for heir context to stop execution
1: 1:
ldub [%o1 + SPARC_CONTEXT_CONTROL_IS_EXECUTING_OFFSET], %g1 ldub [%o1 + SPARC_CONTEXT_CONTROL_IS_EXECUTING_OFFSET], %g1
cmp %g1, 0 cmp %g1, 0
bne 1b bne 1b
mov 1, %g1 mov 1, %g1
! Indicate that this context is executing ! The heir context executes now on this processor
stb %g1, [%o1 + SPARC_CONTEXT_CONTROL_IS_EXECUTING_OFFSET] stb %g1, [%o1 + SPARC_CONTEXT_CONTROL_IS_EXECUTING_OFFSET]
#endif #endif

View File

@@ -123,17 +123,11 @@ void _CPU_Context_Initialize(
else else
msr_value &= ~PPC_MSR_FP; msr_value &= ~PPC_MSR_FP;
memset( the_context, 0, sizeof( *the_context ) );
the_ppc_context = ppc_get_context( the_context ); the_ppc_context = ppc_get_context( the_context );
the_ppc_context->gpr1 = sp; the_ppc_context->gpr1 = sp;
the_ppc_context->msr = msr_value; the_ppc_context->msr = msr_value;
the_ppc_context->lr = (uint32_t) entry_point; the_ppc_context->lr = (uint32_t) entry_point;
#ifdef RTEMS_SMP
the_ppc_context->is_executing = false;
#endif
#ifdef __ALTIVEC__ #ifdef __ALTIVEC__
_CPU_Context_initialize_altivec( the_ppc_context ); _CPU_Context_initialize_altivec( the_ppc_context );
#endif #endif

View File

@@ -327,28 +327,26 @@ PROC (_CPU_Context_switch):
stw r2, PPC_CONTEXT_OFFSET_GPR2(r3) stw r2, PPC_CONTEXT_OFFSET_GPR2(r3)
#ifdef RTEMS_SMP #ifdef RTEMS_SMP
/* Indicate that this context is no longer executing */ /* The executing context no longer executes on this processor */
msync msync
li r5, 0 li r5, 0
stb r5, PPC_CONTEXT_OFFSET_IS_EXECUTING(r3) stb r5, PPC_CONTEXT_OFFSET_IS_EXECUTING(r3)
#endif
/* Restore context from r4 */ /* Wait for heir context to stop execution */
restore_context:
#ifdef RTEMS_SMP
/* Wait for context to stop execution if necessary */
1: 1:
lbz r5, PPC_CONTEXT_OFFSET_IS_EXECUTING(r4) lbz r5, PPC_CONTEXT_OFFSET_IS_EXECUTING(r4)
cmpwi r5, 0 cmpwi r5, 0
bne 1b bne 1b
/* Indicate that this context is executing */ /* The heir context executes now on this processor */
li r5, 1 li r5, 1
stb r5, PPC_CONTEXT_OFFSET_IS_EXECUTING(r4) stb r5, PPC_CONTEXT_OFFSET_IS_EXECUTING(r4)
isync isync
#endif #endif
/* Restore context from r4 */
restore_context:
#ifdef __ALTIVEC__ #ifdef __ALTIVEC__
mr r14, r4 mr r14, r4
.extern _CPU_Context_switch_altivec .extern _CPU_Context_switch_altivec

View File

@@ -101,10 +101,6 @@ void _CPU_Context_Initialize(
the_context->thread_id = (uint32_t) tls_area; the_context->thread_id = (uint32_t) tls_area;
#endif #endif
#ifdef RTEMS_SMP
the_context->is_executing = false;
#endif
if ( tls_area != NULL ) { if ( tls_area != NULL ) {
_TLS_TCB_at_area_begin_initialize( tls_area ); _TLS_TCB_at_area_begin_initialize( tls_area );
} }

View File

@@ -68,10 +68,21 @@ DEFINE_FUNCTION_ARM(_CPU_Context_switch)
#endif #endif
#ifdef RTEMS_SMP #ifdef RTEMS_SMP
/* Indicate that this context is no longer executing */ /* The executing context no longer executes on this processor */
dmb dmb
mov r3, #0 mov r3, #0
strb r3, [r0, #ARM_CONTEXT_CONTROL_IS_EXECUTING_OFFSET] strb r3, [r0, #ARM_CONTEXT_CONTROL_IS_EXECUTING_OFFSET]
/* Wait for heir context to stop execution */
1:
ldrb r3, [r1, #ARM_CONTEXT_CONTROL_IS_EXECUTING_OFFSET]
cmp r3, #0
bne 1b
/* The heir context executes now on this processor */
dmb
mov r3, #1
strb r3, [r1, #ARM_CONTEXT_CONTROL_IS_EXECUTING_OFFSET]
#endif #endif
/* Start restoring context */ /* Start restoring context */
@@ -80,19 +91,6 @@ _restore:
clrex clrex
#endif #endif
#ifdef RTEMS_SMP
/* Wait for context to stop execution if necessary */
1:
ldrb r3, [r1, #ARM_CONTEXT_CONTROL_IS_EXECUTING_OFFSET]
cmp r3, #0
bne 1b
/* Indicate that this context is executing */
dmb
mov r3, #1
strb r3, [r1, #ARM_CONTEXT_CONTROL_IS_EXECUTING_OFFSET]
#endif
#ifdef ARM_MULTILIB_HAS_THREAD_ID_REGISTER #ifdef ARM_MULTILIB_HAS_THREAD_ID_REGISTER
ldr r3, [r1, #ARM_CONTEXT_CONTROL_THREAD_ID_OFFSET] ldr r3, [r1, #ARM_CONTEXT_CONTROL_THREAD_ID_OFFSET]
mcr p15, 0, r3, c13, c0, 3 mcr p15, 0, r3, c13, c0, 3

View File

@@ -422,8 +422,20 @@ void _CPU_Context_Initialize(
(_context)->register_sp (_context)->register_sp
#ifdef RTEMS_SMP #ifdef RTEMS_SMP
#define _CPU_Context_Get_is_executing( _context ) \ static inline bool _CPU_Context_Get_is_executing(
(_context)->is_executing const Context_Control *context
)
{
return context->is_executing;
}
static inline void _CPU_Context_Set_is_executing(
Context_Control *context,
bool is_executing
)
{
context->is_executing = is_executing;
}
#endif #endif
#define _CPU_Context_Restart_self( _the_context ) \ #define _CPU_Context_Restart_self( _the_context ) \

View File

@@ -58,24 +58,24 @@ SYM (_CPU_Context_switch):
movl edi,REG_EDI(eax) /* save destination register */ movl edi,REG_EDI(eax) /* save destination register */
#ifdef RTEMS_SMP #ifdef RTEMS_SMP
/* Indicate that this context is no longer executing */ /* The executing context no longer executes on this processor */
movb $0, I386_CONTEXT_CONTROL_IS_EXECUTING_OFFSET(eax) movb $0, I386_CONTEXT_CONTROL_IS_EXECUTING_OFFSET(eax)
#endif #endif
movl HEIRCONTEXT_ARG(esp),eax /* eax = heir threads context */ movl HEIRCONTEXT_ARG(esp),eax /* eax = heir threads context */
restore:
#ifdef RTEMS_SMP #ifdef RTEMS_SMP
/* Wait for context to stop execution if necessary */ /* Wait for heir context to stop execution */
1: 1:
movb I386_CONTEXT_CONTROL_IS_EXECUTING_OFFSET(eax), bl movb I386_CONTEXT_CONTROL_IS_EXECUTING_OFFSET(eax), bl
testb bl, bl testb bl, bl
jne 1b jne 1b
/* Indicate that this context is executing */ /* The heir context executes now on this processor */
movb $1, I386_CONTEXT_CONTROL_IS_EXECUTING_OFFSET(eax) movb $1, I386_CONTEXT_CONTROL_IS_EXECUTING_OFFSET(eax)
#endif #endif
restore:
pushl REG_EFLAGS(eax) /* push eflags */ pushl REG_EFLAGS(eax) /* push eflags */
popf /* restore eflags */ popf /* restore eflags */
movl REG_ESP(eax),esp /* restore stack pointer */ movl REG_ESP(eax),esp /* restore stack pointer */

View File

@@ -167,8 +167,20 @@ typedef struct {
(_context)->esp (_context)->esp
#ifdef RTEMS_SMP #ifdef RTEMS_SMP
#define _CPU_Context_Get_is_executing( _context ) \ static inline bool _CPU_Context_Get_is_executing(
(_context)->is_executing const Context_Control *context
)
{
return context->is_executing;
}
static inline void _CPU_Context_Set_is_executing(
Context_Control *context,
bool is_executing
)
{
context->is_executing = is_executing;
}
#endif #endif
/* /*
@@ -454,12 +466,6 @@ uint32_t _CPU_ISR_Get_level( void );
*/ */
#ifdef RTEMS_SMP
#define _I386_Context_Initialize_is_executing( _the_context ) \
(_the_context)->is_executing = false
#else
#define _I386_Context_Initialize_is_executing( _the_context )
#endif
#define _CPU_Context_Initialize( _the_context, _stack_base, _size, \ #define _CPU_Context_Initialize( _the_context, _stack_base, _size, \
_isr, _entry_point, _is_fp, _tls_area ) \ _isr, _entry_point, _is_fp, _tls_area ) \
@@ -475,7 +481,6 @@ uint32_t _CPU_ISR_Get_level( void );
*((proc_ptr *)(_stack)) = (_entry_point); \ *((proc_ptr *)(_stack)) = (_entry_point); \
(_the_context)->ebp = (void *) 0; \ (_the_context)->ebp = (void *) 0; \
(_the_context)->esp = (void *) _stack; \ (_the_context)->esp = (void *) _stack; \
_I386_Context_Initialize_is_executing( _the_context ); \
} while (0) } while (0)
#define _CPU_Context_Restart_self( _the_context ) \ #define _CPU_Context_Restart_self( _the_context ) \

View File

@@ -1596,10 +1596,30 @@ register struct Per_CPU_Control *_CPU_Per_CPU_current asm( "rX" );
} }
/** /**
* @brief Macro to return the is executing field of the thread context. * @brief Gets the is executing indicator of the thread context.
*
* @param[in] context The context.
*/ */
#define _CPU_Context_Get_is_executing( _context ) \ static inline bool _CPU_Context_Get_is_executing(
( ( _context )->is_executing ) const Context_Control *context
)
{
return context->is_executing;
}
/**
* @brief Sets the is executing indicator of the thread context.
*
* @param[in] context The context.
* @param[in] is_executing The new value for the is executing indicator.
*/
static inline void _CPU_Context_Set_is_executing(
Context_Control *context,
bool is_executing
)
{
context->is_executing = is_executing;
}
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -343,8 +343,20 @@ static inline ppc_context *ppc_get_context( const Context_Control *context )
ppc_get_context(_context)->gpr1 ppc_get_context(_context)->gpr1
#ifdef RTEMS_SMP #ifdef RTEMS_SMP
#define _CPU_Context_Get_is_executing( _context ) \ static inline bool _CPU_Context_Get_is_executing(
ppc_get_context(_context)->is_executing const Context_Control *context
)
{
return ppc_get_context(context)->is_executing;
}
static inline void _CPU_Context_Set_is_executing(
Context_Control *context,
bool is_executing
)
{
ppc_get_context(context)->is_executing = is_executing;
}
#endif #endif
#endif /* ASM */ #endif /* ASM */

View File

@@ -330,10 +330,6 @@ void _CPU_Context_Initialize(
*/ */
the_context->isr_dispatch_disable = 0; the_context->isr_dispatch_disable = 0;
#if defined(RTEMS_SMP)
the_context->is_executing = false;
#endif
if ( tls_area != NULL ) { if ( tls_area != NULL ) {
void *tcb = _TLS_TCB_after_TLS_block_initialize( tls_area ); void *tcb = _TLS_TCB_after_TLS_block_initialize( tls_area );

View File

@@ -488,8 +488,20 @@ typedef struct {
(_context)->o6_sp (_context)->o6_sp
#ifdef RTEMS_SMP #ifdef RTEMS_SMP
#define _CPU_Context_Get_is_executing( _context ) \ static inline bool _CPU_Context_Get_is_executing(
(_context)->is_executing const Context_Control *context
)
{
return context->is_executing;
}
static inline void _CPU_Context_Set_is_executing(
Context_Control *context,
bool is_executing
)
{
context->is_executing = is_executing;
}
#endif #endif
#endif /* ASM */ #endif /* ASM */

View File

@@ -184,6 +184,7 @@ bool _Thread_Initialize(
the_thread->is_scheduled = false; the_thread->is_scheduled = false;
the_thread->is_in_the_air = false; the_thread->is_in_the_air = false;
the_thread->scheduler = scheduler; the_thread->scheduler = scheduler;
_CPU_Context_Set_is_executing( &the_thread->Registers, false );
#endif #endif
_Thread_Debug_set_real_processor( the_thread, cpu ); _Thread_Debug_set_real_processor( the_thread, cpu );

View File

@@ -160,12 +160,39 @@ static void test(test_context *self)
wait_for_finish(); wait_for_finish();
} }
static void test_context_is_executing(void)
{
#if defined(RTEMS_SMP)
Context_Control context;
bool is_executing;
memset(&context, 0, sizeof(context));
is_executing = _CPU_Context_Get_is_executing(&context);
rtems_test_assert(!is_executing);
_CPU_Context_Set_is_executing(&context, true);
is_executing = _CPU_Context_Get_is_executing(&context);
rtems_test_assert(is_executing);
_CPU_Context_Set_is_executing(&context, false);
is_executing = _CPU_Context_Get_is_executing(&context);
rtems_test_assert(!is_executing);
_CPU_Context_Set_is_executing(&context, true);
_CPU_Context_Initialize(&context, NULL, 0, 0, NULL, false, NULL);
is_executing = _CPU_Context_Get_is_executing(&context);
rtems_test_assert(is_executing);
#endif
}
static void Init(rtems_task_argument arg) static void Init(rtems_task_argument arg)
{ {
test_context *self = &test_instance; test_context *self = &test_instance;
TEST_BEGIN(); TEST_BEGIN();
test_context_is_executing();
test(self); test(self);
TEST_END(); TEST_END();