RTOS - hard fault handler


AAPCS

    procedure call standard for ARM architecture


Ref: hard-fault-handler.pdf





Ref: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0337e/Cihcbadd.html 


可以先看 HFSR 是哪一種錯誤


Hard Fault Status Register

Use the Hard Fault Status Register (HFSR) to obtain information about events that activate the Hard Fault handler.
The register address, access type, and Reset state are:
Address
0xE000ED2C
Access
Read/write-one-to-clear
Reset state
0x00000000
The HFSR is a write-clear register. This means that writing a 1 to a bit clears that bit. Figure 8.19 shows the fields of the Hard Fault Status Register.
Figure 8.19. Hard Fault Status Register bit assignments
Table 8.24 describes the fields of the Hard Fault Status Register.
Table 8.24. Hard Fault Status Register bit assignments
BitsFieldFunction
[31]DEBUGEVT
This bit is set if there is a fault related to debug.
This is only possible when halting debug is not enabled. For monitor enabled debug, it only happens for BKPT when the current priority is higher than the monitor. When both halting and monitor debug are disabled, it only happens for debug events that are not ignored (minimally, BKPT). The Debug Fault Status Register is updated.
[30]FORCEDHard Fault activated because a Configurable Fault was received and cannot activate because of priority or because the Configurable Fault is disabled.The Hard Fault handler then has to read the other fault status registers to determine cause.
[29:2]-Reserved.
[1]VECTTBLThis bit is set if there is a fault because of vector table read on exception processing (Bus Fault). This case is always a Hard Fault. The return PC points to the pre-empted instruction.
[0]-Reserved.


--

如果是 configurable fault

Configurable Fault Status Registers

