xref: /rk3399_ARM-atf/services/spd/tlkd/tlkd_common.c (revision 220383153c582b8b1d26b21ab2f240143a0558f5)
1 /*
2  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <arch_helpers.h>
32 #include <assert.h>
33 #include <bl_common.h>
34 #include <context_mgmt.h>
35 #include <string.h>
36 #include "tlkd_private.h"
37 
38 /*******************************************************************************
39  * Given a secure payload entrypoint, register width, cpu id & pointer to a
40  * context data structure, this function will create a secure context ready for
41  * programming an entry into the secure payload.
42  ******************************************************************************/
43 void tlkd_init_tlk_ep_state(struct entry_point_info *tlk_entry_point,
44 			    uint32_t rw,
45 			    uint64_t pc,
46 			    tlk_context_t *tlk_ctx)
47 {
48 	uint32_t ep_attr, spsr;
49 
50 	/* Passing a NULL context is a critical programming error */
51 	assert(tlk_ctx);
52 	assert(tlk_entry_point);
53 	assert(pc);
54 
55 	/* Associate this context with the cpu specified */
56 	tlk_ctx->mpidr = read_mpidr_el1();
57 	clr_std_smc_active_flag(tlk_ctx->state);
58 	cm_set_context(&tlk_ctx->cpu_ctx, SECURE);
59 
60 	if (rw == SP_AARCH64)
61 		spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
62 	else
63 		spsr = SPSR_MODE32(MODE32_svc,
64 				   SPSR_T_ARM,
65 				   read_sctlr_el3() & SCTLR_EE_BIT,
66 				   DISABLE_ALL_EXCEPTIONS);
67 
68 	/* initialise an entrypoint to set up the CPU context */
69 	ep_attr = SECURE | EP_ST_ENABLE;
70 	if (read_sctlr_el3() & SCTLR_EE_BIT)
71 		ep_attr |= EP_EE_BIG;
72 	SET_PARAM_HEAD(tlk_entry_point, PARAM_EP, VERSION_1, ep_attr);
73 
74 	tlk_entry_point->pc = pc;
75 	tlk_entry_point->spsr = spsr;
76 }
77 
78 /*******************************************************************************
79  * This function takes a TLK context pointer and:
80  * 1. Applies the S-EL1 system register context from tlk_ctx->cpu_ctx.
81  * 2. Saves the current C runtime state (callee saved registers) on the stack
82  *    frame and saves a reference to this state.
83  * 3. Calls el3_exit() so that the EL3 system and general purpose registers
84  *    from the tlk_ctx->cpu_ctx are used to enter the secure payload image.
85  ******************************************************************************/
86 uint64_t tlkd_synchronous_sp_entry(tlk_context_t *tlk_ctx)
87 {
88 	uint64_t rc;
89 
90 	/* Passing a NULL context is a critical programming error */
91 	assert(tlk_ctx);
92 	assert(tlk_ctx->c_rt_ctx == 0);
93 
94 	/* Apply the Secure EL1 system register context and switch to it */
95 	assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
96 	cm_el1_sysregs_context_restore(SECURE);
97 	cm_set_next_eret_context(SECURE);
98 
99 	rc = tlkd_enter_sp(&tlk_ctx->c_rt_ctx);
100 #if DEBUG
101 	tlk_ctx->c_rt_ctx = 0;
102 #endif
103 
104 	return rc;
105 }
106 
107 /*******************************************************************************
108  * This function takes a TLK context pointer and:
109  * 1. Saves the S-EL1 system register context to tlk_ctx->cpu_ctx.
110  * 2. Restores the current C runtime state (callee saved registers) from the
111  *    stack frame using reference to this state saved in tlkd_enter_sp().
112  * 3. It does not need to save any general purpose or EL3 system register state
113  *    as the generic smc entry routine should have saved those.
114  ******************************************************************************/
115 void tlkd_synchronous_sp_exit(tlk_context_t *tlk_ctx, uint64_t ret)
116 {
117 	/* Passing a NULL context is a critical programming error */
118 	assert(tlk_ctx);
119 
120 	/* Save the Secure EL1 system register context */
121 	assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
122 	cm_el1_sysregs_context_save(SECURE);
123 
124 	assert(tlk_ctx->c_rt_ctx != 0);
125 	tlkd_exit_sp(tlk_ctx->c_rt_ctx, ret);
126 
127 	/* Should never reach here */
128 	assert(0);
129 }
130