source: rtems-docs/c_user/user_extensions.rst @ 170418a

4.115
Last change on this file since 170418a was 25d55d4, checked in by Chris Johns <chrisj@…>, on 02/18/16 at 04:41:28

Change code to code-block.

  • Property mode set to 100644
File size: 19.5 KB
Line 
1.. COMMENT: COPYRIGHT (c) 1988-2008.
2.. COMMENT: On-Line Applications Research Corporation (OAR).
3.. COMMENT: All rights reserved.
4
5User Extensions Manager
6#######################
7
8.. index:: user extensions
9
10Introduction
11============
12
13The RTEMS User Extensions Manager allows the application developer to augment
14the executive by allowing them to supply extension routines which are invoked
15at critical system events.  The directives provided by the user extensions
16manager are:
17
18- rtems_extension_create_ - Create an extension set
19
20- rtems_extension_ident_ - Get ID of an extension set
21
22- rtems_extension_delete_ - Delete an extension set
23
24Background
25==========
26
27User extension routines are invoked when the following system events occur:
28
29- Task creation
30
31- Task initiation
32
33- Task reinitiation
34
35- Task deletion
36
37- Task context switch
38
39- Post task context switch
40
41- Task begin
42
43- Task exits
44
45- Fatal error detection
46
47These extensions are invoked as a function with arguments that are appropriate
48to the system event.
49
50Extension Sets
51--------------
52.. index:: extension set
53
54An extension set is defined as a set of routines which are invoked at each of
55the critical system events at which user extension routines are invoked.
56Together a set of these routines typically perform a specific functionality
57such as performance monitoring or debugger support.  RTEMS is informed of the
58entry points which constitute an extension set via the following
59structure:.. index:: rtems_extensions_table
60
61.. code-block:: c
62
63    typedef struct {
64        rtems_task_create_extension      thread_create;
65        rtems_task_start_extension       thread_start;
66        rtems_task_restart_extension     thread_restart;
67        rtems_task_delete_extension      thread_delete;
68        rtems_task_switch_extension      thread_switch;
69        rtems_task_begin_extension       thread_begin;
70        rtems_task_exitted_extension     thread_exitted;
71        rtems_fatal_extension            fatal;
72    } rtems_extensions_table;
73
74RTEMS allows the user to have multiple extension sets active at the same time.
75First, a single static extension set may be defined as the application's User
76Extension Table which is included as part of the Configuration Table.  This
77extension set is active for the entire life of the system and may not be
78deleted.  This extension set is especially important because it is the only way
79the application can provided a FATAL error extension which is invoked if RTEMS
80fails during the initialize_executive directive.  The static extension set is
81optional and may be configured as NULL if no static extension set is required.
82
83Second, the user can install dynamic extensions using the
84``rtems_extension_create`` directive.  These extensions are RTEMS objects in
85that they have a name, an ID, and can be dynamically created and deleted.  In
86contrast to the static extension set, these extensions can only be created and
87installed after the initialize_executive directive successfully completes
88execution.  Dynamic extensions are useful for encapsulating the functionality
89of an extension set.  For example, the application could use extensions to
90manage a special coprocessor, do performance monitoring, and to do stack bounds
91checking.  Each of these extension sets could be written and installed
92independently of the others.
93
94All user extensions are optional and RTEMS places no naming restrictions on the
95user. The user extension entry points are copied into an internal RTEMS
96structure. This means the user does not need to keep the table after creating
97it, and changing the handler entry points dynamically in a table once created
98has no effect. Creating a table local to a function can save space in space
99limited applications.
100
101Extension switches do not effect the context switch overhead if no switch
102handler is installed.
103
104TCB Extension Area
105------------------
106.. index:: TCB extension area
107
108RTEMS provides for a pointer to a user-defined data area for each extension set
109to be linked to each task's control block.  This set of pointers is an
110extension of the TCB and can be used to store additional data required by the
111user's extension functions.
112
113The TCB extension is an array of pointers in the TCB. The index into the table
114can be obtained from the extension id returned when the extension is
115created:
116
117.. index:: rtems extensions table index
118
119.. code-block:: c
120
121    index = rtems_object_id_get_index(extension_id);
122
123The number of pointers in the area is the same as the number of user extension
124sets configured.  This allows an application to augment the TCB with
125user-defined information.  For example, an application could implement task
126profiling by storing timing statistics in the TCB's extended memory area.  When
127a task context switch is being executed, the ``TASK_SWITCH`` extension could
128read a real-time clock to calculate how long the task being swapped out has run
129as well as timestamp the starting time for the task being swapped in.
130
131If used, the extended memory area for the TCB should be allocated and the TCB
132extension pointer should be set at the time the task is created or started by
133either the ``TASK_CREATE`` or ``TASK_START`` extension.  The application is
134responsible for managing this extended memory area for the TCBs.  The memory
135may be reinitialized by the ``TASK_RESTART`` extension and should be
136deallocated by the ``TASK_DELETE`` extension when the task is deleted.  Since
137the TCB extension buffers would most likely be of a fixed size, the RTEMS
138partition manager could be used to manage the application's extended memory
139area.  The application could create a partition of fixed size TCB extension
140buffers and use the partition manager's allocation and deallocation directives
141to obtain and release the extension buffers.
142
143Extensions
144----------
145
146The sections that follow will contain a description of each extension.  Each
147section will contain a prototype of a function with the appropriate calling
148sequence for the corresponding extension.  The names given for the C function
149and its arguments are all defined by the user.  The names used in the examples
150were arbitrarily chosen and impose no naming conventions on the user.
151
152TASK_CREATE Extension
153~~~~~~~~~~~~~~~~~~~~~
154
155The TASK_CREATE extension directly corresponds to the ``rtems_task_create``
156directive.  If this extension is defined in any static or dynamic extension set
157and a task is being created, then the extension routine will automatically be
158invoked by RTEMS.  The extension should have a prototype similar to the
159following:
160
161.. index:: rtems_task_create_extension
162.. index:: rtems_extension
163
164.. code-block:: c
165
166    bool user_task_create(
167       rtems_tcb *current_task,
168       rtems_tcb *new_task
169    );
170
171where ``current_task`` can be used to access the TCB for the currently
172executing task, and new_task can be used to access the TCB for the new task
173being created.  This extension is invoked from the ``rtems_task_create``
174directive after ``new_task`` has been completely initialized, but before it is
175placed on a ready TCB chain.
176
177The user extension is expected to return the boolean value ``true`` if it
178successfully executed and ``false`` otherwise.  A task create user extension
179will frequently attempt to allocate resources.  If this allocation fails, then
180the extension should return ``false`` and the entire task create operation will
181fail.
182
183TASK_START Extension
184~~~~~~~~~~~~~~~~~~~~
185
186The ``TASK_START`` extension directly corresponds to the task_start directive.
187If this extension is defined in any static or dynamic extension set and a task
188is being started, then the extension routine will automatically be invoked by
189RTEMS.  The extension should have a prototype similar to the following:
190
191.. index:: rtems_task_start_extension
192
193.. code-block:: c
194
195    void user_task_start(
196        rtems_tcb *current_task,
197        rtems_tcb *started_task
198    );
199
200where current_task can be used to access the TCB for the currently executing
201task, and started_task can be used to access the TCB for the dormant task being
202started. This extension is invoked from the task_start directive after
203started_task has been made ready to start execution, but before it is placed on
204a ready TCB chain.
205
206TASK_RESTART Extension
207~~~~~~~~~~~~~~~~~~~~~~
208
209The ``TASK_RESTART`` extension directly corresponds to the task_restart
210directive.  If this extension is defined in any static or dynamic extension set
211and a task is being restarted, then the extension should have a prototype
212similar to the following:
213
214.. index:: rtems_task_restart_extension
215
216.. code-block:: c
217
218    void user_task_restart(
219        rtems_tcb *current_task,
220        rtems_tcb *restarted_task
221    );
222
223where current_task can be used to access the TCB for the currently executing
224task, and restarted_task can be used to access the TCB for the task being
225restarted. This extension is invoked from the task_restart directive after
226restarted_task has been made ready to start execution, but before it is placed
227on a ready TCB chain.
228
229TASK_DELETE Extension
230~~~~~~~~~~~~~~~~~~~~~
231
232The ``TASK_DELETE`` extension is associated with the task_delete directive.  If
233this extension is defined in any static or dynamic extension set and a task is
234being deleted, then the extension routine will automatically be invoked by
235RTEMS.  The extension should have a prototype similar to the
236following:
237
238.. index:: rtems_task_delete_extension
239
240.. code-block:: c
241
242    void user_task_delete(
243        rtems_tcb *current_task,
244        rtems_tcb *deleted_task
245    );
246
247where current_task can be used to access the TCB for the currently executing
248task, and deleted_task can be used to access the TCB for the task being
249deleted. This extension is invoked from the task_delete directive after the TCB
250has been removed from a ready TCB chain, but before all its resources including
251the TCB have been returned to their respective free pools.  This extension
252should not call any RTEMS directives if a task is deleting itself (current_task
253is equal to deleted_task).
254
255TASK_SWITCH Extension
256~~~~~~~~~~~~~~~~~~~~~
257
258The ``TASK_SWITCH`` extension corresponds to a task context switch.  If this
259extension is defined in any static or dynamic extension set and a task context
260switch is in progress, then the extension routine will automatically be invoked
261by RTEMS.  The extension should have a prototype similar to the following:
262
263.. index:: rtems_task_switch_extension
264
265.. code-block:: c
266
267    void user_task_switch(
268        rtems_tcb *current_task,
269        rtems_tcb *heir_task
270    );
271
272where current_task can be used to access the TCB for the task that is being
273swapped out, and heir_task can be used to access the TCB for the task being
274swapped in.  This extension is invoked from RTEMS' dispatcher routine after the
275current_task context has been saved, but before the heir_task context has been
276restored.  This extension should not call any RTEMS directives.
277
278TASK_BEGIN Extension
279~~~~~~~~~~~~~~~~~~~~
280
281The ``TASK_BEGIN`` extension is invoked when a task begins execution.  It is
282invoked immediately before the body of the starting procedure and executes in
283the context in the task.  This user extension have a prototype similar to the
284following:
285
286.. index:: rtems_task_begin_extension
287
288.. code-block:: c
289
290    void user_task_begin(
291        rtems_tcb *current_task
292    );
293
294where current_task can be used to access the TCB for the currently executing
295task which has begun.  The distinction between the ``TASK_BEGIN`` and
296TASK_START extension is that the ``TASK_BEGIN`` extension is executed in the
297context of the actual task while the TASK_START extension is executed in the
298context of the task performing the task_start directive.  For most extensions,
299this is not a critical distinction.
300
301TASK_EXITTED Extension
302~~~~~~~~~~~~~~~~~~~~~~
303
304The ``TASK_EXITTED`` extension is invoked when a task exits the body of the
305starting procedure by either an implicit or explicit return statement.  This
306user extension have a prototype similar to the following:
307
308.. index:: rtems_task_exitted_extension
309
310.. code-block:: c
311
312    void user_task_exitted(
313        rtems_tcb *current_task
314    );
315
316where current_task can be used to access the TCB for the currently executing
317task which has just exitted.
318
319Although exiting of task is often considered to be a fatal error, this
320extension allows recovery by either restarting or deleting the exiting task.
321If the user does not wish to recover, then a fatal error may be reported.  If
322the user does not provide a ``TASK_EXITTED`` extension or the provided handler
323returns control to RTEMS, then the RTEMS default handler will be used.  This
324default handler invokes the directive fatal_error_occurred with the
325``RTEMS_TASK_EXITTED`` directive status.
326
327FATAL Error Extension
328~~~~~~~~~~~~~~~~~~~~~
329
330The ``FATAL`` error extension is associated with the fatal_error_occurred
331directive.  If this extension is defined in any static or dynamic extension set
332and the fatal_error_occurred directive has been invoked, then this extension
333will be called.  This extension should have a prototype similar to the
334following:
335
336.. index:: rtems_fatal_extension
337
338.. code-block:: c
339
340    void user_fatal_error(
341        Internal_errors_Source  the_source,
342        bool                    is_internal,
343        uint32_t                the_error
344    );
345
346where the_error is the error code passed to the fatal_error_occurred
347directive. This extension is invoked from the fatal_error_occurred directive.
348
349If defined, the user's ``FATAL`` error extension is invoked before RTEMS'
350default fatal error routine is invoked and the processor is stopped.  For
351example, this extension could be used to pass control to a debugger when a
352fatal error occurs.  This extension should not call any RTEMS directives.
353
354Order of Invocation
355-------------------
356
357When one of the critical system events occur, the user extensions are invoked
358in either "forward" or "reverse" order.  Forward order indicates that the
359static extension set is invoked followed by the dynamic extension sets in the
360order in which they were created.  Reverse order means that the dynamic
361extension sets are invoked in the opposite of the order in which they were
362created followed by the static extension set.  By invoking the extension sets
363in this order, extensions can be built upon one another.  At the following
364system events, the extensions are invoked in forward order:
365
366#. Task creation
367
368#. Task initiation
369
370#. Task reinitiation
371
372#. Task deletion
373
374#. Task context switch
375
376#. Post task context switch
377
378#. Task begins to execute
379
380At the following system events, the extensions are invoked in reverse order:
381
382#. Task deletion
383
384#. Fatal error detection
385
386At these system events, the extensions are invoked in reverse order to insure
387that if an extension set is built upon another, the more complicated extension
388is invoked before the extension set it is built upon.  For example, by invoking
389the static extension set last it is known that the "system" fatal error
390extension will be the last fatal error extension executed.  Another example is
391use of the task delete extension by the Standard C Library.  Extension sets
392which are installed after the Standard C Library will operate correctly even if
393they utilize the C Library because the C Library's ``TASK_DELETE`` extension is
394invoked after that of the other extensions.
395
396Operations
397==========
398
399Creating an Extension Set
400-------------------------
401
402The ``rtems_extension_create`` directive creates and installs an extension set
403by allocating a Extension Set Control Block (ESCB), assigning the extension set
404a user-specified name, and assigning it an extension set ID.  Newly created
405extension sets are immediately installed and are invoked upon the next system
406even supporting an extension.
407
408Obtaining Extension Set IDs
409---------------------------
410
411When an extension set is created, RTEMS generates a unique extension set ID and
412assigns it to the created extension set until it is deleted.  The extension ID
413may be obtained by either of two methods.  First, as the result of an
414invocation of the ``rtems_extension_create`` directive, the extension set ID is
415stored in a user provided location.  Second, the extension set ID may be
416obtained later using the ``rtems_extension_ident`` directive.  The extension
417set ID is used by other directives to manipulate this extension set.
418
419Deleting an Extension Set
420-------------------------
421
422The ``rtems_extension_delete`` directive is used to delete an extension set.
423The extension set's control block is returned to the ESCB free list when it is
424deleted.  An extension set can be deleted by a task other than the task which
425created the extension set.  Any subsequent references to the extension's name
426and ID are invalid.
427
428Directives
429==========
430
431This section details the user extension manager's directives.  A subsection is
432dedicated to each of this manager's directives and describes the calling
433sequence, related constants, usage, and status codes.
434
435.. _rtems_extension_create:
436
437EXTENSION_CREATE - Create a extension set
438-----------------------------------------
439.. index:: create an extension set
440
441**CALLING SEQUENCE:**
442
443.. index:: rtems_extension_create
444
445.. code-block:: c
446
447    rtems_status_code rtems_extension_create(
448        rtems_name              name,
449        rtems_extensions_table *table,
450        rtems_id               *id
451    );
452
453**DIRECTIVE STATUS CODES:**
454
455.. list-table::
456 :class: rtems-table
457
458 * - ``RTEMS_SUCCESSFUL``
459   - extension set created successfully
460 * - ``RTEMS_INVALID_NAME``
461   - invalid extension set name
462 * - ``RTEMS_TOO_MANY``
463   - too many extension sets created
464
465**DESCRIPTION:**
466
467This directive creates a extension set.  The assigned extension set id is
468returned in id.  This id is used to access the extension set with other user
469extension manager directives.  For control and maintenance of the extension
470set, RTEMS allocates an ESCB from the local ESCB free pool and initializes it.
471
472**NOTES:**
473
474This directive will not cause the calling task to be
475preempted.
476
477.. _rtems_extension_ident:
478
479EXTENSION_IDENT - Get ID of a extension set
480-------------------------------------------
481.. index:: get ID of an extension set
482.. index:: obtain ID of an extension set
483
484**CALLING SEQUENCE:**
485
486.. index:: rtems_extension_ident
487
488.. code-block:: c
489
490    rtems_status_code rtems_extension_ident(
491        rtems_name  name,
492        rtems_id   *id
493    );
494
495**DIRECTIVE STATUS CODES:**
496
497.. list-table::
498 :class: rtems-table
499
500 * - ``RTEMS_SUCCESSFUL``
501   - extension set identified successfully
502 * - ``RTEMS_INVALID_NAME``
503   - extension set name not found
504
505**DESCRIPTION:**
506
507This directive obtains the extension set id associated with the extension set
508name to be acquired.  If the extension set name is not unique, then the
509extension set id will match one of the extension sets with that name.  However,
510this extension set id is not guaranteed to correspond to the desired extension
511set.  The extension set id is used to access this extension set in other
512extension set related directives.
513
514**NOTES:**
515
516This directive will not cause the running task to be preempted.
517
518.. _rtems_extension_delete:
519
520EXTENSION_DELETE - Delete a extension set
521-----------------------------------------
522.. index:: delete an extension set
523
524**CALLING SEQUENCE:**
525
526.. index:: rtems_extension_delete
527
528.. code-block:: c
529
530    rtems_status_code rtems_extension_delete(
531        rtems_id id
532    );
533
534**DIRECTIVE STATUS CODES:**
535
536.. list-table::
537 :class: rtems-table
538
539 * - ``RTEMS_SUCCESSFUL``
540   - extension set deleted successfully
541 * - ``RTEMS_INVALID_ID``
542   - invalid extension set id
543
544**DESCRIPTION:**
545
546This directive deletes the extension set specified by ``id``.  If the extension
547set is running, it is automatically canceled.  The ESCB for the deleted
548extension set is reclaimed by RTEMS.
549
550**NOTES:**
551
552This directive will not cause the running task to be preempted.
553
554A extension set can be deleted by a task other than the task which created the
555extension set.
556
557**NOTES:**
558
559This directive will not cause the running task to be preempted.
Note: See TracBrowser for help on using the repository browser.