1 /* 2 * Copyright (c) 2013-2014, 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 <stdio.h> 32 #include <errno.h> 33 #include <string.h> 34 #include <assert.h> 35 #include <arch_helpers.h> 36 #include <platform.h> 37 #include <bl_common.h> 38 #include <runtime_svc.h> 39 #include <context_mgmt.h> 40 #include <tspd_private.h> 41 42 /******************************************************************************* 43 * Given a secure payload entrypoint, register width, cpu id & pointer to a 44 * context data structure, this function will create a secure context ready for 45 * programming an entry into the secure payload. 46 ******************************************************************************/ 47 int32_t tspd_init_secure_context(uint64_t entrypoint, 48 uint32_t rw, 49 uint64_t mpidr, 50 tsp_context *tsp_ctx) 51 { 52 uint32_t scr = read_scr(), sctlr = read_sctlr(); 53 el1_sys_regs *el1_state; 54 uint32_t spsr; 55 56 /* Passing a NULL context is a critical programming error */ 57 assert(tsp_ctx); 58 59 /* 60 * We support AArch64 TSP for now. 61 * TODO: Add support for AArch32 TSP 62 */ 63 assert(rw == TSP_AARCH64); 64 65 /* 66 * This might look redundant if the context was statically 67 * allocated but this function cannot make that assumption. 68 */ 69 memset(tsp_ctx, 0, sizeof(*tsp_ctx)); 70 71 /* Set the right security state and register width for the SP */ 72 scr &= ~SCR_NS_BIT; 73 scr &= ~SCR_RW_BIT; 74 if (rw == TSP_AARCH64) 75 scr |= SCR_RW_BIT; 76 77 /* Get a pointer to the S-EL1 context memory */ 78 el1_state = get_sysregs_ctx(&tsp_ctx->cpu_ctx); 79 80 /* 81 * Program the sctlr to allow execution in S-EL1 with caches 82 * and mmu off 83 */ 84 sctlr &= SCTLR_EE_BIT; 85 sctlr |= SCTLR_EL1_RES1; 86 write_ctx_reg(el1_state, CTX_SCTLR_EL1, sctlr); 87 88 /* Set this context as ready to be initialised i.e OFF */ 89 tsp_ctx->state = TSP_STATE_OFF; 90 91 /* Associate this context with the cpu specified */ 92 tsp_ctx->mpidr = mpidr; 93 94 cm_set_context(mpidr, &tsp_ctx->cpu_ctx, SECURE); 95 spsr = make_spsr(MODE_EL1, MODE_SP_ELX, rw); 96 cm_set_el3_eret_context(SECURE, entrypoint, spsr, scr); 97 98 cm_init_exception_stack(mpidr, SECURE); 99 100 return 0; 101 } 102 103 /******************************************************************************* 104 * This function takes an SP context pointer and: 105 * 1. Applies the S-EL1 system register context from tsp_ctx->cpu_ctx. 106 * 2. Saves the current C runtime state (callee saved registers) on the stack 107 * frame and saves a reference to this state. 108 * 3. Calls el3_exit() so that the EL3 system and general purpose registers 109 * from the tsp_ctx->cpu_ctx are used to enter the secure payload image. 110 ******************************************************************************/ 111 uint64_t tspd_synchronous_sp_entry(tsp_context *tsp_ctx) 112 { 113 uint64_t rc; 114 115 assert(tsp_ctx->c_rt_ctx == 0); 116 117 /* Apply the Secure EL1 system register context and switch to it */ 118 assert(cm_get_context(read_mpidr(), SECURE) == &tsp_ctx->cpu_ctx); 119 cm_el1_sysregs_context_restore(SECURE); 120 cm_set_next_eret_context(SECURE); 121 122 rc = tspd_enter_sp(&tsp_ctx->c_rt_ctx); 123 #if DEBUG 124 tsp_ctx->c_rt_ctx = 0; 125 #endif 126 127 return rc; 128 } 129 130 131 /******************************************************************************* 132 * This function takes an SP context pointer and: 133 * 1. Saves the S-EL1 system register context tp tsp_ctx->cpu_ctx. 134 * 2. Restores the current C runtime state (callee saved registers) from the 135 * stack frame using the reference to this state saved in tspd_enter_sp(). 136 * 3. It does not need to save any general purpose or EL3 system register state 137 * as the generic smc entry routine should have saved those. 138 ******************************************************************************/ 139 void tspd_synchronous_sp_exit(tsp_context *tsp_ctx, uint64_t ret) 140 { 141 /* Save the Secure EL1 system register context */ 142 assert(cm_get_context(read_mpidr(), SECURE) == &tsp_ctx->cpu_ctx); 143 cm_el1_sysregs_context_save(SECURE); 144 145 assert(tsp_ctx->c_rt_ctx != 0); 146 tspd_exit_sp(tsp_ctx->c_rt_ctx, ret); 147 148 /* Should never reach here */ 149 assert(0); 150 } 151