xref: /optee_os/core/arch/arm/sm/sm.c (revision aea0999e2360b6f1c124169fbfeb544e356ebc85)
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 /*
17  * Return false/0 if secure monitor shall retrun to non-secure and
18  * true/1 if secure monitor shall enter OP-TEE core to proceed
19  * invocation.
20  */
21 bool sm_from_nsec(struct sm_ctx *ctx)
22 {
23 	uint32_t *nsec_r0 = (uint32_t *)(&ctx->nsec.r0);
24 
25 	/*
26 	 * Check that struct sm_ctx has the different parts properly
27 	 * aligned since the stack pointer will be updated to point at
28 	 * different parts of this struct.
29 	 */
30 	COMPILE_TIME_ASSERT(!(offsetof(struct sm_ctx, sec.r0) % 8));
31 	COMPILE_TIME_ASSERT(!(offsetof(struct sm_ctx, nsec.r0) % 8));
32 	COMPILE_TIME_ASSERT(!(sizeof(struct sm_ctx) % 8));
33 
34 	if (sm_platform_handler(ctx) == SM_HANDLER_SMC_HANDLED)
35 		return false;
36 
37 #ifdef CFG_PSCI_ARM32
38 	if (OPTEE_SMC_OWNER_NUM(*nsec_r0) == OPTEE_SMC_OWNER_STANDARD) {
39 		smc_std_handler((struct thread_smc_args *)nsec_r0, &ctx->nsec);
40 		return false;	/* Return to non secure state */
41 	}
42 #endif
43 
44 	sm_save_unbanked_regs(&ctx->nsec.ub_regs);
45 	sm_restore_unbanked_regs(&ctx->sec.ub_regs);
46 
47 	memcpy(&ctx->sec.r0, nsec_r0, sizeof(uint32_t) * 8);
48 	if (OPTEE_SMC_IS_FAST_CALL(ctx->sec.r0))
49 		ctx->sec.mon_lr = (uint32_t)&thread_vector_table.fast_smc_entry;
50 	else
51 		ctx->sec.mon_lr = (uint32_t)&thread_vector_table.std_smc_entry;
52 	return true;	/* return into secure state */
53 }
54