Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | a5b4c40 | 2018-01-08 17:33:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch_helpers.h> |
| 8 | #include <assert.h> |
| 9 | #include <bl31.h> |
| 10 | #include <context_mgmt.h> |
| 11 | #include <debug.h> |
| 12 | #include <errno.h> |
Antonio Nino Diaz | a5b4c40 | 2018-01-08 17:33:34 +0000 | [diff] [blame] | 13 | #include <mm_svc.h> |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 14 | #include <platform.h> |
| 15 | #include <runtime_svc.h> |
| 16 | #include <secure_partition.h> |
Antonio Nino Diaz | 085e80e | 2018-03-21 10:49:27 +0000 | [diff] [blame] | 17 | #include <smccc.h> |
| 18 | #include <smccc_helpers.h> |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 19 | #include <spinlock.h> |
| 20 | #include <spm_svc.h> |
| 21 | #include <utils.h> |
| 22 | #include <xlat_tables_v2.h> |
| 23 | |
| 24 | #include "spm_private.h" |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 25 | |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 26 | /******************************************************************************* |
| 27 | * Secure Partition context information. |
| 28 | ******************************************************************************/ |
| 29 | static secure_partition_context_t sp_ctx; |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 30 | |
| 31 | /******************************************************************************* |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 32 | * This function takes an SP context pointer and prepares the CPU to enter. |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 33 | ******************************************************************************/ |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 34 | static void spm_sp_prepare_enter(secure_partition_context_t *sp_ctx) |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 35 | { |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 36 | assert(sp_ctx != NULL); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 37 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 38 | /* Assign the context of the SP to this CPU */ |
| 39 | cm_set_context(&(sp_ctx->cpu_ctx), SECURE); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 40 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 41 | /* Restore the context assigned above */ |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 42 | cm_el1_sysregs_context_restore(SECURE); |
| 43 | cm_set_next_eret_context(SECURE); |
| 44 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 45 | /* Invalidate TLBs at EL1. */ |
| 46 | tlbivmalle1(); |
| 47 | dsbish(); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 50 | /******************************************************************************* |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 51 | * Enter SP after preparing it with spm_sp_prepare_enter(). |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 52 | ******************************************************************************/ |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 53 | static uint64_t spm_sp_enter(secure_partition_context_t *sp_ctx) |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 54 | { |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 55 | /* Enter Secure Partition */ |
| 56 | return spm_secure_partition_enter(&sp_ctx->c_rt_ctx); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /******************************************************************************* |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 60 | * Jump to each Secure Partition for the first time. |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 61 | ******************************************************************************/ |
Antonio Nino Diaz | b3323cd | 2018-04-17 15:10:18 +0100 | [diff] [blame] | 62 | static int32_t spm_init(void) |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 63 | { |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 64 | uint64_t rc = 0; |
| 65 | secure_partition_context_t *ctx; |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 66 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 67 | INFO("Secure Partition init...\n"); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 68 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 69 | ctx = &sp_ctx; |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 70 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 71 | ctx->sp_init_in_progress = 1; |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 72 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 73 | spm_sp_prepare_enter(ctx); |
| 74 | rc |= spm_sp_enter(ctx); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 75 | assert(rc == 0); |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 76 | |
| 77 | ctx->sp_init_in_progress = 0; |
| 78 | |
| 79 | INFO("Secure Partition initialized.\n"); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 80 | |
| 81 | return rc; |
| 82 | } |
| 83 | |
| 84 | /******************************************************************************* |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 85 | * Initialize contexts of all Secure Partitions. |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 86 | ******************************************************************************/ |
| 87 | int32_t spm_setup(void) |
| 88 | { |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 89 | secure_partition_context_t *ctx; |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 90 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 91 | /* Disable MMU at EL1 (initialized by BL2) */ |
| 92 | disable_mmu_icache_el1(); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 93 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 94 | /* Initialize context of the SP */ |
| 95 | INFO("Secure Partition context setup start...\n"); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 96 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 97 | ctx = &sp_ctx; |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 98 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 99 | /* Assign translation tables context. */ |
| 100 | ctx->xlat_ctx_handle = spm_get_sp_xlat_context(); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 101 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 102 | secure_partition_setup(ctx); |
| 103 | |
| 104 | /* Register init function for deferred init. */ |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 105 | bl31_register_bl32_init(&spm_init); |
| 106 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 107 | INFO("Secure Partition setup done.\n"); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 112 | /******************************************************************************* |
| 113 | * Secure Partition Manager SMC handler. |
| 114 | ******************************************************************************/ |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 115 | uint64_t spm_smc_handler(uint32_t smc_fid, |
| 116 | uint64_t x1, |
| 117 | uint64_t x2, |
| 118 | uint64_t x3, |
| 119 | uint64_t x4, |
| 120 | void *cookie, |
| 121 | void *handle, |
| 122 | uint64_t flags) |
| 123 | { |
| 124 | cpu_context_t *ns_cpu_context; |
| 125 | unsigned int ns; |
| 126 | |
| 127 | /* Determine which security state this SMC originated from */ |
| 128 | ns = is_caller_non_secure(flags); |
| 129 | |
| 130 | if (ns == SMC_FROM_SECURE) { |
| 131 | |
| 132 | /* Handle SMCs from Secure world. */ |
| 133 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 134 | assert(handle == cm_get_context(SECURE)); |
| 135 | |
| 136 | /* Make next ERET jump to S-EL0 instead of S-EL1. */ |
| 137 | cm_set_elr_spsr_el3(SECURE, read_elr_el1(), read_spsr_el1()); |
| 138 | |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 139 | switch (smc_fid) { |
| 140 | |
Sandrine Bailleux | 4d2787c | 2017-12-07 09:48:56 +0000 | [diff] [blame] | 141 | case SPM_VERSION_AARCH32: |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 142 | SMC_RET1(handle, SPM_VERSION_COMPILED); |
| 143 | |
| 144 | case SP_EVENT_COMPLETE_AARCH64: |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 145 | /* Save secure state */ |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 146 | cm_el1_sysregs_context_save(SECURE); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 147 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 148 | if (sp_ctx.sp_init_in_progress == 1) { |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 149 | /* |
| 150 | * SPM reports completion. The SPM must have |
| 151 | * initiated the original request through a |
| 152 | * synchronous entry into the secure |
| 153 | * partition. Jump back to the original C |
| 154 | * runtime context. |
| 155 | */ |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 156 | spm_secure_partition_exit(sp_ctx.c_rt_ctx, x1); |
| 157 | |
| 158 | /* spm_secure_partition_exit doesn't return */ |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Antonio Nino Diaz | a43c85d | 2018-01-08 09:59:33 +0000 | [diff] [blame] | 161 | /* Release the Secure Partition context */ |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 162 | spin_unlock(&(sp_ctx.lock)); |
Antonio Nino Diaz | a43c85d | 2018-01-08 09:59:33 +0000 | [diff] [blame] | 163 | |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 164 | /* |
| 165 | * This is the result from the Secure partition of an |
| 166 | * earlier request. Copy the result into the non-secure |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 167 | * context and return to the non-secure state. |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 168 | */ |
| 169 | |
| 170 | /* Get a reference to the non-secure context */ |
| 171 | ns_cpu_context = cm_get_context(NON_SECURE); |
Antonio Nino Diaz | b3323cd | 2018-04-17 15:10:18 +0100 | [diff] [blame] | 172 | assert(ns_cpu_context != NULL); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 173 | |
| 174 | /* Restore non-secure state */ |
| 175 | cm_el1_sysregs_context_restore(NON_SECURE); |
| 176 | cm_set_next_eret_context(NON_SECURE); |
| 177 | |
| 178 | /* Return to normal world */ |
| 179 | SMC_RET1(ns_cpu_context, x1); |
| 180 | |
Antonio Nino Diaz | fa0ed2b | 2017-12-01 14:12:43 +0000 | [diff] [blame] | 181 | case SP_MEMORY_ATTRIBUTES_GET_AARCH64: |
| 182 | INFO("Received SP_MEMORY_ATTRIBUTES_GET_AARCH64 SMC\n"); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 183 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 184 | if (sp_ctx.sp_init_in_progress == 0) { |
Antonio Nino Diaz | fa0ed2b | 2017-12-01 14:12:43 +0000 | [diff] [blame] | 185 | WARN("SP_MEMORY_ATTRIBUTES_GET_AARCH64 is available at boot time only\n"); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 186 | SMC_RET1(handle, SPM_NOT_SUPPORTED); |
| 187 | } |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 188 | SMC_RET1(handle, |
| 189 | spm_memory_attributes_get_smc_handler( |
| 190 | &sp_ctx, x1)); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 191 | |
Antonio Nino Diaz | fa0ed2b | 2017-12-01 14:12:43 +0000 | [diff] [blame] | 192 | case SP_MEMORY_ATTRIBUTES_SET_AARCH64: |
| 193 | INFO("Received SP_MEMORY_ATTRIBUTES_SET_AARCH64 SMC\n"); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 194 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 195 | if (sp_ctx.sp_init_in_progress == 0) { |
Antonio Nino Diaz | fa0ed2b | 2017-12-01 14:12:43 +0000 | [diff] [blame] | 196 | WARN("SP_MEMORY_ATTRIBUTES_SET_AARCH64 is available at boot time only\n"); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 197 | SMC_RET1(handle, SPM_NOT_SUPPORTED); |
| 198 | } |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 199 | SMC_RET1(handle, |
| 200 | spm_memory_attributes_set_smc_handler( |
| 201 | &sp_ctx, x1, x2, x3)); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 202 | default: |
| 203 | break; |
| 204 | } |
| 205 | } else { |
| 206 | |
| 207 | /* Handle SMCs from Non-secure world. */ |
| 208 | |
| 209 | switch (smc_fid) { |
| 210 | |
Antonio Nino Diaz | a5b4c40 | 2018-01-08 17:33:34 +0000 | [diff] [blame] | 211 | case MM_VERSION_AARCH32: |
| 212 | SMC_RET1(handle, MM_VERSION_COMPILED); |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 213 | |
Sandrine Bailleux | 756a2a0 | 2017-12-01 09:44:21 +0000 | [diff] [blame] | 214 | case MM_COMMUNICATE_AARCH32: |
| 215 | case MM_COMMUNICATE_AARCH64: |
Sandrine Bailleux | 4d2787c | 2017-12-07 09:48:56 +0000 | [diff] [blame] | 216 | { |
| 217 | uint64_t mm_cookie = x1; |
| 218 | uint64_t comm_buffer_address = x2; |
| 219 | uint64_t comm_size_address = x3; |
| 220 | |
| 221 | /* Cookie. Reserved for future use. It must be zero. */ |
Antonio Nino Diaz | b3323cd | 2018-04-17 15:10:18 +0100 | [diff] [blame] | 222 | if (mm_cookie != 0U) { |
Sandrine Bailleux | 4d2787c | 2017-12-07 09:48:56 +0000 | [diff] [blame] | 223 | ERROR("MM_COMMUNICATE: cookie is not zero\n"); |
| 224 | SMC_RET1(handle, SPM_INVALID_PARAMETER); |
| 225 | } |
| 226 | |
Antonio Nino Diaz | b3323cd | 2018-04-17 15:10:18 +0100 | [diff] [blame] | 227 | if (comm_buffer_address == 0U) { |
Sandrine Bailleux | 4d2787c | 2017-12-07 09:48:56 +0000 | [diff] [blame] | 228 | ERROR("MM_COMMUNICATE: comm_buffer_address is zero\n"); |
| 229 | SMC_RET1(handle, SPM_INVALID_PARAMETER); |
| 230 | } |
| 231 | |
Antonio Nino Diaz | b3323cd | 2018-04-17 15:10:18 +0100 | [diff] [blame] | 232 | if (comm_size_address != 0U) { |
Sandrine Bailleux | 4d2787c | 2017-12-07 09:48:56 +0000 | [diff] [blame] | 233 | VERBOSE("MM_COMMUNICATE: comm_size_address is not 0 as recommended.\n"); |
| 234 | } |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 235 | |
| 236 | /* Save the Normal world context */ |
| 237 | cm_el1_sysregs_context_save(NON_SECURE); |
| 238 | |
Antonio Nino Diaz | a43c85d | 2018-01-08 09:59:33 +0000 | [diff] [blame] | 239 | /* Lock the Secure Partition context. */ |
| 240 | spin_lock(&sp_ctx.lock); |
| 241 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 242 | /* Jump to the Secure Partition. */ |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 243 | |
Antonio Nino Diaz | 22282bb | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 244 | spm_sp_prepare_enter(&sp_ctx); |
| 245 | |
| 246 | SMC_RET4(&(sp_ctx.cpu_ctx), smc_fid, |
| 247 | comm_buffer_address, comm_size_address, |
| 248 | plat_my_core_pos()); |
Sandrine Bailleux | 4d2787c | 2017-12-07 09:48:56 +0000 | [diff] [blame] | 249 | } |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 250 | |
Antonio Nino Diaz | fa0ed2b | 2017-12-01 14:12:43 +0000 | [diff] [blame] | 251 | case SP_MEMORY_ATTRIBUTES_GET_AARCH64: |
| 252 | case SP_MEMORY_ATTRIBUTES_SET_AARCH64: |
Antonio Nino Diaz | 2fccb22 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 253 | /* SMC interfaces reserved for secure callers. */ |
| 254 | SMC_RET1(handle, SPM_NOT_SUPPORTED); |
| 255 | |
| 256 | default: |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | SMC_RET1(handle, SMC_UNK); |
| 262 | } |