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