Optimise fault handler assembly

Fault handler assembly was very simplistic. Optimise for a 112 byte saving.
pull/12824/head
Kevin Bracey 2020-04-17 11:11:39 +03:00
parent c1641e79c5
commit 03ce9190f9
5 changed files with 131 additions and 248 deletions

View File

@ -40,29 +40,29 @@ FAULT_TYPE_USAGE_FAULT EQU 0x40
HardFault_Handler\ HardFault_Handler\
PROC PROC
EXPORT HardFault_Handler EXPORT HardFault_Handler
LDR R3,=FAULT_TYPE_HARD_FAULT MOVS R3,#FAULT_TYPE_HARD_FAULT
B Fault_Handler B Fault_Handler
ENDP ENDP
MemManage_Handler\ MemManage_Handler\
PROC PROC
EXPORT MemManage_Handler EXPORT MemManage_Handler
LDR R3,=FAULT_TYPE_MEMMANAGE_FAULT MOVS R3,#FAULT_TYPE_MEMMANAGE_FAULT
B Fault_Handler B Fault_Handler
ENDP ENDP
BusFault_Handler\ BusFault_Handler\
PROC PROC
EXPORT BusFault_Handler EXPORT BusFault_Handler
LDR R3,=FAULT_TYPE_BUS_FAULT MOVS R3,#FAULT_TYPE_BUS_FAULT
B Fault_Handler B Fault_Handler
ENDP ENDP
UsageFault_Handler\ UsageFault_Handler\
PROC PROC
EXPORT UsageFault_Handler EXPORT UsageFault_Handler
LDR R3,=FAULT_TYPE_USAGE_FAULT MOVS R3,#FAULT_TYPE_USAGE_FAULT
B Fault_Handler ; Fall into Fault_Handler
ENDP ENDP
Fault_Handler\ Fault_Handler\
@ -72,91 +72,52 @@ Fault_Handler\
IMPORT mbed_fault_context IMPORT mbed_fault_context
IMPORT mbed_fault_handler IMPORT mbed_fault_handler
MRS R0,MSP MOV R12,R3
LDR R1,=0x4 PUSH {R4-R7}
MOV R2,LR ADD R6,SP,#16
TST R2,R1 ; Check EXC_RETURN for bit 2 MOV R5,LR
BEQ Fault_Handler_Continue LSRS R0,R5,#3 ; Check EXC_RETURN for bit 2
MRS R0,PSP BCC Fault_Handler_Continue
MRS R6,PSP
Fault_Handler_Continue Fault_Handler_Continue
MOV R12,R3 LDR R7,=mbed_fault_context
LDR R3,=mbed_fault_context LDR R7,[R7]
LDR R1,[R3] LDMIA R6!,{R0-R3}
LDR R2,[R0] ; Capture R0 STMIA R7!,{R0-R3} ; Capture R0..R3
STR R2,[R1] POP {R0-R3}
ADDS R1,#4 STMIA R7!,{R0-R3} ; Capture R4..R7
LDR R2,[R0,#4] ; Capture R1 MOV R0,R8
STR R2,[R1] MOV R1,R9
ADDS R1,#4 MOV R2,R10
LDR R2,[R0,#8] ; Capture R2 MOV R3,R11
STR R2,[R1] STMIA R7!,{R0-R3} ; Capture R8..R11
ADDS R1,#4 LDMIA R6!,{R0,R2-R4} ; Load R12,LR,PC,xPSR
LDR R2,[R0,#12] ; Capture R3 ; Adjust stack pointer to its original value
STR R2,[R1] MOVS R1,R6
ADDS R1,#4 LSRS R6,R4,#10 ; Check for if STK was aligned by checking bit-9 in xPSR value
STMIA R1!,{R4-R7} ; Capture R4..R7 BCC Fault_Handler_Continue1
MOV R7,R8 ; Capture R8 ADDS R1,#0x4
STR R7,[R1]
ADDS R1,#4
MOV R7,R9 ; Capture R9
STR R7,[R1]
ADDS R1,#4
MOV R7,R10 ; Capture R10
STR R7,[R1]
ADDS R1,#4
MOV R7,R11 ; Capture R11
STR R7,[R1]
ADDS R1,#4
LDR R2,[R0,#16] ; Capture R12
STR R2,[R1]
ADDS R1,#8 ; Add 8 here to capture LR next, we will capture SP later
LDR R2,[R0,#20] ; Capture LR
STR R2,[R1]
ADDS R1,#4
LDR R2,[R0,#24] ; Capture PC
STR R2,[R1]
ADDS R1,#4
LDR R2,[R0,#28] ; Capture xPSR
STR R2,[R1]
ADDS R1,#4
; Adjust stack pointer to its original value and capture it
MOV R3,R0
ADDS R3,#0x20 ; Add 0x20 to get the SP value prior to exception
LDR R6,=0x200
TST R2,R6 ; Check for if STK was aligned by checking bit-9 in xPSR value
BEQ Fault_Handler_Continue1
ADDS R3,#0x4
Fault_Handler_Continue1 Fault_Handler_Continue1
MOV R5,LR LSRS R6,R5,#5 ; Check EXC_RETURN bit-4 to see if FP context was saved
LDR R6,=0x10 ; Check for bit-4 to see if FP context was saved BCS Fault_Handler_Continue2
TST R5,R6 ADDS R1,#0x48 ; 16 FP regs + FPCSR + 1 Reserved
BNE Fault_Handler_Continue2
ADDS R3,#0x48 ; 16 FP regs + FPCSR + 1 Reserved
Fault_Handler_Continue2 Fault_Handler_Continue2
MOV R4,R1 STMIA R7!,{R0-R4} ; Capture R12,SP,LR,PC,xPSR
SUBS R4,#0x10 ; Set the location of SP in ctx MRS R0,PSP
STR R3,[R4] ; Capture the adjusted SP MOV R1,SP
MRS R2,PSP ; Get PSP MRS R6,CONTROL
STR R2,[R1] STMIA R7!,{R0,R1,R5,R6} ; Capture PSP,MSP,EXC_RETURN,CONTROL
ADDS R1,#4
MRS R2,MSP ; Get MSP
STR R2,[R1]
ADDS R1,#4
MOV R2,LR ; Get current LR(EXC_RETURN)
STR R2,[R1]
ADDS R1,#4
MRS R2,CONTROL ; Get CONTROL Reg
STR R2,[R1]
MOV R0,R12 MOV R0,R12
LDR R3,=mbed_fault_context MOVS R1,R7
LDR R1,[R3] SUBS R1,#21*4
BL mbed_fault_handler BL mbed_fault_handler ; mbed_fault_handler does not return
#else
B .
#endif #endif
B . ; Just in case we come back here
ENDP ENDP
ALIGN
#endif #endif
END END

View File

@ -47,7 +47,7 @@
.cantunwind .cantunwind
HardFault_Handler: HardFault_Handler:
LDR R3,=FAULT_TYPE_HARD_FAULT MOVS R3,#FAULT_TYPE_HARD_FAULT
B Fault_Handler B Fault_Handler
.fnend .fnend
@ -61,7 +61,7 @@ HardFault_Handler:
.cantunwind .cantunwind
MemManage_Handler: MemManage_Handler:
LDR R3,=FAULT_TYPE_MEMMANAGE_FAULT MOVS R3,#FAULT_TYPE_MEMMANAGE_FAULT
B Fault_Handler B Fault_Handler
.fnend .fnend
@ -75,7 +75,7 @@ MemManage_Handler:
.cantunwind .cantunwind
BusFault_Handler: BusFault_Handler:
LDR R3,=FAULT_TYPE_BUS_FAULT MOVS R3,#FAULT_TYPE_BUS_FAULT
B Fault_Handler B Fault_Handler
.fnend .fnend
@ -89,8 +89,8 @@ BusFault_Handler:
.cantunwind .cantunwind
UsageFault_Handler: UsageFault_Handler:
LDR R3,=FAULT_TYPE_USAGE_FAULT MOVS R3,#FAULT_TYPE_USAGE_FAULT
B Fault_Handler // Fall into Fault_Handler
.fnend .fnend
.size UsageFault_Handler, .-UsageFault_Handler .size UsageFault_Handler, .-UsageFault_Handler
@ -104,94 +104,55 @@ UsageFault_Handler:
Fault_Handler: Fault_Handler:
#if (DOMAIN_NS == 1) #if (DOMAIN_NS == 1)
MRS R0,MSP MOV R12,R3
LDR R1,=0x4 PUSH {R4-R7}
MOV R2,LR ADD R6,SP,#16
TST R2,R1 // Check EXC_RETURN for bit 2 MOV R5,LR
BEQ Fault_Handler_Continue LSRS R0,R5,#3 // Check EXC_RETURN for bit 2
MRS R0,PSP BCC Fault_Handler_Continue
MRS R6,PSP
Fault_Handler_Continue: Fault_Handler_Continue:
MOV R12,R3 LDR R7,=mbed_fault_context
LDR R3,=mbed_fault_context LDR R7,[R7]
LDR R1,[R3] LDMIA R6!,{R0-R3}
LDR R2,[R0] // Capture R0 STMIA R7!,{R0-R3} // Capture R0..R3
STR R2,[R1] POP {R0-R3}
ADDS R1,#4 STMIA R7!,{R0-R3} // Capture R4..R7
LDR R2,[R0,#4] // Capture R1 MOV R0,R8
STR R2,[R1] MOV R1,R9
ADDS R1,#4 MOV R2,R10
LDR R2,[R0,#8] // Capture R2 MOV R3,R11
STR R2,[R1] STMIA R7!,{R0-R3} // Capture R8..R11
ADDS R1,#4 LDMIA R6!,{R0,R2-R4} // Load R12,LR,PC,xPSR
LDR R2,[R0,#12] // Capture R3 // Adjust stack pointer to its original value
STR R2,[R1] MOVS R1,R6
ADDS R1,#4 LSRS R6,R4,#10 // Check for if STK was aligned by checking bit-9 in xPSR value
STMIA R1!,{R4-R7} // Capture R4..R7 BCC Fault_Handler_Continue1
MOV R7,R8 // Capture R8 ADDS R1,#0x4
STR R7,[R1]
ADDS R1,#4
MOV R7,R9 // Capture R9
STR R7,[R1]
ADDS R1,#4
MOV R7,R10 // Capture R10
STR R7,[R1]
ADDS R1,#4
MOV R7,R11 // Capture R11
STR R7,[R1]
ADDS R1,#4
LDR R2,[R0,#16] // Capture R12
STR R2,[R1]
ADDS R1,#8 // Add 8 here to capture LR next, we will capture SP later
LDR R2,[R0,#20] // Capture LR
STR R2,[R1]
ADDS R1,#4
LDR R2,[R0,#24] // Capture PC
STR R2,[R1]
ADDS R1,#4
LDR R2,[R0,#28] // Capture xPSR
STR R2,[R1]
ADDS R1,#4
// Adjust stack pointer to its original value and capture it
MOV R3,R0
ADDS R3,#0x20 // Add 0x20 to get the SP value prior to exception
LDR R6,=0x200
TST R2,R6 // Check for if STK was aligned by checking bit-9 in xPSR value
BEQ Fault_Handler_Continue1
ADDS R3,#0x4
Fault_Handler_Continue1: Fault_Handler_Continue1:
MOV R5,LR LSRS R6,R5,#5 // Check EXC_RETURN bit-4 to see if FP context was saved
LDR R6,=0x10 // Check for bit-4 to see if FP context was saved BCS Fault_Handler_Continue2
TST R5,R6 ADDS R1,#0x48 // 16 FP regs + FPCSR + 1 Reserved
BNE Fault_Handler_Continue2
ADDS R3,#0x48 // 16 FP regs + FPCSR + 1 Reserved
Fault_Handler_Continue2: Fault_Handler_Continue2:
MOV R4,R1 STMIA R7!,{R0-R4} // Capture R12,SP,LR,PC,xPSR
SUBS R4,#0x10 // Set the location of SP in ctx MRS R0,PSP
STR R3,[R4] // Capture the adjusted SP MOV R1,SP
MRS R2,PSP // Get PSP MRS R6,CONTROL
STR R2,[R1] STMIA R7!,{R0,R1,R5,R6} // Capture PSP,MSP,EXC_RETURN,CONTROL
ADDS R1,#4
MRS R2,MSP // Get MSP
STR R2,[R1]
ADDS R1,#4
MOV R2,LR // Get current LR(EXC_RETURN)
STR R2,[R1]
ADDS R1,#4
MRS R2,CONTROL // Get CONTROL Reg
STR R2,[R1]
MOV R0,R12 MOV R0,R12
LDR R3,=mbed_fault_context MOV R1,R7
LDR R1,[R3] SUBS R1,R1,#21*4
BL mbed_fault_handler BL mbed_fault_handler // mbed_fault_handler does not return
#else
B .
#endif #endif
B . // Just in case we come back here
.fnend .fnend
.size Fault_Handler, .-Fault_Handler .size Fault_Handler, .-Fault_Handler
.align
#endif #endif
.end .end

View File

@ -42,23 +42,23 @@ FAULT_TYPE_USAGE_FAULT EQU 0x40
HardFault_Handler HardFault_Handler
EXPORT HardFault_Handler EXPORT HardFault_Handler
LDR R3,=FAULT_TYPE_HARD_FAULT MOVS R3,#FAULT_TYPE_HARD_FAULT
B Fault_Handler B Fault_Handler
MemManage_Handler MemManage_Handler
EXPORT MemManage_Handler EXPORT MemManage_Handler
LDR R3,=FAULT_TYPE_MEMMANAGE_FAULT MOVS R3,#FAULT_TYPE_MEMMANAGE_FAULT
B Fault_Handler B Fault_Handler
BusFault_Handler BusFault_Handler
EXPORT BusFault_Handler EXPORT BusFault_Handler
LDR R3,=FAULT_TYPE_BUS_FAULT MOVS R3,#FAULT_TYPE_BUS_FAULT
B Fault_Handler B Fault_Handler
UsageFault_Handler UsageFault_Handler
EXPORT UsageFault_Handler EXPORT UsageFault_Handler
LDR R3,=FAULT_TYPE_USAGE_FAULT MOVS R3,#FAULT_TYPE_USAGE_FAULT
B Fault_Handler ; Fall into Fault_Handler
Fault_Handler Fault_Handler
EXPORT Fault_Handler EXPORT Fault_Handler
@ -66,90 +66,51 @@ Fault_Handler
IMPORT mbed_fault_context IMPORT mbed_fault_context
IMPORT mbed_fault_handler IMPORT mbed_fault_handler
MRS R0,MSP MOV R12,R3
LDR R1,=0x4 PUSH {R4-R7}
MOV R2,LR ADD R6,SP,#16
TST R2,R1 ; Check EXC_RETURN for bit 2 MOV R5,LR
BEQ Fault_Handler_Continue LSRS R0,R5,#3 ; Check EXC_RETURN for bit 2
MRS R0,PSP BCC Fault_Handler_Continue
MRS R6,PSP
Fault_Handler_Continue Fault_Handler_Continue
MOV R12,R3 LDR R7,=mbed_fault_context
LDR R3,=mbed_fault_context LDR R7,[R7]
LDR R1,[R3] LDMIA R6!,{R0-R3}
LDR R2,[R0] ; Capture R0 STMIA R7!,{R0-R3} ; Capture R0..R3
STR R2,[R1] POP {R0-R3}
ADDS R1,#4 STMIA R7!,{R0-R3} ; Capture R4..R7
LDR R2,[R0,#4] ; Capture R1 MOV R0,R8
STR R2,[R1] MOV R1,R9
ADDS R1,#4 MOV R2,R10
LDR R2,[R0,#8] ; Capture R2 MOV R3,R11
STR R2,[R1] STMIA R7!,{R0-R3} ; Capture R8..R11
ADDS R1,#4 LDMIA R6!,{R0,R2-R4} ; Load R12,LR,PC,xPSR
LDR R2,[R0,#12] ; Capture R3 ; Adjust stack pointer to its original value
STR R2,[R1] MOVS R1,R6
ADDS R1,#4 LSRS R6,R4,#10 ; Check for if STK was aligned by checking bit-9 in xPSR value
STMIA R1!,{R4-R7} ; Capture R4..R7 BCC Fault_Handler_Continue1
MOV R7,R8 ; Capture R8 ADDS R1,#0x4
STR R7,[R1]
ADDS R1,#4
MOV R7,R9 ; Capture R9
STR R7,[R1]
ADDS R1,#4
MOV R7,R10 ; Capture R10
STR R7,[R1]
ADDS R1,#4
MOV R7,R11 ; Capture R11
STR R7,[R1]
ADDS R1,#4
LDR R2,[R0,#16] ; Capture R12
STR R2,[R1]
ADDS R1,#8 ; Add 8 here to capture LR next, we will capture SP later
LDR R2,[R0,#20] ; Capture LR
STR R2,[R1]
ADDS R1,#4
LDR R2,[R0,#24] ; Capture PC
STR R2,[R1]
ADDS R1,#4
LDR R2,[R0,#28] ; Capture xPSR
STR R2,[R1]
ADDS R1,#4
; Adjust stack pointer to its original value and capture it
MOV R3,R0
ADDS R3,#0x20 ; Add 0x20 to get the SP value prior to exception
LDR R6,=0x200
TST R2,R6 ; Check for if STK was aligned by checking bit-9 in xPSR value
BEQ Fault_Handler_Continue1
ADDS R3,#0x4
Fault_Handler_Continue1 Fault_Handler_Continue1
MOV R5,LR LSRS R6,R5,#5 ; Check EXC_RETURN bit-4 to see if FP context was saved
LDR R6,=0x10 ; Check for bit-4 to see if FP context was saved BCS Fault_Handler_Continue2
TST R5,R6 ADDS R1,#0x48 ; 16 FP regs + FPCSR + 1 Reserved
BNE Fault_Handler_Continue2
ADDS R3,#0x48 ; 16 FP regs + FPCSR + 1 Reserved
Fault_Handler_Continue2 Fault_Handler_Continue2
MOV R4,R1 STMIA R7!,{R0-R4} ; Capture R12,SP,LR,PC,xPSR
SUBS R4,#0x10 ; Set the location of SP in ctx MRS R0,PSP
STR R3,[R4] ; Capture the adjusted SP MOV R1,SP
MRS R2,PSP ; Get PSP MRS R6,CONTROL
STR R2,[R1] STMIA R7!,{R0,R1,R5,R6} ; Capture PSP,MSP,EXC_RETURN,CONTROL
ADDS R1,#4
MRS R2,MSP ; Get MSP
STR R2,[R1]
ADDS R1,#4
MOV R2,LR ; Get current LR(EXC_RETURN)
STR R2,[R1]
ADDS R1,#4
MRS R2,CONTROL ; Get CONTROL Reg
STR R2,[R1]
MOV R0,R12 MOV R0,R12
LDR R3,=mbed_fault_context MOVS R1,R7
LDR R1,[R3] SUBS R1,#21*4
BL mbed_fault_handler BL mbed_fault_handler ; mbed_fault_handler does not return
#else
B .
#endif #endif
B . ; Just in case we come back here ALIGN
#endif #endif
END END

View File

@ -45,7 +45,7 @@ extern bool mbed_error_in_progress;
//This is a handler function called from Fault handler to print the error information out. //This is a handler function called from Fault handler to print the error information out.
//This runs in fault context and uses special functions(defined in mbed_rtx_fault_handler.c) to print the information without using C-lib support. //This runs in fault context and uses special functions(defined in mbed_rtx_fault_handler.c) to print the information without using C-lib support.
void mbed_fault_handler(uint32_t fault_type, const mbed_fault_context_t *mbed_fault_context_in) MBED_NORETURN void mbed_fault_handler(uint32_t fault_type, const mbed_fault_context_t *mbed_fault_context_in)
{ {
mbed_error_status_t faultStatus = MBED_SUCCESS; mbed_error_status_t faultStatus = MBED_SUCCESS;

View File

@ -58,7 +58,7 @@ typedef struct {
//This is a handler function called from Fault handler to print the error information out. //This is a handler function called from Fault handler to print the error information out.
//This runs in fault context and uses special functions(defined in mbed_fault_handler.c) to print the information without using C-lib support. //This runs in fault context and uses special functions(defined in mbed_fault_handler.c) to print the information without using C-lib support.
void mbed_fault_handler(uint32_t fault_type, const mbed_fault_context_t *mbed_fault_context_in); MBED_NORETURN void mbed_fault_handler(uint32_t fault_type, const mbed_fault_context_t *mbed_fault_context_in);
/** /**
* Call this function to retrieve the fault context after a fatal exception which triggered a system reboot. The function retrieves the fault context stored in crash-report ram area which is preserved over reboot. * Call this function to retrieve the fault context after a fatal exception which triggered a system reboot. The function retrieves the fault context stored in crash-report ram area which is preserved over reboot.