122038315SVarun Wadekar /* 2a08a2014SDaniel Boulby * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. 322038315SVarun Wadekar * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 522038315SVarun Wadekar */ 622038315SVarun Wadekar 722038315SVarun Wadekar #include <assert.h> 822038315SVarun Wadekar #include <string.h> 9*09d40e0eSAntonio Nino Diaz 10*09d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 11*09d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 12*09d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h> 13*09d40e0eSAntonio Nino Diaz 1422038315SVarun Wadekar #include "tlkd_private.h" 1522038315SVarun Wadekar 166e159e7aSVarun Wadekar #define AT_MASK 3 176e159e7aSVarun Wadekar 186e159e7aSVarun Wadekar /******************************************************************************* 196e159e7aSVarun Wadekar * This function helps the SP to translate NS/S virtual addresses. 206e159e7aSVarun Wadekar ******************************************************************************/ 216e159e7aSVarun Wadekar uint64_t tlkd_va_translate(uintptr_t va, int type) 226e159e7aSVarun Wadekar { 236e159e7aSVarun Wadekar uint64_t pa; 246e159e7aSVarun Wadekar 256e159e7aSVarun Wadekar if (type & TLK_TRANSLATE_NS_VADDR) { 266e159e7aSVarun Wadekar 276e159e7aSVarun Wadekar /* save secure context */ 286e159e7aSVarun Wadekar cm_el1_sysregs_context_save(SECURE); 296e159e7aSVarun Wadekar 306e159e7aSVarun Wadekar /* restore non-secure context */ 316e159e7aSVarun Wadekar cm_el1_sysregs_context_restore(NON_SECURE); 326e159e7aSVarun Wadekar 336e159e7aSVarun Wadekar /* switch NS bit to start using 64-bit, non-secure mappings */ 346e159e7aSVarun Wadekar write_scr(cm_get_scr_el3(NON_SECURE)); 356e159e7aSVarun Wadekar isb(); 366e159e7aSVarun Wadekar } 376e159e7aSVarun Wadekar 386e159e7aSVarun Wadekar int at = type & AT_MASK; 396e159e7aSVarun Wadekar switch (at) { 406e159e7aSVarun Wadekar case 0: 416e159e7aSVarun Wadekar ats12e1r(va); 426e159e7aSVarun Wadekar break; 436e159e7aSVarun Wadekar case 1: 446e159e7aSVarun Wadekar ats12e1w(va); 456e159e7aSVarun Wadekar break; 466e159e7aSVarun Wadekar case 2: 476e159e7aSVarun Wadekar ats12e0r(va); 486e159e7aSVarun Wadekar break; 496e159e7aSVarun Wadekar case 3: 506e159e7aSVarun Wadekar ats12e0w(va); 516e159e7aSVarun Wadekar break; 526e159e7aSVarun Wadekar default: 53a08a2014SDaniel Boulby assert(0); /* Unreachable */ 54185a23ffSJonathan Wright break; 556e159e7aSVarun Wadekar } 566e159e7aSVarun Wadekar 576e159e7aSVarun Wadekar /* get the (NS/S) physical address */ 586e159e7aSVarun Wadekar isb(); 596e159e7aSVarun Wadekar pa = read_par_el1(); 606e159e7aSVarun Wadekar 616e159e7aSVarun Wadekar /* Restore secure state */ 626e159e7aSVarun Wadekar if (type & TLK_TRANSLATE_NS_VADDR) { 636e159e7aSVarun Wadekar 646e159e7aSVarun Wadekar /* restore secure context */ 656e159e7aSVarun Wadekar cm_el1_sysregs_context_restore(SECURE); 666e159e7aSVarun Wadekar 676e159e7aSVarun Wadekar /* switch NS bit to start using 32-bit, secure mappings */ 686e159e7aSVarun Wadekar write_scr(cm_get_scr_el3(SECURE)); 696e159e7aSVarun Wadekar isb(); 706e159e7aSVarun Wadekar } 716e159e7aSVarun Wadekar 726e159e7aSVarun Wadekar return pa; 736e159e7aSVarun Wadekar } 746e159e7aSVarun Wadekar 7522038315SVarun Wadekar /******************************************************************************* 7622038315SVarun Wadekar * Given a secure payload entrypoint, register width, cpu id & pointer to a 7722038315SVarun Wadekar * context data structure, this function will create a secure context ready for 7822038315SVarun Wadekar * programming an entry into the secure payload. 7922038315SVarun Wadekar ******************************************************************************/ 8022038315SVarun Wadekar void tlkd_init_tlk_ep_state(struct entry_point_info *tlk_entry_point, 8122038315SVarun Wadekar uint32_t rw, 8222038315SVarun Wadekar uint64_t pc, 8322038315SVarun Wadekar tlk_context_t *tlk_ctx) 8422038315SVarun Wadekar { 8522038315SVarun Wadekar uint32_t ep_attr, spsr; 8622038315SVarun Wadekar 8722038315SVarun Wadekar /* Passing a NULL context is a critical programming error */ 8822038315SVarun Wadekar assert(tlk_ctx); 8922038315SVarun Wadekar assert(tlk_entry_point); 9022038315SVarun Wadekar assert(pc); 9122038315SVarun Wadekar 9222038315SVarun Wadekar /* Associate this context with the cpu specified */ 9322038315SVarun Wadekar tlk_ctx->mpidr = read_mpidr_el1(); 94bbbbcdaeSDavid Cunado clr_yield_smc_active_flag(tlk_ctx->state); 9522038315SVarun Wadekar cm_set_context(&tlk_ctx->cpu_ctx, SECURE); 9622038315SVarun Wadekar 9722038315SVarun Wadekar if (rw == SP_AARCH64) 9822038315SVarun Wadekar spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); 9922038315SVarun Wadekar else 10022038315SVarun Wadekar spsr = SPSR_MODE32(MODE32_svc, 10122038315SVarun Wadekar SPSR_T_ARM, 10222038315SVarun Wadekar read_sctlr_el3() & SCTLR_EE_BIT, 10322038315SVarun Wadekar DISABLE_ALL_EXCEPTIONS); 10422038315SVarun Wadekar 10522038315SVarun Wadekar /* initialise an entrypoint to set up the CPU context */ 10622038315SVarun Wadekar ep_attr = SECURE | EP_ST_ENABLE; 10722038315SVarun Wadekar if (read_sctlr_el3() & SCTLR_EE_BIT) 10822038315SVarun Wadekar ep_attr |= EP_EE_BIG; 10922038315SVarun Wadekar SET_PARAM_HEAD(tlk_entry_point, PARAM_EP, VERSION_1, ep_attr); 11022038315SVarun Wadekar 11122038315SVarun Wadekar tlk_entry_point->pc = pc; 11222038315SVarun Wadekar tlk_entry_point->spsr = spsr; 11322038315SVarun Wadekar } 11422038315SVarun Wadekar 11522038315SVarun Wadekar /******************************************************************************* 11622038315SVarun Wadekar * This function takes a TLK context pointer and: 11722038315SVarun Wadekar * 1. Applies the S-EL1 system register context from tlk_ctx->cpu_ctx. 11822038315SVarun Wadekar * 2. Saves the current C runtime state (callee saved registers) on the stack 11922038315SVarun Wadekar * frame and saves a reference to this state. 12022038315SVarun Wadekar * 3. Calls el3_exit() so that the EL3 system and general purpose registers 12122038315SVarun Wadekar * from the tlk_ctx->cpu_ctx are used to enter the secure payload image. 12222038315SVarun Wadekar ******************************************************************************/ 12322038315SVarun Wadekar uint64_t tlkd_synchronous_sp_entry(tlk_context_t *tlk_ctx) 12422038315SVarun Wadekar { 12522038315SVarun Wadekar uint64_t rc; 12622038315SVarun Wadekar 12722038315SVarun Wadekar /* Passing a NULL context is a critical programming error */ 12822038315SVarun Wadekar assert(tlk_ctx); 12922038315SVarun Wadekar assert(tlk_ctx->c_rt_ctx == 0); 13022038315SVarun Wadekar 13122038315SVarun Wadekar /* Apply the Secure EL1 system register context and switch to it */ 13222038315SVarun Wadekar assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx); 13322038315SVarun Wadekar cm_el1_sysregs_context_restore(SECURE); 13422038315SVarun Wadekar cm_set_next_eret_context(SECURE); 13522038315SVarun Wadekar 13622038315SVarun Wadekar rc = tlkd_enter_sp(&tlk_ctx->c_rt_ctx); 13792cad5faSAntonio Nino Diaz #if ENABLE_ASSERTIONS 13822038315SVarun Wadekar tlk_ctx->c_rt_ctx = 0; 13922038315SVarun Wadekar #endif 14022038315SVarun Wadekar 14122038315SVarun Wadekar return rc; 14222038315SVarun Wadekar } 14322038315SVarun Wadekar 14422038315SVarun Wadekar /******************************************************************************* 14522038315SVarun Wadekar * This function takes a TLK context pointer and: 14622038315SVarun Wadekar * 1. Saves the S-EL1 system register context to tlk_ctx->cpu_ctx. 14722038315SVarun Wadekar * 2. Restores the current C runtime state (callee saved registers) from the 14822038315SVarun Wadekar * stack frame using reference to this state saved in tlkd_enter_sp(). 14922038315SVarun Wadekar * 3. It does not need to save any general purpose or EL3 system register state 15022038315SVarun Wadekar * as the generic smc entry routine should have saved those. 15122038315SVarun Wadekar ******************************************************************************/ 15222038315SVarun Wadekar void tlkd_synchronous_sp_exit(tlk_context_t *tlk_ctx, uint64_t ret) 15322038315SVarun Wadekar { 15422038315SVarun Wadekar /* Passing a NULL context is a critical programming error */ 15522038315SVarun Wadekar assert(tlk_ctx); 15622038315SVarun Wadekar 15722038315SVarun Wadekar /* Save the Secure EL1 system register context */ 15822038315SVarun Wadekar assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx); 15922038315SVarun Wadekar cm_el1_sysregs_context_save(SECURE); 16022038315SVarun Wadekar 16122038315SVarun Wadekar assert(tlk_ctx->c_rt_ctx != 0); 16222038315SVarun Wadekar tlkd_exit_sp(tlk_ctx->c_rt_ctx, ret); 16322038315SVarun Wadekar 16422038315SVarun Wadekar /* Should never reach here */ 16522038315SVarun Wadekar assert(0); 16622038315SVarun Wadekar } 167