中断处理代码分析:
.macro hal_vectors_init
//中断向量初始化,packages/hal/mips/ref4955/v2_0/include/platform.inc
# If we don~t play nice with a ROM monitor, copy the required
# vectors into the proper location.
la t0,0x80000000 # dest addr
//有些MIPS变种不需要处理tlb的异常
la t1,utlb_vector # source addr
la t3,utlb_vector_end # end dest addr
1:
lw v0,0(t1) # get word
addi t1,t1,4
sw v0,0(t0) # write word
addi t0,t0,4
bne t1,t3,1b
nop
la t0,0x80000180 # dest addr
//将other_vector拷贝到0x80000180处,完成中断向量的安装
la t1,other_vector # source addr
la t3,other_vector_end # end dest addr
1:
lw v0,0(t1) # get word
addi t1,t1,4
sw v0,0(t0) # write word
addi t0,t0,4
bne t1,t3,1b
nop
.endm
.section ".other_vector","ax"
//中断处理代码安装的位置
# Common vector at 0x80000080 or 0xBFC00180
FUNC_START(other_vector)
//MIPS异常处理程序的入口,packages/hal/mips/arch/v2_0/src/vectors.S
mfc0 k0,cause # K0 = exception cause
//读取cause寄存器
nop
andi k0,k0,0x7F # isolate exception code
//取cause寄存器中的ExcCode值
la k1,hal_vsr_table # address of VSR table
//取VSR表的基地址
add k1,k1,k0 # offset of VSR entry
//根据ExcCode值取得VSR表的入口地址
lw k1,0(k1) # k1 = pointer to VSR
//取得VSR表中对应处理程序的地址
jr k1 # go there
//跳转执行
nop # (delay slot)
FUNC_END(other_vector)
## VSR table.
## The main interrupt code indirects through here to find the VSR
## to execute for each architecture defined interrupt.
## This is only used for simulated targets, on real targets a fixed location VSR
## table is now allocated at 0x80000100.
#ifndef CYG_HAL_MIPS_VSR_TABLE_DEFINED
## .section ".vsr_table","a"
//VSR表
.data
//VSR表在data段中
.globl hal_vsr_table
hal_vsr_table:
.long __default_interrupt_vsr
//定义了64个interrupt入口
.rept 63
.long __default_exception_vsr
.endr
#endif
## Default interrupt VSR.
## Saves machine state and calls appropriate ISR. When done, calls
## interrupt_end() to finish up and possibly reschedule.
FUNC_START(__default_interrupt_vsr)
//缺省的中断处理程序
# We enter here with all of the CPU state still
# in its registers except:
# K0 = vector index
# K1 = address of this function
move k1,sp # K1 = original SP
//保存原始SP
addi sp,sp,-mips_exception_decrement
//分配stack空间
# space for registers + safety margin
sw k0,mipsreg_vector(sp) # store vector
//将异常类型(即ExcCode值)存入堆栈
# store GPRs
//保存通用寄存器
.set noat
sgpr 0,sp
sgpr 1,sp
sgpr 2,sp
sgpr 3,sp
sgpr 4,sp
sgpr 5,sp
sgpr 6,sp
sgpr 7,sp
sgpr 8,sp
sgpr 9,sp
sgpr 10,sp
sgpr 11,sp
sgpr 12,sp
sgpr 13,sp
sgpr 14,sp
sgpr 15,sp
sgpr 16,sp
sgpr 17,sp
sgpr 18,sp
sgpr 19,sp
sgpr 20,sp
sgpr 21,sp
sgpr 22,sp
sgpr 23,sp
sgpr 24,sp
sgpr 25,sp
# sgpr 26,sp # == K0
# sgpr 27,sp # == K1
sgpr 28,sp # == GP
# sgpr 29,sp # == SP
sgpr 30,sp # == FP
sgpr 31,sp # == RA
.set at
mfhi a0
//保存hi和lo寄存器
mflo a1
shi a0,sp
slo a1,sp
# K1 contains original SP
ssp k1,sp # store in reg dump
//保存原始SP到新的stack中的特殊位置上
mfc0 t1,status
//保存status,cachectrl和epc寄存器
mfc0 t2,cachectrl
mvafc0 t3,epc
sw t1,mipsreg_sr(sp)
sw t2,mipsreg_cachectrl(sp)
sva t3,mipsreg_pc(sp)
//epc寄存器
hal_fpu_save sp
//保存浮点寄存器,packages/hal/mips/arch/v2_0/include/arch.inc,如果处理器没有fpu则该宏为空,Lupine没有浮点寄存器
//到此为止处理器的状态已经保存到栈上了
# The machine state is now all saved on the stack.
# Load Global Pointer register.
la gp,_gp
//加载gp指针
#ifdef CYGFUN_HAL_COMMON_KERNEL_SUPPORT
.extern cyg_scheduler_sched_lock
la v0,cyg_scheduler_sched_lock
//锁住调度器
lw a0,0(v0)
addi a0,a0,1
sw a0,0(v0)
#endif
move s0,sp # save pointer to saved state
//取sp指针,留做后用
#ifdef CYGIMP_HAL_COMMON_INTERRUPTS_USE_INTERRUPT_STACK
//如果中断使用独立堆栈
la a0,__interrupt_stack # a0 = stack top
//栈顶指针
la a1,__interrupt_stack_base # a1 = stack base
//栈基址,定义如下:
.bss
.balign 16
.global cyg_interrupt_stack_base
cyg_interrupt_stack_base
__interrupt_stack_base:
.rept CYGNUM_HAL_COMMON_INTERRUPTS_STACK_SIZE //stack的大小
.byte 0
.endr
sub a3,sp,a1 # a3 = sp - base
//现在的sp是否比__interrupt_stack_base小
bltz a3,1f # not on istack if < 0
nop # delay slot
sub t0,a0,sp # t0 = top - sp
//现在的sp是否比__interrupt_stack大
bgtz t0,8f # already on istack if > 0
nop # delay slot
1:
move sp,a0 # switch to istack
//not on istack
8:
addi sp,sp,-8 # space for old SP
//on istack,为保存现在的sp留出空间
# (8 to keep dword alignment!)
sw s0,0(sp) # save old SP on stack
//保存现在的sp
#endif
subu sp,sp,mips_stack_frame_size # make a null frame
//做一个空的栈帧(32bytes)
# Need to set up back pointers etc. ???
# Decode external interrupt via interrupt controller
hal_intc_decode s2
//根据cause寄存器的IP位解码,返回IP号,packages/hal/mips/tx39/v2_0/include/variant.inc
# Here, s2 contains the number of the interrupt being serviced,
# we need to derive from that the vector number to call in the ISR
# table.
hal_intc_translate s2,s1
//packages/hal/mips/tx39/v2_0/include/variant.inc
# Here s1 is the number of the vector to be called and s2 is
# the number of the interrupt being serviced.
hal_diag_intr_start
//中断诊断程序,可以不实现,packages/hal/mips/vrc437x/v2_0/include/platform.inc
#if defined(CYGPKG_KERNEL_INSTRUMENT) && defined(CYGDBG_KERNEL_INSTRUMENT_INTR)
# Call cyg_instrument to record that this interrupt is being raised.
//记录程序
li a0,0x0301 # a0 = type = INTR,RAISE
move a1,s1 # a1 = vector number
jal cyg_instrument # call instrument function
move a2,s2 # a2 = interrupt number
#endif
#if defined(CYGDBG_HAL_MIPS_DEBUG_GDB_CTRLC_SUPPORT)
# If we are supporting Ctrl-C interrupts from GDB, we must squirrel
# away a pointer to the save interrupt state here so that we can
# plant a breakpoint at some later time.
.extern hal_saved_interrupt_state
//使用GDB stub调试时,用于保存中断状态
la v0,hal_saved_interrupt_state
sw s0,0(v0)
//s0是sp指针
#endif
sll s1,s1,2 # s1 = byte offset of vector
//根据中断向量号计算中断向量偏移
//在此之前都是禁用中断的
hal_cpu_except_enable # reenable exceptions
//启用中断,packages/hal/mips/arch/v2_0/include/arch.inc
la t2,hal_interrupt_handlers # handler table
//加载中断处理程序表,如果未定以CYG_HAL_MIPS_ISR_TABLES_DEFINED,该表在packages/hal/mips/arch/v2_0/src/vectors.S中,否则根据platform不同有不同定义,如packages/hal/mips/tx39/v2_0/src/variant.S
add t2,t2,s1 # address of ISR ptr
//根据中断向量号计算在中断处理程序表中的偏移
lw t2,0(t2) # ISR pointer
//取中断处理程序地址
la a1,hal_interrupt_data # data table
//加载中断处理程序数据表,定义位置与hal_interrupts_handlers一样
add a1,a1,s1 # address of data ptr
//计算中断处理程序数据偏移
lw a1,0(a1) # Data pointer
//将中断处理程序数据地址存入a1,作为参数
move a0,s2 # pass interrupt number
//将中断号存入a0寄存器,作为参数
jalr t2 # call ISR via t2
//调用具体的中断处理程序,有两个参数,分别是中断向量号和中断处理程序数据
nop # (delay slot)
#ifdef CYGIMP_HAL_COMMON_INTERRUPTS_USE_INTERRUPT_STACK
# If we are returning from the last nested interrupt, move back
# to the thread stack. interrupt_end() must be called on the
# thread stack since it potentially causes a context switch.
# Since we have arranged for the top of stack location to
# contain the sp we need to go back to here, just pop it off
# and put it in SP.
lw sp,mips_stack_frame_size(sp) # sp = *sp
subu sp,sp,mips_stack_frame_size # make a null frame
#endif
#ifdef CYGFUN_HAL_COMMON_KERNEL_SUPPORT
# We only need to call _interrupt_end() when there is a kernel
# present to do any tidying up.
# On return v0 bit 1 will indicate whether a DSR is
# to be posted. Pass this together with a pointer to
# the interrupt object we have just used to the
# interrupt tidy up routine.
# Note that s0, s1 and s2 are defined to be preserved across
# calls by the calling convention, so they still contain
# the register dump, the vector offset and the interrupt number
# respectively.
move s2,v0
//临时保存中断处理程序的返回值
la a1,hal_interrupt_objects # interrupt object table
//取中断对象,定义位置与hal_interrupt_handlers一样,将地址存入a1,用作参数
add a1,a1,s1 # address of object ptr
lw a1,0(a1) # a1 = object ptr
move a2,s0 # arg3 = saved register dump
//堆栈指针存入a2,作为参数
.extern interrupt_end
jal interrupt_end # call into C to finish off
//调用interrupt_end做后面的处理(挂起DSR,解锁调度器,执行DSR),有三个参数,分别是ISR返回值,中断对象,堆栈指针,此处可能导致线程上下文切换
move a0,v0 # put ISR result in arg0
//将ISR返回值放入a0,用作参数
move v0,s2 # return value from isr
//恢复ISR返回值
#endif
restore_state:
//从中断处理返回的例程
#if defined(CYGSEM_HAL_USE_ROM_MONITOR_CygMon)
move k0,v0
#endif
# All done, restore CPU state and continue
addi sp,sp,mips_stack_frame_size # retrieve CPU state ptr
//释放这块堆栈
# Disable interrupts again while we restore state.
hal_cpu_int_disable
//再次禁用中断直到现场恢复,packages/hal/mips/arch/v2_0/include/arch.inc
hal_diag_restore
//中断恢复的诊断程序
hal_fpu_load sp
//恢复fpu寄存器,packages/hal/mips/arch/v2_0/include/arch.inc
lw t0,mipsreg_cachectrl(sp)
//恢复cachectrl,hi和lo寄存器
lhi t1,sp
llo t2,sp
mtc0 t0,cachectrl
mthi t1
mtlo t2
# load GPRs
//恢复通用寄存器
.set noat
# lgpr 0,sp
lgpr 1,sp
lgpr 2,sp
lgpr 3,sp
lgpr 4,sp
lgpr 5,sp
lgpr 6,sp
lgpr 7,sp
lgpr 8,sp
lgpr 9,sp
lgpr 10,sp
lgpr 11,sp
lgpr 12,sp
lgpr 13,sp
lgpr 14,sp
lgpr 15,sp
lgpr 16,sp
lgpr 17,sp
lgpr 18,sp
lgpr 19,sp
lgpr 20,sp
lgpr 21,sp
lgpr 22,sp
lgpr 23,sp
lgpr 24,sp
lgpr 25,sp
# lgpr 26,sp # == K0
# lgpr 27,sp # == K1
lgpr 28,sp # == GP
# lgpr 29,sp # == SP
lgpr 30,sp # == FP
lgpr 31,sp # == RA
.set at
#if defined(CYGSEM_HAL_USE_ROM_MONITOR_CygMon)
# If we have a Cygmon that wants to listen to network interrupts, then
# the return code from the earlier call to hal_default_isr() will
# have been negative to indicate this. So we jump into Cygmon here
# because Cygmon requires the processor state to be the same as when
# the interrupt was taken, but with k0 as the exception number.
bgez k0,1f
nop
# Check for new cygmon
sw k0,(mipsreg_regs+26*4)(sp) # save k0
la k1,0x80000100 + 41*4 # New cygmon "magic" id
lw k1,0(k1)
lui k0,0x55aa
ori k0,0x4321
bne k0,k1,1f
# Need to let cygmon handle this
la k1,0x80000100 + 39*4 # stub entry vector
lw k0,(mipsreg_regs+26*4)(sp) # restore k0
lw k1,0(k1)
lw sp,(mipsreg_regs+29*4)(sp) # restore SP
sll k0,1 # clear bit 31.
jr k1
srl k0,1
1:
#endif
lw k1,mipsreg_sr(sp) # K1 = saved SR
//恢复status寄存器
#if 0 < CYGINT_HAL_MIPS_INTERRUPT_RETURN_KEEP_SR_IM
# Keep the current settings of the IM[7:0] bits within the status
# register. These may be used as interrupt masks, so if an ISR or
# DSR masks interrupts they must be preserved.
# If they are not used, then this does no harm.
ori k0,zero,0xff00
nor k0,k0,k0 # 0xffff00ff
and k1,k1,k0 # all interrupts disabled
mfc0 k0,status # V0 = current SR
nop
nop
andi k0,k0,0xff00 # preserve interrupt set
//保留IM[7:0]
or k1,k1,k0 # insert into "saved SR"
#endif // 0 < CYGINT_HAL_MIPS_INTERRUPT_RETURN_KEEP_SR_IM
lva k0,mipsreg_pc(sp) # K0 = return PC
//取epc寄存器到k0中
lsp sp,sp # load SP
//恢复sp寄存器
# Invoke CPU specific mechanism for returning from this
# exception
hal_cpu_eret k0,k1
//从异常恢复,packages/hal/mips/arch/v2_0/include/arch.inc,k0是epc寄存器(即返回地址),k1是status寄存器,该宏定义如下:
.macro hal_cpu_eret pc,sr
mtc0 /sr,status # Load status register
nop
nop
nop
sync # settle things down
jr /pc # jump back to interrupted code
rfe # restore state (delay slot)
.endm
FUNC_END(__default_interrupt_vsr)