1 /* 2 * Copyright (c) 2014, STMicroelectronics International N.V. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #include <compiler.h> 28 #include <platform_config.h> 29 30 #include <sm/sm.h> 31 #include <sm/optee_smc.h> 32 #include <sm/teesmc_opteed_macros.h> 33 #include <sm/teesmc_opteed.h> 34 35 #include <arm.h> 36 37 #include <kernel/misc.h> 38 39 #include "sm_private.h" 40 41 static struct sm_nsec_ctx sm_nsec_ctx[CFG_TEE_CORE_NB_CORE]; 42 static struct sm_sec_ctx sm_sec_ctx[CFG_TEE_CORE_NB_CORE]; 43 44 /* 45 * Has to match layout of thread_vector_table. Some of the entries are 46 * never used. 47 * 48 * We're using this layout to be able to used the same vector when this 49 * secure monitor is used and when the secure monitor in ARM Trusted 50 * Firmware is used. 51 */ 52 static struct { 53 uint32_t std_smc_entry; 54 uint32_t fast_smc_entry; 55 uint32_t cpu_on_entry; 56 uint32_t cpu_off_entry; 57 uint32_t cpu_resume_entry; 58 uint32_t cpu_suspend_entry; 59 uint32_t fiq_entry; 60 uint32_t system_off_entry; 61 uint32_t system_reset_entry; 62 } *sm_entry_vector; 63 64 struct sm_nsec_ctx *sm_get_nsec_ctx(void) 65 { 66 return &sm_nsec_ctx[get_core_pos()]; 67 } 68 69 struct sm_sec_ctx *sm_get_sec_ctx(void) 70 { 71 return &sm_sec_ctx[get_core_pos()]; 72 } 73 74 void sm_set_sec_smc_entry(const struct sm_reg_r0_to_r3 *regs) 75 { 76 struct sm_sec_ctx *sec_ctx = sm_get_sec_ctx(); 77 78 if (OPTEE_SMC_IS_FAST_CALL(regs->r0)) 79 sec_ctx->mon_lr = (uint32_t)&sm_entry_vector->fast_smc_entry; 80 else 81 sec_ctx->mon_lr = (uint32_t)&sm_entry_vector->std_smc_entry; 82 } 83 84 void sm_set_nsec_ret_vals(struct sm_reg_r0_to_r3 *regs, uint32_t r4) 85 { 86 if (regs->r0 == TEESMC_OPTEED_RETURN_FIQ_DONE) { 87 /* On FIQ exit we're restoring r0-r3 from nsec context */ 88 struct sm_nsec_ctx *nsec_ctx = sm_get_nsec_ctx(); 89 90 regs->r0 = nsec_ctx->r0; 91 regs->r1 = nsec_ctx->r1; 92 regs->r2 = nsec_ctx->r2; 93 regs->r3 = nsec_ctx->r3; 94 } else { 95 /* On all other exits we're shifting r1-r4 into r0-r3 */ 96 regs->r0 = regs->r1; 97 regs->r1 = regs->r2; 98 regs->r2 = regs->r3; 99 regs->r3 = r4; 100 } 101 } 102 103 void sm_set_sec_fiq_entry(void) 104 { 105 struct sm_sec_ctx *sec_ctx = sm_get_sec_ctx(); 106 107 sec_ctx->mon_lr = (uint32_t)&sm_entry_vector->fiq_entry; 108 } 109 110 void sm_set_entry_vector(void *entry_vector) 111 { 112 sm_entry_vector = entry_vector; 113 } 114