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