1 /* 2 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. 3 * 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 31 32 /******************************************************************************* 33 * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a 34 * plug-in component to the Secure Monitor, registered as a runtime service. The 35 * SPD is expected to be a functional extension of the Secure Payload (SP) that 36 * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting 37 * the Trusted OS/Applications range to the dispatcher. The SPD will either 38 * handle the request locally or delegate it to the Secure Payload. It is also 39 * responsible for initialising and maintaining communication with the SP. 40 ******************************************************************************/ 41 #include <arch_helpers.h> 42 #include <assert.h> 43 #include <bl_common.h> 44 #include <bl31.h> 45 #include <context_mgmt.h> 46 #include <runtime_svc.h> 47 #include <stddef.h> 48 #include <tsp.h> 49 #include <uuid.h> 50 #include "tspd_private.h" 51 52 /******************************************************************************* 53 * Single structure to hold information about the various entry points into the 54 * Secure Payload. It is initialised once on the primary core after a cold boot. 55 ******************************************************************************/ 56 entry_info_t *tsp_entry_info; 57 58 /******************************************************************************* 59 * Array to keep track of per-cpu Secure Payload state 60 ******************************************************************************/ 61 tsp_context_t tspd_sp_context[TSPD_CORE_COUNT]; 62 63 64 /* TSP UID */ 65 DEFINE_SVC_UUID(tsp_uuid, 66 0x5b3056a0, 0x3291, 0x427b, 0x98, 0x11, 67 0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa); 68 69 int32_t tspd_init(void); 70 71 72 /******************************************************************************* 73 * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type 74 * (aarch32/aarch64) if not already known and initialises the context for entry 75 * into the SP for its initialisation. 76 ******************************************************************************/ 77 int32_t tspd_setup(void) 78 { 79 entry_point_info_t *image_info; 80 int32_t rc; 81 uint64_t mpidr = read_mpidr(); 82 uint32_t linear_id; 83 84 linear_id = platform_get_core_pos(mpidr); 85 86 /* 87 * Get information about the Secure Payload (BL32) image. Its 88 * absence is a critical failure. TODO: Add support to 89 * conditionally include the SPD service 90 */ 91 image_info = bl31_get_next_image_info(SECURE); 92 assert(image_info); 93 94 /* 95 * If there's no valid entry point for SP, we return a non-zero value 96 * signalling failure initializing the service. We bail out without 97 * registering any handlers 98 */ 99 if (!image_info->pc) 100 return 1; 101 102 /* 103 * We could inspect the SP image and determine it's execution 104 * state i.e whether AArch32 or AArch64. Assuming it's AArch64 105 * for the time being. 106 */ 107 rc = tspd_init_secure_context(image_info->pc, 108 TSP_AARCH64, 109 mpidr, 110 &tspd_sp_context[linear_id]); 111 assert(rc == 0); 112 113 /* 114 * All TSPD initialization done. Now register our init function with 115 * BL31 for deferred invocation 116 */ 117 bl31_register_bl32_init(&tspd_init); 118 119 return rc; 120 } 121 122 /******************************************************************************* 123 * This function passes control to the Secure Payload image (BL32) for the first 124 * time on the primary cpu after a cold boot. It assumes that a valid secure 125 * context has already been created by tspd_setup() which can be directly used. 126 * It also assumes that a valid non-secure context has been initialised by PSCI 127 * so it does not need to save and restore any non-secure state. This function 128 * performs a synchronous entry into the Secure payload. The SP passes control 129 * back to this routine through a SMC. 130 ******************************************************************************/ 131 int32_t tspd_init(void) 132 { 133 uint64_t mpidr = read_mpidr(); 134 uint32_t linear_id = platform_get_core_pos(mpidr); 135 uint64_t rc; 136 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 137 138 /* 139 * Arrange for an entry into the test secure payload. We expect an array 140 * of vectors in return 141 */ 142 rc = tspd_synchronous_sp_entry(tsp_ctx); 143 assert(rc != 0); 144 if (rc) { 145 tsp_ctx->state = TSP_STATE_ON; 146 147 /* 148 * TSP has been successfully initialized. Register power 149 * managemnt hooks with PSCI 150 */ 151 psci_register_spd_pm_hook(&tspd_pm); 152 } 153 154 return rc; 155 } 156 157 158 /******************************************************************************* 159 * This function is responsible for handling all SMCs in the Trusted OS/App 160 * range from the non-secure state as defined in the SMC Calling Convention 161 * Document. It is also responsible for communicating with the Secure payload 162 * to delegate work and return results back to the non-secure state. Lastly it 163 * will also return any information that the secure payload needs to do the 164 * work assigned to it. 165 ******************************************************************************/ 166 uint64_t tspd_smc_handler(uint32_t smc_fid, 167 uint64_t x1, 168 uint64_t x2, 169 uint64_t x3, 170 uint64_t x4, 171 void *cookie, 172 void *handle, 173 uint64_t flags) 174 { 175 cpu_context_t *ns_cpu_context; 176 gp_regs_t *ns_gp_regs; 177 unsigned long mpidr = read_mpidr(); 178 uint32_t linear_id = platform_get_core_pos(mpidr), ns; 179 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 180 181 /* Determine which security state this SMC originated from */ 182 ns = is_caller_non_secure(flags); 183 184 switch (smc_fid) { 185 186 /* 187 * This function ID is used only by the SP to indicate it has 188 * finished initialising itself after a cold boot 189 */ 190 case TSP_ENTRY_DONE: 191 if (ns) 192 SMC_RET1(handle, SMC_UNK); 193 194 /* 195 * Stash the SP entry points information. This is done 196 * only once on the primary cpu 197 */ 198 assert(tsp_entry_info == NULL); 199 tsp_entry_info = (entry_info_t *) x1; 200 201 /* 202 * SP reports completion. The SPD must have initiated 203 * the original request through a synchronous entry 204 * into the SP. Jump back to the original C runtime 205 * context. 206 */ 207 tspd_synchronous_sp_exit(tsp_ctx, x1); 208 209 /* Should never reach here */ 210 assert(0); 211 212 /* 213 * These function IDs is used only by the SP to indicate it has 214 * finished: 215 * 1. turning itself on in response to an earlier psci 216 * cpu_on request 217 * 2. resuming itself after an earlier psci cpu_suspend 218 * request. 219 */ 220 case TSP_ON_DONE: 221 case TSP_RESUME_DONE: 222 223 /* 224 * These function IDs is used only by the SP to indicate it has 225 * finished: 226 * 1. suspending itself after an earlier psci cpu_suspend 227 * request. 228 * 2. turning itself off in response to an earlier psci 229 * cpu_off request. 230 */ 231 case TSP_OFF_DONE: 232 case TSP_SUSPEND_DONE: 233 if (ns) 234 SMC_RET1(handle, SMC_UNK); 235 236 /* 237 * SP reports completion. The SPD must have initiated the 238 * original request through a synchronous entry into the SP. 239 * Jump back to the original C runtime context, and pass x1 as 240 * return value to the caller 241 */ 242 tspd_synchronous_sp_exit(tsp_ctx, x1); 243 244 /* Should never reach here */ 245 assert(0); 246 247 /* 248 * Request from non-secure client to perform an 249 * arithmetic operation or response from secure 250 * payload to an earlier request. 251 */ 252 case TSP_FID_ADD: 253 case TSP_FID_SUB: 254 case TSP_FID_MUL: 255 case TSP_FID_DIV: 256 if (ns) { 257 /* 258 * This is a fresh request from the non-secure client. 259 * The parameters are in x1 and x2. Figure out which 260 * registers need to be preserved, save the non-secure 261 * state and send the request to the secure payload. 262 */ 263 assert(handle == cm_get_context(mpidr, NON_SECURE)); 264 cm_el1_sysregs_context_save(NON_SECURE); 265 266 /* Save x1 and x2 for use by TSP_GET_ARGS call below */ 267 SMC_SET_GP(handle, CTX_GPREG_X1, x1); 268 SMC_SET_GP(handle, CTX_GPREG_X2, x2); 269 270 /* 271 * We are done stashing the non-secure context. Ask the 272 * secure payload to do the work now. 273 */ 274 275 /* 276 * Verify if there is a valid context to use, copy the 277 * operation type and parameters to the secure context 278 * and jump to the fast smc entry point in the secure 279 * payload. Entry into S-EL1 will take place upon exit 280 * from this function. 281 */ 282 assert(&tsp_ctx->cpu_ctx == cm_get_context(mpidr, SECURE)); 283 set_aapcs_args7(&tsp_ctx->cpu_ctx, smc_fid, x1, x2, 0, 0, 284 0, 0, 0); 285 cm_set_el3_elr(SECURE, (uint64_t) tsp_entry_info->fast_smc_entry); 286 cm_el1_sysregs_context_restore(SECURE); 287 cm_set_next_eret_context(SECURE); 288 289 return smc_fid; 290 } else { 291 /* 292 * This is the result from the secure client of an 293 * earlier request. The results are in x1-x2. Copy it 294 * into the non-secure context, save the secure state 295 * and return to the non-secure state. 296 */ 297 assert(handle == cm_get_context(mpidr, SECURE)); 298 cm_el1_sysregs_context_save(SECURE); 299 300 /* Get a reference to the non-secure context */ 301 ns_cpu_context = cm_get_context(mpidr, NON_SECURE); 302 assert(ns_cpu_context); 303 ns_gp_regs = get_gpregs_ctx(ns_cpu_context); 304 305 /* Restore non-secure state */ 306 cm_el1_sysregs_context_restore(NON_SECURE); 307 cm_set_next_eret_context(NON_SECURE); 308 309 SMC_RET2(ns_gp_regs, x1, x2); 310 } 311 312 break; 313 314 /* 315 * This is a request from the secure payload for more arguments 316 * for an ongoing arithmetic operation requested by the 317 * non-secure world. Simply return the arguments from the non- 318 * secure client in the original call. 319 */ 320 case TSP_GET_ARGS: 321 if (ns) 322 SMC_RET1(handle, SMC_UNK); 323 324 /* Get a reference to the non-secure context */ 325 ns_cpu_context = cm_get_context(mpidr, NON_SECURE); 326 assert(ns_cpu_context); 327 ns_gp_regs = get_gpregs_ctx(ns_cpu_context); 328 329 SMC_RET2(handle, read_ctx_reg(ns_gp_regs, CTX_GPREG_X1), 330 read_ctx_reg(ns_gp_regs, CTX_GPREG_X2)); 331 332 case TOS_CALL_COUNT: 333 /* 334 * Return the number of service function IDs implemented to 335 * provide service to non-secure 336 */ 337 SMC_RET1(handle, TSP_NUM_FID); 338 339 case TOS_UID: 340 /* Return TSP UID to the caller */ 341 SMC_UUID_RET(handle, tsp_uuid); 342 343 case TOS_CALL_VERSION: 344 /* Return the version of current implementation */ 345 SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR); 346 347 default: 348 break; 349 } 350 351 SMC_RET1(handle, SMC_UNK); 352 } 353 354 /* Define a SPD runtime service descriptor */ 355 DECLARE_RT_SVC( 356 spd, 357 358 OEN_TOS_START, 359 OEN_TOS_END, 360 SMC_TYPE_FAST, 361 tspd_setup, 362 tspd_smc_handler 363 ); 364