source: rtems/cpukit/score/include/rtems/score/smpimpl.h @ 8e7db68c

4.115
Last change on this file since 8e7db68c was d134adeb, checked in by Sebastian Huber <sebastian.huber@…>, on 05/23/14 at 13:32:32

score: Fix race condition in SMP startup

Do not use the Per_CPU_Control::started in
_SMP_Start_multitasking_on_secondary_processor() since this field may be
not up to date when a secondary processor reads it. Use the read-only
scheduler assignment instead.

Add a new fatal error SMP_FATAL_MULTITASKING_START_ON_INVALID_PROCESSOR.
This prevents out-of-bounds access.

It is currently not possible to test these fatal errors. One option
would be to fake values of the _CPU_SMP_Get_current_processor(), but
unfortunately this function is inline on some architectures.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup ScoreSMPImpl
5 *
6 * @brief SuperCore SMP Implementation
7 */
8
9/*
10 *  COPYRIGHT (c) 1989-2011.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 *  The license and distribution terms for this file may be
14 *  found in the file LICENSE in this distribution or at
15 *  http://www.rtems.org/license/LICENSE.
16 */
17
18#ifndef _RTEMS_SCORE_SMPIMPL_H
19#define _RTEMS_SCORE_SMPIMPL_H
20
21#include <rtems/score/smp.h>
22#include <rtems/score/percpu.h>
23#include <rtems/fatal.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/**
30 * @defgroup ScoreSMP SMP Support
31 *
32 * @ingroup Score
33 *
34 * This defines the interface of the SuperCore SMP support.
35 *
36 * @{
37 */
38
39/**
40 * @brief SMP message to request a processor shutdown.
41 *
42 * @see _SMP_Send_message().
43 */
44#define SMP_MESSAGE_SHUTDOWN 0x1UL
45
46/**
47 * @brief SMP message to request a test handler invocation.
48 *
49 * @see _SMP_Send_message().
50 */
51#define SMP_MESSAGE_TEST 0x2UL
52
53/**
54 * @brief SMP fatal codes.
55 */
56typedef enum {
57  SMP_FATAL_SHUTDOWN,
58  SMP_FATAL_SHUTDOWN_EARLY,
59  SMP_FATAL_BOOT_PROCESSOR_NOT_ASSIGNED_TO_SCHEDULER,
60  SMP_FATAL_MANDATORY_PROCESSOR_NOT_PRESENT,
61  SMP_FATAL_MULTITASKING_START_ON_UNASSIGNED_PROCESSOR,
62  SMP_FATAL_START_OF_MANDATORY_PROCESSOR_FAILED,
63  SMP_FATAL_SHUTDOWN_RESPONSE,
64  SMP_FATAL_MULTITASKING_START_ON_INVALID_PROCESSOR
65} SMP_Fatal_code;
66
67static inline void _SMP_Fatal( SMP_Fatal_code code )
68{
69  _Terminate( RTEMS_FATAL_SOURCE_SMP, false, code );
70}
71
72/**
73 *  @brief Initialize SMP Handler
74 *
75 *  This method initialize the SMP Handler.
76 */
77#if defined( RTEMS_SMP )
78  void _SMP_Handler_initialize( void );
79#else
80  #define _SMP_Handler_initialize() \
81    do { } while ( 0 )
82#endif
83
84#if defined( RTEMS_SMP )
85
86/**
87 * @brief Performs high-level initialization of a secondary processor and runs
88 * the application threads.
89 *
90 * The low-level initialization code must call this function to hand over the
91 * control of this processor to RTEMS.  Interrupts must be disabled.  It must
92 * be possible to send inter-processor interrupts to this processor.  Since
93 * interrupts are disabled the inter-processor interrupt delivery is postponed
94 * until interrupts are enabled the first time.  Interrupts are enabled during
95 * the execution begin of threads in case they have interrupt level zero (this
96 * is the default).
97 *
98 * The pre-requisites for the call to this function are
99 * - disabled interrupts,
100 * - delivery of inter-processor interrupts is possible,
101 * - a valid stack pointer and enough stack space,
102 * - a valid code memory, and
103 * - a valid BSS section.
104 *
105 * This function must not be called by the main processor.  The main processor
106 * uses _Thread_Start_multitasking() instead.
107 *
108 * This function does not return to the caller.
109 */
110void _SMP_Start_multitasking_on_secondary_processor( void )
111  RTEMS_COMPILER_NO_RETURN_ATTRIBUTE;
112
113typedef void ( *SMP_Test_message_handler )( Per_CPU_Control *cpu_self );
114
115extern SMP_Test_message_handler _SMP_Test_message_handler;
116
117/**
118 * @brief Sets the handler for test messages.
119 *
120 * This handler can be used to test the inter-processor interrupt
121 * implementation.
122 */
123static inline void _SMP_Set_test_message_handler(
124  SMP_Test_message_handler handler
125)
126{
127  _SMP_Test_message_handler = handler;
128}
129
130/**
131 * @brief Interrupt handler for inter-processor interrupts.
132 */
133static inline void _SMP_Inter_processor_interrupt_handler( void )
134{
135  Per_CPU_Control *cpu_self = _Per_CPU_Get();
136
137  if ( _Atomic_Load_ulong( &cpu_self->message, ATOMIC_ORDER_RELAXED ) != 0 ) {
138    unsigned long message = _Atomic_Exchange_ulong(
139      &cpu_self->message,
140      0UL,
141      ATOMIC_ORDER_RELAXED
142    );
143
144    if ( ( message & SMP_MESSAGE_SHUTDOWN ) != 0 ) {
145      _SMP_Fatal( SMP_FATAL_SHUTDOWN_RESPONSE );
146      /* does not continue past here */
147    }
148
149    if ( ( message & SMP_MESSAGE_TEST ) != 0 ) {
150      ( *_SMP_Test_message_handler )( cpu_self );
151    }
152  }
153}
154
155/**
156 *  @brief Sends a SMP message to a processor.
157 *
158 *  The target processor may be the sending processor.
159 *
160 *  @param[in] cpu_index The target processor of the message.
161 *  @param[in] message The message.
162 */
163void _SMP_Send_message( uint32_t cpu_index, unsigned long message );
164
165/**
166 *  @brief Request of others CPUs.
167 *
168 *  This method is invoked by RTEMS when it needs to make a request
169 *  of the other CPUs.  It should be implemented using some type of
170 *  interprocessor interrupt. CPUs not including the originating
171 *  CPU should receive the message.
172 *
173 *  @param [in] message is message to send
174 */
175void _SMP_Broadcast_message(
176  uint32_t  message
177);
178
179#endif /* defined( RTEMS_SMP ) */
180
181/**
182 * @brief Requests a multitasking start on all configured and available
183 * processors.
184 */
185#if defined( RTEMS_SMP )
186  void _SMP_Request_start_multitasking( void );
187#else
188  #define _SMP_Request_start_multitasking() \
189    do { } while ( 0 )
190#endif
191
192/**
193 * @brief Requests a shutdown of all processors.
194 *
195 * This function is a part of the system termination procedure.
196 *
197 * @see _Terminate().
198 */
199#if defined( RTEMS_SMP )
200  void _SMP_Request_shutdown( void );
201#else
202  #define _SMP_Request_shutdown() \
203    do { } while ( 0 )
204#endif
205
206/** @} */
207
208#ifdef __cplusplus
209}
210#endif
211
212#endif
213/* end of include file */
Note: See TracBrowser for help on using the repository browser.