source: rtems/cpukit/score/src/mpci.c @ fce900b5

5
Last change on this file since fce900b5 was 3a659b04, checked in by Sebastian Huber <sebastian.huber@…>, on 12/09/16 at 06:19:22

score: Introduce _Internal_error()

  • Property mode set to 100644
File size: 12.4 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    _Internal_error( INTERNAL_ERROR_NO_MPCI );
99
100  _MPCI_table = users_mpci_table;
101
102  if ( !_System_state_Is_multiprocessing )
103    return;
104
105  /*
106   *  Register the MP Process Packet routine.
107   */
108
109  _MPCI_Register_packet_processor(
110    MP_PACKET_MPCI_INTERNAL,
111    _MPCI_Internal_packets_Process_packet
112  );
113
114  /*
115   *  Create the counting semaphore used by the MPCI Receive Server.
116   */
117
118  _CORE_semaphore_Initialize(
119    &_MPCI_Semaphore,
120    0                         /* initial_value */
121  );
122}
123
124static void _MPCI_Create_server( void )
125{
126  Thread_Entry_information entry = {
127    .adaptor = _Thread_Entry_adaptor_numeric,
128    .Kinds = {
129      .Numeric = {
130        .entry = _MPCI_Receive_server
131      }
132    }
133  };
134  ISR_lock_Context lock_context;
135  Objects_Name     name;
136
137
138  if ( !_System_state_Is_multiprocessing )
139    return;
140
141  /*
142   *  Initialize the MPCI Receive Server
143   */
144
145  _MPCI_Receive_server_tcb = _Thread_Internal_allocate();
146
147  name.name_u32 = _Objects_Build_name( 'M', 'P', 'C', 'I' );
148  _Thread_Initialize(
149    &_Thread_Internal_information,
150    _MPCI_Receive_server_tcb,
151    &_Scheduler_Table[ 0 ],
152    NULL,        /* allocate the stack */
153    _Stack_Minimum() +
154      CPU_MPCI_RECEIVE_SERVER_EXTRA_STACK +
155      _Configuration_MP_table->extra_mpci_receive_server_stack,
156    CPU_ALL_TASKS_ARE_FP,
157    PRIORITY_PSEUDO_ISR,
158    false,       /* no preempt */
159    THREAD_CPU_BUDGET_ALGORITHM_NONE,
160    NULL,        /* no budget algorithm callout */
161    0,           /* all interrupts enabled */
162    name
163  );
164
165  _ISR_lock_ISR_disable( &lock_context );
166  _Thread_Start( _MPCI_Receive_server_tcb, &entry, &lock_context );
167}
168
169static void _MPCI_Initialization( void )
170{
171  (*_MPCI_table->initialization)();
172}
173
174void _MPCI_Register_packet_processor(
175  MP_packet_Classes      the_class,
176  MPCI_Packet_processor  the_packet_processor
177
178)
179{
180  _MPCI_Packet_processors[ the_class ] = the_packet_processor;
181}
182
183MP_packet_Prefix *_MPCI_Get_packet ( void )
184{
185  MP_packet_Prefix  *the_packet;
186
187  (*_MPCI_table->get_packet)( &the_packet );
188
189  if ( the_packet == NULL )
190    _Internal_error( INTERNAL_ERROR_OUT_OF_PACKETS );
191
192  /*
193   *  Put in a default timeout that will be used for
194   *  all packets that do not otherwise have a timeout.
195   */
196
197  the_packet->timeout = MPCI_DEFAULT_TIMEOUT;
198
199  return the_packet;
200}
201
202void _MPCI_Return_packet (
203  MP_packet_Prefix   *the_packet
204)
205{
206  (*_MPCI_table->return_packet)( the_packet );
207}
208
209void _MPCI_Send_process_packet (
210  uint32_t            destination,
211  MP_packet_Prefix   *the_packet
212)
213{
214  the_packet->source_tid = _Thread_Executing->Object.id;
215  the_packet->to_convert =
216     ( the_packet->to_convert - sizeof(MP_packet_Prefix) ) / sizeof(uint32_t);
217
218  (*_MPCI_table->send_packet)( destination, the_packet );
219}
220
221static void _MPCI_Enqueue_callout(
222  Thread_queue_Queue   *queue,
223  Thread_Control       *the_thread,
224  Thread_queue_Context *queue_context
225)
226{
227  _Thread_Dispatch_unnest( _Per_CPU_Get() );
228}
229
230Status_Control _MPCI_Send_request_packet(
231  uint32_t          destination,
232  MP_packet_Prefix *the_packet,
233  States_Control    extra_state
234)
235{
236  Per_CPU_Control      *cpu_self;
237  Thread_queue_Context  queue_context;
238  Thread_Control       *executing;
239
240  /*
241   *  See if we need a default timeout
242   */
243
244  if (the_packet->timeout == MPCI_DEFAULT_TIMEOUT)
245      the_packet->timeout = _MPCI_table->default_timeout;
246
247  _Thread_queue_Context_initialize( &queue_context );
248  _Thread_queue_Context_set_thread_state(
249    &queue_context,
250    STATES_WAITING_FOR_RPC_REPLY | extra_state
251  );
252  _Thread_queue_Context_set_enqueue_callout(
253    &queue_context,
254    _MPCI_Enqueue_callout
255  );
256  _Thread_queue_Context_set_relative_timeout( &queue_context, the_packet->timeout );
257
258  cpu_self = _Thread_Dispatch_disable();
259
260  executing = _Per_CPU_Get_executing( cpu_self );
261  executing->Wait.remote_id = the_packet->id;
262
263  the_packet->source_tid      = executing->Object.id;
264  the_packet->source_priority = _Thread_Get_priority( executing );
265  the_packet->to_convert =
266     ( the_packet->to_convert - sizeof(MP_packet_Prefix) ) / sizeof(uint32_t);
267
268  (*_MPCI_table->send_packet)( destination, the_packet );
269
270  _Thread_queue_Acquire( &_MPCI_Remote_blocked_threads, &queue_context );
271  _Thread_queue_Enqueue(
272    &_MPCI_Remote_blocked_threads.Queue,
273    &_Thread_queue_Operations_FIFO,
274    executing,
275    &queue_context
276  );
277  return _Thread_Wait_get_status( executing );
278}
279
280void _MPCI_Send_response_packet (
281  uint32_t            destination,
282  MP_packet_Prefix   *the_packet
283)
284{
285  the_packet->source_tid = _Thread_Executing->Object.id;
286
287  (*_MPCI_table->send_packet)( destination, the_packet );
288}
289
290MP_packet_Prefix  *_MPCI_Receive_packet ( void )
291{
292  MP_packet_Prefix  *the_packet;
293
294  (*_MPCI_table->receive_packet)( &the_packet );
295
296  return the_packet;
297}
298
299Thread_Control *_MPCI_Process_response (
300  MP_packet_Prefix  *the_packet
301)
302{
303  ISR_lock_Context  lock_context;
304  Thread_Control   *the_thread;
305
306  the_thread = _Thread_Get( the_packet->id, &lock_context );
307  _Assert( the_thread != NULL );
308
309  /*
310   * FIXME: This is broken on SMP, see https://devel.rtems.org/ticket/2703.
311   *
312   * Should use _Thread_queue_Extract_critical() instead with a handler
313   * function provided by the caller of _MPCI_Process_response().  Similar to
314   * the filter function in _Thread_queue_Flush_critical().
315   */
316  _ISR_lock_ISR_enable( &lock_context );
317  _Thread_queue_Extract( the_thread );
318  the_thread->Wait.return_code = the_packet->return_code;
319  return the_thread;
320}
321
322/*
323 *  _MPCI_Receive_server
324 *
325 */
326
327void _MPCI_Receive_server(
328  Thread_Entry_numeric_type ignored
329)
330{
331
332  MP_packet_Prefix      *the_packet;
333  MPCI_Packet_processor  the_function;
334  Thread_Control        *executing;
335  Thread_queue_Context   queue_context;
336
337  executing = _Thread_Get_executing();
338  _Thread_queue_Context_initialize( &queue_context );
339  _Thread_queue_Context_set_no_timeout( &queue_context );
340
341  for ( ; ; ) {
342
343    executing->receive_packet = NULL;
344
345    _ISR_lock_ISR_disable( &queue_context.Lock_context.Lock_context );
346    _CORE_semaphore_Seize(
347      &_MPCI_Semaphore,
348      MPCI_SEMAPHORE_TQ_OPERATIONS,
349      executing,
350      true,
351      &queue_context
352    );
353
354    for ( ; ; ) {
355      executing->receive_packet = NULL;
356
357      the_packet = _MPCI_Receive_packet();
358
359      if ( !the_packet )
360        break;
361
362      executing->receive_packet = the_packet;
363
364      if ( !_Mp_packet_Is_valid_packet_class ( the_packet->the_class ) )
365        break;
366
367      the_function = _MPCI_Packet_processors[ the_packet->the_class ];
368
369      if ( !the_function )
370        _Internal_error( INTERNAL_ERROR_BAD_PACKET );
371
372       (*the_function)( the_packet );
373    }
374  }
375}
376
377void _MPCI_Announce ( void )
378{
379  Thread_queue_Context queue_context;
380
381  _ISR_lock_ISR_disable( &queue_context.Lock_context.Lock_context );
382  (void) _CORE_semaphore_Surrender(
383    &_MPCI_Semaphore,
384    MPCI_SEMAPHORE_TQ_OPERATIONS,
385    UINT32_MAX,
386    &queue_context
387  );
388}
389
390void _MPCI_Internal_packets_Send_process_packet (
391   MPCI_Internal_Remote_operations operation
392)
393{
394  MPCI_Internal_packet *the_packet;
395
396  switch ( operation ) {
397
398    case MPCI_PACKETS_SYSTEM_VERIFY:
399
400      the_packet                    = _MPCI_Internal_packets_Get_packet();
401      the_packet->Prefix.the_class  = MP_PACKET_MPCI_INTERNAL;
402      the_packet->Prefix.length     = sizeof ( MPCI_Internal_packet );
403      the_packet->Prefix.to_convert = sizeof ( MPCI_Internal_packet );
404      the_packet->operation         = operation;
405
406      the_packet->maximum_nodes = _Objects_Maximum_nodes;
407
408      the_packet->maximum_global_objects = _Objects_MP_Maximum_global_objects;
409
410      _MPCI_Send_process_packet( MPCI_ALL_NODES, &the_packet->Prefix );
411      break;
412  }
413}
414
415/*
416 *  _MPCI_Internal_packets_Send_request_packet
417 *
418 *  This subprogram is not needed since there are no request
419 *  packets to be sent by this manager.
420 *
421 */
422
423/*
424 *  _MPCI_Internal_packets_Send_response_packet
425 *
426 *  This subprogram is not needed since there are no response
427 *  packets to be sent by this manager.
428 *
429 */
430
431void _MPCI_Internal_packets_Process_packet (
432  MP_packet_Prefix  *the_packet_prefix
433)
434{
435  MPCI_Internal_packet *the_packet;
436  uint32_t                    maximum_nodes;
437  uint32_t                    maximum_global_objects;
438
439  the_packet = (MPCI_Internal_packet *) the_packet_prefix;
440
441  switch ( the_packet->operation ) {
442
443    case MPCI_PACKETS_SYSTEM_VERIFY:
444
445      maximum_nodes          = the_packet->maximum_nodes;
446      maximum_global_objects = the_packet->maximum_global_objects;
447      if ( maximum_nodes != _Objects_Maximum_nodes ||
448           maximum_global_objects != _Objects_MP_Maximum_global_objects ) {
449
450        _MPCI_Return_packet( the_packet_prefix );
451
452        _Internal_error( INTERNAL_ERROR_INCONSISTENT_MP_INFORMATION );
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.