ARM异常处理 M33

1. ARMv8-M异常类型及其详细解释

ARMv8-M Exception分为两类:预定义系统异常(015)和外部中断(1616+N)。
在这里插入图片描述
各种异常的状态可以通过Status bit查看,获取更信息的异常原因:
在这里插入图片描述

CFSR是由UFSR、BFSR和MMFSR组成:在这里插入图片描述

下面列举HFSR、MMFSR、BFSR、UFSR的详细解释。
1.1 HFSR
在这里插入图片描述
DEBUGEVT, bit [31] Debug event. Indicates when a debug event has occurred.
The possible values of this bit are:
0 No debug event has occurred.
1 Debug event has occurred. The Debug Fault Status Register has been updated.

FORCED, bit [30] Forced. Indicates that a fault with configurable priority has been escalated to a HardFault exception, because
it could not be made active, because of priority, or because it was disabled.
The possible values of this bit are:
0 No priority escalation has occurred.
1 Processor has escalated a configurable-priority exception to HardFault.

VECTTBL, bit [1] Vector table. Indicates when a fault has occurred because of a vector table read error on exception processing.
The possible values of this bit are:
0 No vector table read fault has occurred.
1 Vector table read fault has occurred.

1.2 MMFSR
在这里插入图片描述
MMARVALID, bit [7] MMFAR valid flag. Indicates validity of the MMFAR register.
The possible values of this bit are:
0 MMFAR content not valid.
1 MMFAR content valid.

MLSPERR, bit [5] MemManage lazy state preservation error flag. Records whether a MemManage fault occurred during FP lazy state preservation.
The possible values of this bit are:
0 No MemManage occurred.
1 MemManage occurred.

MSTKERR, bit [4] MemManage stacking error flag. Records whether a derived MemManage fault occurred during exception entry stacking.
The possible values of this bit are:
0 No derived MemManage occurred.
1 Derived MemManage occurred during exception entry.

MUNSTKERR, bit [3] MemManage unstacking error flag. Records whether a derived MemManage fault occurred during exception return unstacking.
The possible values of this bit are:
0 No derived MemManage fault occurred.
1 Derived MemManage fault occurred during excep

DACCVIOL, bit [1] Data access violation flag. Records whether a data access violation has occurred.
The possible values of this bit are:
0 No MemManage fault on data access has occurred.
1 MemManage fault on data access has occurred.

IACCVIOL, bit [0] Instruction access violation. Records whether an instruction related memory access violation has occurred.
The possible values of this bit are:
0 No MemManage fault on instruction access has occurred.
1 MemManage fault on instruction access has occurred.

1.3 BFSR

在这里插入图片描述
BFARVALID, bit [7] BFAR valid. Indicates validity of the contents of the BFAR register.
The possible values of this bit are:
0 BFAR content not valid.
1 BFAR content valid.

LSPERR, bit [5] Lazy state preservation error. Records whether a precise BusFault occurred during FP lazy state preservation.
The possible values of this bit are:
0 No BusFault occurred.
1 BusFault occurred.

STKERR, bit [4] Stack error. Records whether a precise derived BusFault occurred during exception entry stacking.
The possible values of this bit are:
0 No derived BusFault occurred.
1 Derived BusFault occurred during exception entry.

UNSTKERR, bit [3] Unstack error. Records whether a precise derived BusFault occurred during exception return unstacking.
The possible values of this bit are:
0 No derived BusFault occurred.
1 Derived BusFault occurred during exception return.

IMPRECISERR, bit [2] Imprecise error. Records whether an imprecise data access error has occurred.

The possible values of this bit are:
0 No imprecise data access error has occurred.
1 Imprecise data access error has occurred.

PRECISERR, bit [1] Precise error. Records whether a precise data access error has occurred.
The possible values of this bit are:
0 No precise data access error has occurred.
1 Precise data access error has occurred.

IBUSERR, bit [0]
Instruction bus error. Records whether a precise BusFault on an instruction prefetch has occurred.
The possible values of this bit are:
0
No BusFault on instruction prefetch has occurred.
1
A BusFault on an instruction prefetch has occurred.

