source: rtems/cpukit/score/src/mpci.c @ 2581a56

5
Last change on this file since 2581a56 was 2581a56, checked in by Sebastian Huber <sebastian.huber@…>, on 05/20/16 at 19:39:56

score: Add semaphore variants

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