分为两部分:
1 参考资料浏览流水
2 代码分析
a.异常调转上下文分析

本文参考文献如下

一 . 参考资料浏览
讨论内容
This application note describes the usage of fault exceptions. It lists the peripheral registers in the System Control Block (SCB) that control fault exceptions or provide information of their cause. Software examples show the usage of fault exceptions during program debugging or for recovering errors in the application software.
1.SCB模块控制硬件异常并且可以提供硬件异常的信息。
2.在调试中可以观察硬件异常,或者在应用程序中进行异常恢复处理。

异常分类
有若干个系统异常专用于 fault 处理。 CM3 中的 Faults 可分为以下几类:
1 总线 faults
detects memory access errors on instruction fetch, data read/write, interrupt vector fetch,and register stacking (save/restore) on interrupt (entry/exit).

2 存储器管理 faults
detects memory access violations to regions that are defined in the Memory Management Unit (MPU); for example, code execution from a memory region with read/write access only.

3 用法 faults
detects execution of undefined instructions, unaligned memory access for load/storemultiple. When enabled, divide-by-zero and other unaligned memory accesses are detected.

4 硬 fault
HardFault: is the default exception and can be triggered because of an error during exception processing, or because an exception cannot be managed by any other exception mechanism.

硬件异常号和优先级
ARM Cortex M系列优先级数值越低逻辑上的优先级越高

硬件错误(3,上表异常号) 优先级为-1 ,其余(4,5,6)为可编程。reset后 4.5.6 默认为disable状态,可以在System Control Block (SCB)中配置。

Priority escalation
优先级升级
Usually, the exception priority, together with the values of the exception mask registers, determines whetherthe processor enters the fault handler, and whether a fault handler can preempt another fault handler.In some situations, a fault with configurable priority is treated as a HardFault. This is called priority escalation,and the fault is described as escalated to HardFault。
▪ A fault handler causes the same kind of fault as the one it is servicing. This escalation to HardFault
occurs because a handler cannot preempt itself (it must have the same priority as the current priority
level).
▪ A fault handler causes a fault with the same or lower priority as the fault it is servicing. This is because
the handler for the new fault cannot preempt the currently executing fault handler.
▪ An exception handler causes a fault for which the priority is the same as or lower than the currently
executing exception.
▪ A fault occurs and the handler for that fault is not enabled.
如果4,5,6异常没有被开启,那么发生异常时就会上升回到硬件异常,故优先级升级。

Synchronous and asynchronous BusFaults

同步意识是发生异常后即可处理异常,异常跳入的位置是发生错误的位置。
非同步意识是,发生错误后,异常处理并未立刻调转,又运行了其他的指令然后进行异常调转。这样异常发生的指令不好追溯。

Fault types in Keil MDK
在MDK环境中可以用Fault Reports dialog 来监控错误的类型。通过Bit Name 来判断错误发生的类型。

Fault exception registers异常处理相关的寄存器

The System Control Block (SCB) provides system implementation information, and system control. This includes configuration, control, and reporting of the system exceptions. Some of its registers are used to control fault exceptions
System Control Block (SCB) SCB模块可以提供异常的信息。

▪ The Configuration and Control Register CCR register controls the behavior of the UsageFault for divideby-zero and unaligned memory accesses.
▪ The System Handler Priority Registers SHP control the exception priority.
▪ The System Handler Control and State Register SHCSR enables the system handlers, and indicates he
pending status of the BusFault, MemManage fault, and SVC exceptions.

MemManage Status Register (MMFSR)
The MemManage fault status register (MMFSR) indicates a memory access violation detected by the Memory
Protection Unit (MPU). Privileged access permitted only. Unprivileged accesses generate a BusFault.

MemManage Address Register (MMFAR)
The BFAR address is associated with a precise data access BusFault. Privileged access permitted only.
Unprivileged accesses generate a BusFault.

BusFault Status Register (BFSR)
The BusFault Status Register shows the status of bus errors resulting from instruction fetches and data accesses
and indicates memory access faults detected during a bus operation. Only privileged access is permitted.
Unprivileged access will generate a BusFault.

BusFault Address Register (BFAR)
The BFAR address is associated with a precise data access BusFault. Privileged access permitted only.
Unprivileged accesses generate a BusFault.

Implementing fault handlers实现异常处理

启动文件

二。代码分析

异常调转上下文分析

手动开启一个PendSV 异常,记录上下文切换前的 上下文状态

在进入PendSV 前的状态
PC为当前程序运行位置为0x08005174
SP 为当前栈地址 0x200021B0 与MSP地址相同。说明使用的是主栈
mode 为 Theard mode
Stack MSP
R0~R15寄存器如上图所示
如果发生PendSV 异常
异常标志置位,切换到 Handler mode,硬件会主动入栈
顺序如下 最先入栈的是xPSR 最后是R0,这部分由硬件完成

但是 我们CPU的寄存器是R0~R15 剩下没有入栈的寄存器怎么办,这个由我们的编译器来做。编译器可以根据异常处理中使用的寄存器来进行保存,或者全部剩下的都保存,反正这是C编译器的特性。
当前栈位置为21b0 ,cortex m内核栈的生长方向为高地址向低地址,所以后面的入栈地址都是小于21b0.

调转到PendSV Handler


首先XPSR寄存器变化,Mode 切换为Handler 表示进入到异常处理函数(当然这些标志可以通过相应寄存器状态判断MDK帮我们查找了)
MSP 指针由于入栈的关系发生了变化,指向最后一个入栈的R0寄存器。
倒数低7入栈的是PC 5174 所以我们在0x20002148 往后数7的地址单元上看到了上个PC的位置0x08005174
LR 寄存器为子函数返回值,这边不用关注。
当执行完成PendSV的时候,从MSP指针位置 0x20002148 开始依照队形出栈。

