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