fatal.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2019 Intel Corporation.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <kernel.h>
  7. #include <kernel_internal.h>
  8. #include <kernel_structs.h>
  9. #include <sys/__assert.h>
  10. #include <arch/cpu.h>
  11. #include <logging/log_ctrl.h>
  12. #include <logging/log.h>
  13. #include <fatal.h>
  14. #include <debug/coredump.h>
  15. #include <soc_pstore.h>
  16. #include <debug/actions_exception.h>
  17. #include <debug/ramdump.h>
  18. LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
  19. /* LCOV_EXCL_START */
  20. FUNC_NORETURN __weak void arch_system_halt(unsigned int reason)
  21. {
  22. ARG_UNUSED(reason);
  23. /* TODO: What's the best way to totally halt the system if SMP
  24. * is enabled?
  25. */
  26. (void)arch_irq_lock();
  27. for (;;) {
  28. /* Spin endlessly */
  29. }
  30. }
  31. /* LCOV_EXCL_STOP */
  32. /* LCOV_EXCL_START */
  33. __weak void k_sys_fatal_error_handler(unsigned int reason,
  34. const z_arch_esf_t *esf)
  35. {
  36. //volatile int flag = true;
  37. ARG_UNUSED(esf);
  38. LOG_PANIC();
  39. LOG_ERR("Halting system");
  40. //jtag_set();
  41. //(void)arch_irq_lock();
  42. //while(flag);
  43. }
  44. /* LCOV_EXCL_STOP */
  45. static const char *thread_name_get(struct k_thread *thread)
  46. {
  47. const char *thread_name = (thread != NULL) ? k_thread_name_get(thread) : NULL;
  48. if ((thread_name == NULL) || (thread_name[0] == '\0')) {
  49. thread_name = "unknown";
  50. }
  51. return thread_name;
  52. }
  53. static const char *reason_to_str(unsigned int reason)
  54. {
  55. switch (reason) {
  56. case K_ERR_CPU_EXCEPTION:
  57. return "CPU exception";
  58. case K_ERR_SPURIOUS_IRQ:
  59. return "Unhandled interrupt";
  60. case K_ERR_STACK_CHK_FAIL:
  61. return "Stack overflow";
  62. case K_ERR_KERNEL_OOPS:
  63. return "Kernel oops";
  64. case K_ERR_KERNEL_PANIC:
  65. return "Kernel panic";
  66. default:
  67. return "Unknown error";
  68. }
  69. }
  70. /* LCOV_EXCL_START */
  71. FUNC_NORETURN void k_fatal_halt(unsigned int reason)
  72. {
  73. arch_system_halt(reason);
  74. }
  75. /* LCOV_EXCL_STOP */
  76. static inline int get_cpu(void)
  77. {
  78. #if defined(CONFIG_SMP)
  79. return arch_curr_cpu()->id;
  80. #else
  81. return 0;
  82. #endif
  83. }
  84. void z_fatal_error(unsigned int reason, const z_arch_esf_t *esf)
  85. {
  86. /* We can't allow this code to be preempted, but don't need to
  87. * synchronize between CPUs, so an arch-layer lock is
  88. * appropriate.
  89. */
  90. unsigned int key = arch_irq_lock();
  91. struct k_thread *thread = IS_ENABLED(CONFIG_MULTITHREADING) ?
  92. k_current_get() : NULL;
  93. /* twister looks for the "ZEPHYR FATAL ERROR" string, don't
  94. * change it without also updating twister
  95. */
  96. LOG_ERR(">>> ZEPHYR FATAL ERROR %d: %s on CPU %d", reason,
  97. reason_to_str(reason), get_cpu());
  98. /* FIXME: This doesn't seem to work as expected on all arches.
  99. * Need a reliable way to determine whether the fault happened when
  100. * an IRQ or exception was being handled, or thread context.
  101. *
  102. * See #17656
  103. */
  104. #if defined(CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION)
  105. if ((esf != NULL) && arch_is_in_nested_exception(esf)) {
  106. LOG_ERR("Fault during interrupt handling\n");
  107. }
  108. #endif
  109. LOG_ERR("Current thread: %p (%s)", thread,
  110. log_strdup(thread_name_get(thread)));
  111. soc_pstore_set(SOC_PSTORE_TAG_SYS_PANIC, 1);
  112. k_sys_fatal_error_handler(reason, esf);
  113. exception_run();
  114. coredump(reason, esf, thread);
  115. ramdump_save(esf, 0);
  116. volatile int flag = true;
  117. (void)arch_irq_lock();
  118. jtag_set();
  119. while(flag);
  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. *
  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. *
  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 */
  137. #if defined(CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION)
  138. if ((esf != NULL) && arch_is_in_nested_exception(esf)) {
  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)
  150. if ((esf != NULL) && arch_is_in_nested_exception(esf)) {
  151. /* Abort the thread only on STACK Sentinel check fail. */
  152. #if defined(CONFIG_STACK_SENTINEL)
  153. if (reason != K_ERR_STACK_CHK_FAIL) {
  154. arch_irq_unlock(key);
  155. return;
  156. }
  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. }
  170. #endif /*CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION */
  171. }
  172. arch_irq_unlock(key);
  173. if (IS_ENABLED(CONFIG_MULTITHREADING)) {
  174. k_thread_abort(thread);
  175. }
  176. }