这里可以看到 R0~R14回复到了加入PendSV前的状态。
但是内存0x20002148的内容被更改没有恢复原状态,所以出栈执行后只做了栈指针的移动,栈空间的内容是不会发生变化。
所以栈空间溢出检查思路由此诞生,这里不多提。

也就是说如果发生同步异常,我们在栈的相应位置就可以找到调转前的PC指针位置,从而找到出是哪条指令触发了异常。

1.在汇编中找到0x080005174指令位置
2.C文件中的代码就可以看到了
3.PC代表当前位置,所以上一条是触发异常的指令
可以看到是 SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk; 这条语句触发了异常,从而定义出异常位置。

所以
分析异常 如果跳入了硬件异常需要分析的话
1.记录各个异常状态位
2.分析栈内容,从而找到PC,寻找到异常发生点。
MDK也帮我们做了这件事,如下所示 show caller code


Fault 异常或陷阱 分析相关推荐

  1. Hard Fault 异常错误堆栈分析

    origin:http://www.51hei.com/bbs/dpj-39846-1.html 看到有朋友遇到Hard Fault 异常错误,特地找到一篇飞思卡尔工程师写的一片经验帖,定位Hard ...

  2. PCA图像数据降维及重构误差分析实战并使用TSNE进行异常数据可视化分析

    PCA图像数据降维及重构误差分析实战并使用TSNE进行异常数据可视化分析 目录 PCA图像数据降维及重构误差分析实战并使用TSNE进行异常数据可视化分析</

  3. Java异常的性能分析

    Java异常的性能分析 参考文章: (1)Java异常的性能分析 (2)https://www.cnblogs.com/grefr/p/6089116.html 备忘一下.

  4. python try-except处理异常的常用方法分析

    python try-except处理异常的常用方法分析 参考文章: (1)python try-except处理异常的常用方法分析 (2)https://www.cnblogs.com/silenc ...

  5. arm linux 中断 分析,armlinux中断异常的处理分析.pdf

    基于 ARM Linux 中断.异常的处理分析 本文是基于ARM S3C2410X 系统的Linux 2.6 中断.异常和系统调用的处理分析. 主要有以下几个部分: 1. ARM 的硬件中断机制 2. ...

  6. Oracle JOB异常中断原因分析

    链接:http://blog.itpub.net/28602568/viewspace-1731805/ 标题: Oracle JOB异常中断原因分析 作者:lōττéry©版权所有[文章允许转载,但 ...

  7. Android 系统(55)---Android App开发之ANR异常的原因分析及处理总结

    Android App开发之ANR异常的原因分析及处理总结 Android App开发之ANR异常的原因分析及处理总结 ANR的全称是application not responding,根据它的意思 ...

  8. java操作集合中 concurrentModifyException 异常的原因分析

    java操作集合中 concurrentModifyException 异常的原因分析 参考文章: (1)java操作集合中 concurrentModifyException 异常的原因分析 (2) ...

  9. iOS_异常堆栈报告分析

    在使用 Xcode 工具的开发过程中, 面对运行异常, 很多初学者往往毫无头绪, 不知道如何跟踪异常堆栈, 如何分析异常堆栈报告. 这里就给大家介绍如何<b>跟踪异常堆栈</b> ...

  10. 内中断,外中断,软中断,硬中断,异常,陷阱

    不管是内中断,外中断,软中断还是硬中断都是按照中断源来划分的. 一.外中断 狭义上的中断(interruption)指的就是外中断. 指来自CPU执行指令以外的事件的发生,希望处理机能够向设备发下一个 ...

最新文章

  1. Outlook中删除重复的邮件
  2. 超郁闷的本地连接故障解决过程!!!
  3. [BZOJ 2588]Count on a tree
  4. Standard Deviation Normal Distribution
  5. 以太坊本地开发环境搭建
  6. 计算机信息安全技术计算题,计算机信息安全技术练习题.doc
  7. c语言 程序设计一篇,用c语言编程任务br/请编写一个程序,从输入中读取一篇中文文 爱问知识人...
  8. android模拟触控power键
  9. 免费试用一年微软云服务领取教程
  10. 【Opencv实战】纯手工代码打造车牌检测程序,秒变智能检测你值得拥有~(附源码)
  11. 信用卡有很多好处,远不止解决你燃眉之急这么简单
  12. Windows防火墙导致FTP服务器不能访问的解决方法
  13. win10 电脑中模块initpki.dll加载失败提示0x80004005错误代码如何解决
  14. 如何创作一款商业级的安卓独立应用
  15. 秒杀小程序 php后台,基于ThinkPhp6.0+Vue 开发实现微信小程序、公众号、商城、拼团、秒杀、后台管理等功能...
  16. Deepin系统正版官方下载站
  17. 有人云网络IO控制器 对接私有服务器
  18. 图形处理 柔化和锐化处理
  19. 小游戏2048设计思路超简单
  20. usbhid类之mouse、keyboard

热门文章

  1. 周杰伦出道20周年,相关.fans域名等你来抢注~
  2. Ubuntu上无法登录网页版本微信的解决方案
  3. 堕落了!经典软件下载网站被查
  4. 9367: 【动态规划】雷涛的小猫
  5. 如何打开计算机本地组策略编辑器
  6. 心知天气使用签名验证方式
  7. mySql笔记之基础篇(参看尚硅谷视频)
  8. 瑞幸咖啡2022,摆脱困境,迎来坦途
  9. 关于j2me mmapi的player接口的一些理解.
  10. 用Python爬了我的微信好友,他们是这样的...