在上篇《GNU ARM汇编–(四)中止汇编之非嵌套中止处理》中剖析了最简略的中止处理的写法,再看TQ2440发动代码中的中止向量表的写法就一望而知了.今日抽时间对嵌套中止处理的学习做下收拾.
嵌套中止处理的中心代码如下:
- ;/*
- ;*____________________________________________________________________
- ;*
- ;*Copyright(c)2004,AndrewN.Sloss,ChrisWrightandDominicSymes
- ;*Allrightsreserved.
- ;*____________________________________________________________________
- ;*
- ;*NON-COMMERCIALUSELicense
- ;*
- ;*Redistributionanduseinsourceandbinaryforms,withorwithout
- ;*modification,arepermittedprovidedthatthefollowingconditions
- ;*aremet:
- ;*
- ;*1.ForNON-COMMERCIALUSEonly.
- ;*
- ;*2.Redistributionsofsourcecodemustretaintheabovecopyright
- ;*notice,thislistofconditionsandthefollowingdisclaimer.
- ;*
- ;*3.Redistributionsinbinaryformmustreproducetheabove
- ;*copyrightnotice,thislistofconditionsandthefollowing
- ;*disclaimerinthedocumentationand/orothermaterialsprovided
- ;*withthedistribution.
- ;*
- ;*4.Alladvertisingmaterialsmentioningfeaturesoruseofthis
- ;*softwaremustdisplaythefollowingacknowledgement:
- ;*
- ;*ThisproductincludessoftwaredevelopedbyAndrewN.Sloss,
- ;*ChrisWrightandDominicSymes.
- ;*
- ;*THISSOFTWAREISPROVIDEDBYTHECONTRIBUTORS“ASISANDANY
- ;*EXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THE
- ;*IMPLIEDWARRANTIESOFMERCHANTABILITYANDFITNESSFORAPARTICULAR
- ;*PURPOSEAREDISCLAIMED.INNOEVENTSHALLTHECONTRIBUTORSBE
- ;*LIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,SPECIAL,EXEMPLARY,
- ;*ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,
- ;*PROCUREMENTOFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,DATA,
- ;*ORPROFITS;ORBUSINESSINTERRUPTION)HOWEVERCAUSEDANDONANY
- ;*THEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,OR
- ;*TORT(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUT
- ;*OFTHEUSEOFTHISSOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITY
- ;*OFSUCHDAMAGE.
- ;*
- ;*Ifyouhavequestionsaboutthislicenseorwouldlikeadifferent
- ;*licensepleaseemail:
- ;*
- ;*andrew@sloss.net
- ;*
- ;*
- ;*/
- ;/***********************************************************************
- ;*
- ;*Module:nih9_9.s
- ;*Descriptions:NestedInterruptHandler
- ;*Example:9.9
- ;*OS:generic
- ;*Platform:generic
- ;*History:
- ;*
- ;*31thDecember2003
- ;*-addedheader
- ;*
- ;***********************************************************************/
- EXPORTnestedInterruptHandler
- MaskmdEQU0x1f;processormodemask
- SVC32mdEQU0x13;SVCmode
- I_BitEQU0x80;IRQbit
- FRAME_R0EQU0x00
- FRAME_R1EQUFRAME_R0+4
- FRAME_R2EQUFRAME_R1+4
- FRAME_R3EQUFRAME_R2+4
- FRAME_R4EQUFRAME_R3+4
- FRAME_R5EQUFRAME_R4+4
- FRAME_R6EQUFRAME_R5+4
- FRAME_R7EQUFRAME_R6+4
- FRAME_R8EQUFRAME_R7+4
- FRAME_R9EQUFRAME_R8+4
- FRAME_R10EQUFRAME_R9+4
- FRAME_R11EQUFRAME_R10+4
- FRAME_R12EQUFRAME_R11+4
- FRAME_PSREQUFRAME_R12+4
- FRAME_LREQUFRAME_PSR+4
- FRAME_PCEQUFRAME_LR+4
- FRAME_SIZEEQUFRAME_PC+4
- AREAnih9_9,CODE,readonly
- nestedInterruptHandler;instructionstate:comment
- SUBr14,r14,#4;2:
- STMDBr13!,{r0-r3,r12,r14};2:savecontext
- ;
- BLread_RescheduleFlag;3:moreprocessing
- CMPr0,#0;3:ifprocessing?
- LDMNEIAr13!,{r0-r3,r12,pc}^;4:thenreturn
- MRSr2,SPSR;5:copySPSR_irq
- MOVr0,r13;5:copyr13_irq
- ADDr13,r13,#6*4;5:resetstack
- MRSr1,CPSR;6:copyCPSR
- BICr1,r1,#Maskmd;6:
- ORRr1,r1,#SVC32md;6:
- MSRCPSR_c,r1;6:changeSVCmode
- SUBr13,r13,#FRAME_SIZE-FRAME_R4;7:makestackspace
- STMIAr13,{r4-r11};7:saver4-r11
- LDMIAr0,{r4-r9};7:r4-r9IRQstack
- BICr1,r1,#I_Bit;8:
- MSRCPSR_c,r1;8:enableint
- STMDBr13!,{r4-r7};9:saver4-r7SVC
- STRr2,[r13,#FRAME_PSR];9:savePSR
- STRr8,[r13,#FRAME_R12];9:saver12
- STRr9,[r13,#FRAME_PC];9:savepc
- STRr14,[r13,#FRAME_LR];9:savelr
- ;
- LDMIAr13!,{r0-r12,r14};11:restorecontext
- MSRSPSR_cxsf,r14;11:restoreSPSR
- LDMIAr13!,{r14,pc}^;11:return
- read_RescheduleFlag
- ;
- MOVr0,#0;moreprocessing
- MOVpc,r14;return
- END
代码的要害便是在中止后切换到SVC形式下,使用svc mode的stack来完成中止嵌套进程的备份以及康复操作.从代码中能够看到,从R0到PC都在栈中有备份,这儿咱们叫栈帧.记住《深化了解计算机体系》一书在讲x86汇编的函数调用时也是栈帧的概念.这点上中止嵌套和函数调用有相似之处.有了这个栈帧,使用压栈出栈操作就全部ok了.
刚看这个代码,对有个当地有疑问,便是觉得中止开早了:
BIC r1,r1,#I_Bit ; 8 :
MSR CPSR_c,r1 ; 8 : enable int
STMDB r13!,{r4-r7} ; 9 : save r4-r7 SVC
STR r2,[r13,#FRAME_PSR] ; 9 : save PSR
STR r8,[r13,#FRAME_R12] ; 9 : save r12
STR r9,[r13,#FRAME_PC] ; 9 : save pc
STR r14,[r13,#FRAME_LR] ; 9 : save lr
觉得开中止的代码应该放在后边,这样才干确保svc mode下的stack frame不会被损坏.但在草稿纸上画一下irq和svc下的stack图,就发现仓库操作并没有问题.能够假定刚开中止立马就有新的中止了,r4-r7 r8 r9都有在STMIA r13,{r4-r11} 中保存到svc的stack中,LDMIA r0,{r4-r9} 和STMDB r13!,{r4-r7} 确保了开始的r0-r3在栈中,而LDMIA r0,{r4-r9}和STR r8,[r13,#FRAME_R12] 以及STR r9,[r13,#FRAME_PC] 确保了R12和PC,确保正确回来.(这儿的r9装的是r14_irq,所以pc便是r14_irq,这样就确保了从中止服务例程中回来).至于STR r14,[r13,#FRAME_LR]中的r14是r14_svc,将其压入svc的stack中,中止例程用bl就不会呈现错误了,在最终LDMIA r13!,{r14,pc}^ 中r14得到康复.而r2保存的是spsr,也便是svc形式的状况,一向不变,不必忧虑会被掩盖.
最终,再看了一遍图,觉得r10和r11的帧能够省去,由于r4-r9是用来存atpcs的r0-r3,r12,r14,而r10和r11用不到.形似能够省点空间和时间,详细的待会试验一下.
下面给出实践的嵌套中止处理,使用r10来保存INTOFFSET的值,依据该值来断定是什么中止,然后做不同的处理.详细的作用是:代码会做流水灯的动作,Key1代表INT1,中止处理动作是4个灯全全亮然后全灭,Key4代表代表INT0,中止处理动作是第一个灯和第三个灯亮,然后第二个灯和第四个灯亮.
- /*
- simpleinterruption
- copyleft@dndxhej@gmail.com
- */
- .equMaskmd,0x1f@processormodemask
- .equSVC32md,0x13@SVCmode
- .equI_Bit,0x80@IRQbit
- .equFRAME_R0,0x00
- .equFRAME_R1,FRAME_R0+4
- .equFRAME_R2,FRAME_R1+4
- .equFRAME_R3,FRAME_R2+4
- .equFRAME_R4,FRAME_R3+4
- .equFRAME_R5,FRAME_R4+4
- .equFRAME_R6,FRAME_R5+4
- .equFRAME_R7,FRAME_R6+4
- .equFRAME_R8,FRAME_R7+4
- .equFRAME_R9,FRAME_R8+4
- .equFRAME_R10,FRAME_R9+4
- .equFRAME_R11,FRAME_R10+4
- .equFRAME_R12,FRAME_R11+4
- .equFRAME_PSR,FRAME_R12+4
- .equFRAME_LR,FRAME_PSR+4
- .equFRAME_PC,FRAME_LR+4
- .equFRAME_SIZE,FRAME_PC+4
- .equNOINT,0xc0
- .equWTCON,0x53000000
- .equGPBCON,0x56000010@led
- .equGPBDAT,0x56000014@led
- .equGPBUP,0x56000018@led
- .equGPFCON,0x56000050@interruptconfig
- .equEINTMASK,0x560000a4
- .equEXTINT0,0x56000088
- .equEXTINT1,0x5600008c
- .equEXTINT2,0x56000090
- .equINTMSK,0x4A000008
- .equEINTPEND,0x560000a8
- .equINTSUBMSK,0X4A00001C
- .equSRCPND,0X4A000000
- .equINTPND,0X4A000010
- .equINTOFFSET,0x4A000014
- .global_start
- _start:breset
- ldrpc,_undefined_instruction
- ldrpc,_software_interrupt
- ldrpc,_prefetch_abort
- ldrpc,_data_abort
- ldrpc,_not_used
- @birq
- ldrpc,_irq
- ldrpc,_fiq
- _undefined_instruction:.wordundefined_instruction
- _software_interrupt:.wordsoftware_interrupt
- _prefetch_abort:.wordprefetch_abort
- _data_abort:.worddata_abort
- _not_used:.wordnot_used
- _irq:.wordirq
- _fiq:.wordfiq
- .balignl16,0xdeadbeef
- reset:
- ldrr3,=WTCON
- movr4,#0x0
- strr4,[r3]@disablewatchdog
- ldrr0,=GPBCON
- ldrr1,=0x15400
- strr1,[r0]
- ldrr2,=GPBDAT
- ldrr1,=0x160
- strr1,[r2]
- bldelay
- msrcpsr_c,#0xd2@进入中止形式
- ldrsp,=0xc00@中止形式的栈指针界说
- msrcpsr_c,#0xd3@进入svc形式
- ldrsp,=0xfff@设置svc形式的栈指针
- @——————————————–
- ldrr0,=GPBUP
- ldrr1,=0x03f0
- strr1,[r0]
-