1.4 UFSR
在这里插入图片描述

DIVBYZERO, bit [9] Divide by zero flag. Sticky flag indicating whether an integer division by zero error has occurred.
The possible values of this bit are:
0 Error has not occurred.
1 Error has occurred.

UNALIGNED, bit [8] Unaligned access flag. Sticky flag indicating whether an unaligned access error has occurred.
The possible values of this bit are:
0 Error has not occurred.
1 Error has occurred.

STKOF, bit [4] Stack overflow flag. Sticky flag indicating whether a stack overflow error has occurred.
The possible values of this bit are:
0 Error has not occurred.
1 Error has occurred.
NOCP, bit [3] No coprocessor flag. Sticky flag indicating whether a coprocessor disabled or not present error has occurred.
The possible values of this bit are:
0 Error has not occurred.
1 Error has occurred.
INVPC, bit [2] Invalid PC flag. Sticky flag indicating whether an integrity check error has occurred.
The possible values of this bit are:
0 Error has not occurred.
1 Error has occurred.

INVSTATE, bit [1] Invalid state flag. Sticky flag indicating whether an EPSR.T, EPSR.IT, or FPSCR.LTPSIZE validity error has occurred.
The possible values of this bit are:
0 Error has not occurred.
1 Error has occurred.

UNDEFINSTR, bit [0] UNDEFINED instruction flag. Sticky flag indicating whether an UNDEFINED instruction error has occurred.
The possible values of this bit are:
0 Error has not occurred.
1 Error has occurred.

2 ARMv8-M ARM中关于异常入口处理和压栈

在ARMv8-M ARM中介绍了异常发生时,硬件所做的一系列操作:
在这里插入图片描述在这里插入图片描述

从中可以看出对R0-R3、R12、LR、XPSR、ReturnAddress进行了压栈操作,最后PC指向异常处理函数。

当异常发生时,压栈的内容和顺序是固定的:XPSR->ReturnAddress->LR->R12->R3->R2->R1->R0。
在这里插入图片描述
这里的LR指的是异常的PC值,是真正的死亡前现场。ReturnAddress是处理器决定的异常后返回地址。

EXC_RETURN
EXC_RETURN代表异常入口时LR的值。
ARMv8-M规格书中关于EXC_RETURN定义如下:
在这里插入图片描述

PREFIX, bits [31:24] Prefix. Indicates that this is an EXC_RETURN value.This field reads as 0b11111111.

S, bit [6] Secure or Non-secure stack.

DCRS, bit [5] Default callee register stacking.

FType, bit [4] Stack frame type. 0 Extended stack frame. 1 Standard stack frame.

Mode, bit [3] Mode. Indicates the Mode that was stacked from. 0 Handler mode. 1 Thread mode.

SPSEL, bit [2] Stack pointer selection. 0 Main stack pointer. 1 Process stack pointer.

ES, bit [0] Exception Secure. 0 Non-secure. 1 Secure.

RETPSR
当异常进入的时候,会将RETPSR的值压栈。
在这里插入图片描述
N, bit [31] Negative condition flag. 0 Result is positive or zero. 1 Result is negative.

Z, bit [30] Zero condition flag.0 Result is nonzero. 1 Result is zero.

C, bit [29] Carry condition flag. 0 No carry occurred, or last bit shifted was clear. 1 Carry occurred, or last bit shifted was set.

V, bit [28] Overflow condition flag. 0 Signed overflow did not occur. 1 Signed overflow occurred.

Q, bit [27] Sticky saturation flag. 0 Saturation or overflow has not occurred since bit was last cleared. 1 Saturation or overflow has occurred since bit was last cleared.

T, bit [24] T32 state. 0 Execution of any instruction generates an INVSTATE UsageFault. 1 Instructions decoded as T32 instructions.

SFPA, bit [20] Secure Floating-point active.

GE, bits [19:16] Greater than or equal flags.

