From e39982618999a62cf48198bf60368a37de088ce2 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Tue, 18 Sep 2018 08:54:44 +0200 Subject: [PATCH 1/8] logging: allow mulitple log_panic calls Ensure that only first log_panic() sends panic signal to backends Signed-off-by: Krzysztof Chruscinski --- subsys/logging/log_core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/subsys/logging/log_core.c b/subsys/logging/log_core.c index 009a90af16297..0bf4517bd612b 100644 --- a/subsys/logging/log_core.c +++ b/subsys/logging/log_core.c @@ -304,6 +304,10 @@ void log_panic(void) { struct log_backend const *backend; + if (panic_mode) { + return; + } + for (int i = 0; i < log_backend_count_get(); i++) { backend = log_backend_get(i); From 5461a027e361f92fec4574283cc9e661ac982882 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Tue, 18 Sep 2018 08:56:31 +0200 Subject: [PATCH 2/8] arch: arm: add log handling to fault handlers Signed-off-by: Krzysztof Chruscinski --- arch/arm/core/fatal.c | 27 +++-- arch/arm/core/fault.c | 135 ++++++++++++------------ arch/arm/core/sys_fatal_error_handler.c | 7 +- 3 files changed, 85 insertions(+), 84 deletions(-) diff --git a/arch/arm/core/fatal.c b/arch/arm/core/fatal.c index 623c257926be2..85d0488681807 100644 --- a/arch/arm/core/fatal.c +++ b/arch/arm/core/fatal.c @@ -14,11 +14,12 @@ #include #include #include - #include #include -#include +#include +#include +LOG_MODULE_REGISTER(fatal); /** * @@ -47,35 +48,37 @@ void _NanoFatalErrorHandler(unsigned int reason, const NANO_ESF *pEsf) { + LOG_PANIC(); + switch (reason) { case _NANO_ERR_HW_EXCEPTION: - printk("***** Hardware exception *****\n"); + LOG_ERR("***** Hardware exception *****"); break; #if defined(CONFIG_STACK_CANARIES) || defined(CONFIG_STACK_SENTINEL) case _NANO_ERR_STACK_CHK_FAIL: - printk("***** Stack Check Fail! *****\n"); + LOG_ERR("***** Stack Check Fail! *****"); break; #endif /* CONFIG_STACK_CANARIES */ case _NANO_ERR_ALLOCATION_FAIL: - printk("**** Kernel Allocation Failure! ****\n"); + LOG_ERR("**** Kernel Allocation Failure! ****"); break; case _NANO_ERR_KERNEL_OOPS: - printk("***** Kernel OOPS! *****\n"); + LOG_ERR("***** Kernel OOPS! *****"); break; case _NANO_ERR_KERNEL_PANIC: - printk("***** Kernel Panic! *****\n"); + LOG_ERR("***** Kernel Panic! *****"); break; default: - printk("**** Unknown Fatal Error %d! ****\n", reason); + LOG_ERR("**** Unknown Fatal Error %d! ****", reason); break; } - printk("Current thread ID = %p\n" - "Faulting instruction address = 0x%x\n", - k_current_get(), pEsf->pc); + + LOG_ERR("Current thread ID = %p", k_current_get()); + LOG_ERR("Faulting instruction address = 0x%x", pEsf->pc); /* * Now that the error has been reported, call the user implemented @@ -98,6 +101,8 @@ FUNC_NORETURN void _arch_syscall_oops(void *ssf_ptr) u32_t *ssf_contents = ssf_ptr; NANO_ESF oops_esf = { 0 }; + LOG_PANIC(); + oops_esf.pc = ssf_contents[3]; _do_kernel_oops(&oops_esf); diff --git a/arch/arm/core/fault.c b/arch/arm/core/fault.c index 2ab22195199fc..65f8e49fb7aa7 100644 --- a/arch/arm/core/fault.c +++ b/arch/arm/core/fault.c @@ -13,26 +13,14 @@ #include #include - #include #include #include #include +#include +#include -#ifdef CONFIG_PRINTK -#include -#define PR_EXC(...) printk(__VA_ARGS__) -#define STORE_xFAR(reg_var, reg) u32_t reg_var = (u32_t)reg -#else -#define PR_EXC(...) -#define STORE_xFAR(reg_var, reg) -#endif /* CONFIG_PRINTK */ - -#if (CONFIG_FAULT_DUMP == 2) -#define PR_FAULT_INFO(...) PR_EXC(__VA_ARGS__) -#else -#define PR_FAULT_INFO(...) -#endif +LOG_MODULE_DECLARE(fatal); #if defined(CONFIG_NXP_MPU) #define EMN(edr) (((edr) & SYSMPU_EDR_EMN_MASK) >> SYSMPU_EDR_EMN_SHIFT) @@ -137,13 +125,13 @@ #if (CONFIG_FAULT_DUMP == 1) static void _FaultShow(const NANO_ESF *esf, int fault) { - PR_EXC("Fault! EXC #%d\n", fault); + LOG_ERR("Fault! EXC #%d\n", fault); #if defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE) - PR_EXC("MMFSR: 0x%x, BFSR: 0x%x, UFSR: 0x%x\n", + LOG_ERR("MMFSR: 0x%x, BFSR: 0x%x, UFSR: 0x%x", SCB_MMFSR, SCB_BFSR, SCB_UFSR); #if defined(CONFIG_ARM_SECURE_FIRMWARE) - PR_EXC("SFSR: 0x%x\n", SAU->SFSR); + LOG_ERR("SFSR: 0x%x", SAU->SFSR); #endif /* CONFIG_ARM_SECURE_FIRMWARE */ #endif /* CONFIG_ARMV7_M_ARMV8_M_MAINLINE */ } @@ -208,16 +196,16 @@ static u32_t _MpuFault(NANO_ESF *esf, int fromHardFault) { u32_t reason = _NANO_ERR_HW_EXCEPTION; - PR_FAULT_INFO("***** MPU FAULT *****\n"); + LOG_INF("***** MPU FAULT *****"); if (SCB->CFSR & SCB_CFSR_MSTKERR_Msk) { - PR_FAULT_INFO(" Stacking error\n"); + LOG_INF(" Stacking error"); } if (SCB->CFSR & SCB_CFSR_MUNSTKERR_Msk) { - PR_FAULT_INFO(" Unstacking error\n"); + LOG_INF(" Unstacking error"); } if (SCB->CFSR & SCB_CFSR_DACCVIOL_Msk) { - PR_FAULT_INFO(" Data Access Violation\n"); + LOG_INF(" Data Access Violation"); /* In a fault handler, to determine the true faulting address: * 1. Read and save the MMFAR value. * 2. Read the MMARVALID bit in the MMFSR. @@ -229,7 +217,7 @@ static u32_t _MpuFault(NANO_ESF *esf, int fromHardFault) u32_t mmfar = SCB->MMFAR; if (SCB->CFSR & SCB_CFSR_MMARVALID_Msk) { - PR_EXC(" MMFAR Address: 0x%x\n", mmfar); + LOG_ERR(" MMFAR Address: 0x%x", mmfar); if (fromHardFault) { /* clear SCB_MMAR[VALID] to reset */ SCB->CFSR &= ~SCB_CFSR_MMARVALID_Msk; @@ -263,12 +251,11 @@ static u32_t _MpuFault(NANO_ESF *esf, int fromHardFault) } } if (SCB->CFSR & SCB_CFSR_IACCVIOL_Msk) { - PR_FAULT_INFO(" Instruction Access Violation\n"); + LOG_INF(" Instruction Access Violation"); } #if defined(CONFIG_ARMV7_M_ARMV8_M_FP) if (SCB->CFSR & SCB_CFSR_MLSPERR_Msk) { - PR_FAULT_INFO( - " Floating-point lazy state preservation error\n"); + LOG_INF(" Floating-point lazy state preservation error"); } #endif /* !defined(CONFIG_ARMV7_M_ARMV8_M_FP) */ @@ -290,14 +277,16 @@ static u32_t _MpuFault(NANO_ESF *esf, int fromHardFault) */ static int _BusFault(NANO_ESF *esf, int fromHardFault) { - PR_FAULT_INFO("***** BUS FAULT *****\n"); + LOG_INF("***** BUS FAULT *****"); if (SCB->CFSR & SCB_CFSR_STKERR_Msk) { - PR_FAULT_INFO(" Stacking error\n"); + LOG_INF(" Stacking error"); } else if (SCB->CFSR & SCB_CFSR_UNSTKERR_Msk) { - PR_FAULT_INFO(" Unstacking error\n"); + LOG_INF(" Unstacking error"); } else if (SCB->CFSR & SCB_CFSR_PRECISERR_Msk) { - PR_FAULT_INFO(" Precise data bus error\n"); + u32_t bfar; + + LOG_INF(" Precise data bus error"); /* In a fault handler, to determine the true faulting address: * 1. Read and save the BFAR value. * 2. Read the BFARVALID bit in the BFSR. @@ -306,10 +295,10 @@ static int _BusFault(NANO_ESF *esf, int fromHardFault) * Software must follow this sequence because another * higher priority exception might change the BFAR value. */ - STORE_xFAR(bfar, SCB->BFAR); + bfar = SCB->BFAR; if (SCB->CFSR & SCB_CFSR_BFARVALID_Msk) { - PR_EXC(" BFAR Address: 0x%x\n", bfar); + LOG_ERR(" BFAR Address: 0x%x", bfar); if (fromHardFault) { /* clear SCB_CFSR_BFAR[VALID] to reset */ SCB->CFSR &= ~SCB_CFSR_BFARVALID_Msk; @@ -317,17 +306,17 @@ static int _BusFault(NANO_ESF *esf, int fromHardFault) } /* it's possible to have both a precise and imprecise fault */ if (SCB->CFSR & SCB_CFSR_IMPRECISERR_Msk) { - PR_FAULT_INFO(" Imprecise data bus error\n"); + LOG_INF(" Imprecise data bus error"); } } else if (SCB->CFSR & SCB_CFSR_IMPRECISERR_Msk) { - PR_FAULT_INFO(" Imprecise data bus error\n"); + LOG_INF(" Imprecise data bus error"); } else if (SCB->CFSR & SCB_CFSR_IBUSERR_Msk) { - PR_FAULT_INFO(" Instruction bus error\n"); + LOG_INF(" Instruction bus error"); #if !defined(CONFIG_ARMV7_M_ARMV8_M_FP) } #else } else if (SCB->CFSR & SCB_CFSR_LSPERR_Msk) { - PR_FAULT_INFO(" Floating-point lazy state preservation error\n"); + LOG_INF(" Floating-point lazy state preservation error"); } #endif /* !defined(CONFIG_ARMV7_M_ARMV8_M_FP) */ @@ -338,19 +327,20 @@ static int _BusFault(NANO_ESF *esf, int fromHardFault) if (sperr) { for (i = 0; i < SYSMPU_EAR_COUNT; i++, mask >>= 1) { + u32_t edr, ear; + if (!(sperr & mask)) { continue; } - STORE_xFAR(edr, SYSMPU->SP[i].EDR); - STORE_xFAR(ear, SYSMPU->SP[i].EAR); + edr = SYSMPU->SP[i].EDR; + ear = SYSMPU->SP[i].EAR; - PR_FAULT_INFO(" NXP MPU error, port %d\n", i); - PR_FAULT_INFO(" Mode: %s, %s Address: 0x%x\n", + LOG_INF(" NXP MPU error, port %d", i); + LOG_INF(" Mode: %s, %s Address: 0x%x", edr & BIT(2) ? "Supervisor" : "User", edr & BIT(1) ? "Data" : "Instruction", ear); - PR_FAULT_INFO( - " Type: %s, Master: %d, Regions: 0x%x\n", + LOG_INF(" Type: %s, Master: %d, Regions: 0x%x", edr & BIT(0) ? "Write" : "Read", EMN(edr), EACD(edr)); } @@ -382,18 +372,18 @@ static u32_t _UsageFault(const NANO_ESF *esf) { u32_t reason = _NANO_ERR_HW_EXCEPTION; - PR_FAULT_INFO("***** USAGE FAULT *****\n"); + LOG_INF("***** USAGE FAULT *****"); /* bits are sticky: they stack and must be reset */ if (SCB->CFSR & SCB_CFSR_DIVBYZERO_Msk) { - PR_FAULT_INFO(" Division by zero\n"); + LOG_INF(" Division by zero"); } if (SCB->CFSR & SCB_CFSR_UNALIGNED_Msk) { - PR_FAULT_INFO(" Unaligned memory access\n"); + LOG_INF(" Unaligned memory access"); } #if defined(CONFIG_ARMV8_M_MAINLINE) if (SCB->CFSR & SCB_CFSR_STKOF_Msk) { - PR_FAULT_INFO(" Stack overflow\n"); + LOG_INF(" Stack overflow"); #if defined(CONFIG_HW_STACK_PROTECTION) /* Stack Overflows are reported as stack * corruption errors. @@ -403,16 +393,16 @@ static u32_t _UsageFault(const NANO_ESF *esf) } #endif /* CONFIG_ARMV8_M_MAINLINE */ if (SCB->CFSR & SCB_CFSR_NOCP_Msk) { - PR_FAULT_INFO(" No coprocessor instructions\n"); + LOG_INF(" No coprocessor instructions"); } if (SCB->CFSR & SCB_CFSR_INVPC_Msk) { - PR_FAULT_INFO(" Illegal load of EXC_RETURN into PC\n"); + LOG_INF(" Illegal load of EXC_RETURN into PC"); } if (SCB->CFSR & SCB_CFSR_INVSTATE_Msk) { - PR_FAULT_INFO(" Illegal use of the EPSR\n"); + LOG_INF(" Illegal use of the EPSR"); } if (SCB->CFSR & SCB_CFSR_UNDEFINSTR_Msk) { - PR_FAULT_INFO(" Attempt to execute undefined instruction\n"); + LOG_INF(" Attempt to execute undefined instruction"); } /* clear USFR sticky bits */ @@ -432,28 +422,30 @@ static u32_t _UsageFault(const NANO_ESF *esf) */ static void _SecureFault(const NANO_ESF *esf) { - PR_FAULT_INFO("***** SECURE FAULT *****\n"); + u32_t sfar; + + LOG_INF("***** SECURE FAULT *****"); - STORE_xFAR(sfar, SAU->SFAR); + sfar = SAU->SFAR); if (SAU->SFSR & SAU_SFSR_SFARVALID_Msk) { - PR_EXC(" Address: 0x%x\n", sfar); + LOG_ERR(" Address: 0x%x", sfar); } /* bits are sticky: they stack and must be reset */ if (SAU->SFSR & SAU_SFSR_INVEP_Msk) { - PR_FAULT_INFO(" Invalid entry point\n"); + LOG_INF(" Invalid entry point"); } else if (SAU->SFSR & SAU_SFSR_INVIS_Msk) { - PR_FAULT_INFO(" Invalid integrity signature\n"); + LOG_INF(" Invalid integrity signature"); } else if (SAU->SFSR & SAU_SFSR_INVER_Msk) { - PR_FAULT_INFO(" Invalid exception return\n"); + LOG_INF(" Invalid exception return"); } else if (SAU->SFSR & SAU_SFSR_AUVIOL_Msk) { - PR_FAULT_INFO(" Attribution unit violation\n"); + LOG_INF(" Attribution unit violation"); } else if (SAU->SFSR & SAU_SFSR_INVTRAN_Msk) { - PR_FAULT_INFO(" Invalid transition\n"); + LOG_INF(" Invalid transition"); } else if (SAU->SFSR & SAU_SFSR_LSPERR_Msk) { - PR_FAULT_INFO(" Lazy state preservation\n"); + LOG_INF(" Lazy state preservation"); } else if (SAU->SFSR & SAU_SFSR_LSERR_Msk) { - PR_FAULT_INFO(" Lazy state error\n"); + LOG_INF(" Lazy state error"); } /* clear SFSR sticky bits */ @@ -473,8 +465,7 @@ static void _DebugMonitor(const NANO_ESF *esf) { ARG_UNUSED(esf); - PR_FAULT_INFO( - "***** Debug monitor exception (not implemented) *****\n"); + LOG_INF("***** Debug monitor exception (not implemented) *****"); } #else @@ -493,7 +484,7 @@ static u32_t _HardFault(NANO_ESF *esf) { u32_t reason = _NANO_ERR_HW_EXCEPTION; - PR_FAULT_INFO("***** HARD FAULT *****\n"); + LOG_INF("***** HARD FAULT *****"); #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE) if (_MemoryFaultIsRecoverable(esf)) { @@ -501,9 +492,9 @@ static u32_t _HardFault(NANO_ESF *esf) } #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE) if (SCB->HFSR & SCB_HFSR_VECTTBL_Msk) { - PR_EXC(" Bus fault on vector table read\n"); + LOG_ERR(" Bus fault on vector table read"); } else if (SCB->HFSR & SCB_HFSR_FORCED_Msk) { - PR_EXC(" Fault escalation (see below)\n"); + LOG_ERR(" Fault escalation (see below)"); if (SCB_MMFSR) { reason = _MpuFault(esf, 1); } else if (SCB_BFSR) { @@ -535,7 +526,7 @@ static void _ReservedException(const NANO_ESF *esf, int fault) { ARG_UNUSED(esf); - PR_FAULT_INFO("***** %s %d) *****\n", + LOG_INF("***** %s %d) *****", fault < 16 ? "Reserved Exception (" : "Spurious interrupt (IRQ ", fault - 16); } @@ -626,7 +617,7 @@ static void _SecureStackDump(const NANO_ESF *secure_esf) */ sec_ret_addr = *top_of_sec_stack; } - PR_FAULT_INFO(" S instruction address: 0x%x\n", sec_ret_addr); + LOG_INF(" S instruction address: 0x%x", sec_ret_addr); } #define SECURE_STACK_DUMP(esf) _SecureStackDump(esf) @@ -668,7 +659,11 @@ static void _SecureStackDump(const NANO_ESF *secure_esf) void _Fault(NANO_ESF *esf, u32_t exc_return) { u32_t reason; - int fault = SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk; + int fault; + + LOG_PANIC(); + + fault = SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk; #if defined(CONFIG_ARM_SECURE_FIRMWARE) if ((exc_return & EXC_RETURN_INDICATOR_PREFIX) != @@ -701,13 +696,13 @@ void _Fault(NANO_ESF *esf, u32_t exc_return) if (exc_return & EXC_RETURN_MODE_THREAD) { esf = (NANO_ESF *)__TZ_get_PSP_NS(); if ((SCB->ICSR & SCB_ICSR_RETTOBASE_Msk) == 0) { - PR_EXC("RETTOBASE does not match EXC_RETURN\n"); + LOG_ERR("RETTOBASE does not match EXC_RETURN"); goto _exit_fatal; } } else { esf = (NANO_ESF *)__TZ_get_MSP_NS(); if ((SCB->ICSR & SCB_ICSR_RETTOBASE_Msk) != 0) { - PR_EXC("RETTOBASE does not match EXC_RETURN\n"); + LOG_ERR("RETTOBASE does not match EXC_RETURN"); goto _exit_fatal; } } diff --git a/arch/arm/core/sys_fatal_error_handler.c b/arch/arm/core/sys_fatal_error_handler.c index a26305191a502..2725bc87e3774 100644 --- a/arch/arm/core/sys_fatal_error_handler.c +++ b/arch/arm/core/sys_fatal_error_handler.c @@ -16,8 +16,9 @@ #include #include #include -#include +#include +LOG_MODULE_DECLARE(fatal); /** * * @brief Fatal error handler @@ -53,11 +54,11 @@ void __weak _SysFatalErrorHandler(unsigned int reason, goto hang_system; } if (k_is_in_isr() || _is_thread_essential()) { - printk("Fatal fault in %s! Spinning...\n", + LOG_ERR("Fatal fault in %s! Spinning...", k_is_in_isr() ? "ISR" : "essential thread"); goto hang_system; } - printk("Fatal fault in thread %p! Aborting.\n", _current); + LOG_ERR("Fatal fault in thread %p! Aborting.", _current); k_thread_abort(_current); return; From 145559364769b9e8d8674aa9fb6842739f878827 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Tue, 18 Sep 2018 08:56:53 +0200 Subject: [PATCH 3/8] arch: arc: add log handling to fault handlers Added log_panic() to fault handlers. Replaced printk with log API Signed-off-by: Krzysztof Chruscinski --- arch/arc/core/fatal.c | 23 +++++++++++++++-------- arch/arc/core/fault.c | 11 ++++++++--- arch/arc/core/sys_fatal_error_handler.c | 8 +++++--- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/arch/arc/core/fatal.c b/arch/arc/core/fatal.c index 3fb91560c1f87..9ad2293bdeda5 100644 --- a/arch/arc/core/fatal.c +++ b/arch/arc/core/fatal.c @@ -16,7 +16,10 @@ #include #include #include -#include +#include +#include + +LOG_MODULE_REGISTER(fatal); /** * @@ -35,6 +38,8 @@ */ void _NanoFatalErrorHandler(unsigned int reason, const NANO_ESF *pEsf) { + LOG_PANIC(); + switch (reason) { case _NANO_ERR_HW_EXCEPTION: break; @@ -42,31 +47,31 @@ void _NanoFatalErrorHandler(unsigned int reason, const NANO_ESF *pEsf) #if defined(CONFIG_STACK_CANARIES) || defined(CONFIG_ARC_STACK_CHECKING) \ || defined(CONFIG_STACK_SENTINEL) case _NANO_ERR_STACK_CHK_FAIL: - printk("***** Stack Check Fail! *****\n"); + LOG_ERR("***** Stack Check Fail! *****"); break; #endif case _NANO_ERR_ALLOCATION_FAIL: - printk("**** Kernel Allocation Failure! ****\n"); + LOG_ERR("**** Kernel Allocation Failure! ****"); break; case _NANO_ERR_KERNEL_OOPS: - printk("***** Kernel OOPS! *****\n"); + LOG_ERR("***** Kernel OOPS! *****"); break; case _NANO_ERR_KERNEL_PANIC: - printk("***** Kernel Panic! *****\n"); + LOG_ERR("***** Kernel Panic! *****"); break; default: - printk("**** Unknown Fatal Error %d! ****\n", reason); + LOG_ERR("**** Unknown Fatal Error %d! ****", reason); break; } - printk("Current thread ID = %p\n", k_current_get()); + LOG_ERR("Current thread ID = %p", k_current_get()); if (reason == _NANO_ERR_HW_EXCEPTION) { - printk("Faulting instruction address = 0x%lx\n", + LOG_ERR("Faulting instruction address = 0x%lx", _arc_v2_aux_reg_read(_ARC_V2_ERET)); } @@ -83,6 +88,8 @@ void _NanoFatalErrorHandler(unsigned int reason, const NANO_ESF *pEsf) FUNC_NORETURN void _arch_syscall_oops(void *ssf_ptr) { + LOG_PANIC(); + _SysFatalErrorHandler(_NANO_ERR_KERNEL_OOPS, ssf_ptr); CODE_UNREACHABLE; } diff --git a/arch/arc/core/fault.c b/arch/arc/core/fault.c index a51154c7fae76..a943fb5831dbc 100644 --- a/arch/arc/core/fault.c +++ b/arch/arc/core/fault.c @@ -17,8 +17,11 @@ #include #include -#include #include +#include +#include + +LOG_MODULE_DECLARE(fatal); #ifdef CONFIG_USERSPACE Z_EXC_DECLARE(z_arch_user_string_nlen); @@ -42,6 +45,8 @@ void _Fault(NANO_ESF *esf) u32_t exc_addr = _arc_v2_aux_reg_read(_ARC_V2_EFA); u32_t ecr = _arc_v2_aux_reg_read(_ARC_V2_ECR); + LOG_PANIC(); + #ifdef CONFIG_USERSPACE for (int i = 0; i < ARRAY_SIZE(exceptions); i++) { u32_t start = (u32_t)exceptions[i].start; @@ -65,9 +70,9 @@ void _Fault(NANO_ESF *esf) return; } - printk("Exception vector: 0x%x, cause code: 0x%x, parameter 0x%x\n", + LOG_ERR("Exception vector: 0x%x, cause code: 0x%x, parameter 0x%x", vector, code, parameter); - printk("Address 0x%x\n", exc_addr); + LOG_ERR("Address 0x%x", exc_addr); #ifdef CONFIG_ARC_STACK_CHECKING /* Vector 6 = EV_ProV. Regardless of code, parameter 2 means stack * check violation diff --git a/arch/arc/core/sys_fatal_error_handler.c b/arch/arc/core/sys_fatal_error_handler.c index 6f3670869907a..dc995cece3bff 100644 --- a/arch/arc/core/sys_fatal_error_handler.c +++ b/arch/arc/core/sys_fatal_error_handler.c @@ -15,7 +15,9 @@ #include #include #include -#include +#include + +LOG_MODULE_DECLARE(fatal); /** * @@ -53,11 +55,11 @@ __weak void _SysFatalErrorHandler(unsigned int reason, } if (_is_thread_essential()) { - printk("Fatal fault in essential thread! Spinning...\n"); + LOG_ERR("Fatal fault in essential thread! Spinning..."); goto hang_system; } - printk("Fatal fault in thread %p! Aborting.\n", _current); + LOG_ERR("Fatal fault in thread %p! Aborting.", _current); k_thread_abort(_current); From 148469a9d46e0a8714ebe5618a9c8ed8d57727c4 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Tue, 18 Sep 2018 08:58:29 +0200 Subject: [PATCH 4/8] arch: nios2: add log handling to fault handlers Added log_panic() to fault handlers. Replaced printk with log API Signed-off-by: Krzysztof Chruscinski --- arch/nios2/core/fatal.c | 56 ++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/arch/nios2/core/fatal.c b/arch/nios2/core/fatal.c index 8b3a6204abbb5..001f3a07dccc7 100644 --- a/arch/nios2/core/fatal.c +++ b/arch/nios2/core/fatal.c @@ -7,8 +7,11 @@ #include #include #include -#include #include +#include +#include + +LOG_MODULE_REGISTER(fatal); const NANO_ESF _default_esf = { 0xdeadbaad, @@ -50,31 +53,32 @@ const NANO_ESF _default_esf = { FUNC_NORETURN void _NanoFatalErrorHandler(unsigned int reason, const NANO_ESF *esf) { -#ifdef CONFIG_PRINTK + LOG_PANIC(); + switch (reason) { case _NANO_ERR_CPU_EXCEPTION: case _NANO_ERR_SPURIOUS_INT: break; case _NANO_ERR_ALLOCATION_FAIL: - printk("**** Kernel Allocation Failure! ****\n"); + LOG_ERR("**** Kernel Allocation Failure! ****"); break; case _NANO_ERR_KERNEL_OOPS: - printk("***** Kernel OOPS! *****\n"); + LOG_ERR("***** Kernel OOPS! *****"); break; case _NANO_ERR_KERNEL_PANIC: - printk("***** Kernel Panic! *****\n"); + LOG_ERR("***** Kernel Panic! *****"); break; #ifdef CONFIG_STACK_SENTINEL case _NANO_ERR_STACK_CHK_FAIL: - printk("***** Stack overflow *****\n"); + LOG_ERR("***** Stack overflow *****"); break; #endif default: - printk("**** Unknown Fatal Error %u! ****\n", reason); + LOG_ERR("**** Unknown Fatal Error %u! ****", reason); break; } @@ -85,24 +89,22 @@ FUNC_NORETURN void _NanoFatalErrorHandler(unsigned int reason, * We may want to introduce a config option to save and dump all * registers, at the expense of some stack space. */ - printk("Current thread ID: %p\n" - "Faulting instruction: 0x%x\n" - " r1: 0x%x r2: 0x%x r3: 0x%x r4: 0x%x\n" - " r5: 0x%x r6: 0x%x r7: 0x%x r8: 0x%x\n" - " r9: 0x%x r10: 0x%x r11: 0x%x r12: 0x%x\n" - " r13: 0x%x r14: 0x%x r15: 0x%x ra: 0x%x\n" - "estatus: %x\n", k_current_get(), esf->instr - 4, - esf->r1, esf->r2, esf->r3, esf->r4, - esf->r5, esf->r6, esf->r7, esf->r8, - esf->r9, esf->r10, esf->r11, esf->r12, - esf->r13, esf->r14, esf->r15, esf->ra, - esf->estatus); -#endif + LOG_ERR("Current thread ID: %p",k_current_get()); + LOG_ERR("Faulting instruction: 0x%x", esf->instr - 4); + LOG_ERR(" r1: 0x%x r2: 0x%x r3: 0x%x r4: 0x%x", + esf->r1, esf->r2, esf->r3, esf->r4); + LOG_ERR(" r5: 0x%x r6: 0x%x r7: 0x%x r8: 0x%x", + esf->r5, esf->r6, esf->r7, esf->r8); + LOG_ERR(" r9: 0x%x r10: 0x%x r11: 0x%x r12: 0x%x", + esf->r9, esf->r10, esf->r11, esf->r12); + LOG_ERR(" r13: 0x%x r14: 0x%x r15: 0x%x ra: 0x%x", + esf->r13, esf->r14, esf->r15, esf->ra); + LOG_ERR("estatus: %x", esf->estatus); _SysFatalErrorHandler(reason, esf); } -#if defined(CONFIG_EXTRA_EXCEPTION_INFO) && defined(CONFIG_PRINTK) \ +#if defined(CONFIG_EXTRA_EXCEPTION_INFO) \ && defined(ALT_CPU_HAS_EXTRA_EXCEPTION_INFO) static char *cause_str(u32_t cause_code) { @@ -165,7 +167,6 @@ static char *cause_str(u32_t cause_code) FUNC_NORETURN void _Fault(const NANO_ESF *esf) { -#ifdef CONFIG_PRINTK /* Unfortunately, completely unavailable on Nios II/e cores */ #ifdef ALT_CPU_HAS_EXTRA_EXCEPTION_INFO u32_t exc_reg, badaddr_reg, eccftl; @@ -180,16 +181,15 @@ FUNC_NORETURN void _Fault(const NANO_ESF *esf) cause = (exc_reg & NIOS2_EXCEPTION_REG_CAUSE_MASK) >> NIOS2_EXCEPTION_REG_CAUSE_OFST; - printk("Exception cause: %d ECCFTL: 0x%x\n", cause, eccftl); + LOG_ERR("Exception cause: %d ECCFTL: 0x%x", cause, eccftl); #if CONFIG_EXTRA_EXCEPTION_INFO - printk("reason: %s\n", cause_str(cause)); + LOG_ERR("reason: %s", cause_str(cause)); #endif if (BIT(cause) & NIOS2_BADADDR_CAUSE_MASK) { badaddr_reg = _nios2_creg_read(NIOS2_CR_BADADDR); - printk("Badaddr: 0x%x\n", badaddr_reg); + LOG_ERR("Badaddr: 0x%x", badaddr_reg); } #endif /* ALT_CPU_HAS_EXTRA_EXCEPTION_INFO */ -#endif /* CONFIG_PRINTK */ _NanoFatalErrorHandler(_NANO_ERR_CPU_EXCEPTION, esf); } @@ -230,11 +230,11 @@ FUNC_NORETURN __weak void _SysFatalErrorHandler(unsigned int reason, goto hang_system; } if (k_is_in_isr() || _is_thread_essential()) { - printk("Fatal fault in %s! Spinning...\n", + LOG_ERR("Fatal fault in %s! Spinning...", k_is_in_isr() ? "ISR" : "essential thread"); goto hang_system; } - printk("Fatal fault in thread %p! Aborting.\n", _current); + LOG_ERR("Fatal fault in thread %p! Aborting.", _current); k_thread_abort(_current); hang_system: From c4111f86a5a8f4f55c244a62bd5ec687f62553d3 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Tue, 18 Sep 2018 08:58:48 +0200 Subject: [PATCH 5/8] arch: posix: add log handling to fault handlers Added log_panic() to fault handlers. Replaced printk with log API Signed-off-by: Krzysztof Chruscinski --- arch/posix/core/fatal.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/arch/posix/core/fatal.c b/arch/posix/core/fatal.c index 988932ac88e76..50cce3e777c81 100644 --- a/arch/posix/core/fatal.c +++ b/arch/posix/core/fatal.c @@ -8,9 +8,12 @@ #include #include #include -#include #include #include "posix_soc_if.h" +#include +#include + +LOG_MODULE_REGISTER(fatal); const NANO_ESF _default_esf = { 0xdeadbaad @@ -34,41 +37,40 @@ const NANO_ESF _default_esf = { FUNC_NORETURN void _NanoFatalErrorHandler(unsigned int reason, const NANO_ESF *esf) { -#ifdef CONFIG_PRINTK + LOG_PANIC(); + switch (reason) { case _NANO_ERR_CPU_EXCEPTION: case _NANO_ERR_SPURIOUS_INT: break; case _NANO_ERR_INVALID_TASK_EXIT: - printk("***** Invalid Exit Software Error! *****\n"); + LOG_ERR("***** Invalid Exit Software Error! *****"); break; case _NANO_ERR_ALLOCATION_FAIL: - printk("**** Kernel Allocation Failure! ****\n"); + LOG_ERR("**** Kernel Allocation Failure! ****"); break; case _NANO_ERR_KERNEL_OOPS: - printk("***** Kernel OOPS! *****\n"); + LOG_ERR("***** Kernel OOPS! *****"); break; case _NANO_ERR_KERNEL_PANIC: - printk("***** Kernel Panic! *****\n"); + LOG_ERR("***** Kernel Panic! *****"); break; #ifdef CONFIG_STACK_SENTINEL case _NANO_ERR_STACK_CHK_FAIL: - printk("***** Stack overflow *****\n"); + LOG_ERR("***** Stack overflow *****"); break; #endif default: - printk("**** Unknown Fatal Error %u! ****\n", reason); + LOG_ERR("**** Unknown Fatal Error %u! ****", reason); break; } -#endif - void _SysFatalErrorHandler(unsigned int reason, const NANO_ESF *pEsf); _SysFatalErrorHandler(reason, esf); @@ -110,15 +112,15 @@ FUNC_NORETURN __weak void _SysFatalErrorHandler(unsigned int reason, } if (k_is_in_isr() || _is_thread_essential()) { posix_print_error_and_exit( - "Fatal fault in %s! Stopping...\n", + "Fatal fault in %s! Stopping...", k_is_in_isr() ? "ISR" : "essential thread"); } - printk("Fatal fault in thread %p! Aborting.\n", _current); + LOG_ERR("Fatal fault in thread %p! Aborting.", _current); k_thread_abort(_current); hang_system: posix_print_error_and_exit( - "Stopped in _SysFatalErrorHandler()\n"); + "Stopped in _SysFatalErrorHandler()"); CODE_UNREACHABLE; } From ae625b9406f75166bff6f9e867a82e72642aefcc Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Tue, 18 Sep 2018 08:59:06 +0200 Subject: [PATCH 6/8] arch: riscv32: add log handling to fault handlers Added log_panic() to fault handlers. Replaced printk with log API Signed-off-by: Krzysztof Chruscinski --- arch/riscv32/core/fatal.c | 54 ++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/arch/riscv32/core/fatal.c b/arch/riscv32/core/fatal.c index 601dad7cb2772..31dd4454e3044 100644 --- a/arch/riscv32/core/fatal.c +++ b/arch/riscv32/core/fatal.c @@ -7,7 +7,10 @@ #include #include #include -#include +#include +#include + +LOG_MODULE_REGISTER(fatal); const NANO_ESF _default_esf = { 0xdeadbaad, @@ -60,6 +63,8 @@ const NANO_ESF _default_esf = { FUNC_NORETURN void _NanoFatalErrorHandler(unsigned int reason, const NANO_ESF *esf) { + LOG_PANIC(); + switch (reason) { case _NANO_ERR_CPU_EXCEPTION: case _NANO_ERR_SPURIOUS_INT: @@ -67,40 +72,39 @@ FUNC_NORETURN void _NanoFatalErrorHandler(unsigned int reason, #if defined(CONFIG_STACK_CANARIES) || defined(CONFIG_STACK_SENTINEL) case _NANO_ERR_STACK_CHK_FAIL: - printk("***** Stack Check Fail! *****\n"); + LOG_ERR("***** Stack Check Fail! *****"); break; #endif /* CONFIG_STACK_CANARIES */ case _NANO_ERR_ALLOCATION_FAIL: - printk("**** Kernel Allocation Failure! ****\n"); + LOG_ERR("**** Kernel Allocation Failure! ****"); break; case _NANO_ERR_KERNEL_OOPS: - printk("***** Kernel OOPS! *****\n"); + LOG_ERR("***** Kernel OOPS! *****"); break; case _NANO_ERR_KERNEL_PANIC: - printk("***** Kernel Panic! *****\n"); + LOG_ERR("***** Kernel Panic! *****"); break; default: - printk("**** Unknown Fatal Error %d! ****\n", reason); + LOG_ERR("**** Unknown Fatal Error %d! ****", reason); break; } - printk("Current thread ID = %p\n" - "Faulting instruction address = 0x%x\n" - " ra: 0x%x gp: 0x%x tp: 0x%x t0: 0x%x\n" - " t1: 0x%x t2: 0x%x t3: 0x%x t4: 0x%x\n" - " t5: 0x%x t6: 0x%x a0: 0x%x a1: 0x%x\n" - " a2: 0x%x a3: 0x%x a4: 0x%x a5: 0x%x\n" - " a6: 0x%x a7: 0x%x\n", - k_current_get(), - (esf->mepc == 0xdeadbaad) ? 0xdeadbaad : esf->mepc, - esf->ra, esf->gp, esf->tp, esf->t0, - esf->t1, esf->t2, esf->t3, esf->t4, - esf->t5, esf->t6, esf->a0, esf->a1, - esf->a2, esf->a3, esf->a4, esf->a5, + LOG_ERR("Current thread ID = %p", k_current_get()); + LOG_ERR("Faulting instruction address = 0x%x", + (esf->mepc == 0xdeadbaad) ? 0xdeadbaad : esf->mepc); + LOG_ERR(" ra: 0x%x gp: 0x%x tp: 0x%x t0: 0x%x", + esf->ra, esf->gp, esf->tp, esf->t0); + LOG_ERR(" t1: 0x%x t2: 0x%x t3: 0x%x t4: 0x%x", + esf->t1, esf->t2, esf->t3, esf->t4); + LOG_ERR(" t5: 0x%x t6: 0x%x a0: 0x%x a1: 0x%x", + esf->t5, esf->t6, esf->a0, esf->a1); + LOG_ERR(" a2: 0x%x a3: 0x%x a4: 0x%x a5: 0x%x", + esf->a2, esf->a3, esf->a4, esf->a5); + LOG_ERR(" a6: 0x%x a7: 0x%x", esf->a6, esf->a7); _SysFatalErrorHandler(reason, esf); @@ -135,6 +139,8 @@ FUNC_NORETURN __weak void _SysFatalErrorHandler(unsigned int reason, { ARG_UNUSED(esf); + LOG_PANIC(); + #if !defined(CONFIG_SIMPLE_FATAL_ERROR_HANDLER) #ifdef CONFIG_STACK_SENTINEL if (reason == _NANO_ERR_STACK_CHK_FAIL) { @@ -145,11 +151,11 @@ FUNC_NORETURN __weak void _SysFatalErrorHandler(unsigned int reason, goto hang_system; } if (k_is_in_isr() || _is_thread_essential()) { - printk("Fatal fault in %s! Spinning...\n", + LOG_ERR("Fatal fault in %s! Spinning...", k_is_in_isr() ? "ISR" : "essential thread"); goto hang_system; } - printk("Fatal fault in thread %p! Aborting.\n", _current); + LOG_ERR("Fatal fault in thread %p! Aborting.", _current); k_thread_abort(_current); hang_system: @@ -163,8 +169,6 @@ FUNC_NORETURN __weak void _SysFatalErrorHandler(unsigned int reason, CODE_UNREACHABLE; } - -#ifdef CONFIG_PRINTK static char *cause_str(u32_t cause) { switch (cause) { @@ -184,8 +188,6 @@ static char *cause_str(u32_t cause) return "unknown"; } } -#endif - FUNC_NORETURN void _Fault(const NANO_ESF *esf) { @@ -195,7 +197,7 @@ FUNC_NORETURN void _Fault(const NANO_ESF *esf) mcause &= SOC_MCAUSE_EXP_MASK; - printk("Exception cause %s (%d)\n", cause_str(mcause), (int)mcause); + LOG_ERR("Exception cause %s (%d)", cause_str(mcause), (int)mcause); _NanoFatalErrorHandler(_NANO_ERR_CPU_EXCEPTION, esf); } From b5be9aaa663c61b8a4b00c1e25e8d637c8a1a36f Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Tue, 18 Sep 2018 08:59:22 +0200 Subject: [PATCH 7/8] arch: x86: add log handling to fault handlers Added log_panic() to fault handlers. Replaced printk with log API Signed-off-by: Krzysztof Chruscinski --- arch/x86/core/fatal.c | 77 ++++++++++++------------- arch/x86/core/sys_fatal_error_handler.c | 13 ++++- 2 files changed, 48 insertions(+), 42 deletions(-) diff --git a/arch/x86/core/fatal.c b/arch/x86/core/fatal.c index 5a0516c5af4ab..d8faa4ce48f76 100644 --- a/arch/x86/core/fatal.c +++ b/arch/x86/core/fatal.c @@ -16,12 +16,15 @@ #include #include -#include #include #include #include #include #include +#include +#include + +LOG_MODULE_REGISTER(fatal); __weak void _debug_fatal_hook(const NANO_ESF *esf) { ARG_UNUSED(esf); } @@ -39,14 +42,16 @@ static void unwind_stack(u32_t base_ptr) struct stack_frame *frame; int i; + LOG_ERR("call trace:"); + if (!base_ptr) { - printk("NULL base ptr\n"); + LOG_ERR("NULL base ptr"); return; } for (i = 0; i < MAX_STACK_FRAMES; i++) { if (base_ptr % sizeof(base_ptr) != 0) { - printk("unaligned frame ptr\n"); + LOG_ERR("unaligned frame ptr"); return; } @@ -55,9 +60,9 @@ static void unwind_stack(u32_t base_ptr) break; } #ifdef CONFIG_X86_IAMCU - printk(" 0x%08x\n", frame->ret_addr); + LOG_ERR(" 0x%08x", frame->ret_addr); #else - printk(" 0x%08x (0x%x)\n", frame->ret_addr, frame->args); + LOG_ERR(" 0x%08x (0x%x)", frame->ret_addr, frame->args); #endif base_ptr = frame->next; } @@ -83,9 +88,9 @@ static void unwind_stack(u32_t base_ptr) FUNC_NORETURN void _NanoFatalErrorHandler(unsigned int reason, const NANO_ESF *pEsf) { - _debug_fatal_hook(pEsf); + LOG_PANIC(); -#ifdef CONFIG_PRINTK + _debug_fatal_hook(pEsf); /* Display diagnostic information about the error */ @@ -96,56 +101,50 @@ FUNC_NORETURN void _NanoFatalErrorHandler(unsigned int reason, case _NANO_ERR_SPURIOUS_INT: { int vector = _irq_controller_isr_vector_get(); - printk("***** Unhandled interrupt vector "); + LOG_ERR("***** Unhandled interrupt vector "); if (vector >= 0) { - printk("%d ", vector); + LOG_ERR("%d ", vector); } - printk("*****\n"); + LOG_ERR("*****"); break; } #if defined(CONFIG_STACK_CANARIES) || defined(CONFIG_STACK_SENTINEL) || \ defined(CONFIG_HW_STACK_PROTECTION) || \ defined(CONFIG_USERSPACE) case _NANO_ERR_STACK_CHK_FAIL: - printk("***** Stack Check Fail! *****\n"); + LOG_ERR("***** Stack Check Fail! *****"); break; #endif /* CONFIG_STACK_CANARIES */ case _NANO_ERR_KERNEL_OOPS: - printk("***** Kernel OOPS! *****\n"); + LOG_ERR("***** Kernel OOPS! *****"); break; case _NANO_ERR_KERNEL_PANIC: - printk("***** Kernel Panic! *****\n"); + LOG_ERR("***** Kernel Panic! *****"); break; case _NANO_ERR_ALLOCATION_FAIL: - printk("**** Kernel Allocation Failure! ****\n"); + LOG_ERR("**** Kernel Allocation Failure! ****"); break; default: - printk("**** Unknown Fatal Error %d! ****\n", reason); + LOG_ERR("**** Unknown Fatal Error %d! ****", reason); break; } - printk("Current thread ID = %p\n" - "eax: 0x%08x, ebx: 0x%08x, ecx: 0x%08x, edx: 0x%08x\n" - "esi: 0x%08x, edi: 0x%08x, ebp: 0x%08x, esp: 0x%08x\n" - "eflags: 0x%08x cs: 0x%04x\n" -#ifdef CONFIG_EXCEPTION_STACK_TRACE - "call trace:\n" -#endif - "eip: 0x%08x\n", - k_current_get(), - pEsf->eax, pEsf->ebx, pEsf->ecx, pEsf->edx, - pEsf->esi, pEsf->edi, pEsf->ebp, pEsf->esp, + LOG_ERR("Current thread ID = %p", k_current_get()); + LOG_ERR("eax: 0x%08x, ebx: 0x%08x, ecx: 0x%08x, edx: 0x%08x", + pEsf->eax, pEsf->ebx, pEsf->ecx, pEsf->edx); + LOG_ERR("esi: 0x%08x, edi: 0x%08x, ebp: 0x%08x, esp: 0x%08x", + pEsf->esi, pEsf->edi, pEsf->ebp, pEsf->esp); + LOG_ERR("eflags: 0x%08x cs: 0x%04x eip: 0x%08x", pEsf->eflags, pEsf->cs & 0xFFFF, pEsf->eip); + #ifdef CONFIG_EXCEPTION_STACK_TRACE unwind_stack(pEsf->ebp); #endif -#endif /* CONFIG_PRINTK */ - /* * Error was fatal to a kernel task or a thread; invoke the system @@ -216,14 +215,14 @@ const NANO_ESF _default_esf = { static FUNC_NORETURN void generic_exc_handle(unsigned int vector, const NANO_ESF *pEsf) { - printk("***** "); + LOG_ERR("***** "); if (vector == 13) { - printk("General Protection Fault\n"); + LOG_ERR("General Protection Fault"); } else { - printk("CPU exception %d\n", vector); + LOG_ERR("CPU exception %d", vector); } if ((1 << vector) & _EXC_ERROR_CODE_FAULTS) { - printk("***** Exception code: 0x%x\n", pEsf->errorCode); + LOG_ERR("***** Exception code: 0x%x", pEsf->errorCode); } _NanoFatalErrorHandler(_NANO_ERR_CPU_EXCEPTION, pEsf); } @@ -281,7 +280,7 @@ EXC_FUNC_NOCODE(IV_MACHINE_CHECK); static void dump_entry_flags(x86_page_entry_data_t flags) { #ifdef CONFIG_X86_PAE_MODE - printk("0x%x%x %s, %s, %s, %s\n", (u32_t)(flags>>32), + LOG_ERR("0x%x%x %s, %s, %s, %s", (u32_t)(flags>>32), (u32_t)(flags), flags & (x86_page_entry_data_t)MMU_ENTRY_PRESENT ? "Present" : "Non-present", @@ -292,7 +291,7 @@ static void dump_entry_flags(x86_page_entry_data_t flags) flags & (x86_page_entry_data_t)MMU_ENTRY_EXECUTE_DISABLE ? "Execute Disable" : "Execute Enabled"); #else - printk("0x%03x %s, %s, %s\n", flags, + LOG_ERR("0x%03x %s, %s, %s", flags, flags & (x86_page_entry_data_t)MMU_ENTRY_PRESENT ? "Present" : "Non-present", flags & (x86_page_entry_data_t)MMU_ENTRY_WRITE ? @@ -308,10 +307,10 @@ static void dump_mmu_flags(void *addr) _x86_mmu_get_flags(addr, &pde_flags, &pte_flags); - printk("PDE: "); + LOG_ERR("PDE: "); dump_entry_flags(pde_flags); - printk("PTE: "); + LOG_ERR("PTE: "); dump_entry_flags(pte_flags); } #endif @@ -342,9 +341,9 @@ void page_fault_handler(NANO_ESF *pEsf) __asm__ ("mov %%cr2, %0" : "=r" (cr2)); err = pEsf->errorCode; - printk("***** CPU Page Fault (error code 0x%08x)\n", err); + LOG_ERR("***** CPU Page Fault (error code 0x%08x)", err); - printk("%s thread %s address 0x%08x\n", + LOG_ERR("%s thread %s address 0x%08x", err & US ? "User" : "Supervisor", err & ID ? "executed" : (err & WR ? "wrote" : "read"), cr2); @@ -407,7 +406,7 @@ static FUNC_NORETURN __used void _df_handler_bottom(void) */ _x86_mmu_get_flags((void *)_df_esf.esp - 1, &pde_flags, &pte_flags); if (pte_flags & MMU_ENTRY_PRESENT) { - printk("***** Double Fault *****\n"); + LOG_ERR("***** Double Fault *****"); reason = _NANO_ERR_CPU_EXCEPTION; } else { reason = _NANO_ERR_STACK_CHK_FAIL; diff --git a/arch/x86/core/sys_fatal_error_handler.c b/arch/x86/core/sys_fatal_error_handler.c index 9ef9375c82967..d6dcd1ffaf713 100644 --- a/arch/x86/core/sys_fatal_error_handler.c +++ b/arch/x86/core/sys_fatal_error_handler.c @@ -18,6 +18,11 @@ #include #include +#include +#include + +LOG_MODULE_DECLARE(fatal); + /** * * @brief Fatal error handler @@ -43,6 +48,8 @@ FUNC_NORETURN __weak void _SysFatalErrorHandler(unsigned int reason, { ARG_UNUSED(pEsf); + LOG_PANIC(); + #if !defined(CONFIG_SIMPLE_FATAL_ERROR_HANDLER) #ifdef CONFIG_STACK_SENTINEL if (reason == _NANO_ERR_STACK_CHK_FAIL) { @@ -53,11 +60,11 @@ FUNC_NORETURN __weak void _SysFatalErrorHandler(unsigned int reason, goto hang_system; } if (k_is_in_isr() || _is_thread_essential()) { - printk("Fatal fault in %s! Spinning...\n", + LOG_ERR("Fatal fault in %s! Spinning...", k_is_in_isr() ? "ISR" : "essential thread"); goto hang_system; } - printk("Fatal fault in thread %p! Aborting.\n", _current); + LOG_ERR("Fatal fault in thread %p! Aborting.", _current); k_thread_abort(_current); hang_system: @@ -66,7 +73,7 @@ FUNC_NORETURN __weak void _SysFatalErrorHandler(unsigned int reason, #endif #ifdef CONFIG_BOARD_QEMU_X86 - printk("Terminate emulator due to fatal kernel error\n"); + LOG_ERR("Terminate emulator due to fatal kernel error"); /* Causes QEMU to exit. We passed the following on the command line: * -device isa-debug-exit,iobase=0xf4,iosize=0x04 */ From 4faefe389e8f8f85147fc9dd16cac52a14c5f526 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Tue, 18 Sep 2018 08:59:45 +0200 Subject: [PATCH 8/8] arch: xtensa: add log handling to fault handlers Added log_panic() to fault handlers. Replaced printk with log API Signed-off-by: Krzysztof Chruscinski --- arch/xtensa/core/fatal.c | 53 ++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/arch/xtensa/core/fatal.c b/arch/xtensa/core/fatal.c index c48e26b2ed131..371f1a7878579 100644 --- a/arch/xtensa/core/fatal.c +++ b/arch/xtensa/core/fatal.c @@ -8,8 +8,11 @@ #include #include #include -#include #include +#include +#include + +LOG_MODULE_REGISTER(fatal); #ifdef XT_SIMULATOR #include @@ -51,6 +54,8 @@ const NANO_ESF _default_esf = { XTENSA_ERR_NORET void _NanoFatalErrorHandler(unsigned int reason, const NANO_ESF *pEsf) { + LOG_PANIC(); + switch (reason) { case _NANO_ERR_HW_EXCEPTION: case _NANO_ERR_RESERVED_IRQ: @@ -58,29 +63,27 @@ XTENSA_ERR_NORET void _NanoFatalErrorHandler(unsigned int reason, #if defined(CONFIG_STACK_CANARIES) || defined(CONFIG_STACK_SENTINEL) case _NANO_ERR_STACK_CHK_FAIL: - printk("***** Stack Check Fail! *****\n"); + LOG_ERR("***** Stack Check Fail! *****"); break; #endif /* CONFIG_STACK_CANARIES */ case _NANO_ERR_ALLOCATION_FAIL: - printk("**** Kernel Allocation Failure! ****\n"); + LOG_ERR("**** Kernel Allocation Failure! ****"); break; case _NANO_ERR_KERNEL_OOPS: - printk("***** Kernel OOPS! *****\n"); + LOG_ERR("***** Kernel OOPS! *****"); break; case _NANO_ERR_KERNEL_PANIC: - printk("***** Kernel Panic! *****\n"); + LOG_ERR("***** Kernel Panic! *****"); break; default: - printk("**** Unknown Fatal Error %d! ****\n", reason); + LOG_ERR("**** Unknown Fatal Error %d! ****", reason); break; } - printk("Current thread ID = %p\n" - "Faulting instruction address = 0x%x\n", - k_current_get(), - pEsf->pc); + LOG_ERR("Current thread ID = %p", k_current_get()); + LOG_ERR("Faulting instruction address = 0x%x", pEsf->pc); /* * Now that the error has been reported, call the user implemented @@ -92,8 +95,6 @@ XTENSA_ERR_NORET void _NanoFatalErrorHandler(unsigned int reason, _SysFatalErrorHandler(reason, pEsf); } - -#ifdef CONFIG_PRINTK static char *cause_str(unsigned int cause_code) { switch (cause_code) { @@ -147,7 +148,6 @@ static char *cause_str(unsigned int cause_code) return "unknown/reserved"; } } -#endif static inline unsigned int get_bits(int offset, int num_bits, unsigned int val) { @@ -160,42 +160,39 @@ static inline unsigned int get_bits(int offset, int num_bits, unsigned int val) static void dump_exc_state(void) { -#ifdef CONFIG_PRINTK unsigned int cause, ps; cause = get_sreg(EXCCAUSE); ps = get_sreg(PS); - printk("Exception cause %d (%s):\n" - " EPC1 : 0x%08x EXCSAVE1 : 0x%08x EXCVADDR : 0x%08x\n", + LOG_ERR("Exception cause %d (%s):" + " EPC1 : 0x%08x EXCSAVE1 : 0x%08x EXCVADDR : 0x%08x", cause, cause_str(cause), get_sreg(EPC_1), get_sreg(EXCSAVE_1), get_sreg(EXCVADDR)); - printk("Program state (PS):\n" - " INTLEVEL : %02d EXCM : %d UM : %d RING : %d WOE : %d\n", + LOG_ERR("Program state (PS):"); + LOG_ERR(" INTLEVEL : %02d EXCM : %d UM : %d RING : %d WOE : %d", get_bits(0, 4, ps), get_bits(4, 1, ps), get_bits(5, 1, ps), get_bits(6, 2, ps), get_bits(18, 1, ps)); #ifndef __XTENSA_CALL0_ABI__ - printk(" OWB : %02d CALLINC : %d\n", + LOG_ERR(" OWB : %02d CALLINC : %d", get_bits(8, 4, ps), get_bits(16, 2, ps)); #endif -#endif /* CONFIG_PRINTK */ } XTENSA_ERR_NORET void FatalErrorHandler(void) { - printk("*** Unhandled exception ****\n"); + LOG_ERR("*** Unhandled exception ****"); dump_exc_state(); _NanoFatalErrorHandler(_NANO_ERR_HW_EXCEPTION, &_default_esf); } XTENSA_ERR_NORET void ReservedInterruptHandler(unsigned int intNo) { - printk("*** Reserved Interrupt ***\n"); + LOG_ERR("*** Reserved Interrupt ***"); dump_exc_state(); - printk("INTENABLE = 0x%x\n" - "INTERRUPT = 0x%x (%d)\n", + LOG_ERR("INTENABLE = 0x%x INTERRUPT = 0x%x (%d)", get_sreg(INTENABLE), (1 << intNo), intNo); _NanoFatalErrorHandler(_NANO_ERR_RESERVED_IRQ, &_default_esf); } @@ -211,7 +208,7 @@ void exit(int return_code) : [code] "r" (return_code), [call] "i" (SYS_exit) : "a3", "a2"); #else - printk("exit(%d)\n", return_code); + LOG_ERR("exit(%d)", return_code); k_panic(); #endif } @@ -241,6 +238,8 @@ XTENSA_ERR_NORET __weak void _SysFatalErrorHandler(unsigned int reason, { ARG_UNUSED(pEsf); + LOG_PANIC(); + #if !defined(CONFIG_SIMPLE_FATAL_ERROR_HANDLER) #ifdef CONFIG_STACK_SENTINEL if (reason == _NANO_ERR_STACK_CHK_FAIL) { @@ -251,11 +250,11 @@ XTENSA_ERR_NORET __weak void _SysFatalErrorHandler(unsigned int reason, goto hang_system; } if (k_is_in_isr() || _is_thread_essential()) { - printk("Fatal fault in %s! Spinning...\n", + LOG_ERR("Fatal fault in %s! Spinning...", k_is_in_isr() ? "ISR" : "essential thread"); goto hang_system; } - printk("Fatal fault in thread %p! Aborting.\n", _current); + LOG_ERR("Fatal fault in thread %p! Aborting.", _current); k_thread_abort(_current); hang_system: