您的位置:首页 > 运维架构

17. OP-TEE中的中断处理(三)------系统IRQ事件的处理

2017-05-28 01:35 1246 查看
  IRQ事件的处理一般会用在ROS端,但是当CPU为是secure world态时,系统产生了IRQ事件,而该事件又不能被暴力的作为无用事件而轻易丢弃,系统还是需要有响应并执行相关操作的。那针对该种情况,在在OP-TEE文件当,针对这种情况的处理方式和逻辑图如下,本文将从介绍secure world态下对IRQ事件的处理逻辑:



1. OP-TEE中对IRQ事件的响应

  在系统初始化的时候,系统会调用thread_init_vbar函数来初始化secure world态的中断向量表并将中断向量的地址保存到VBAR寄存器中。所以在secure world态中产生了IRQ事件的时候,系统将会通过VBAR寄存器获取到中断向量表地址,然后命中写入的IRQ事件处理函数thread_irq_handler函数并逐步执行解析IRQ事件并处理之,整个处理过程的流程图如下图所示:



1.1
thread_irq_handler函数

  在《15. OP-TEE中的中断处理(一)------中断配置和向量表的配置》介绍了在secure world态中中断向量表的初始化,thread_vect_table即为secure world态的中断向量,当IRQ事件发生时,系统将调用thread_irq_handler函数来对IRQ事件进行处理,该函数定义在optee_os/core/arch/arm/kernel/thread_a32.S文件中,其内容如下:

LOCAL_FUNC thread_irq_handler , :
UNWIND( .fnstart)
UNWIND( .cantunwind)
#if defined(CFG_ARM_GICV3)
native_intr_handler irq
#else
foreign_intr_handler irq
#endif
UNWIND( .fnend)
END_FUNC thread_irq_handler  由于没有使能GICV3,所以最终会调用foreign_intr_handler宏来对IRQ事件进行处理。

1.2
foreign_intr_handler宏

  foreign_intr_handler宏将suspend系统当前正在执行的thread并进行相关的设置后调用smc指令出发smc时间,将cpu状态切换到monitor态进行进一步处理。

.macro foreign_intr_handler mode:req
.ifc \mode\(),irq
/*
* Disable FIQ if the foreign interrupt is sent as IRQ.
* IRQ mode is set up to use tmp stack so FIQ has to be
* disabled before touching the stack. We can also assign
* SVC sp from IRQ sp to get SVC mode into the state we
* need when doing the SMC below.
* If it is sent as FIQ, the IRQ has already been masked by hardware
*/
cpsid f
.endif
sub lr, lr, #4
push {lr}
push {r12}

.ifc \mode\(),fiq
bl thread_save_state_fiq
.else
bl thread_save_state
.endif

mov r0, #THREAD_FLAGS_EXIT_ON_FOREIGN_INTR
mrs r1, spsr
pop {r12}
pop {r2}
blx thread_state_suspend //suspend当前系统中thread
mov r4, r0 /* Supply thread index */

/*
* Switch to SVC mode and copy current stack pointer as it already
* is the tmp stack.
*/
mov r0, sp
cps #CPSR_MODE_SVC
mov sp, r0

ldr r0, =TEESMC_OPTEED_RETURN_CALL_DONE
ldr r1, =OPTEE_SMC_RETURN_RPC_FOREIGN_INTR
mov r2, #0
mov r3, #0
/* r4 is already filled in above */
smc #0 //调用smc,出发smc事件将CPU状态切换到monitor执行进一步的处理
b . /* SMC should not return */
.endm

1.3 monitor态下的smc处理

  当系统触发smc之后,cpu进入到monitor态,并进入到monitor态下的smc处理函数sm_smc_entry,当执行smc之后相当于产生了类似软中断的操作,而该为何执行smc之后CPU会执行sm_smc_entry函数请阅读《16. OP-TEE中的中断处理(二)------系统FIQ事件的处理》中的1.1章节。sm_smc_entry函数定义在optee_os/core/arch/arm/sm/sm_a32.S文件中,其内容如下:

LOCAL_FUNC sm_smc_entry , :
UNWIND( .fnstart)
UNWIND( .cantunwind)
srsdb sp!, #CPSR_MODE_MON
push {r0-r7}

clrex /* Clear the exclusive monitor */

/* Find out if we're doing an secure or non-secure entry */
read_scr r1
tst r1, #SCR_NS
bne .smc_from_nsec

/*
* As we're coming from secure world (NS bit cleared) the stack
* pointer points to sm_ctx.sec.r0 at this stage. After the
* instruction below the stack pointer points to sm_ctx.
*/
sub sp, sp, #(SM_CTX_SEC + SM_SEC_CTX_R0)

/* Save secure context */
/* 保存secure world的context */
add r0, sp, #SM_CTX_SEC
bl sm_save_modes_regs

/*
* On FIQ exit we're restoring the non-secure context unchanged, on
* all other exits we're shifting r1-r4 from secure context into
* r0-r3 in non-secure context.
*/
/* 配置好传递到non-secure world的参数 */
add r8, sp, #(SM_CTX_SEC + SM_SEC_CTX_R0)
ldm r8, {r0-r4}
mov_imm r9, TEESMC_OPTEED_RETURN_FIQ_DONE
cmp r0, r9
addne r8, sp, #(SM_CTX_NSEC + SM_NSEC_CTX_R0)
stmne r8, {r1-r4}

/* Restore non-secure context */
/* 加载non-secure world态的context */
add r0, sp, #SM_CTX_NSEC
bl sm_restore_modes_regs

/* 返回到non-secure world态 */
.sm_ret_to_nsec:
/*
* Return to non-secure world
*/
add r0, sp, #(SM_CTX_NSEC + SM_NSEC_CTX_R8)
ldm r0, {r8-r12}

/* Update SCR */
read_scr r0
orr r0, r0, #(SCR_NS | SCR_FIQ) /* Set NS and FIQ bit in SCR */
write_scr r0

add sp, sp, #(SM_CTX_NSEC + SM_NSEC_CTX_R0)
b .sm_exit //退出smc操作,切换到non-secure world态

.smc_from_nsec:
/*
* As we're coming from non-secure world (NS bit set) the stack
* pointer points to sm_ctx.nsec.r0 at this stage. After the
* instruction below the stack pointer points to sm_ctx.
*/
sub sp, sp, #(SM_CTX_NSEC + SM_NSEC_CTX_R0)

bic r1, r1, #(SCR_NS | SCR_FIQ) /* Clear NS and FIQ bit in SCR */
write_scr r1

add r0, sp, #(SM_CTX_NSEC + SM_NSEC_CTX_R8)
stm r0, {r8-r12}

mov r0, sp
bl sm_from_nsec
cmp r0, #0
beq .sm_ret_to_nsec

/*
* Continue into secure world
*/
add sp, sp, #(SM_CTX_SEC + SM_SEC_CTX_R0)

.sm_exit:
pop {r0-r7}
rfefd sp!
UNWIND( .fnend)
END_FUNC sm_smc_entry

1.4 返回到secure world态中继续执行

  当monitor态将IRQ事件传递到了non-secure world后并将CPU的状态切换到non-secure world态之后,ROS将根据具体得到的参数执行具体的IRQ事件的处理。当完成IRQ事件处理自后,会调用smc重新切回到monitor态,然后恢复secure world中被中断的thread状态继续执行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  OP-TEE IRQ