xref: /aosp_15_r20/external/arm-trusted-firmware/services/spd/tlkd/tlkd_common.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park  *
4*54fd6939SJiyong Park  * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park  */
6*54fd6939SJiyong Park 
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park #include <string.h>
9*54fd6939SJiyong Park 
10*54fd6939SJiyong Park #include <arch_helpers.h>
11*54fd6939SJiyong Park #include <common/bl_common.h>
12*54fd6939SJiyong Park #include <lib/el3_runtime/context_mgmt.h>
13*54fd6939SJiyong Park 
14*54fd6939SJiyong Park #include "tlkd_private.h"
15*54fd6939SJiyong Park 
16*54fd6939SJiyong Park #define AT_MASK		3
17*54fd6939SJiyong Park 
18*54fd6939SJiyong Park /*******************************************************************************
19*54fd6939SJiyong Park  * This function helps the SP to translate NS/S virtual addresses.
20*54fd6939SJiyong Park  ******************************************************************************/
tlkd_va_translate(uintptr_t va,int type)21*54fd6939SJiyong Park uint64_t tlkd_va_translate(uintptr_t va, int type)
22*54fd6939SJiyong Park {
23*54fd6939SJiyong Park 	uint64_t pa;
24*54fd6939SJiyong Park 
25*54fd6939SJiyong Park 	if (type & TLK_TRANSLATE_NS_VADDR) {
26*54fd6939SJiyong Park 
27*54fd6939SJiyong Park 		/* save secure context */
28*54fd6939SJiyong Park 		cm_el1_sysregs_context_save(SECURE);
29*54fd6939SJiyong Park 
30*54fd6939SJiyong Park 		/* restore non-secure context */
31*54fd6939SJiyong Park 		cm_el1_sysregs_context_restore(NON_SECURE);
32*54fd6939SJiyong Park 
33*54fd6939SJiyong Park 		/* switch NS bit to start using 64-bit, non-secure mappings */
34*54fd6939SJiyong Park 		write_scr(cm_get_scr_el3(NON_SECURE));
35*54fd6939SJiyong Park 		isb();
36*54fd6939SJiyong Park 	}
37*54fd6939SJiyong Park 
38*54fd6939SJiyong Park 	int at = type & AT_MASK;
39*54fd6939SJiyong Park 	switch (at) {
40*54fd6939SJiyong Park 	case 0:
41*54fd6939SJiyong Park 		AT(ats12e1r, va);
42*54fd6939SJiyong Park 		break;
43*54fd6939SJiyong Park 	case 1:
44*54fd6939SJiyong Park 		AT(ats12e1w, va);
45*54fd6939SJiyong Park 		break;
46*54fd6939SJiyong Park 	case 2:
47*54fd6939SJiyong Park 		AT(ats12e0r, va);
48*54fd6939SJiyong Park 		break;
49*54fd6939SJiyong Park 	case 3:
50*54fd6939SJiyong Park 		AT(ats12e0w, va);
51*54fd6939SJiyong Park 		break;
52*54fd6939SJiyong Park 	default:
53*54fd6939SJiyong Park 		assert(0); /* Unreachable */
54*54fd6939SJiyong Park 		break;
55*54fd6939SJiyong Park 	}
56*54fd6939SJiyong Park 
57*54fd6939SJiyong Park 	/* get the (NS/S) physical address */
58*54fd6939SJiyong Park 	isb();
59*54fd6939SJiyong Park 	pa = read_par_el1();
60*54fd6939SJiyong Park 
61*54fd6939SJiyong Park 	/* Restore secure state */
62*54fd6939SJiyong Park 	if (type & TLK_TRANSLATE_NS_VADDR) {
63*54fd6939SJiyong Park 
64*54fd6939SJiyong Park 		/* restore secure context */
65*54fd6939SJiyong Park 		cm_el1_sysregs_context_restore(SECURE);
66*54fd6939SJiyong Park 
67*54fd6939SJiyong Park 		/* switch NS bit to start using 32-bit, secure mappings */
68*54fd6939SJiyong Park 		write_scr(cm_get_scr_el3(SECURE));
69*54fd6939SJiyong Park 		isb();
70*54fd6939SJiyong Park 	}
71*54fd6939SJiyong Park 
72*54fd6939SJiyong Park 	return pa;
73*54fd6939SJiyong Park }
74*54fd6939SJiyong Park 
75*54fd6939SJiyong Park /*******************************************************************************
76*54fd6939SJiyong Park  * Given a secure payload entrypoint, register width, cpu id & pointer to a
77*54fd6939SJiyong Park  * context data structure, this function will create a secure context ready for
78*54fd6939SJiyong Park  * programming an entry into the secure payload.
79*54fd6939SJiyong Park  ******************************************************************************/
tlkd_init_tlk_ep_state(struct entry_point_info * tlk_entry_point,uint32_t rw,uint64_t pc,tlk_context_t * tlk_ctx)80*54fd6939SJiyong Park void tlkd_init_tlk_ep_state(struct entry_point_info *tlk_entry_point,
81*54fd6939SJiyong Park 			    uint32_t rw,
82*54fd6939SJiyong Park 			    uint64_t pc,
83*54fd6939SJiyong Park 			    tlk_context_t *tlk_ctx)
84*54fd6939SJiyong Park {
85*54fd6939SJiyong Park 	uint32_t ep_attr, spsr;
86*54fd6939SJiyong Park 
87*54fd6939SJiyong Park 	/* Passing a NULL context is a critical programming error */
88*54fd6939SJiyong Park 	assert(tlk_ctx);
89*54fd6939SJiyong Park 	assert(tlk_entry_point);
90*54fd6939SJiyong Park 	assert(pc);
91*54fd6939SJiyong Park 
92*54fd6939SJiyong Park 	/* Associate this context with the cpu specified */
93*54fd6939SJiyong Park 	tlk_ctx->mpidr = read_mpidr_el1();
94*54fd6939SJiyong Park 	clr_yield_smc_active_flag(tlk_ctx->state);
95*54fd6939SJiyong Park 	cm_set_context(&tlk_ctx->cpu_ctx, SECURE);
96*54fd6939SJiyong Park 
97*54fd6939SJiyong Park 	if (rw == SP_AARCH64)
98*54fd6939SJiyong Park 		spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
99*54fd6939SJiyong Park 	else
100*54fd6939SJiyong Park 		spsr = SPSR_MODE32(MODE32_svc,
101*54fd6939SJiyong Park 				   SPSR_T_ARM,
102*54fd6939SJiyong Park 				   read_sctlr_el3() & SCTLR_EE_BIT,
103*54fd6939SJiyong Park 				   DISABLE_ALL_EXCEPTIONS);
104*54fd6939SJiyong Park 
105*54fd6939SJiyong Park 	/* initialise an entrypoint to set up the CPU context */
106*54fd6939SJiyong Park 	ep_attr = SECURE | EP_ST_ENABLE;
107*54fd6939SJiyong Park 	if (read_sctlr_el3() & SCTLR_EE_BIT)
108*54fd6939SJiyong Park 		ep_attr |= EP_EE_BIG;
109*54fd6939SJiyong Park 	SET_PARAM_HEAD(tlk_entry_point, PARAM_EP, VERSION_1, ep_attr);
110*54fd6939SJiyong Park 
111*54fd6939SJiyong Park 	tlk_entry_point->pc = pc;
112*54fd6939SJiyong Park 	tlk_entry_point->spsr = spsr;
113*54fd6939SJiyong Park }
114*54fd6939SJiyong Park 
115*54fd6939SJiyong Park /*******************************************************************************
116*54fd6939SJiyong Park  * This function takes a TLK context pointer and:
117*54fd6939SJiyong Park  * 1. Applies the S-EL1 system register context from tlk_ctx->cpu_ctx.
118*54fd6939SJiyong Park  * 2. Saves the current C runtime state (callee saved registers) on the stack
119*54fd6939SJiyong Park  *    frame and saves a reference to this state.
120*54fd6939SJiyong Park  * 3. Calls el3_exit() so that the EL3 system and general purpose registers
121*54fd6939SJiyong Park  *    from the tlk_ctx->cpu_ctx are used to enter the secure payload image.
122*54fd6939SJiyong Park  ******************************************************************************/
tlkd_synchronous_sp_entry(tlk_context_t * tlk_ctx)123*54fd6939SJiyong Park uint64_t tlkd_synchronous_sp_entry(tlk_context_t *tlk_ctx)
124*54fd6939SJiyong Park {
125*54fd6939SJiyong Park 	uint64_t rc;
126*54fd6939SJiyong Park 
127*54fd6939SJiyong Park 	/* Passing a NULL context is a critical programming error */
128*54fd6939SJiyong Park 	assert(tlk_ctx);
129*54fd6939SJiyong Park 
130*54fd6939SJiyong Park 	/* Apply the Secure EL1 system register context and switch to it */
131*54fd6939SJiyong Park 	assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
132*54fd6939SJiyong Park 	cm_el1_sysregs_context_restore(SECURE);
133*54fd6939SJiyong Park 	cm_set_next_eret_context(SECURE);
134*54fd6939SJiyong Park 
135*54fd6939SJiyong Park 	rc = tlkd_enter_sp(&tlk_ctx->c_rt_ctx);
136*54fd6939SJiyong Park #if ENABLE_ASSERTIONS
137*54fd6939SJiyong Park 	tlk_ctx->c_rt_ctx = 0;
138*54fd6939SJiyong Park #endif
139*54fd6939SJiyong Park 
140*54fd6939SJiyong Park 	return rc;
141*54fd6939SJiyong Park }
142*54fd6939SJiyong Park 
143*54fd6939SJiyong Park /*******************************************************************************
144*54fd6939SJiyong Park  * This function takes a TLK context pointer and:
145*54fd6939SJiyong Park  * 1. Saves the S-EL1 system register context to tlk_ctx->cpu_ctx.
146*54fd6939SJiyong Park  * 2. Restores the current C runtime state (callee saved registers) from the
147*54fd6939SJiyong Park  *    stack frame using reference to this state saved in tlkd_enter_sp().
148*54fd6939SJiyong Park  * 3. It does not need to save any general purpose or EL3 system register state
149*54fd6939SJiyong Park  *    as the generic smc entry routine should have saved those.
150*54fd6939SJiyong Park  ******************************************************************************/
tlkd_synchronous_sp_exit(tlk_context_t * tlk_ctx,uint64_t ret)151*54fd6939SJiyong Park void tlkd_synchronous_sp_exit(tlk_context_t *tlk_ctx, uint64_t ret)
152*54fd6939SJiyong Park {
153*54fd6939SJiyong Park 	/* Passing a NULL context is a critical programming error */
154*54fd6939SJiyong Park 	assert(tlk_ctx);
155*54fd6939SJiyong Park 
156*54fd6939SJiyong Park 	/* Save the Secure EL1 system register context */
157*54fd6939SJiyong Park 	assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
158*54fd6939SJiyong Park 	cm_el1_sysregs_context_save(SECURE);
159*54fd6939SJiyong Park 
160*54fd6939SJiyong Park 	assert(tlk_ctx->c_rt_ctx != 0);
161*54fd6939SJiyong Park 	tlkd_exit_sp(tlk_ctx->c_rt_ctx, ret);
162*54fd6939SJiyong Park 
163*54fd6939SJiyong Park 	/* Should never reach here */
164*54fd6939SJiyong Park 	assert(0);
165*54fd6939SJiyong Park }
166