1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park * Copyright (c) 2013-2017, 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 <bl32/tsp/tsp.h>
12*54fd6939SJiyong Park #include <common/bl_common.h>
13*54fd6939SJiyong Park #include <common/debug.h>
14*54fd6939SJiyong Park #include <lib/el3_runtime/context_mgmt.h>
15*54fd6939SJiyong Park #include <lib/utils.h>
16*54fd6939SJiyong Park
17*54fd6939SJiyong Park #include "tspd_private.h"
18*54fd6939SJiyong Park
19*54fd6939SJiyong Park /*******************************************************************************
20*54fd6939SJiyong Park * Given a secure payload entrypoint info pointer, entry point PC, register
21*54fd6939SJiyong Park * width, cpu id & pointer to a context data structure, this function will
22*54fd6939SJiyong Park * initialize tsp context and entry point info for the secure payload
23*54fd6939SJiyong Park ******************************************************************************/
tspd_init_tsp_ep_state(struct entry_point_info * tsp_entry_point,uint32_t rw,uint64_t pc,tsp_context_t * tsp_ctx)24*54fd6939SJiyong Park void tspd_init_tsp_ep_state(struct entry_point_info *tsp_entry_point,
25*54fd6939SJiyong Park uint32_t rw,
26*54fd6939SJiyong Park uint64_t pc,
27*54fd6939SJiyong Park tsp_context_t *tsp_ctx)
28*54fd6939SJiyong Park {
29*54fd6939SJiyong Park uint32_t ep_attr;
30*54fd6939SJiyong Park
31*54fd6939SJiyong Park /* Passing a NULL context is a critical programming error */
32*54fd6939SJiyong Park assert(tsp_ctx);
33*54fd6939SJiyong Park assert(tsp_entry_point);
34*54fd6939SJiyong Park assert(pc);
35*54fd6939SJiyong Park
36*54fd6939SJiyong Park /*
37*54fd6939SJiyong Park * We support AArch64 TSP for now.
38*54fd6939SJiyong Park * TODO: Add support for AArch32 TSP
39*54fd6939SJiyong Park */
40*54fd6939SJiyong Park assert(rw == TSP_AARCH64);
41*54fd6939SJiyong Park
42*54fd6939SJiyong Park /* Associate this context with the cpu specified */
43*54fd6939SJiyong Park tsp_ctx->mpidr = read_mpidr_el1();
44*54fd6939SJiyong Park tsp_ctx->state = 0;
45*54fd6939SJiyong Park set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF);
46*54fd6939SJiyong Park clr_yield_smc_active_flag(tsp_ctx->state);
47*54fd6939SJiyong Park
48*54fd6939SJiyong Park cm_set_context(&tsp_ctx->cpu_ctx, SECURE);
49*54fd6939SJiyong Park
50*54fd6939SJiyong Park /* initialise an entrypoint to set up the CPU context */
51*54fd6939SJiyong Park ep_attr = SECURE | EP_ST_ENABLE;
52*54fd6939SJiyong Park if (read_sctlr_el3() & SCTLR_EE_BIT)
53*54fd6939SJiyong Park ep_attr |= EP_EE_BIG;
54*54fd6939SJiyong Park SET_PARAM_HEAD(tsp_entry_point, PARAM_EP, VERSION_1, ep_attr);
55*54fd6939SJiyong Park
56*54fd6939SJiyong Park tsp_entry_point->pc = pc;
57*54fd6939SJiyong Park tsp_entry_point->spsr = SPSR_64(MODE_EL1,
58*54fd6939SJiyong Park MODE_SP_ELX,
59*54fd6939SJiyong Park DISABLE_ALL_EXCEPTIONS);
60*54fd6939SJiyong Park zeromem(&tsp_entry_point->args, sizeof(tsp_entry_point->args));
61*54fd6939SJiyong Park }
62*54fd6939SJiyong Park
63*54fd6939SJiyong Park /*******************************************************************************
64*54fd6939SJiyong Park * This function takes an SP context pointer and:
65*54fd6939SJiyong Park * 1. Applies the S-EL1 system register context from tsp_ctx->cpu_ctx.
66*54fd6939SJiyong Park * 2. Saves the current C runtime state (callee saved registers) on the stack
67*54fd6939SJiyong Park * frame and saves a reference to this state.
68*54fd6939SJiyong Park * 3. Calls el3_exit() so that the EL3 system and general purpose registers
69*54fd6939SJiyong Park * from the tsp_ctx->cpu_ctx are used to enter the secure payload image.
70*54fd6939SJiyong Park ******************************************************************************/
tspd_synchronous_sp_entry(tsp_context_t * tsp_ctx)71*54fd6939SJiyong Park uint64_t tspd_synchronous_sp_entry(tsp_context_t *tsp_ctx)
72*54fd6939SJiyong Park {
73*54fd6939SJiyong Park uint64_t rc;
74*54fd6939SJiyong Park
75*54fd6939SJiyong Park assert(tsp_ctx != NULL);
76*54fd6939SJiyong Park assert(tsp_ctx->c_rt_ctx == 0);
77*54fd6939SJiyong Park
78*54fd6939SJiyong Park /* Apply the Secure EL1 system register context and switch to it */
79*54fd6939SJiyong Park assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
80*54fd6939SJiyong Park cm_el1_sysregs_context_restore(SECURE);
81*54fd6939SJiyong Park cm_set_next_eret_context(SECURE);
82*54fd6939SJiyong Park
83*54fd6939SJiyong Park rc = tspd_enter_sp(&tsp_ctx->c_rt_ctx);
84*54fd6939SJiyong Park #if ENABLE_ASSERTIONS
85*54fd6939SJiyong Park tsp_ctx->c_rt_ctx = 0;
86*54fd6939SJiyong Park #endif
87*54fd6939SJiyong Park
88*54fd6939SJiyong Park return rc;
89*54fd6939SJiyong Park }
90*54fd6939SJiyong Park
91*54fd6939SJiyong Park
92*54fd6939SJiyong Park /*******************************************************************************
93*54fd6939SJiyong Park * This function takes an SP context pointer and:
94*54fd6939SJiyong Park * 1. Saves the S-EL1 system register context tp tsp_ctx->cpu_ctx.
95*54fd6939SJiyong Park * 2. Restores the current C runtime state (callee saved registers) from the
96*54fd6939SJiyong Park * stack frame using the reference to this state saved in tspd_enter_sp().
97*54fd6939SJiyong Park * 3. It does not need to save any general purpose or EL3 system register state
98*54fd6939SJiyong Park * as the generic smc entry routine should have saved those.
99*54fd6939SJiyong Park ******************************************************************************/
tspd_synchronous_sp_exit(tsp_context_t * tsp_ctx,uint64_t ret)100*54fd6939SJiyong Park void tspd_synchronous_sp_exit(tsp_context_t *tsp_ctx, uint64_t ret)
101*54fd6939SJiyong Park {
102*54fd6939SJiyong Park assert(tsp_ctx != NULL);
103*54fd6939SJiyong Park /* Save the Secure EL1 system register context */
104*54fd6939SJiyong Park assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
105*54fd6939SJiyong Park cm_el1_sysregs_context_save(SECURE);
106*54fd6939SJiyong Park
107*54fd6939SJiyong Park assert(tsp_ctx->c_rt_ctx != 0);
108*54fd6939SJiyong Park tspd_exit_sp(tsp_ctx->c_rt_ctx, ret);
109*54fd6939SJiyong Park
110*54fd6939SJiyong Park /* Should never reach here */
111*54fd6939SJiyong Park assert(0);
112*54fd6939SJiyong Park }
113*54fd6939SJiyong Park
114*54fd6939SJiyong Park /*******************************************************************************
115*54fd6939SJiyong Park * This function takes an SP context pointer and abort any preempted SMC
116*54fd6939SJiyong Park * request.
117*54fd6939SJiyong Park * Return 1 if there was a preempted SMC request, 0 otherwise.
118*54fd6939SJiyong Park ******************************************************************************/
tspd_abort_preempted_smc(tsp_context_t * tsp_ctx)119*54fd6939SJiyong Park int tspd_abort_preempted_smc(tsp_context_t *tsp_ctx)
120*54fd6939SJiyong Park {
121*54fd6939SJiyong Park if (!get_yield_smc_active_flag(tsp_ctx->state))
122*54fd6939SJiyong Park return 0;
123*54fd6939SJiyong Park
124*54fd6939SJiyong Park /* Abort any preempted SMC request */
125*54fd6939SJiyong Park clr_yield_smc_active_flag(tsp_ctx->state);
126*54fd6939SJiyong Park
127*54fd6939SJiyong Park /*
128*54fd6939SJiyong Park * Arrange for an entry into the test secure payload. It will
129*54fd6939SJiyong Park * be returned via TSP_ABORT_DONE case in tspd_smc_handler.
130*54fd6939SJiyong Park */
131*54fd6939SJiyong Park cm_set_elr_el3(SECURE,
132*54fd6939SJiyong Park (uint64_t) &tsp_vectors->abort_yield_smc_entry);
133*54fd6939SJiyong Park uint64_t rc = tspd_synchronous_sp_entry(tsp_ctx);
134*54fd6939SJiyong Park
135*54fd6939SJiyong Park if (rc != 0)
136*54fd6939SJiyong Park panic();
137*54fd6939SJiyong Park
138*54fd6939SJiyong Park return 1;
139*54fd6939SJiyong Park }
140*54fd6939SJiyong Park
141