LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/kernel - fatal.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 0.0 % 21 0
Test Date: 2026-11-06 14:43:19 Functions: 0.0 % 3 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0.0 % 8 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2019 Intel Corporation.
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : 
       7                 :             : #include <zephyr/kernel.h>
       8                 :             : 
       9                 :             : #include <kernel_internal.h>
      10                 :             : #include <zephyr/sys/__assert.h>
      11                 :             : #include <zephyr/arch/cpu.h>
      12                 :             : #include <zephyr/arch/exception.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>
      17                 :             : #include <zephyr/sys/reboot.h>
      18                 :             : 
      19                 :             : LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
      20                 :             : 
      21                 :             : /* LCOV_EXCL_START */
      22                 :             : FUNC_NORETURN __weak void arch_system_halt(unsigned int reason)
      23                 :             : {
      24                 :             :         ARG_UNUSED(reason);
      25                 :             : 
      26                 :             :         /* TODO: What's the best way to totally halt the system if SMP
      27                 :             :          * is enabled?
      28                 :             :          */
      29                 :             : 
      30                 :             :         (void)arch_irq_lock();
      31                 :             :         for (;;) {
      32                 :             :                 /* Spin endlessly */
      33                 :             :         }
      34                 :             : }
      35                 :             : /* LCOV_EXCL_STOP */
      36                 :             : 
      37                 :             : /* LCOV_EXCL_START */
      38                 :             : __weak void k_sys_fatal_error_handler(unsigned int reason,
      39                 :             :                                       const struct arch_esf *esf)
      40                 :             : {
      41                 :             :         ARG_UNUSED(esf);
      42                 :             :         LOG_PANIC();
      43                 :             : 
      44                 :             : #if CONFIG_RESET_ON_FATAL_ERROR
      45                 :             :         EXCEPTION_DUMP("Resetting system");
      46                 :             :         sys_reboot(SYS_REBOOT_WARM);
      47                 :             : #else
      48                 :             :         EXCEPTION_DUMP("Halting system");
      49                 :             :         arch_system_halt(reason);
      50                 :             : #endif /* CONFIG_RESET_ON_FATAL_ERROR */
      51                 :             : 
      52                 :             :         CODE_UNREACHABLE;
      53                 :             : }
      54                 :             : /* LCOV_EXCL_STOP */
      55                 :             : 
      56                 :           0 : static const char *thread_name_get(struct k_thread *thread)
      57                 :             : {
      58         [ #  # ]:           0 :         const char *thread_name = (thread != NULL) ? k_thread_name_get(thread) : NULL;
      59                 :             : 
      60   [ #  #  #  # ]:           0 :         if ((thread_name == NULL) || (thread_name[0] == '\0')) {
      61                 :             :                 thread_name = "unknown";
      62                 :             :         }
      63                 :             : 
      64                 :           0 :         return thread_name;
      65                 :             : }
      66                 :             : 
      67                 :           0 : static const char *reason_to_str(unsigned int reason)
      68                 :             : {
      69         [ #  # ]:           0 :         switch (reason) {
      70                 :             :         case K_ERR_CPU_EXCEPTION:
      71                 :             :                 return "CPU exception";
      72                 :             :         case K_ERR_SPURIOUS_IRQ:
      73                 :             :                 return "Unhandled interrupt";
      74                 :             :         case K_ERR_STACK_CHK_FAIL:
      75                 :             :                 return "Stack overflow";
      76                 :             :         case K_ERR_KERNEL_OOPS:
      77                 :             :                 return "Kernel oops";
      78                 :             :         case K_ERR_KERNEL_PANIC:
      79                 :             :                 return "Kernel panic";
      80                 :             :         default:
      81                 :             :                 return "Unknown error";
      82                 :             :         }
      83                 :             : }
      84                 :             : 
      85                 :             : /* LCOV_EXCL_START */
      86                 :             : FUNC_NORETURN void k_fatal_halt(unsigned int reason)
      87                 :             : {
      88                 :             :         arch_system_halt(reason);
      89                 :             : }
      90                 :             : /* LCOV_EXCL_STOP */
      91                 :             : 
      92                 :           0 : void z_fatal_error(unsigned int reason, const struct arch_esf *esf)
      93                 :             : {
      94                 :             :         /* We can't allow this code to be preempted, but don't need to
      95                 :             :          * synchronize between CPUs, so an arch-layer lock is
      96                 :             :          * appropriate.
      97                 :             :          */
      98                 :           0 :         unsigned int key = arch_irq_lock();
      99                 :           0 :         struct k_thread *thread = IS_ENABLED(CONFIG_MULTITHREADING) ?
     100                 :             :                         _current : NULL;
     101                 :             : 
     102                 :             :         /* The printk lock is not trustworthy from here on: this context may
     103                 :             :          * have failed while holding it, or a CPU that is never coming back
     104                 :             :          * may own it. Give up on serialized output rather than on output.
     105                 :             :          */
     106                 :           0 :         printk_panic();
     107                 :             : 
     108                 :             :         /* twister looks for the "ZEPHYR FATAL ERROR" string, don't
     109                 :             :          * change it without also updating twister
     110                 :             :          */
     111                 :           0 :         EXCEPTION_DUMP(">>> ZEPHYR FATAL ERROR %d: %s on CPU %d", reason, reason_to_str(reason),
     112                 :             :                        _current_cpu->id);
     113                 :             : 
     114                 :             :         /* FIXME: This doesn't seem to work as expected on all arches.
     115                 :             :          * Need a reliable way to determine whether the fault happened when
     116                 :             :          * an IRQ or exception was being handled, or thread context.
     117                 :             :          *
     118                 :             :          * See #17656
     119                 :             :          */
     120                 :             : #if defined(CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION)
     121                 :             :         if ((esf != NULL) && arch_is_in_nested_exception(esf)) {
     122                 :             :                 EXCEPTION_DUMP("Fault during interrupt handling");
     123                 :             :         }
     124                 :             : #endif /* CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION */
     125                 :             : 
     126                 :           0 :         if (IS_ENABLED(CONFIG_MULTITHREADING)) {
     127                 :           0 :                 EXCEPTION_DUMP("Current thread: %p (%s)", thread, thread_name_get(thread));
     128                 :             :         }
     129                 :             : 
     130                 :             :         /*
     131                 :             :          * Must use preprocessor guard, not if (IS_ENABLED(...)): the arch hook
     132                 :             :          * prototypes exist only when CONFIG_DEBUG_COREDUMP_FATAL_UNLOCK_IRQS is
     133                 :             :          * set (kernel_arch_interface.h). IS_ENABLED still parses both branches,
     134                 :             :          * which breaks builds when the option is off (implicit declaration).
     135                 :             :          */
     136                 :             : #ifdef CONFIG_DEBUG_COREDUMP_FATAL_UNLOCK_IRQS
     137                 :             :         unsigned int coredump_irq_cookie;
     138                 :             : 
     139                 :             :         arch_coredump_fatal_irq_unlock(key, &coredump_irq_cookie);
     140                 :             :         coredump(reason, esf, thread);
     141                 :             :         key = arch_coredump_fatal_irq_lock(&coredump_irq_cookie);
     142                 :             : #else
     143                 :           0 :         coredump(reason, esf, thread);
     144                 :             : #endif
     145                 :             : 
     146                 :           0 :         k_sys_fatal_error_handler(reason, esf);
     147                 :             : 
     148                 :             :         /* If the system fatal error handler returns, then kill the faulting
     149                 :             :          * thread; a policy decision was made not to hang the system.
     150                 :             :          *
     151                 :             :          * Policy for fatal errors in ISRs: unconditionally panic.
     152                 :             :          *
     153                 :             :          * There is one exception to this policy: a stack sentinel
     154                 :             :          * check may be performed (on behalf of the current thread)
     155                 :             :          * during ISR exit, but in this case the thread should be
     156                 :             :          * aborted.
     157                 :             :          *
     158                 :             :          * Note that k_thread_abort() returns on some architectures but
     159                 :             :          * not others; e.g. on ARC, x86_64, Xtensa with ASM2, ARM
     160                 :             :          */
     161                 :           0 :         if (!IS_ENABLED(CONFIG_TEST)) {
     162                 :             :                 __ASSERT(reason != K_ERR_KERNEL_PANIC,
     163                 :             :                          "Attempted to recover from a kernel panic condition");
     164                 :             :                 /* FIXME: #17656 */
     165                 :             : #if defined(CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION)
     166                 :             :                 if ((esf != NULL) && arch_is_in_nested_exception(esf)) {
     167                 :             : #if defined(CONFIG_STACK_SENTINEL)
     168                 :             :                         if (reason != K_ERR_STACK_CHK_FAIL) {
     169                 :             :                                 __ASSERT(0,
     170                 :             :                                  "Attempted to recover from a fatal error in ISR");
     171                 :             :                          }
     172                 :             : #endif /* CONFIG_STACK_SENTINEL */
     173                 :             :                 }
     174                 :             : #endif /* CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION */
     175                 :             :         } else {
     176                 :             :                 /* Test mode */
     177                 :             : #if defined(CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION)
     178                 :             :                 if ((esf != NULL) && arch_is_in_nested_exception(esf)) {
     179                 :             :                         /* Abort the thread only on STACK Sentinel check fail. */
     180                 :             : #if defined(CONFIG_STACK_SENTINEL)
     181                 :             :                         if (reason != K_ERR_STACK_CHK_FAIL) {
     182                 :             :                                 arch_irq_unlock(key);
     183                 :             :                                 return;
     184                 :             :                         }
     185                 :             : #else
     186                 :             :                         arch_irq_unlock(key);
     187                 :             :                         return;
     188                 :             : #endif /* CONFIG_STACK_SENTINEL */
     189                 :             :                 } else {
     190                 :             :                         /* Abort the thread only if the fault is not due to
     191                 :             :                          * a spurious ISR handler triggered.
     192                 :             :                          */
     193                 :             :                         if (reason == K_ERR_SPURIOUS_IRQ) {
     194                 :             :                                 arch_irq_unlock(key);
     195                 :             :                                 return;
     196                 :             :                         }
     197                 :             :                 }
     198                 :             : #endif /*CONFIG_ARCH_HAS_NESTED_EXCEPTION_DETECTION */
     199                 :           0 :         }
     200                 :             : 
     201                 :           0 :         arch_irq_unlock(key);
     202                 :             : 
     203                 :           0 :         if (IS_ENABLED(CONFIG_MULTITHREADING)) {
     204                 :           0 :                 k_thread_abort(thread);
     205                 :             :         }
     206                 :           0 : }
        

Generated by: LCOV version 2.0-1