Armando Montanez | 5104cd6 | 2019-12-10 14:36:43 -0800 | [diff] [blame] | 1 | .. _chapter-pw-cpu-exception: |
| 2 | |
| 3 | .. default-domain:: cpp |
| 4 | |
| 5 | .. highlight:: cpp |
| 6 | |
| 7 | ---------------- |
| 8 | pw_cpu_exception |
| 9 | ---------------- |
| 10 | Pigweed's exception module provides a consistent interface for entering an |
| 11 | application's CPU exception handler. While the actual exception handling |
| 12 | behavior is left to an application to implement, this module deals with any |
| 13 | architecture-specific actions required before calling the application exception |
| 14 | handler. More specifically, the exception module collects CPU state that may |
| 15 | otherwise be clobbered by an application's exception handler. |
| 16 | |
| 17 | Setup |
| 18 | ===== |
| 19 | An application using this module **must** connect ``pw_CpuExceptionEntry()`` to |
| 20 | the platform's CPU exception handler interrupt so ``pw_CpuExceptionEntry()`` is |
| 21 | called immediately upon a CPU exception. For specifics on how this may be done, |
| 22 | see the backend documentation for your architecture. |
| 23 | |
| 24 | Applications must also provide an implementation for |
| 25 | ``pw::cpu_exception::HandleCpuException()``. The behavior of this functions |
| 26 | is 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 | |
| 35 | Module Usage |
| 36 | ============ |
| 37 | Basic usage of this module entails applications supplying a definition for |
| 38 | ``pw::cpu_exception::HandleCpuException()``. ``HandleCpuException()`` should |
| 39 | contain any logic to determine if a exception can be recovered from, as well |
| 40 | as necessary actions to properly recover. If the device cannot recover from the |
| 41 | exception, the function should **not** return. |
| 42 | |
| 43 | When writing an exception handler, prefer to use the functions provided by this |
| 44 | interface rather than relying on the backend implementation of ``CpuState``. |
| 45 | This allows better code portability as it helps prevent an application fault |
| 46 | handler from being tied to a single backend. |
| 47 | |
| 48 | For example; when logging or dumping CPU state, prefer ``ToString()`` or |
| 49 | ``RawFaultingCpuState()`` over directly accessing members of a ``CpuState`` |
| 50 | object. |
| 51 | |
| 52 | Some exception handling behavior may require architecture-specific CPU state to |
| 53 | attempt to correct a fault. In this situation, the application's exception |
| 54 | handler will be tied to the backend implementation of the CPU exception module. |
| 55 | |
| 56 | Dependencies |
| 57 | ============ |
| 58 | * pw_span |
| 59 | * pw_preprocessor |
| 60 | |
| 61 | Backend Expectations |
| 62 | ==================== |
| 63 | CPU exception backends do not provide an exception handler, but instead provide |
| 64 | mechanisms to capture CPU state for use by an application's exception handler, |
| 65 | and 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). |