Skip to content

Commit 84f42f6

Browse files
authored
Merge pull request #5847 from SenRamakri/sen_FaultHandler
Unify fault handling and add script to show faults
2 parents e0d79ff + 19ad4e2 commit 84f42f6

File tree

14 files changed

+1064
-48
lines changed

14 files changed

+1064
-48
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
;/*
2+
; * Copyright (c) 2014-2018 ARM Limited. All rights reserved.
3+
; *
4+
; * SPDX-License-Identifier: Apache-2.0
5+
; *
6+
; * Licensed under the Apache License, Version 2.0 (the License); you may
7+
; * not use this file except in compliance with the License.
8+
; * You may obtain a copy of the License at
9+
; *
10+
; * www.apache.org/licenses/LICENSE-2.0
11+
; *
12+
; * Unless required by applicable law or agreed to in writing, software
13+
; * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14+
; * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
; * See the License for the specific language governing permissions and
16+
; * limitations under the License.
17+
; *
18+
; * -----------------------------------------------------------------------------
19+
; *
20+
; * Title: Cortex-M Fault Exception handlers ( Common for both ARMv7M and ARMV6M )
21+
; *
22+
; * -----------------------------------------------------------------------------
23+
; */
24+
#ifndef MBED_FAULT_HANDLER_DISABLED
25+
26+
#ifndef __DOMAIN_NS
27+
#define __DOMAIN_NS 1
28+
#endif
29+
30+
FAULT_TYPE_HARD_FAULT EQU 0x10
31+
FAULT_TYPE_MEMMANAGE_FAULT EQU 0x20
32+
FAULT_TYPE_BUS_FAULT EQU 0x30
33+
FAULT_TYPE_USAGE_FAULT EQU 0x40
34+
35+
PRESERVE8
36+
THUMB
37+
38+
AREA |.text|, CODE, READONLY
39+
40+
HardFault_Handler\
41+
PROC
42+
EXPORT HardFault_Handler
43+
LDR R3,=FAULT_TYPE_HARD_FAULT
44+
B Fault_Handler
45+
ENDP
46+
47+
MemManage_Handler\
48+
PROC
49+
EXPORT MemManage_Handler
50+
LDR R3,=FAULT_TYPE_MEMMANAGE_FAULT
51+
B Fault_Handler
52+
ENDP
53+
54+
BusFault_Handler\
55+
PROC
56+
EXPORT BusFault_Handler
57+
LDR R3,=FAULT_TYPE_BUS_FAULT
58+
B Fault_Handler
59+
ENDP
60+
61+
UsageFault_Handler\
62+
PROC
63+
EXPORT UsageFault_Handler
64+
LDR R3,=FAULT_TYPE_USAGE_FAULT
65+
B Fault_Handler
66+
ENDP
67+
68+
Fault_Handler PROC
69+
EXPORT Fault_Handler
70+
#if (__DOMAIN_NS == 1)
71+
IMPORT osRtxInfo
72+
IMPORT mbed_fault_handler
73+
IMPORT mbed_fault_context
74+
75+
MRS R0,MSP
76+
LDR R1,=0x4
77+
MOV R2,LR
78+
TST R2,R1 ; Check EXC_RETURN for bit 2
79+
BEQ Fault_Handler_Continue
80+
MRS R0,PSP
81+
82+
Fault_Handler_Continue
83+
MOV R12,R3
84+
LDR R1,=mbed_fault_context
85+
LDR R2,[R0] ; Capture R0
86+
STR R2,[R1]
87+
ADDS R1,#4
88+
LDR R2,[R0,#4] ; Capture R1
89+
STR R2,[R1]
90+
ADDS R1,#4
91+
LDR R2,[R0,#8] ; Capture R2
92+
STR R2,[R1]
93+
ADDS R1,#4
94+
LDR R2,[R0,#12] ; Capture R3
95+
STR R2,[R1]
96+
ADDS R1,#4
97+
STMIA R1!,{R4-R7} ; Capture R4..R7
98+
MOV R7,R8 ; Capture R8
99+
STR R7,[R1]
100+
ADDS R1,#4
101+
MOV R7,R9 ; Capture R9
102+
STR R7,[R1]
103+
ADDS R1,#4
104+
MOV R7,R10 ; Capture R10
105+
STR R7,[R1]
106+
ADDS R1,#4
107+
MOV R7,R11 ; Capture R11
108+
STR R7,[R1]
109+
ADDS R1,#4
110+
LDR R2,[R0,#16] ; Capture R12
111+
STR R2,[R1]
112+
ADDS R1,#8 ; Add 8 here to capture LR next, we will capture SP later
113+
LDR R2,[R0,#20] ; Capture LR
114+
STR R2,[R1]
115+
ADDS R1,#4
116+
LDR R2,[R0,#24] ; Capture PC
117+
STR R2,[R1]
118+
ADDS R1,#4
119+
LDR R2,[R0,#28] ; Capture xPSR
120+
STR R2,[R1]
121+
ADDS R1,#4
122+
; Adjust stack pointer to its original value and capture it
123+
MOV R3,R0
124+
ADDS R3,#0x20 ; Add 0x20 to get the SP value prior to exception
125+
LDR R6,=0x200
126+
TST R2,R6 ; Check for if STK was aligned by checking bit-9 in xPSR value
127+
BEQ Fault_Handler_Continue1
128+
ADDS R3,#0x4
129+
130+
Fault_Handler_Continue1
131+
MOV R5,LR
132+
LDR R6,=0x10 ; Check for bit-4 to see if FP context was saved
133+
TST R5,R6
134+
BNE Fault_Handler_Continue2
135+
ADDS R3,#0x48 ; 16 FP regs + FPCSR + 1 Reserved
136+
137+
Fault_Handler_Continue2
138+
MOV R4,R1
139+
SUBS R4,#0x10 ; Set the location of SP in ctx
140+
STR R3,[R4] ; Capture the adjusted SP
141+
MRS R2,PSP ; Get PSP
142+
STR R2,[R1]
143+
ADDS R1,#4
144+
MRS R2,MSP ; Get MSP
145+
STR R2,[R1]
146+
ADDS R1,#4
147+
LDR R3,=mbed_fault_handler ; Load address of mbedFaultHandler
148+
MOV R0,R12
149+
LDR R1,=mbed_fault_context
150+
LDR R2,=osRtxInfo
151+
BLX R3
152+
#endif
153+
B . ; Just in case we come back here
154+
ENDP
155+
156+
#endif
157+
158+
END
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/*
2+
* Copyright (c) 2014-2018 ARM Limited. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the License); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* -----------------------------------------------------------------------------
19+
*
20+
* Title: Cortex-M Fault Exception handlers ( Common for both ARMv7M and ARMV6M )
21+
*
22+
* -----------------------------------------------------------------------------
23+
*/
24+
#ifndef MBED_FAULT_HANDLER_DISABLED
25+
26+
.file "except.S"
27+
.syntax unified
28+
29+
#ifndef __DOMAIN_NS
30+
#define __DOMAIN_NS 1
31+
#endif
32+
33+
.equ FAULT_TYPE_HARD_FAULT, 0x10
34+
.equ FAULT_TYPE_MEMMANAGE_FAULT, 0x20
35+
.equ FAULT_TYPE_BUS_FAULT, 0x30
36+
.equ FAULT_TYPE_USAGE_FAULT, 0x40
37+
38+
.thumb
39+
.section ".text"
40+
.align 2
41+
42+
//HardFault_Handler
43+
.thumb_func
44+
.type HardFault_Handler, %function
45+
.global HardFault_Handler
46+
.fnstart
47+
.cantunwind
48+
49+
HardFault_Handler:
50+
LDR R3,=FAULT_TYPE_HARD_FAULT
51+
B Fault_Handler
52+
53+
.fnend
54+
.size HardFault_Handler, .-HardFault_Handler
55+
56+
//MemManage_Handler
57+
.thumb_func
58+
.type MemManage_Handler, %function
59+
.global MemManage_Handler
60+
.fnstart
61+
.cantunwind
62+
63+
MemManage_Handler:
64+
LDR R3,=FAULT_TYPE_MEMMANAGE_FAULT
65+
B Fault_Handler
66+
67+
.fnend
68+
.size MemManage_Handler, .-MemManage_Handler
69+
70+
//BusFault_Handler
71+
.thumb_func
72+
.type BusFault_Handler, %function
73+
.global BusFault_Handler
74+
.fnstart
75+
.cantunwind
76+
77+
BusFault_Handler:
78+
LDR R3,=FAULT_TYPE_BUS_FAULT
79+
B Fault_Handler
80+
81+
.fnend
82+
.size BusFault_Handler, .-BusFault_Handler
83+
84+
//UsageFault_Handler
85+
.thumb_func
86+
.type UsageFault_Handler, %function
87+
.global UsageFault_Handler
88+
.fnstart
89+
.cantunwind
90+
91+
UsageFault_Handler:
92+
LDR R3,=FAULT_TYPE_USAGE_FAULT
93+
B Fault_Handler
94+
95+
.fnend
96+
.size UsageFault_Handler, .-UsageFault_Handler
97+
98+
//Common Fault_Handler to capture the context
99+
.thumb_func
100+
.type Fault_Handler, %function
101+
.global Fault_Handler
102+
.fnstart
103+
.cantunwind
104+
105+
Fault_Handler:
106+
#if (__DOMAIN_NS == 1)
107+
MRS R0,MSP
108+
LDR R1,=0x4
109+
MOV R2,LR
110+
TST R2,R1 // Check EXC_RETURN for bit 2
111+
BEQ Fault_Handler_Continue
112+
MRS R0,PSP
113+
114+
Fault_Handler_Continue:
115+
MOV R12,R3
116+
LDR R1,=mbed_fault_context
117+
LDR R2,[R0] // Capture R0
118+
STR R2,[R1]
119+
ADDS R1,#4
120+
LDR R2,[R0,#4] // Capture R1
121+
STR R2,[R1]
122+
ADDS R1,#4
123+
LDR R2,[R0,#8] // Capture R2
124+
STR R2,[R1]
125+
ADDS R1,#4
126+
LDR R2,[R0,#12] // Capture R3
127+
STR R2,[R1]
128+
ADDS R1,#4
129+
STMIA R1!,{R4-R7} // Capture R4..R7
130+
MOV R7,R8 // Capture R8
131+
STR R7,[R1]
132+
ADDS R1,#4
133+
MOV R7,R9 // Capture R9
134+
STR R7,[R1]
135+
ADDS R1,#4
136+
MOV R7,R10 // Capture R10
137+
STR R7,[R1]
138+
ADDS R1,#4
139+
MOV R7,R11 // Capture R11
140+
STR R7,[R1]
141+
ADDS R1,#4
142+
LDR R2,[R0,#16] // Capture R12
143+
STR R2,[R1]
144+
ADDS R1,#8 // Add 8 here to capture LR next, we will capture SP later
145+
LDR R2,[R0,#20] // Capture LR
146+
STR R2,[R1]
147+
ADDS R1,#4
148+
LDR R2,[R0,#24] // Capture PC
149+
STR R2,[R1]
150+
ADDS R1,#4
151+
LDR R2,[R0,#28] // Capture xPSR
152+
STR R2,[R1]
153+
ADDS R1,#4
154+
// Adjust stack pointer to its original value and capture it
155+
MOV R3,R0
156+
ADDS R3,#0x20 // Add 0x20 to get the SP value prior to exception
157+
LDR R6,=0x200
158+
TST R2,R6 // Check for if STK was aligned by checking bit-9 in xPSR value
159+
BEQ Fault_Handler_Continue1
160+
ADDS R3,#0x4
161+
162+
Fault_Handler_Continue1:
163+
MOV R5,LR
164+
LDR R6,=0x10 // Check for bit-4 to see if FP context was saved
165+
TST R5,R6
166+
BNE Fault_Handler_Continue2
167+
ADDS R3,#0x48 // 16 FP regs + FPCSR + 1 Reserved
168+
169+
Fault_Handler_Continue2:
170+
MOV R4,R1
171+
SUBS R4,#0x10 // Set the location of SP in ctx
172+
STR R3,[R4] // Capture the adjusted SP
173+
MRS R2,PSP // Get PSP
174+
STR R2,[R1]
175+
ADDS R1,#4
176+
MRS R2,MSP // Get MSP
177+
STR R2,[R1]
178+
ADDS R1,#4
179+
LDR R3,=mbed_fault_handler // Load address of mbedFaultHandler
180+
MOV R0,R12
181+
LDR R1,=mbed_fault_context
182+
LDR R2,=osRtxInfo
183+
BLX R3
184+
#endif
185+
B . // Just in case we come back here
186+
187+
.fnend
188+
.size Fault_Handler, .-Fault_Handler
189+
190+
#endif
191+
192+
.end
193+
194+

0 commit comments

Comments
 (0)