首页 > 编程知识 正文

中断嵌套的概念,中断嵌套遵循的原则是什么

时间:2023-05-05 05:51:01 阅读:276165 作者:1610

读研期间,有幸接触了Stellaris系列的基于ARM Cortex-M3内核的LM3S9B9x微控制器,该微控制器支持中断的嵌套。

中断嵌套是指高优先级的中断能够打断低优先级的中断(反过来不可以),处理完高优先级的中断后,还得回来继续处理低优先级的中断。

中断发生时,下列动作会发生:

入栈(Stacking),即把八个寄存器的值压入堆栈;取向量(Vector fetch),即从向量表中读取中断服务函数的入口地址;更新堆栈指针(stack pointer,SP)、链接寄存器(link register)和程序计数器(Program counter,PC)。

响应中断的第一个动作就是自动保存现场的必要部分,即把八个寄存器的值压入堆栈。如果响应中断时,当前的代码正在使用PSP,则压入PSP,也就是进程堆栈;否则就压入MSP,使用主堆栈。一旦进入中断服务函数,就将一直使用主堆栈,因而所有的嵌套中断也将使用主堆栈。

嵌套中断支持已经内置于Cortex-M3内核以及嵌套向量中断控制器(Nested Vectored Interrupt Controller,NVIC),因而无需使用汇编去写贴皮代码(Wrapper Code),我们所要做的就是为每个中断设置优先级。

NVIC和Cortex-M3微控制器会根据优先级来控制抢占和嵌套行为,因此,在某个中断正在响应时,所有优先级不高于它的中断都不能抢占它,包括自己也不能抢占自己(相同的中断是不允许重入的)。

有了自动入栈和出栈(Unstacking),中断嵌套时,就没有了寄存器数据丢失的风险。然而,必须注意的是,由于所有的中断服务函数都使用主堆栈,当中断嵌套很深时,主堆栈的压力会增大,每嵌套一级,都需要额外的堆栈空间,因而要防止主堆栈被用穿的风险。《ARM Cortex-M3 权威指南》一书指出:堆栈用穿(溢出)是致命的,它会使入栈数据和主堆栈前面的数据区发生混迭,使这些数据被破坏;若在中断服务函数返回前混迭区的数据又被更改了,则堆栈内容被破坏,这样一来,在中断服务函数返回后,系统极可能功能紊乱,甚至造成程序跑飞。

(一) 咬尾中断(Tail-chaining)
当响应某个中断时,又有新的中断发生,但是新发生中断的优先级不够高,那么新的中断就会被挂起(Pending)。那么当中断返回后,系统处理挂起的中断时,如果还是先POP(出栈指令),然后把POP的内容再PUSH(入栈指令)回去,这就成了“砸锅炼铁再铸锅”,白白浪费了CPU时间。Cortex-M3不会傻乎乎地POP这些寄存器,而是继续使用上一个中断已经PUSH好的成果,从而避免了资源浪费。这就好像是一个中断把前一个中断的尾巴咬断了,前前后后只执行了一次入栈/出栈操作。

(二) 晚到的中断(Late Arrivals)
另外一种提高中断性能的机制就是“晚到的中断处理”。当处理器对某一中断的响应还在早期,即入栈阶段,如果此时收到了高优先级中断的请求,则后到的中断会被优先处理,也就是说前面的入栈操作就成了为高优先级中断所做的了,尽管它来的晚,但是由于优先级高而受到偏袒。比如,在响应低优先级的中断#1的早期时,检测到高优先级的中断#2,则只要中断#2没有太晚,就能以“晚到的中断”机制来处理,如下图所示。

当然了,如果中断#2来的太晚,以至于已经执行了中断#1的指令,则按普通的抢占处理,这会需要更多的处理器时间和额外的堆栈空间。在中断#2执行完毕后,再按照前面所述的“咬尾中断”机制来启动中断#1的执行。

参考文献:
1. 《ARM Cortex-M3权威指南》,北京:北京航空航天大学出版社,2009.7
2. 《The Definitive Guide to the ARM Cortex-M3》,Joseph Yiu

<li class="tool-item tool-active is-like "><a rel="external nofollow" rel="external nofollow" rel="external nofollow" href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:rel="external nofollow" href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count"></span> </a></li> <li class="tool-item tool-active is-collection "><a rel="external nofollow" rel="external nofollow" rel="external nofollow" href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true"> <use xlink:rel="external nofollow" href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a rel="external nofollow" rel="external nofollow" rel="external nofollow" href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true"> <use xlink:rel="external nofollow" href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a rel="external nofollow" rel="external nofollow" href="https://blog.csdn.net/Ronnie_Hu"> <img src="https://profile.csdnimg.cn/B/A/2/3_ronnie_hu" class="avatar_pic" username="Ronnie_Hu"> <img src="https://g.csdnimg.cn/static/user-reg-year/1x/3.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a rel="external nofollow" rel="external nofollow" href="https://blog.csdn.net/Ronnie_Hu" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">Ronnie_Hu</a></span> </div> <div class="text"><span>发布了58 篇原创文章</span> · <span>获赞 59</span> · <span>访问量 14万+</span></div> </div> <div class="right-message"> <a rel="external nofollow" href="https://im.csdn.net/im/main.html?userName=Ronnie_Hu" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a> </div> </div> </div>

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。