xref: /aosp_15_r20/external/pigweed/pw_cpu_exception/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_cpu_exception:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker================
4*61c4878aSAndroid Build Coastguard Workerpw_cpu_exception
5*61c4878aSAndroid Build Coastguard Worker================
6*61c4878aSAndroid Build Coastguard WorkerPigweed's exception module provides a consistent interface for entering an
7*61c4878aSAndroid Build Coastguard Workerapplication's CPU exception handler. While the actual exception handling
8*61c4878aSAndroid Build Coastguard Workerbehavior is left to an application to implement, this module deals with any
9*61c4878aSAndroid Build Coastguard Workerarchitecture-specific actions required before calling the application exception
10*61c4878aSAndroid Build Coastguard Workerhandler. More specifically, the exception module collects CPU state that may
11*61c4878aSAndroid Build Coastguard Workerotherwise be clobbered by an application's exception handler.
12*61c4878aSAndroid Build Coastguard Worker
13*61c4878aSAndroid Build Coastguard Worker-----
14*61c4878aSAndroid Build Coastguard WorkerSetup
15*61c4878aSAndroid Build Coastguard Worker-----
16*61c4878aSAndroid Build Coastguard WorkerThis module has three facades, each of whose backends must be provided by the
17*61c4878aSAndroid Build Coastguard Workertarget or application.
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard WorkerEntry facade
20*61c4878aSAndroid Build Coastguard Worker============
21*61c4878aSAndroid Build Coastguard WorkerThis is the library that handles early exception entry and prepares any CPU
22*61c4878aSAndroid Build Coastguard Workerstate that must be available to the exception handler via the
23*61c4878aSAndroid Build Coastguard Workerpw_cpu_exception_State object. The backend for this facade is
24*61c4878aSAndroid Build Coastguard Workerarchitecture-specific.
25*61c4878aSAndroid Build Coastguard Worker
26*61c4878aSAndroid Build Coastguard WorkerAn application using this module **must** connect ``pw_cpu_exception_Entry()`` to
27*61c4878aSAndroid Build Coastguard Workerthe platform's CPU exception handler interrupt so ``pw_cpu_exception_Entry()`` is
28*61c4878aSAndroid Build Coastguard Workercalled immediately upon a CPU exception. For specifics on how this may be done,
29*61c4878aSAndroid Build Coastguard Workersee the backend documentation for your architecture.
30*61c4878aSAndroid Build Coastguard Worker
31*61c4878aSAndroid Build Coastguard WorkerThe GN variable to set the backend for this facade is
32*61c4878aSAndroid Build Coastguard Worker``pw_cpu_exception_ENTRY_BACKEND``.
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard WorkerHandler facade
35*61c4878aSAndroid Build Coastguard Worker==============
36*61c4878aSAndroid Build Coastguard WorkerThis facade is backed by an application-specific handler that determines what to
37*61c4878aSAndroid Build Coastguard Workerdo when an exception is encountered. This may be capturing a crash report before
38*61c4878aSAndroid Build Coastguard Workerresetting the device, or in some cases handling the exception to allow execution
39*61c4878aSAndroid Build Coastguard Workerto continue.
40*61c4878aSAndroid Build Coastguard Worker
41*61c4878aSAndroid Build Coastguard WorkerApplications must also provide an implementation for
42*61c4878aSAndroid Build Coastguard Worker``pw_cpu_exception_DefaultHandler()``. The behavior of this functions is entirely
43*61c4878aSAndroid Build Coastguard Workerup to the application/project, but some examples are provided below:
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard Worker* Enter an infinite loop so the device can be debugged by JTAG.
46*61c4878aSAndroid Build Coastguard Worker* Reset the device.
47*61c4878aSAndroid Build Coastguard Worker* Attempt to handle the exception so execution can continue.
48*61c4878aSAndroid Build Coastguard Worker* Capture and record additional device state and save to flash for a crash
49*61c4878aSAndroid Build Coastguard Worker  report.
50*61c4878aSAndroid Build Coastguard Worker* A combination of the above, using logic that fits the needs of your project.
51*61c4878aSAndroid Build Coastguard Worker
52*61c4878aSAndroid Build Coastguard WorkerThe GN variable to set the backend for this facade is
53*61c4878aSAndroid Build Coastguard Worker``pw_cpu_exception_HANDLER_BACKEND``.
54*61c4878aSAndroid Build Coastguard Worker
55*61c4878aSAndroid Build Coastguard WorkerSupport facade
56*61c4878aSAndroid Build Coastguard Worker==============
57*61c4878aSAndroid Build Coastguard WorkerThis facade provides architecture-independent functions that may be helpful for
58*61c4878aSAndroid Build Coastguard Workerdumping CPU state in various forms. This allows an application to create an
59*61c4878aSAndroid Build Coastguard Workerapplication-specific handler that is portable across multiple architectures.
60*61c4878aSAndroid Build Coastguard Worker
61*61c4878aSAndroid Build Coastguard WorkerThe GN variable to set the backend for this facade is
62*61c4878aSAndroid Build Coastguard Worker``pw_cpu_exception_SUPPORT_BACKEND``.
63*61c4878aSAndroid Build Coastguard Worker
64*61c4878aSAndroid Build Coastguard WorkerAvoiding circular dependencies with the entry facade
65*61c4878aSAndroid Build Coastguard Worker====================================================
66*61c4878aSAndroid Build Coastguard WorkerThe entry facade is hard tied to the definition of the
67*61c4878aSAndroid Build Coastguard Worker``pw_cpu_exception_State``, so spliting them into separate facades would require
68*61c4878aSAndroid Build Coastguard Workerextra configurations along with extra compatibility checks to ensure they are
69*61c4878aSAndroid Build Coastguard Workernever mismatched.
70*61c4878aSAndroid Build Coastguard Worker
71*61c4878aSAndroid Build Coastguard WorkerIn GN, this circular dependency is avoided by collecting the backend's full
72*61c4878aSAndroid Build Coastguard Workerimplementation including the entry method through the
73*61c4878aSAndroid Build Coastguard Worker``pw_cpu_exception:entry_impl`` group. When ``pw_cpu_exception_ENTRY_BACKEND``
74*61c4878aSAndroid Build Coastguard Workeris set, ``$dir_pw_cpu_exception:entry_impl`` must listed in the
75*61c4878aSAndroid Build Coastguard Worker``pw_build_LINK_DEPS`` variable. See :ref:`module-pw_build-link-deps`.
76*61c4878aSAndroid Build Coastguard Worker
77*61c4878aSAndroid Build Coastguard WorkerEntry backends must provide their own ``*.impl`` target that collects their
78*61c4878aSAndroid Build Coastguard Workerentry implementation.
79*61c4878aSAndroid Build Coastguard Worker
80*61c4878aSAndroid Build Coastguard WorkerIn Bazel, this circular dependency is avoided by putting the backend's full
81*61c4878aSAndroid Build Coastguard Workerimplementation including the entry method into a separate override-able
82*61c4878aSAndroid Build Coastguard Worker``entry_backend_impl`` library. When the entry facade is being used, the
83*61c4878aSAndroid Build Coastguard Workerapplication should add a dependency on the
84*61c4878aSAndroid Build Coastguard Worker``//pw_cpu_exception:entry_backend_impl`` label_flag.
85*61c4878aSAndroid Build Coastguard Worker
86*61c4878aSAndroid Build Coastguard Worker------------
87*61c4878aSAndroid Build Coastguard WorkerModule Usage
88*61c4878aSAndroid Build Coastguard Worker------------
89*61c4878aSAndroid Build Coastguard WorkerBasic usage of this module entails applications supplying a definition for
90*61c4878aSAndroid Build Coastguard Worker``pw_cpu_exception_DefaultHandler()``. ``pw_cpu_exception_DefaultHandler()`` should
91*61c4878aSAndroid Build Coastguard Workercontain any logic to determine if a exception can be recovered from, as well as
92*61c4878aSAndroid Build Coastguard Workernecessary actions to properly recover. If the device cannot recover from the
93*61c4878aSAndroid Build Coastguard Workerexception, the function should **not** return.
94*61c4878aSAndroid Build Coastguard Worker
95*61c4878aSAndroid Build Coastguard Worker``pw_cpu_exception_DefaultHandler()`` is called indirectly, and may be overridden
96*61c4878aSAndroid Build Coastguard Workerat runtime via ``pw_cpu_exception_SetHandler()``. The handler can also be reset to
97*61c4878aSAndroid Build Coastguard Workerpoint to ``pw_cpu_exception_DefaultHandler()`` by calling
98*61c4878aSAndroid Build Coastguard Worker``pw_cpu_exception_RestoreDefaultHandler()``.
99*61c4878aSAndroid Build Coastguard Worker
100*61c4878aSAndroid Build Coastguard WorkerWhen writing an exception handler, prefer to use the functions provided by this
101*61c4878aSAndroid Build Coastguard Workerinterface rather than relying on the backend implementation of
102*61c4878aSAndroid Build Coastguard Worker``pw_cpu_exception_State``. This allows better code portability as it helps
103*61c4878aSAndroid Build Coastguard Workerprevent an application fault handler from being tied to a single backend.
104*61c4878aSAndroid Build Coastguard Worker
105*61c4878aSAndroid Build Coastguard WorkerFor example; when logging or dumping CPU state, prefer ``ToString()`` or
106*61c4878aSAndroid Build Coastguard Worker``RawFaultingCpuState()`` over directly accessing members of a
107*61c4878aSAndroid Build Coastguard Worker``pw_cpu_exception_State`` object.
108*61c4878aSAndroid Build Coastguard Worker
109*61c4878aSAndroid Build Coastguard WorkerSome exception handling behavior may require architecture-specific CPU state to
110*61c4878aSAndroid Build Coastguard Workerattempt to correct a fault. In this situation, the application's exception
111*61c4878aSAndroid Build Coastguard Workerhandler will be tied to the backend implementation of the CPU exception module.
112*61c4878aSAndroid Build Coastguard Worker
113*61c4878aSAndroid Build Coastguard Worker--------------------
114*61c4878aSAndroid Build Coastguard WorkerBackend Expectations
115*61c4878aSAndroid Build Coastguard Worker--------------------
116*61c4878aSAndroid Build Coastguard WorkerCPU exception backends do not provide an exception handler, but instead provide
117*61c4878aSAndroid Build Coastguard Workermechanisms to capture CPU state for use by an application's exception handler,
118*61c4878aSAndroid Build Coastguard Workerand allow recovery from CPU exceptions when possible.
119*61c4878aSAndroid Build Coastguard Worker
120*61c4878aSAndroid Build Coastguard Worker* The entry backend should provide a definition for the
121*61c4878aSAndroid Build Coastguard Worker  ``pw_cpu_exception_State`` object through
122*61c4878aSAndroid Build Coastguard Worker  ``pw_cpu_exception_backend/state.h``.
123*61c4878aSAndroid Build Coastguard Worker* In GN, the entry backend should also provide a ``.impl`` suffixed form of the
124*61c4878aSAndroid Build Coastguard Worker  entry backend target which collects the actual entry implementation to avoid
125*61c4878aSAndroid Build Coastguard Worker  circular dependencies due to the state definition in the entry backend target.
126*61c4878aSAndroid Build Coastguard Worker* The entry backend should implement the ``pw_cpu_exception_Entry()`` function
127*61c4878aSAndroid Build Coastguard Worker  that will call ``pw_cpu_exception_HandleException()`` after performing any
128*61c4878aSAndroid Build Coastguard Worker  necessary actions prior to handing control to the application's exception
129*61c4878aSAndroid Build Coastguard Worker  handler (e.g. capturing necessary CPU state).
130*61c4878aSAndroid Build Coastguard Worker* If an application's exception handler backend modifies the captured CPU state,
131*61c4878aSAndroid Build Coastguard Worker  the state should be treated as though it were the original state of the CPU
132*61c4878aSAndroid Build Coastguard Worker  when the exception occurred. The backend may need to manually restore some of
133*61c4878aSAndroid Build Coastguard Worker  the modified state to ensure this on exception handler return.
134*61c4878aSAndroid Build Coastguard Worker
135*61c4878aSAndroid Build Coastguard Worker-------------
136*61c4878aSAndroid Build Coastguard WorkerCompatibility
137*61c4878aSAndroid Build Coastguard Worker-------------
138*61c4878aSAndroid Build Coastguard WorkerMost of the pw_cpu_exception module is C-compatible. The exception to this is
139*61c4878aSAndroid Build Coastguard Workerthe "support" facade and library, which requires C++.
140*61c4878aSAndroid Build Coastguard Worker
141*61c4878aSAndroid Build Coastguard Worker------------
142*61c4878aSAndroid Build Coastguard WorkerDependencies
143*61c4878aSAndroid Build Coastguard Worker------------
144*61c4878aSAndroid Build Coastguard Worker- :ref:`module-pw_span`
145*61c4878aSAndroid Build Coastguard Worker- :ref:`module-pw_preprocessor`
146*61c4878aSAndroid Build Coastguard Worker
147*61c4878aSAndroid Build Coastguard Worker.. toctree::
148*61c4878aSAndroid Build Coastguard Worker   :hidden:
149*61c4878aSAndroid Build Coastguard Worker   :maxdepth: 1
150*61c4878aSAndroid Build Coastguard Worker
151*61c4878aSAndroid Build Coastguard Worker   Backends <backends>
152