xref: /aosp_15_r20/external/pigweed/pw_cpu_exception/public/pw_cpu_exception/handler.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include "pw_cpu_exception/state.h"
17 #include "pw_preprocessor/compiler.h"
18 #include "pw_preprocessor/util.h"
19 
20 PW_EXTERN_C_START
21 
22 // By default, the exception entry function will terminate by handing execution
23 // over to pw_cpu_exception_DefaultHandler(). This can be used to override the
24 // current handler. This allows runtime insertion of an exception handler which
25 // may also be helpful for loading a bootloader exception handler by default
26 // that an application overrides.
27 void pw_cpu_exception_SetHandler(void (*handler)(pw_cpu_exception_State*));
28 
29 // Set the exception handler to point to pw_cpu_exception_DefaultHandler().
30 void pw_cpu_exception_RestoreDefaultHandler(void);
31 
32 // Application-defined recoverable CPU exception handler.
33 //
34 // Applications must define this function; it is not defined by the exception
35 // entry backend. After CPU state is captured by the cpu exception entry
36 // backend, this function is called. Applications can then choose to either
37 // gracefully handle the exception and return, or decide the exception cannot be
38 // handled and abort normal execution (e.g. reset).
39 //
40 // Examples of what applications could do in the handler: gracefully recover
41 // (e.g. enabling a floating point unit after triggering an exception executing
42 // a floating point instruction), reset the device, or wait for a debugger to
43 // attach.
44 //
45 // See the cpu_exception module documentation for more details.
46 PW_USED void pw_cpu_exception_DefaultHandler(pw_cpu_exception_State* state);
47 
48 // This is the underlying function the CPU exception entry backend should call.
49 // This calls the currently set handler.
50 void pw_cpu_exception_HandleException(void* cpu_state);
51 
52 PW_EXTERN_C_END
53