source: rtems-docs/c_user/user_extensions.rst @ 54514fe

4.115
Last change on this file since 54514fe was 489740f, checked in by Chris Johns <chrisj@…>, on 05/20/16 at 02:47:09

Set SPDX License Identifier in each source file.

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