From 72f45b83e08b5188d7f0a061966fb0f28295e195 Mon Sep 17 00:00:00 2001 From: Senthil Ramakrishnan Date: Mon, 5 Mar 2018 15:30:01 -0600 Subject: [PATCH] Add mode and privilege info to crash dump --- .../TARGET_CORTEX_M/TOOLCHAIN_ARM/except.S | 9 ++++++-- .../TARGET_CORTEX_M/TOOLCHAIN_GCC/except.S | 7 +++++- .../TARGET_CORTEX_M/TOOLCHAIN_IAR/except.S | 11 +++++++--- .../TARGET_CORTEX_M/mbed_rtx_fault_handler.c | 22 ++++++++++++++++++- .../TARGET_CORTEX_M/mbed_rtx_fault_handler.h | 2 ++ 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_ARM/except.S b/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_ARM/except.S index c1b46e054c..036663ca34 100644 --- a/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_ARM/except.S +++ b/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_ARM/except.S @@ -144,13 +144,18 @@ Fault_Handler_Continue2 MRS R2,MSP ; Get MSP STR R2,[R1] ADDS R1,#4 - LDR R3,=mbed_fault_handler ; Load address of mbedFaultHandler + MOV R2,LR ; Get current LR(EXC_RETURN) + STR R2,[R1] + ADDS R1,#4 + MRS R2,CONTROL ; Get CONTROL Reg + STR R2,[R1] + LDR R3,=mbed_fault_handler ; Load address of mbedFaultHandler MOV R0,R12 LDR R1,=mbed_fault_context LDR R2,=osRtxInfo BLX R3 #endif - B . ; Just in case we come back here + B . ; Just in case we come back here ENDP #endif diff --git a/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_GCC/except.S b/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_GCC/except.S index f6f081007a..94937384b1 100644 --- a/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_GCC/except.S +++ b/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_GCC/except.S @@ -176,7 +176,12 @@ Fault_Handler_Continue2: MRS R2,MSP // Get MSP STR R2,[R1] ADDS R1,#4 - LDR R3,=mbed_fault_handler // Load address of mbedFaultHandler + MOV R2,LR // Get current LR(EXC_RETURN) + STR R2,[R1] + ADDS R1,#4 + MRS R2,CONTROL // Get CONTROL Reg + STR R2,[R1] + LDR R3,=mbed_fault_handler // Load address of mbedFaultHandler MOV R0,R12 LDR R1,=mbed_fault_context LDR R2,=osRtxInfo diff --git a/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_IAR/except.S b/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_IAR/except.S index 1e5b5a6dbb..7b85e25a98 100644 --- a/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_IAR/except.S +++ b/rtos/TARGET_CORTEX/TARGET_CORTEX_M/TOOLCHAIN_IAR/except.S @@ -139,13 +139,18 @@ Fault_Handler_Continue2 MRS R2,MSP ; Get MSP STR R2,[R1] ADDS R1,#4 - LDR R3,=mbed_fault_handler ; Load address of mbedFaultHandler + MOV R2,LR ; Get current LR(EXC_RETURN) + STR R2,[R1] + ADDS R1,#4 + MRS R2,CONTROL ; Get CONTROL Reg + STR R2,[R1] + LDR R3,=mbed_fault_handler ; Load address of mbedFaultHandler MOV R0,R12 LDR R1,=mbed_fault_context LDR R2,=osRtxInfo BLX R3 #endif - B . ; Just in case we come back here -#endif ; #if (MBED_FAULT_HANDLER_SUPPORT == 1) + B . ; Just in case we come back here +#endif ; #if (MBED_FAULT_HANDLER_SUPPORT == 1) END diff --git a/rtos/TARGET_CORTEX/TARGET_CORTEX_M/mbed_rtx_fault_handler.c b/rtos/TARGET_CORTEX/TARGET_CORTEX_M/mbed_rtx_fault_handler.c index 0fa7321530..4cb3762b1d 100644 --- a/rtos/TARGET_CORTEX/TARGET_CORTEX_M/mbed_rtx_fault_handler.c +++ b/rtos/TARGET_CORTEX/TARGET_CORTEX_M/mbed_rtx_fault_handler.c @@ -143,7 +143,27 @@ void print_context_info() fault_print_str("\nBFAR : %",(uint32_t *)&SCB->BFAR); } #endif - + //Print Mode + if(mbed_fault_context.EXC_RETURN & 0x8) { + fault_print_str("\nMode : Thread", NULL); + //Print Priv level in Thread mode - We capture CONTROL reg which reflects the privilege. + //Note that the CONTROL register captured still reflects the privilege status of the + //thread mode eventhough we are in Handler mode by the time we capture it. + if(mbed_fault_context.CONTROL & 0x1) { + fault_print_str("\nPriv : User", NULL); + } else { + fault_print_str("\nPriv : Privileged", NULL); + } + } else { + fault_print_str("\nMode : Handler", NULL); + fault_print_str("\nPriv : Privileged", NULL); + } + //Print Return Stack + if(mbed_fault_context.EXC_RETURN & 0x4) { + fault_print_str("\nStack: PSP", NULL); + } else { + fault_print_str("\nStack: MSP", NULL); + } } /* Prints thread info from a list */ diff --git a/rtos/TARGET_CORTEX/TARGET_CORTEX_M/mbed_rtx_fault_handler.h b/rtos/TARGET_CORTEX/TARGET_CORTEX_M/mbed_rtx_fault_handler.h index 75628876d0..54c85c736f 100644 --- a/rtos/TARGET_CORTEX/TARGET_CORTEX_M/mbed_rtx_fault_handler.h +++ b/rtos/TARGET_CORTEX/TARGET_CORTEX_M/mbed_rtx_fault_handler.h @@ -37,6 +37,8 @@ typedef struct { uint32_t xPSR; uint32_t PSP; uint32_t MSP; + uint32_t EXC_RETURN; + uint32_t CONTROL; } mbed_fault_context_t; //Fault type definitions