Use the three Configurable Fault Status Registers to obtain information about local faults. These registers include:
The flags in these registers indicate the causes of local faults. Multiple flags can be set if more than one fault occurs. These register are read/write-clear. This means that they can be read normally, but writing a 1 to any bit clears that bit.
The register addresses, access types, and Reset states are:
Address
0xE000ED28 Memory Manage Fault Status Register
0xE000ED29 Bus Fault Status Register
0xE000ED2A Usage Fault Status Register
Access
Read/write-one-to-clear
Reset state
0x00000000
Figure 8.15 shows the fields of the Configurable Fault Status Registers.
Figure 8.15. Configurable Fault Status Registers bit assignments
Memory Manage Fault Status Register
The flags in the Memory Manage Fault Status Register indicate the cause of memory access faults.
The register address, access type, and Reset state are:
Address
0xE000ED28
Access
Read/write-one-to-clear
Reset state
0x00000000
Figure 8.16 shows the fields of the Memory Manage Fault Status Register.
Figure 8.16. Memory Manage Fault Register bit assignments
Table 8.21 describes the fields of the Memory Manage Fault Status Register.
Table 8.21. Memory Manage Fault Status Register bit assignments
BitsFieldFunction
[7]MMARVALID
Memory Manage Address Register (MMAR) address valid flag:
1 = valid fault address in MMAR. A later-arriving fault, such as a bus fault, can clear a memory manage fault.
0 = no valid fault address in MMAR.
If a MemManage fault occurs that is escalated to a Hard Fault because of priority, the Hard Fault handler must clear this bit. This prevents problems on return to a stacked active MemManage handler whose MMAR value has been overwritten.
[4]MSTKERRStacking from exception has caused one or more access violations. The SP is still adjusted and the values in the context area on the stack might be incorrect. The MMAR is not written.
[3]MUNSTKERRUnstack from exception return has caused one or more access violations. This is chained to the handler, so that the original return stack is still present. SP is not adjusted from failing return and new save is not performed. The MMAR is not written.
[1]DACCVIOLData access violation flag. Attempting to load or store at a location that does not permit the operation sets the DACCVIOL flag. The return PC points to the faulting instruction. This error loads MMAR with the address of the attempted access.
[0]IACCVIOLInstruction access violation flag. Attempting to fetch an instruction from a location that does not permit execution sets the IACCVIOL flag. This occurs on any access to an XN region, even when the MPU is disabled or not present. The return PC points to the faulting instruction. The MMAR is not written.
Bus Fault Status Register
The flags in the Bus Fault Status Register indicate the cause of bus access faults.
The register address, access type, and Reset state are:
Address
0xE000ED29
Access
Read/write-one-to-clear
Reset state
0x00000000
Figure 8.17 shows the fields of the Bus Fault Status Register.
Figure 8.17. Bus Fault Status Register bit assignments
Table 8.22 describes the fields of the Bus Fault Status Register.
Table 8.22. Bus Fault Status Register bit assignments
BitsFieldFunction
[7]BFARVALID
This bit is set if the Bus Fault Address Register (BFAR) contains a valid address. This is true after a bus fault where the address is known. Other faults can clear this bit, such as a Mem Manage fault occurring later.
If a Bus fault occurs that is escalated to a Hard Fault because of priority, the Hard Fault handler must clear this bit. This prevents problems if returning to a stacked active Bus fault handler whose BFAR value has been overwritten.
[6:5]-Reserved.
[4]STKERRStacking from exception has caused one or more bus faults. The SP is still adjusted and the values in the context area on the stack might be incorrect. The BFAR is not written.
[3]UNSTKERRUnstack from exception return has caused one or more bus faults. This is chained to the handler, so that the original return stack is still present. SP is not adjusted from failing return and new save is not performed. The BFAR is not written.
[2]IMPRECISERRImprecise data bus error. It is a BusFault, but the Return PC is not related to the causing instruction. This is not a synchronous fault. So, if detected when the priority of the current activation is higher than the Bus Fault, it only pends. Bus fault activates when returning to a lower priority activation. If a precise fault occurs before returning to a lower priority exception, the handler detects both IMPRECISERR set and one of the precise fault status bits set at the same time. The BFAR is not written.
[1]PRECISERRPrecise data bus error return.
[0]IBUSERR
Instruction bus error flag:
1 = instruction bus error
0 = no instruction bus error.
The IBUSERR flag is set by a prefetch error. The fault stops on the instruction, so if the error occurs under a branch shadow, no fault occurs. The BFAR is not written.
Usage Fault Status Register
The flags in the Usage Fault Status Register indicate the following errors:
  • illegal combination of EPSR and instruction
  • illegal PC load
  • illegal processor state
  • instruction decode error
  • attempt to use a coprocessor instruction
  • illegal unaligned access.
The register address, access type, and Reset state are:
Address
0xE000ED2B
Access
Read/write clear
Reset state
0x00000000
Figure 8.18 shows the fields of the Usage Fault Status Register.
Figure 8.18. Usage Fault Status Register bit assignments
Table 8.23 describes the fields of the Usage Fault Status Register.
Table 8.23. Usage Fault Status Register bit assignments
BitsFieldFunction
[9]DIVBYZEROWhen DIV_0_TRP (see Configuration Control Register) is enabled and an SDIV or UDIV instruction is used with a divisor of 0, this fault occurs The instruction is executed and the return PC points to it. If DIV_0_TRP is not set, then the divide returns a quotient of 0.
[8]UNALIGNEDWhen UNALIGN_TRP is enabled (see Configuration Control Register), and there is an attempt to make an unaligned memory access, then this fault occurs.Unaligned LDM/STM/LDRD/STRD instructions always fault irrespective of the setting of UNALIGN_TRP.
[7:4]-Reserved.
[3]NOCPAttempt to use a coprocessor instruction. The processor does not support coprocessor instructions.
[2]INVPCAttempt to load EXC_RETURN into PC illegally. Invalid instruction, invalid context, invalid value. The return PC points to the instruction that tried to set the PC.
[1]INVSTATEInvalid combination of EPSR and instruction, for reasons other than UNDEFINED instruction. Return PC points to faulting instruction, with the invalid state.
[0]UNDEFINSTRThe UNDEFINSTR flag is set when the processor attempts to execute an undefined instruction. This is an instruction that the processor cannot decode. The return PC points to the undefined instruction.


