Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 Intel Corporation. |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 7 | #include <zephyr/kernel.h> |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 8 | |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 9 | #include <kernel_internal.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 10 | #include <zephyr/kernel_structs.h> |
| 11 | #include <zephyr/sys/__assert.h> |
| 12 | #include <zephyr/arch/cpu.h> |
| 13 | #include <zephyr/logging/log_ctrl.h> |
| 14 | #include <zephyr/logging/log.h> |
| 15 | #include <zephyr/fatal.h> |
| 16 | #include <zephyr/debug/coredump.h> |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 17 | |
Krzysztof Chruscinski | 3ed8083 | 2020-11-26 19:32:34 +0100 | [diff] [blame] | 18 | LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL); |
Andrew Boie | 8a9e8e0 | 2019-07-15 22:03:56 -0700 | [diff] [blame] | 19 | |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 20 | /* LCOV_EXCL_START */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 21 | FUNC_NORETURN __weak void arch_system_halt(unsigned int reason) |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 22 | { |
| 23 | ARG_UNUSED(reason); |
| 24 | |
| 25 | /* TODO: What's the best way to totally halt the system if SMP |
| 26 | * is enabled? |
| 27 | */ |
| 28 | |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 29 | (void)arch_irq_lock(); |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 30 | for (;;) { |
Andrew Boie | 90e6536 | 2019-09-06 07:57:41 -0700 | [diff] [blame] | 31 | /* Spin endlessly */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | /* LCOV_EXCL_STOP */ |
| 35 | |
| 36 | /* LCOV_EXCL_START */ |
| 37 | __weak void k_sys_fatal_error_handler(unsigned int reason, |
Andrew Boie | 96571a8 | 2019-07-16 15:21:19 -0700 | [diff] [blame] | 38 | const z_arch_esf_t *esf) |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 39 | { |
| 40 | ARG_UNUSED(esf); |
| 41 | |
| 42 | LOG_PANIC(); |
Andrew Boie | a470ba1 | 2019-08-07 09:06:23 +0200 | [diff] [blame] | 43 | LOG_ERR("Halting system"); |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 44 | arch_system_halt(reason); |
Anas Nashif | 05315ea | 2023-12-19 08:37:58 -0500 | [diff] [blame] | 45 | CODE_UNREACHABLE; |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 46 | } |
| 47 | /* LCOV_EXCL_STOP */ |
| 48 | |
| 49 | static const char *thread_name_get(struct k_thread *thread) |
| 50 | { |
Anas Nashif | 3f4f3f6 | 2021-03-29 17:13:47 -0400 | [diff] [blame] | 51 | const char *thread_name = (thread != NULL) ? k_thread_name_get(thread) : NULL; |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 52 | |
Anas Nashif | 3f4f3f6 | 2021-03-29 17:13:47 -0400 | [diff] [blame] | 53 | if ((thread_name == NULL) || (thread_name[0] == '\0')) { |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 54 | thread_name = "unknown"; |
| 55 | } |
| 56 | |
| 57 | return thread_name; |
| 58 | } |
| 59 | |
| 60 | static const char *reason_to_str(unsigned int reason) |
| 61 | { |
| 62 | switch (reason) { |
| 63 | case K_ERR_CPU_EXCEPTION: |
| 64 | return "CPU exception"; |
| 65 | case K_ERR_SPURIOUS_IRQ: |
| 66 | return "Unhandled interrupt"; |
| 67 | case K_ERR_STACK_CHK_FAIL: |
| 68 | return "Stack overflow"; |
| 69 | case K_ERR_KERNEL_OOPS: |
| 70 | return "Kernel oops"; |
| 71 | case K_ERR_KERNEL_PANIC: |
| 72 | return "Kernel panic"; |
| 73 | default: |
| 74 | return "Unknown error"; |
| 75 | } |
| 76 | } |
| 77 | |
Peng Su | 1084f48 | 2019-08-22 17:12:26 +0800 | [diff] [blame] | 78 | /* LCOV_EXCL_START */ |
Andrew Boie | 00bf76e | 2019-08-06 12:10:49 -0700 | [diff] [blame] | 79 | FUNC_NORETURN void k_fatal_halt(unsigned int reason) |
| 80 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 81 | arch_system_halt(reason); |
Andrew Boie | 00bf76e | 2019-08-06 12:10:49 -0700 | [diff] [blame] | 82 | } |
Peng Su | 1084f48 | 2019-08-22 17:12:26 +0800 | [diff] [blame] | 83 | /* LCOV_EXCL_STOP */ |
| 84 | |
Andrew Boie | 96571a8 | 2019-07-16 15:21:19 -0700 | [diff] [blame] | 85 | void z_fatal_error(unsigned int reason, const z_arch_esf_t *esf) |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 86 | { |
Andy Ross | 8153144 | 2020-02-06 12:58:53 -0800 | [diff] [blame] | 87 | /* We can't allow this code to be preempted, but don't need to |
| 88 | * synchronize between CPUs, so an arch-layer lock is |
| 89 | * appropriate. |
| 90 | */ |
| 91 | unsigned int key = arch_irq_lock(); |
Krzysztof Chruscinski | 1ba23ca | 2021-04-14 13:38:01 +0200 | [diff] [blame] | 92 | struct k_thread *thread = IS_ENABLED(CONFIG_MULTITHREADING) ? |
Flavio Ceolin | 711c171 | 2023-09-28 16:17:06 -0700 | [diff] [blame] | 93 | _current : NULL; |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 94 | |
Anas Nashif | 3d33d76 | 2020-12-07 11:56:32 -0500 | [diff] [blame] | 95 | /* twister looks for the "ZEPHYR FATAL ERROR" string, don't |
| 96 | * change it without also updating twister |
Andrew Boie | 81ef42d | 2019-07-16 15:29:46 -0700 | [diff] [blame] | 97 | */ |
Andrew Boie | 91468b5 | 2019-11-06 13:03:38 -0800 | [diff] [blame] | 98 | LOG_ERR(">>> ZEPHYR FATAL ERROR %d: %s on CPU %d", reason, |
Flavio Ceolin | 121d051 | 2023-09-28 16:21:06 -0700 | [diff] [blame] | 99 | reason_to_str(reason), _current_cpu->id); |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 100 | |
| 101 | /* FIXME: This doesn't seem to work as expected on all arches. |
| 102 | * Need a reliable way to determine whether the fault happened when |
| 103 | * an IRQ or exception was being handled, or thread context. |
| 104 | * |
| 105 | * See #17656 |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 106 | */ |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 107 | #if defined(CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION) |
Ioannis Glaropoulos | 49fb5d0 | 2020-03-09 20:36:21 +0100 | [diff] [blame] | 108 | if ((esf != NULL) && arch_is_in_nested_exception(esf)) { |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 109 | LOG_ERR("Fault during interrupt handling\n"); |
| 110 | } |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 111 | #endif /* CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 112 | |
Andrew Boie | a470ba1 | 2019-08-07 09:06:23 +0200 | [diff] [blame] | 113 | LOG_ERR("Current thread: %p (%s)", thread, |
Krzysztof Chruscinski | 041f0e5 | 2022-06-20 07:43:37 +0200 | [diff] [blame] | 114 | thread_name_get(thread)); |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 115 | |
Daniel Leung | d3218ca | 2021-01-21 10:11:44 -0800 | [diff] [blame] | 116 | coredump(reason, esf, thread); |
Daniel Leung | 49206a8 | 2020-08-07 10:47:37 -0700 | [diff] [blame] | 117 | |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 118 | k_sys_fatal_error_handler(reason, esf); |
| 119 | |
| 120 | /* If the system fatal error handler returns, then kill the faulting |
| 121 | * thread; a policy decision was made not to hang the system. |
| 122 | * |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 123 | * Policy for fatal errors in ISRs: unconditionally panic. |
| 124 | * |
| 125 | * There is one exception to this policy: a stack sentinel |
| 126 | * check may be performed (on behalf of the current thread) |
| 127 | * during ISR exit, but in this case the thread should be |
| 128 | * aborted. |
| 129 | * |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 130 | * Note that k_thread_abort() returns on some architectures but |
| 131 | * not others; e.g. on ARC, x86_64, Xtensa with ASM2, ARM |
| 132 | */ |
| 133 | if (!IS_ENABLED(CONFIG_TEST)) { |
| 134 | __ASSERT(reason != K_ERR_KERNEL_PANIC, |
| 135 | "Attempted to recover from a kernel panic condition"); |
| 136 | /* FIXME: #17656 */ |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 137 | #if defined(CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION) |
Ioannis Glaropoulos | 49fb5d0 | 2020-03-09 20:36:21 +0100 | [diff] [blame] | 138 | if ((esf != NULL) && arch_is_in_nested_exception(esf)) { |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 139 | #if defined(CONFIG_STACK_SENTINEL) |
| 140 | if (reason != K_ERR_STACK_CHK_FAIL) { |
| 141 | __ASSERT(0, |
| 142 | "Attempted to recover from a fatal error in ISR"); |
| 143 | } |
| 144 | #endif /* CONFIG_STACK_SENTINEL */ |
| 145 | } |
| 146 | #endif /* CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION */ |
| 147 | } else { |
| 148 | /* Test mode */ |
| 149 | #if defined(CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION) |
Ioannis Glaropoulos | 1c56f87 | 2020-03-09 22:35:50 +0100 | [diff] [blame] | 150 | if ((esf != NULL) && arch_is_in_nested_exception(esf)) { |
| 151 | /* Abort the thread only on STACK Sentinel check fail. */ |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 152 | #if defined(CONFIG_STACK_SENTINEL) |
Ioannis Glaropoulos | 1c56f87 | 2020-03-09 22:35:50 +0100 | [diff] [blame] | 153 | if (reason != K_ERR_STACK_CHK_FAIL) { |
Ioannis Glaropoulos | 3a3364e | 2020-02-14 15:32:53 +0100 | [diff] [blame] | 154 | arch_irq_unlock(key); |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 155 | return; |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 156 | } |
Ioannis Glaropoulos | 1c56f87 | 2020-03-09 22:35:50 +0100 | [diff] [blame] | 157 | #else |
| 158 | arch_irq_unlock(key); |
| 159 | return; |
| 160 | #endif /* CONFIG_STACK_SENTINEL */ |
| 161 | } else { |
| 162 | /* Abort the thread only if the fault is not due to |
| 163 | * a spurious ISR handler triggered. |
| 164 | */ |
| 165 | if (reason == K_ERR_SPURIOUS_IRQ) { |
| 166 | arch_irq_unlock(key); |
| 167 | return; |
| 168 | } |
| 169 | } |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 170 | #endif /*CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 171 | } |
Ioannis Glaropoulos | df02923 | 2019-10-07 11:24:36 +0200 | [diff] [blame] | 172 | |
Andy Ross | 8153144 | 2020-02-06 12:58:53 -0800 | [diff] [blame] | 173 | arch_irq_unlock(key); |
Krzysztof Chruscinski | 1ba23ca | 2021-04-14 13:38:01 +0200 | [diff] [blame] | 174 | |
| 175 | if (IS_ENABLED(CONFIG_MULTITHREADING)) { |
| 176 | k_thread_abort(thread); |
| 177 | } |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 178 | } |