在bl31中的runtime_svc_init函数会初始化OP-TEE对应的service,通过调用该service的init函数来完成OP-TEE的启动。
1. opteed_setup
OP-TEE的service通过DECLARE_RT_SVC宏在编译的时候被存放到了rt_svc_des段中。启动该段中的init成员会被初始化成opteed_setup函数,该函数的内容如下:
int32_t opteed_setup(void)
{
entry_point_info_t *optee_ep_info;
uint32_t linear_id;
linear_id = plat_my_core_pos();
/*
* Get information about the Secure Payload (BL32) image. Its
* absence is a critical failure. TODO: Add support to
* conditionally include the SPD service
*/
/* 获取bl32(OP-TEE)镜像的描述信息 */
optee_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
if (!optee_ep_info) {
WARN("No OPTEE provided by BL2 boot loader, Booting device"
" without OPTEE initialization. SMC`s destined for OPTEE"
" will return SMC_UNK\n");
return 1;
}
/*
* If there's no valid entry point for SP, we return a non-zero value
* signalling failure initializing the service. We bail out without
* registering any handlers
*/
if (!optee_ep_info->pc)
return 1;
/*
* We could inspect the SP image and determine it's execution
* state i.e whether AArch32 or AArch64. Assuming it's AArch32
* for the time being.
*/
opteed_rw = OPTEE_AARCH64;
/* 初始化CPU的smc上下文 */
opteed_init_optee_ep_state(optee_ep_info,
opteed_rw,
optee_ep_info->pc,
&opteed_sp_context[linear_id]);
/*
* All OPTEED initialization done. Now register our init function with
* BL31 for deferred invocation
*/
/* 使用Opteed_init初始化bl32_init变量,以备在bl31中调用 */
bl31_register_bl32_init(&opteed_init);
return 0;
}
2. opteed_init
该函数将会在bl31中的所有service执行完init操作之后,通过执行存放在bl32_init变量中的函数指针来实现调用。该函数调用之后会进入到OP-TEE OS的初始化阶段。
static int32_t opteed_init(void)
{
uint32_t linear_id = plat_my_core_pos();
optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
entry_point_info_t *optee_entry_point;
uint64_t rc;
/*
* Get information about the OPTEE (BL32) image. Its
* absence is a critical failure.
*/
/* 获取OPTEE image的信息 */
optee_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
assert(optee_entry_point);
/* 使用optee image的entry point信息初始化cpu的上下文 */
cm_init_my_context(optee_entry_point);
/*
* Arrange for an entry into OPTEE. It will be returned via
* OPTEE_ENTRY_DONE case
*/
/* 开始设置CPU参数,最终会调用opteed_enter_sp函数执行跳转到OPTEE的操作 */
rc = opteed_synchronous_sp_entry(optee_ctx);
assert(rc != 0);
return rc;
}
3. opteed_enter_sp
opteed_entr_sp函数完成跳转到OP-TEE image进行执行的操作,该函数中将会保存一些列的寄存器值,设定好堆栈信息,然后通过调用el3_eixt函数来实现跳转操作
func opteed_enter_sp
/* Make space for the registers that we're going to save */
mov x3, sp
str x3, [x0, #0]
sub sp, sp, #OPTEED_C_RT_CTX_SIZE
/* Save callee-saved registers on to the stack */
stp x19, x20, [sp, #OPTEED_C_RT_CTX_X19]
stp x21, x22, [sp, #OPTEED_C_RT_CTX_X21]
stp x23, x24, [sp, #OPTEED_C_RT_CTX_X23]
stp x25, x26, [sp, #OPTEED_C_RT_CTX_X25]
stp x27, x28, [sp, #OPTEED_C_RT_CTX_X27]
stp x29, x30, [sp, #OPTEED_C_RT_CTX_X29]
/* ---------------------------------------------
* Everything is setup now. el3_exit() will
* use the secure context to restore to the
* general purpose and EL3 system registers to
* ERET into OPTEE.
* ---------------------------------------------
*/
b el3_exit //使用设定好的安全CPU上下文,退出EL3进入OPTEE
endfunc opteed_enter_sp