1 /* 2 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * Redistributions of source code must retain the above copyright notice, this 8 * list of conditions and the following disclaimer. 9 * 10 * Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * Neither the name of ARM nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without specific 16 * prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <arch_helpers.h> 32 #include <assert.h> 33 #include <bl_common.h> 34 #include <context_mgmt.h> 35 #include <string.h> 36 #include "tlkd_private.h" 37 38 #define AT_MASK 3 39 40 /******************************************************************************* 41 * This function helps the SP to translate NS/S virtual addresses. 42 ******************************************************************************/ 43 uint64_t tlkd_va_translate(uintptr_t va, int type) 44 { 45 uint64_t pa; 46 47 if (type & TLK_TRANSLATE_NS_VADDR) { 48 49 /* save secure context */ 50 cm_el1_sysregs_context_save(SECURE); 51 52 /* restore non-secure context */ 53 cm_el1_sysregs_context_restore(NON_SECURE); 54 55 /* switch NS bit to start using 64-bit, non-secure mappings */ 56 write_scr(cm_get_scr_el3(NON_SECURE)); 57 isb(); 58 } 59 60 int at = type & AT_MASK; 61 switch (at) { 62 case 0: 63 ats12e1r(va); 64 break; 65 case 1: 66 ats12e1w(va); 67 break; 68 case 2: 69 ats12e0r(va); 70 break; 71 case 3: 72 ats12e0w(va); 73 break; 74 default: 75 assert(0); 76 } 77 78 /* get the (NS/S) physical address */ 79 isb(); 80 pa = read_par_el1(); 81 82 /* Restore secure state */ 83 if (type & TLK_TRANSLATE_NS_VADDR) { 84 85 /* restore secure context */ 86 cm_el1_sysregs_context_restore(SECURE); 87 88 /* switch NS bit to start using 32-bit, secure mappings */ 89 write_scr(cm_get_scr_el3(SECURE)); 90 isb(); 91 } 92 93 return pa; 94 } 95 96 /******************************************************************************* 97 * Given a secure payload entrypoint, register width, cpu id & pointer to a 98 * context data structure, this function will create a secure context ready for 99 * programming an entry into the secure payload. 100 ******************************************************************************/ 101 void tlkd_init_tlk_ep_state(struct entry_point_info *tlk_entry_point, 102 uint32_t rw, 103 uint64_t pc, 104 tlk_context_t *tlk_ctx) 105 { 106 uint32_t ep_attr, spsr; 107 108 /* Passing a NULL context is a critical programming error */ 109 assert(tlk_ctx); 110 assert(tlk_entry_point); 111 assert(pc); 112 113 /* Associate this context with the cpu specified */ 114 tlk_ctx->mpidr = read_mpidr_el1(); 115 clr_std_smc_active_flag(tlk_ctx->state); 116 cm_set_context(&tlk_ctx->cpu_ctx, SECURE); 117 118 if (rw == SP_AARCH64) 119 spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); 120 else 121 spsr = SPSR_MODE32(MODE32_svc, 122 SPSR_T_ARM, 123 read_sctlr_el3() & SCTLR_EE_BIT, 124 DISABLE_ALL_EXCEPTIONS); 125 126 /* initialise an entrypoint to set up the CPU context */ 127 ep_attr = SECURE | EP_ST_ENABLE; 128 if (read_sctlr_el3() & SCTLR_EE_BIT) 129 ep_attr |= EP_EE_BIG; 130 SET_PARAM_HEAD(tlk_entry_point, PARAM_EP, VERSION_1, ep_attr); 131 132 tlk_entry_point->pc = pc; 133 tlk_entry_point->spsr = spsr; 134 } 135 136 /******************************************************************************* 137 * This function takes a TLK context pointer and: 138 * 1. Applies the S-EL1 system register context from tlk_ctx->cpu_ctx. 139 * 2. Saves the current C runtime state (callee saved registers) on the stack 140 * frame and saves a reference to this state. 141 * 3. Calls el3_exit() so that the EL3 system and general purpose registers 142 * from the tlk_ctx->cpu_ctx are used to enter the secure payload image. 143 ******************************************************************************/ 144 uint64_t tlkd_synchronous_sp_entry(tlk_context_t *tlk_ctx) 145 { 146 uint64_t rc; 147 148 /* Passing a NULL context is a critical programming error */ 149 assert(tlk_ctx); 150 assert(tlk_ctx->c_rt_ctx == 0); 151 152 /* Apply the Secure EL1 system register context and switch to it */ 153 assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx); 154 cm_el1_sysregs_context_restore(SECURE); 155 cm_set_next_eret_context(SECURE); 156 157 rc = tlkd_enter_sp(&tlk_ctx->c_rt_ctx); 158 #if DEBUG 159 tlk_ctx->c_rt_ctx = 0; 160 #endif 161 162 return rc; 163 } 164 165 /******************************************************************************* 166 * This function takes a TLK context pointer and: 167 * 1. Saves the S-EL1 system register context to tlk_ctx->cpu_ctx. 168 * 2. Restores the current C runtime state (callee saved registers) from the 169 * stack frame using reference to this state saved in tlkd_enter_sp(). 170 * 3. It does not need to save any general purpose or EL3 system register state 171 * as the generic smc entry routine should have saved those. 172 ******************************************************************************/ 173 void tlkd_synchronous_sp_exit(tlk_context_t *tlk_ctx, uint64_t ret) 174 { 175 /* Passing a NULL context is a critical programming error */ 176 assert(tlk_ctx); 177 178 /* Save the Secure EL1 system register context */ 179 assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx); 180 cm_el1_sysregs_context_save(SECURE); 181 182 assert(tlk_ctx->c_rt_ctx != 0); 183 tlkd_exit_sp(tlk_ctx->c_rt_ctx, ret); 184 185 /* Should never reach here */ 186 assert(0); 187 } 188