1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2016-2021, Linaro Limited 4 * Copyright (c) 2014, STMicroelectronics International N.V. 5 * Copyright (c) 2020-2021, Arm Limited 6 */ 7 8 #include <config.h> 9 #include <kernel/asan.h> 10 #include <kernel/lockdep.h> 11 #include <kernel/misc.h> 12 #include <kernel/panic.h> 13 #include <kernel/spinlock.h> 14 #include <kernel/thread.h> 15 #include <kernel/thread_private.h> 16 #include <mm/mobj.h> 17 18 struct thread_ctx threads[CFG_NUM_THREADS]; 19 20 struct thread_core_local thread_core_local[CFG_TEE_CORE_NB_CORE] __nex_bss; 21 22 /* 23 * Stacks 24 * 25 * [Lower addresses on the left] 26 * 27 * [ STACK_CANARY_SIZE/2 | STACK_CHECK_EXTRA | STACK_XXX_SIZE | STACK_CANARY_SIZE/2 ] 28 * ^ ^ ^ ^ 29 * stack_xxx[n] "hard" top "soft" top bottom 30 */ 31 32 #ifdef CFG_WITH_STACK_CANARIES 33 #define START_CANARY_VALUE 0xdededede 34 #define END_CANARY_VALUE 0xabababab 35 #define GET_START_CANARY(name, stack_num) name[stack_num][0] 36 #define GET_END_CANARY(name, stack_num) \ 37 name[stack_num][sizeof(name[stack_num]) / sizeof(uint32_t) - 1] 38 #endif 39 40 #define DECLARE_STACK(name, num_stacks, stack_size, linkage) \ 41 linkage uint32_t name[num_stacks] \ 42 [ROUNDUP(stack_size + STACK_CANARY_SIZE + STACK_CHECK_EXTRA, \ 43 STACK_ALIGNMENT) / sizeof(uint32_t)] \ 44 __attribute__((section(".nozi_stack." # name), \ 45 aligned(STACK_ALIGNMENT))) 46 47 #define GET_STACK(stack) ((vaddr_t)(stack) + STACK_SIZE(stack)) 48 49 DECLARE_STACK(stack_tmp, CFG_TEE_CORE_NB_CORE, STACK_TMP_SIZE, 50 /* global linkage */); 51 DECLARE_STACK(stack_abt, CFG_TEE_CORE_NB_CORE, STACK_ABT_SIZE, static); 52 #ifndef CFG_WITH_PAGER 53 DECLARE_STACK(stack_thread, CFG_NUM_THREADS, STACK_THREAD_SIZE, static); 54 #endif 55 56 #define GET_STACK_TOP_HARD(stack, n) \ 57 ((vaddr_t)&(stack)[n] + STACK_CANARY_SIZE / 2) 58 #define GET_STACK_TOP_SOFT(stack, n) \ 59 (GET_STACK_TOP_HARD(stack, n) + STACK_CHECK_EXTRA) 60 #define GET_STACK_BOTTOM(stack, n) ((vaddr_t)&(stack)[n] + sizeof(stack[n]) - \ 61 STACK_CANARY_SIZE / 2) 62 63 const uint32_t stack_tmp_stride __section(".identity_map.stack_tmp_stride") = 64 sizeof(stack_tmp[0]); 65 66 /* 67 * This stack setup info is required by secondary boot cores before they 68 * each locally enable the pager (the mmu). Hence kept in pager sections. 69 */ 70 DECLARE_KEEP_PAGER(stack_tmp_stride); 71 72 static unsigned int thread_global_lock __nex_bss = SPINLOCK_UNLOCK; 73 74 void thread_init_canaries(void) 75 { 76 #ifdef CFG_WITH_STACK_CANARIES 77 size_t n; 78 #define INIT_CANARY(name) \ 79 for (n = 0; n < ARRAY_SIZE(name); n++) { \ 80 uint32_t *start_canary = &GET_START_CANARY(name, n); \ 81 uint32_t *end_canary = &GET_END_CANARY(name, n); \ 82 \ 83 *start_canary = START_CANARY_VALUE; \ 84 *end_canary = END_CANARY_VALUE; \ 85 } 86 87 INIT_CANARY(stack_tmp); 88 INIT_CANARY(stack_abt); 89 #if !defined(CFG_WITH_PAGER) && !defined(CFG_VIRTUALIZATION) 90 INIT_CANARY(stack_thread); 91 #endif 92 #endif/*CFG_WITH_STACK_CANARIES*/ 93 } 94 95 #define CANARY_DIED(stack, loc, n, addr) \ 96 do { \ 97 EMSG_RAW("Dead canary at %s of '%s[%zu]' (%p)", #loc, #stack, \ 98 n, (void *)addr); \ 99 panic(); \ 100 } while (0) 101 102 void thread_check_canaries(void) 103 { 104 #ifdef CFG_WITH_STACK_CANARIES 105 uint32_t *canary = NULL; 106 size_t n = 0; 107 108 for (n = 0; n < ARRAY_SIZE(stack_tmp); n++) { 109 canary = &GET_START_CANARY(stack_tmp, n); 110 if (*canary != START_CANARY_VALUE) 111 CANARY_DIED(stack_tmp, start, n, canary); 112 canary = &GET_END_CANARY(stack_tmp, n); 113 if (*canary != END_CANARY_VALUE) 114 CANARY_DIED(stack_tmp, end, n, canary); 115 } 116 117 for (n = 0; n < ARRAY_SIZE(stack_abt); n++) { 118 canary = &GET_START_CANARY(stack_abt, n); 119 if (*canary != START_CANARY_VALUE) 120 CANARY_DIED(stack_abt, start, n, canary); 121 canary = &GET_END_CANARY(stack_abt, n); 122 if (*canary != END_CANARY_VALUE) 123 CANARY_DIED(stack_abt, end, n, canary); 124 } 125 #if !defined(CFG_WITH_PAGER) && !defined(CFG_VIRTUALIZATION) 126 for (n = 0; n < ARRAY_SIZE(stack_thread); n++) { 127 canary = &GET_START_CANARY(stack_thread, n); 128 if (*canary != START_CANARY_VALUE) 129 CANARY_DIED(stack_thread, start, n, canary); 130 canary = &GET_END_CANARY(stack_thread, n); 131 if (*canary != END_CANARY_VALUE) 132 CANARY_DIED(stack_thread, end, n, canary); 133 } 134 #endif 135 #endif/*CFG_WITH_STACK_CANARIES*/ 136 } 137 138 void thread_lock_global(void) 139 { 140 cpu_spin_lock(&thread_global_lock); 141 } 142 143 void thread_unlock_global(void) 144 { 145 cpu_spin_unlock(&thread_global_lock); 146 } 147 148 static struct thread_core_local * __nostackcheck 149 get_core_local(unsigned int pos) 150 { 151 /* 152 * Foreign interrupts must be disabled before playing with core_local 153 * since we otherwise may be rescheduled to a different core in the 154 * middle of this function. 155 */ 156 assert(thread_get_exceptions() & THREAD_EXCP_FOREIGN_INTR); 157 158 assert(pos < CFG_TEE_CORE_NB_CORE); 159 return &thread_core_local[pos]; 160 } 161 162 struct thread_core_local * __nostackcheck thread_get_core_local(void) 163 { 164 unsigned int pos = get_core_pos(); 165 166 return get_core_local(pos); 167 } 168 169 #ifdef CFG_CORE_DEBUG_CHECK_STACKS 170 static void print_stack_limits(void) 171 { 172 size_t n = 0; 173 vaddr_t __maybe_unused start = 0; 174 vaddr_t __maybe_unused end = 0; 175 176 for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) { 177 start = GET_STACK_TOP_SOFT(stack_tmp, n); 178 end = GET_STACK_BOTTOM(stack_tmp, n); 179 DMSG("tmp [%zu] 0x%" PRIxVA "..0x%" PRIxVA, n, start, end); 180 } 181 for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) { 182 start = GET_STACK_TOP_SOFT(stack_abt, n); 183 end = GET_STACK_BOTTOM(stack_abt, n); 184 DMSG("abt [%zu] 0x%" PRIxVA "..0x%" PRIxVA, n, start, end); 185 } 186 for (n = 0; n < CFG_NUM_THREADS; n++) { 187 end = threads[n].stack_va_end; 188 start = end - STACK_THREAD_SIZE + STACK_CHECK_EXTRA; 189 DMSG("thr [%zu] 0x%" PRIxVA "..0x%" PRIxVA, n, start, end); 190 } 191 } 192 193 static void check_stack_limits(void) 194 { 195 vaddr_t stack_start = 0; 196 vaddr_t stack_end = 0; 197 /* Any value in the current stack frame will do */ 198 vaddr_t current_sp = (vaddr_t)&stack_start; 199 200 if (!get_stack_soft_limits(&stack_start, &stack_end)) 201 panic("Unknown stack limits"); 202 if (current_sp < stack_start || current_sp > stack_end) { 203 EMSG("Stack pointer out of range: 0x%" PRIxVA " not in [0x%" 204 PRIxVA " .. 0x%" PRIxVA "]", current_sp, stack_start, 205 stack_end); 206 print_stack_limits(); 207 panic(); 208 } 209 } 210 211 static bool * __nostackcheck get_stackcheck_recursion_flag(void) 212 { 213 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 214 unsigned int pos = get_core_pos(); 215 struct thread_core_local *l = get_core_local(pos); 216 int ct = l->curr_thread; 217 bool *p = NULL; 218 219 if (l->flags & (THREAD_CLF_ABORT | THREAD_CLF_TMP)) 220 p = &l->stackcheck_recursion; 221 else if (!l->flags) 222 p = &threads[ct].tsd.stackcheck_recursion; 223 224 thread_unmask_exceptions(exceptions); 225 return p; 226 } 227 228 void __cyg_profile_func_enter(void *this_fn, void *call_site); 229 void __nostackcheck __cyg_profile_func_enter(void *this_fn __unused, 230 void *call_site __unused) 231 { 232 bool *p = get_stackcheck_recursion_flag(); 233 234 assert(p); 235 if (*p) 236 return; 237 *p = true; 238 check_stack_limits(); 239 *p = false; 240 } 241 242 void __cyg_profile_func_exit(void *this_fn, void *call_site); 243 void __nostackcheck __cyg_profile_func_exit(void *this_fn __unused, 244 void *call_site __unused) 245 { 246 } 247 #else 248 static void print_stack_limits(void) 249 { 250 } 251 #endif 252 253 void thread_init_boot_thread(void) 254 { 255 struct thread_core_local *l = thread_get_core_local(); 256 257 thread_init_threads(); 258 259 l->curr_thread = 0; 260 threads[0].state = THREAD_STATE_ACTIVE; 261 } 262 263 void __nostackcheck thread_clr_boot_thread(void) 264 { 265 struct thread_core_local *l = thread_get_core_local(); 266 267 assert(l->curr_thread >= 0 && l->curr_thread < CFG_NUM_THREADS); 268 assert(threads[l->curr_thread].state == THREAD_STATE_ACTIVE); 269 threads[l->curr_thread].state = THREAD_STATE_FREE; 270 l->curr_thread = THREAD_ID_INVALID; 271 } 272 273 void __nostackcheck *thread_get_tmp_sp(void) 274 { 275 struct thread_core_local *l = thread_get_core_local(); 276 277 /* 278 * Called from assembly when switching to the temporary stack, so flags 279 * need updating 280 */ 281 l->flags |= THREAD_CLF_TMP; 282 283 return (void *)l->tmp_stack_va_end; 284 } 285 286 vaddr_t thread_stack_start(void) 287 { 288 struct thread_ctx *thr; 289 int ct = thread_get_id_may_fail(); 290 291 if (ct == THREAD_ID_INVALID) 292 return 0; 293 294 thr = threads + ct; 295 return thr->stack_va_end - STACK_THREAD_SIZE; 296 } 297 298 size_t thread_stack_size(void) 299 { 300 return STACK_THREAD_SIZE; 301 } 302 303 bool get_stack_limits(vaddr_t *start, vaddr_t *end, bool hard) 304 { 305 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 306 unsigned int pos = get_core_pos(); 307 struct thread_core_local *l = get_core_local(pos); 308 int ct = l->curr_thread; 309 bool ret = false; 310 311 if (l->flags & THREAD_CLF_TMP) { 312 if (hard) 313 *start = GET_STACK_TOP_HARD(stack_tmp, pos); 314 else 315 *start = GET_STACK_TOP_SOFT(stack_tmp, pos); 316 *end = GET_STACK_BOTTOM(stack_tmp, pos); 317 ret = true; 318 } else if (l->flags & THREAD_CLF_ABORT) { 319 if (hard) 320 *start = GET_STACK_TOP_HARD(stack_abt, pos); 321 else 322 *start = GET_STACK_TOP_SOFT(stack_abt, pos); 323 *end = GET_STACK_BOTTOM(stack_abt, pos); 324 ret = true; 325 } else if (!l->flags) { 326 if (ct < 0 || ct >= CFG_NUM_THREADS) 327 goto out; 328 329 *end = threads[ct].stack_va_end; 330 *start = *end - STACK_THREAD_SIZE; 331 if (!hard) 332 *start += STACK_CHECK_EXTRA; 333 ret = true; 334 } 335 out: 336 thread_unmask_exceptions(exceptions); 337 return ret; 338 } 339 340 bool thread_is_from_abort_mode(void) 341 { 342 struct thread_core_local *l = thread_get_core_local(); 343 344 return (l->flags >> THREAD_CLF_SAVED_SHIFT) & THREAD_CLF_ABORT; 345 } 346 347 /* 348 * This function should always be accurate, but it might be possible to 349 * implement a more efficient depending on cpu architecture. 350 */ 351 bool __weak thread_is_in_normal_mode(void) 352 { 353 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 354 struct thread_core_local *l = thread_get_core_local(); 355 bool ret; 356 357 /* 358 * If any bit in l->flags is set aside from THREAD_CLF_TMP we're 359 * handling some exception. 360 */ 361 ret = (l->curr_thread != THREAD_ID_INVALID) && 362 !(l->flags & ~THREAD_CLF_TMP); 363 thread_unmask_exceptions(exceptions); 364 365 return ret; 366 } 367 368 short int thread_get_id_may_fail(void) 369 { 370 /* 371 * thread_get_core_local() requires foreign interrupts to be disabled 372 */ 373 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 374 struct thread_core_local *l = thread_get_core_local(); 375 short int ct = l->curr_thread; 376 377 thread_unmask_exceptions(exceptions); 378 return ct; 379 } 380 381 short int thread_get_id(void) 382 { 383 short int ct = thread_get_id_may_fail(); 384 385 /* Thread ID has to fit in a short int */ 386 COMPILE_TIME_ASSERT(CFG_NUM_THREADS <= SHRT_MAX); 387 assert(ct >= 0 && ct < CFG_NUM_THREADS); 388 return ct; 389 } 390 391 #ifdef CFG_WITH_PAGER 392 static void init_thread_stacks(void) 393 { 394 size_t n = 0; 395 396 /* 397 * Allocate virtual memory for thread stacks. 398 */ 399 for (n = 0; n < CFG_NUM_THREADS; n++) { 400 tee_mm_entry_t *mm = NULL; 401 vaddr_t sp = 0; 402 size_t num_pages = 0; 403 struct fobj *fobj = NULL; 404 405 /* Find vmem for thread stack and its protection gap */ 406 mm = tee_mm_alloc(&tee_mm_vcore, 407 SMALL_PAGE_SIZE + STACK_THREAD_SIZE); 408 assert(mm); 409 410 /* Claim eventual physical page */ 411 tee_pager_add_pages(tee_mm_get_smem(mm), tee_mm_get_size(mm), 412 true); 413 414 num_pages = tee_mm_get_bytes(mm) / SMALL_PAGE_SIZE - 1; 415 fobj = fobj_locked_paged_alloc(num_pages); 416 417 /* Add the region to the pager */ 418 tee_pager_add_core_region(tee_mm_get_smem(mm) + SMALL_PAGE_SIZE, 419 PAGED_REGION_TYPE_LOCK, fobj); 420 fobj_put(fobj); 421 422 /* init effective stack */ 423 sp = tee_mm_get_smem(mm) + tee_mm_get_bytes(mm); 424 asan_tag_access((void *)tee_mm_get_smem(mm), (void *)sp); 425 if (!thread_init_stack(n, sp)) 426 panic("init stack failed"); 427 } 428 } 429 #else 430 static void init_thread_stacks(void) 431 { 432 size_t n; 433 434 /* Assign the thread stacks */ 435 for (n = 0; n < CFG_NUM_THREADS; n++) { 436 if (!thread_init_stack(n, GET_STACK_BOTTOM(stack_thread, n))) 437 panic("thread_init_stack failed"); 438 } 439 } 440 #endif /*CFG_WITH_PAGER*/ 441 442 void thread_init_threads(void) 443 { 444 size_t n = 0; 445 446 init_thread_stacks(); 447 print_stack_limits(); 448 pgt_init(); 449 450 mutex_lockdep_init(); 451 452 for (n = 0; n < CFG_NUM_THREADS; n++) { 453 TAILQ_INIT(&threads[n].tsd.sess_stack); 454 SLIST_INIT(&threads[n].tsd.pgt_cache); 455 } 456 } 457 458 void __nostackcheck thread_init_thread_core_local(void) 459 { 460 size_t n = 0; 461 struct thread_core_local *tcl = thread_core_local; 462 463 for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) { 464 tcl[n].curr_thread = THREAD_ID_INVALID; 465 tcl[n].flags = THREAD_CLF_TMP; 466 } 467 tcl[0].tmp_stack_va_end = GET_STACK_BOTTOM(stack_tmp, 0); 468 } 469 470 void thread_init_core_local_stacks(void) 471 { 472 size_t n = 0; 473 struct thread_core_local *tcl = thread_core_local; 474 475 for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) { 476 tcl[n].tmp_stack_va_end = GET_STACK_BOTTOM(stack_tmp, n) - 477 STACK_TMP_OFFS; 478 tcl[n].abt_stack_va_end = GET_STACK_BOTTOM(stack_abt, n); 479 } 480 } 481 482 struct thread_specific_data *thread_get_tsd(void) 483 { 484 return &threads[thread_get_id()].tsd; 485 } 486 487 struct thread_ctx_regs * __nostackcheck thread_get_ctx_regs(void) 488 { 489 struct thread_core_local *l = thread_get_core_local(); 490 491 assert(l->curr_thread != THREAD_ID_INVALID); 492 return &threads[l->curr_thread].regs; 493 } 494 495 void thread_set_foreign_intr(bool enable) 496 { 497 /* thread_get_core_local() requires foreign interrupts to be disabled */ 498 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 499 struct thread_core_local *l; 500 501 l = thread_get_core_local(); 502 503 assert(l->curr_thread != THREAD_ID_INVALID); 504 505 if (enable) { 506 threads[l->curr_thread].flags |= 507 THREAD_FLAGS_FOREIGN_INTR_ENABLE; 508 thread_set_exceptions(exceptions & ~THREAD_EXCP_FOREIGN_INTR); 509 } else { 510 /* 511 * No need to disable foreign interrupts here since they're 512 * already disabled above. 513 */ 514 threads[l->curr_thread].flags &= 515 ~THREAD_FLAGS_FOREIGN_INTR_ENABLE; 516 } 517 } 518 519 void thread_restore_foreign_intr(void) 520 { 521 /* thread_get_core_local() requires foreign interrupts to be disabled */ 522 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 523 struct thread_core_local *l; 524 525 l = thread_get_core_local(); 526 527 assert(l->curr_thread != THREAD_ID_INVALID); 528 529 if (threads[l->curr_thread].flags & THREAD_FLAGS_FOREIGN_INTR_ENABLE) 530 thread_set_exceptions(exceptions & ~THREAD_EXCP_FOREIGN_INTR); 531 } 532 533 static struct mobj *alloc_shm(enum thread_shm_type shm_type, size_t size) 534 { 535 switch (shm_type) { 536 case THREAD_SHM_TYPE_APPLICATION: 537 return thread_rpc_alloc_payload(size); 538 case THREAD_SHM_TYPE_KERNEL_PRIVATE: 539 return thread_rpc_alloc_kernel_payload(size); 540 case THREAD_SHM_TYPE_GLOBAL: 541 return thread_rpc_alloc_global_payload(size); 542 default: 543 return NULL; 544 } 545 } 546 547 static void clear_shm_cache_entry(struct thread_shm_cache_entry *ce) 548 { 549 if (ce->mobj) { 550 switch (ce->type) { 551 case THREAD_SHM_TYPE_APPLICATION: 552 thread_rpc_free_payload(ce->mobj); 553 break; 554 case THREAD_SHM_TYPE_KERNEL_PRIVATE: 555 thread_rpc_free_kernel_payload(ce->mobj); 556 break; 557 case THREAD_SHM_TYPE_GLOBAL: 558 thread_rpc_free_global_payload(ce->mobj); 559 break; 560 default: 561 assert(0); /* "can't happen" */ 562 break; 563 } 564 } 565 ce->mobj = NULL; 566 ce->size = 0; 567 } 568 569 static struct thread_shm_cache_entry * 570 get_shm_cache_entry(enum thread_shm_cache_user user) 571 { 572 struct thread_shm_cache *cache = &threads[thread_get_id()].shm_cache; 573 struct thread_shm_cache_entry *ce = NULL; 574 575 SLIST_FOREACH(ce, cache, link) 576 if (ce->user == user) 577 return ce; 578 579 ce = calloc(1, sizeof(*ce)); 580 if (ce) { 581 ce->user = user; 582 SLIST_INSERT_HEAD(cache, ce, link); 583 } 584 585 return ce; 586 } 587 588 void *thread_rpc_shm_cache_alloc(enum thread_shm_cache_user user, 589 enum thread_shm_type shm_type, 590 size_t size, struct mobj **mobj) 591 { 592 struct thread_shm_cache_entry *ce = NULL; 593 size_t sz = size; 594 paddr_t p = 0; 595 void *va = NULL; 596 597 if (!size) 598 return NULL; 599 600 ce = get_shm_cache_entry(user); 601 if (!ce) 602 return NULL; 603 604 /* 605 * Always allocate in page chunks as normal world allocates payload 606 * memory as complete pages. 607 */ 608 sz = ROUNDUP(size, SMALL_PAGE_SIZE); 609 610 if (ce->type != shm_type || sz > ce->size) { 611 clear_shm_cache_entry(ce); 612 613 ce->mobj = alloc_shm(shm_type, sz); 614 if (!ce->mobj) 615 return NULL; 616 617 if (mobj_get_pa(ce->mobj, 0, 0, &p)) 618 goto err; 619 620 if (!IS_ALIGNED_WITH_TYPE(p, uint64_t)) 621 goto err; 622 623 va = mobj_get_va(ce->mobj, 0, sz); 624 if (!va) 625 goto err; 626 627 ce->size = sz; 628 ce->type = shm_type; 629 } else { 630 va = mobj_get_va(ce->mobj, 0, sz); 631 if (!va) 632 goto err; 633 } 634 *mobj = ce->mobj; 635 636 return va; 637 err: 638 clear_shm_cache_entry(ce); 639 return NULL; 640 } 641 642 void thread_rpc_shm_cache_clear(struct thread_shm_cache *cache) 643 { 644 while (true) { 645 struct thread_shm_cache_entry *ce = SLIST_FIRST(cache); 646 647 if (!ce) 648 break; 649 SLIST_REMOVE_HEAD(cache, link); 650 clear_shm_cache_entry(ce); 651 free(ce); 652 } 653 } 654