blob: 70959d75679dc8f71b5e78af66bab6e5a3894125 [file] [log] [blame]
Achin Gupta375f5382014-02-18 18:12:48 +00001/*
Douglas Raillarda8954fc2017-01-26 15:54:44 +00002 * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
Achin Gupta375f5382014-02-18 18:12:48 +00003 *
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
Achin Gupta375f5382014-02-18 18:12:48 +000031#include <arch_helpers.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010032#include <assert.h>
Achin Gupta375f5382014-02-18 18:12:48 +000033#include <bl_common.h>
Achin Gupta375f5382014-02-18 18:12:48 +000034#include <context_mgmt.h>
Douglas Raillardf2129652016-11-24 15:43:19 +000035#include <debug.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010036#include <string.h>
Douglas Raillardf2129652016-11-24 15:43:19 +000037#include <tsp.h>
Douglas Raillarda8954fc2017-01-26 15:54:44 +000038#include <utils.h>
Dan Handley714a0d22014-04-09 13:13:04 +010039#include "tspd_private.h"
Achin Gupta375f5382014-02-18 18:12:48 +000040
41/*******************************************************************************
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010042 * Given a secure payload entrypoint info pointer, entry point PC, register
43 * width, cpu id & pointer to a context data structure, this function will
44 * initialize tsp context and entry point info for the secure payload
Achin Gupta375f5382014-02-18 18:12:48 +000045 ******************************************************************************/
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010046void tspd_init_tsp_ep_state(struct entry_point_info *tsp_entry_point,
47 uint32_t rw,
48 uint64_t pc,
49 tsp_context_t *tsp_ctx)
Achin Gupta375f5382014-02-18 18:12:48 +000050{
Andrew Thoelke4e126072014-06-04 21:10:52 +010051 uint32_t ep_attr;
Achin Gupta375f5382014-02-18 18:12:48 +000052
53 /* Passing a NULL context is a critical programming error */
54 assert(tsp_ctx);
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010055 assert(tsp_entry_point);
56 assert(pc);
Achin Gupta375f5382014-02-18 18:12:48 +000057
58 /*
59 * We support AArch64 TSP for now.
60 * TODO: Add support for AArch32 TSP
61 */
62 assert(rw == TSP_AARCH64);
63
Andrew Thoelke4e126072014-06-04 21:10:52 +010064 /* Associate this context with the cpu specified */
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010065 tsp_ctx->mpidr = read_mpidr_el1();
Andrew Thoelke4e126072014-06-04 21:10:52 +010066 tsp_ctx->state = 0;
Achin Gupta18d6eaf2014-05-04 18:23:26 +010067 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF);
Achin Gupta18d6eaf2014-05-04 18:23:26 +010068 clr_std_smc_active_flag(tsp_ctx->state);
Achin Gupta375f5382014-02-18 18:12:48 +000069
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010070 cm_set_context(&tsp_ctx->cpu_ctx, SECURE);
Andrew Thoelke4e126072014-06-04 21:10:52 +010071
72 /* initialise an entrypoint to set up the CPU context */
73 ep_attr = SECURE | EP_ST_ENABLE;
74 if (read_sctlr_el3() & SCTLR_EE_BIT)
75 ep_attr |= EP_EE_BIG;
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010076 SET_PARAM_HEAD(tsp_entry_point, PARAM_EP, VERSION_1, ep_attr);
Achin Gupta375f5382014-02-18 18:12:48 +000077
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010078 tsp_entry_point->pc = pc;
79 tsp_entry_point->spsr = SPSR_64(MODE_EL1,
80 MODE_SP_ELX,
81 DISABLE_ALL_EXCEPTIONS);
Douglas Raillarda8954fc2017-01-26 15:54:44 +000082 zeromem(&tsp_entry_point->args, sizeof(tsp_entry_point->args));
Achin Gupta375f5382014-02-18 18:12:48 +000083}
84
85/*******************************************************************************
86 * This function takes an SP context pointer and:
87 * 1. Applies the S-EL1 system register context from tsp_ctx->cpu_ctx.
88 * 2. Saves the current C runtime state (callee saved registers) on the stack
89 * frame and saves a reference to this state.
90 * 3. Calls el3_exit() so that the EL3 system and general purpose registers
91 * from the tsp_ctx->cpu_ctx are used to enter the secure payload image.
92 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +010093uint64_t tspd_synchronous_sp_entry(tsp_context_t *tsp_ctx)
Achin Gupta375f5382014-02-18 18:12:48 +000094{
95 uint64_t rc;
96
Juan Castillof558cac2014-06-05 09:45:36 +010097 assert(tsp_ctx != NULL);
Achin Gupta375f5382014-02-18 18:12:48 +000098 assert(tsp_ctx->c_rt_ctx == 0);
99
100 /* Apply the Secure EL1 system register context and switch to it */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100101 assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
Achin Gupta375f5382014-02-18 18:12:48 +0000102 cm_el1_sysregs_context_restore(SECURE);
103 cm_set_next_eret_context(SECURE);
104
105 rc = tspd_enter_sp(&tsp_ctx->c_rt_ctx);
106#if DEBUG
107 tsp_ctx->c_rt_ctx = 0;
108#endif
109
110 return rc;
111}
112
113
114/*******************************************************************************
115 * This function takes an SP context pointer and:
116 * 1. Saves the S-EL1 system register context tp tsp_ctx->cpu_ctx.
117 * 2. Restores the current C runtime state (callee saved registers) from the
118 * stack frame using the reference to this state saved in tspd_enter_sp().
119 * 3. It does not need to save any general purpose or EL3 system register state
120 * as the generic smc entry routine should have saved those.
121 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +0100122void tspd_synchronous_sp_exit(tsp_context_t *tsp_ctx, uint64_t ret)
Achin Gupta375f5382014-02-18 18:12:48 +0000123{
Juan Castillof558cac2014-06-05 09:45:36 +0100124 assert(tsp_ctx != NULL);
Achin Gupta375f5382014-02-18 18:12:48 +0000125 /* Save the Secure EL1 system register context */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100126 assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
Achin Gupta375f5382014-02-18 18:12:48 +0000127 cm_el1_sysregs_context_save(SECURE);
128
129 assert(tsp_ctx->c_rt_ctx != 0);
130 tspd_exit_sp(tsp_ctx->c_rt_ctx, ret);
131
132 /* Should never reach here */
133 assert(0);
134}
Douglas Raillardf2129652016-11-24 15:43:19 +0000135
136/*******************************************************************************
137 * This function takes an SP context pointer and abort any preempted SMC
138 * request.
139 * Return 1 if there was a preempted SMC request, 0 otherwise.
140 ******************************************************************************/
141int tspd_abort_preempted_smc(tsp_context_t *tsp_ctx)
142{
143 if (!get_std_smc_active_flag(tsp_ctx->state))
144 return 0;
145
146 /* Abort any preempted SMC request */
147 clr_std_smc_active_flag(tsp_ctx->state);
148
149 /*
150 * Arrange for an entry into the test secure payload. It will
151 * be returned via TSP_ABORT_DONE case in tspd_smc_handler.
152 */
153 cm_set_elr_el3(SECURE,
154 (uint64_t) &tsp_vectors->abort_std_smc_entry);
155 uint64_t rc = tspd_synchronous_sp_entry(tsp_ctx);
156
157 if (rc != 0)
158 panic();
159
160 return 1;
161}
162