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 <bl32/tsp/tsp.h> 12*91f16700Schasinglulu #include <common/bl_common.h> 13*91f16700Schasinglulu #include <common/debug.h> 14*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h> 15*91f16700Schasinglulu #include <lib/utils.h> 16*91f16700Schasinglulu 17*91f16700Schasinglulu #include "tspd_private.h" 18*91f16700Schasinglulu 19*91f16700Schasinglulu /******************************************************************************* 20*91f16700Schasinglulu * Given a secure payload entrypoint info pointer, entry point PC, register 21*91f16700Schasinglulu * width, cpu id & pointer to a context data structure, this function will 22*91f16700Schasinglulu * initialize tsp context and entry point info for the secure payload 23*91f16700Schasinglulu ******************************************************************************/ 24*91f16700Schasinglulu void tspd_init_tsp_ep_state(struct entry_point_info *tsp_entry_point, 25*91f16700Schasinglulu uint32_t rw, 26*91f16700Schasinglulu uint64_t pc, 27*91f16700Schasinglulu tsp_context_t *tsp_ctx) 28*91f16700Schasinglulu { 29*91f16700Schasinglulu uint32_t ep_attr; 30*91f16700Schasinglulu 31*91f16700Schasinglulu /* Passing a NULL context is a critical programming error */ 32*91f16700Schasinglulu assert(tsp_ctx); 33*91f16700Schasinglulu assert(tsp_entry_point); 34*91f16700Schasinglulu assert(pc); 35*91f16700Schasinglulu 36*91f16700Schasinglulu /* 37*91f16700Schasinglulu * We support AArch64 TSP for now. 38*91f16700Schasinglulu * TODO: Add support for AArch32 TSP 39*91f16700Schasinglulu */ 40*91f16700Schasinglulu assert(rw == TSP_AARCH64); 41*91f16700Schasinglulu 42*91f16700Schasinglulu /* Associate this context with the cpu specified */ 43*91f16700Schasinglulu tsp_ctx->mpidr = read_mpidr_el1(); 44*91f16700Schasinglulu tsp_ctx->state = 0; 45*91f16700Schasinglulu set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF); 46*91f16700Schasinglulu clr_yield_smc_active_flag(tsp_ctx->state); 47*91f16700Schasinglulu 48*91f16700Schasinglulu cm_set_context(&tsp_ctx->cpu_ctx, SECURE); 49*91f16700Schasinglulu 50*91f16700Schasinglulu /* initialise an entrypoint to set up the CPU context */ 51*91f16700Schasinglulu ep_attr = SECURE | EP_ST_ENABLE; 52*91f16700Schasinglulu if (read_sctlr_el3() & SCTLR_EE_BIT) 53*91f16700Schasinglulu ep_attr |= EP_EE_BIG; 54*91f16700Schasinglulu SET_PARAM_HEAD(tsp_entry_point, PARAM_EP, VERSION_1, ep_attr); 55*91f16700Schasinglulu 56*91f16700Schasinglulu tsp_entry_point->pc = pc; 57*91f16700Schasinglulu tsp_entry_point->spsr = SPSR_64(MODE_EL1, 58*91f16700Schasinglulu MODE_SP_ELX, 59*91f16700Schasinglulu DISABLE_ALL_EXCEPTIONS); 60*91f16700Schasinglulu zeromem(&tsp_entry_point->args, sizeof(tsp_entry_point->args)); 61*91f16700Schasinglulu } 62*91f16700Schasinglulu 63*91f16700Schasinglulu /******************************************************************************* 64*91f16700Schasinglulu * This function takes an SP context pointer and: 65*91f16700Schasinglulu * 1. Applies the S-EL1 system register context from tsp_ctx->cpu_ctx. 66*91f16700Schasinglulu * 2. Saves the current C runtime state (callee saved registers) on the stack 67*91f16700Schasinglulu * frame and saves a reference to this state. 68*91f16700Schasinglulu * 3. Calls el3_exit() so that the EL3 system and general purpose registers 69*91f16700Schasinglulu * from the tsp_ctx->cpu_ctx are used to enter the secure payload image. 70*91f16700Schasinglulu ******************************************************************************/ 71*91f16700Schasinglulu uint64_t tspd_synchronous_sp_entry(tsp_context_t *tsp_ctx) 72*91f16700Schasinglulu { 73*91f16700Schasinglulu uint64_t rc; 74*91f16700Schasinglulu 75*91f16700Schasinglulu assert(tsp_ctx != NULL); 76*91f16700Schasinglulu assert(tsp_ctx->c_rt_ctx == 0); 77*91f16700Schasinglulu 78*91f16700Schasinglulu /* Apply the Secure EL1 system register context and switch to it */ 79*91f16700Schasinglulu assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx); 80*91f16700Schasinglulu cm_el1_sysregs_context_restore(SECURE); 81*91f16700Schasinglulu cm_set_next_eret_context(SECURE); 82*91f16700Schasinglulu 83*91f16700Schasinglulu rc = tspd_enter_sp(&tsp_ctx->c_rt_ctx); 84*91f16700Schasinglulu #if ENABLE_ASSERTIONS 85*91f16700Schasinglulu tsp_ctx->c_rt_ctx = 0; 86*91f16700Schasinglulu #endif 87*91f16700Schasinglulu 88*91f16700Schasinglulu return rc; 89*91f16700Schasinglulu } 90*91f16700Schasinglulu 91*91f16700Schasinglulu 92*91f16700Schasinglulu /******************************************************************************* 93*91f16700Schasinglulu * This function takes an SP context pointer and: 94*91f16700Schasinglulu * 1. Saves the S-EL1 system register context tp tsp_ctx->cpu_ctx. 95*91f16700Schasinglulu * 2. Restores the current C runtime state (callee saved registers) from the 96*91f16700Schasinglulu * stack frame using the reference to this state saved in tspd_enter_sp(). 97*91f16700Schasinglulu * 3. It does not need to save any general purpose or EL3 system register state 98*91f16700Schasinglulu * as the generic smc entry routine should have saved those. 99*91f16700Schasinglulu ******************************************************************************/ 100*91f16700Schasinglulu void tspd_synchronous_sp_exit(tsp_context_t *tsp_ctx, uint64_t ret) 101*91f16700Schasinglulu { 102*91f16700Schasinglulu assert(tsp_ctx != NULL); 103*91f16700Schasinglulu /* Save the Secure EL1 system register context */ 104*91f16700Schasinglulu assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx); 105*91f16700Schasinglulu cm_el1_sysregs_context_save(SECURE); 106*91f16700Schasinglulu 107*91f16700Schasinglulu assert(tsp_ctx->c_rt_ctx != 0); 108*91f16700Schasinglulu tspd_exit_sp(tsp_ctx->c_rt_ctx, ret); 109*91f16700Schasinglulu 110*91f16700Schasinglulu /* Should never reach here */ 111*91f16700Schasinglulu assert(0); 112*91f16700Schasinglulu } 113*91f16700Schasinglulu 114*91f16700Schasinglulu /******************************************************************************* 115*91f16700Schasinglulu * This function takes an SP context pointer and abort any preempted SMC 116*91f16700Schasinglulu * request. 117*91f16700Schasinglulu * Return 1 if there was a preempted SMC request, 0 otherwise. 118*91f16700Schasinglulu ******************************************************************************/ 119*91f16700Schasinglulu int tspd_abort_preempted_smc(tsp_context_t *tsp_ctx) 120*91f16700Schasinglulu { 121*91f16700Schasinglulu if (!get_yield_smc_active_flag(tsp_ctx->state)) 122*91f16700Schasinglulu return 0; 123*91f16700Schasinglulu 124*91f16700Schasinglulu /* Abort any preempted SMC request */ 125*91f16700Schasinglulu clr_yield_smc_active_flag(tsp_ctx->state); 126*91f16700Schasinglulu 127*91f16700Schasinglulu /* 128*91f16700Schasinglulu * Arrange for an entry into the test secure payload. It will 129*91f16700Schasinglulu * be returned via TSP_ABORT_DONE case in tspd_smc_handler. 130*91f16700Schasinglulu */ 131*91f16700Schasinglulu cm_set_elr_el3(SECURE, 132*91f16700Schasinglulu (uint64_t) &tsp_vectors->abort_yield_smc_entry); 133*91f16700Schasinglulu uint64_t rc = tspd_synchronous_sp_entry(tsp_ctx); 134*91f16700Schasinglulu 135*91f16700Schasinglulu if (rc != 0) 136*91f16700Schasinglulu panic(); 137*91f16700Schasinglulu 138*91f16700Schasinglulu return 1; 139*91f16700Schasinglulu } 140*91f16700Schasinglulu 141