1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved. 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu #include <assert.h> 8*91f16700Schasinglulu #include <string.h> 9*91f16700Schasinglulu 10*91f16700Schasinglulu #include <arch_helpers.h> 11*91f16700Schasinglulu #include <common/bl_common.h> 12*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h> 13*91f16700Schasinglulu #include <lib/utils.h> 14*91f16700Schasinglulu 15*91f16700Schasinglulu #include "opteed_private.h" 16*91f16700Schasinglulu 17*91f16700Schasinglulu /******************************************************************************* 18*91f16700Schasinglulu * Given a OPTEE entrypoint info pointer, entry point PC, register width, 19*91f16700Schasinglulu * cpu id & pointer to a context data structure, this function will 20*91f16700Schasinglulu * initialize OPTEE context and entry point info for OPTEE. 21*91f16700Schasinglulu ******************************************************************************/ 22*91f16700Schasinglulu void opteed_init_optee_ep_state(struct entry_point_info *optee_entry_point, 23*91f16700Schasinglulu uint32_t rw, uint64_t pc, 24*91f16700Schasinglulu uint64_t pageable_part, uint64_t mem_limit, 25*91f16700Schasinglulu uint64_t dt_addr, optee_context_t *optee_ctx) 26*91f16700Schasinglulu { 27*91f16700Schasinglulu uint32_t ep_attr; 28*91f16700Schasinglulu 29*91f16700Schasinglulu /* Passing a NULL context is a critical programming error */ 30*91f16700Schasinglulu assert(optee_ctx); 31*91f16700Schasinglulu assert(optee_entry_point); 32*91f16700Schasinglulu assert(pc); 33*91f16700Schasinglulu 34*91f16700Schasinglulu /* Associate this context with the cpu specified */ 35*91f16700Schasinglulu optee_ctx->mpidr = read_mpidr_el1(); 36*91f16700Schasinglulu optee_ctx->state = 0; 37*91f16700Schasinglulu set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_OFF); 38*91f16700Schasinglulu 39*91f16700Schasinglulu cm_set_context(&optee_ctx->cpu_ctx, SECURE); 40*91f16700Schasinglulu 41*91f16700Schasinglulu /* initialise an entrypoint to set up the CPU context */ 42*91f16700Schasinglulu ep_attr = SECURE | EP_ST_ENABLE; 43*91f16700Schasinglulu if (read_sctlr_el3() & SCTLR_EE_BIT) 44*91f16700Schasinglulu ep_attr |= EP_EE_BIG; 45*91f16700Schasinglulu SET_PARAM_HEAD(optee_entry_point, PARAM_EP, VERSION_1, ep_attr); 46*91f16700Schasinglulu optee_entry_point->pc = pc; 47*91f16700Schasinglulu if (rw == OPTEE_AARCH64) 48*91f16700Schasinglulu optee_entry_point->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, 49*91f16700Schasinglulu DISABLE_ALL_EXCEPTIONS); 50*91f16700Schasinglulu else 51*91f16700Schasinglulu optee_entry_point->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM, 52*91f16700Schasinglulu SPSR_E_LITTLE, 53*91f16700Schasinglulu DAIF_FIQ_BIT | 54*91f16700Schasinglulu DAIF_IRQ_BIT | 55*91f16700Schasinglulu DAIF_ABT_BIT); 56*91f16700Schasinglulu zeromem(&optee_entry_point->args, sizeof(optee_entry_point->args)); 57*91f16700Schasinglulu optee_entry_point->args.arg0 = pageable_part; 58*91f16700Schasinglulu optee_entry_point->args.arg1 = mem_limit; 59*91f16700Schasinglulu optee_entry_point->args.arg2 = dt_addr; 60*91f16700Schasinglulu } 61*91f16700Schasinglulu 62*91f16700Schasinglulu /******************************************************************************* 63*91f16700Schasinglulu * This function takes an OPTEE context pointer and: 64*91f16700Schasinglulu * 1. Applies the S-EL1 system register context from optee_ctx->cpu_ctx. 65*91f16700Schasinglulu * 2. Saves the current C runtime state (callee saved registers) on the stack 66*91f16700Schasinglulu * frame and saves a reference to this state. 67*91f16700Schasinglulu * 3. Calls el3_exit() so that the EL3 system and general purpose registers 68*91f16700Schasinglulu * from the optee_ctx->cpu_ctx are used to enter the OPTEE image. 69*91f16700Schasinglulu ******************************************************************************/ 70*91f16700Schasinglulu uint64_t opteed_synchronous_sp_entry(optee_context_t *optee_ctx) 71*91f16700Schasinglulu { 72*91f16700Schasinglulu uint64_t rc; 73*91f16700Schasinglulu 74*91f16700Schasinglulu assert(optee_ctx != NULL); 75*91f16700Schasinglulu assert(optee_ctx->c_rt_ctx == 0); 76*91f16700Schasinglulu 77*91f16700Schasinglulu /* Apply the Secure EL1 system register context and switch to it */ 78*91f16700Schasinglulu assert(cm_get_context(SECURE) == &optee_ctx->cpu_ctx); 79*91f16700Schasinglulu cm_el1_sysregs_context_restore(SECURE); 80*91f16700Schasinglulu cm_set_next_eret_context(SECURE); 81*91f16700Schasinglulu 82*91f16700Schasinglulu rc = opteed_enter_sp(&optee_ctx->c_rt_ctx); 83*91f16700Schasinglulu #if ENABLE_ASSERTIONS 84*91f16700Schasinglulu optee_ctx->c_rt_ctx = 0; 85*91f16700Schasinglulu #endif 86*91f16700Schasinglulu 87*91f16700Schasinglulu return rc; 88*91f16700Schasinglulu } 89*91f16700Schasinglulu 90*91f16700Schasinglulu 91*91f16700Schasinglulu /******************************************************************************* 92*91f16700Schasinglulu * This function takes an OPTEE context pointer and: 93*91f16700Schasinglulu * 1. Saves the S-EL1 system register context tp optee_ctx->cpu_ctx. 94*91f16700Schasinglulu * 2. Restores the current C runtime state (callee saved registers) from the 95*91f16700Schasinglulu * stack frame using the reference to this state saved in opteed_enter_sp(). 96*91f16700Schasinglulu * 3. It does not need to save any general purpose or EL3 system register state 97*91f16700Schasinglulu * as the generic smc entry routine should have saved those. 98*91f16700Schasinglulu ******************************************************************************/ 99*91f16700Schasinglulu void opteed_synchronous_sp_exit(optee_context_t *optee_ctx, uint64_t ret) 100*91f16700Schasinglulu { 101*91f16700Schasinglulu assert(optee_ctx != NULL); 102*91f16700Schasinglulu /* Save the Secure EL1 system register context */ 103*91f16700Schasinglulu assert(cm_get_context(SECURE) == &optee_ctx->cpu_ctx); 104*91f16700Schasinglulu cm_el1_sysregs_context_save(SECURE); 105*91f16700Schasinglulu 106*91f16700Schasinglulu assert(optee_ctx->c_rt_ctx != 0); 107*91f16700Schasinglulu opteed_exit_sp(optee_ctx->c_rt_ctx, ret); 108*91f16700Schasinglulu 109*91f16700Schasinglulu /* Should never reach here */ 110*91f16700Schasinglulu assert(0); 111*91f16700Schasinglulu } 112