blob: 7f14c57d89f959b0ed7356b806ea251e3c4a49df [file] [log] [blame]
Armando Montanez5104cd62019-12-10 14:36:43 -08001.. _chapter-pw-cpu-exception:
2
3.. default-domain:: cpp
4
5.. highlight:: cpp
6
7----------------
8pw_cpu_exception
9----------------
10Pigweed's exception module provides a consistent interface for entering an
11application's CPU exception handler. While the actual exception handling
12behavior is left to an application to implement, this module deals with any
13architecture-specific actions required before calling the application exception
14handler. More specifically, the exception module collects CPU state that may
15otherwise be clobbered by an application's exception handler.
16
17Setup
18=====
19An application using this module **must** connect ``pw_CpuExceptionEntry()`` to
20the platform's CPU exception handler interrupt so ``pw_CpuExceptionEntry()`` is
21called immediately upon a CPU exception. For specifics on how this may be done,
22see the backend documentation for your architecture.
23
24Applications must also provide an implementation for
25``pw::cpu_exception::HandleCpuException()``. The behavior of this functions
26is entirely up to the application/project, but some examples are provided below:
27
28 * Enter an infinite loop so the device can be debugged by JTAG.
29 * Reset the device.
30 * Attempt to handle the exception so execution can continue.
31 * Capture and record additional device state and save to flash for a crash
32 report.
33 * A combination of the above, using logic that fits the needs of your project.
34
35Module Usage
36============
37Basic usage of this module entails applications supplying a definition for
38``pw::cpu_exception::HandleCpuException()``. ``HandleCpuException()`` should
39contain any logic to determine if a exception can be recovered from, as well
40as necessary actions to properly recover. If the device cannot recover from the
41exception, the function should **not** return.
42
43When writing an exception handler, prefer to use the functions provided by this
44interface rather than relying on the backend implementation of ``CpuState``.
45This allows better code portability as it helps prevent an application fault
46handler from being tied to a single backend.
47
48For example; when logging or dumping CPU state, prefer ``ToString()`` or
49``RawFaultingCpuState()`` over directly accessing members of a ``CpuState``
50object.
51
52Some exception handling behavior may require architecture-specific CPU state to
53attempt to correct a fault. In this situation, the application's exception
54handler will be tied to the backend implementation of the CPU exception module.
55
56Dependencies
57============
58 * pw_span
59 * pw_preprocessor
60
61Backend Expectations
62====================
63CPU exception backends do not provide an exception handler, but instead provide
64mechanisms to capture CPU state for use by an application's exception handler,
65and allow recovery from CPU exceptions when possible.
66
67 * A backend should provide a definition for the ``CpuState`` struct that
68 provides suitable means to access and modify any captured CPU state.
69 * If an application's exception handler modifies the captured CPU state, the
70 state should be treated as though it were the original state of the CPU when
71 the exception occurred. The backend may need to manually restore some of the
72 modified state to ensure this on exception handler return.
73 * A backend should implement the ``pw_CpuExceptionEntry()`` function that will
74 call ``HandleCpuException()`` after performing any necessary actions prior
75 to handing control to the application's exception handler (e.g. capturing
76 necessary CPU state).