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; 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 DMSG("Stack pointer out of range (0x%" PRIxVA ")", current_sp); 204 print_stack_limits(); 205 panic(); 206 } 207 } 208 209 static bool * __nostackcheck get_stackcheck_recursion_flag(void) 210 { 211 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 212 unsigned int pos = get_core_pos(); 213 struct thread_core_local *l = get_core_local(pos); 214 int ct = l->curr_thread; 215 bool *p = NULL; 216 217 if (l->flags & (THREAD_CLF_ABORT | THREAD_CLF_TMP)) 218 p = &l->stackcheck_recursion; 219 else if (!l->flags) 220 p = &threads[ct].tsd.stackcheck_recursion; 221 222 thread_unmask_exceptions(exceptions); 223 return p; 224 } 225 226 void __cyg_profile_func_enter(void *this_fn, void *call_site); 227 void __nostackcheck __cyg_profile_func_enter(void *this_fn __unused, 228 void *call_site __unused) 229 { 230 bool *p = get_stackcheck_recursion_flag(); 231 232 assert(p); 233 if (*p) 234 return; 235 *p = true; 236 check_stack_limits(); 237 *p = false; 238 } 239 240 void __cyg_profile_func_exit(void *this_fn, void *call_site); 241 void __nostackcheck __cyg_profile_func_exit(void *this_fn __unused, 242 void *call_site __unused) 243 { 244 } 245 #else 246 static void print_stack_limits(void) 247 { 248 } 249 #endif 250 251 void thread_init_boot_thread(void) 252 { 253 struct thread_core_local *l = thread_get_core_local(); 254 255 thread_init_threads(); 256 257 l->curr_thread = 0; 258 threads[0].state = THREAD_STATE_ACTIVE; 259 } 260 261 void __nostackcheck thread_clr_boot_thread(void) 262 { 263 struct thread_core_local *l = thread_get_core_local(); 264 265 assert(l->curr_thread >= 0 && l->curr_thread < CFG_NUM_THREADS); 266 assert(threads[l->curr_thread].state == THREAD_STATE_ACTIVE); 267 threads[l->curr_thread].state = THREAD_STATE_FREE; 268 l->curr_thread = THREAD_ID_INVALID; 269 } 270 271 void __nostackcheck *thread_get_tmp_sp(void) 272 { 273 struct thread_core_local *l = thread_get_core_local(); 274 275 /* 276 * Called from assembly when switching to the temporary stack, so flags 277 * need updating 278 */ 279 l->flags |= THREAD_CLF_TMP; 280 281 return (void *)l->tmp_stack_va_end; 282 } 283 284 vaddr_t thread_stack_start(void) 285 { 286 struct thread_ctx *thr; 287 int ct = thread_get_id_may_fail(); 288 289 if (ct == THREAD_ID_INVALID) 290 return 0; 291 292 thr = threads + ct; 293 return thr->stack_va_end - STACK_THREAD_SIZE; 294 } 295 296 size_t thread_stack_size(void) 297 { 298 return STACK_THREAD_SIZE; 299 } 300 301 bool get_stack_limits(vaddr_t *start, vaddr_t *end, bool hard) 302 { 303 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 304 unsigned int pos = get_core_pos(); 305 struct thread_core_local *l = get_core_local(pos); 306 int ct = l->curr_thread; 307 bool ret = false; 308 309 if (l->flags & THREAD_CLF_TMP) { 310 if (hard) 311 *start = GET_STACK_TOP_HARD(stack_tmp, pos); 312 else 313 *start = GET_STACK_TOP_SOFT(stack_tmp, pos); 314 *end = GET_STACK_BOTTOM(stack_tmp, pos); 315 ret = true; 316 } else if (l->flags & THREAD_CLF_ABORT) { 317 if (hard) 318 *start = GET_STACK_TOP_HARD(stack_abt, pos); 319 else 320 *start = GET_STACK_TOP_SOFT(stack_abt, pos); 321 *end = GET_STACK_BOTTOM(stack_abt, pos); 322 ret = true; 323 } else if (!l->flags) { 324 if (ct < 0 || ct >= CFG_NUM_THREADS) 325 goto out; 326 327 *end = threads[ct].stack_va_end; 328 *start = *end - STACK_THREAD_SIZE; 329 if (!hard) 330 *start += STACK_CHECK_EXTRA; 331 ret = true; 332 } 333 out: 334 thread_unmask_exceptions(exceptions); 335 return ret; 336 } 337 338 bool thread_is_from_abort_mode(void) 339 { 340 struct thread_core_local *l = thread_get_core_local(); 341 342 return (l->flags >> THREAD_CLF_SAVED_SHIFT) & THREAD_CLF_ABORT; 343 } 344 345 /* 346 * This function should always be accurate, but it might be possible to 347 * implement a more efficient depending on cpu architecture. 348 */ 349 bool __weak thread_is_in_normal_mode(void) 350 { 351 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 352 struct thread_core_local *l = thread_get_core_local(); 353 bool ret; 354 355 /* 356 * If any bit in l->flags is set aside from THREAD_CLF_TMP we're 357 * handling some exception. 358 */ 359 ret = (l->curr_thread != THREAD_ID_INVALID) && 360 !(l->flags & ~THREAD_CLF_TMP); 361 thread_unmask_exceptions(exceptions); 362 363 return ret; 364 } 365 366 short int thread_get_id_may_fail(void) 367 { 368 /* 369 * thread_get_core_local() requires foreign interrupts to be disabled 370 */ 371 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 372 struct thread_core_local *l = thread_get_core_local(); 373 short int ct = l->curr_thread; 374 375 thread_unmask_exceptions(exceptions); 376 return ct; 377 } 378 379 short int thread_get_id(void) 380 { 381 short int ct = thread_get_id_may_fail(); 382 383 /* Thread ID has to fit in a short int */ 384 COMPILE_TIME_ASSERT(CFG_NUM_THREADS <= SHRT_MAX); 385 assert(ct >= 0 && ct < CFG_NUM_THREADS); 386 return ct; 387 } 388 389 #ifdef CFG_WITH_PAGER 390 static void init_thread_stacks(void) 391 { 392 size_t n = 0; 393 394 /* 395 * Allocate virtual memory for thread stacks. 396 */ 397 for (n = 0; n < CFG_NUM_THREADS; n++) { 398 tee_mm_entry_t *mm = NULL; 399 vaddr_t sp = 0; 400 size_t num_pages = 0; 401 struct fobj *fobj = NULL; 402 403 /* Find vmem for thread stack and its protection gap */ 404 mm = tee_mm_alloc(&tee_mm_vcore, 405 SMALL_PAGE_SIZE + STACK_THREAD_SIZE); 406 assert(mm); 407 408 /* Claim eventual physical page */ 409 tee_pager_add_pages(tee_mm_get_smem(mm), tee_mm_get_size(mm), 410 true); 411 412 num_pages = tee_mm_get_bytes(mm) / SMALL_PAGE_SIZE - 1; 413 fobj = fobj_locked_paged_alloc(num_pages); 414 415 /* Add the region to the pager */ 416 tee_pager_add_core_region(tee_mm_get_smem(mm) + SMALL_PAGE_SIZE, 417 PAGED_REGION_TYPE_LOCK, fobj); 418 fobj_put(fobj); 419 420 /* init effective stack */ 421 sp = tee_mm_get_smem(mm) + tee_mm_get_bytes(mm); 422 asan_tag_access((void *)tee_mm_get_smem(mm), (void *)sp); 423 if (!thread_init_stack(n, sp)) 424 panic("init stack failed"); 425 } 426 } 427 #else 428 static void init_thread_stacks(void) 429 { 430 size_t n; 431 432 /* Assign the thread stacks */ 433 for (n = 0; n < CFG_NUM_THREADS; n++) { 434 if (!thread_init_stack(n, GET_STACK_BOTTOM(stack_thread, n))) 435 panic("thread_init_stack failed"); 436 } 437 } 438 #endif /*CFG_WITH_PAGER*/ 439 440 void thread_init_threads(void) 441 { 442 size_t n = 0; 443 444 init_thread_stacks(); 445 print_stack_limits(); 446 pgt_init(); 447 448 mutex_lockdep_init(); 449 450 for (n = 0; n < CFG_NUM_THREADS; n++) { 451 TAILQ_INIT(&threads[n].tsd.sess_stack); 452 SLIST_INIT(&threads[n].tsd.pgt_cache); 453 } 454 } 455 456 void __nostackcheck thread_init_thread_core_local(void) 457 { 458 size_t n = 0; 459 struct thread_core_local *tcl = thread_core_local; 460 461 for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) { 462 tcl[n].curr_thread = THREAD_ID_INVALID; 463 tcl[n].flags = THREAD_CLF_TMP; 464 } 465 tcl[0].tmp_stack_va_end = GET_STACK_BOTTOM(stack_tmp, 0); 466 } 467 468 void thread_init_core_local_stacks(void) 469 { 470 size_t n = 0; 471 struct thread_core_local *tcl = thread_core_local; 472 473 for (n = 0; n < CFG_TEE_CORE_NB_CORE; n++) { 474 tcl[n].tmp_stack_va_end = GET_STACK_BOTTOM(stack_tmp, n) - 475 STACK_TMP_OFFS; 476 tcl[n].abt_stack_va_end = GET_STACK_BOTTOM(stack_abt, n); 477 } 478 } 479 480 struct thread_specific_data *thread_get_tsd(void) 481 { 482 return &threads[thread_get_id()].tsd; 483 } 484 485 struct thread_ctx_regs * __nostackcheck thread_get_ctx_regs(void) 486 { 487 struct thread_core_local *l = thread_get_core_local(); 488 489 assert(l->curr_thread != THREAD_ID_INVALID); 490 return &threads[l->curr_thread].regs; 491 } 492 493 void thread_set_foreign_intr(bool enable) 494 { 495 /* thread_get_core_local() requires foreign interrupts to be disabled */ 496 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 497 struct thread_core_local *l; 498 499 l = thread_get_core_local(); 500 501 assert(l->curr_thread != THREAD_ID_INVALID); 502 503 if (enable) { 504 threads[l->curr_thread].flags |= 505 THREAD_FLAGS_FOREIGN_INTR_ENABLE; 506 thread_set_exceptions(exceptions & ~THREAD_EXCP_FOREIGN_INTR); 507 } else { 508 /* 509 * No need to disable foreign interrupts here since they're 510 * already disabled above. 511 */ 512 threads[l->curr_thread].flags &= 513 ~THREAD_FLAGS_FOREIGN_INTR_ENABLE; 514 } 515 } 516 517 void thread_restore_foreign_intr(void) 518 { 519 /* thread_get_core_local() requires foreign interrupts to be disabled */ 520 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 521 struct thread_core_local *l; 522 523 l = thread_get_core_local(); 524 525 assert(l->curr_thread != THREAD_ID_INVALID); 526 527 if (threads[l->curr_thread].flags & THREAD_FLAGS_FOREIGN_INTR_ENABLE) 528 thread_set_exceptions(exceptions & ~THREAD_EXCP_FOREIGN_INTR); 529 } 530 531 static struct mobj *alloc_shm(enum thread_shm_type shm_type, size_t size) 532 { 533 switch (shm_type) { 534 case THREAD_SHM_TYPE_APPLICATION: 535 return thread_rpc_alloc_payload(size); 536 case THREAD_SHM_TYPE_KERNEL_PRIVATE: 537 return thread_rpc_alloc_kernel_payload(size); 538 case THREAD_SHM_TYPE_GLOBAL: 539 return thread_rpc_alloc_global_payload(size); 540 default: 541 return NULL; 542 } 543 } 544 545 static void clear_shm_cache_entry(struct thread_shm_cache_entry *ce) 546 { 547 if (ce->mobj) { 548 switch (ce->type) { 549 case THREAD_SHM_TYPE_APPLICATION: 550 thread_rpc_free_payload(ce->mobj); 551 break; 552 case THREAD_SHM_TYPE_KERNEL_PRIVATE: 553 thread_rpc_free_kernel_payload(ce->mobj); 554 break; 555 case THREAD_SHM_TYPE_GLOBAL: 556 thread_rpc_free_global_payload(ce->mobj); 557 break; 558 default: 559 assert(0); /* "can't happen" */ 560 break; 561 } 562 } 563 ce->mobj = NULL; 564 ce->size = 0; 565 } 566 567 static struct thread_shm_cache_entry * 568 get_shm_cache_entry(enum thread_shm_cache_user user) 569 { 570 struct thread_shm_cache *cache = &threads[thread_get_id()].shm_cache; 571 struct thread_shm_cache_entry *ce = NULL; 572 573 SLIST_FOREACH(ce, cache, link) 574 if (ce->user == user) 575 return ce; 576 577 ce = calloc(1, sizeof(*ce)); 578 if (ce) { 579 ce->user = user; 580 SLIST_INSERT_HEAD(cache, ce, link); 581 } 582 583 return ce; 584 } 585 586 void *thread_rpc_shm_cache_alloc(enum thread_shm_cache_user user, 587 enum thread_shm_type shm_type, 588 size_t size, struct mobj **mobj) 589 { 590 struct thread_shm_cache_entry *ce = NULL; 591 size_t sz = size; 592 paddr_t p = 0; 593 void *va = NULL; 594 595 if (!size) 596 return NULL; 597 598 ce = get_shm_cache_entry(user); 599 if (!ce) 600 return NULL; 601 602 /* 603 * Always allocate in page chunks as normal world allocates payload 604 * memory as complete pages. 605 */ 606 sz = ROUNDUP(size, SMALL_PAGE_SIZE); 607 608 if (ce->type != shm_type || sz > ce->size) { 609 clear_shm_cache_entry(ce); 610 611 ce->mobj = alloc_shm(shm_type, sz); 612 if (!ce->mobj) 613 return NULL; 614 615 if (mobj_get_pa(ce->mobj, 0, 0, &p)) 616 goto err; 617 618 if (!IS_ALIGNED_WITH_TYPE(p, uint64_t)) 619 goto err; 620 621 va = mobj_get_va(ce->mobj, 0, sz); 622 if (!va) 623 goto err; 624 625 ce->size = sz; 626 ce->type = shm_type; 627 } else { 628 va = mobj_get_va(ce->mobj, 0, sz); 629 if (!va) 630 goto err; 631 } 632 *mobj = ce->mobj; 633 634 return va; 635 err: 636 clear_shm_cache_entry(ce); 637 return NULL; 638 } 639 640 void thread_rpc_shm_cache_clear(struct thread_shm_cache *cache) 641 { 642 while (true) { 643 struct thread_shm_cache_entry *ce = SLIST_FIRST(cache); 644 645 if (!ce) 646 break; 647 SLIST_REMOVE_HEAD(cache, link); 648 clear_shm_cache_entry(ce); 649 free(ce); 650 } 651 } 652