xref: /arm-trusted-firmware/services/spd/pncd/pncd_common.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2021-2022, ProvenRun S.A.S. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu #include <string.h>
9*91f16700Schasinglulu 
10*91f16700Schasinglulu #include <arch_helpers.h>
11*91f16700Schasinglulu #include <common/bl_common.h>
12*91f16700Schasinglulu #include <common/debug.h>
13*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h>
14*91f16700Schasinglulu #include <lib/utils.h>
15*91f16700Schasinglulu #include <plat/common/platform.h>
16*91f16700Schasinglulu 
17*91f16700Schasinglulu #include "pncd_private.h"
18*91f16700Schasinglulu 
19*91f16700Schasinglulu /*******************************************************************************
20*91f16700Schasinglulu  * Given a secure payload entrypoint info pointer, entry point PC & pointer to a
21*91f16700Schasinglulu  * context data structure, this function will initialize pnc context and entry
22*91f16700Schasinglulu  * point info for the secure payload
23*91f16700Schasinglulu  ******************************************************************************/
24*91f16700Schasinglulu void pncd_init_pnc_ep_state(struct entry_point_info *pnc_entry_point,
25*91f16700Schasinglulu 				uint64_t pc,
26*91f16700Schasinglulu 				pnc_context_t *pnc_ctx)
27*91f16700Schasinglulu {
28*91f16700Schasinglulu 	uint32_t ep_attr;
29*91f16700Schasinglulu 
30*91f16700Schasinglulu 	/* Passing a NULL context is a critical programming error */
31*91f16700Schasinglulu 	assert(pnc_ctx);
32*91f16700Schasinglulu 	assert(pnc_entry_point);
33*91f16700Schasinglulu 	assert(pc);
34*91f16700Schasinglulu 
35*91f16700Schasinglulu 	/* Associate this context with the current cpu */
36*91f16700Schasinglulu 	pnc_ctx->mpidr = read_mpidr();
37*91f16700Schasinglulu 
38*91f16700Schasinglulu 	cm_set_context(&pnc_ctx->cpu_ctx, SECURE);
39*91f16700Schasinglulu 
40*91f16700Schasinglulu 	/* initialise an entrypoint to set up the CPU context */
41*91f16700Schasinglulu 	ep_attr = SECURE | EP_ST_ENABLE;
42*91f16700Schasinglulu 	if (read_sctlr_el3() & SCTLR_EE_BIT) {
43*91f16700Schasinglulu 		ep_attr |= EP_EE_BIG;
44*91f16700Schasinglulu 	}
45*91f16700Schasinglulu 	SET_PARAM_HEAD(pnc_entry_point, PARAM_EP, VERSION_1, ep_attr);
46*91f16700Schasinglulu 
47*91f16700Schasinglulu 	pnc_entry_point->pc = pc;
48*91f16700Schasinglulu 	pnc_entry_point->spsr = SPSR_64(MODE_EL1,
49*91f16700Schasinglulu 					MODE_SP_ELX,
50*91f16700Schasinglulu 					DISABLE_ALL_EXCEPTIONS);
51*91f16700Schasinglulu 	memset(&pnc_entry_point->args, 0, sizeof(pnc_entry_point->args));
52*91f16700Schasinglulu }
53*91f16700Schasinglulu 
54*91f16700Schasinglulu /*******************************************************************************
55*91f16700Schasinglulu  * This function takes an SP context pointer and:
56*91f16700Schasinglulu  * 1. Applies the S-EL1 system register context from pnc_ctx->cpu_ctx.
57*91f16700Schasinglulu  * 2. Saves the current C runtime state (callee saved registers) on the stack
58*91f16700Schasinglulu  *    frame and saves a reference to this state.
59*91f16700Schasinglulu  * 3. Calls el3_exit() so that the EL3 system and general purpose registers
60*91f16700Schasinglulu  *    from the pnc_ctx->cpu_ctx are used to enter the secure payload image.
61*91f16700Schasinglulu  ******************************************************************************/
62*91f16700Schasinglulu uint64_t pncd_synchronous_sp_entry(pnc_context_t *pnc_ctx)
63*91f16700Schasinglulu {
64*91f16700Schasinglulu 	assert(pnc_ctx != NULL);
65*91f16700Schasinglulu 	assert(pnc_ctx->c_rt_ctx == 0U);
66*91f16700Schasinglulu 
67*91f16700Schasinglulu 	/* Apply the Secure EL1 system register context and switch to it */
68*91f16700Schasinglulu 	assert(cm_get_context(SECURE) == &pnc_ctx->cpu_ctx);
69*91f16700Schasinglulu 	cm_el1_sysregs_context_restore(SECURE);
70*91f16700Schasinglulu #if CTX_INCLUDE_FPREGS
71*91f16700Schasinglulu 	fpregs_context_restore(get_fpregs_ctx(cm_get_context(SECURE)));
72*91f16700Schasinglulu #endif
73*91f16700Schasinglulu 	cm_set_next_eret_context(SECURE);
74*91f16700Schasinglulu 
75*91f16700Schasinglulu 	return pncd_enter_sp(&pnc_ctx->c_rt_ctx);
76*91f16700Schasinglulu }
77*91f16700Schasinglulu 
78*91f16700Schasinglulu 
79*91f16700Schasinglulu /*******************************************************************************
80*91f16700Schasinglulu  * This function takes an SP context pointer and:
81*91f16700Schasinglulu  * 1. Saves the S-EL1 system register context tp pnc_ctx->cpu_ctx.
82*91f16700Schasinglulu  * 2. Restores the current C runtime state (callee saved registers) from the
83*91f16700Schasinglulu  *    stack frame using the reference to this state saved in pncd_enter_sp().
84*91f16700Schasinglulu  * 3. It does not need to save any general purpose or EL3 system register state
85*91f16700Schasinglulu  *    as the generic smc entry routine should have saved those.
86*91f16700Schasinglulu  ******************************************************************************/
87*91f16700Schasinglulu void pncd_synchronous_sp_exit(pnc_context_t *pnc_ctx, uint64_t ret)
88*91f16700Schasinglulu {
89*91f16700Schasinglulu 	assert(pnc_ctx != NULL);
90*91f16700Schasinglulu 	/* Save the Secure EL1 system register context */
91*91f16700Schasinglulu 	assert(cm_get_context(SECURE) == &pnc_ctx->cpu_ctx);
92*91f16700Schasinglulu 	cm_el1_sysregs_context_save(SECURE);
93*91f16700Schasinglulu #if CTX_INCLUDE_FPREGS
94*91f16700Schasinglulu 	fpregs_context_save(get_fpregs_ctx(cm_get_context(SECURE)));
95*91f16700Schasinglulu #endif
96*91f16700Schasinglulu 
97*91f16700Schasinglulu 	assert(pnc_ctx->c_rt_ctx != 0);
98*91f16700Schasinglulu 	pncd_exit_sp(pnc_ctx->c_rt_ctx, ret);
99*91f16700Schasinglulu 
100*91f16700Schasinglulu 	/* Should never reach here */
101*91f16700Schasinglulu 	panic();
102*91f16700Schasinglulu }
103