1 /* 2 * Copyright (c) 2015, 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 * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a 33 * plug-in component to the Secure Monitor, registered as a runtime service. The 34 * SPD is expected to be a functional extension of the Secure Payload (SP) that 35 * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting 36 * the Trusted OS/Applications range to the dispatcher. The SPD will either 37 * handle the request locally or delegate it to the Secure Payload. It is also 38 * responsible for initialising and maintaining communication with the SP. 39 ******************************************************************************/ 40 #include <arch_helpers.h> 41 #include <assert.h> 42 #include <bl_common.h> 43 #include <bl31.h> 44 #include <context_mgmt.h> 45 #include <debug.h> 46 #include <errno.h> 47 #include <platform.h> 48 #include <runtime_svc.h> 49 #include <stddef.h> 50 #include <tlk.h> 51 #include <uuid.h> 52 #include "tlkd_private.h" 53 54 extern const spd_pm_ops_t tlkd_pm_ops; 55 56 /******************************************************************************* 57 * Array to keep track of per-cpu Secure Payload state 58 ******************************************************************************/ 59 static tlk_context_t tlk_ctx; 60 61 /* TLK UID: RFC-4122 compliant UUID (version-5, sha-1) */ 62 DEFINE_SVC_UUID(tlk_uuid, 63 0xbd11e9c9, 0x2bba, 0x52ee, 0xb1, 0x72, 64 0x46, 0x1f, 0xba, 0x97, 0x7f, 0x63); 65 66 int32_t tlkd_init(void); 67 68 /* 69 * The number of arguments/results to save during a SMC call for TLK. 70 */ 71 #define TLK_SHDBUF_SIZE 4 72 73 /******************************************************************************* 74 * Shared memory buffer for passing SMC args/results to TLK 75 ******************************************************************************/ 76 typedef struct tlk_args_results { 77 uint64_t args[TLK_SHDBUF_SIZE]; 78 } tlk_args_results_t; 79 80 static tlk_args_results_t *tlk_args_results_buf; 81 82 /* 83 * Helper function to store args from TLK and pass results back 84 */ 85 static inline void store_tlk_args_results(uint64_t x0, uint64_t x1, uint64_t x2, 86 uint64_t x3) 87 { 88 /* store arguments sent by TLK */ 89 tlk_args_results_buf->args[0] = x0; 90 tlk_args_results_buf->args[1] = x1; 91 tlk_args_results_buf->args[2] = x2; 92 tlk_args_results_buf->args[3] = x3; 93 94 flush_dcache_range((uint64_t)tlk_args_results_buf, 95 sizeof(tlk_args_results_t)); 96 } 97 98 /******************************************************************************* 99 * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type 100 * (aarch32/aarch64) if not already known and initialises the context for entry 101 * into the SP for its initialisation. 102 ******************************************************************************/ 103 int32_t tlkd_setup(void) 104 { 105 entry_point_info_t *tlk_ep_info; 106 107 /* 108 * Get information about the Secure Payload (BL32) image. Its 109 * absence is a critical failure. 110 */ 111 tlk_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 112 if (!tlk_ep_info) { 113 WARN("No SP provided. Booting device without SP" 114 " initialization. SMC`s destined for SP" 115 " will return SMC_UNK\n"); 116 return 1; 117 } 118 119 /* 120 * If there's no valid entry point for SP, we return a non-zero value 121 * signalling failure initializing the service. We bail out without 122 * registering any handlers 123 */ 124 if (!tlk_ep_info->pc) 125 return 1; 126 127 /* 128 * Inspect the SP image's SPSR and determine it's execution state 129 * i.e whether AArch32 or AArch64. 130 */ 131 tlkd_init_tlk_ep_state(tlk_ep_info, 132 (tlk_ep_info->spsr >> MODE_RW_SHIFT) & MODE_RW_MASK, 133 tlk_ep_info->pc, 134 &tlk_ctx); 135 136 /* 137 * All TLK SPD initialization done. Now register our init function 138 * with BL31 for deferred invocation 139 */ 140 bl31_register_bl32_init(&tlkd_init); 141 142 return 0; 143 } 144 145 /******************************************************************************* 146 * This function passes control to the Secure Payload image (BL32) for the first 147 * time on the primary cpu after a cold boot. It assumes that a valid secure 148 * context has already been created by tlkd_setup() which can be directly 149 * used. This function performs a synchronous entry into the Secure payload. 150 * The SP passes control back to this routine through a SMC. 151 ******************************************************************************/ 152 int32_t tlkd_init(void) 153 { 154 uint64_t mpidr = read_mpidr(); 155 entry_point_info_t *tlk_entry_point; 156 157 /* 158 * Get information about the Secure Payload (BL32) image. Its 159 * absence is a critical failure. 160 */ 161 tlk_entry_point = bl31_plat_get_next_image_ep_info(SECURE); 162 assert(tlk_entry_point); 163 164 cm_init_context(mpidr, tlk_entry_point); 165 166 /* 167 * Arrange for an entry into the test secure payload. 168 */ 169 return tlkd_synchronous_sp_entry(&tlk_ctx); 170 } 171 172 /******************************************************************************* 173 * This function is responsible for handling all SMCs in the Trusted OS/App 174 * range from the non-secure state as defined in the SMC Calling Convention 175 * Document. It is also responsible for communicating with the Secure payload 176 * to delegate work and return results back to the non-secure state. Lastly it 177 * will also return any information that the secure payload needs to do the 178 * work assigned to it. 179 ******************************************************************************/ 180 uint64_t tlkd_smc_handler(uint32_t smc_fid, 181 uint64_t x1, 182 uint64_t x2, 183 uint64_t x3, 184 uint64_t x4, 185 void *cookie, 186 void *handle, 187 uint64_t flags) 188 { 189 cpu_context_t *ns_cpu_context; 190 uint32_t ns; 191 192 /* Passing a NULL context is a critical programming error */ 193 assert(handle); 194 195 /* Determine which security state this SMC originated from */ 196 ns = is_caller_non_secure(flags); 197 198 switch (smc_fid) { 199 200 /* 201 * This is a request from the non-secure context to: 202 * 203 * a. register shared memory with the SP for storing it's 204 * activity logs. 205 * b. register shared memory with the SP for passing args 206 * required for maintaining sessions with the Trusted 207 * Applications. 208 */ 209 case TLK_REGISTER_LOGBUF: 210 case TLK_REGISTER_REQBUF: 211 if (!ns || !tlk_args_results_buf) 212 SMC_RET1(handle, SMC_UNK); 213 214 /* 215 * This is a fresh request from the non-secure client. 216 * The parameters are in x1 and x2. Figure out which 217 * registers need to be preserved, save the non-secure 218 * state and send the request to the secure payload. 219 */ 220 assert(handle == cm_get_context(NON_SECURE)); 221 222 /* Check if we are already preempted */ 223 if (get_std_smc_active_flag(tlk_ctx.state)) 224 SMC_RET1(handle, SMC_UNK); 225 226 cm_el1_sysregs_context_save(NON_SECURE); 227 228 /* 229 * Verify if there is a valid context to use. 230 */ 231 assert(&tlk_ctx.cpu_ctx == cm_get_context(SECURE)); 232 233 /* 234 * Mark the SP state as active. 235 */ 236 set_std_smc_active_flag(tlk_ctx.state); 237 238 /* Save args for use by the SP on return */ 239 store_tlk_args_results(smc_fid, x1, x2, x3); 240 241 /* 242 * We are done stashing the non-secure context. Ask the 243 * secure payload to do the work now. 244 */ 245 cm_el1_sysregs_context_restore(SECURE); 246 cm_set_next_eret_context(SECURE); 247 SMC_RET0(&tlk_ctx.cpu_ctx); 248 249 /* 250 * This is a request from the SP to mark completion of 251 * a standard function ID. 252 */ 253 case TLK_REQUEST_DONE: 254 if (ns || !tlk_args_results_buf) 255 SMC_RET1(handle, SMC_UNK); 256 257 /* 258 * Mark the SP state as inactive. 259 */ 260 clr_std_smc_active_flag(tlk_ctx.state); 261 262 /* Get a reference to the non-secure context */ 263 ns_cpu_context = cm_get_context(NON_SECURE); 264 assert(ns_cpu_context); 265 266 /* 267 * This is a request completion SMC and we must switch to 268 * the non-secure world to pass the result. 269 */ 270 cm_el1_sysregs_context_save(SECURE); 271 272 /* 273 * We are done stashing the secure context. Switch to the 274 * non-secure context and return the result. 275 */ 276 cm_el1_sysregs_context_restore(NON_SECURE); 277 cm_set_next_eret_context(NON_SECURE); 278 SMC_RET1(ns_cpu_context, tlk_args_results_buf->args[0]); 279 280 /* 281 * This function ID is used only by the SP to indicate it has 282 * finished initialising itself after a cold boot 283 */ 284 case TLK_ENTRY_DONE: 285 if (ns || !tlk_args_results_buf) 286 SMC_RET1(handle, SMC_UNK); 287 288 /* 289 * SP has been successfully initialized. Register power 290 * managemnt hooks with PSCI 291 */ 292 psci_register_spd_pm_hook(&tlkd_pm_ops); 293 294 /* 295 * TLK reports completion. The SPD must have initiated 296 * the original request through a synchronous entry 297 * into the SP. Jump back to the original C runtime 298 * context. 299 */ 300 tlkd_synchronous_sp_exit(&tlk_ctx, tlk_args_results_buf->args[0]); 301 302 /* 303 * This is a request from the secure payload to register 304 * shared memory to pass SMC args/results between EL1, EL3. 305 */ 306 case TLK_FID_SHARED_MEMBUF: 307 if (ns || !x1) 308 SMC_RET1(handle, SMC_UNK); 309 310 /* 311 * TODO: Check if the passed memory pointer is valid. Might 312 * require a call into the platform code. 313 */ 314 315 tlk_args_results_buf = (tlk_args_results_t *)x1; 316 SMC_RET0(handle); 317 318 /* 319 * Return the number of service function IDs implemented to 320 * provide service to non-secure 321 */ 322 case TOS_CALL_COUNT: 323 SMC_RET1(handle, TLK_NUM_FID); 324 325 /* 326 * Return TLK's UID to the caller 327 */ 328 case TOS_UID: 329 SMC_UUID_RET(handle, tlk_uuid); 330 331 /* 332 * Return the version of current implementation 333 */ 334 case TOS_CALL_VERSION: 335 SMC_RET2(handle, TLK_VERSION_MAJOR, TLK_VERSION_MINOR); 336 337 default: 338 break; 339 } 340 341 SMC_RET1(handle, SMC_UNK); 342 } 343 344 /* Define a SPD runtime service descriptor for fast SMC calls */ 345 DECLARE_RT_SVC( 346 tlkd_tos_fast, 347 348 OEN_TOS_START, 349 OEN_TOS_END, 350 SMC_TYPE_FAST, 351 tlkd_setup, 352 tlkd_smc_handler 353 ); 354 355 /* Define a SPD runtime service descriptor for standard SMC calls */ 356 DECLARE_RT_SVC( 357 tlkd_tos_std, 358 359 OEN_TOS_START, 360 OEN_TOS_END, 361 SMC_TYPE_STD, 362 NULL, 363 tlkd_smc_handler 364 ); 365