--

Configuration Control Register

Use the Configuration Control Register to:
  • enable NMI, Hard Fault and FAULTMASK to ignore bus fault
  • trap divide by zero, and unaligned accesses
  • enable user access to the Software Trigger Exception Register
  • control entry to Thread Mode.
The register address, access type, and Reset state are:
Address
0xE000ED14
Access
Read/write
Reset state
0x00000000
Figure 8.12 shows the fields of the Configuration Control Register.

Figure 8.12. Configuration Control Register bit assignments
Table 8.18 describes the fields of the Configuration Control Register.

Table 8.18. Configuration Control Register bit assignments
BitsFieldFunction
[9]STKALIGN
1 = on exception entry, the SP used prior to the exception is adjusted to be 8-byte aligned and the context to restore it is saved. The SP is restored on the associated exception return.
0 = only 4-byte alignment is guaranteed for the SP used prior to the exception on exception entry.
[8]BFHFNMIGNWhen enabled, this causes handlers running at priority -1 and -2 (Hard Fault, NMI, and FAULTMASK escalated handlers) to ignore Data Bus faults caused by load and store instructions. When disabled, these bus faults cause a lock-up. You must only use this enable with extreme caution. All data bus faults are ignored – you must only use it when the handler and its data are in absolutely safe memory. Its normal use is to probe system devices and bridges to detect control path problems and fix them.
[4]DIV_0_TRPTrap on Divide by 0. This enables faulting/halting when an attempt is made to divide by 0. The relevant Usage Fault Status Register bit is DIVBYZERO, see Usage Fault Status Register.
[3]UNALIGN_TRPTrap for unaligned access. This enables faulting/halting on any unaligned half or full word access. Unaligned load-store multiples always fault. The relevant Usage Fault Status Register bit is UNALIGNED, see Usage Fault Status Register.
[1]USERSETMPENDIf written as 1, enables user code to write the Software Trigger Interrupt register to trigger (pend) a Main exception, which is one associated with the Main stack pointer.
[0]NONEBASETHRDENAWhen 0, default, It is only possible to enter Thread mode when returning from the last exception. When set to 1, Thread mode can be entered from any level in Handler mode by controlled return value.

- SHCSR

System Handler Control and State Register

Use the System Handler Control and State Register to:
  • enable or disable the system handlers
  • determine the pending status of bus fault, mem manage fault, and SVC
  • determine the active status of the system handlers.
If a fault condition occurs while its fault handler is disabled, the fault escalates to a Hard Fault.
The register address, access type, and Reset state are:
Address
0xE000ED24
Access
Read/write
Reset state
0x00000000
Figure 8.14 shows the fields of the System Handler and State Control Register.
Figure 8.14. System Handler Control and State Register bit assignments
Table 8.20 describes the fields of the System Handler Control Register.
Table 8.20. System Handler Control and State Register bit assignments
BitsFieldFunction
[31:19]-Reserved
[18]USGFAULTENASet to 0 to disable, else 1 for enabled.
[17]BUSFAULTENASet to 0 to disable, else 1 for enabled.
[16]MEMFAULTENASet to 0 to disable, else 1 for enabled.
[15]SVCALLPENDEDReads as 1 if SVCall is pended.
[14]BUSFAULTPENDED
Reads as 1 if BusFault is pended.
[13]MEMFAULTPENDEDReads as 1 if MemManage is pended.
[12]USGFAULTPENDEDRead as 1 if usage fault is pended
[11]SYSTICKACTReads as 1 if SysTick is active.
[10]PENDSVACTReads as 1 if PendSV is active.
[9]-Reserved
[8]MONITORACTReads as 1 if the Monitor is active.
[7]SVCALLACTReads as 1 if SVCall is active.
[6:4]-Reserved
[3]USGFAULTACTReads as 1 if UsageFault is active.
[2]-Reserved
[1]BUSFAULTACTReads as 1 if BusFault is active.
[0]MEMFAULTACTReads as 1 if MemManage is active.


留言

熱門文章