source: rtems-docs/c-user/fatal_error.rst @ 789b0ca

5
Last change on this file since 789b0ca was 789b0ca, checked in by Sebastian Huber <sebastian.huber@…>, on 02/19/19 at 08:17:57

c-user: INTERNAL_ERROR_NO_MEMORY_FOR_PER_CPU_DATA

Update #3507.

  • Property mode set to 100644
File size: 21.6 KB
Line 
1.. SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. Copyright (C) 1988, 2008 On-Line Applications Research Corporation (OAR)
4
5.. index:: fatal errors
6
7.. _fatal_error_manager:
8
9Fatal Error Manager
10*******************
11
12Introduction
13============
14
15The fatal error manager processes all fatal or irrecoverable errors and other
16sources of system termination (for example after :c:func:`exit()`).  Fatal
17errors are identified by the (fatal source, error code) pair.  The directives
18provided by the fatal error manager are:
19
20- rtems_fatal_ - Invoke the fatal error handler
21
22- rtems_panic_ - Print a message and invoke the fatal error handler
23
24- rtems_shutdown_executive_ - Shutdown RTEMS
25
26- rtems_exception_frame_print_ - Print the CPU exception frame
27
28- rtems_fatal_source_text_ - Return the fatal source text
29
30- rtems_internal_error_text_ - Return the error code text
31
32- rtems_fatal_error_occurred_ - Invoke the fatal error handler (deprecated)
33
34Background
35==========
36
37.. index:: fatal error detection
38.. index:: fatal error processing
39.. index:: fatal error user extension
40
41Overview
42--------
43
44The fatal error manager is called upon detection of an irrecoverable error
45condition by either RTEMS or the application software.  Fatal errors are also
46used in case it is difficult or impossible to return an error condition by
47other means, e.g. a return value of a directive call.  Fatal errors can be
48detected from various sources, for example
49
50- the executive (RTEMS),
51- support libraries,
52- user system code, and
53- user application code.
54
55RTEMS automatically invokes the fatal error manager upon detection of an error
56it considers to be fatal.  Similarly, the user should invoke the fatal error
57manager upon detection of a fatal error.
58
59Each static or dynamic user extension set may include a fatal error handler.
60The fatal error handler in the static extension set can be used to provide
61access to debuggers and monitors which may be present on the target hardware.
62If any user-supplied fatal error handlers are installed, the fatal error
63manager will invoke them.  Usually, the board support package provides a fatal
64error extension which resets the board.  If no user handlers are configured or
65if all the user handler return control to the fatal error manager, then the
66RTEMS default fatal error handler is invoked.  If the default fatal error
67handler is invoked, then the system state is marked as failed.
68
69Although the precise behavior of the default fatal error handler is processor
70specific, in general, it will disable all maskable interrupts, place the error
71code in a known processor dependent place (generally either on the stack or in
72a register), and halt the processor.  The precise actions of the RTEMS fatal
73error are discussed in the Default Fatal Error Processing chapter of the
74Applications Supplement document for a specific target processor.
75
76Fatal Sources
77-------------
78
79The following fatal sources are defined for RTEMS via the
80:c:type:`rtems_fatal_source` enumeration.  Each symbolic name has the
81corresponding numeric fatal source in parenthesis.
82
83INTERNAL_ERROR_CORE (0)
84    Errors of the core operating system.  See :ref:`internal_errors`.
85
86INTERNAL_ERROR_RTEMS_API (1)
87    Errors of the Classic API.
88
89INTERNAL_ERROR_POSIX_API (2)
90    Errors of the POSIX API.
91
92RTEMS_FATAL_SOURCE_BDBUF (3)
93    Fatal source for the block device cache.  See
94    :c:type:`rtems_bdbuf_fatal_code`.
95
96RTEMS_FATAL_SOURCE_APPLICATION (4)
97    Fatal source for application-specific errors.  The fatal code is
98    application-specific.
99
100RTEMS_FATAL_SOURCE_EXIT (5)
101    Fatal source of :c:func:`exit()`.  The fatal code is the :c:func:`exit()`
102    status code.
103
104RTEMS_FATAL_SOURCE_BSP (6)
105    Fatal source for BSP errors.  The fatal codes are defined in
106    :file:`<bsp/fatal.h>`.  Examples are interrupt and exception
107    initialization.  See :c:type:`bsp_fatal_code` and :c:func:`bsp_fatal()`.
108
109RTEMS_FATAL_SOURCE_ASSERT (7)
110    Fatal source of :c:macro:`assert()`.  The fatal code is the pointer value
111    of the assert context.  See :c:type:`rtems_assert_context`.
112
113RTEMS_FATAL_SOURCE_STACK_CHECKER (8)
114    Fatal source of the stack checker.  The fatal code is the object name of
115    the executing task.
116
117RTEMS_FATAL_SOURCE_EXCEPTION (9)
118    Fatal source of the exceptions.  The fatal code is the pointer value of the
119    exception frame pointer.  See :c:type:`rtems_exception_frame` and
120    :ref:`rtems_exception_frame_print`.
121
122RTEMS_FATAL_SOURCE_SMP (10)
123    Fatal source of SMP domain.  See :c:type:`SMP_Fatal_code`.
124
125RTEMS_FATAL_SOURCE_PANIC (11)
126    Fatal source of :c:func:`rtems_panic`, see :ref:`rtems_panic`.
127
128RTEMS_FATAL_SOURCE_INVALID_HEAP_FREE (12)
129    Fatal source for invalid C program heap frees via :c:func:`free`.  The
130    fatal code is the bad pointer.
131
132.. _internal_errors:
133
134Internal Error Codes
135--------------------
136
137The following error codes are defined for the :c:data:`INTERNAL_ERROR_CORE`
138fatal source.  Each symbolic name has the corresponding numeric error code in
139parenthesis.
140
141INTERNAL_ERROR_TOO_LITTLE_WORKSPACE (2)
142    There is not enough memory for the workspace.  This fatal error may occur
143    during system initialization.  It is an application configuration error.
144
145INTERNAL_ERROR_WORKSPACE_ALLOCATION (3)
146    An allocation from the workspace failed.  This fatal error may occur during
147    system initialization.  It is an application configuration error.
148
149INTERNAL_ERROR_INTERRUPT_STACK_TOO_SMALL (4)
150    The configured interrupt stack size is too small.  This fatal error may
151    occur during system initialization.  It is an application configuration
152    error.
153
154INTERNAL_ERROR_THREAD_EXITTED (5)
155    A non-POSIX thread entry function returned.  This is an API usage error.
156
157    An example code to provoke this fatal error is:
158
159    .. code-block:: c
160
161        void task( rtems_arg arg )
162        {
163          /* Classic API tasks must not return */
164        }
165
166        void create_bad_task( void )
167        {
168          rtems_status_code sc;
169          rtems_id          task_id;
170
171          sc = rtems_task_create(
172            rtems_build_name('T', 'A', 'S', 'K'),
173            1,
174            RTEMS_DEFAULT_MODES,
175            RTEMS_DEFAULT_ATTRIBUTES,
176            &task_id
177          );
178          assert( sc == RTEMS_SUCCESSFUL );
179
180          sc = rtems_task_start( task_id, task, 0 );
181          assert( sc == RTEMS_SUCCESSFUL );
182        }
183
184INTERNAL_ERROR_INCONSISTENT_MP_INFORMATION (6)
185    This fatal error can only occur on MPCI configurations.  The MPCI nodes or
186    global objects configuration is inconsistent.  This fatal error may occur
187    during system initialization.  It is an application configuration error.
188
189INTERNAL_ERROR_INVALID_NODE (7)
190    This fatal error can only occur on MPCI configurations.  The own MPCI node
191    number is invalid.  This fatal error may occur during system
192    initialization.  It is an application configuration error.
193
194INTERNAL_ERROR_NO_MPCI (8)
195    This fatal error can only occur on MPCI configurations.  There is no MPCI
196    configuration table.  This fatal error may occur during system
197    initialization.  It is an application configuration error.
198
199INTERNAL_ERROR_BAD_PACKET (9)
200    This fatal error can only occur on MPCI configurations.  The MPCI server
201    thread received a bad packet.
202
203INTERNAL_ERROR_OUT_OF_PACKETS (10)
204    This fatal error can only occur on MPCI configurations.  The MPCI packet
205    pool is empty.  It is an application configuration error.
206
207INTERNAL_ERROR_OUT_OF_GLOBAL_OBJECTS (11)
208    This fatal error can only occur on MPCI configurations.  The MPCI global
209    objects pool is empty.  It is an application configuration error.
210
211INTERNAL_ERROR_OUT_OF_PROXIES (12)
212    This fatal error can only occur on MPCI configurations.  The MPCI thread
213    proxy pool is empty.  It is an application configuration error.
214
215INTERNAL_ERROR_INVALID_GLOBAL_ID (13)
216    This fatal error can only occur on MPCI configurations.  The system cannot
217    find the global object for a specific object identifier.  In case this
218    happens, then this is probably an operating system bug.
219
220INTERNAL_ERROR_BAD_STACK_HOOK (14)
221    The stack allocator hook or stack free hook is NULL.  This fatal error may
222    occur during system initialization.  It is an application configuration
223    error.
224
225INTERNAL_ERROR_UNLIMITED_AND_MAXIMUM_IS_0 (19)
226    An object class is configured to use the unlimited objects option, however,
227    the count of objects for each extension is zero.  This fatal error may
228    occur during system initialization.  It is an application configuration
229    error.
230
231INTERNAL_ERROR_NO_MEMORY_FOR_HEAP (23)
232    There is not enough memory for the C program heap.  This fatal error may
233    occur during system initialization.  It is an application configuration
234    error.
235
236INTERNAL_ERROR_CPU_ISR_INSTALL_VECTOR (24)
237    The use of :c:func:`_CPU_ISR_install_vector()` is illegal on this system.
238
239INTERNAL_ERROR_RESOURCE_IN_USE (25)
240    This fatal error can only occur on debug configurations.  It happens in
241    case a thread which owns mutexes is deleted.  Mutexes owned by a deleted
242    thread are in an inconsistent state.
243
244INTERNAL_ERROR_RTEMS_INIT_TASK_ENTRY_IS_NULL (26)
245    An RTEMS initialization task entry function is NULL.  This fatal error may
246    occur during system initialization.  It is an application configuration
247    error.
248
249INTERNAL_ERROR_THREAD_QUEUE_DEADLOCK (28)
250    A deadlock was detected during a thread queue enqueue operation.
251
252INTERNAL_ERROR_THREAD_QUEUE_ENQUEUE_STICKY_FROM_BAD_STATE (29)
253    This fatal error can only happen in SMP configurations.  It is not allowed
254    to obtain MrsP semaphores in a context with thread dispatching disabled,
255    for example interrupt context.
256
257    An example code to provoke this fatal error is:
258
259    .. code-block:: c
260
261        void bad( rtems_id timer_id, void *arg )
262        {
263          rtems_id *sem_id;
264
265          sem_id = arg;
266
267          rtems_semaphore_obtain( *sem_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
268          assert( 0 );
269        }
270
271        void fire_bad_timer( rtems_task_argument arg )
272        {
273          rtems_status_code sc;
274          rtems_id          sem_id;
275          rtems_id          timer_id;
276
277          sc = rtems_semaphore_create(
278            rtems_build_name('M', 'R', 'S', 'P'),
279            1,
280            RTEMS_MULTIPROCESSOR_RESOURCE_SHARING
281              | RTEMS_BINARY_SEMAPHORE,
282            1,
283            &sem_id
284          );
285          assert( sc == RTEMS_SUCCESSFUL );
286
287          sc = rtems_timer_create(
288            rtems_build_name( 'E', 'V', 'I', 'L' ),
289            &timer_id
290          );
291          assert( sc == RTEMS_SUCCESSFUL );
292
293          sc = rtems_semaphore_obtain( sem_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
294          assert( sc == RTEMS_SUCCESSFUL );
295
296          sc = rtems_timer_fire_after( timer_id, 1, bad, &sem_id );
297          assert( sc == RTEMS_SUCCESSFUL );
298
299          rtems_task_wake_after( 2 );
300          assert( 0 );
301        }
302
303INTERNAL_ERROR_BAD_THREAD_DISPATCH_DISABLE_LEVEL (30)
304    It is illegal to call blocking operating system services with thread
305    dispatching disabled, for example in interrupt context.
306
307    An example code to provoke this fatal error is:
308
309    .. code-block:: c
310
311        void bad( rtems_id id, void *arg )
312        {
313          rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
314          assert( 0 );
315        }
316
317        void fire_bad_timer( void )
318        {
319          rtems_status_code sc;
320          rtems_id          id;
321
322          sc = rtems_timer_create(
323            rtems_build_name( 'E', 'V', 'I', 'L' ),
324            &id
325          );
326          assert( sc == RTEMS_SUCCESSFUL );
327
328          sc = rtems_timer_fire_after( id, 1, bad, NULL );
329          assert( sc == RTEMS_SUCCESSFUL );
330
331          rtems_task_wake_after( 2 );
332          assert( 0 );
333        }
334
335INTERNAL_ERROR_BAD_THREAD_DISPATCH_ENVIRONMENT (31)
336    In SMP configurations, it is a fatal error to call blocking operating
337    system with interrupts disabled, since this prevents delivery of
338    inter-processor interrupts.  This could lead to executing threads which are
339    not allowed to execute resulting in undefined system behaviour.
340
341    Some CPU ports, for example the ARM Cortex-M port, have a similar problem,
342    since the interrupt state is not a part of the thread context.
343
344    This fatal error is detected in the operating system core function
345    :c:func:`_Thread_Do_dispatch()` responsible to carry out a thread dispatch.
346
347    An example code to provoke this fatal error is:
348
349    .. code-block:: c
350
351        void bad( void )
352        {
353          rtems_interrupt_level level;
354
355          rtems_interrupt_local_disable( level );
356          rtems_task_suspend( RTEMS_SELF );
357          rtems_interrupt_local_enable( level  );
358        }
359
360INTERNAL_ERROR_RTEMS_INIT_TASK_CREATE_FAILED (32)
361    Creation of an RTEMS initialization task failed.  This fatal error may
362    occur during system initialization.  It is an application configuration
363    error.
364
365INTERNAL_ERROR_POSIX_INIT_THREAD_CREATE_FAILED (33)
366    Creation of a POSIX initialization thread failed.  This fatal error may
367    occur during system initialization.  It is an application configuration
368    error.
369
370INTERNAL_ERROR_LIBIO_USER_ENV_KEY_CREATE_FAILED (34)
371    Creation of the IO library user environment POSIX key failed.  This fatal
372    error may occur during system initialization.  It is an application
373    configuration error.
374
375INTERNAL_ERROR_LIBIO_SEM_CREATE_FAILED (35)
376    Creation of the IO library semaphore failed.  This fatal error may occur
377    during system initialization.  It is an application configuration error.
378
379INTERNAL_ERROR_LIBIO_STDOUT_FD_OPEN_FAILED (36)
380    Open of the standard output file descriptor failed or resulted in an
381    unexpected file descriptor number.  This fatal error may occur during
382    system initialization.  It is an application configuration error.
383
384INTERNAL_ERROR_LIBIO_STDERR_FD_OPEN_FAILED (37)
385    Open of the standard error file descriptor failed or resulted in an
386    unexpected file descriptor number.  This fatal error may occur during
387    system initialization.  It is an application configuration error.
388
389INTERNAL_ERROR_ILLEGAL_USE_OF_FLOATING_POINT_UNIT (38)
390    The floating point unit was used illegally, for example in interrupt
391    context on some architectures.
392
393INTERNAL_ERROR_ARC4RANDOM_GETENTROPY_FAIL (39)
394    A :c:func:`getentropy` system call failed in one of the `ARC4RANDOM(3)
395    <https://man.openbsd.org/arc4random.3>`_ functions.  This fatal error can
396    only be fixed with a different implementation of :c:func:`getentropy`.
397
398INTERNAL_ERROR_NO_MEMORY_FOR_PER_CPU_DATA (40)
399    This fatal error may happen during workspace initialization.  There is not
400    enough memory available to populate the per-CPU data areas, see
401    `<rtems/score/percpudata.h> <https://git.rtems.org/rtems/tree/cpukit/include/rtems/score/percpudata.h>`_.
402
403Operations
404==========
405
406.. index:: _Terminate
407
408.. _Terminate:
409
410Announcing a Fatal Error
411------------------------
412
413The :c:func:`_Terminate()` internal error handler is invoked when the
414application or the executive itself determines that a fatal error has occurred
415or a final system state is reached (for example after :c:func:`rtems_fatal()`
416or :c:func:`exit()`).
417
418The first action of the internal error handler is to call the fatal extension of
419the user extensions.  For the initial extensions the following conditions are
420required
421
422- a valid stack pointer and enough stack space,
423
424- a valid code memory, and
425
426- valid read-only data.
427
428For the initial extensions the read-write data (including .bss segment) is not
429required on single processor configurations.  In SMP configurations, however,
430the read-write data must be initialized since this function must determine the
431state of the other processors and request them to shut-down if necessary.
432
433Non-initial extensions require in addition valid read-write data.  The board
434support package (BSP) may install an initial extension that performs a system
435reset.  In this case the non-initial extensions will be not called.
436
437The fatal extensions are called with three parameters:
438
439- the fatal source,
440
441- a legacy parameter which is always false, and
442
443- an error code with a fatal source dependent content.
444
445Once all fatal extensions executed, the error information will be stored to
446:c:data:`_Internal_errors_What_happened` and the system state is set to
447:c:macro:`SYSTEM_STATE_TERMINATED`.
448
449The final step is to call the CPU port specific :c:func:`_CPU_Fatal_halt()`.
450
451Directives
452==========
453
454This section details the fatal error manager's directives.  A subsection is
455dedicated to each of this manager's directives and describes the calling
456sequence, related constants, usage, and status codes.
457
458.. raw:: latex
459
460   \clearpage
461
462.. index:: announce fatal error
463.. index:: fatal error, announce
464.. index:: rtems_fatal
465
466.. _rtems_fatal:
467
468FATAL - Invoke the fatal error handler
469--------------------------------------
470
471CALLING SEQUENCE:
472    .. code-block:: c
473
474        void rtems_fatal(
475          rtems_fatal_source fatal_source,
476          rtems_fatal_code   error_code
477        ) RTEMS_NO_RETURN;
478
479DIRECTIVE STATUS CODES:
480    NONE - This function will not return to the caller.
481
482DESCRIPTION:
483    This directive terminates the system.
484
485NOTE:
486    Registered :c:func:`atexit()` or :c:func:`on_exit()` handlers are not
487    called.  Use :c:func:`exit()` in case these handlers should be invoked.
488
489.. raw:: latex
490
491   \clearpage
492
493.. index:: panic
494.. index:: rtems_panic
495
496.. _rtems_panic:
497
498PANIC - Print a message and invoke the fatal error handler
499----------------------------------------------------------
500
501CALLING SEQUENCE:
502    .. code-block:: c
503
504        void rtems_panic(
505          const char *fmt,
506          ...
507        ) RTEMS_NO_RETURN RTEMS_PRINTFLIKE( 1, 2 );
508
509DIRECTIVE STATUS CODES:
510    NONE - This function will not return to the caller.
511
512DESCRIPTION:
513    This directive prints a message via :c:func:`printk` specified by the
514    format and optional parameters and then terminates the system with the
515    :c:macro:`RTEMS_FATAL_SOURCE_PANIC` fatal source.  The fatal code is set to
516    the format string address.
517
518NOTE:
519    Registered :c:func:`atexit()` or :c:func:`on_exit()` handlers are not
520    called.  Use :c:func:`exit()` in case these handlers should be invoked.
521
522.. raw:: latex
523
524   \clearpage
525
526.. index:: shutdown RTEMS
527.. index:: rtems_shutdown_executive
528
529.. _rtems_shutdown_executive:
530
531SHUTDOWN_EXECUTIVE - Shutdown RTEMS
532-----------------------------------
533
534CALLING SEQUENCE:
535    .. code-block:: c
536
537        void rtems_shutdown_executive(
538            uint32_t result
539        );
540
541DIRECTIVE STATUS CODES:
542    NONE - This function will not return to the caller.
543
544DESCRIPTION:
545    This directive is called when the application wishes to shutdown RTEMS.
546    The system is terminated with a fatal source of ``RTEMS_FATAL_SOURCE_EXIT``
547    and the specified ``result`` code.
548
549NOTES:
550    This directive *must* be the last RTEMS directive invoked by an application
551    and it *does not return* to the caller.
552
553    This directive may be called any time.
554
555.. raw:: latex
556
557   \clearpage
558
559.. index:: exception frame
560.. index:: rtems_exception_frame_print
561
562.. _rtems_exception_frame_print:
563
564EXCEPTION_FRAME_PRINT - Prints the exception frame
565--------------------------------------------------
566
567CALLING SEQUENCE:
568    .. code-block:: c
569
570        void rtems_exception_frame_print(
571            const rtems_exception_frame *frame
572        );
573
574DIRECTIVE STATUS CODES:
575    NONE
576
577DESCRIPTION:
578    Prints the exception frame via ``printk()``.
579
580.. raw:: latex
581
582   \clearpage
583
584.. index:: fatal error
585.. index:: rtems_fatal_source_text
586
587.. _rtems_fatal_source_text:
588
589FATAL_SOURCE_TEXT - Returns a text for a fatal source
590-----------------------------------------------------
591
592CALLING SEQUENCE:
593    .. code-block:: c
594
595        const char *rtems_fatal_source_text(
596            rtems_fatal_source source
597        );
598
599DIRECTIVE STATUS CODES:
600    The fatal source text or "?" in case the passed fatal source is invalid.
601
602DESCRIPTION:
603    Returns a text for a fatal source.  The text for fatal source is the
604    enumerator constant.
605
606.. raw:: latex
607
608   \clearpage
609
610.. index:: fatal error
611.. index:: rtems_internal_error_text
612
613.. _rtems_internal_error_text:
614
615INTERNAL_ERROR_TEXT - Returns a text for an internal error code
616---------------------------------------------------------------
617
618CALLING SEQUENCE:
619    .. code-block:: c
620
621        const char *rtems_internal_error_text(
622            rtems_fatal_code error
623        );
624
625DIRECTIVE STATUS CODES:
626    The error code text or "?" in case the passed error code is invalid.
627
628DESCRIPTION:
629    Returns a text for an internal error code.  The text for each internal
630    error code is the enumerator constant.
631
632.. raw:: latex
633
634   \clearpage
635
636.. index:: announce fatal error
637.. index:: fatal error, announce
638.. index:: rtems_fatal_error_occurred
639
640.. _rtems_fatal_error_occurred:
641
642FATAL_ERROR_OCCURRED - Invoke the fatal error handler (deprecated)
643------------------------------------------------------------------
644
645CALLING SEQUENCE:
646    .. code-block:: c
647
648        void rtems_fatal_error_occurred(
649            uint32_t  the_error
650        ) RTEMS_NO_RETURN;
651
652DIRECTIVE STATUS CODES:
653    NONE - This function will not return to the caller.
654
655DESCRIPTION:
656    This directive processes fatal errors.  If the FATAL error extension is
657    defined in the configuration table, then the user-defined error extension
658    is called.  If configured and the provided FATAL error extension returns,
659    then the RTEMS default error handler is invoked.  This directive can be
660    invoked by RTEMS or by the user's application code including initialization
661    tasks, other tasks, and ISRs.
662
663NOTES:
664    This directive is deprecated and should not be used in new code.
665
666    This directive supports local operations only.
667
668    Unless the user-defined error extension takes special actions such as
669    restarting the calling task, this directive WILL NOT RETURN to the caller.
670
671    The user-defined extension for this directive may wish to initiate a global
672    shutdown.
Note: See TracBrowser for help on using the repository browser.