1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2016, Linaro Limited 4 * Copyright (c) 2014, STMicroelectronics International N.V. 5 */ 6 7 #include <platform_config.h> 8 9 #include <arm.h> 10 #include <assert.h> 11 #include <config.h> 12 #include <io.h> 13 #include <keep.h> 14 #include <kernel/asan.h> 15 #include <kernel/boot.h> 16 #include <kernel/linker.h> 17 #include <kernel/lockdep.h> 18 #include <kernel/misc.h> 19 #include <kernel/panic.h> 20 #include <kernel/spinlock.h> 21 #include <kernel/tee_ta_manager.h> 22 #include <kernel/thread_defs.h> 23 #include <kernel/thread.h> 24 #include <kernel/virtualization.h> 25 #include <mm/core_memprot.h> 26 #include <mm/mobj.h> 27 #include <mm/tee_mm.h> 28 #include <mm/tee_mmu.h> 29 #include <mm/tee_pager.h> 30 #include <smccc.h> 31 #include <sm/sm.h> 32 #include <trace.h> 33 #include <util.h> 34 35 #include "thread_private.h" 36 37 struct thread_ctx threads[CFG_NUM_THREADS]; 38 39 struct thread_core_local thread_core_local[CFG_TEE_CORE_NB_CORE] __nex_bss; 40 41 /* 42 * Stacks 43 * 44 * [Lower addresses on the left] 45 * 46 * [ STACK_CANARY_SIZE/2 | STACK_CHECK_EXTRA | STACK_XXX_SIZE | STACK_CANARY_SIZE/2 ] 47 * ^ ^ ^ ^ 48 * stack_xxx[n] "hard" top "soft" top bottom 49 */ 50 51 #ifdef CFG_WITH_ARM_TRUSTED_FW 52 #define STACK_TMP_OFFS 0 53 #else 54 #define STACK_TMP_OFFS SM_STACK_TMP_RESERVE_SIZE 55 #endif 56 57 #ifdef ARM32 58 #ifdef CFG_CORE_SANITIZE_KADDRESS 59 #define STACK_TMP_SIZE (3072 + STACK_TMP_OFFS) 60 #else 61 #define STACK_TMP_SIZE (2048 + STACK_TMP_OFFS) 62 #endif 63 #define STACK_THREAD_SIZE 8192 64 65 #if defined(CFG_CORE_SANITIZE_KADDRESS) || defined(__clang__) 66 #define STACK_ABT_SIZE 3072 67 #else 68 #define STACK_ABT_SIZE 2048 69 #endif 70 71 #endif /*ARM32*/ 72 73 #ifdef ARM64 74 #if defined(__clang__) && !defined(__OPTIMIZE_SIZE__) 75 #define STACK_TMP_SIZE (4096 + STACK_TMP_OFFS) 76 #else 77 #define STACK_TMP_SIZE (2048 + STACK_TMP_OFFS) 78 #endif 79 #define STACK_THREAD_SIZE 8192 80 81 #if TRACE_LEVEL > 0 82 #define STACK_ABT_SIZE 3072 83 #else 84 #define STACK_ABT_SIZE 1024 85 #endif 86 #endif /*ARM64*/ 87 88 #ifdef CFG_WITH_STACK_CANARIES 89 #ifdef ARM32 90 #define STACK_CANARY_SIZE (4 * sizeof(uint32_t)) 91 #endif 92 #ifdef ARM64 93 #define STACK_CANARY_SIZE (8 * sizeof(uint32_t)) 94 #endif 95 #define START_CANARY_VALUE 0xdededede 96 #define END_CANARY_VALUE 0xabababab 97 #define GET_START_CANARY(name, stack_num) name[stack_num][0] 98 #define GET_END_CANARY(name, stack_num) \ 99 name[stack_num][sizeof(name[stack_num]) / sizeof(uint32_t) - 1] 100 #else 101 #define STACK_CANARY_SIZE 0 102 #endif 103 104 #ifdef CFG_CORE_DEBUG_CHECK_STACKS 105 /* 106 * Extra space added to each stack in order to reliably detect and dump stack 107 * overflows. Should cover the maximum expected overflow size caused by any C 108 * function (say, 512 bytes; no function should have that much local variables), 109 * plus the maximum stack space needed by __cyg_profile_func_exit(): about 1 KB, 110 * a large part of which is used to print the call stack. Total: 1.5 KB. 111 */ 112 #define STACK_CHECK_EXTRA 1536 113 #else 114 #define STACK_CHECK_EXTRA 0 115 #endif 116 117 #define DECLARE_STACK(name, num_stacks, stack_size, linkage) \ 118 linkage uint32_t name[num_stacks] \ 119 [ROUNDUP(stack_size + STACK_CANARY_SIZE + STACK_CHECK_EXTRA, \ 120 STACK_ALIGNMENT) / sizeof(uint32_t)] \ 121 __attribute__((section(".nozi_stack." # name), \ 122 aligned(STACK_ALIGNMENT))) 123 124 #define GET_STACK(stack) ((vaddr_t)(stack) + STACK_SIZE(stack)) 125 126 DECLARE_STACK(stack_tmp, CFG_TEE_CORE_NB_CORE, 127 STACK_TMP_SIZE + CFG_STACK_TMP_EXTRA, static); 128 DECLARE_STACK(stack_abt, CFG_TEE_CORE_NB_CORE, STACK_ABT_SIZE, static); 129 #ifndef CFG_WITH_PAGER 130 DECLARE_STACK(stack_thread, CFG_NUM_THREADS, 131 STACK_THREAD_SIZE + CFG_STACK_THREAD_EXTRA, static); 132 #endif 133 134 #define GET_STACK_TOP_HARD(stack, n) \ 135 ((vaddr_t)&(stack)[n] + STACK_CANARY_SIZE / 2) 136 #define GET_STACK_TOP_SOFT(stack, n) \ 137 (GET_STACK_TOP_HARD(stack, n) + STACK_CHECK_EXTRA) 138 #define GET_STACK_BOTTOM(stack, n) ((vaddr_t)&(stack)[n] + sizeof(stack[n]) - \ 139 STACK_CANARY_SIZE / 2) 140 141 const void *stack_tmp_export __section(".identity_map.stack_tmp_export") = 142 (void *)(GET_STACK_BOTTOM(stack_tmp, 0) - STACK_TMP_OFFS); 143 const uint32_t stack_tmp_stride __section(".identity_map.stack_tmp_stride") = 144 sizeof(stack_tmp[0]); 145 146 /* 147 * These stack setup info are required by secondary boot cores before they 148 * each locally enable the pager (the mmu). Hence kept in pager sections. 149 */ 150 DECLARE_KEEP_PAGER(stack_tmp_export); 151 DECLARE_KEEP_PAGER(stack_tmp_stride); 152 153 #ifdef CFG_CORE_UNMAP_CORE_AT_EL0 154 static vaddr_t thread_user_kcode_va __nex_bss; 155 long thread_user_kcode_offset __nex_bss; 156 static size_t thread_user_kcode_size __nex_bss; 157 #endif 158 159 #if defined(CFG_CORE_UNMAP_CORE_AT_EL0) && \ 160 defined(CFG_CORE_WORKAROUND_SPECTRE_BP_SEC) && defined(ARM64) 161 long thread_user_kdata_sp_offset __nex_bss; 162 static uint8_t thread_user_kdata_page[ 163 ROUNDUP(sizeof(thread_core_local), SMALL_PAGE_SIZE)] 164 __aligned(SMALL_PAGE_SIZE) 165 #ifndef CFG_VIRTUALIZATION 166 __section(".nozi.kdata_page"); 167 #else 168 __section(".nex_nozi.kdata_page"); 169 #endif 170 #endif 171 172 static unsigned int thread_global_lock __nex_bss = SPINLOCK_UNLOCK; 173 174 static void init_canaries(void) 175 { 176 #ifdef CFG_WITH_STACK_CANARIES 177 size_t n; 178 #define INIT_CANARY(name) \ 179 for (n = 0; n < ARRAY_SIZE(name); n++) { \ 180 uint32_t *start_canary = &GET_START_CANARY(name, n); \ 181 uint32_t *end_canary = &GET_END_CANARY(name, n); \ 182 \ 183 *start_canary = START_CANARY_VALUE; \ 184 *end_canary = END_CANARY_VALUE; \ 185 } 186 187 INIT_CANARY(stack_tmp); 188 INIT_CANARY(stack_abt); 189 #if !defined(CFG_WITH_PAGER) && !defined(CFG_VIRTUALIZATION) 190 INIT_CANARY(stack_thread); 191 #endif 192 #endif/*CFG_WITH_STACK_CANARIES*/ 193 } 194 195 #define CANARY_DIED(stack, loc, n, addr) \ 196 do { \ 197 EMSG_RAW("Dead canary at %s of '%s[%zu]' (%p)", #loc, #stack, \ 198 n, (void *)addr); \ 199 panic(); \ 200 } while (0) 201 202 void thread_check_canaries(void) 203 { 204 #ifdef CFG_WITH_STACK_CANARIES 205 uint32_t *canary = NULL; 206 size_t n = 0; 207 208 for (n = 0; n < ARRAY_SIZE(stack_tmp); n++) { 209 canary = &GET_START_CANARY(stack_tmp, n); 210 if (*canary != START_CANARY_VALUE) 211 CANARY_DIED(stack_tmp, start, n, canary); 212 canary = &GET_END_CANARY(stack_tmp, n); 213 if (*canary != END_CANARY_VALUE) 214 CANARY_DIED(stack_tmp, end, n, canary); 215 } 216 217 for (n = 0; n < ARRAY_SIZE(stack_abt); n++) { 218 canary = &GET_START_CANARY(stack_abt, n); 219 if (*canary != START_CANARY_VALUE) 220 CANARY_DIED(stack_abt, start, n, canary); 221 canary = &GET_END_CANARY(stack_abt, n); 222 if (*canary != END_CANARY_VALUE) 223 CANARY_DIED(stack_abt, end, n, canary); 224 225 } 226 #if !defined(CFG_WITH_PAGER) && !defined(CFG_VIRTUALIZATION) 227 for (n = 0; n < ARRAY_SIZE(stack_thread); n++) { 228 canary = &GET_START_CANARY(stack_thread, n); 229 if (*canary != START_CANARY_VALUE) 230 CANARY_DIED(stack_thread, start, n, canary); 231 canary = &GET_END_CANARY(stack_thread, n); 232 if (*canary != END_CANARY_VALUE) 233 CANARY_DIED(stack_thread, end, n, canary); 234 } 235 #endif 236 #endif/*CFG_WITH_STACK_CANARIES*/ 237 } 238 239 void thread_lock_global(void) 240 { 241 cpu_spin_lock(&thread_global_lock); 242 } 243 244 void thread_unlock_global(void) 245 { 246 cpu_spin_unlock(&thread_global_lock); 247 } 248 249 #ifdef ARM32 250 uint32_t __nostackcheck thread_get_exceptions(void) 251 { 252 uint32_t cpsr = read_cpsr(); 253 254 return (cpsr >> CPSR_F_SHIFT) & THREAD_EXCP_ALL; 255 } 256 257 void __nostackcheck thread_set_exceptions(uint32_t exceptions) 258 { 259 uint32_t cpsr = read_cpsr(); 260 261 /* Foreign interrupts must not be unmasked while holding a spinlock */ 262 if (!(exceptions & THREAD_EXCP_FOREIGN_INTR)) 263 assert_have_no_spinlock(); 264 265 cpsr &= ~(THREAD_EXCP_ALL << CPSR_F_SHIFT); 266 cpsr |= ((exceptions & THREAD_EXCP_ALL) << CPSR_F_SHIFT); 267 268 barrier(); 269 write_cpsr(cpsr); 270 barrier(); 271 } 272 #endif /*ARM32*/ 273 274 #ifdef ARM64 275 uint32_t __nostackcheck thread_get_exceptions(void) 276 { 277 uint32_t daif = read_daif(); 278 279 return (daif >> DAIF_F_SHIFT) & THREAD_EXCP_ALL; 280 } 281 282 void __nostackcheck thread_set_exceptions(uint32_t exceptions) 283 { 284 uint32_t daif = read_daif(); 285 286 /* Foreign interrupts must not be unmasked while holding a spinlock */ 287 if (!(exceptions & THREAD_EXCP_FOREIGN_INTR)) 288 assert_have_no_spinlock(); 289 290 daif &= ~(THREAD_EXCP_ALL << DAIF_F_SHIFT); 291 daif |= ((exceptions & THREAD_EXCP_ALL) << DAIF_F_SHIFT); 292 293 barrier(); 294 write_daif(daif); 295 barrier(); 296 } 297 #endif /*ARM64*/ 298 299 uint32_t __nostackcheck thread_mask_exceptions(uint32_t exceptions) 300 { 301 uint32_t state = thread_get_exceptions(); 302 303 thread_set_exceptions(state | (exceptions & THREAD_EXCP_ALL)); 304 return state; 305 } 306 307 void __nostackcheck thread_unmask_exceptions(uint32_t state) 308 { 309 thread_set_exceptions(state & THREAD_EXCP_ALL); 310 } 311 312 313 static struct thread_core_local * __nostackcheck 314 get_core_local(unsigned int pos) 315 { 316 /* 317 * Foreign interrupts must be disabled before playing with core_local 318 * since we otherwise may be rescheduled to a different core in the 319 * middle of this function. 320 */ 321 assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR); 322 323 assert(pos < CFG_TEE_CORE_NB_CORE); 324 return &thread_core_local[pos]; 325 } 326 327 struct thread_core_local * __nostackcheck thread_get_core_local(void) 328 { 329 unsigned int pos = get_core_pos(); 330 331 return get_core_local(pos); 332 } 333 334 #ifdef CFG_CORE_DEBUG_CHECK_STACKS 335 static void print_stack_limits(void) 336 { 337 size_t n = 0; 338 vaddr_t __maybe_unused start = 0; 339 vaddr_t __maybe_unused end = 0; 340 341 for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) { 342 start = GET_STACK_TOP_SOFT(stack_tmp, n); 343 end = GET_STACK_BOTTOM(stack_tmp, n); 344 DMSG("tmp [%zu] 0x%" PRIxVA "..0x%" PRIxVA, n, start, end); 345 } 346 for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) { 347 start = GET_STACK_TOP_SOFT(stack_abt, n); 348 end = GET_STACK_BOTTOM(stack_abt, n); 349 DMSG("abt [%zu] 0x%" PRIxVA "..0x%" PRIxVA, n, start, end); 350 } 351 for (n = 0; n < CFG_NUM_THREADS; n++) { 352 end = threads[n].stack_va_end; 353 start = end - STACK_THREAD_SIZE; 354 DMSG("thr [%zu] 0x%" PRIxVA "..0x%" PRIxVA, n, start, end); 355 } 356 } 357 358 static void check_stack_limits(void) 359 { 360 vaddr_t stack_start = 0; 361 vaddr_t stack_end = 0; 362 /* Any value in the current stack frame will do */ 363 vaddr_t current_sp = (vaddr_t)&stack_start; 364 365 if (!get_stack_soft_limits(&stack_start, &stack_end)) 366 panic("Unknown stack limits"); 367 if (current_sp < stack_start || current_sp > stack_end) { 368 DMSG("Stack pointer out of range (0x%" PRIxVA ")", current_sp); 369 print_stack_limits(); 370 panic(); 371 } 372 } 373 374 static bool * __nostackcheck get_stackcheck_recursion_flag(void) 375 { 376 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 377 unsigned int pos = get_core_pos(); 378 struct thread_core_local *l = get_core_local(pos); 379 int ct = l->curr_thread; 380 bool *p = NULL; 381 382 if (l->flags & (THREAD_CLF_ABORT | THREAD_CLF_TMP)) 383 p = &l->stackcheck_recursion; 384 else if (!l->flags) 385 p = &threads[ct].tsd.stackcheck_recursion; 386 387 thread_unmask_exceptions(exceptions); 388 return p; 389 } 390 391 void __cyg_profile_func_enter(void *this_fn, void *call_site); 392 void __nostackcheck __cyg_profile_func_enter(void *this_fn __unused, 393 void *call_site __unused) 394 { 395 bool *p = get_stackcheck_recursion_flag(); 396 397 assert(p); 398 if (*p) 399 return; 400 *p = true; 401 check_stack_limits(); 402 *p = false; 403 } 404 405 void __cyg_profile_func_exit(void *this_fn, void *call_site); 406 void __nostackcheck __cyg_profile_func_exit(void *this_fn __unused, 407 void *call_site __unused) 408 { 409 } 410 #else 411 static void print_stack_limits(void) 412 { 413 } 414 #endif 415 416 static void thread_lazy_save_ns_vfp(void) 417 { 418 #ifdef CFG_WITH_VFP 419 struct thread_ctx *thr = threads + thread_get_id(); 420 421 thr->vfp_state.ns_saved = false; 422 vfp_lazy_save_state_init(&thr->vfp_state.ns); 423 #endif /*CFG_WITH_VFP*/ 424 } 425 426 static void thread_lazy_restore_ns_vfp(void) 427 { 428 #ifdef CFG_WITH_VFP 429 struct thread_ctx *thr = threads + thread_get_id(); 430 struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp; 431 432 assert(!thr->vfp_state.sec_lazy_saved && !thr->vfp_state.sec_saved); 433 434 if (tuv && tuv->lazy_saved && !tuv->saved) { 435 vfp_lazy_save_state_final(&tuv->vfp, false /*!force_save*/); 436 tuv->saved = true; 437 } 438 439 vfp_lazy_restore_state(&thr->vfp_state.ns, thr->vfp_state.ns_saved); 440 thr->vfp_state.ns_saved = false; 441 #endif /*CFG_WITH_VFP*/ 442 } 443 444 #ifdef ARM32 445 static void init_regs(struct thread_ctx *thread, uint32_t a0, uint32_t a1, 446 uint32_t a2, uint32_t a3) 447 { 448 thread->regs.pc = (uint32_t)thread_std_smc_entry; 449 450 /* 451 * Stdcalls starts in SVC mode with masked foreign interrupts, masked 452 * Asynchronous abort and unmasked native interrupts. 453 */ 454 thread->regs.cpsr = read_cpsr() & ARM32_CPSR_E; 455 thread->regs.cpsr |= CPSR_MODE_SVC | CPSR_A | 456 (THREAD_EXCP_FOREIGN_INTR << ARM32_CPSR_F_SHIFT); 457 /* Enable thumb mode if it's a thumb instruction */ 458 if (thread->regs.pc & 1) 459 thread->regs.cpsr |= CPSR_T; 460 /* Reinitialize stack pointer */ 461 thread->regs.svc_sp = thread->stack_va_end; 462 463 /* 464 * Copy arguments into context. This will make the 465 * arguments appear in r0-r7 when thread is started. 466 */ 467 thread->regs.r0 = a0; 468 thread->regs.r1 = a1; 469 thread->regs.r2 = a2; 470 thread->regs.r3 = a3; 471 thread->regs.r4 = 0; 472 thread->regs.r5 = 0; 473 thread->regs.r6 = 0; 474 thread->regs.r7 = 0; 475 } 476 #endif /*ARM32*/ 477 478 #ifdef ARM64 479 static void init_regs(struct thread_ctx *thread, uint32_t a0, uint32_t a1, 480 uint32_t a2, uint32_t a3) 481 { 482 thread->regs.pc = (uint64_t)thread_std_smc_entry; 483 484 /* 485 * Stdcalls starts in SVC mode with masked foreign interrupts, masked 486 * Asynchronous abort and unmasked native interrupts. 487 */ 488 thread->regs.cpsr = SPSR_64(SPSR_64_MODE_EL1, SPSR_64_MODE_SP_EL0, 489 THREAD_EXCP_FOREIGN_INTR | DAIFBIT_ABT); 490 /* Reinitialize stack pointer */ 491 thread->regs.sp = thread->stack_va_end; 492 493 /* 494 * Copy arguments into context. This will make the 495 * arguments appear in x0-x7 when thread is started. 496 */ 497 thread->regs.x[0] = a0; 498 thread->regs.x[1] = a1; 499 thread->regs.x[2] = a2; 500 thread->regs.x[3] = a3; 501 thread->regs.x[4] = 0; 502 thread->regs.x[5] = 0; 503 thread->regs.x[6] = 0; 504 thread->regs.x[7] = 0; 505 506 /* Set up frame pointer as per the Aarch64 AAPCS */ 507 thread->regs.x[29] = 0; 508 } 509 #endif /*ARM64*/ 510 511 void thread_init_boot_thread(void) 512 { 513 struct thread_core_local *l = thread_get_core_local(); 514 515 thread_init_threads(); 516 517 l->curr_thread = 0; 518 threads[0].state = THREAD_STATE_ACTIVE; 519 } 520 521 void __nostackcheck thread_clr_boot_thread(void) 522 { 523 struct thread_core_local *l = thread_get_core_local(); 524 525 assert(l->curr_thread >= 0 && l->curr_thread < CFG_NUM_THREADS); 526 assert(threads[l->curr_thread].state == THREAD_STATE_ACTIVE); 527 threads[l->curr_thread].state = THREAD_STATE_FREE; 528 l->curr_thread = -1; 529 } 530 531 void thread_alloc_and_run(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3) 532 { 533 size_t n; 534 struct thread_core_local *l = thread_get_core_local(); 535 bool found_thread = false; 536 537 assert(l->curr_thread == -1); 538 539 thread_lock_global(); 540 541 for (n = 0; n < CFG_NUM_THREADS; n++) { 542 if (threads[n].state == THREAD_STATE_FREE) { 543 threads[n].state = THREAD_STATE_ACTIVE; 544 found_thread = true; 545 break; 546 } 547 } 548 549 thread_unlock_global(); 550 551 if (!found_thread) 552 return; 553 554 l->curr_thread = n; 555 556 threads[n].flags = 0; 557 init_regs(threads + n, a0, a1, a2, a3); 558 559 thread_lazy_save_ns_vfp(); 560 561 l->flags &= ~THREAD_CLF_TMP; 562 thread_resume(&threads[n].regs); 563 /*NOTREACHED*/ 564 panic(); 565 } 566 567 #ifdef ARM32 568 static void copy_a0_to_a3(struct thread_ctx_regs *regs, uint32_t a0, 569 uint32_t a1, uint32_t a2, uint32_t a3) 570 { 571 /* 572 * Update returned values from RPC, values will appear in 573 * r0-r3 when thread is resumed. 574 */ 575 regs->r0 = a0; 576 regs->r1 = a1; 577 regs->r2 = a2; 578 regs->r3 = a3; 579 } 580 #endif /*ARM32*/ 581 582 #ifdef ARM64 583 static void copy_a0_to_a3(struct thread_ctx_regs *regs, uint32_t a0, 584 uint32_t a1, uint32_t a2, uint32_t a3) 585 { 586 /* 587 * Update returned values from RPC, values will appear in 588 * x0-x3 when thread is resumed. 589 */ 590 regs->x[0] = a0; 591 regs->x[1] = a1; 592 regs->x[2] = a2; 593 regs->x[3] = a3; 594 } 595 #endif /*ARM64*/ 596 597 #ifdef ARM32 598 static bool is_from_user(uint32_t cpsr) 599 { 600 return (cpsr & ARM32_CPSR_MODE_MASK) == ARM32_CPSR_MODE_USR; 601 } 602 #endif 603 604 #ifdef ARM64 605 static bool is_from_user(uint32_t cpsr) 606 { 607 if (cpsr & (SPSR_MODE_RW_32 << SPSR_MODE_RW_SHIFT)) 608 return true; 609 if (((cpsr >> SPSR_64_MODE_EL_SHIFT) & SPSR_64_MODE_EL_MASK) == 610 SPSR_64_MODE_EL0) 611 return true; 612 return false; 613 } 614 #endif 615 616 #ifdef CFG_SYSCALL_FTRACE 617 static void __noprof ftrace_suspend(void) 618 { 619 struct tee_ta_session *s = TAILQ_FIRST(&thread_get_tsd()->sess_stack); 620 621 if (!s) 622 return; 623 624 if (s->fbuf) 625 s->fbuf->syscall_trace_suspended = true; 626 } 627 628 static void __noprof ftrace_resume(void) 629 { 630 struct tee_ta_session *s = TAILQ_FIRST(&thread_get_tsd()->sess_stack); 631 632 if (!s) 633 return; 634 635 if (s->fbuf) 636 s->fbuf->syscall_trace_suspended = false; 637 } 638 #else 639 static void __noprof ftrace_suspend(void) 640 { 641 } 642 643 static void __noprof ftrace_resume(void) 644 { 645 } 646 #endif 647 648 static bool is_user_mode(struct thread_ctx_regs *regs) 649 { 650 return is_from_user((uint32_t)regs->cpsr); 651 } 652 653 void thread_resume_from_rpc(uint32_t thread_id, uint32_t a0, uint32_t a1, 654 uint32_t a2, uint32_t a3) 655 { 656 size_t n = thread_id; 657 struct thread_core_local *l = thread_get_core_local(); 658 bool found_thread = false; 659 660 assert(l->curr_thread == -1); 661 662 thread_lock_global(); 663 664 if (n < CFG_NUM_THREADS && threads[n].state == THREAD_STATE_SUSPENDED) { 665 threads[n].state = THREAD_STATE_ACTIVE; 666 found_thread = true; 667 } 668 669 thread_unlock_global(); 670 671 if (!found_thread) 672 return; 673 674 l->curr_thread = n; 675 676 if (threads[n].have_user_map) { 677 core_mmu_set_user_map(&threads[n].user_map); 678 if (threads[n].flags & THREAD_FLAGS_EXIT_ON_FOREIGN_INTR) 679 tee_ta_ftrace_update_times_resume(); 680 } 681 682 if (is_user_mode(&threads[n].regs)) 683 tee_ta_update_session_utime_resume(); 684 685 /* 686 * Return from RPC to request service of a foreign interrupt must not 687 * get parameters from non-secure world. 688 */ 689 if (threads[n].flags & THREAD_FLAGS_COPY_ARGS_ON_RETURN) { 690 copy_a0_to_a3(&threads[n].regs, a0, a1, a2, a3); 691 threads[n].flags &= ~THREAD_FLAGS_COPY_ARGS_ON_RETURN; 692 } 693 694 thread_lazy_save_ns_vfp(); 695 696 if (threads[n].have_user_map) 697 ftrace_resume(); 698 699 l->flags &= ~THREAD_CLF_TMP; 700 thread_resume(&threads[n].regs); 701 /*NOTREACHED*/ 702 panic(); 703 } 704 705 void __nostackcheck *thread_get_tmp_sp(void) 706 { 707 struct thread_core_local *l = thread_get_core_local(); 708 709 /* 710 * Called from assembly when switching to the temporary stack, so flags 711 * need updating 712 */ 713 l->flags |= THREAD_CLF_TMP; 714 715 return (void *)l->tmp_stack_va_end; 716 } 717 718 #ifdef ARM64 719 vaddr_t thread_get_saved_thread_sp(void) 720 { 721 struct thread_core_local *l = thread_get_core_local(); 722 int ct = l->curr_thread; 723 724 assert(ct != -1); 725 return threads[ct].kern_sp; 726 } 727 #endif /*ARM64*/ 728 729 vaddr_t thread_stack_start(void) 730 { 731 struct thread_ctx *thr; 732 int ct = thread_get_id_may_fail(); 733 734 if (ct == -1) 735 return 0; 736 737 thr = threads + ct; 738 return thr->stack_va_end - STACK_THREAD_SIZE; 739 } 740 741 size_t thread_stack_size(void) 742 { 743 return STACK_THREAD_SIZE; 744 } 745 746 bool get_stack_limits(vaddr_t *start, vaddr_t *end, bool hard) 747 { 748 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 749 unsigned int pos = get_core_pos(); 750 struct thread_core_local *l = get_core_local(pos); 751 int ct = l->curr_thread; 752 bool ret = false; 753 754 if (l->flags & THREAD_CLF_TMP) { 755 if (hard) 756 *start = GET_STACK_TOP_HARD(stack_tmp, pos); 757 else 758 *start = GET_STACK_TOP_SOFT(stack_tmp, pos); 759 *end = GET_STACK_BOTTOM(stack_tmp, pos); 760 ret = true; 761 } else if (l->flags & THREAD_CLF_ABORT) { 762 if (hard) 763 *start = GET_STACK_TOP_HARD(stack_abt, pos); 764 else 765 *start = GET_STACK_TOP_SOFT(stack_abt, pos); 766 *end = GET_STACK_BOTTOM(stack_abt, pos); 767 ret = true; 768 } else if (!l->flags) { 769 if (ct < 0 || ct >= CFG_NUM_THREADS) 770 goto out; 771 772 *end = threads[ct].stack_va_end; 773 *start = *end - STACK_THREAD_SIZE; 774 if (!hard) 775 *start += STACK_CHECK_EXTRA; 776 ret = true; 777 } 778 out: 779 thread_unmask_exceptions(exceptions); 780 return ret; 781 } 782 783 bool thread_is_from_abort_mode(void) 784 { 785 struct thread_core_local *l = thread_get_core_local(); 786 787 return (l->flags >> THREAD_CLF_SAVED_SHIFT) & THREAD_CLF_ABORT; 788 } 789 790 #ifdef ARM32 791 bool thread_is_in_normal_mode(void) 792 { 793 return (read_cpsr() & ARM32_CPSR_MODE_MASK) == ARM32_CPSR_MODE_SVC; 794 } 795 #endif 796 797 #ifdef ARM64 798 bool thread_is_in_normal_mode(void) 799 { 800 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 801 struct thread_core_local *l = thread_get_core_local(); 802 bool ret; 803 804 /* 805 * If any bit in l->flags is set aside from THREAD_CLF_TMP we're 806 * handling some exception. 807 */ 808 ret = (l->curr_thread != -1) && !(l->flags & ~THREAD_CLF_TMP); 809 thread_unmask_exceptions(exceptions); 810 811 return ret; 812 } 813 #endif 814 815 void thread_state_free(void) 816 { 817 struct thread_core_local *l = thread_get_core_local(); 818 int ct = l->curr_thread; 819 820 assert(ct != -1); 821 822 thread_lazy_restore_ns_vfp(); 823 tee_pager_release_phys( 824 (void *)(threads[ct].stack_va_end - STACK_THREAD_SIZE), 825 STACK_THREAD_SIZE); 826 827 thread_lock_global(); 828 829 assert(threads[ct].state == THREAD_STATE_ACTIVE); 830 threads[ct].state = THREAD_STATE_FREE; 831 threads[ct].flags = 0; 832 l->curr_thread = -1; 833 834 #ifdef CFG_VIRTUALIZATION 835 virt_unset_guest(); 836 #endif 837 thread_unlock_global(); 838 } 839 840 #ifdef CFG_WITH_PAGER 841 static void release_unused_kernel_stack(struct thread_ctx *thr, 842 uint32_t cpsr __maybe_unused) 843 { 844 #ifdef ARM64 845 /* 846 * If we're from user mode then thr->regs.sp is the saved user 847 * stack pointer and thr->kern_sp holds the last kernel stack 848 * pointer. But if we're from kernel mode then thr->kern_sp isn't 849 * up to date so we need to read from thr->regs.sp instead. 850 */ 851 vaddr_t sp = is_from_user(cpsr) ? thr->kern_sp : thr->regs.sp; 852 #else 853 vaddr_t sp = thr->regs.svc_sp; 854 #endif 855 vaddr_t base = thr->stack_va_end - STACK_THREAD_SIZE; 856 size_t len = sp - base; 857 858 tee_pager_release_phys((void *)base, len); 859 } 860 #else 861 static void release_unused_kernel_stack(struct thread_ctx *thr __unused, 862 uint32_t cpsr __unused) 863 { 864 } 865 #endif 866 867 int thread_state_suspend(uint32_t flags, uint32_t cpsr, vaddr_t pc) 868 { 869 struct thread_core_local *l = thread_get_core_local(); 870 int ct = l->curr_thread; 871 872 assert(ct != -1); 873 874 if (core_mmu_user_mapping_is_active()) 875 ftrace_suspend(); 876 877 thread_check_canaries(); 878 879 release_unused_kernel_stack(threads + ct, cpsr); 880 881 if (is_from_user(cpsr)) { 882 thread_user_save_vfp(); 883 tee_ta_update_session_utime_suspend(); 884 tee_ta_gprof_sample_pc(pc); 885 } 886 thread_lazy_restore_ns_vfp(); 887 888 thread_lock_global(); 889 890 assert(threads[ct].state == THREAD_STATE_ACTIVE); 891 threads[ct].flags |= flags; 892 threads[ct].regs.cpsr = cpsr; 893 threads[ct].regs.pc = pc; 894 threads[ct].state = THREAD_STATE_SUSPENDED; 895 896 threads[ct].have_user_map = core_mmu_user_mapping_is_active(); 897 if (threads[ct].have_user_map) { 898 if (threads[ct].flags & THREAD_FLAGS_EXIT_ON_FOREIGN_INTR) 899 tee_ta_ftrace_update_times_suspend(); 900 core_mmu_get_user_map(&threads[ct].user_map); 901 core_mmu_set_user_map(NULL); 902 } 903 904 l->curr_thread = -1; 905 906 #ifdef CFG_VIRTUALIZATION 907 virt_unset_guest(); 908 #endif 909 910 thread_unlock_global(); 911 912 return ct; 913 } 914 915 #ifdef ARM32 916 static void set_tmp_stack(struct thread_core_local *l, vaddr_t sp) 917 { 918 l->tmp_stack_va_end = sp; 919 thread_set_irq_sp(sp); 920 thread_set_fiq_sp(sp); 921 } 922 923 static void set_abt_stack(struct thread_core_local *l, vaddr_t sp) 924 { 925 l->abt_stack_va_end = sp; 926 thread_set_abt_sp((vaddr_t)l); 927 thread_set_und_sp((vaddr_t)l); 928 } 929 #endif /*ARM32*/ 930 931 #ifdef ARM64 932 static void set_tmp_stack(struct thread_core_local *l, vaddr_t sp) 933 { 934 /* 935 * We're already using the tmp stack when this function is called 936 * so there's no need to assign it to any stack pointer. However, 937 * we'll need to restore it at different times so store it here. 938 */ 939 l->tmp_stack_va_end = sp; 940 } 941 942 static void set_abt_stack(struct thread_core_local *l, vaddr_t sp) 943 { 944 l->abt_stack_va_end = sp; 945 } 946 #endif /*ARM64*/ 947 948 bool thread_init_stack(uint32_t thread_id, vaddr_t sp) 949 { 950 if (thread_id >= CFG_NUM_THREADS) 951 return false; 952 threads[thread_id].stack_va_end = sp; 953 return true; 954 } 955 956 short int thread_get_id_may_fail(void) 957 { 958 /* 959 * thread_get_core_local() requires foreign interrupts to be disabled 960 */ 961 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 962 struct thread_core_local *l = thread_get_core_local(); 963 short int ct = l->curr_thread; 964 965 thread_unmask_exceptions(exceptions); 966 return ct; 967 } 968 969 short int thread_get_id(void) 970 { 971 short int ct = thread_get_id_may_fail(); 972 973 /* Thread ID has to fit in a short int */ 974 COMPILE_TIME_ASSERT(CFG_NUM_THREADS <= SHRT_MAX); 975 assert(ct >= 0 && ct < CFG_NUM_THREADS); 976 return ct; 977 } 978 979 #ifdef CFG_WITH_PAGER 980 static void init_thread_stacks(void) 981 { 982 size_t n = 0; 983 984 /* 985 * Allocate virtual memory for thread stacks. 986 */ 987 for (n = 0; n < CFG_NUM_THREADS; n++) { 988 tee_mm_entry_t *mm = NULL; 989 vaddr_t sp = 0; 990 size_t num_pages = 0; 991 struct fobj *fobj = NULL; 992 993 /* Find vmem for thread stack and its protection gap */ 994 mm = tee_mm_alloc(&tee_mm_vcore, 995 SMALL_PAGE_SIZE + STACK_THREAD_SIZE); 996 assert(mm); 997 998 /* Claim eventual physical page */ 999 tee_pager_add_pages(tee_mm_get_smem(mm), tee_mm_get_size(mm), 1000 true); 1001 1002 num_pages = tee_mm_get_bytes(mm) / SMALL_PAGE_SIZE - 1; 1003 fobj = fobj_locked_paged_alloc(num_pages); 1004 1005 /* Add the area to the pager */ 1006 tee_pager_add_core_area(tee_mm_get_smem(mm) + SMALL_PAGE_SIZE, 1007 PAGER_AREA_TYPE_LOCK, fobj); 1008 fobj_put(fobj); 1009 1010 /* init effective stack */ 1011 sp = tee_mm_get_smem(mm) + tee_mm_get_bytes(mm); 1012 asan_tag_access((void *)tee_mm_get_smem(mm), (void *)sp); 1013 if (!thread_init_stack(n, sp)) 1014 panic("init stack failed"); 1015 } 1016 } 1017 #else 1018 static void init_thread_stacks(void) 1019 { 1020 size_t n; 1021 1022 /* Assign the thread stacks */ 1023 for (n = 0; n < CFG_NUM_THREADS; n++) { 1024 if (!thread_init_stack(n, GET_STACK_BOTTOM(stack_thread, n))) 1025 panic("thread_init_stack failed"); 1026 } 1027 } 1028 #endif /*CFG_WITH_PAGER*/ 1029 1030 static void init_user_kcode(void) 1031 { 1032 #ifdef CFG_CORE_UNMAP_CORE_AT_EL0 1033 vaddr_t v = (vaddr_t)thread_excp_vect; 1034 vaddr_t ve = (vaddr_t)thread_excp_vect_end; 1035 1036 thread_user_kcode_va = ROUNDDOWN(v, CORE_MMU_USER_CODE_SIZE); 1037 ve = ROUNDUP(ve, CORE_MMU_USER_CODE_SIZE); 1038 thread_user_kcode_size = ve - thread_user_kcode_va; 1039 1040 core_mmu_get_user_va_range(&v, NULL); 1041 thread_user_kcode_offset = thread_user_kcode_va - v; 1042 1043 #if defined(CFG_CORE_WORKAROUND_SPECTRE_BP_SEC) && defined(ARM64) 1044 /* 1045 * When transitioning to EL0 subtract SP with this much to point to 1046 * this special kdata page instead. SP is restored by add this much 1047 * while transitioning back to EL1. 1048 */ 1049 v += thread_user_kcode_size; 1050 thread_user_kdata_sp_offset = (vaddr_t)thread_core_local - v; 1051 #endif 1052 #endif /*CFG_CORE_UNMAP_CORE_AT_EL0*/ 1053 } 1054 1055 void thread_init_threads(void) 1056 { 1057 size_t n = 0; 1058 1059 init_thread_stacks(); 1060 print_stack_limits(); 1061 pgt_init(); 1062 1063 mutex_lockdep_init(); 1064 1065 for (n = 0; n < CFG_NUM_THREADS; n++) { 1066 TAILQ_INIT(&threads[n].tsd.sess_stack); 1067 SLIST_INIT(&threads[n].tsd.pgt_cache); 1068 } 1069 } 1070 1071 void __nostackcheck thread_init_thread_core_local(void) 1072 { 1073 size_t n = 0; 1074 struct thread_core_local *tcl = thread_core_local; 1075 1076 for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) { 1077 tcl[n].curr_thread = -1; 1078 tcl[n].flags = THREAD_CLF_TMP; 1079 } 1080 1081 tcl[0].tmp_stack_va_end = GET_STACK_BOTTOM(stack_tmp, 0); 1082 } 1083 1084 void thread_init_primary(void) 1085 { 1086 /* Initialize canaries around the stacks */ 1087 init_canaries(); 1088 1089 init_user_kcode(); 1090 } 1091 1092 static void init_sec_mon_stack(size_t pos __maybe_unused) 1093 { 1094 #if !defined(CFG_WITH_ARM_TRUSTED_FW) 1095 /* Initialize secure monitor */ 1096 sm_init(GET_STACK_BOTTOM(stack_tmp, pos)); 1097 #endif 1098 } 1099 1100 static uint32_t __maybe_unused get_midr_implementer(uint32_t midr) 1101 { 1102 return (midr >> MIDR_IMPLEMENTER_SHIFT) & MIDR_IMPLEMENTER_MASK; 1103 } 1104 1105 static uint32_t __maybe_unused get_midr_primary_part(uint32_t midr) 1106 { 1107 return (midr >> MIDR_PRIMARY_PART_NUM_SHIFT) & 1108 MIDR_PRIMARY_PART_NUM_MASK; 1109 } 1110 1111 #ifdef ARM64 1112 static bool probe_workaround_available(void) 1113 { 1114 int32_t r; 1115 1116 r = thread_smc(SMCCC_VERSION, 0, 0, 0); 1117 if (r < 0) 1118 return false; 1119 if (r < 0x10001) /* compare with version 1.1 */ 1120 return false; 1121 1122 /* Version >= 1.1, so SMCCC_ARCH_FEATURES is available */ 1123 r = thread_smc(SMCCC_ARCH_FEATURES, SMCCC_ARCH_WORKAROUND_1, 0, 0); 1124 return r >= 0; 1125 } 1126 1127 static vaddr_t __maybe_unused select_vector(vaddr_t a) 1128 { 1129 if (probe_workaround_available()) { 1130 DMSG("SMCCC_ARCH_WORKAROUND_1 (%#08" PRIx32 ") available", 1131 SMCCC_ARCH_WORKAROUND_1); 1132 DMSG("SMC Workaround for CVE-2017-5715 used"); 1133 return a; 1134 } 1135 1136 DMSG("SMCCC_ARCH_WORKAROUND_1 (%#08" PRIx32 ") unavailable", 1137 SMCCC_ARCH_WORKAROUND_1); 1138 DMSG("SMC Workaround for CVE-2017-5715 not needed (if ARM-TF is up to date)"); 1139 return (vaddr_t)thread_excp_vect; 1140 } 1141 #else 1142 static vaddr_t __maybe_unused select_vector(vaddr_t a) 1143 { 1144 return a; 1145 } 1146 #endif 1147 1148 static vaddr_t get_excp_vect(void) 1149 { 1150 #ifdef CFG_CORE_WORKAROUND_SPECTRE_BP_SEC 1151 uint32_t midr = read_midr(); 1152 1153 if (get_midr_implementer(midr) != MIDR_IMPLEMENTER_ARM) 1154 return (vaddr_t)thread_excp_vect; 1155 1156 switch (get_midr_primary_part(midr)) { 1157 #ifdef ARM32 1158 case CORTEX_A8_PART_NUM: 1159 case CORTEX_A9_PART_NUM: 1160 case CORTEX_A17_PART_NUM: 1161 #endif 1162 case CORTEX_A57_PART_NUM: 1163 case CORTEX_A72_PART_NUM: 1164 case CORTEX_A73_PART_NUM: 1165 case CORTEX_A75_PART_NUM: 1166 return select_vector((vaddr_t)thread_excp_vect_workaround); 1167 #ifdef ARM32 1168 case CORTEX_A15_PART_NUM: 1169 return select_vector((vaddr_t)thread_excp_vect_workaround_a15); 1170 #endif 1171 default: 1172 return (vaddr_t)thread_excp_vect; 1173 } 1174 #endif /*CFG_CORE_WORKAROUND_SPECTRE_BP_SEC*/ 1175 1176 return (vaddr_t)thread_excp_vect; 1177 } 1178 1179 void thread_init_per_cpu(void) 1180 { 1181 size_t pos = get_core_pos(); 1182 struct thread_core_local *l = thread_get_core_local(); 1183 1184 init_sec_mon_stack(pos); 1185 1186 set_tmp_stack(l, GET_STACK_BOTTOM(stack_tmp, pos) - STACK_TMP_OFFS); 1187 set_abt_stack(l, GET_STACK_BOTTOM(stack_abt, pos)); 1188 1189 thread_init_vbar(get_excp_vect()); 1190 1191 #ifdef CFG_FTRACE_SUPPORT 1192 /* 1193 * Enable accesses to frequency register and physical counter 1194 * register in EL0/PL0 required for timestamping during 1195 * function tracing. 1196 */ 1197 write_cntkctl(read_cntkctl() | CNTKCTL_PL0PCTEN); 1198 #endif 1199 } 1200 1201 struct thread_specific_data *thread_get_tsd(void) 1202 { 1203 return &threads[thread_get_id()].tsd; 1204 } 1205 1206 struct thread_ctx_regs * __nostackcheck thread_get_ctx_regs(void) 1207 { 1208 struct thread_core_local *l = thread_get_core_local(); 1209 1210 assert(l->curr_thread != -1); 1211 return &threads[l->curr_thread].regs; 1212 } 1213 1214 void thread_set_foreign_intr(bool enable) 1215 { 1216 /* thread_get_core_local() requires foreign interrupts to be disabled */ 1217 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 1218 struct thread_core_local *l; 1219 1220 l = thread_get_core_local(); 1221 1222 assert(l->curr_thread != -1); 1223 1224 if (enable) { 1225 threads[l->curr_thread].flags |= 1226 THREAD_FLAGS_FOREIGN_INTR_ENABLE; 1227 thread_set_exceptions(exceptions & ~THREAD_EXCP_FOREIGN_INTR); 1228 } else { 1229 /* 1230 * No need to disable foreign interrupts here since they're 1231 * already disabled above. 1232 */ 1233 threads[l->curr_thread].flags &= 1234 ~THREAD_FLAGS_FOREIGN_INTR_ENABLE; 1235 } 1236 } 1237 1238 void thread_restore_foreign_intr(void) 1239 { 1240 /* thread_get_core_local() requires foreign interrupts to be disabled */ 1241 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 1242 struct thread_core_local *l; 1243 1244 l = thread_get_core_local(); 1245 1246 assert(l->curr_thread != -1); 1247 1248 if (threads[l->curr_thread].flags & THREAD_FLAGS_FOREIGN_INTR_ENABLE) 1249 thread_set_exceptions(exceptions & ~THREAD_EXCP_FOREIGN_INTR); 1250 } 1251 1252 #ifdef CFG_WITH_VFP 1253 uint32_t thread_kernel_enable_vfp(void) 1254 { 1255 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 1256 struct thread_ctx *thr = threads + thread_get_id(); 1257 struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp; 1258 1259 assert(!vfp_is_enabled()); 1260 1261 if (!thr->vfp_state.ns_saved) { 1262 vfp_lazy_save_state_final(&thr->vfp_state.ns, 1263 true /*force_save*/); 1264 thr->vfp_state.ns_saved = true; 1265 } else if (thr->vfp_state.sec_lazy_saved && 1266 !thr->vfp_state.sec_saved) { 1267 /* 1268 * This happens when we're handling an abort while the 1269 * thread was using the VFP state. 1270 */ 1271 vfp_lazy_save_state_final(&thr->vfp_state.sec, 1272 false /*!force_save*/); 1273 thr->vfp_state.sec_saved = true; 1274 } else if (tuv && tuv->lazy_saved && !tuv->saved) { 1275 /* 1276 * This can happen either during syscall or abort 1277 * processing (while processing a syscall). 1278 */ 1279 vfp_lazy_save_state_final(&tuv->vfp, false /*!force_save*/); 1280 tuv->saved = true; 1281 } 1282 1283 vfp_enable(); 1284 return exceptions; 1285 } 1286 1287 void thread_kernel_disable_vfp(uint32_t state) 1288 { 1289 uint32_t exceptions; 1290 1291 assert(vfp_is_enabled()); 1292 1293 vfp_disable(); 1294 exceptions = thread_get_exceptions(); 1295 assert(exceptions & THREAD_EXCP_FOREIGN_INTR); 1296 exceptions &= ~THREAD_EXCP_FOREIGN_INTR; 1297 exceptions |= state & THREAD_EXCP_FOREIGN_INTR; 1298 thread_set_exceptions(exceptions); 1299 } 1300 1301 void thread_kernel_save_vfp(void) 1302 { 1303 struct thread_ctx *thr = threads + thread_get_id(); 1304 1305 assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR); 1306 if (vfp_is_enabled()) { 1307 vfp_lazy_save_state_init(&thr->vfp_state.sec); 1308 thr->vfp_state.sec_lazy_saved = true; 1309 } 1310 } 1311 1312 void thread_kernel_restore_vfp(void) 1313 { 1314 struct thread_ctx *thr = threads + thread_get_id(); 1315 1316 assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR); 1317 assert(!vfp_is_enabled()); 1318 if (thr->vfp_state.sec_lazy_saved) { 1319 vfp_lazy_restore_state(&thr->vfp_state.sec, 1320 thr->vfp_state.sec_saved); 1321 thr->vfp_state.sec_saved = false; 1322 thr->vfp_state.sec_lazy_saved = false; 1323 } 1324 } 1325 1326 void thread_user_enable_vfp(struct thread_user_vfp_state *uvfp) 1327 { 1328 struct thread_ctx *thr = threads + thread_get_id(); 1329 struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp; 1330 1331 assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR); 1332 assert(!vfp_is_enabled()); 1333 1334 if (!thr->vfp_state.ns_saved) { 1335 vfp_lazy_save_state_final(&thr->vfp_state.ns, 1336 true /*force_save*/); 1337 thr->vfp_state.ns_saved = true; 1338 } else if (tuv && uvfp != tuv) { 1339 if (tuv->lazy_saved && !tuv->saved) { 1340 vfp_lazy_save_state_final(&tuv->vfp, 1341 false /*!force_save*/); 1342 tuv->saved = true; 1343 } 1344 } 1345 1346 if (uvfp->lazy_saved) 1347 vfp_lazy_restore_state(&uvfp->vfp, uvfp->saved); 1348 uvfp->lazy_saved = false; 1349 uvfp->saved = false; 1350 1351 thr->vfp_state.uvfp = uvfp; 1352 vfp_enable(); 1353 } 1354 1355 void thread_user_save_vfp(void) 1356 { 1357 struct thread_ctx *thr = threads + thread_get_id(); 1358 struct thread_user_vfp_state *tuv = thr->vfp_state.uvfp; 1359 1360 assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR); 1361 if (!vfp_is_enabled()) 1362 return; 1363 1364 assert(tuv && !tuv->lazy_saved && !tuv->saved); 1365 vfp_lazy_save_state_init(&tuv->vfp); 1366 tuv->lazy_saved = true; 1367 } 1368 1369 void thread_user_clear_vfp(struct thread_user_vfp_state *uvfp) 1370 { 1371 struct thread_ctx *thr = threads + thread_get_id(); 1372 1373 if (uvfp == thr->vfp_state.uvfp) 1374 thr->vfp_state.uvfp = NULL; 1375 uvfp->lazy_saved = false; 1376 uvfp->saved = false; 1377 } 1378 #endif /*CFG_WITH_VFP*/ 1379 1380 #ifdef ARM32 1381 static bool get_spsr(bool is_32bit, unsigned long entry_func, uint32_t *spsr) 1382 { 1383 uint32_t s; 1384 1385 if (!is_32bit) 1386 return false; 1387 1388 s = read_cpsr(); 1389 s &= ~(CPSR_MODE_MASK | CPSR_T | CPSR_IT_MASK1 | CPSR_IT_MASK2); 1390 s |= CPSR_MODE_USR; 1391 if (entry_func & 1) 1392 s |= CPSR_T; 1393 *spsr = s; 1394 return true; 1395 } 1396 #endif 1397 1398 #ifdef ARM64 1399 static bool get_spsr(bool is_32bit, unsigned long entry_func, uint32_t *spsr) 1400 { 1401 uint32_t s; 1402 1403 if (is_32bit) { 1404 s = read_daif() & (SPSR_32_AIF_MASK << SPSR_32_AIF_SHIFT); 1405 s |= SPSR_MODE_RW_32 << SPSR_MODE_RW_SHIFT; 1406 s |= (entry_func & SPSR_32_T_MASK) << SPSR_32_T_SHIFT; 1407 } else { 1408 s = read_daif() & (SPSR_64_DAIF_MASK << SPSR_64_DAIF_SHIFT); 1409 } 1410 1411 *spsr = s; 1412 return true; 1413 } 1414 #endif 1415 1416 static void set_ctx_regs(struct thread_ctx_regs *regs, unsigned long a0, 1417 unsigned long a1, unsigned long a2, unsigned long a3, 1418 unsigned long user_sp, unsigned long entry_func, 1419 uint32_t spsr) 1420 { 1421 /* 1422 * First clear all registers to avoid leaking information from 1423 * other TAs or even the Core itself. 1424 */ 1425 *regs = (struct thread_ctx_regs){ }; 1426 #ifdef ARM32 1427 regs->r0 = a0; 1428 regs->r1 = a1; 1429 regs->r2 = a2; 1430 regs->r3 = a3; 1431 regs->usr_sp = user_sp; 1432 regs->pc = entry_func; 1433 regs->cpsr = spsr; 1434 #endif 1435 #ifdef ARM64 1436 regs->x[0] = a0; 1437 regs->x[1] = a1; 1438 regs->x[2] = a2; 1439 regs->x[3] = a3; 1440 regs->sp = user_sp; 1441 regs->pc = entry_func; 1442 regs->cpsr = spsr; 1443 regs->x[13] = user_sp; /* Used when running TA in Aarch32 */ 1444 regs->sp = user_sp; /* Used when running TA in Aarch64 */ 1445 /* Set frame pointer (user stack can't be unwound past this point) */ 1446 regs->x[29] = 0; 1447 #endif 1448 } 1449 1450 uint32_t thread_enter_user_mode(unsigned long a0, unsigned long a1, 1451 unsigned long a2, unsigned long a3, unsigned long user_sp, 1452 unsigned long entry_func, bool is_32bit, 1453 uint32_t *exit_status0, uint32_t *exit_status1) 1454 { 1455 uint32_t spsr = 0; 1456 uint32_t exceptions = 0; 1457 uint32_t rc = 0; 1458 struct thread_ctx_regs *regs = NULL; 1459 1460 tee_ta_update_session_utime_resume(); 1461 1462 /* Derive SPSR from current CPSR/PSTATE readout. */ 1463 if (!get_spsr(is_32bit, entry_func, &spsr)) { 1464 *exit_status0 = 1; /* panic */ 1465 *exit_status1 = 0xbadbadba; 1466 return 0; 1467 } 1468 1469 exceptions = thread_mask_exceptions(THREAD_EXCP_ALL); 1470 /* 1471 * We're using the per thread location of saved context registers 1472 * for temporary storage. Now that exceptions are masked they will 1473 * not be used for any thing else until they are eventually 1474 * unmasked when user mode has been entered. 1475 */ 1476 regs = thread_get_ctx_regs(); 1477 set_ctx_regs(regs, a0, a1, a2, a3, user_sp, entry_func, spsr); 1478 rc = __thread_enter_user_mode(regs, exit_status0, exit_status1); 1479 thread_unmask_exceptions(exceptions); 1480 return rc; 1481 } 1482 1483 #ifdef CFG_CORE_UNMAP_CORE_AT_EL0 1484 void thread_get_user_kcode(struct mobj **mobj, size_t *offset, 1485 vaddr_t *va, size_t *sz) 1486 { 1487 core_mmu_get_user_va_range(va, NULL); 1488 *mobj = mobj_tee_ram; 1489 *offset = thread_user_kcode_va - VCORE_START_VA; 1490 *sz = thread_user_kcode_size; 1491 } 1492 #endif 1493 1494 #if defined(CFG_CORE_UNMAP_CORE_AT_EL0) && \ 1495 defined(CFG_CORE_WORKAROUND_SPECTRE_BP_SEC) && defined(ARM64) 1496 void thread_get_user_kdata(struct mobj **mobj, size_t *offset, 1497 vaddr_t *va, size_t *sz) 1498 { 1499 vaddr_t v; 1500 1501 core_mmu_get_user_va_range(&v, NULL); 1502 *va = v + thread_user_kcode_size; 1503 *mobj = mobj_tee_ram; 1504 *offset = (vaddr_t)thread_user_kdata_page - VCORE_START_VA; 1505 *sz = sizeof(thread_user_kdata_page); 1506 } 1507 #endif 1508 1509 static void setup_unwind_user_mode(struct thread_svc_regs *regs) 1510 { 1511 #ifdef ARM32 1512 regs->lr = (uintptr_t)thread_unwind_user_mode; 1513 regs->spsr = read_cpsr(); 1514 #endif 1515 #ifdef ARM64 1516 regs->elr = (uintptr_t)thread_unwind_user_mode; 1517 regs->spsr = SPSR_64(SPSR_64_MODE_EL1, SPSR_64_MODE_SP_EL0, 0); 1518 regs->spsr |= read_daif(); 1519 /* 1520 * Regs is the value of stack pointer before calling the SVC 1521 * handler. By the addition matches for the reserved space at the 1522 * beginning of el0_sync_svc(). This prepares the stack when 1523 * returning to thread_unwind_user_mode instead of a normal 1524 * exception return. 1525 */ 1526 regs->sp_el0 = (uint64_t)(regs + 1); 1527 #endif 1528 } 1529 1530 /* 1531 * Note: this function is weak just to make it possible to exclude it from 1532 * the unpaged area. 1533 */ 1534 void __weak thread_svc_handler(struct thread_svc_regs *regs) 1535 { 1536 struct tee_ta_session *sess = NULL; 1537 uint32_t state = 0; 1538 1539 /* Enable native interrupts */ 1540 state = thread_get_exceptions(); 1541 thread_unmask_exceptions(state & ~THREAD_EXCP_NATIVE_INTR); 1542 1543 thread_user_save_vfp(); 1544 1545 /* TA has just entered kernel mode */ 1546 tee_ta_update_session_utime_suspend(); 1547 1548 /* Restore foreign interrupts which are disabled on exception entry */ 1549 thread_restore_foreign_intr(); 1550 1551 tee_ta_get_current_session(&sess); 1552 assert(sess && sess->ctx->ops && sess->ctx->ops->handle_svc); 1553 if (sess->ctx->ops->handle_svc(regs)) { 1554 /* We're about to switch back to user mode */ 1555 tee_ta_update_session_utime_resume(); 1556 } else { 1557 /* We're returning from __thread_enter_user_mode() */ 1558 setup_unwind_user_mode(regs); 1559 } 1560 } 1561 1562 static struct mobj *alloc_shm(enum thread_shm_type shm_type, size_t size) 1563 { 1564 switch (shm_type) { 1565 case THREAD_SHM_TYPE_APPLICATION: 1566 return thread_rpc_alloc_payload(size); 1567 case THREAD_SHM_TYPE_KERNEL_PRIVATE: 1568 return thread_rpc_alloc_kernel_payload(size); 1569 case THREAD_SHM_TYPE_GLOBAL: 1570 return thread_rpc_alloc_global_payload(size); 1571 default: 1572 return NULL; 1573 } 1574 } 1575 1576 static void clear_shm_cache_entry(struct thread_shm_cache_entry *ce) 1577 { 1578 if (ce->mobj) { 1579 switch (ce->type) { 1580 case THREAD_SHM_TYPE_APPLICATION: 1581 thread_rpc_free_payload(ce->mobj); 1582 break; 1583 case THREAD_SHM_TYPE_KERNEL_PRIVATE: 1584 thread_rpc_free_kernel_payload(ce->mobj); 1585 break; 1586 case THREAD_SHM_TYPE_GLOBAL: 1587 thread_rpc_free_global_payload(ce->mobj); 1588 break; 1589 default: 1590 assert(0); /* "can't happen" */ 1591 break; 1592 } 1593 } 1594 ce->mobj = NULL; 1595 ce->size = 0; 1596 } 1597 1598 static struct thread_shm_cache_entry * 1599 get_shm_cache_entry(enum thread_shm_cache_user user) 1600 { 1601 struct thread_shm_cache *cache = &threads[thread_get_id()].shm_cache; 1602 struct thread_shm_cache_entry *ce = NULL; 1603 1604 SLIST_FOREACH(ce, cache, link) 1605 if (ce->user == user) 1606 return ce; 1607 1608 ce = calloc(1, sizeof(*ce)); 1609 if (ce) { 1610 ce->user = user; 1611 SLIST_INSERT_HEAD(cache, ce, link); 1612 } 1613 1614 return ce; 1615 } 1616 1617 void *thread_rpc_shm_cache_alloc(enum thread_shm_cache_user user, 1618 enum thread_shm_type shm_type, 1619 size_t size, struct mobj **mobj) 1620 { 1621 struct thread_shm_cache_entry *ce = NULL; 1622 size_t sz = size; 1623 paddr_t p = 0; 1624 void *va = NULL; 1625 1626 if (!size) 1627 return NULL; 1628 1629 ce = get_shm_cache_entry(user); 1630 if (!ce) 1631 return NULL; 1632 1633 /* 1634 * Always allocate in page chunks as normal world allocates payload 1635 * memory as complete pages. 1636 */ 1637 sz = ROUNDUP(size, SMALL_PAGE_SIZE); 1638 1639 if (ce->type != shm_type || sz > ce->size) { 1640 clear_shm_cache_entry(ce); 1641 1642 ce->mobj = alloc_shm(shm_type, sz); 1643 if (!ce->mobj) 1644 return NULL; 1645 1646 if (mobj_get_pa(ce->mobj, 0, 0, &p)) 1647 goto err; 1648 1649 if (!ALIGNMENT_IS_OK(p, uint64_t)) 1650 goto err; 1651 1652 va = mobj_get_va(ce->mobj, 0); 1653 if (!va) 1654 goto err; 1655 1656 ce->size = sz; 1657 ce->type = shm_type; 1658 } else { 1659 va = mobj_get_va(ce->mobj, 0); 1660 if (!va) 1661 goto err; 1662 } 1663 *mobj = ce->mobj; 1664 1665 return va; 1666 err: 1667 clear_shm_cache_entry(ce); 1668 return NULL; 1669 } 1670 1671 void thread_rpc_shm_cache_clear(struct thread_shm_cache *cache) 1672 { 1673 while (true) { 1674 struct thread_shm_cache_entry *ce = SLIST_FIRST(cache); 1675 1676 if (!ce) 1677 break; 1678 SLIST_REMOVE_HEAD(cache, link); 1679 clear_shm_cache_entry(ce); 1680 free(ce); 1681 } 1682 } 1683 1684 #ifdef CFG_WITH_ARM_TRUSTED_FW 1685 /* 1686 * These five functions are __weak to allow platforms to override them if 1687 * needed. 1688 */ 1689 unsigned long __weak thread_cpu_off_handler(unsigned long a0 __unused, 1690 unsigned long a1 __unused) 1691 { 1692 return 0; 1693 } 1694 DECLARE_KEEP_PAGER(thread_cpu_off_handler); 1695 1696 unsigned long __weak thread_cpu_suspend_handler(unsigned long a0 __unused, 1697 unsigned long a1 __unused) 1698 { 1699 return 0; 1700 } 1701 DECLARE_KEEP_PAGER(thread_cpu_suspend_handler); 1702 1703 unsigned long __weak thread_cpu_resume_handler(unsigned long a0 __unused, 1704 unsigned long a1 __unused) 1705 { 1706 return 0; 1707 } 1708 DECLARE_KEEP_PAGER(thread_cpu_resume_handler); 1709 1710 unsigned long __weak thread_system_off_handler(unsigned long a0 __unused, 1711 unsigned long a1 __unused) 1712 { 1713 return 0; 1714 } 1715 DECLARE_KEEP_PAGER(thread_system_off_handler); 1716 1717 unsigned long __weak thread_system_reset_handler(unsigned long a0 __unused, 1718 unsigned long a1 __unused) 1719 { 1720 return 0; 1721 } 1722 DECLARE_KEEP_PAGER(thread_system_reset_handler); 1723 #endif /*CFG_WITH_ARM_TRUSTED_FW*/ 1724