1 | /* |
---|
2 | * Multiprocessing Communications Interface (MPCI) Handler |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2007. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <rtems/system.h> |
---|
20 | #include <rtems/score/cpu.h> |
---|
21 | #include <rtems/score/interr.h> |
---|
22 | #if defined(RTEMS_MULTIPROCESSING) |
---|
23 | #include <rtems/score/mpci.h> |
---|
24 | #include <rtems/score/mppkt.h> |
---|
25 | #endif |
---|
26 | #include <rtems/score/states.h> |
---|
27 | #include <rtems/score/thread.h> |
---|
28 | #include <rtems/score/threadq.h> |
---|
29 | #include <rtems/score/tqdata.h> |
---|
30 | #include <rtems/score/watchdog.h> |
---|
31 | #include <rtems/score/sysstate.h> |
---|
32 | |
---|
33 | #include <rtems/score/coresem.h> |
---|
34 | |
---|
35 | /*PAGE |
---|
36 | * |
---|
37 | * _MPCI_Handler_initialization |
---|
38 | * |
---|
39 | * This subprogram performs the initialization necessary for this handler. |
---|
40 | */ |
---|
41 | |
---|
42 | void _MPCI_Handler_initialization( |
---|
43 | MPCI_Control *users_mpci_table, |
---|
44 | uint32_t timeout_status |
---|
45 | ) |
---|
46 | { |
---|
47 | CORE_semaphore_Attributes attributes; |
---|
48 | |
---|
49 | if ( _System_state_Is_multiprocessing && !users_mpci_table ) |
---|
50 | _Internal_error_Occurred( |
---|
51 | INTERNAL_ERROR_CORE, |
---|
52 | TRUE, |
---|
53 | INTERNAL_ERROR_NO_MPCI |
---|
54 | ); |
---|
55 | |
---|
56 | _MPCI_table = users_mpci_table; |
---|
57 | |
---|
58 | if ( !_System_state_Is_multiprocessing ) |
---|
59 | return; |
---|
60 | |
---|
61 | /* |
---|
62 | * Register the MP Process Packet routine. |
---|
63 | */ |
---|
64 | |
---|
65 | _MPCI_Register_packet_processor( |
---|
66 | MP_PACKET_MPCI_INTERNAL, |
---|
67 | _MPCI_Internal_packets_Process_packet |
---|
68 | ); |
---|
69 | |
---|
70 | /* |
---|
71 | * Create the counting semaphore used by the MPCI Receive Server. |
---|
72 | */ |
---|
73 | |
---|
74 | attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO; |
---|
75 | |
---|
76 | _CORE_semaphore_Initialize( |
---|
77 | &_MPCI_Semaphore, |
---|
78 | &attributes, /* the_semaphore_attributes */ |
---|
79 | 0 /* initial_value */ |
---|
80 | ); |
---|
81 | |
---|
82 | _Thread_queue_Initialize( |
---|
83 | &_MPCI_Remote_blocked_threads, |
---|
84 | THREAD_QUEUE_DISCIPLINE_FIFO, |
---|
85 | STATES_WAITING_FOR_RPC_REPLY, |
---|
86 | timeout_status |
---|
87 | ); |
---|
88 | } |
---|
89 | |
---|
90 | /*PAGE |
---|
91 | * |
---|
92 | * _MPCI_Create_server |
---|
93 | * |
---|
94 | * This subprogram creates the MPCI receive server. |
---|
95 | */ |
---|
96 | |
---|
97 | char *_MPCI_Internal_name = "MPCI"; |
---|
98 | |
---|
99 | void _MPCI_Create_server( void ) |
---|
100 | { |
---|
101 | |
---|
102 | if ( !_System_state_Is_multiprocessing ) |
---|
103 | return; |
---|
104 | |
---|
105 | /* |
---|
106 | * Initialize the MPCI Receive Server |
---|
107 | */ |
---|
108 | |
---|
109 | _MPCI_Receive_server_tcb = _Thread_Internal_allocate(); |
---|
110 | |
---|
111 | _Thread_Initialize( |
---|
112 | &_Thread_Internal_information, |
---|
113 | _MPCI_Receive_server_tcb, |
---|
114 | NULL, /* allocate the stack */ |
---|
115 | MPCI_RECEIVE_SERVER_STACK_SIZE, |
---|
116 | CPU_ALL_TASKS_ARE_FP, |
---|
117 | PRIORITY_MINIMUM, |
---|
118 | FALSE, /* no preempt */ |
---|
119 | THREAD_CPU_BUDGET_ALGORITHM_NONE, |
---|
120 | NULL, /* no budget algorithm callout */ |
---|
121 | 0, /* all interrupts enabled */ |
---|
122 | _MPCI_Internal_name |
---|
123 | ); |
---|
124 | |
---|
125 | _Thread_Start( |
---|
126 | _MPCI_Receive_server_tcb, |
---|
127 | THREAD_START_NUMERIC, |
---|
128 | (void *) _MPCI_Receive_server, |
---|
129 | NULL, |
---|
130 | 0 |
---|
131 | ); |
---|
132 | } |
---|
133 | |
---|
134 | /*PAGE |
---|
135 | * |
---|
136 | * _MPCI_Initialization |
---|
137 | * |
---|
138 | * This subprogram initializes the MPCI driver by |
---|
139 | * invoking the user provided MPCI initialization callout. |
---|
140 | */ |
---|
141 | |
---|
142 | void _MPCI_Initialization ( void ) |
---|
143 | { |
---|
144 | (*_MPCI_table->initialization)(); |
---|
145 | } |
---|
146 | |
---|
147 | /*PAGE |
---|
148 | * |
---|
149 | * _MPCI_Register_packet_processor |
---|
150 | * |
---|
151 | * This routine registers the MPCI packet processor for the |
---|
152 | * designated object class. |
---|
153 | */ |
---|
154 | |
---|
155 | void _MPCI_Register_packet_processor( |
---|
156 | MP_packet_Classes the_class, |
---|
157 | MPCI_Packet_processor the_packet_processor |
---|
158 | |
---|
159 | ) |
---|
160 | { |
---|
161 | _MPCI_Packet_processors[ the_class ] = the_packet_processor; |
---|
162 | } |
---|
163 | |
---|
164 | /*PAGE |
---|
165 | * |
---|
166 | * _MPCI_Get_packet |
---|
167 | * |
---|
168 | * This subprogram obtains a packet by invoking the user provided |
---|
169 | * MPCI get packet callout. |
---|
170 | */ |
---|
171 | |
---|
172 | MP_packet_Prefix *_MPCI_Get_packet ( void ) |
---|
173 | { |
---|
174 | MP_packet_Prefix *the_packet; |
---|
175 | |
---|
176 | (*_MPCI_table->get_packet)( &the_packet ); |
---|
177 | |
---|
178 | if ( the_packet == NULL ) |
---|
179 | _Internal_error_Occurred( |
---|
180 | INTERNAL_ERROR_CORE, |
---|
181 | TRUE, |
---|
182 | INTERNAL_ERROR_OUT_OF_PACKETS |
---|
183 | ); |
---|
184 | |
---|
185 | /* |
---|
186 | * Put in a default timeout that will be used for |
---|
187 | * all packets that do not otherwise have a timeout. |
---|
188 | */ |
---|
189 | |
---|
190 | the_packet->timeout = MPCI_DEFAULT_TIMEOUT; |
---|
191 | |
---|
192 | return the_packet; |
---|
193 | } |
---|
194 | |
---|
195 | /*PAGE |
---|
196 | * |
---|
197 | * _MPCI_Return_packet |
---|
198 | * |
---|
199 | * This subprogram returns a packet by invoking the user provided |
---|
200 | * MPCI return packet callout. |
---|
201 | */ |
---|
202 | |
---|
203 | void _MPCI_Return_packet ( |
---|
204 | MP_packet_Prefix *the_packet |
---|
205 | ) |
---|
206 | { |
---|
207 | (*_MPCI_table->return_packet)( the_packet ); |
---|
208 | } |
---|
209 | |
---|
210 | /*PAGE |
---|
211 | * |
---|
212 | * _MPCI_Send_process_packet |
---|
213 | * |
---|
214 | * This subprogram sends a process packet by invoking the user provided |
---|
215 | * MPCI send callout. |
---|
216 | */ |
---|
217 | |
---|
218 | void _MPCI_Send_process_packet ( |
---|
219 | uint32_t destination, |
---|
220 | MP_packet_Prefix *the_packet |
---|
221 | ) |
---|
222 | { |
---|
223 | the_packet->source_tid = _Thread_Executing->Object.id; |
---|
224 | the_packet->to_convert = |
---|
225 | ( the_packet->to_convert - sizeof(MP_packet_Prefix) ) / |
---|
226 | sizeof(uint32_t ); |
---|
227 | |
---|
228 | (*_MPCI_table->send_packet)( destination, the_packet ); |
---|
229 | } |
---|
230 | |
---|
231 | /*PAGE |
---|
232 | * |
---|
233 | * _MPCI_Send_request_packet |
---|
234 | * |
---|
235 | * This subprogram sends a request packet by invoking the user provided |
---|
236 | * MPCI send callout. |
---|
237 | */ |
---|
238 | |
---|
239 | uint32_t _MPCI_Send_request_packet ( |
---|
240 | uint32_t destination, |
---|
241 | MP_packet_Prefix *the_packet, |
---|
242 | States_Control extra_state |
---|
243 | ) |
---|
244 | { |
---|
245 | the_packet->source_tid = _Thread_Executing->Object.id; |
---|
246 | the_packet->source_priority = _Thread_Executing->current_priority; |
---|
247 | the_packet->to_convert = |
---|
248 | ( the_packet->to_convert - sizeof(MP_packet_Prefix) ) / |
---|
249 | sizeof(uint32_t ); |
---|
250 | |
---|
251 | _Thread_Executing->Wait.id = the_packet->id; |
---|
252 | |
---|
253 | _Thread_Executing->Wait.queue = &_MPCI_Remote_blocked_threads; |
---|
254 | |
---|
255 | _Thread_Disable_dispatch(); |
---|
256 | |
---|
257 | (*_MPCI_table->send_packet)( destination, the_packet ); |
---|
258 | |
---|
259 | _Thread_queue_Enter_critical_section( &_MPCI_Remote_blocked_threads ); |
---|
260 | |
---|
261 | /* |
---|
262 | * See if we need a default timeout |
---|
263 | */ |
---|
264 | |
---|
265 | if (the_packet->timeout == MPCI_DEFAULT_TIMEOUT) |
---|
266 | the_packet->timeout = _MPCI_table->default_timeout; |
---|
267 | |
---|
268 | _Thread_queue_Enqueue( &_MPCI_Remote_blocked_threads, the_packet->timeout ); |
---|
269 | |
---|
270 | _Thread_Executing->current_state = |
---|
271 | _States_Set( extra_state, _Thread_Executing->current_state ); |
---|
272 | |
---|
273 | _Thread_Enable_dispatch(); |
---|
274 | |
---|
275 | return _Thread_Executing->Wait.return_code; |
---|
276 | } |
---|
277 | |
---|
278 | /*PAGE |
---|
279 | * |
---|
280 | * _MPCI_Send_response_packet |
---|
281 | * |
---|
282 | * This subprogram sends a response packet by invoking the user provided |
---|
283 | * MPCI send callout. |
---|
284 | */ |
---|
285 | |
---|
286 | void _MPCI_Send_response_packet ( |
---|
287 | uint32_t destination, |
---|
288 | MP_packet_Prefix *the_packet |
---|
289 | ) |
---|
290 | { |
---|
291 | the_packet->source_tid = _Thread_Executing->Object.id; |
---|
292 | |
---|
293 | (*_MPCI_table->send_packet)( destination, the_packet ); |
---|
294 | } |
---|
295 | |
---|
296 | /*PAGE |
---|
297 | * |
---|
298 | * _MPCI_Receive_packet |
---|
299 | * |
---|
300 | * This subprogram receives a packet by invoking the user provided |
---|
301 | * MPCI receive callout. |
---|
302 | */ |
---|
303 | |
---|
304 | MP_packet_Prefix *_MPCI_Receive_packet ( void ) |
---|
305 | { |
---|
306 | MP_packet_Prefix *the_packet; |
---|
307 | |
---|
308 | (*_MPCI_table->receive_packet)( &the_packet ); |
---|
309 | |
---|
310 | return the_packet; |
---|
311 | } |
---|
312 | |
---|
313 | /*PAGE |
---|
314 | * |
---|
315 | * _MPCI_Process_response |
---|
316 | * |
---|
317 | * This subprogram obtains a packet by invoking the user provided |
---|
318 | * MPCI get packet callout. |
---|
319 | */ |
---|
320 | |
---|
321 | Thread_Control *_MPCI_Process_response ( |
---|
322 | MP_packet_Prefix *the_packet |
---|
323 | ) |
---|
324 | { |
---|
325 | Thread_Control *the_thread; |
---|
326 | Objects_Locations location; |
---|
327 | |
---|
328 | the_thread = _Thread_Get( the_packet->id, &location ); |
---|
329 | switch ( location ) { |
---|
330 | case OBJECTS_ERROR: |
---|
331 | #if defined(RTEMS_MULTIPROCESSING) |
---|
332 | case OBJECTS_REMOTE: |
---|
333 | #endif |
---|
334 | the_thread = NULL; /* IMPOSSIBLE */ |
---|
335 | break; |
---|
336 | case OBJECTS_LOCAL: |
---|
337 | _Thread_queue_Extract( &_MPCI_Remote_blocked_threads, the_thread ); |
---|
338 | the_thread->Wait.return_code = the_packet->return_code; |
---|
339 | _Thread_Unnest_dispatch(); |
---|
340 | break; |
---|
341 | } |
---|
342 | |
---|
343 | return the_thread; |
---|
344 | } |
---|
345 | |
---|
346 | /*PAGE |
---|
347 | * |
---|
348 | * _MPCI_Receive_server |
---|
349 | * |
---|
350 | */ |
---|
351 | |
---|
352 | Thread _MPCI_Receive_server( |
---|
353 | uint32_t ignored |
---|
354 | ) |
---|
355 | { |
---|
356 | |
---|
357 | MP_packet_Prefix *the_packet; |
---|
358 | MPCI_Packet_processor the_function; |
---|
359 | Thread_Control *executing; |
---|
360 | |
---|
361 | executing = _Thread_Executing; |
---|
362 | |
---|
363 | for ( ; ; ) { |
---|
364 | |
---|
365 | executing->receive_packet = NULL; |
---|
366 | |
---|
367 | _Thread_Disable_dispatch(); |
---|
368 | _CORE_semaphore_Seize( &_MPCI_Semaphore, 0, TRUE, WATCHDOG_NO_TIMEOUT ); |
---|
369 | _Thread_Enable_dispatch(); |
---|
370 | |
---|
371 | for ( ; ; ) { |
---|
372 | the_packet = _MPCI_Receive_packet(); |
---|
373 | |
---|
374 | if ( !the_packet ) |
---|
375 | break; |
---|
376 | |
---|
377 | executing->receive_packet = the_packet; |
---|
378 | |
---|
379 | if ( !_Mp_packet_Is_valid_packet_class ( the_packet->the_class ) ) |
---|
380 | break; |
---|
381 | |
---|
382 | the_function = _MPCI_Packet_processors[ the_packet->the_class ]; |
---|
383 | |
---|
384 | if ( !the_function ) |
---|
385 | _Internal_error_Occurred( |
---|
386 | INTERNAL_ERROR_CORE, |
---|
387 | TRUE, |
---|
388 | INTERNAL_ERROR_BAD_PACKET |
---|
389 | ); |
---|
390 | |
---|
391 | (*the_function)( the_packet ); |
---|
392 | } |
---|
393 | } |
---|
394 | |
---|
395 | return 0; /* unreached - only to remove warnings */ |
---|
396 | } |
---|
397 | |
---|
398 | /*PAGE |
---|
399 | * |
---|
400 | * _MPCI_Announce |
---|
401 | * |
---|
402 | */ |
---|
403 | |
---|
404 | void _MPCI_Announce ( void ) |
---|
405 | { |
---|
406 | _Thread_Disable_dispatch(); |
---|
407 | (void) _CORE_semaphore_Surrender( &_MPCI_Semaphore, 0, 0 ); |
---|
408 | _Thread_Enable_dispatch(); |
---|
409 | } |
---|
410 | |
---|
411 | /*PAGE |
---|
412 | * |
---|
413 | * _MPCI_Internal_packets_Send_process_packet |
---|
414 | * |
---|
415 | */ |
---|
416 | |
---|
417 | void _MPCI_Internal_packets_Send_process_packet ( |
---|
418 | MPCI_Internal_Remote_operations operation |
---|
419 | ) |
---|
420 | { |
---|
421 | MPCI_Internal_packet *the_packet; |
---|
422 | |
---|
423 | switch ( operation ) { |
---|
424 | |
---|
425 | case MPCI_PACKETS_SYSTEM_VERIFY: |
---|
426 | |
---|
427 | the_packet = _MPCI_Internal_packets_Get_packet(); |
---|
428 | the_packet->Prefix.the_class = MP_PACKET_MPCI_INTERNAL; |
---|
429 | the_packet->Prefix.length = sizeof ( MPCI_Internal_packet ); |
---|
430 | the_packet->Prefix.to_convert = sizeof ( MPCI_Internal_packet ); |
---|
431 | the_packet->operation = operation; |
---|
432 | |
---|
433 | the_packet->maximum_nodes = _Objects_Maximum_nodes; |
---|
434 | |
---|
435 | the_packet->maximum_global_objects = _Objects_MP_Maximum_global_objects; |
---|
436 | |
---|
437 | _MPCI_Send_process_packet( MPCI_ALL_NODES, &the_packet->Prefix ); |
---|
438 | break; |
---|
439 | } |
---|
440 | } |
---|
441 | |
---|
442 | /*PAGE |
---|
443 | * |
---|
444 | * _MPCI_Internal_packets_Send_request_packet |
---|
445 | * |
---|
446 | * This subprogram is not needed since there are no request |
---|
447 | * packets to be sent by this manager. |
---|
448 | * |
---|
449 | */ |
---|
450 | |
---|
451 | /*PAGE |
---|
452 | * |
---|
453 | * _MPCI_Internal_packets_Send_response_packet |
---|
454 | * |
---|
455 | * This subprogram is not needed since there are no response |
---|
456 | * packets to be sent by this manager. |
---|
457 | * |
---|
458 | */ |
---|
459 | |
---|
460 | /*PAGE |
---|
461 | * |
---|
462 | * |
---|
463 | * _MPCI_Internal_packets_Process_packet |
---|
464 | * |
---|
465 | */ |
---|
466 | |
---|
467 | void _MPCI_Internal_packets_Process_packet ( |
---|
468 | MP_packet_Prefix *the_packet_prefix |
---|
469 | ) |
---|
470 | { |
---|
471 | MPCI_Internal_packet *the_packet; |
---|
472 | uint32_t maximum_nodes; |
---|
473 | uint32_t maximum_global_objects; |
---|
474 | |
---|
475 | the_packet = (MPCI_Internal_packet *) the_packet_prefix; |
---|
476 | |
---|
477 | switch ( the_packet->operation ) { |
---|
478 | |
---|
479 | case MPCI_PACKETS_SYSTEM_VERIFY: |
---|
480 | |
---|
481 | maximum_nodes = the_packet->maximum_nodes; |
---|
482 | maximum_global_objects = the_packet->maximum_global_objects; |
---|
483 | if ( maximum_nodes != _Objects_Maximum_nodes || |
---|
484 | maximum_global_objects != _Objects_MP_Maximum_global_objects ) { |
---|
485 | |
---|
486 | _MPCI_Return_packet( the_packet_prefix ); |
---|
487 | |
---|
488 | _Internal_error_Occurred( |
---|
489 | INTERNAL_ERROR_CORE, |
---|
490 | TRUE, |
---|
491 | INTERNAL_ERROR_INCONSISTENT_MP_INFORMATION |
---|
492 | ); |
---|
493 | } |
---|
494 | |
---|
495 | _MPCI_Return_packet( the_packet_prefix ); |
---|
496 | |
---|
497 | break; |
---|
498 | } |
---|
499 | } |
---|
500 | |
---|
501 | /*PAGE |
---|
502 | * |
---|
503 | * _MPCI_Internal_packets_Send_object_was_deleted |
---|
504 | * |
---|
505 | * This subprogram is not needed since there are no objects |
---|
506 | * deleted by this manager. |
---|
507 | * |
---|
508 | */ |
---|
509 | |
---|
510 | /*PAGE |
---|
511 | * |
---|
512 | * _MPCI_Internal_packets_Send_extract_proxy |
---|
513 | * |
---|
514 | * This subprogram is not needed since there are no objects |
---|
515 | * deleted by this manager. |
---|
516 | * |
---|
517 | */ |
---|
518 | |
---|
519 | /*PAGE |
---|
520 | * |
---|
521 | * _MPCI_Internal_packets_Get_packet |
---|
522 | * |
---|
523 | */ |
---|
524 | |
---|
525 | MPCI_Internal_packet *_MPCI_Internal_packets_Get_packet ( void ) |
---|
526 | { |
---|
527 | return ( (MPCI_Internal_packet *) _MPCI_Get_packet() ); |
---|
528 | } |
---|
529 | |
---|
530 | /* end of file */ |
---|