SPREALIGN, bit [9]
0 The stack pointer was 8-byte aligned before exception entry began, no special handling is required on exception return.
1 The stack pointer was only 4-byte aligned before exception entry. The exception entry realigned SP to 8-byte alignment by increasing the stack frame size by 4-bytes.
Exception, bits [8:0] Exception number.

3 异常Handler以及分析

异常的入口是异常向量表,根据异常号调用对应的处理函数:

__isr_vector:.long    __StackTop            /* Top of Stack */.long    Reset_Handler         /* 1. Reset Handler */.long    NMI_Handler           /* 2. NMI Handler */.long    HardFault_Handler     /* 3. Hard Fault Handler */.long    MemManage_Handler     /* 4. MPU Fault Handler */.long    BusFault_Handler      /* 5. Bus Fault Handler */.long    UsageFault_Handler    /* 6. Usage Fault Handler */.long    0                     /* 7. Reserved */.long    0                     /* 8. Reserved */.long    0                     /* 9. Reserved */.long    0                     /* 10. Reserved */.long    SVC_Handler           /* 11. SVCall Handler */.long    DebugMon_Handler      /* 12. Debug Monitor Handler */.long    0                     /* 13. Reserved */.long    PendSV_Handler        /* 14. PendSV Handler */.long    SysTick_Handler       /* 15. SysTick Handler *//* External interrupts *//* The interrupts 0 to 31 */.long    Default_IRQHandler /*16. External Interrupt 0*/.long    Default_IRQHandler

在进入Handler的时候,异常栈顶为包括R0~R3、R12、LR、ReturnAddress、RETPSR寄存器的内容。

下面的寄存器通过判断EXC_RETURN[2]来决定使用msp还是psp:

asm volatile(" tst lr, #4                        \n"--测试EXC_RETURN[2]是否为1,即测试当前StackPointer是MSP(0)还是PSP(1)" ite eq                            \n"--当EXC_RETURN[2]0,则z=1;当EXC_RETURN[2]1,则z=1" mrseq r0, msp                     \n"--当EXC_RETURN[2]0,将msp放入r0。" mrsne r0, psp                     \n"--当EXC_RETURN[2]1,将psp放入r0。"b common_handler_c             \n"--: /* no output */: /* no input */: "r0" /* clobber */
);

其中B和BL区别:

B Label ;程序无条件跳转到标号 Label 处执行。

BL Label ;当程序无条件跳转到标号 Label 处执行时,同时将当前的 PC 值保存到 R14 中。L ;用来区分 分支是否是有带返回的分支指令。

下面以HardFault为例,介绍代码和分析流程。

void HardFault_Handler(void)
{asm volatile(" tst lr, #4                        \n"" ite eq                            \n"" mrseq r0, msp                     \n"" mrsne r0, psp                     \n""b hardfault_handler_c             \n": /* no output */: /* no input */: "r0" /* clobber */);
}void hardfault_handler_c(sContextStateFrame* regs)--传入的参数为msp的值。
{unsigned int hfsr = SCB->HFSR;star_stack_dump(regs);MSG("Cause of Hard Fault:\n");if(hfsr & SCB_HFSR_DEBUGEVT_Msk) {MSG("Debug event has occurred, ");unsigned dfsr = SCB->DFSR;if(dfsr & SCB_DFSR_PMU_Msk)MSG("PMU event.\n");if(dfsr & SCB_DFSR_EXTERNAL_Msk)MSG("External event.\n");if(dfsr & SCB_DFSR_VCATCH_Msk)MSG("Vector Catch event.\n");if(dfsr & SCB_DFSR_DWTTRAP_Msk)MSG("Watchpoint event.\n");if(dfsr & SCB_DFSR_BKPT_Msk)MSG("Breakpoint event.\n");if(dfsr & SCB_DFSR_HALTED_Msk)MSG("Halt or step event.\n");}if(hfsr & SCB_HFSR_FORCED_Msk) {MSG("Processor has escalated a configurable-priority exception to HardFault.\n");aon_system_reset();}if(hfsr & SCB_HFSR_VECTTBL_Msk) {MSG("Vector table read fault has occurred.\n");aon_system_reset();}
}void star_stack_dump(sContextStateFrame* regs)
{unsigned int *stackPtr = NULL;MSG("ExceptionStack(%08x):\n", regs);--输出异常入栈信息:R0~R3、R12、LR、ReturnAddress、XPSR。MSG("R0 = %08x\n", regs->r0);MSG("R1 = %08x\n", regs->r1);MSG("R2 = %08x\n", regs->r2);MSG("R3 = %08x\n", regs->r3);MSG("R12 = %08x\n", regs->r12);MSG("LR = %08x\n", regs->lr);MSG("ReturnAddr = %08x\n", regs->return_address);MSG("PSR = %08x: N(%u) Z(%u) C(%u) V(%u) Q(%u) IT(%u) T(%u) SFPA(%u) GE(%u) SPRealign(%u) ISR(%u) \n", regs->xpsr.w,regs->xpsr.b.N,regs->xpsr.b.Z,regs->xpsr.b.C,regs->xpsr.b.V,regs->xpsr.b.Q,regs->xpsr.b.IT,regs->xpsr.b.T,regs->xpsr.b.SFPA,regs->xpsr.b.GE,regs->xpsr.b.SPREALIGN,regs->xpsr.b.ISR);--RETPSR的几种情况暂未分别分析。MSG("\nStack from 0x%08x in [StackTop(0x%08x), MSPLIM(0x%08x)]:\n", regs, &__StackTop,  __get_MSPLIM());for(stackPtr = (unsigned int *)regs; stackPtr < &__StackTop; stackPtr++ ) {--遍历输出栈内容,方便后续分析。MSG("0x%08x %08x\n", stackPtr, *stackPtr);}
}

