1 /* 2 * Copyright (c) 2016, Linaro Limited 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 #include <platform_config.h> 29 #include <kernel/panic.h> 30 #include <kernel/thread.h> 31 #include <kernel/thread_defs.h> 32 #include "thread_private.h" 33 #include <sm/sm_defs.h> 34 #include <sm/sm.h> 35 #include <optee_msg.h> 36 #include <sm/optee_smc.h> 37 #include <arm.h> 38 #include <kernel/tz_proc_def.h> 39 #include <kernel/tz_proc.h> 40 #include <kernel/misc.h> 41 #include <mm/tee_mmu.h> 42 #include <mm/core_memprot.h> 43 #include <mm/tee_mmu_defs.h> 44 #include <mm/tee_mm.h> 45 #include <mm/tee_pager.h> 46 #include <kernel/tee_ta_manager.h> 47 #include <util.h> 48 #include <trace.h> 49 #include <assert.h> 50 51 #ifdef ARM32 52 #define STACK_TMP_SIZE 1024 53 #define STACK_THREAD_SIZE 8192 54 55 #if TRACE_LEVEL > 0 56 #define STACK_ABT_SIZE 2048 57 #else 58 #define STACK_ABT_SIZE 1024 59 #endif 60 61 #endif /*ARM32*/ 62 63 #ifdef ARM64 64 #define STACK_TMP_SIZE 2048 65 #define STACK_THREAD_SIZE 8192 66 67 #if TRACE_LEVEL > 0 68 #define STACK_ABT_SIZE 3072 69 #else 70 #define STACK_ABT_SIZE 1024 71 #endif 72 #endif /*ARM64*/ 73 74 #define RPC_MAX_NUM_PARAMS 2 75 76 struct thread_ctx threads[CFG_NUM_THREADS]; 77 78 static struct thread_core_local thread_core_local[CFG_TEE_CORE_NB_CORE]; 79 80 #ifdef CFG_WITH_STACK_CANARIES 81 #ifdef ARM32 82 #define STACK_CANARY_SIZE (4 * sizeof(uint32_t)) 83 #endif 84 #ifdef ARM64 85 #define STACK_CANARY_SIZE (8 * sizeof(uint32_t)) 86 #endif 87 #define START_CANARY_VALUE 0xdededede 88 #define END_CANARY_VALUE 0xabababab 89 #define GET_START_CANARY(name, stack_num) name[stack_num][0] 90 #define GET_END_CANARY(name, stack_num) \ 91 name[stack_num][sizeof(name[stack_num]) / sizeof(uint32_t) - 1] 92 #else 93 #define STACK_CANARY_SIZE 0 94 #endif 95 96 #define DECLARE_STACK(name, num_stacks, stack_size) \ 97 static uint32_t name[num_stacks][ \ 98 ROUNDUP(stack_size + STACK_CANARY_SIZE, STACK_ALIGNMENT) / \ 99 sizeof(uint32_t)] \ 100 __attribute__((section(".nozi.stack"), \ 101 aligned(STACK_ALIGNMENT))) 102 103 #define GET_STACK(stack) \ 104 ((vaddr_t)(stack) + sizeof(stack) - STACK_CANARY_SIZE / 2) 105 106 DECLARE_STACK(stack_tmp, CFG_TEE_CORE_NB_CORE, STACK_TMP_SIZE); 107 DECLARE_STACK(stack_abt, CFG_TEE_CORE_NB_CORE, STACK_ABT_SIZE); 108 #if !defined(CFG_WITH_ARM_TRUSTED_FW) 109 DECLARE_STACK(stack_sm, CFG_TEE_CORE_NB_CORE, SM_STACK_SIZE); 110 #endif 111 #ifndef CFG_WITH_PAGER 112 DECLARE_STACK(stack_thread, CFG_NUM_THREADS, STACK_THREAD_SIZE); 113 #endif 114 115 const vaddr_t stack_tmp_top[CFG_TEE_CORE_NB_CORE] = { 116 GET_STACK(stack_tmp[0]), 117 #if CFG_TEE_CORE_NB_CORE > 1 118 GET_STACK(stack_tmp[1]), 119 #endif 120 #if CFG_TEE_CORE_NB_CORE > 2 121 GET_STACK(stack_tmp[2]), 122 #endif 123 #if CFG_TEE_CORE_NB_CORE > 3 124 GET_STACK(stack_tmp[3]), 125 #endif 126 #if CFG_TEE_CORE_NB_CORE > 4 127 GET_STACK(stack_tmp[4]), 128 #endif 129 #if CFG_TEE_CORE_NB_CORE > 5 130 GET_STACK(stack_tmp[5]), 131 #endif 132 #if CFG_TEE_CORE_NB_CORE > 6 133 GET_STACK(stack_tmp[6]), 134 #endif 135 #if CFG_TEE_CORE_NB_CORE > 7 136 GET_STACK(stack_tmp[7]), 137 #endif 138 #if CFG_TEE_CORE_NB_CORE > 8 139 #error "Top of tmp stacks aren't defined for more than 8 CPUS" 140 #endif 141 }; 142 143 thread_smc_handler_t thread_std_smc_handler_ptr; 144 static thread_smc_handler_t thread_fast_smc_handler_ptr; 145 thread_fiq_handler_t thread_fiq_handler_ptr; 146 thread_pm_handler_t thread_cpu_on_handler_ptr; 147 thread_pm_handler_t thread_cpu_off_handler_ptr; 148 thread_pm_handler_t thread_cpu_suspend_handler_ptr; 149 thread_pm_handler_t thread_cpu_resume_handler_ptr; 150 thread_pm_handler_t thread_system_off_handler_ptr; 151 thread_pm_handler_t thread_system_reset_handler_ptr; 152 153 154 static unsigned int thread_global_lock = SPINLOCK_UNLOCK; 155 static bool thread_prealloc_rpc_cache; 156 157 static void init_canaries(void) 158 { 159 #ifdef CFG_WITH_STACK_CANARIES 160 size_t n; 161 #define INIT_CANARY(name) \ 162 for (n = 0; n < ARRAY_SIZE(name); n++) { \ 163 uint32_t *start_canary = &GET_START_CANARY(name, n); \ 164 uint32_t *end_canary = &GET_END_CANARY(name, n); \ 165 \ 166 *start_canary = START_CANARY_VALUE; \ 167 *end_canary = END_CANARY_VALUE; \ 168 DMSG("#Stack canaries for %s[%zu] with top at %p\n", \ 169 #name, n, (void *)(end_canary - 1)); \ 170 DMSG("watch *%p\n", (void *)end_canary); \ 171 } 172 173 INIT_CANARY(stack_tmp); 174 INIT_CANARY(stack_abt); 175 #if !defined(CFG_WITH_ARM_TRUSTED_FW) 176 INIT_CANARY(stack_sm); 177 #endif 178 #ifndef CFG_WITH_PAGER 179 INIT_CANARY(stack_thread); 180 #endif 181 #endif/*CFG_WITH_STACK_CANARIES*/ 182 } 183 184 void thread_check_canaries(void) 185 { 186 #ifdef CFG_WITH_STACK_CANARIES 187 size_t n; 188 189 for (n = 0; n < ARRAY_SIZE(stack_tmp); n++) { 190 assert(GET_START_CANARY(stack_tmp, n) == START_CANARY_VALUE); 191 assert(GET_END_CANARY(stack_tmp, n) == END_CANARY_VALUE); 192 } 193 194 for (n = 0; n < ARRAY_SIZE(stack_abt); n++) { 195 assert(GET_START_CANARY(stack_abt, n) == START_CANARY_VALUE); 196 assert(GET_END_CANARY(stack_abt, n) == END_CANARY_VALUE); 197 } 198 #if !defined(CFG_WITH_ARM_TRUSTED_FW) 199 for (n = 0; n < ARRAY_SIZE(stack_sm); n++) { 200 assert(GET_START_CANARY(stack_sm, n) == START_CANARY_VALUE); 201 assert(GET_END_CANARY(stack_sm, n) == END_CANARY_VALUE); 202 } 203 #endif 204 #ifndef CFG_WITH_PAGER 205 for (n = 0; n < ARRAY_SIZE(stack_thread); n++) { 206 assert(GET_START_CANARY(stack_thread, n) == START_CANARY_VALUE); 207 assert(GET_END_CANARY(stack_thread, n) == END_CANARY_VALUE); 208 } 209 #endif 210 #endif/*CFG_WITH_STACK_CANARIES*/ 211 } 212 213 static void lock_global(void) 214 { 215 cpu_spin_lock(&thread_global_lock); 216 } 217 218 static void unlock_global(void) 219 { 220 cpu_spin_unlock(&thread_global_lock); 221 } 222 223 #ifdef ARM32 224 uint32_t thread_get_exceptions(void) 225 { 226 uint32_t cpsr = read_cpsr(); 227 228 return (cpsr >> CPSR_F_SHIFT) & THREAD_EXCP_ALL; 229 } 230 231 void thread_set_exceptions(uint32_t exceptions) 232 { 233 uint32_t cpsr = read_cpsr(); 234 235 cpsr &= ~(THREAD_EXCP_ALL << CPSR_F_SHIFT); 236 cpsr |= ((exceptions & THREAD_EXCP_ALL) << CPSR_F_SHIFT); 237 write_cpsr(cpsr); 238 } 239 #endif /*ARM32*/ 240 241 #ifdef ARM64 242 uint32_t thread_get_exceptions(void) 243 { 244 uint32_t daif = read_daif(); 245 246 return (daif >> DAIF_F_SHIFT) & THREAD_EXCP_ALL; 247 } 248 249 void thread_set_exceptions(uint32_t exceptions) 250 { 251 uint32_t daif = read_daif(); 252 253 daif &= ~(THREAD_EXCP_ALL << DAIF_F_SHIFT); 254 daif |= ((exceptions & THREAD_EXCP_ALL) << DAIF_F_SHIFT); 255 write_daif(daif); 256 } 257 #endif /*ARM64*/ 258 259 uint32_t thread_mask_exceptions(uint32_t exceptions) 260 { 261 uint32_t state = thread_get_exceptions(); 262 263 thread_set_exceptions(state | (exceptions & THREAD_EXCP_ALL)); 264 return state; 265 } 266 267 void thread_unmask_exceptions(uint32_t state) 268 { 269 thread_set_exceptions(state & THREAD_EXCP_ALL); 270 } 271 272 273 struct thread_core_local *thread_get_core_local(void) 274 { 275 uint32_t cpu_id = get_core_pos(); 276 277 /* 278 * IRQs must be disabled before playing with core_local since 279 * we otherwise may be rescheduled to a different core in the 280 * middle of this function. 281 */ 282 assert(thread_get_exceptions() & THREAD_EXCP_IRQ); 283 284 assert(cpu_id < CFG_TEE_CORE_NB_CORE); 285 return &thread_core_local[cpu_id]; 286 } 287 288 static void thread_lazy_save_ns_vfp(void) 289 { 290 #ifdef CFG_WITH_VFP 291 struct thread_ctx *thr = threads + thread_get_id(); 292 293 thr->vfp_state.ns_saved = false; 294 #if defined(ARM64) && defined(CFG_WITH_ARM_TRUSTED_FW) 295 /* 296 * ARM TF saves and restores CPACR_EL1, so we must assume NS world 297 * uses VFP and always preserve the register file when secure world 298 * is about to use it 299 */ 300 thr->vfp_state.ns.force_save = true; 301 #endif 302 vfp_lazy_save_state_init(&thr->vfp_state.ns); 303 #endif /*CFG_WITH_VFP*/ 304 } 305 306 static void thread_lazy_restore_ns_vfp(void) 307 { 308 #ifdef CFG_WITH_VFP 309 struct thread_ctx *thr = threads + thread_get_id(); 310 struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp; 311 312 assert(!thr->vfp_state.sec_lazy_saved && !thr->vfp_state.sec_saved); 313 314 if (tuv && tuv->lazy_saved && !tuv->saved) { 315 vfp_lazy_save_state_final(&tuv->vfp); 316 tuv->saved = true; 317 } 318 319 vfp_lazy_restore_state(&thr->vfp_state.ns, thr->vfp_state.ns_saved); 320 thr->vfp_state.ns_saved = false; 321 #endif /*CFG_WITH_VFP*/ 322 } 323 324 #ifdef ARM32 325 static void init_regs(struct thread_ctx *thread, 326 struct thread_smc_args *args) 327 { 328 thread->regs.pc = (uint32_t)thread_std_smc_entry; 329 330 /* 331 * Stdcalls starts in SVC mode with masked IRQ, masked Asynchronous 332 * abort and unmasked FIQ. 333 */ 334 thread->regs.cpsr = read_cpsr() & ARM32_CPSR_E; 335 thread->regs.cpsr |= CPSR_MODE_SVC | CPSR_I | CPSR_A; 336 /* Enable thumb mode if it's a thumb instruction */ 337 if (thread->regs.pc & 1) 338 thread->regs.cpsr |= CPSR_T; 339 /* Reinitialize stack pointer */ 340 thread->regs.svc_sp = thread->stack_va_end; 341 342 /* 343 * Copy arguments into context. This will make the 344 * arguments appear in r0-r7 when thread is started. 345 */ 346 thread->regs.r0 = args->a0; 347 thread->regs.r1 = args->a1; 348 thread->regs.r2 = args->a2; 349 thread->regs.r3 = args->a3; 350 thread->regs.r4 = args->a4; 351 thread->regs.r5 = args->a5; 352 thread->regs.r6 = args->a6; 353 thread->regs.r7 = args->a7; 354 } 355 #endif /*ARM32*/ 356 357 #ifdef ARM64 358 static void init_regs(struct thread_ctx *thread, 359 struct thread_smc_args *args) 360 { 361 thread->regs.pc = (uint64_t)thread_std_smc_entry; 362 363 /* 364 * Stdcalls starts in SVC mode with masked IRQ, masked Asynchronous 365 * abort and unmasked FIQ. 366 */ 367 thread->regs.cpsr = SPSR_64(SPSR_64_MODE_EL1, SPSR_64_MODE_SP_EL0, 368 DAIFBIT_IRQ | DAIFBIT_ABT); 369 /* Reinitialize stack pointer */ 370 thread->regs.sp = thread->stack_va_end; 371 372 /* 373 * Copy arguments into context. This will make the 374 * arguments appear in x0-x7 when thread is started. 375 */ 376 thread->regs.x[0] = args->a0; 377 thread->regs.x[1] = args->a1; 378 thread->regs.x[2] = args->a2; 379 thread->regs.x[3] = args->a3; 380 thread->regs.x[4] = args->a4; 381 thread->regs.x[5] = args->a5; 382 thread->regs.x[6] = args->a6; 383 thread->regs.x[7] = args->a7; 384 } 385 #endif /*ARM64*/ 386 387 void thread_init_boot_thread(void) 388 { 389 struct thread_core_local *l = thread_get_core_local(); 390 size_t n; 391 392 for (n = 0; n < CFG_NUM_THREADS; n++) { 393 TAILQ_INIT(&threads[n].mutexes); 394 TAILQ_INIT(&threads[n].tsd.sess_stack); 395 #ifdef CFG_SMALL_PAGE_USER_TA 396 SLIST_INIT(&threads[n].tsd.pgt_cache); 397 #endif 398 } 399 400 for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) 401 thread_core_local[n].curr_thread = -1; 402 403 l->curr_thread = 0; 404 threads[0].state = THREAD_STATE_ACTIVE; 405 } 406 407 void thread_clr_boot_thread(void) 408 { 409 struct thread_core_local *l = thread_get_core_local(); 410 411 assert(l->curr_thread >= 0 && l->curr_thread < CFG_NUM_THREADS); 412 assert(threads[l->curr_thread].state == THREAD_STATE_ACTIVE); 413 assert(TAILQ_EMPTY(&threads[l->curr_thread].mutexes)); 414 threads[l->curr_thread].state = THREAD_STATE_FREE; 415 l->curr_thread = -1; 416 } 417 418 static void thread_alloc_and_run(struct thread_smc_args *args) 419 { 420 size_t n; 421 struct thread_core_local *l = thread_get_core_local(); 422 bool found_thread = false; 423 424 assert(l->curr_thread == -1); 425 426 lock_global(); 427 428 for (n = 0; n < CFG_NUM_THREADS; n++) { 429 if (threads[n].state == THREAD_STATE_FREE) { 430 threads[n].state = THREAD_STATE_ACTIVE; 431 found_thread = true; 432 break; 433 } 434 } 435 436 unlock_global(); 437 438 if (!found_thread) { 439 args->a0 = OPTEE_SMC_RETURN_ETHREAD_LIMIT; 440 return; 441 } 442 443 l->curr_thread = n; 444 445 threads[n].flags = 0; 446 init_regs(threads + n, args); 447 448 /* Save Hypervisor Client ID */ 449 threads[n].hyp_clnt_id = args->a7; 450 451 thread_lazy_save_ns_vfp(); 452 thread_resume(&threads[n].regs); 453 } 454 455 #ifdef ARM32 456 static void copy_a0_to_a5(struct thread_ctx_regs *regs, 457 struct thread_smc_args *args) 458 { 459 /* 460 * Update returned values from RPC, values will appear in 461 * r0-r3 when thread is resumed. 462 */ 463 regs->r0 = args->a0; 464 regs->r1 = args->a1; 465 regs->r2 = args->a2; 466 regs->r3 = args->a3; 467 regs->r4 = args->a4; 468 regs->r5 = args->a5; 469 } 470 #endif /*ARM32*/ 471 472 #ifdef ARM64 473 static void copy_a0_to_a5(struct thread_ctx_regs *regs, 474 struct thread_smc_args *args) 475 { 476 /* 477 * Update returned values from RPC, values will appear in 478 * x0-x3 when thread is resumed. 479 */ 480 regs->x[0] = args->a0; 481 regs->x[1] = args->a1; 482 regs->x[2] = args->a2; 483 regs->x[3] = args->a3; 484 regs->x[4] = args->a4; 485 regs->x[5] = args->a5; 486 } 487 #endif /*ARM64*/ 488 489 static void thread_resume_from_rpc(struct thread_smc_args *args) 490 { 491 size_t n = args->a3; /* thread id */ 492 struct thread_core_local *l = thread_get_core_local(); 493 uint32_t rv = 0; 494 495 assert(l->curr_thread == -1); 496 497 lock_global(); 498 499 if (n < CFG_NUM_THREADS && 500 threads[n].state == THREAD_STATE_SUSPENDED && 501 args->a7 == threads[n].hyp_clnt_id) 502 threads[n].state = THREAD_STATE_ACTIVE; 503 else 504 rv = OPTEE_SMC_RETURN_ERESUME; 505 506 unlock_global(); 507 508 if (rv) { 509 args->a0 = rv; 510 return; 511 } 512 513 l->curr_thread = n; 514 515 if (threads[n].have_user_map) 516 core_mmu_set_user_map(&threads[n].user_map); 517 518 /* 519 * Return from RPC to request service of an IRQ must not 520 * get parameters from non-secure world. 521 */ 522 if (threads[n].flags & THREAD_FLAGS_COPY_ARGS_ON_RETURN) { 523 copy_a0_to_a5(&threads[n].regs, args); 524 threads[n].flags &= ~THREAD_FLAGS_COPY_ARGS_ON_RETURN; 525 } 526 527 thread_lazy_save_ns_vfp(); 528 thread_resume(&threads[n].regs); 529 } 530 531 void thread_handle_fast_smc(struct thread_smc_args *args) 532 { 533 thread_check_canaries(); 534 thread_fast_smc_handler_ptr(args); 535 /* Fast handlers must not unmask any exceptions */ 536 assert(thread_get_exceptions() == THREAD_EXCP_ALL); 537 } 538 539 void thread_handle_std_smc(struct thread_smc_args *args) 540 { 541 thread_check_canaries(); 542 543 if (args->a0 == OPTEE_SMC_CALL_RETURN_FROM_RPC) 544 thread_resume_from_rpc(args); 545 else 546 thread_alloc_and_run(args); 547 } 548 549 /* Helper routine for the assembly function thread_std_smc_entry() */ 550 void __thread_std_smc_entry(struct thread_smc_args *args) 551 { 552 struct thread_ctx *thr = threads + thread_get_id(); 553 554 if (!thr->rpc_arg) { 555 paddr_t parg; 556 uint64_t carg; 557 void *arg; 558 559 thread_rpc_alloc_arg( 560 OPTEE_MSG_GET_ARG_SIZE(RPC_MAX_NUM_PARAMS), 561 &parg, &carg); 562 if (!parg || !ALIGNMENT_IS_OK(parg, struct optee_msg_arg) || 563 !(arg = phys_to_virt(parg, CORE_MEM_NSEC_SHM))) { 564 thread_rpc_free_arg(carg); 565 args->a0 = OPTEE_SMC_RETURN_ENOMEM; 566 return; 567 } 568 569 thr->rpc_arg = arg; 570 thr->rpc_carg = carg; 571 } 572 573 thread_std_smc_handler_ptr(args); 574 575 if (!thread_prealloc_rpc_cache) { 576 thread_rpc_free_arg(thr->rpc_carg); 577 thr->rpc_carg = 0; 578 thr->rpc_arg = 0; 579 } 580 } 581 582 void *thread_get_tmp_sp(void) 583 { 584 struct thread_core_local *l = thread_get_core_local(); 585 586 return (void *)l->tmp_stack_va_end; 587 } 588 589 #ifdef ARM64 590 vaddr_t thread_get_saved_thread_sp(void) 591 { 592 struct thread_core_local *l = thread_get_core_local(); 593 int ct = l->curr_thread; 594 595 assert(ct != -1); 596 return threads[ct].kern_sp; 597 } 598 #endif /*ARM64*/ 599 600 bool thread_addr_is_in_stack(vaddr_t va) 601 { 602 struct thread_ctx *thr = threads + thread_get_id(); 603 604 return va < thr->stack_va_end && 605 va >= (thr->stack_va_end - STACK_THREAD_SIZE); 606 } 607 608 void thread_state_free(void) 609 { 610 struct thread_core_local *l = thread_get_core_local(); 611 int ct = l->curr_thread; 612 613 assert(ct != -1); 614 assert(TAILQ_EMPTY(&threads[ct].mutexes)); 615 616 thread_lazy_restore_ns_vfp(); 617 618 lock_global(); 619 620 assert(threads[ct].state == THREAD_STATE_ACTIVE); 621 threads[ct].state = THREAD_STATE_FREE; 622 threads[ct].flags = 0; 623 l->curr_thread = -1; 624 625 unlock_global(); 626 } 627 628 #ifdef ARM32 629 static bool is_from_user(uint32_t cpsr) 630 { 631 return (cpsr & ARM32_CPSR_MODE_MASK) == ARM32_CPSR_MODE_USR; 632 } 633 #endif 634 635 #ifdef ARM64 636 static bool is_from_user(uint32_t cpsr) 637 { 638 if (cpsr & (SPSR_MODE_RW_32 << SPSR_MODE_RW_SHIFT)) 639 return true; 640 if (((cpsr >> SPSR_64_MODE_EL_SHIFT) & SPSR_64_MODE_EL_MASK) == 641 SPSR_64_MODE_EL0) 642 return true; 643 return false; 644 } 645 #endif 646 647 int thread_state_suspend(uint32_t flags, uint32_t cpsr, vaddr_t pc) 648 { 649 struct thread_core_local *l = thread_get_core_local(); 650 int ct = l->curr_thread; 651 652 assert(ct != -1); 653 654 thread_check_canaries(); 655 656 if (is_from_user(cpsr)) 657 thread_user_save_vfp(); 658 thread_lazy_restore_ns_vfp(); 659 660 lock_global(); 661 662 assert(threads[ct].state == THREAD_STATE_ACTIVE); 663 threads[ct].flags |= flags; 664 threads[ct].regs.cpsr = cpsr; 665 threads[ct].regs.pc = pc; 666 threads[ct].state = THREAD_STATE_SUSPENDED; 667 668 threads[ct].have_user_map = core_mmu_user_mapping_is_active(); 669 if (threads[ct].have_user_map) { 670 core_mmu_get_user_map(&threads[ct].user_map); 671 core_mmu_set_user_map(NULL); 672 } 673 674 675 l->curr_thread = -1; 676 677 unlock_global(); 678 679 return ct; 680 } 681 682 #ifdef ARM32 683 static void set_tmp_stack(struct thread_core_local *l, vaddr_t sp) 684 { 685 l->tmp_stack_va_end = sp; 686 thread_set_irq_sp(sp); 687 thread_set_fiq_sp(sp); 688 } 689 690 static void set_abt_stack(struct thread_core_local *l __unused, vaddr_t sp) 691 { 692 thread_set_abt_sp(sp); 693 } 694 #endif /*ARM32*/ 695 696 #ifdef ARM64 697 static void set_tmp_stack(struct thread_core_local *l, vaddr_t sp) 698 { 699 /* 700 * We're already using the tmp stack when this function is called 701 * so there's no need to assign it to any stack pointer. However, 702 * we'll need to restore it at different times so store it here. 703 */ 704 l->tmp_stack_va_end = sp; 705 } 706 707 static void set_abt_stack(struct thread_core_local *l, vaddr_t sp) 708 { 709 l->abt_stack_va_end = sp; 710 } 711 #endif /*ARM64*/ 712 713 bool thread_init_stack(uint32_t thread_id, vaddr_t sp) 714 { 715 if (thread_id >= CFG_NUM_THREADS) 716 return false; 717 threads[thread_id].stack_va_end = sp; 718 return true; 719 } 720 721 int thread_get_id_may_fail(void) 722 { 723 /* thread_get_core_local() requires IRQs to be disabled */ 724 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_IRQ); 725 struct thread_core_local *l = thread_get_core_local(); 726 int ct = l->curr_thread; 727 728 thread_unmask_exceptions(exceptions); 729 return ct; 730 } 731 732 int thread_get_id(void) 733 { 734 int ct = thread_get_id_may_fail(); 735 736 assert((ct >= 0) && (ct < CFG_NUM_THREADS)); 737 return ct; 738 } 739 740 static void init_handlers(const struct thread_handlers *handlers) 741 { 742 thread_std_smc_handler_ptr = handlers->std_smc; 743 thread_fast_smc_handler_ptr = handlers->fast_smc; 744 thread_fiq_handler_ptr = handlers->fiq; 745 thread_cpu_on_handler_ptr = handlers->cpu_on; 746 thread_cpu_off_handler_ptr = handlers->cpu_off; 747 thread_cpu_suspend_handler_ptr = handlers->cpu_suspend; 748 thread_cpu_resume_handler_ptr = handlers->cpu_resume; 749 thread_system_off_handler_ptr = handlers->system_off; 750 thread_system_reset_handler_ptr = handlers->system_reset; 751 } 752 753 754 #ifdef CFG_WITH_PAGER 755 static void init_thread_stacks(void) 756 { 757 size_t n; 758 759 /* 760 * Allocate virtual memory for thread stacks. 761 */ 762 for (n = 0; n < CFG_NUM_THREADS; n++) { 763 tee_mm_entry_t *mm; 764 vaddr_t sp; 765 766 /* Find vmem for thread stack and its protection gap */ 767 mm = tee_mm_alloc(&tee_mm_vcore, 768 SMALL_PAGE_SIZE + STACK_THREAD_SIZE); 769 TEE_ASSERT(mm); 770 771 /* Claim eventual physical page */ 772 tee_pager_add_pages(tee_mm_get_smem(mm), tee_mm_get_size(mm), 773 true); 774 775 /* Add the area to the pager */ 776 tee_pager_add_core_area(tee_mm_get_smem(mm) + SMALL_PAGE_SIZE, 777 tee_mm_get_bytes(mm) - SMALL_PAGE_SIZE, 778 TEE_PAGER_AREA_RW | TEE_PAGER_AREA_LOCK, 779 NULL, NULL); 780 781 /* init effective stack */ 782 sp = tee_mm_get_smem(mm) + tee_mm_get_bytes(mm); 783 if (!thread_init_stack(n, sp)) 784 panic(); 785 } 786 } 787 #else 788 static void init_thread_stacks(void) 789 { 790 size_t n; 791 792 /* Assign the thread stacks */ 793 for (n = 0; n < CFG_NUM_THREADS; n++) { 794 if (!thread_init_stack(n, GET_STACK(stack_thread[n]))) 795 panic(); 796 } 797 } 798 #endif /*CFG_WITH_PAGER*/ 799 800 void thread_init_primary(const struct thread_handlers *handlers) 801 { 802 init_handlers(handlers); 803 804 /* Initialize canaries around the stacks */ 805 init_canaries(); 806 807 init_thread_stacks(); 808 pgt_init(); 809 } 810 811 static void init_sec_mon(size_t pos __maybe_unused) 812 { 813 #if !defined(CFG_WITH_ARM_TRUSTED_FW) 814 /* Initialize secure monitor */ 815 sm_init(GET_STACK(stack_sm[pos])); 816 sm_set_entry_vector(thread_vector_table); 817 #endif 818 } 819 820 void thread_init_per_cpu(void) 821 { 822 size_t pos = get_core_pos(); 823 struct thread_core_local *l = thread_get_core_local(); 824 825 init_sec_mon(pos); 826 827 set_tmp_stack(l, GET_STACK(stack_tmp[pos])); 828 set_abt_stack(l, GET_STACK(stack_abt[pos])); 829 830 thread_init_vbar(); 831 } 832 833 struct thread_specific_data *thread_get_tsd(void) 834 { 835 return &threads[thread_get_id()].tsd; 836 } 837 838 struct thread_ctx_regs *thread_get_ctx_regs(void) 839 { 840 struct thread_core_local *l = thread_get_core_local(); 841 842 assert(l->curr_thread != -1); 843 return &threads[l->curr_thread].regs; 844 } 845 846 void thread_set_irq(bool enable) 847 { 848 /* thread_get_core_local() requires IRQs to be disabled */ 849 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_IRQ); 850 struct thread_core_local *l; 851 852 l = thread_get_core_local(); 853 854 assert(l->curr_thread != -1); 855 856 if (enable) { 857 threads[l->curr_thread].flags |= THREAD_FLAGS_IRQ_ENABLE; 858 thread_set_exceptions(exceptions & ~THREAD_EXCP_IRQ); 859 } else { 860 /* 861 * No need to disable IRQ here since it's already disabled 862 * above. 863 */ 864 threads[l->curr_thread].flags &= ~THREAD_FLAGS_IRQ_ENABLE; 865 } 866 } 867 868 void thread_restore_irq(void) 869 { 870 /* thread_get_core_local() requires IRQs to be disabled */ 871 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_IRQ); 872 struct thread_core_local *l; 873 874 l = thread_get_core_local(); 875 876 assert(l->curr_thread != -1); 877 878 if (threads[l->curr_thread].flags & THREAD_FLAGS_IRQ_ENABLE) 879 thread_set_exceptions(exceptions & ~THREAD_EXCP_IRQ); 880 } 881 882 #ifdef CFG_WITH_VFP 883 uint32_t thread_kernel_enable_vfp(void) 884 { 885 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_IRQ); 886 struct thread_ctx *thr = threads + thread_get_id(); 887 struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp; 888 889 assert(!vfp_is_enabled()); 890 891 if (!thr->vfp_state.ns_saved) { 892 vfp_lazy_save_state_final(&thr->vfp_state.ns); 893 thr->vfp_state.ns_saved = true; 894 } else if (thr->vfp_state.sec_lazy_saved && 895 !thr->vfp_state.sec_saved) { 896 /* 897 * This happens when we're handling an abort while the 898 * thread was using the VFP state. 899 */ 900 vfp_lazy_save_state_final(&thr->vfp_state.sec); 901 thr->vfp_state.sec_saved = true; 902 } else if (tuv && tuv->lazy_saved && !tuv->saved) { 903 /* 904 * This can happen either during syscall or abort 905 * processing (while processing a syscall). 906 */ 907 vfp_lazy_save_state_final(&tuv->vfp); 908 tuv->saved = true; 909 } 910 911 vfp_enable(); 912 return exceptions; 913 } 914 915 void thread_kernel_disable_vfp(uint32_t state) 916 { 917 uint32_t exceptions; 918 919 assert(vfp_is_enabled()); 920 921 vfp_disable(); 922 exceptions = thread_get_exceptions(); 923 assert(exceptions & THREAD_EXCP_IRQ); 924 exceptions &= ~THREAD_EXCP_IRQ; 925 exceptions |= state & THREAD_EXCP_IRQ; 926 thread_set_exceptions(exceptions); 927 } 928 929 void thread_kernel_save_vfp(void) 930 { 931 struct thread_ctx *thr = threads + thread_get_id(); 932 933 assert(thread_get_exceptions() & THREAD_EXCP_IRQ); 934 if (vfp_is_enabled()) { 935 vfp_lazy_save_state_init(&thr->vfp_state.sec); 936 thr->vfp_state.sec_lazy_saved = true; 937 } 938 } 939 940 void thread_kernel_restore_vfp(void) 941 { 942 struct thread_ctx *thr = threads + thread_get_id(); 943 944 assert(thread_get_exceptions() & THREAD_EXCP_IRQ); 945 assert(!vfp_is_enabled()); 946 if (thr->vfp_state.sec_lazy_saved) { 947 vfp_lazy_restore_state(&thr->vfp_state.sec, 948 thr->vfp_state.sec_saved); 949 thr->vfp_state.sec_saved = false; 950 thr->vfp_state.sec_lazy_saved = false; 951 } 952 } 953 954 void thread_user_enable_vfp(struct thread_user_vfp_state *uvfp) 955 { 956 struct thread_ctx *thr = threads + thread_get_id(); 957 struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp; 958 959 assert(thread_get_exceptions() & THREAD_EXCP_IRQ); 960 assert(!vfp_is_enabled()); 961 962 if (!thr->vfp_state.ns_saved) { 963 vfp_lazy_save_state_final(&thr->vfp_state.ns); 964 thr->vfp_state.ns_saved = true; 965 } else if (tuv && uvfp != tuv) { 966 if (tuv->lazy_saved && !tuv->saved) { 967 vfp_lazy_save_state_final(&tuv->vfp); 968 tuv->saved = true; 969 } 970 } 971 972 if (uvfp->lazy_saved) 973 vfp_lazy_restore_state(&uvfp->vfp, uvfp->saved); 974 uvfp->lazy_saved = false; 975 uvfp->saved = false; 976 977 thr->vfp_state.uvfp = uvfp; 978 vfp_enable(); 979 } 980 981 void thread_user_save_vfp(void) 982 { 983 struct thread_ctx *thr = threads + thread_get_id(); 984 struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp; 985 986 assert(thread_get_exceptions() & THREAD_EXCP_IRQ); 987 if (!vfp_is_enabled()) 988 return; 989 990 assert(tuv && !tuv->lazy_saved && !tuv->saved); 991 vfp_lazy_save_state_init(&tuv->vfp); 992 tuv->lazy_saved = true; 993 } 994 995 void thread_user_clear_vfp(struct thread_user_vfp_state *uvfp) 996 { 997 struct thread_ctx *thr = threads + thread_get_id(); 998 999 if (uvfp == thr->vfp_state.uvfp) 1000 thr->vfp_state.uvfp = NULL; 1001 uvfp->lazy_saved = false; 1002 uvfp->saved = false; 1003 } 1004 #endif /*CFG_WITH_VFP*/ 1005 1006 #ifdef ARM32 1007 static bool get_spsr(bool is_32bit, unsigned long entry_func, uint32_t *spsr) 1008 { 1009 uint32_t s; 1010 1011 if (!is_32bit) 1012 return false; 1013 1014 s = read_spsr(); 1015 s &= ~(CPSR_MODE_MASK | CPSR_T | CPSR_IT_MASK1 | CPSR_IT_MASK2); 1016 s |= CPSR_MODE_USR; 1017 if (entry_func & 1) 1018 s |= CPSR_T; 1019 *spsr = s; 1020 return true; 1021 } 1022 #endif 1023 1024 #ifdef ARM64 1025 static bool get_spsr(bool is_32bit, unsigned long entry_func, uint32_t *spsr) 1026 { 1027 uint32_t s; 1028 1029 if (is_32bit) { 1030 s = read_daif() & (SPSR_32_AIF_MASK << SPSR_32_AIF_SHIFT); 1031 s |= SPSR_MODE_RW_32 << SPSR_MODE_RW_SHIFT; 1032 s |= (entry_func & SPSR_32_T_MASK) << SPSR_32_T_SHIFT; 1033 } else { 1034 s = read_daif() & (SPSR_64_DAIF_MASK << SPSR_64_DAIF_SHIFT); 1035 } 1036 1037 *spsr = s; 1038 return true; 1039 } 1040 #endif 1041 1042 uint32_t thread_enter_user_mode(unsigned long a0, unsigned long a1, 1043 unsigned long a2, unsigned long a3, unsigned long user_sp, 1044 unsigned long entry_func, bool is_32bit, 1045 uint32_t *exit_status0, uint32_t *exit_status1) 1046 { 1047 uint32_t spsr; 1048 1049 if (!get_spsr(is_32bit, entry_func, &spsr)) { 1050 *exit_status0 = 1; /* panic */ 1051 *exit_status1 = 0xbadbadba; 1052 return 0; 1053 } 1054 return __thread_enter_user_mode(a0, a1, a2, a3, user_sp, entry_func, 1055 spsr, exit_status0, exit_status1); 1056 } 1057 1058 void thread_add_mutex(struct mutex *m) 1059 { 1060 struct thread_core_local *l = thread_get_core_local(); 1061 int ct = l->curr_thread; 1062 1063 assert(ct != -1 && threads[ct].state == THREAD_STATE_ACTIVE); 1064 assert(m->owner_id == -1); 1065 m->owner_id = ct; 1066 TAILQ_INSERT_TAIL(&threads[ct].mutexes, m, link); 1067 } 1068 1069 void thread_rem_mutex(struct mutex *m) 1070 { 1071 struct thread_core_local *l = thread_get_core_local(); 1072 int ct = l->curr_thread; 1073 1074 assert(ct != -1 && threads[ct].state == THREAD_STATE_ACTIVE); 1075 assert(m->owner_id == ct); 1076 m->owner_id = -1; 1077 TAILQ_REMOVE(&threads[ct].mutexes, m, link); 1078 } 1079 1080 bool thread_disable_prealloc_rpc_cache(uint64_t *cookie) 1081 { 1082 bool rv; 1083 size_t n; 1084 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_IRQ); 1085 1086 lock_global(); 1087 1088 for (n = 0; n < CFG_NUM_THREADS; n++) { 1089 if (threads[n].state != THREAD_STATE_FREE) { 1090 rv = false; 1091 goto out; 1092 } 1093 } 1094 1095 rv = true; 1096 for (n = 0; n < CFG_NUM_THREADS; n++) { 1097 if (threads[n].rpc_arg) { 1098 *cookie = threads[n].rpc_carg; 1099 threads[n].rpc_carg = 0; 1100 threads[n].rpc_arg = NULL; 1101 goto out; 1102 } 1103 } 1104 1105 *cookie = 0; 1106 thread_prealloc_rpc_cache = false; 1107 out: 1108 unlock_global(); 1109 thread_unmask_exceptions(exceptions); 1110 return rv; 1111 } 1112 1113 bool thread_enable_prealloc_rpc_cache(void) 1114 { 1115 bool rv; 1116 size_t n; 1117 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_IRQ); 1118 1119 lock_global(); 1120 1121 for (n = 0; n < CFG_NUM_THREADS; n++) { 1122 if (threads[n].state != THREAD_STATE_FREE) { 1123 rv = false; 1124 goto out; 1125 } 1126 } 1127 1128 rv = true; 1129 thread_prealloc_rpc_cache = true; 1130 out: 1131 unlock_global(); 1132 thread_unmask_exceptions(exceptions); 1133 return rv; 1134 } 1135 1136 static uint32_t rpc_cmd_nolock(uint32_t cmd, size_t num_params, 1137 struct optee_msg_param *params) 1138 { 1139 uint32_t rpc_args[THREAD_RPC_NUM_ARGS] = { OPTEE_SMC_RETURN_RPC_CMD }; 1140 struct thread_ctx *thr = threads + thread_get_id(); 1141 struct optee_msg_arg *arg = thr->rpc_arg; 1142 uint64_t carg = thr->rpc_carg; 1143 const size_t params_size = sizeof(struct optee_msg_param) * num_params; 1144 size_t n; 1145 1146 TEE_ASSERT(arg && carg && num_params <= RPC_MAX_NUM_PARAMS); 1147 1148 memset(arg, 0, OPTEE_MSG_GET_ARG_SIZE(RPC_MAX_NUM_PARAMS)); 1149 arg->cmd = cmd; 1150 arg->ret = TEE_ERROR_GENERIC; /* in case value isn't updated */ 1151 arg->num_params = num_params; 1152 memcpy(OPTEE_MSG_GET_PARAMS(arg), params, params_size); 1153 1154 reg_pair_from_64(carg, rpc_args + 1, rpc_args + 2); 1155 thread_rpc(rpc_args); 1156 for (n = 0; n < num_params; n++) { 1157 switch (params[n].attr & OPTEE_MSG_ATTR_TYPE_MASK) { 1158 case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT: 1159 case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT: 1160 case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT: 1161 case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT: 1162 case OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT: 1163 case OPTEE_MSG_ATTR_TYPE_TMEM_INOUT: 1164 memcpy(params + n, OPTEE_MSG_GET_PARAMS(arg) + n, 1165 sizeof(struct optee_msg_param)); 1166 break; 1167 default: 1168 break; 1169 } 1170 } 1171 return arg->ret; 1172 } 1173 1174 uint32_t thread_rpc_cmd(uint32_t cmd, size_t num_params, 1175 struct optee_msg_param *params) 1176 { 1177 uint32_t ret; 1178 1179 ret = rpc_cmd_nolock(cmd, num_params, params); 1180 1181 return ret; 1182 } 1183 1184 void thread_rpc_alloc_arg(size_t size, paddr_t *arg, uint64_t *cookie) 1185 { 1186 uint32_t rpc_args[THREAD_RPC_NUM_ARGS] = { 1187 OPTEE_SMC_RETURN_RPC_ALLOC, size}; 1188 1189 thread_rpc(rpc_args); 1190 *arg = reg_pair_to_64(rpc_args[1], rpc_args[2]); 1191 *cookie = reg_pair_to_64(rpc_args[4], rpc_args[5]); 1192 } 1193 1194 /** 1195 * Allocates shared memory buffer via RPC 1196 * 1197 * @size: size in bytes of shared memory buffer 1198 * @align: required alignment of buffer 1199 * @bt: buffer type OPTEE_MSG_RPC_SHM_TYPE_* 1200 * @payload: returned physical pointer to buffer, 0 if allocation 1201 * failed. 1202 * @cookie: returned cookie used when freeing the buffer 1203 */ 1204 static void thread_rpc_alloc(size_t size, size_t align, unsigned bt, 1205 paddr_t *payload, uint64_t *cookie) 1206 { 1207 uint32_t rpc_args[THREAD_RPC_NUM_ARGS] = { OPTEE_SMC_RETURN_RPC_CMD }; 1208 struct thread_ctx *thr = threads + thread_get_id(); 1209 struct optee_msg_arg *arg = thr->rpc_arg; 1210 uint64_t carg = thr->rpc_carg; 1211 struct optee_msg_param *params = OPTEE_MSG_GET_PARAMS(arg); 1212 1213 memset(arg, 0, OPTEE_MSG_GET_ARG_SIZE(1)); 1214 arg->cmd = OPTEE_MSG_RPC_CMD_SHM_ALLOC; 1215 arg->ret = TEE_ERROR_GENERIC; /* in case value isn't updated */ 1216 arg->num_params = 1; 1217 1218 params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; 1219 params[0].u.value.a = bt; 1220 params[0].u.value.b = size; 1221 params[0].u.value.c = align; 1222 1223 reg_pair_from_64(carg, rpc_args + 1, rpc_args + 2); 1224 thread_rpc(rpc_args); 1225 if (arg->ret != TEE_SUCCESS) 1226 goto fail; 1227 1228 if (arg->num_params != 1) 1229 goto fail; 1230 1231 if (params[0].attr != OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT) 1232 goto fail; 1233 1234 *payload = params[0].u.tmem.buf_ptr; 1235 *cookie = params[0].u.tmem.shm_ref; 1236 return; 1237 fail: 1238 *payload = 0; 1239 *cookie = 0; 1240 } 1241 1242 /** 1243 * Free physical memory previously allocated with thread_rpc_alloc() 1244 * 1245 * @cookie: cookie received when allocating the buffer 1246 * @bt: must be the same as supplied when allocating 1247 */ 1248 static void thread_rpc_free(unsigned bt, uint64_t cookie) 1249 { 1250 uint32_t rpc_args[THREAD_RPC_NUM_ARGS] = { OPTEE_SMC_RETURN_RPC_CMD }; 1251 struct thread_ctx *thr = threads + thread_get_id(); 1252 struct optee_msg_arg *arg = thr->rpc_arg; 1253 uint64_t carg = thr->rpc_carg; 1254 struct optee_msg_param *params = OPTEE_MSG_GET_PARAMS(arg); 1255 1256 memset(arg, 0, OPTEE_MSG_GET_ARG_SIZE(1)); 1257 arg->cmd = OPTEE_MSG_RPC_CMD_SHM_FREE; 1258 arg->ret = TEE_ERROR_GENERIC; /* in case value isn't updated */ 1259 arg->num_params = 1; 1260 1261 params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; 1262 params[0].u.value.a = bt; 1263 params[0].u.value.b = cookie; 1264 params[0].u.value.c = 0; 1265 1266 reg_pair_from_64(carg, rpc_args + 1, rpc_args + 2); 1267 thread_rpc(rpc_args); 1268 } 1269 1270 1271 void thread_rpc_free_arg(uint64_t cookie) 1272 { 1273 if (cookie) { 1274 uint32_t rpc_args[THREAD_RPC_NUM_ARGS] = { 1275 OPTEE_SMC_RETURN_RPC_FREE}; 1276 1277 reg_pair_from_64(cookie, rpc_args + 1, rpc_args + 2); 1278 thread_rpc(rpc_args); 1279 } 1280 } 1281 1282 void thread_rpc_alloc_payload(size_t size, paddr_t *payload, uint64_t *cookie) 1283 { 1284 thread_rpc_alloc(size, 8, OPTEE_MSG_RPC_SHM_TYPE_APPL, payload, cookie); 1285 } 1286 1287 void thread_rpc_free_payload(uint64_t cookie) 1288 { 1289 thread_rpc_free(OPTEE_MSG_RPC_SHM_TYPE_APPL, cookie); 1290 } 1291