1/* mbed Microcontroller Library 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#ifdef CFG_FHDLR 25 26 .file "exception.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 49HardFault_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 63MemManage_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 77BusFault_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 91UsageFault_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 105Fault_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 114Fault_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 162Fault_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 169Fault_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 MOV R2,LR // Get current LR(EXC_RETURN) 180 STR R2,[R1] 181 ADDS R1,#4 182 MRS R2,CONTROL // Get CONTROL Reg 183 STR R2,[R1] 184 LDR R3,=mbed_fault_handler // Load address of mbedFaultHandler 185 MOV R0,R12 186 LDR R1,=mbed_fault_context 187 BLX R3 188#endif 189 B . // Just in case we come back here 190 191 .fnend 192 .size Fault_Handler, .-Fault_Handler 193 194#endif 195 196 .end 197 198 199