触发产生异常:

void fault_test_by_trigger(void) {MSG("%s\n", __func__);SCB->SHCSR |= SCB_SHCSR_HARDFAULTPENDED_Msk;
//    SCB->SHCSR |= SCB_SHCSR_BUSFAULTPENDED_Msk;
//    SCB->SHCSR |= SCB_SHCSR_MEMFAULTPENDED_Msk;
//    SCB->SHCSR |= SCB_SHCSR_USGFAULTPENDED_Msk;
}

结果如下:

fault_test_by_trigger
ExceptionStack(2003FFA8):
R0 = 00000007
R1 = 0000000A
R2 = E000ED00
R3 = 00270000
R12 = 00000000
LR = 000002E7
ReturnAddr = 000002F2
PSR = 69000000: N(0) Z(1) C(1) V(0) Q(1) IT(0) T(1) SFPA(0) GE(0) SPRealign(0) ISR(0) Stack from 0x2003FFA8 in [StackTop(0x2003FFF0), MSPLIM(0x2003F3F0)]:
0x2003FFA8 00000007
0x2003FFAC 0000000A
0x2003FFB0 E000ED00
0x2003FFB4 00270000
0x2003FFB8 00000000
0x2003FFBC 000002E7
0x2003FFC0 000002F2
0x2003FFC4 69000000--到此都为异常入栈内容。
0x2003FFC8 0000E4E0
0x2003FFCC 0000E4E0
0x2003FFD0 0000E4E0
0x2003FFD4 0000C0CC
0x2003FFD8 00000000
0x2003FFDC 00000000
0x2003FFE0 00000000
0x2003FFE4 00000000
0x2003FFE8 00000000
0x2003FFEC 0000B6C1
Cause of Hard Fault:

从上述log可知三个地址0x000002E7、0x000002F2、0x0000B6C1。

使用addrline工具分析对应符号表,

arm-linux-gnueabihf-addr2line -e main.elf -a -f 0x000002E7 0x000002F2 0x0000B6C1

结果如下:

0x000002e7
fault_test_by_trigger--异常栈中的LR,对应异常现场PC值。是导致问题产生的原因。
xxx.c:34
0x000002f2
main--这是异常退出后处理器PC指向的地方,即退出异常后将要执行的代码。
xxx.c:66
0x0000b6c1
Reset_Handler--栈回溯部分。
xxx.S:284

基本可以得到函数调用关系。

引用链接:https://www.cnblogs.com/arnoldlu/p/16199437.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/494540.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

