1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2016, Linaro Limited 4 * Copyright (c) 2014, STMicroelectronics International N.V. 5 */ 6 #include <arm.h> 7 #include <compiler.h> 8 #include <kernel/misc.h> 9 #include <platform_config.h> 10 #include <sm/optee_smc.h> 11 #include <sm/sm.h> 12 #include <sm/std_smc.h> 13 #include <string.h> 14 #include "sm_private.h" 15 16 bool sm_from_nsec(struct sm_ctx *ctx) 17 { 18 uint32_t *nsec_r0 = (uint32_t *)(&ctx->nsec.r0); 19 20 /* 21 * Check that struct sm_ctx has the different parts properly 22 * aligned since the stack pointer will be updated to point at 23 * different parts of this struct. 24 */ 25 COMPILE_TIME_ASSERT(!(offsetof(struct sm_ctx, sec.r0) % 8)); 26 COMPILE_TIME_ASSERT(!(offsetof(struct sm_ctx, nsec.r0) % 8)); 27 COMPILE_TIME_ASSERT(!(sizeof(struct sm_ctx) % 8)); 28 29 if (!sm_platform_handler(ctx)) 30 return false; 31 32 #ifdef CFG_PSCI_ARM32 33 if (OPTEE_SMC_OWNER_NUM(*nsec_r0) == OPTEE_SMC_OWNER_STANDARD) { 34 smc_std_handler((struct thread_smc_args *)nsec_r0, &ctx->nsec); 35 return false; /* Return to non secure state */ 36 } 37 #endif 38 39 sm_save_unbanked_regs(&ctx->nsec.ub_regs); 40 sm_restore_unbanked_regs(&ctx->sec.ub_regs); 41 42 memcpy(&ctx->sec.r0, nsec_r0, sizeof(uint32_t) * 8); 43 if (OPTEE_SMC_IS_FAST_CALL(ctx->sec.r0)) 44 ctx->sec.mon_lr = (uint32_t)&thread_vector_table.fast_smc_entry; 45 else 46 ctx->sec.mon_lr = (uint32_t)&thread_vector_table.std_smc_entry; 47 return true; /* return into secure state */ 48 } 49