blob: d375623cdfb28e4c3b7cc7c3c0dcb3a0c0041eae [file] [log] [blame]
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -07001.. _module-pw_cpu_exception:
Armando Montanez5104cd62019-12-10 14:36:43 -08002
3----------------
4pw_cpu_exception
5----------------
6Pigweed's exception module provides a consistent interface for entering an
7application's CPU exception handler. While the actual exception handling
8behavior is left to an application to implement, this module deals with any
9architecture-specific actions required before calling the application exception
10handler. More specifically, the exception module collects CPU state that may
11otherwise be clobbered by an application's exception handler.
12
13Setup
14=====
Armando Montaneza9ca9992021-01-26 17:06:10 -080015An application using this module **must** connect ``pw_cpu_exception_Entry()`` to
16the platform's CPU exception handler interrupt so ``pw_cpu_exception_Entry()`` is
Armando Montanez5104cd62019-12-10 14:36:43 -080017called immediately upon a CPU exception. For specifics on how this may be done,
18see the backend documentation for your architecture.
19
20Applications must also provide an implementation for
Armando Montaneza9ca9992021-01-26 17:06:10 -080021``pw_cpu_exception_DefaultHandler()``. The behavior of this functions is entirely
Armando Montanez356bf972020-06-04 10:35:55 -070022up to the application/project, but some examples are provided below:
Armando Montanez5104cd62019-12-10 14:36:43 -080023
24 * Enter an infinite loop so the device can be debugged by JTAG.
25 * Reset the device.
26 * Attempt to handle the exception so execution can continue.
27 * Capture and record additional device state and save to flash for a crash
28 report.
29 * A combination of the above, using logic that fits the needs of your project.
30
31Module Usage
32============
33Basic usage of this module entails applications supplying a definition for
Armando Montaneza9ca9992021-01-26 17:06:10 -080034``pw_cpu_exception_DefaultHandler()``. ``pw_cpu_exception_DefaultHandler()`` should
Armando Montanez356bf972020-06-04 10:35:55 -070035contain any logic to determine if a exception can be recovered from, as well as
36necessary actions to properly recover. If the device cannot recover from the
Armando Montanez5104cd62019-12-10 14:36:43 -080037exception, the function should **not** return.
38
Armando Montaneza9ca9992021-01-26 17:06:10 -080039``pw_cpu_exception_DefaultHandler()`` is called indirectly, and may be overridden
40at runtime via ``pw_cpu_exception_SetHandler()``. The handler can also be reset to
41point to ``pw_cpu_exception_DefaultHandler()`` by calling
42``pw_cpu_exception_RestoreDefaultHandler()``.
Armando Montanez356bf972020-06-04 10:35:55 -070043
Armando Montanez5104cd62019-12-10 14:36:43 -080044When writing an exception handler, prefer to use the functions provided by this
Armando Montanez356bf972020-06-04 10:35:55 -070045interface rather than relying on the backend implementation of
Armando Montaneza9ca9992021-01-26 17:06:10 -080046``pw_cpu_exception_State``. This allows better code portability as it helps
Armando Montanez356bf972020-06-04 10:35:55 -070047prevent an application fault handler from being tied to a single backend.
Armando Montanez5104cd62019-12-10 14:36:43 -080048
49For example; when logging or dumping CPU state, prefer ``ToString()`` or
Armando Montanez356bf972020-06-04 10:35:55 -070050``RawFaultingCpuState()`` over directly accessing members of a
Armando Montaneza9ca9992021-01-26 17:06:10 -080051``pw_cpu_exception_State`` object.
Armando Montanez5104cd62019-12-10 14:36:43 -080052
53Some exception handling behavior may require architecture-specific CPU state to
54attempt to correct a fault. In this situation, the application's exception
55handler will be tied to the backend implementation of the CPU exception module.
56
Armando Montanez5104cd62019-12-10 14:36:43 -080057Backend Expectations
58====================
59CPU exception backends do not provide an exception handler, but instead provide
60mechanisms to capture CPU state for use by an application's exception handler,
61and allow recovery from CPU exceptions when possible.
62
Armando Montaneza9ca9992021-01-26 17:06:10 -080063 * A backend should provide a definition for the ``pw_cpu_exception_State``
Armando Montanez356bf972020-06-04 10:35:55 -070064 struct that provides suitable means to access and modify any captured CPU
65 state.
Armando Montanez5104cd62019-12-10 14:36:43 -080066 * If an application's exception handler modifies the captured CPU state, the
67 state should be treated as though it were the original state of the CPU when
68 the exception occurred. The backend may need to manually restore some of the
69 modified state to ensure this on exception handler return.
Armando Montaneza9ca9992021-01-26 17:06:10 -080070 * A backend should implement the ``pw_cpu_exception_Entry()`` function that will
71 call ``pw_cpu_exception_HandleException()`` after performing any necessary
Armando Montanez356bf972020-06-04 10:35:55 -070072 actions prior to handing control to the application's exception handler
73 (e.g. capturing necessary CPU state).
74
75Compatibility
76=============
77Most of the pw_cpu_exception module is C-compatible. The exception to this is
78the "support" facade and library, which requires C++.
79
80Dependencies
81============
82 * ``pw_span``
83 * ``pw_preprocessor``