xref: /optee_os/core/arch/arm/sm/sm.c (revision 5a913ee74d3c71af2a2860ce8a4e7aeab2916f9b)
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 uint32_t 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 #ifdef CFG_SM_PLATFORM_HANDLER
30 	if (sm_platform_handler(ctx) == SM_HANDLER_SMC_HANDLED)
31 		return SM_EXIT_TO_NON_SECURE;
32 #endif
33 
34 #ifdef CFG_PSCI_ARM32
35 	if (OPTEE_SMC_OWNER_NUM(*nsec_r0) == OPTEE_SMC_OWNER_STANDARD) {
36 		smc_std_handler((struct thread_smc_args *)nsec_r0, &ctx->nsec);
37 		return SM_EXIT_TO_NON_SECURE;
38 	}
39 #endif
40 
41 	sm_save_unbanked_regs(&ctx->nsec.ub_regs);
42 	sm_restore_unbanked_regs(&ctx->sec.ub_regs);
43 
44 	memcpy(&ctx->sec.r0, nsec_r0, sizeof(uint32_t) * 8);
45 	if (OPTEE_SMC_IS_FAST_CALL(ctx->sec.r0))
46 		ctx->sec.mon_lr = (uint32_t)&thread_vector_table.fast_smc_entry;
47 	else
48 		ctx->sec.mon_lr = (uint32_t)&thread_vector_table.std_smc_entry;
49 
50 	return SM_EXIT_TO_SECURE;
51 }
52