1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Multiprocessing Communications Interface (MPCI) Handler |
---|
5 | * @ingroup RTEMSScoreMPCI |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2014. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/score/mpciimpl.h> |
---|
22 | #include <rtems/score/coresemimpl.h> |
---|
23 | #include <rtems/score/interr.h> |
---|
24 | #include <rtems/score/objectmp.h> |
---|
25 | #include <rtems/score/stackimpl.h> |
---|
26 | #include <rtems/score/sysstate.h> |
---|
27 | #include <rtems/score/schedulerimpl.h> |
---|
28 | #include <rtems/score/threadimpl.h> |
---|
29 | #include <rtems/score/threadqimpl.h> |
---|
30 | #include <rtems/sysinit.h> |
---|
31 | |
---|
32 | RTEMS_STATIC_ASSERT( |
---|
33 | sizeof(MPCI_Internal_packet) <= MP_PACKET_MINIMUM_PACKET_SIZE, |
---|
34 | MPCI_Internal_packet |
---|
35 | ); |
---|
36 | |
---|
37 | #define MPCI_SEMAPHORE_TQ_OPERATIONS &_Thread_queue_Operations_FIFO |
---|
38 | |
---|
39 | bool _System_state_Is_multiprocessing; |
---|
40 | |
---|
41 | /** |
---|
42 | * This is the core semaphore which the MPCI Receive Server blocks on. |
---|
43 | */ |
---|
44 | CORE_semaphore_Control _MPCI_Semaphore; |
---|
45 | |
---|
46 | Thread_queue_Control _MPCI_Remote_blocked_threads = |
---|
47 | THREAD_QUEUE_INITIALIZER( "MPCI Remote Blocked Threads" ); |
---|
48 | |
---|
49 | MPCI_Control *_MPCI_table; |
---|
50 | |
---|
51 | Thread_Control *_MPCI_Receive_server_tcb; |
---|
52 | |
---|
53 | MPCI_Packet_processor _MPCI_Packet_processors[ MP_PACKET_CLASSES_LAST + 1 ]; |
---|
54 | |
---|
55 | static void _MPCI_Handler_early_initialization( void ) |
---|
56 | { |
---|
57 | /* |
---|
58 | * Initialize the system state based on whether this is an MP system. |
---|
59 | * In an MP configuration, internally we view single processor |
---|
60 | * systems as a very restricted multiprocessor system. |
---|
61 | */ |
---|
62 | if ( _MPCI_Configuration.maximum_nodes > 1 ) { |
---|
63 | _System_state_Is_multiprocessing = true; |
---|
64 | } |
---|
65 | |
---|
66 | _Objects_MP_Handler_early_initialization(); |
---|
67 | } |
---|
68 | |
---|
69 | static void _MPCI_Handler_initialization( void ) |
---|
70 | { |
---|
71 | MPCI_Control *users_mpci_table; |
---|
72 | |
---|
73 | _Objects_MP_Handler_initialization(); |
---|
74 | |
---|
75 | users_mpci_table = _MPCI_Configuration.User_mpci_table; |
---|
76 | |
---|
77 | if ( _System_state_Is_multiprocessing && !users_mpci_table ) |
---|
78 | _Internal_error( INTERNAL_ERROR_NO_MPCI ); |
---|
79 | |
---|
80 | _MPCI_table = users_mpci_table; |
---|
81 | |
---|
82 | if ( !_System_state_Is_multiprocessing ) |
---|
83 | return; |
---|
84 | |
---|
85 | /* |
---|
86 | * Register the MP Process Packet routine. |
---|
87 | */ |
---|
88 | |
---|
89 | _MPCI_Register_packet_processor( |
---|
90 | MP_PACKET_MPCI_INTERNAL, |
---|
91 | _MPCI_Internal_packets_Process_packet |
---|
92 | ); |
---|
93 | |
---|
94 | /* |
---|
95 | * Create the counting semaphore used by the MPCI Receive Server. |
---|
96 | */ |
---|
97 | |
---|
98 | _CORE_semaphore_Initialize( |
---|
99 | &_MPCI_Semaphore, |
---|
100 | 0 /* initial_value */ |
---|
101 | ); |
---|
102 | } |
---|
103 | |
---|
104 | static void _MPCI_Create_server( void ) |
---|
105 | { |
---|
106 | Thread_Entry_information entry = { |
---|
107 | .adaptor = _Thread_Entry_adaptor_numeric, |
---|
108 | .Kinds = { |
---|
109 | .Numeric = { |
---|
110 | .entry = _MPCI_Receive_server |
---|
111 | } |
---|
112 | } |
---|
113 | }; |
---|
114 | ISR_lock_Context lock_context; |
---|
115 | Objects_Name name; |
---|
116 | |
---|
117 | |
---|
118 | if ( !_System_state_Is_multiprocessing ) |
---|
119 | return; |
---|
120 | |
---|
121 | /* |
---|
122 | * Initialize the MPCI Receive Server |
---|
123 | */ |
---|
124 | |
---|
125 | _MPCI_Receive_server_tcb = _Thread_Internal_allocate(); |
---|
126 | |
---|
127 | name.name_u32 = _Objects_Build_name( 'M', 'P', 'C', 'I' ); |
---|
128 | _Thread_Initialize( |
---|
129 | &_Thread_Information, |
---|
130 | _MPCI_Receive_server_tcb, |
---|
131 | &_Scheduler_Table[ 0 ], |
---|
132 | NULL, /* allocate the stack */ |
---|
133 | _Stack_Minimum() + |
---|
134 | CPU_MPCI_RECEIVE_SERVER_EXTRA_STACK + |
---|
135 | _MPCI_Configuration.extra_mpci_receive_server_stack, |
---|
136 | CPU_ALL_TASKS_ARE_FP, |
---|
137 | PRIORITY_PSEUDO_ISR, |
---|
138 | false, /* no preempt */ |
---|
139 | THREAD_CPU_BUDGET_ALGORITHM_NONE, |
---|
140 | NULL, /* no budget algorithm callout */ |
---|
141 | 0, /* all interrupts enabled */ |
---|
142 | name |
---|
143 | ); |
---|
144 | |
---|
145 | _ISR_lock_ISR_disable( &lock_context ); |
---|
146 | _Thread_Start( _MPCI_Receive_server_tcb, &entry, &lock_context ); |
---|
147 | } |
---|
148 | |
---|
149 | static void _MPCI_Initialization( void ) |
---|
150 | { |
---|
151 | (*_MPCI_table->initialization)(); |
---|
152 | } |
---|
153 | |
---|
154 | void _MPCI_Register_packet_processor( |
---|
155 | MP_packet_Classes the_class, |
---|
156 | MPCI_Packet_processor the_packet_processor |
---|
157 | |
---|
158 | ) |
---|
159 | { |
---|
160 | _MPCI_Packet_processors[ the_class ] = the_packet_processor; |
---|
161 | } |
---|
162 | |
---|
163 | MP_packet_Prefix *_MPCI_Get_packet ( void ) |
---|
164 | { |
---|
165 | MP_packet_Prefix *the_packet; |
---|
166 | |
---|
167 | (*_MPCI_table->get_packet)( &the_packet ); |
---|
168 | |
---|
169 | if ( the_packet == NULL ) |
---|
170 | _Internal_error( INTERNAL_ERROR_OUT_OF_PACKETS ); |
---|
171 | |
---|
172 | /* |
---|
173 | * Put in a default timeout that will be used for |
---|
174 | * all packets that do not otherwise have a timeout. |
---|
175 | */ |
---|
176 | |
---|
177 | the_packet->timeout = MPCI_DEFAULT_TIMEOUT; |
---|
178 | |
---|
179 | return the_packet; |
---|
180 | } |
---|
181 | |
---|
182 | void _MPCI_Return_packet ( |
---|
183 | MP_packet_Prefix *the_packet |
---|
184 | ) |
---|
185 | { |
---|
186 | (*_MPCI_table->return_packet)( the_packet ); |
---|
187 | } |
---|
188 | |
---|
189 | void _MPCI_Send_process_packet ( |
---|
190 | uint32_t destination, |
---|
191 | MP_packet_Prefix *the_packet |
---|
192 | ) |
---|
193 | { |
---|
194 | the_packet->source_tid = _Thread_Executing->Object.id; |
---|
195 | the_packet->to_convert = |
---|
196 | ( the_packet->to_convert - sizeof(MP_packet_Prefix) ) / sizeof(uint32_t); |
---|
197 | |
---|
198 | (*_MPCI_table->send_packet)( destination, the_packet ); |
---|
199 | } |
---|
200 | |
---|
201 | static void _MPCI_Enqueue_callout( |
---|
202 | Thread_queue_Queue *queue, |
---|
203 | Thread_Control *the_thread, |
---|
204 | struct Per_CPU_Control *cpu_self, |
---|
205 | Thread_queue_Context *queue_context |
---|
206 | ) |
---|
207 | { |
---|
208 | _Thread_queue_Add_timeout_ticks( queue, the_thread, cpu_self, queue_context ); |
---|
209 | _Thread_Dispatch_unnest( cpu_self ); |
---|
210 | } |
---|
211 | |
---|
212 | Status_Control _MPCI_Send_request_packet( |
---|
213 | uint32_t destination, |
---|
214 | MP_packet_Prefix *the_packet, |
---|
215 | States_Control extra_state |
---|
216 | ) |
---|
217 | { |
---|
218 | Per_CPU_Control *cpu_self; |
---|
219 | Thread_queue_Context queue_context; |
---|
220 | Thread_Control *executing; |
---|
221 | |
---|
222 | /* |
---|
223 | * See if we need a default timeout |
---|
224 | */ |
---|
225 | |
---|
226 | if (the_packet->timeout == MPCI_DEFAULT_TIMEOUT) |
---|
227 | the_packet->timeout = _MPCI_table->default_timeout; |
---|
228 | |
---|
229 | _Thread_queue_Context_initialize( &queue_context ); |
---|
230 | _Thread_queue_Context_set_thread_state( |
---|
231 | &queue_context, |
---|
232 | STATES_WAITING_FOR_RPC_REPLY | extra_state |
---|
233 | ); |
---|
234 | _Thread_queue_Context_set_timeout_ticks( &queue_context, the_packet->timeout ); |
---|
235 | _Thread_queue_Context_set_enqueue_callout( |
---|
236 | &queue_context, |
---|
237 | _MPCI_Enqueue_callout |
---|
238 | ); |
---|
239 | |
---|
240 | cpu_self = _Thread_Dispatch_disable(); |
---|
241 | |
---|
242 | executing = _Per_CPU_Get_executing( cpu_self ); |
---|
243 | executing->Wait.remote_id = the_packet->id; |
---|
244 | |
---|
245 | the_packet->source_tid = executing->Object.id; |
---|
246 | the_packet->source_priority = _Thread_Get_priority( executing ); |
---|
247 | the_packet->to_convert = |
---|
248 | ( the_packet->to_convert - sizeof(MP_packet_Prefix) ) / sizeof(uint32_t); |
---|
249 | |
---|
250 | (*_MPCI_table->send_packet)( destination, the_packet ); |
---|
251 | |
---|
252 | _Thread_queue_Acquire( &_MPCI_Remote_blocked_threads, &queue_context ); |
---|
253 | _Thread_queue_Enqueue( |
---|
254 | &_MPCI_Remote_blocked_threads.Queue, |
---|
255 | &_Thread_queue_Operations_FIFO, |
---|
256 | executing, |
---|
257 | &queue_context |
---|
258 | ); |
---|
259 | return _Thread_Wait_get_status( executing ); |
---|
260 | } |
---|
261 | |
---|
262 | void _MPCI_Send_response_packet ( |
---|
263 | uint32_t destination, |
---|
264 | MP_packet_Prefix *the_packet |
---|
265 | ) |
---|
266 | { |
---|
267 | the_packet->source_tid = _Thread_Executing->Object.id; |
---|
268 | |
---|
269 | (*_MPCI_table->send_packet)( destination, the_packet ); |
---|
270 | } |
---|
271 | |
---|
272 | MP_packet_Prefix *_MPCI_Receive_packet ( void ) |
---|
273 | { |
---|
274 | MP_packet_Prefix *the_packet; |
---|
275 | |
---|
276 | (*_MPCI_table->receive_packet)( &the_packet ); |
---|
277 | |
---|
278 | return the_packet; |
---|
279 | } |
---|
280 | |
---|
281 | Thread_Control *_MPCI_Process_response ( |
---|
282 | MP_packet_Prefix *the_packet |
---|
283 | ) |
---|
284 | { |
---|
285 | ISR_lock_Context lock_context; |
---|
286 | Thread_Control *the_thread; |
---|
287 | |
---|
288 | the_thread = _Thread_Get( the_packet->id, &lock_context ); |
---|
289 | _Assert( the_thread != NULL ); |
---|
290 | |
---|
291 | /* |
---|
292 | * FIXME: This is broken on SMP, see https://devel.rtems.org/ticket/2703. |
---|
293 | * |
---|
294 | * Should use _Thread_queue_Extract_critical() instead with a handler |
---|
295 | * function provided by the caller of _MPCI_Process_response(). Similar to |
---|
296 | * the filter function in _Thread_queue_Flush_critical(). |
---|
297 | */ |
---|
298 | _ISR_lock_ISR_enable( &lock_context ); |
---|
299 | _Thread_queue_Extract( the_thread ); |
---|
300 | the_thread->Wait.return_code = the_packet->return_code; |
---|
301 | return the_thread; |
---|
302 | } |
---|
303 | |
---|
304 | /* |
---|
305 | * _MPCI_Receive_server |
---|
306 | * |
---|
307 | */ |
---|
308 | |
---|
309 | void _MPCI_Receive_server( |
---|
310 | Thread_Entry_numeric_type ignored |
---|
311 | ) |
---|
312 | { |
---|
313 | |
---|
314 | MP_packet_Prefix *the_packet; |
---|
315 | MPCI_Packet_processor the_function; |
---|
316 | Thread_Control *executing; |
---|
317 | Thread_queue_Context queue_context; |
---|
318 | |
---|
319 | executing = _Thread_Get_executing(); |
---|
320 | _Thread_queue_Context_initialize( &queue_context ); |
---|
321 | _Thread_queue_Context_set_enqueue_do_nothing_extra( &queue_context ); |
---|
322 | |
---|
323 | for ( ; ; ) { |
---|
324 | |
---|
325 | executing->receive_packet = NULL; |
---|
326 | |
---|
327 | _ISR_lock_ISR_disable( &queue_context.Lock_context.Lock_context ); |
---|
328 | _CORE_semaphore_Seize( |
---|
329 | &_MPCI_Semaphore, |
---|
330 | MPCI_SEMAPHORE_TQ_OPERATIONS, |
---|
331 | executing, |
---|
332 | true, |
---|
333 | &queue_context |
---|
334 | ); |
---|
335 | |
---|
336 | for ( ; ; ) { |
---|
337 | executing->receive_packet = NULL; |
---|
338 | |
---|
339 | the_packet = _MPCI_Receive_packet(); |
---|
340 | |
---|
341 | if ( !the_packet ) |
---|
342 | break; |
---|
343 | |
---|
344 | executing->receive_packet = the_packet; |
---|
345 | |
---|
346 | if ( !_Mp_packet_Is_valid_packet_class ( the_packet->the_class ) ) |
---|
347 | break; |
---|
348 | |
---|
349 | the_function = _MPCI_Packet_processors[ the_packet->the_class ]; |
---|
350 | |
---|
351 | if ( !the_function ) |
---|
352 | _Internal_error( INTERNAL_ERROR_BAD_PACKET ); |
---|
353 | |
---|
354 | (*the_function)( the_packet ); |
---|
355 | } |
---|
356 | } |
---|
357 | } |
---|
358 | |
---|
359 | void _MPCI_Announce ( void ) |
---|
360 | { |
---|
361 | Thread_queue_Context queue_context; |
---|
362 | |
---|
363 | _ISR_lock_ISR_disable( &queue_context.Lock_context.Lock_context ); |
---|
364 | (void) _CORE_semaphore_Surrender( |
---|
365 | &_MPCI_Semaphore, |
---|
366 | MPCI_SEMAPHORE_TQ_OPERATIONS, |
---|
367 | UINT32_MAX, |
---|
368 | &queue_context |
---|
369 | ); |
---|
370 | } |
---|
371 | |
---|
372 | void _MPCI_Internal_packets_Send_process_packet ( |
---|
373 | MPCI_Internal_Remote_operations operation |
---|
374 | ) |
---|
375 | { |
---|
376 | MPCI_Internal_packet *the_packet; |
---|
377 | |
---|
378 | switch ( operation ) { |
---|
379 | |
---|
380 | case MPCI_PACKETS_SYSTEM_VERIFY: |
---|
381 | |
---|
382 | the_packet = _MPCI_Internal_packets_Get_packet(); |
---|
383 | the_packet->Prefix.the_class = MP_PACKET_MPCI_INTERNAL; |
---|
384 | the_packet->Prefix.length = sizeof ( MPCI_Internal_packet ); |
---|
385 | the_packet->Prefix.to_convert = sizeof ( MPCI_Internal_packet ); |
---|
386 | the_packet->operation = operation; |
---|
387 | |
---|
388 | the_packet->maximum_nodes = _Objects_Maximum_nodes; |
---|
389 | |
---|
390 | the_packet->maximum_global_objects = _Objects_MP_Maximum_global_objects; |
---|
391 | |
---|
392 | _MPCI_Send_process_packet( MPCI_ALL_NODES, &the_packet->Prefix ); |
---|
393 | break; |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | /* |
---|
398 | * _MPCI_Internal_packets_Send_request_packet |
---|
399 | * |
---|
400 | * This subprogram is not needed since there are no request |
---|
401 | * packets to be sent by this manager. |
---|
402 | * |
---|
403 | */ |
---|
404 | |
---|
405 | /* |
---|
406 | * _MPCI_Internal_packets_Send_response_packet |
---|
407 | * |
---|
408 | * This subprogram is not needed since there are no response |
---|
409 | * packets to be sent by this manager. |
---|
410 | * |
---|
411 | */ |
---|
412 | |
---|
413 | void _MPCI_Internal_packets_Process_packet ( |
---|
414 | MP_packet_Prefix *the_packet_prefix |
---|
415 | ) |
---|
416 | { |
---|
417 | MPCI_Internal_packet *the_packet; |
---|
418 | uint32_t maximum_nodes; |
---|
419 | uint32_t maximum_global_objects; |
---|
420 | |
---|
421 | the_packet = (MPCI_Internal_packet *) the_packet_prefix; |
---|
422 | |
---|
423 | switch ( the_packet->operation ) { |
---|
424 | |
---|
425 | case MPCI_PACKETS_SYSTEM_VERIFY: |
---|
426 | |
---|
427 | maximum_nodes = the_packet->maximum_nodes; |
---|
428 | maximum_global_objects = the_packet->maximum_global_objects; |
---|
429 | if ( maximum_nodes != _Objects_Maximum_nodes || |
---|
430 | maximum_global_objects != _Objects_MP_Maximum_global_objects ) { |
---|
431 | |
---|
432 | _MPCI_Return_packet( the_packet_prefix ); |
---|
433 | |
---|
434 | _Internal_error( INTERNAL_ERROR_INCONSISTENT_MP_INFORMATION ); |
---|
435 | } |
---|
436 | |
---|
437 | _MPCI_Return_packet( the_packet_prefix ); |
---|
438 | |
---|
439 | break; |
---|
440 | } |
---|
441 | } |
---|
442 | |
---|
443 | /* |
---|
444 | * _MPCI_Internal_packets_Send_object_was_deleted |
---|
445 | * |
---|
446 | * This subprogram is not needed since there are no objects |
---|
447 | * deleted by this manager. |
---|
448 | * |
---|
449 | */ |
---|
450 | |
---|
451 | /* |
---|
452 | * _MPCI_Internal_packets_Send_extract_proxy |
---|
453 | * |
---|
454 | * This subprogram is not needed since there are no objects |
---|
455 | * deleted by this manager. |
---|
456 | * |
---|
457 | */ |
---|
458 | |
---|
459 | MPCI_Internal_packet *_MPCI_Internal_packets_Get_packet ( void ) |
---|
460 | { |
---|
461 | return ( (MPCI_Internal_packet *) _MPCI_Get_packet() ); |
---|
462 | } |
---|
463 | |
---|
464 | static void _MPCI_Finalize( void ) |
---|
465 | { |
---|
466 | if ( _System_state_Is_multiprocessing ) { |
---|
467 | _MPCI_Initialization(); |
---|
468 | _MPCI_Internal_packets_Send_process_packet( MPCI_PACKETS_SYSTEM_VERIFY ); |
---|
469 | } |
---|
470 | } |
---|
471 | |
---|
472 | RTEMS_SYSINIT_ITEM( |
---|
473 | _MPCI_Handler_early_initialization, |
---|
474 | RTEMS_SYSINIT_MP_EARLY, |
---|
475 | RTEMS_SYSINIT_ORDER_MIDDLE |
---|
476 | ); |
---|
477 | |
---|
478 | RTEMS_SYSINIT_ITEM( |
---|
479 | _MPCI_Handler_initialization, |
---|
480 | RTEMS_SYSINIT_MP, |
---|
481 | RTEMS_SYSINIT_ORDER_MIDDLE |
---|
482 | ); |
---|
483 | |
---|
484 | RTEMS_SYSINIT_ITEM( |
---|
485 | _MPCI_Create_server, |
---|
486 | RTEMS_SYSINIT_MP_SERVER, |
---|
487 | RTEMS_SYSINIT_ORDER_MIDDLE |
---|
488 | ); |
---|
489 | |
---|
490 | RTEMS_SYSINIT_ITEM( |
---|
491 | _MPCI_Finalize, |
---|
492 | RTEMS_SYSINIT_MP_FINALIZE, |
---|
493 | RTEMS_SYSINIT_ORDER_MIDDLE |
---|
494 | ); |
---|
495 | |
---|
496 | /* end of file */ |
---|