|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Intel Corporation |
| 3 | + * Copyright (c) 2017 Oticon A/S |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <kernel.h> |
| 9 | +#include <arch/cpu.h> |
| 10 | +#include <kernel_structs.h> |
| 11 | +#include <misc/printk.h> |
| 12 | +#include <inttypes.h> |
| 13 | +#include "posix_soc_if.h" |
| 14 | + |
| 15 | +const NANO_ESF _default_esf = { |
| 16 | + 0xdeadbaad |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * |
| 21 | + * @brief Kernel fatal error handler |
| 22 | + * |
| 23 | + * This routine is called when a fatal error condition is detected |
| 24 | + * |
| 25 | + * The caller is expected to always provide a usable ESF. In the event that the |
| 26 | + * fatal error does not have a hardware generated ESF, the caller should either |
| 27 | + * create its own or call _Fault instead. |
| 28 | + * |
| 29 | + * @param reason the reason that the handler was called |
| 30 | + * @param pEsf pointer to the exception stack frame |
| 31 | + * |
| 32 | + * @return This function does not return. |
| 33 | + */ |
| 34 | +FUNC_NORETURN void _NanoFatalErrorHandler(unsigned int reason, |
| 35 | + const NANO_ESF *esf) |
| 36 | +{ |
| 37 | +#ifdef CONFIG_PRINTK |
| 38 | + switch (reason) { |
| 39 | + case _NANO_ERR_CPU_EXCEPTION: |
| 40 | + case _NANO_ERR_SPURIOUS_INT: |
| 41 | + break; |
| 42 | + |
| 43 | + case _NANO_ERR_INVALID_TASK_EXIT: |
| 44 | + printk("***** Invalid Exit Software Error! *****\n"); |
| 45 | + break; |
| 46 | + |
| 47 | + |
| 48 | + case _NANO_ERR_ALLOCATION_FAIL: |
| 49 | + printk("**** Kernel Allocation Failure! ****\n"); |
| 50 | + break; |
| 51 | + |
| 52 | + case _NANO_ERR_KERNEL_OOPS: |
| 53 | + printk("***** Kernel OOPS! *****\n"); |
| 54 | + break; |
| 55 | + |
| 56 | + case _NANO_ERR_KERNEL_PANIC: |
| 57 | + printk("***** Kernel Panic! *****\n"); |
| 58 | + break; |
| 59 | + |
| 60 | +#ifdef CONFIG_STACK_SENTINEL |
| 61 | + case _NANO_ERR_STACK_CHK_FAIL: |
| 62 | + printk("***** Stack overflow *****\n"); |
| 63 | + break; |
| 64 | +#endif |
| 65 | + default: |
| 66 | + printk("**** Unknown Fatal Error %u! ****\n", reason); |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | +#endif |
| 71 | + |
| 72 | + void _SysFatalErrorHandler(unsigned int reason, |
| 73 | + const NANO_ESF *pEsf); |
| 74 | + _SysFatalErrorHandler(reason, esf); |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +/** |
| 79 | + * |
| 80 | + * @brief Fatal error handler |
| 81 | + * |
| 82 | + * This routine implements the corrective action to be taken when the system |
| 83 | + * detects a fatal error. |
| 84 | + * |
| 85 | + * This sample implementation attempts to abort the current thread and allow |
| 86 | + * the system to continue executing, which may permit the system to continue |
| 87 | + * functioning with degraded capabilities. |
| 88 | + * |
| 89 | + * System designers may wish to enhance or substitute this sample |
| 90 | + * implementation to take other actions, such as logging error (or debug) |
| 91 | + * information to a persistent repository and/or rebooting the system. |
| 92 | + * |
| 93 | + * @param reason the fatal error reason |
| 94 | + * @param pEsf pointer to exception stack frame |
| 95 | + * |
| 96 | + * @return N/A |
| 97 | + */ |
| 98 | +FUNC_NORETURN __weak void _SysFatalErrorHandler(unsigned int reason, |
| 99 | + const NANO_ESF *pEsf) |
| 100 | +{ |
| 101 | + ARG_UNUSED(pEsf); |
| 102 | + |
| 103 | +#ifdef CONFIG_STACK_SENTINEL |
| 104 | + if (reason == _NANO_ERR_STACK_CHK_FAIL) { |
| 105 | + goto hang_system; |
| 106 | + } |
| 107 | +#endif |
| 108 | + if (reason == _NANO_ERR_KERNEL_PANIC) { |
| 109 | + goto hang_system; |
| 110 | + } |
| 111 | + if (k_is_in_isr() || _is_thread_essential()) { |
| 112 | + posix_print_error_and_exit( |
| 113 | + "Fatal fault in %s! Stopping...\n", |
| 114 | + k_is_in_isr() ? "ISR" : "essential thread"); |
| 115 | + } |
| 116 | + printk("Fatal fault in thread %p! Aborting.\n", _current); |
| 117 | + k_thread_abort(_current); |
| 118 | + |
| 119 | +hang_system: |
| 120 | + |
| 121 | + posix_print_error_and_exit( |
| 122 | + "Stopped in _SysFatalErrorHandler()\n"); |
| 123 | + CODE_UNREACHABLE; |
| 124 | +} |
0 commit comments