百度热力图数据处理,可直接用于论文

数据简介1、CSV点数据2、SHP数据3、TIF数据4、png图片或标准经纬度出图5、案例6、论文的参考方向 其他数据处理/程序/指导&#xff01;&#xff01;&#xff01;&#xff08;1&#xff09;街景数据获取&#xff08;2&#xff09;街景语义分割后像素提取&#xff0c;指标计算代码…

利用Gurobi追溯模型不可行原因的四种方案及详细案例

文章目录 1. 引言2. 追溯不可行集的四种方法2.1 通过约束增减进行判断2.2 通过computeIIS函数获得冲突集2.3 利用 feasRelaxS() 或 feasRelax() 函数辅助排查2.4 利用 IIS Force 属性1. 引言 模型不可行是一个让工程师头疼的问题,对于复杂模型而言,导致模型不可行的原因可能…

数据流图和流程图的区别

在结构化建模中&#xff0c;数据流图和流程图都是非常重要的工具&#xff0c;它们为开发人员提供了强大的手段来分析和设计系统。尽管两者在表面上看起来有些相似&#xff0c;但它们在功能、用途和表达方式上存在显著的区别。本文将详细探讨数据流图和流程图的区别&#xff0c;…

《计算机组成及汇编语言原理》阅读笔记:p48-p81

《计算机组成及汇编语言原理》学习第 4 天&#xff0c;p48-p81 总结&#xff0c;总计 34 页。 一、技术总结 1.CISC vs RISC p49&#xff0c; complex instruction set computing For example, a complex instruction set computing (CISC) chip may be able to move a lar…

GitLab的安装与卸载

目录 GitLab安装 GitLab使用 使用前可选操作 修改web端口 修改Prometheus端口 使用方法 GitLab的卸载 环境说明 系统版本 CentOS 7.2 x86_64 软件版本 gitlab-ce-10.8.4 GitLab安装 Gitlab的rpm包集成了它需要的软件&#xff0c;简化了安装步骤&#xff0c;所以直接…

LAUNCHXL_F28379D_Workspace_CCS124

/// 安装 controlSUITE C:\ti\controlSUITE\device_support\F2837xD\v210 /// /// /// /// /// 删除 /// /// /// >> Compilation failure source_common/subdir_rules.mk:9: recipe for target source_common/F2837xD_Adc.obj failed "C:/ti/controlSUITE/devic…

封装(2)

大家好&#xff0c;今天我们来介绍一下包的概念&#xff0c;知道包的作用可以更好的面对今后的开发&#xff0c;那么我们就来看看包是什么东西吧。 6.3封装扩展之包 6.3.1包的概念 在面向对象体系中,提出了一个软件包的概念,即:为了更好的管理类,把多个类收集在一起成为一组…

Python 写的 智慧记 进销存 辅助 程序 导入导出 excel 可打印

图样&#xff1a; 就可以导入了 上代码 import tkinter as tk from tkinter import ttk import sqlite3 from datetime import datetime from tkinter import messagebox, filedialog import pandas as pd import reclass OrderSystem:def __init__(self, root):self.root r…

C++---------随机库,standfor库

意外修改原始数据 问题描述 由于引用参数允许函数直接修改原始变量&#xff0c;这可能会导致意外的修改。如果函数的使用者没有意识到函数会修改传入的参数&#xff0c;就可能引发错误。例如&#xff0c;有一个函数看起来只是用于打印一个变量的值&#xff0c;但实际上却修改了…

维克日记:私密写作新选择,轻松记录生活点滴

作为一款专注于私密写作的数字日记本&#xff0c;维克日记以其独特的设计理念和全面的功能特性&#xff0c;正在重新定义现代人记录生活的方式。这款软件不仅提供了清新简约的界面设计&#xff0c;更重要的是它完全不需要繁琐的设置就能立即开始写作。对于热爱写作的用户来说&a…

0009.基于springboot+layui的ERP企业进销存管理系统

