blob: 063fd01ff18f080ed904564ecf0a833aaab4d497 [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 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta375f5382014-02-18 18:12:48 +00005 */
6
Dan Handley2bd4ef22014-04-09 13:14:54 +01007#include <assert.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +01008#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
10#include <arch_helpers.h>
11#include <bl32/tsp/tsp.h>
12#include <common/bl_common.h>
13#include <common/debug.h>
14#include <lib/el3_runtime/context_mgmt.h>
15#include <lib/utils.h>
16
Dan Handley714a0d22014-04-09 13:13:04 +010017#include "tspd_private.h"
Achin Gupta375f5382014-02-18 18:12:48 +000018
19/*******************************************************************************
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010020 * Given a secure payload entrypoint info pointer, entry point PC, register
21 * width, cpu id & pointer to a context data structure, this function will
22 * initialize tsp context and entry point info for the secure payload
Achin Gupta375f5382014-02-18 18:12:48 +000023 ******************************************************************************/
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010024void tspd_init_tsp_ep_state(struct entry_point_info *tsp_entry_point,
25 uint32_t rw,
26 uint64_t pc,
27 tsp_context_t *tsp_ctx)
Achin Gupta375f5382014-02-18 18:12:48 +000028{
Andrew Thoelke4e126072014-06-04 21:10:52 +010029 uint32_t ep_attr;
Achin Gupta375f5382014-02-18 18:12:48 +000030
31 /* Passing a NULL context is a critical programming error */
32 assert(tsp_ctx);
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010033 assert(tsp_entry_point);
34 assert(pc);
Achin Gupta375f5382014-02-18 18:12:48 +000035
36 /*
37 * We support AArch64 TSP for now.
38 * TODO: Add support for AArch32 TSP
39 */
40 assert(rw == TSP_AARCH64);
41
Andrew Thoelke4e126072014-06-04 21:10:52 +010042 /* Associate this context with the cpu specified */
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010043 tsp_ctx->mpidr = read_mpidr_el1();
Andrew Thoelke4e126072014-06-04 21:10:52 +010044 tsp_ctx->state = 0;
Achin Gupta18d6eaf2014-05-04 18:23:26 +010045 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF);
David Cunado28f69ab2017-04-05 11:34:03 +010046 clr_yield_smc_active_flag(tsp_ctx->state);
Achin Gupta375f5382014-02-18 18:12:48 +000047
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010048 cm_set_context(&tsp_ctx->cpu_ctx, SECURE);
Andrew Thoelke4e126072014-06-04 21:10:52 +010049
50 /* initialise an entrypoint to set up the CPU context */
51 ep_attr = SECURE | EP_ST_ENABLE;
52 if (read_sctlr_el3() & SCTLR_EE_BIT)
53 ep_attr |= EP_EE_BIG;
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010054 SET_PARAM_HEAD(tsp_entry_point, PARAM_EP, VERSION_1, ep_attr);
Achin Gupta375f5382014-02-18 18:12:48 +000055
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +010056 tsp_entry_point->pc = pc;
57 tsp_entry_point->spsr = SPSR_64(MODE_EL1,
58 MODE_SP_ELX,
59 DISABLE_ALL_EXCEPTIONS);
Douglas Raillarda8954fc2017-01-26 15:54:44 +000060 zeromem(&tsp_entry_point->args, sizeof(tsp_entry_point->args));
Achin Gupta375f5382014-02-18 18:12:48 +000061}
62
63/*******************************************************************************
64 * This function takes an SP context pointer and:
65 * 1. Applies the S-EL1 system register context from tsp_ctx->cpu_ctx.
66 * 2. Saves the current C runtime state (callee saved registers) on the stack
67 * frame and saves a reference to this state.
68 * 3. Calls el3_exit() so that the EL3 system and general purpose registers
69 * from the tsp_ctx->cpu_ctx are used to enter the secure payload image.
70 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +010071uint64_t tspd_synchronous_sp_entry(tsp_context_t *tsp_ctx)
Achin Gupta375f5382014-02-18 18:12:48 +000072{
73 uint64_t rc;
74
Juan Castillof558cac2014-06-05 09:45:36 +010075 assert(tsp_ctx != NULL);
Achin Gupta375f5382014-02-18 18:12:48 +000076 assert(tsp_ctx->c_rt_ctx == 0);
77
78 /* Apply the Secure EL1 system register context and switch to it */
Andrew Thoelkea2f65532014-05-14 17:09:32 +010079 assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
Achin Gupta375f5382014-02-18 18:12:48 +000080 cm_el1_sysregs_context_restore(SECURE);
81 cm_set_next_eret_context(SECURE);
82
83 rc = tspd_enter_sp(&tsp_ctx->c_rt_ctx);
Antonio Nino Diaz0fbaa5c2017-10-19 16:55:48 +010084#if ENABLE_ASSERTIONS
Achin Gupta375f5382014-02-18 18:12:48 +000085 tsp_ctx->c_rt_ctx = 0;
86#endif
87
88 return rc;
89}
90
91
92/*******************************************************************************
93 * This function takes an SP context pointer and:
94 * 1. Saves the S-EL1 system register context tp tsp_ctx->cpu_ctx.
95 * 2. Restores the current C runtime state (callee saved registers) from the
96 * stack frame using the reference to this state saved in tspd_enter_sp().
97 * 3. It does not need to save any general purpose or EL3 system register state
98 * as the generic smc entry routine should have saved those.
99 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +0100100void tspd_synchronous_sp_exit(tsp_context_t *tsp_ctx, uint64_t ret)
Achin Gupta375f5382014-02-18 18:12:48 +0000101{
Juan Castillof558cac2014-06-05 09:45:36 +0100102 assert(tsp_ctx != NULL);
Achin Gupta375f5382014-02-18 18:12:48 +0000103 /* Save the Secure EL1 system register context */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100104 assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
Achin Gupta375f5382014-02-18 18:12:48 +0000105 cm_el1_sysregs_context_save(SECURE);
106
107 assert(tsp_ctx->c_rt_ctx != 0);
108 tspd_exit_sp(tsp_ctx->c_rt_ctx, ret);
109
110 /* Should never reach here */
111 assert(0);
112}
Douglas Raillardf2129652016-11-24 15:43:19 +0000113
114/*******************************************************************************
115 * This function takes an SP context pointer and abort any preempted SMC
116 * request.
117 * Return 1 if there was a preempted SMC request, 0 otherwise.
118 ******************************************************************************/
119int tspd_abort_preempted_smc(tsp_context_t *tsp_ctx)
120{
David Cunado28f69ab2017-04-05 11:34:03 +0100121 if (!get_yield_smc_active_flag(tsp_ctx->state))
Douglas Raillardf2129652016-11-24 15:43:19 +0000122 return 0;
123
124 /* Abort any preempted SMC request */
David Cunado28f69ab2017-04-05 11:34:03 +0100125 clr_yield_smc_active_flag(tsp_ctx->state);
Douglas Raillardf2129652016-11-24 15:43:19 +0000126
127 /*
128 * Arrange for an entry into the test secure payload. It will
129 * be returned via TSP_ABORT_DONE case in tspd_smc_handler.
130 */
131 cm_set_elr_el3(SECURE,
David Cunado28f69ab2017-04-05 11:34:03 +0100132 (uint64_t) &tsp_vectors->abort_yield_smc_entry);
Douglas Raillardf2129652016-11-24 15:43:19 +0000133 uint64_t rc = tspd_synchronous_sp_entry(tsp_ctx);
134
135 if (rc != 0)
136 panic();
137
138 return 1;
139}
140