Skip to content

Commit 29348d8

Browse files
committed
Support for generating core/register/thread-info dump on fault exceptions
1 parent e0d79ff commit 29348d8

File tree

7 files changed

+1102
-0
lines changed

7 files changed

+1102
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
25+
IF :LNOT::DEF:__DOMAIN_NS
26+
__DOMAIN_NS EQU 1
27+
ENDIF
28+
29+
FAULT_TYPE_HARD_FAULT EQU 0x10
30+
FAULT_TYPE_MEMMANAGE_FAULT EQU 0x20
31+
FAULT_TYPE_BUS_FAULT EQU 0x30
32+
FAULT_TYPE_USAGE_FAULT EQU 0x40
33+
34+
PRESERVE8
35+
THUMB
36+
37+
AREA |.text|, CODE, READONLY
38+
39+
HardFault_Handler\
40+
PROC
41+
EXPORT HardFault_Handler
42+
LDR R3,=FAULT_TYPE_HARD_FAULT
43+
B Fault_Handler
44+
ENDP
45+
46+
MemManage_Handler\
47+
PROC
48+
EXPORT MemManage_Handler
49+
LDR R3,=FAULT_TYPE_MEMMANAGE_FAULT
50+
B Fault_Handler
51+
ENDP
52+
53+
BusFault_Handler\
54+
PROC
55+
EXPORT BusFault_Handler
56+
LDR R3,=FAULT_TYPE_BUS_FAULT
57+
B Fault_Handler
58+
ENDP
59+
60+
UsageFault_Handler\
61+
PROC
62+
EXPORT UsageFault_Handler
63+
LDR R3,=FAULT_TYPE_USAGE_FAULT
64+
B Fault_Handler
65+
ENDP
66+
67+
Fault_Handler PROC
68+
EXPORT Fault_Handler
69+
IF __DOMAIN_NS = 1
70+
IMPORT osRtxInfo
71+
IMPORT mbed_fault_handler
72+
IMPORT mbed_fault_context
73+
74+
MRS R0,MSP
75+
LDR R1,=0x4
76+
MOV R2,LR
77+
TST R2,R1 ; Check EXC_RETURN for bit 2
78+
BEQ Fault_Handler_Continue
79+
MRS R0,PSP
80+
81+
Fault_Handler_Continue
82+
MOV R12,R3
83+
LDR R1,=mbed_fault_context
84+
LDR R2,[R0] ; Capture R0
85+
STR R2,[R1]
86+
ADDS R1,#4
87+
LDR R2,[R0,#4] ; Capture R1
88+
STR R2,[R1]
89+
ADDS R1,#4
90+
LDR R2,[R0,#8] ; Capture R2
91+
STR R2,[R1]
92+
ADDS R1,#4
93+
LDR R2,[R0,#12] ; Capture R3
94+
STR R2,[R1]
95+
ADDS R1,#4
96+
STMIA R1!,{R4-R7} ; Capture R4..R7
97+
MOV R7,R8 ; Capture R8
98+
STR R7,[R1]
99+
ADDS R1,#4
100+
MOV R7,R9 ; Capture R9
101+
STR R7,[R1]
102+
ADDS R1,#4
103+
MOV R7,R10 ; Capture R10
104+
STR R7,[R1]
105+
ADDS R1,#4
106+
MOV R7,R11 ; Capture R11
107+
STR R7,[R1]
108+
ADDS R1,#4
109+
LDR R2,[R0,#16] ; Capture R12
110+
STR R2,[R1]
111+
ADDS R1,#8 ; Add 8 here to capture LR next, we will capture SP later
112+
LDR R2,[R0,#20] ; Capture LR
113+
STR R2,[R1]
114+
ADDS R1,#4
115+
LDR R2,[R0,#24] ; Capture PC
116+
STR R2,[R1]
117+
ADDS R1,#4
118+
LDR R2,[R0,#28] ; Capture xPSR
119+
STR R2,[R1]
120+
ADDS R1,#4
121+
; Adjust stack pointer to its original value and capture it
122+
MOV R3,R0
123+
ADDS R3,#0x20 ; Add 0x20 to get the SP value prior to exception
124+
LDR R6,=0x200
125+
TST R2,R6 ; Check for if STK was aligned by checking bit-9 in xPSR value
126+
BEQ Fault_Handler_Continue1
127+
ADDS R3,#0x4
128+
129+
Fault_Handler_Continue1
130+
MOV R5,LR
131+
LDR R6,=0x10 ; Check for bit-4 to see if FP context was saved
132+
TST R5,R6
133+
BNE Fault_Handler_Continue2
134+
ADDS R3,#0x48 ; 16 FP regs + FPCSR + 1 Reserved
135+
136+
Fault_Handler_Continue2
137+
MOV R4,R1
138+
SUBS R4,#0x10 ; Set the location of SP in ctx
139+
STR R3,[R4] ; Capture the adjusted SP
140+
MRS R2,PSP ; Get PSP
141+
STR R2,[R1]
142+
ADDS R1,#4
143+
MRS R2,MSP ; Get MSP
144+
STR R2,[R1]
145+
ADDS R1,#4
146+
LDR R3,=mbed_fault_handler ; Load address of mbedFaultHandler
147+
MOV R0,R12
148+
LDR R1,=mbed_fault_context
149+
LDR R2,=osRtxInfo
150+
BLX R3
151+
ENDIF
152+
B . ; Just in case we come back here
153+
ENDP
154+
155+
END
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
25+
26+
.file "except_cm0.S"
27+
.syntax unified
28+
29+
.ifndef __DOMAIN_NS
30+
.equ __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+
.end

0 commit comments

Comments
 (0)