• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _module-pw_cpu_exception:
2
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
13-----
14Setup
15-----
16This module has three facades, each of whose backends are set with a
17different GN variable.
18
19``pw_cpu_exception_ENTRY_BACKEND``
20==================================
21This is the library that handles early exception entry and prepares any CPU
22state that must be available to the exception handler via the
23pw_cpu_exception_State object. The backend for this facade is
24architecture-specific.
25
26An application using this module **must** connect ``pw_cpu_exception_Entry()`` to
27the platform's CPU exception handler interrupt so ``pw_cpu_exception_Entry()`` is
28called immediately upon a CPU exception. For specifics on how this may be done,
29see the backend documentation for your architecture.
30
31``pw_cpu_exception_HANDLER_BACKEND``
32====================================
33This facade is backed by an application-specific handler that determines what to
34do when an exception is encountered. This may be capturing a crash report before
35resetting the device, or in some cases handling the exception to allow execution
36to continue.
37
38Applications must also provide an implementation for
39``pw_cpu_exception_DefaultHandler()``. The behavior of this functions is entirely
40up to the application/project, but some examples are provided below:
41
42  * Enter an infinite loop so the device can be debugged by JTAG.
43  * Reset the device.
44  * Attempt to handle the exception so execution can continue.
45  * Capture and record additional device state and save to flash for a crash
46    report.
47  * A combination of the above, using logic that fits the needs of your project.
48
49``pw_cpu_exception_SUPPORT_BACKEND``
50====================================
51This facade provides architecture-independent functions that may be helpful for
52dumping CPU state in various forms. This allows an application to create an
53application-specific handler that is portable across multiple architectures.
54
55Avoiding circular dependencies with ``pw_cpu_exception_ENTRY_BACKEND``
56======================================================================
57The entry facade is hard tied to the definition of the
58``pw_cpu_exception_State``, so spliting them into separate facades would require
59extra configurations along with extra compatibility checks to ensure they are
60never mismatched.
61
62In GN, this circular dependency is avoided by collecting the backend's full
63implementation including the entry method through the
64``pw_cpu_exception:entry_impl`` group. When ``pw_cpu_exception_ENTRY_BACKEND``
65is set, ``$dir_pw_cpu_exception:entry_impl`` must listed in the
66``pw_build_LINK_DEPS`` variable. See :ref:`module-pw_build-link-deps`.
67
68Entry backends must provide their own ``*.impl`` target that collects their
69entry implementation.
70
71------------
72Module Usage
73------------
74Basic usage of this module entails applications supplying a definition for
75``pw_cpu_exception_DefaultHandler()``. ``pw_cpu_exception_DefaultHandler()`` should
76contain any logic to determine if a exception can be recovered from, as well as
77necessary actions to properly recover. If the device cannot recover from the
78exception, the function should **not** return.
79
80``pw_cpu_exception_DefaultHandler()`` is called indirectly, and may be overridden
81at runtime via ``pw_cpu_exception_SetHandler()``. The handler can also be reset to
82point to ``pw_cpu_exception_DefaultHandler()`` by calling
83``pw_cpu_exception_RestoreDefaultHandler()``.
84
85When writing an exception handler, prefer to use the functions provided by this
86interface rather than relying on the backend implementation of
87``pw_cpu_exception_State``. This allows better code portability as it helps
88prevent an application fault handler from being tied to a single backend.
89
90For example; when logging or dumping CPU state, prefer ``ToString()`` or
91``RawFaultingCpuState()`` over directly accessing members of a
92``pw_cpu_exception_State`` object.
93
94Some exception handling behavior may require architecture-specific CPU state to
95attempt to correct a fault. In this situation, the application's exception
96handler will be tied to the backend implementation of the CPU exception module.
97
98--------------------
99Backend Expectations
100--------------------
101CPU exception backends do not provide an exception handler, but instead provide
102mechanisms to capture CPU state for use by an application's exception handler,
103and allow recovery from CPU exceptions when possible.
104
105  * The entry backend should provide a definition for the
106    ``pw_cpu_exception_State`` object through
107    ``pw_cpu_exception_backend/state.h``.
108  * In GN, the entry backend should also provide a ``.impl`` suffixed form of
109    the entry backend target which collects the actual entry implementation to
110    avoid circular dependencies due to the state definition in the entry backend
111    target.
112  * The entry backend should implement the ``pw_cpu_exception_Entry()`` function
113    that will call ``pw_cpu_exception_HandleException()`` after performing any
114    necessary actions prior to handing control to the application's exception
115    handler (e.g. capturing necessary CPU state).
116  * If an application's exception handler backend modifies the captured CPU
117    state, the state should be treated as though it were the original state of
118    the CPU when the exception occurred. The backend may need to manually
119    restore some of the modified state to ensure this on exception handler
120    return.
121
122-------------
123Compatibility
124-------------
125Most of the pw_cpu_exception module is C-compatible. The exception to this is
126the "support" facade and library, which requires C++.
127
128------------
129Dependencies
130------------
131  * ``pw_span``
132  * ``pw_preprocessor``
133