一、系统说明 基于springbootlayui的ERP企业进销存管理系统,系统功能齐全, 代码简洁易懂&#xff0c;适合小白学编程,课程设计&#xff0c;毕业设计。 二、系统架构 前端&#xff1a;html| layui 后端&#xff1a;springboot | mybatis| thymeleaf 环境&#xff1a;jdk1.8 |…

3.4 stm32系列:定时器(PWM、定时中断)

一、定时器概述 1.1 软件定时原理 使用纯软件&#xff08;CPU死等&#xff09;的方式实现定时&#xff08;延时&#xff09;功能&#xff1b; 不精准的延迟&#xff1a; /* 微秒级延迟函数* 不精准* stm32存在压出栈过程需要消耗时间* 存在流水线&#xff0c;执行时间不确定…

【XSSed】攻防实战全记录

最后救你一次&#xff0c;不要让欲望击溃你的意志 首先整体浏览网站 进入Level1 Basic XSS 首先整体浏览网站 对源码进行分析 漏洞产生于如下代码段&#xff1a; echo($_GET[‘q’]); 直接将用户输入插入了html页面&#xff0c;没有任何过滤。 构造普通payload&#xff1…

2.6 网络面试问题

tcp 与 udp的区别 1.tcp 是基于连接的 UDP是基于数据包 2.处理并发的方式不通 a.tcp用epoll进行监听的 b. udp是模拟tcp的连接过程&#xff0c;服务端开放一个IP端口&#xff0c;收到连接后&#xff0c;服务端用另一个IP和端口发包给客户端。 3.tcp根据协议MTU黏包及…

在Visual Studio 2022中配置C++计算机视觉库Opencv

本文主要介绍下载OpenCV库以及在Visual Studio 2022中配置、编译C计算机视觉库OpenCv的方法 1.Opencv库安装 ​ 首先&#xff0c;我们需要安装OpenCV库&#xff0c;作为一个开源库&#xff0c;我们可以直接在其官网下载Releases - OpenCV&#xff0c;如果官网下载过慢&#x…

【恶意软件检测】一种基于API语义提取的Android恶意软件检测方法(期刊等级:CCF-B、Q2)

一种基于API语义提取的Android恶意软件检测方法 A novel Android malware detection method with API semantics extraction 摘要 由于Android框架和恶意软件的持续演变&#xff0c;使用过时应用程序训练的传统恶意软件检测方法在有效识别复杂演化的恶意软件方面已显不足。为…

通航飞机(通用航空飞机)的软件关键技术

通航飞机&#xff08;通用航空飞机&#xff09;的软件关键技术主要聚焦于确保飞行安全、提升操作效率以及优化用户体验等核心目标&#xff0c;这些技术涵盖了诸多重要领域&#xff0c;从软件层面来看&#xff0c;各个技术领域均有着独特的实现方式与关键作用&#xff0c;以下将…

docker安装nginx,docker部署vue前端,以及docker部署java的jar部署

一、部署前端部分 前端部分使用vue 2&#xff0c;通过nginx进行部署&#xff0c;首先从nginx安装开始 1.搜索并拉去Nginx镜像 [rootlocalhost ~]# docker search nginx [rootlocalhost /]# docker pull nginx:latest latest: Pulling from library/nginx Digest: sha256:0d1…

【信号滤波 (上)】傅里叶变换和滤波算法去除ADC采样中的噪声(Matlab/C++)

目录 一、ADC采样的噪声简介1.1 常见的ADC噪声来源 二、信号的时域到频域转换2.1 傅里叶变换巧记傅里叶变换 三、傅里叶变换和滤波算法工程实现3.1 使用Matlab计算信号时域到频域的变换3.2 使用Matlab去除特定频点噪声寻找峰值算噪声频率构建陷波滤波器滤除噪声频点陷波滤波器与…

<论文>初代GPT长什么样?

一、摘要 今天我们聊一下论文《Improving Language Understanding by Generative Pre-Training》以及它所提出来的预训练模型——GPT1。我们知道Bert在出道那会儿红极一时&#xff0c;但实际上GPT1比Bert还要早几个月就出道了&#xff0c;而且同样刷新了当时的多个任务记录。GP…