|
| 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 | +/*Started as a copy of the nios2 one with some prunning*/ |
| 9 | + |
| 10 | +#include <kernel.h> |
| 11 | +#include <arch/cpu.h> |
| 12 | +#include <kernel_structs.h> |
| 13 | +#include <misc/printk.h> |
| 14 | +#include <inttypes.h> |
| 15 | + |
| 16 | +const NANO_ESF _default_esf = { |
| 17 | + 0xdeadbaad |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * |
| 22 | + * @brief Kernel fatal error handler |
| 23 | + * |
| 24 | + * This routine is called when a fatal error condition is detected by either |
| 25 | + * hardware or software. |
| 26 | + * |
| 27 | + * The caller is expected to always provide a usable ESF. In the event that the |
| 28 | + * fatal error does not have a hardware generated ESF, the caller should either |
| 29 | + * create its own or call _Fault instead. |
| 30 | + * |
| 31 | + * @param reason the reason that the handler was called |
| 32 | + * @param pEsf pointer to the exception stack frame |
| 33 | + * |
| 34 | + * @return This function does not return. |
| 35 | + */ |
| 36 | +FUNC_NORETURN void _NanoFatalErrorHandler(unsigned int reason, |
| 37 | + const NANO_ESF *esf) |
| 38 | +{ |
| 39 | +#ifdef CONFIG_PRINTK |
| 40 | + switch (reason) { |
| 41 | + case _NANO_ERR_CPU_EXCEPTION: |
| 42 | + case _NANO_ERR_SPURIOUS_INT: |
| 43 | + break; |
| 44 | + |
| 45 | + case _NANO_ERR_INVALID_TASK_EXIT: |
| 46 | + printk("***** Invalid Exit Software Error! *****\n"); |
| 47 | + break; |
| 48 | + |
| 49 | + |
| 50 | + case _NANO_ERR_ALLOCATION_FAIL: |
| 51 | + printk("**** Kernel Allocation Failure! ****\n"); |
| 52 | + break; |
| 53 | + |
| 54 | + case _NANO_ERR_KERNEL_OOPS: |
| 55 | + printk("***** Kernel OOPS! *****\n"); |
| 56 | + break; |
| 57 | + |
| 58 | + case _NANO_ERR_KERNEL_PANIC: |
| 59 | + printk("***** Kernel Panic! *****\n"); |
| 60 | + break; |
| 61 | + |
| 62 | +#ifdef CONFIG_STACK_SENTINEL |
| 63 | + case _NANO_ERR_STACK_CHK_FAIL: |
| 64 | + printk("***** Stack overflow *****\n"); |
| 65 | + break; |
| 66 | +#endif |
| 67 | + default: |
| 68 | + printk("**** Unknown Fatal Error %u! ****\n", reason); |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | +#endif |
| 73 | + |
| 74 | + void _SysFatalErrorHandler(unsigned int reason, |
| 75 | + const NANO_ESF *pEsf); |
| 76 | + _SysFatalErrorHandler(reason, esf); |
| 77 | +} |
| 78 | + |
| 79 | +#if defined(CONFIG_EXTRA_EXCEPTION_INFO) && defined(CONFIG_PRINTK) \ |
| 80 | + && defined(ALT_CPU_HAS_EXTRA_EXCEPTION_INFO) |
| 81 | +static char *cause_str(u32_t cause_code) |
| 82 | +{ |
| 83 | + switch (cause_code) { |
| 84 | + case 0: |
| 85 | + return "reset"; |
| 86 | + case 1: |
| 87 | + return "processor-only reset request"; |
| 88 | + case 2: |
| 89 | + return "interrupt"; |
| 90 | + case 3: |
| 91 | + return "trap"; |
| 92 | + case 4: |
| 93 | + return "unimplemented instruction"; |
| 94 | + case 5: |
| 95 | + return "illegal instruction"; |
| 96 | + case 6: |
| 97 | + return "misaligned data address"; |
| 98 | + case 7: |
| 99 | + return "misaligned destination address"; |
| 100 | + case 8: |
| 101 | + return "division error"; |
| 102 | + case 9: |
| 103 | + return "supervisor-only instruction address"; |
| 104 | + case 10: |
| 105 | + return "supervisor-only instruction"; |
| 106 | + case 11: |
| 107 | + return "supervisor-only data address"; |
| 108 | + case 12: |
| 109 | + return "TLB miss"; |
| 110 | + case 13: |
| 111 | + return "TLB permission violation (execute)"; |
| 112 | + case 14: |
| 113 | + return "TLB permission violation (read)"; |
| 114 | + case 15: |
| 115 | + return "TLB permission violation (write)"; |
| 116 | + case 16: |
| 117 | + return "MPU region violation (instruction)"; |
| 118 | + case 17: |
| 119 | + return "MPU region violation (data)"; |
| 120 | + case 18: |
| 121 | + return "ECC TLB error"; |
| 122 | + case 19: |
| 123 | + return "ECC fetch error (instruction)"; |
| 124 | + case 20: |
| 125 | + return "ECC register file error"; |
| 126 | + case 21: |
| 127 | + return "ECC data error"; |
| 128 | + case 22: |
| 129 | + return "ECC data cache writeback error"; |
| 130 | + case 23: |
| 131 | + return "bus instruction fetch error"; |
| 132 | + case 24: |
| 133 | + return "bus data region violation"; |
| 134 | + default: |
| 135 | + return "unknown"; |
| 136 | + } |
| 137 | +} |
| 138 | +#endif |
| 139 | + |
| 140 | +FUNC_NORETURN void _Fault(const NANO_ESF *esf) |
| 141 | +{ |
| 142 | +#ifdef CONFIG_PRINTK |
| 143 | + /* Unfortunately, completely unavailable on Nios II/e cores */ |
| 144 | +#ifdef ALT_CPU_HAS_EXTRA_EXCEPTION_INFO |
| 145 | + u32_t exc_reg, badaddr_reg, eccftl; |
| 146 | + enum nios2_exception_cause cause; |
| 147 | + |
| 148 | + exc_reg = _nios2_creg_read(NIOS2_CR_EXCEPTION); |
| 149 | + |
| 150 | + /* Bit 31 indicates potentially fatal ECC error */ |
| 151 | + eccftl = (exc_reg & NIOS2_EXCEPTION_REG_ECCFTL_MASK) != 0; |
| 152 | + |
| 153 | + /* Bits 2-6 contain the cause code */ |
| 154 | + cause = (exc_reg & NIOS2_EXCEPTION_REG_CAUSE_MASK) |
| 155 | + >> NIOS2_EXCEPTION_REG_CAUSE_OFST; |
| 156 | + |
| 157 | + printk("Exception cause: %d ECCFTL: 0x%x\n", cause, eccftl); |
| 158 | +#if CONFIG_EXTRA_EXCEPTION_INFO |
| 159 | + printk("reason: %s\n", cause_str(cause)); |
| 160 | +#endif |
| 161 | + if (BIT(cause) & NIOS2_BADADDR_CAUSE_MASK) { |
| 162 | + badaddr_reg = _nios2_creg_read(NIOS2_CR_BADADDR); |
| 163 | + printk("Badaddr: 0x%x\n", badaddr_reg); |
| 164 | + } |
| 165 | +#endif /* ALT_CPU_HAS_EXTRA_EXCEPTION_INFO */ |
| 166 | +#endif /* CONFIG_PRINTK */ |
| 167 | + |
| 168 | + _NanoFatalErrorHandler(_NANO_ERR_CPU_EXCEPTION, esf); |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +/** |
| 173 | + * |
| 174 | + * @brief Fatal error handler |
| 175 | + * |
| 176 | + * This routine implements the corrective action to be taken when the system |
| 177 | + * detects a fatal error. |
| 178 | + * |
| 179 | + * This sample implementation attempts to abort the current thread and allow |
| 180 | + * the system to continue executing, which may permit the system to continue |
| 181 | + * functioning with degraded capabilities. |
| 182 | + * |
| 183 | + * System designers may wish to enhance or substitute this sample |
| 184 | + * implementation to take other actions, such as logging error (or debug) |
| 185 | + * information to a persistent repository and/or rebooting the system. |
| 186 | + * |
| 187 | + * @param reason the fatal error reason |
| 188 | + * @param pEsf pointer to exception stack frame |
| 189 | + * |
| 190 | + * @return N/A |
| 191 | + */ |
| 192 | +FUNC_NORETURN __weak void _SysFatalErrorHandler(unsigned int reason, |
| 193 | + const NANO_ESF *pEsf) |
| 194 | +{ |
| 195 | + ARG_UNUSED(pEsf); |
| 196 | + |
| 197 | +#if !defined(CONFIG_SIMPLE_FATAL_ERROR_HANDLER) |
| 198 | +#ifdef CONFIG_STACK_SENTINEL |
| 199 | + if (reason == _NANO_ERR_STACK_CHK_FAIL) { |
| 200 | + goto hang_system; |
| 201 | + } |
| 202 | +#endif |
| 203 | + if (reason == _NANO_ERR_KERNEL_PANIC) { |
| 204 | + goto hang_system; |
| 205 | + } |
| 206 | + if (k_is_in_isr() || _is_thread_essential()) { |
| 207 | + printk("Fatal fault in %s! Spinning...\n", |
| 208 | + k_is_in_isr() ? "ISR" : "essential thread"); |
| 209 | + goto hang_system; |
| 210 | + } |
| 211 | + printk("Fatal fault in thread %p! Aborting.\n", _current); |
| 212 | + k_thread_abort(_current); |
| 213 | + |
| 214 | +hang_system: |
| 215 | +#else |
| 216 | + ARG_UNUSED(reason); |
| 217 | +#endif |
| 218 | + |
| 219 | +#ifdef ALT_CPU_HAS_DEBUG_STUB |
| 220 | + _nios2_break(); |
| 221 | +#endif |
| 222 | + for (;;) { |
| 223 | + k_cpu_idle(); |
| 224 | + } |
| 225 | + CODE_UNREACHABLE; |
| 226 | +} |
0 commit comments