1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2015-2021, Linaro Limited 4 */ 5 6 #include <arm.h> 7 #include <assert.h> 8 #include <compiler.h> 9 #include <config.h> 10 #include <console.h> 11 #include <crypto/crypto.h> 12 #include <initcall.h> 13 #include <inttypes.h> 14 #include <keep.h> 15 #include <kernel/asan.h> 16 #include <kernel/boot.h> 17 #include <kernel/linker.h> 18 #include <kernel/misc.h> 19 #include <kernel/panic.h> 20 #include <kernel/tee_misc.h> 21 #include <kernel/thread.h> 22 #include <kernel/tpm.h> 23 #include <libfdt.h> 24 #include <malloc.h> 25 #include <mm/core_memprot.h> 26 #include <mm/core_mmu.h> 27 #include <mm/fobj.h> 28 #include <mm/tee_mm.h> 29 #include <mm/tee_pager.h> 30 #include <sm/psci.h> 31 #include <stdio.h> 32 #include <trace.h> 33 #include <utee_defines.h> 34 #include <util.h> 35 36 #include <platform_config.h> 37 38 #if !defined(CFG_WITH_ARM_TRUSTED_FW) 39 #include <sm/sm.h> 40 #endif 41 42 #if defined(CFG_WITH_VFP) 43 #include <kernel/vfp.h> 44 #endif 45 46 /* 47 * In this file we're using unsigned long to represent physical pointers as 48 * they are received in a single register when OP-TEE is initially entered. 49 * This limits 32-bit systems to only use make use of the lower 32 bits 50 * of a physical address for initial parameters. 51 * 52 * 64-bit systems on the other hand can use full 64-bit physical pointers. 53 */ 54 #define PADDR_INVALID ULONG_MAX 55 56 #if defined(CFG_BOOT_SECONDARY_REQUEST) 57 struct ns_entry_context { 58 uintptr_t entry_point; 59 uintptr_t context_id; 60 }; 61 struct ns_entry_context ns_entry_contexts[CFG_TEE_CORE_NB_CORE]; 62 static uint32_t spin_table[CFG_TEE_CORE_NB_CORE]; 63 #endif 64 65 #ifdef CFG_BOOT_SYNC_CPU 66 /* 67 * Array used when booting, to synchronize cpu. 68 * When 0, the cpu has not started. 69 * When 1, it has started 70 */ 71 uint32_t sem_cpu_sync[CFG_TEE_CORE_NB_CORE]; 72 DECLARE_KEEP_PAGER(sem_cpu_sync); 73 #endif 74 75 #ifdef CFG_DT 76 struct dt_descriptor { 77 void *blob; 78 #ifdef CFG_EXTERNAL_DTB_OVERLAY 79 int frag_id; 80 #endif 81 }; 82 83 static struct dt_descriptor external_dt __nex_bss; 84 #endif 85 86 #ifdef CFG_SECONDARY_INIT_CNTFRQ 87 static uint32_t cntfrq; 88 #endif 89 90 /* May be overridden in plat-$(PLATFORM)/main.c */ 91 __weak void plat_primary_init_early(void) 92 { 93 } 94 DECLARE_KEEP_PAGER(plat_primary_init_early); 95 96 /* May be overridden in plat-$(PLATFORM)/main.c */ 97 __weak void main_init_gic(void) 98 { 99 } 100 101 /* May be overridden in plat-$(PLATFORM)/main.c */ 102 __weak void main_secondary_init_gic(void) 103 { 104 } 105 106 /* May be overridden in plat-$(PLATFORM)/main.c */ 107 __weak unsigned long plat_get_aslr_seed(void) 108 { 109 DMSG("Warning: no ASLR seed"); 110 111 return 0; 112 } 113 114 #if defined(CFG_WITH_ARM_TRUSTED_FW) 115 void init_sec_mon(unsigned long nsec_entry __maybe_unused) 116 { 117 assert(nsec_entry == PADDR_INVALID); 118 /* Do nothing as we don't have a secure monitor */ 119 } 120 #else 121 /* May be overridden in plat-$(PLATFORM)/main.c */ 122 __weak void init_sec_mon(unsigned long nsec_entry) 123 { 124 struct sm_nsec_ctx *nsec_ctx; 125 126 assert(nsec_entry != PADDR_INVALID); 127 128 /* Initialize secure monitor */ 129 nsec_ctx = sm_get_nsec_ctx(); 130 nsec_ctx->mon_lr = nsec_entry; 131 nsec_ctx->mon_spsr = CPSR_MODE_SVC | CPSR_I; 132 if (nsec_entry & 1) 133 nsec_ctx->mon_spsr |= CPSR_T; 134 } 135 #endif 136 137 #if defined(CFG_WITH_ARM_TRUSTED_FW) 138 static void init_vfp_nsec(void) 139 { 140 } 141 #else 142 static void init_vfp_nsec(void) 143 { 144 /* Normal world can use CP10 and CP11 (SIMD/VFP) */ 145 write_nsacr(read_nsacr() | NSACR_CP10 | NSACR_CP11); 146 } 147 #endif 148 149 #if defined(CFG_WITH_VFP) 150 151 #ifdef ARM32 152 static void init_vfp_sec(void) 153 { 154 uint32_t cpacr = read_cpacr(); 155 156 /* 157 * Enable Advanced SIMD functionality. 158 * Enable use of D16-D31 of the Floating-point Extension register 159 * file. 160 */ 161 cpacr &= ~(CPACR_ASEDIS | CPACR_D32DIS); 162 /* 163 * Enable usage of CP10 and CP11 (SIMD/VFP) (both kernel and user 164 * mode. 165 */ 166 cpacr |= CPACR_CP(10, CPACR_CP_ACCESS_FULL); 167 cpacr |= CPACR_CP(11, CPACR_CP_ACCESS_FULL); 168 write_cpacr(cpacr); 169 } 170 #endif /* ARM32 */ 171 172 #ifdef ARM64 173 static void init_vfp_sec(void) 174 { 175 /* Not using VFP until thread_kernel_enable_vfp() */ 176 vfp_disable(); 177 } 178 #endif /* ARM64 */ 179 180 #else /* CFG_WITH_VFP */ 181 182 static void init_vfp_sec(void) 183 { 184 /* Not using VFP */ 185 } 186 #endif 187 188 #ifdef CFG_SECONDARY_INIT_CNTFRQ 189 static void primary_save_cntfrq(void) 190 { 191 assert(cntfrq == 0); 192 193 /* 194 * CNTFRQ should be initialized on the primary CPU by a 195 * previous boot stage 196 */ 197 cntfrq = read_cntfrq(); 198 } 199 200 static void secondary_init_cntfrq(void) 201 { 202 assert(cntfrq != 0); 203 write_cntfrq(cntfrq); 204 } 205 #else /* CFG_SECONDARY_INIT_CNTFRQ */ 206 static void primary_save_cntfrq(void) 207 { 208 } 209 210 static void secondary_init_cntfrq(void) 211 { 212 } 213 #endif 214 215 #ifdef CFG_CORE_SANITIZE_KADDRESS 216 static void init_run_constructors(void) 217 { 218 const vaddr_t *ctor; 219 220 for (ctor = &__ctor_list; ctor < &__ctor_end; ctor++) 221 ((void (*)(void))(*ctor))(); 222 } 223 224 static void init_asan(void) 225 { 226 227 /* 228 * CFG_ASAN_SHADOW_OFFSET is also supplied as 229 * -fasan-shadow-offset=$(CFG_ASAN_SHADOW_OFFSET) to the compiler. 230 * Since all the needed values to calculate the value of 231 * CFG_ASAN_SHADOW_OFFSET isn't available in to make we need to 232 * calculate it in advance and hard code it into the platform 233 * conf.mk. Here where we have all the needed values we double 234 * check that the compiler is supplied the correct value. 235 */ 236 237 #define __ASAN_SHADOW_START \ 238 ROUNDUP(TEE_RAM_VA_START + (TEE_RAM_VA_SIZE * 8) / 9 - 8, 8) 239 assert(__ASAN_SHADOW_START == (vaddr_t)&__asan_shadow_start); 240 #define __CFG_ASAN_SHADOW_OFFSET \ 241 (__ASAN_SHADOW_START - (TEE_RAM_VA_START / 8)) 242 COMPILE_TIME_ASSERT(CFG_ASAN_SHADOW_OFFSET == __CFG_ASAN_SHADOW_OFFSET); 243 #undef __ASAN_SHADOW_START 244 #undef __CFG_ASAN_SHADOW_OFFSET 245 246 /* 247 * Assign area covered by the shadow area, everything from start up 248 * to the beginning of the shadow area. 249 */ 250 asan_set_shadowed((void *)TEE_TEXT_VA_START, &__asan_shadow_start); 251 252 /* 253 * Add access to areas that aren't opened automatically by a 254 * constructor. 255 */ 256 asan_tag_access(&__ctor_list, &__ctor_end); 257 asan_tag_access(__rodata_start, __rodata_end); 258 #ifdef CFG_WITH_PAGER 259 asan_tag_access(__pageable_start, __pageable_end); 260 #endif /*CFG_WITH_PAGER*/ 261 asan_tag_access(__nozi_start, __nozi_end); 262 asan_tag_access(__exidx_start, __exidx_end); 263 asan_tag_access(__extab_start, __extab_end); 264 265 init_run_constructors(); 266 267 /* Everything is tagged correctly, let's start address sanitizing. */ 268 asan_start(); 269 } 270 #else /*CFG_CORE_SANITIZE_KADDRESS*/ 271 static void init_asan(void) 272 { 273 } 274 #endif /*CFG_CORE_SANITIZE_KADDRESS*/ 275 276 #ifdef CFG_WITH_PAGER 277 278 #ifdef CFG_CORE_SANITIZE_KADDRESS 279 static void carve_out_asan_mem(tee_mm_pool_t *pool) 280 { 281 const size_t s = pool->hi - pool->lo; 282 tee_mm_entry_t *mm; 283 paddr_t apa = ASAN_MAP_PA; 284 size_t asz = ASAN_MAP_SZ; 285 286 if (core_is_buffer_outside(apa, asz, pool->lo, s)) 287 return; 288 289 /* Reserve the shadow area */ 290 if (!core_is_buffer_inside(apa, asz, pool->lo, s)) { 291 if (apa < pool->lo) { 292 /* 293 * ASAN buffer is overlapping with the beginning of 294 * the pool. 295 */ 296 asz -= pool->lo - apa; 297 apa = pool->lo; 298 } else { 299 /* 300 * ASAN buffer is overlapping with the end of the 301 * pool. 302 */ 303 asz = pool->hi - apa; 304 } 305 } 306 mm = tee_mm_alloc2(pool, apa, asz); 307 assert(mm); 308 } 309 #else 310 static void carve_out_asan_mem(tee_mm_pool_t *pool __unused) 311 { 312 } 313 #endif 314 315 static void print_pager_pool_size(void) 316 { 317 struct tee_pager_stats __maybe_unused stats; 318 319 tee_pager_get_stats(&stats); 320 IMSG("Pager pool size: %zukB", 321 stats.npages_all * SMALL_PAGE_SIZE / 1024); 322 } 323 324 static void init_vcore(tee_mm_pool_t *mm_vcore) 325 { 326 const vaddr_t begin = VCORE_START_VA; 327 vaddr_t end = begin + TEE_RAM_VA_SIZE; 328 329 #ifdef CFG_CORE_SANITIZE_KADDRESS 330 /* Carve out asan memory, flat maped after core memory */ 331 if (end > ASAN_SHADOW_PA) 332 end = ASAN_MAP_PA; 333 #endif 334 335 if (!tee_mm_init(mm_vcore, begin, end, SMALL_PAGE_SHIFT, 336 TEE_MM_POOL_NO_FLAGS)) 337 panic("tee_mm_vcore init failed"); 338 } 339 340 /* 341 * With CFG_CORE_ASLR=y the init part is relocated very early during boot. 342 * The init part is also paged just as the rest of the normal paged code, with 343 * the difference that it's preloaded during boot. When the backing store 344 * is configured the entire paged binary is copied in place and then also 345 * the init part. Since the init part has been relocated (references to 346 * addresses updated to compensate for the new load address) this has to be 347 * undone for the hashes of those pages to match with the original binary. 348 * 349 * If CFG_CORE_ASLR=n, nothing needs to be done as the code/ro pages are 350 * unchanged. 351 */ 352 static void undo_init_relocation(uint8_t *paged_store __maybe_unused) 353 { 354 #ifdef CFG_CORE_ASLR 355 unsigned long *ptr = NULL; 356 const uint32_t *reloc = NULL; 357 const uint32_t *reloc_end = NULL; 358 unsigned long offs = boot_mmu_config.load_offset; 359 const struct boot_embdata *embdata = (const void *)__init_end; 360 vaddr_t addr_end = (vaddr_t)__init_end - offs - TEE_RAM_START; 361 vaddr_t addr_start = (vaddr_t)__init_start - offs - TEE_RAM_START; 362 363 reloc = (const void *)((vaddr_t)embdata + embdata->reloc_offset); 364 reloc_end = reloc + embdata->reloc_len / sizeof(*reloc); 365 366 for (; reloc < reloc_end; reloc++) { 367 if (*reloc < addr_start) 368 continue; 369 if (*reloc >= addr_end) 370 break; 371 ptr = (void *)(paged_store + *reloc - addr_start); 372 *ptr -= offs; 373 } 374 #endif 375 } 376 377 static struct fobj *ro_paged_alloc(tee_mm_entry_t *mm, void *hashes, 378 void *store) 379 { 380 const unsigned int num_pages = tee_mm_get_bytes(mm) / SMALL_PAGE_SIZE; 381 #ifdef CFG_CORE_ASLR 382 unsigned int reloc_offs = (vaddr_t)__pageable_start - VCORE_START_VA; 383 const struct boot_embdata *embdata = (const void *)__init_end; 384 const void *reloc = __init_end + embdata->reloc_offset; 385 386 return fobj_ro_reloc_paged_alloc(num_pages, hashes, reloc_offs, 387 reloc, embdata->reloc_len, store); 388 #else 389 return fobj_ro_paged_alloc(num_pages, hashes, store); 390 #endif 391 } 392 393 static void init_runtime(unsigned long pageable_part) 394 { 395 size_t n; 396 size_t init_size = (size_t)(__init_end - __init_start); 397 size_t pageable_start = (size_t)__pageable_start; 398 size_t pageable_end = (size_t)__pageable_end; 399 size_t pageable_size = pageable_end - pageable_start; 400 size_t tzsram_end = TZSRAM_BASE + TZSRAM_SIZE; 401 size_t hash_size = (pageable_size / SMALL_PAGE_SIZE) * 402 TEE_SHA256_HASH_SIZE; 403 const struct boot_embdata *embdata = (const void *)__init_end; 404 const void *tmp_hashes = NULL; 405 tee_mm_entry_t *mm = NULL; 406 struct fobj *fobj = NULL; 407 uint8_t *paged_store = NULL; 408 uint8_t *hashes = NULL; 409 410 assert(pageable_size % SMALL_PAGE_SIZE == 0); 411 assert(embdata->total_len >= embdata->hashes_offset + 412 embdata->hashes_len); 413 assert(hash_size == embdata->hashes_len); 414 415 tmp_hashes = __init_end + embdata->hashes_offset; 416 417 init_asan(); 418 419 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start); 420 malloc_add_pool(__heap2_start, __heap2_end - __heap2_start); 421 422 /* 423 * This needs to be initialized early to support address lookup 424 * in MEM_AREA_TEE_RAM 425 */ 426 tee_pager_early_init(); 427 428 hashes = malloc(hash_size); 429 IMSG_RAW("\n"); 430 IMSG("Pager is enabled. Hashes: %zu bytes", hash_size); 431 assert(hashes); 432 asan_memcpy_unchecked(hashes, tmp_hashes, hash_size); 433 434 /* 435 * Need tee_mm_sec_ddr initialized to be able to allocate secure 436 * DDR below. 437 */ 438 core_mmu_init_ta_ram(); 439 440 carve_out_asan_mem(&tee_mm_sec_ddr); 441 442 mm = tee_mm_alloc(&tee_mm_sec_ddr, pageable_size); 443 assert(mm); 444 paged_store = phys_to_virt(tee_mm_get_smem(mm), MEM_AREA_TA_RAM, 445 pageable_size); 446 /* 447 * Load pageable part in the dedicated allocated area: 448 * - Move pageable non-init part into pageable area. Note bootloader 449 * may have loaded it anywhere in TA RAM hence use memmove(). 450 * - Copy pageable init part from current location into pageable area. 451 */ 452 memmove(paged_store + init_size, 453 phys_to_virt(pageable_part, 454 core_mmu_get_type_by_pa(pageable_part), 455 __pageable_part_end - __pageable_part_start), 456 __pageable_part_end - __pageable_part_start); 457 asan_memcpy_unchecked(paged_store, __init_start, init_size); 458 /* 459 * Undo eventual relocation for the init part so the hash checks 460 * can pass. 461 */ 462 undo_init_relocation(paged_store); 463 464 /* Check that hashes of what's in pageable area is OK */ 465 DMSG("Checking hashes of pageable area"); 466 for (n = 0; (n * SMALL_PAGE_SIZE) < pageable_size; n++) { 467 const uint8_t *hash = hashes + n * TEE_SHA256_HASH_SIZE; 468 const uint8_t *page = paged_store + n * SMALL_PAGE_SIZE; 469 TEE_Result res; 470 471 DMSG("hash pg_idx %zu hash %p page %p", n, hash, page); 472 res = hash_sha256_check(hash, page, SMALL_PAGE_SIZE); 473 if (res != TEE_SUCCESS) { 474 EMSG("Hash failed for page %zu at %p: res 0x%x", 475 n, (void *)page, res); 476 panic(); 477 } 478 } 479 480 /* 481 * Assert prepaged init sections are page aligned so that nothing 482 * trails uninited at the end of the premapped init area. 483 */ 484 assert(!(init_size & SMALL_PAGE_MASK)); 485 486 /* 487 * Initialize the virtual memory pool used for main_mmu_l2_ttb which 488 * is supplied to tee_pager_init() below. 489 */ 490 init_vcore(&tee_mm_vcore); 491 492 /* 493 * Assign alias area for pager end of the small page block the rest 494 * of the binary is loaded into. We're taking more than needed, but 495 * we're guaranteed to not need more than the physical amount of 496 * TZSRAM. 497 */ 498 mm = tee_mm_alloc2(&tee_mm_vcore, 499 (vaddr_t)tee_mm_vcore.hi - TZSRAM_SIZE, TZSRAM_SIZE); 500 assert(mm); 501 tee_pager_set_alias_area(mm); 502 503 /* 504 * Claim virtual memory which isn't paged. 505 * Linear memory (flat map core memory) ends there. 506 */ 507 mm = tee_mm_alloc2(&tee_mm_vcore, VCORE_UNPG_RX_PA, 508 (vaddr_t)(__pageable_start - VCORE_UNPG_RX_PA)); 509 assert(mm); 510 511 /* 512 * Allocate virtual memory for the pageable area and let the pager 513 * take charge of all the pages already assigned to that memory. 514 */ 515 mm = tee_mm_alloc2(&tee_mm_vcore, (vaddr_t)__pageable_start, 516 pageable_size); 517 assert(mm); 518 fobj = ro_paged_alloc(mm, hashes, paged_store); 519 assert(fobj); 520 tee_pager_add_core_region(tee_mm_get_smem(mm), PAGED_REGION_TYPE_RO, 521 fobj); 522 fobj_put(fobj); 523 524 tee_pager_add_pages(pageable_start, init_size / SMALL_PAGE_SIZE, false); 525 tee_pager_add_pages(pageable_start + init_size, 526 (pageable_size - init_size) / SMALL_PAGE_SIZE, 527 true); 528 if (pageable_end < tzsram_end) 529 tee_pager_add_pages(pageable_end, (tzsram_end - pageable_end) / 530 SMALL_PAGE_SIZE, true); 531 532 /* 533 * There may be physical pages in TZSRAM before the core load address. 534 * These pages can be added to the physical pages pool of the pager. 535 * This setup may happen when a the secure bootloader runs in TZRAM 536 * and its memory can be reused by OP-TEE once boot stages complete. 537 */ 538 tee_pager_add_pages(tee_mm_vcore.lo, 539 (VCORE_UNPG_RX_PA - tee_mm_vcore.lo) / SMALL_PAGE_SIZE, 540 true); 541 542 print_pager_pool_size(); 543 } 544 #else 545 546 static void init_runtime(unsigned long pageable_part __unused) 547 { 548 init_asan(); 549 550 /* 551 * By default whole OP-TEE uses malloc, so we need to initialize 552 * it early. But, when virtualization is enabled, malloc is used 553 * only by TEE runtime, so malloc should be initialized later, for 554 * every virtual partition separately. Core code uses nex_malloc 555 * instead. 556 */ 557 #ifdef CFG_VIRTUALIZATION 558 nex_malloc_add_pool(__nex_heap_start, __nex_heap_end - 559 __nex_heap_start); 560 #else 561 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start); 562 #endif 563 564 IMSG_RAW("\n"); 565 } 566 #endif 567 568 void *get_dt(void) 569 { 570 void *fdt = get_embedded_dt(); 571 572 if (!fdt) 573 fdt = get_external_dt(); 574 575 return fdt; 576 } 577 578 #if defined(CFG_EMBED_DTB) 579 void *get_embedded_dt(void) 580 { 581 static bool checked; 582 583 assert(cpu_mmu_enabled()); 584 585 if (!checked) { 586 IMSG("Embedded DTB found"); 587 588 if (fdt_check_header(embedded_secure_dtb)) 589 panic("Invalid embedded DTB"); 590 591 checked = true; 592 } 593 594 return embedded_secure_dtb; 595 } 596 #else 597 void *get_embedded_dt(void) 598 { 599 return NULL; 600 } 601 #endif /*CFG_EMBED_DTB*/ 602 603 #if defined(CFG_DT) 604 void *get_external_dt(void) 605 { 606 assert(cpu_mmu_enabled()); 607 return external_dt.blob; 608 } 609 610 static TEE_Result release_external_dt(void) 611 { 612 int ret = 0; 613 614 if (!external_dt.blob) 615 return TEE_SUCCESS; 616 617 ret = fdt_pack(external_dt.blob); 618 if (ret < 0) { 619 EMSG("Failed to pack Device Tree at 0x%" PRIxPA ": error %d", 620 virt_to_phys(external_dt.blob), ret); 621 panic(); 622 } 623 624 if (core_mmu_remove_mapping(MEM_AREA_EXT_DT, external_dt.blob, 625 CFG_DTB_MAX_SIZE)) 626 panic("Failed to remove temporary Device Tree mapping"); 627 628 /* External DTB no more reached, reset pointer to invalid */ 629 external_dt.blob = NULL; 630 631 return TEE_SUCCESS; 632 } 633 boot_final(release_external_dt); 634 635 #ifdef CFG_EXTERNAL_DTB_OVERLAY 636 static int add_dt_overlay_fragment(struct dt_descriptor *dt, int ioffs) 637 { 638 char frag[32]; 639 int offs; 640 int ret; 641 642 snprintf(frag, sizeof(frag), "fragment@%d", dt->frag_id); 643 offs = fdt_add_subnode(dt->blob, ioffs, frag); 644 if (offs < 0) 645 return offs; 646 647 dt->frag_id += 1; 648 649 ret = fdt_setprop_string(dt->blob, offs, "target-path", "/"); 650 if (ret < 0) 651 return -1; 652 653 return fdt_add_subnode(dt->blob, offs, "__overlay__"); 654 } 655 656 static int init_dt_overlay(struct dt_descriptor *dt, int __maybe_unused dt_size) 657 { 658 int fragment; 659 int ret; 660 661 ret = fdt_check_header(dt->blob); 662 if (!ret) { 663 fdt_for_each_subnode(fragment, dt->blob, 0) 664 dt->frag_id += 1; 665 return ret; 666 } 667 668 #ifdef CFG_DT_ADDR 669 return fdt_create_empty_tree(dt->blob, dt_size); 670 #else 671 return -1; 672 #endif 673 } 674 #else 675 static int add_dt_overlay_fragment(struct dt_descriptor *dt __unused, int offs) 676 { 677 return offs; 678 } 679 680 static int init_dt_overlay(struct dt_descriptor *dt __unused, 681 int dt_size __unused) 682 { 683 return 0; 684 } 685 #endif /* CFG_EXTERNAL_DTB_OVERLAY */ 686 687 static int add_dt_path_subnode(struct dt_descriptor *dt, const char *path, 688 const char *subnode) 689 { 690 int offs; 691 692 offs = fdt_path_offset(dt->blob, path); 693 if (offs < 0) 694 return -1; 695 offs = add_dt_overlay_fragment(dt, offs); 696 if (offs < 0) 697 return -1; 698 offs = fdt_add_subnode(dt->blob, offs, subnode); 699 if (offs < 0) 700 return -1; 701 return offs; 702 } 703 704 static int add_optee_dt_node(struct dt_descriptor *dt) 705 { 706 int offs; 707 int ret; 708 709 if (fdt_path_offset(dt->blob, "/firmware/optee") >= 0) { 710 DMSG("OP-TEE Device Tree node already exists!"); 711 return 0; 712 } 713 714 offs = fdt_path_offset(dt->blob, "/firmware"); 715 if (offs < 0) { 716 offs = add_dt_path_subnode(dt, "/", "firmware"); 717 if (offs < 0) 718 return -1; 719 } 720 721 offs = fdt_add_subnode(dt->blob, offs, "optee"); 722 if (offs < 0) 723 return -1; 724 725 ret = fdt_setprop_string(dt->blob, offs, "compatible", 726 "linaro,optee-tz"); 727 if (ret < 0) 728 return -1; 729 ret = fdt_setprop_string(dt->blob, offs, "method", "smc"); 730 if (ret < 0) 731 return -1; 732 return 0; 733 } 734 735 #ifdef CFG_PSCI_ARM32 736 static int append_psci_compatible(void *fdt, int offs, const char *str) 737 { 738 return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1); 739 } 740 741 static int dt_add_psci_node(struct dt_descriptor *dt) 742 { 743 int offs; 744 745 if (fdt_path_offset(dt->blob, "/psci") >= 0) { 746 DMSG("PSCI Device Tree node already exists!"); 747 return 0; 748 } 749 750 offs = add_dt_path_subnode(dt, "/", "psci"); 751 if (offs < 0) 752 return -1; 753 if (append_psci_compatible(dt->blob, offs, "arm,psci-1.0")) 754 return -1; 755 if (append_psci_compatible(dt->blob, offs, "arm,psci-0.2")) 756 return -1; 757 if (append_psci_compatible(dt->blob, offs, "arm,psci")) 758 return -1; 759 if (fdt_setprop_string(dt->blob, offs, "method", "smc")) 760 return -1; 761 if (fdt_setprop_u32(dt->blob, offs, "cpu_suspend", PSCI_CPU_SUSPEND)) 762 return -1; 763 if (fdt_setprop_u32(dt->blob, offs, "cpu_off", PSCI_CPU_OFF)) 764 return -1; 765 if (fdt_setprop_u32(dt->blob, offs, "cpu_on", PSCI_CPU_ON)) 766 return -1; 767 if (fdt_setprop_u32(dt->blob, offs, "sys_poweroff", PSCI_SYSTEM_OFF)) 768 return -1; 769 if (fdt_setprop_u32(dt->blob, offs, "sys_reset", PSCI_SYSTEM_RESET)) 770 return -1; 771 return 0; 772 } 773 774 static int check_node_compat_prefix(struct dt_descriptor *dt, int offs, 775 const char *prefix) 776 { 777 const size_t prefix_len = strlen(prefix); 778 size_t l; 779 int plen; 780 const char *prop; 781 782 prop = fdt_getprop(dt->blob, offs, "compatible", &plen); 783 if (!prop) 784 return -1; 785 786 while (plen > 0) { 787 if (memcmp(prop, prefix, prefix_len) == 0) 788 return 0; /* match */ 789 790 l = strlen(prop) + 1; 791 prop += l; 792 plen -= l; 793 } 794 795 return -1; 796 } 797 798 static int dt_add_psci_cpu_enable_methods(struct dt_descriptor *dt) 799 { 800 int offs = 0; 801 802 while (1) { 803 offs = fdt_next_node(dt->blob, offs, NULL); 804 if (offs < 0) 805 break; 806 if (fdt_getprop(dt->blob, offs, "enable-method", NULL)) 807 continue; /* already set */ 808 if (check_node_compat_prefix(dt, offs, "arm,cortex-a")) 809 continue; /* no compatible */ 810 if (fdt_setprop_string(dt->blob, offs, "enable-method", "psci")) 811 return -1; 812 /* Need to restart scanning as offsets may have changed */ 813 offs = 0; 814 } 815 return 0; 816 } 817 818 static int config_psci(struct dt_descriptor *dt) 819 { 820 if (dt_add_psci_node(dt)) 821 return -1; 822 return dt_add_psci_cpu_enable_methods(dt); 823 } 824 #else 825 static int config_psci(struct dt_descriptor *dt __unused) 826 { 827 return 0; 828 } 829 #endif /*CFG_PSCI_ARM32*/ 830 831 static void set_dt_val(void *data, uint32_t cell_size, uint64_t val) 832 { 833 if (cell_size == 1) { 834 fdt32_t v = cpu_to_fdt32((uint32_t)val); 835 836 memcpy(data, &v, sizeof(v)); 837 } else { 838 fdt64_t v = cpu_to_fdt64(val); 839 840 memcpy(data, &v, sizeof(v)); 841 } 842 } 843 844 static int add_res_mem_dt_node(struct dt_descriptor *dt, const char *name, 845 paddr_t pa, size_t size) 846 { 847 int offs = 0; 848 int ret = 0; 849 int addr_size = -1; 850 int len_size = -1; 851 bool found = true; 852 char subnode_name[80] = { 0 }; 853 854 offs = fdt_path_offset(dt->blob, "/reserved-memory"); 855 856 if (offs < 0) { 857 found = false; 858 offs = 0; 859 } 860 861 if (IS_ENABLED(CFG_EXTERNAL_DTB_OVERLAY)) { 862 len_size = sizeof(paddr_t) / sizeof(uint32_t); 863 addr_size = sizeof(paddr_t) / sizeof(uint32_t); 864 } else { 865 len_size = fdt_size_cells(dt->blob, offs); 866 if (len_size < 0) 867 return -1; 868 addr_size = fdt_address_cells(dt->blob, offs); 869 if (addr_size < 0) 870 return -1; 871 } 872 873 if (!found) { 874 offs = add_dt_path_subnode(dt, "/", "reserved-memory"); 875 if (offs < 0) 876 return -1; 877 ret = fdt_setprop_cell(dt->blob, offs, "#address-cells", 878 addr_size); 879 if (ret < 0) 880 return -1; 881 ret = fdt_setprop_cell(dt->blob, offs, "#size-cells", len_size); 882 if (ret < 0) 883 return -1; 884 ret = fdt_setprop(dt->blob, offs, "ranges", NULL, 0); 885 if (ret < 0) 886 return -1; 887 } 888 889 ret = snprintf(subnode_name, sizeof(subnode_name), 890 "%s@0x%" PRIxPA, name, pa); 891 if (ret < 0 || ret >= (int)sizeof(subnode_name)) 892 DMSG("truncated node \"%s@0x%"PRIxPA"\"", name, pa); 893 offs = fdt_add_subnode(dt->blob, offs, subnode_name); 894 if (offs >= 0) { 895 uint32_t data[FDT_MAX_NCELLS * 2]; 896 897 set_dt_val(data, addr_size, pa); 898 set_dt_val(data + addr_size, len_size, size); 899 ret = fdt_setprop(dt->blob, offs, "reg", data, 900 sizeof(uint32_t) * (addr_size + len_size)); 901 if (ret < 0) 902 return -1; 903 ret = fdt_setprop(dt->blob, offs, "no-map", NULL, 0); 904 if (ret < 0) 905 return -1; 906 } else { 907 return -1; 908 } 909 return 0; 910 } 911 912 #ifdef CFG_CORE_DYN_SHM 913 static uint64_t get_dt_val_and_advance(const void *data, size_t *offs, 914 uint32_t cell_size) 915 { 916 uint64_t rv = 0; 917 918 if (cell_size == 1) { 919 uint32_t v; 920 921 memcpy(&v, (const uint8_t *)data + *offs, sizeof(v)); 922 *offs += sizeof(v); 923 rv = fdt32_to_cpu(v); 924 } else { 925 uint64_t v; 926 927 memcpy(&v, (const uint8_t *)data + *offs, sizeof(v)); 928 *offs += sizeof(v); 929 rv = fdt64_to_cpu(v); 930 } 931 932 return rv; 933 } 934 935 /* 936 * Find all non-secure memory from DT. Memory marked inaccessible by Secure 937 * World is ignored since it could not be mapped to be used as dynamic shared 938 * memory. 939 */ 940 static int get_nsec_memory_helper(void *fdt, struct core_mmu_phys_mem *mem) 941 { 942 const uint8_t *prop = NULL; 943 uint64_t a = 0; 944 uint64_t l = 0; 945 size_t prop_offs = 0; 946 size_t prop_len = 0; 947 int elems_total = 0; 948 int addr_size = 0; 949 int len_size = 0; 950 int offs = 0; 951 size_t n = 0; 952 int len = 0; 953 954 addr_size = fdt_address_cells(fdt, 0); 955 if (addr_size < 0) 956 return 0; 957 958 len_size = fdt_size_cells(fdt, 0); 959 if (len_size < 0) 960 return 0; 961 962 while (true) { 963 offs = fdt_node_offset_by_prop_value(fdt, offs, "device_type", 964 "memory", 965 sizeof("memory")); 966 if (offs < 0) 967 break; 968 969 if (_fdt_get_status(fdt, offs) != (DT_STATUS_OK_NSEC | 970 DT_STATUS_OK_SEC)) 971 continue; 972 973 prop = fdt_getprop(fdt, offs, "reg", &len); 974 if (!prop) 975 continue; 976 977 prop_len = len; 978 for (n = 0, prop_offs = 0; prop_offs < prop_len; n++) { 979 a = get_dt_val_and_advance(prop, &prop_offs, addr_size); 980 if (prop_offs >= prop_len) { 981 n--; 982 break; 983 } 984 985 l = get_dt_val_and_advance(prop, &prop_offs, len_size); 986 if (mem) { 987 mem->type = MEM_AREA_DDR_OVERALL; 988 mem->addr = a; 989 mem->size = l; 990 mem++; 991 } 992 } 993 994 elems_total += n; 995 } 996 997 return elems_total; 998 } 999 1000 static struct core_mmu_phys_mem *get_nsec_memory(void *fdt, size_t *nelems) 1001 { 1002 struct core_mmu_phys_mem *mem = NULL; 1003 int elems_total = 0; 1004 1005 elems_total = get_nsec_memory_helper(fdt, NULL); 1006 if (elems_total <= 0) 1007 return NULL; 1008 1009 mem = nex_calloc(elems_total, sizeof(*mem)); 1010 if (!mem) 1011 panic(); 1012 1013 elems_total = get_nsec_memory_helper(fdt, mem); 1014 assert(elems_total > 0); 1015 1016 *nelems = elems_total; 1017 1018 return mem; 1019 } 1020 #endif /*CFG_CORE_DYN_SHM*/ 1021 1022 #ifdef CFG_CORE_RESERVED_SHM 1023 static int mark_static_shm_as_reserved(struct dt_descriptor *dt) 1024 { 1025 vaddr_t shm_start; 1026 vaddr_t shm_end; 1027 1028 core_mmu_get_mem_by_type(MEM_AREA_NSEC_SHM, &shm_start, &shm_end); 1029 if (shm_start != shm_end) 1030 return add_res_mem_dt_node(dt, "optee_shm", 1031 virt_to_phys((void *)shm_start), 1032 shm_end - shm_start); 1033 1034 DMSG("No SHM configured"); 1035 return -1; 1036 } 1037 #endif /*CFG_CORE_RESERVED_SHM*/ 1038 1039 static void init_external_dt(unsigned long phys_dt) 1040 { 1041 struct dt_descriptor *dt = &external_dt; 1042 void *fdt; 1043 int ret; 1044 1045 if (!phys_dt) { 1046 /* 1047 * No need to panic as we're not using the DT in OP-TEE 1048 * yet, we're only adding some nodes for normal world use. 1049 * This makes the switch to using DT easier as we can boot 1050 * a newer OP-TEE with older boot loaders. Once we start to 1051 * initialize devices based on DT we'll likely panic 1052 * instead of returning here. 1053 */ 1054 IMSG("No non-secure external DT"); 1055 return; 1056 } 1057 1058 fdt = core_mmu_add_mapping(MEM_AREA_EXT_DT, phys_dt, CFG_DTB_MAX_SIZE); 1059 if (!fdt) 1060 panic("Failed to map external DTB"); 1061 1062 dt->blob = fdt; 1063 1064 ret = init_dt_overlay(dt, CFG_DTB_MAX_SIZE); 1065 if (ret < 0) { 1066 EMSG("Device Tree Overlay init fail @ %#lx: error %d", phys_dt, 1067 ret); 1068 panic(); 1069 } 1070 1071 ret = fdt_open_into(fdt, fdt, CFG_DTB_MAX_SIZE); 1072 if (ret < 0) { 1073 EMSG("Invalid Device Tree at %#lx: error %d", phys_dt, ret); 1074 panic(); 1075 } 1076 1077 IMSG("Non-secure external DT found"); 1078 } 1079 1080 static int mark_tzdram_as_reserved(struct dt_descriptor *dt) 1081 { 1082 return add_res_mem_dt_node(dt, "optee_core", CFG_TZDRAM_START, 1083 CFG_TZDRAM_SIZE); 1084 } 1085 1086 static void update_external_dt(void) 1087 { 1088 struct dt_descriptor *dt = &external_dt; 1089 1090 if (!dt->blob) 1091 return; 1092 1093 if (!IS_ENABLED(CFG_CORE_FFA) && add_optee_dt_node(dt)) 1094 panic("Failed to add OP-TEE Device Tree node"); 1095 1096 if (config_psci(dt)) 1097 panic("Failed to config PSCI"); 1098 1099 #ifdef CFG_CORE_RESERVED_SHM 1100 if (mark_static_shm_as_reserved(dt)) 1101 panic("Failed to config non-secure memory"); 1102 #endif 1103 1104 if (mark_tzdram_as_reserved(dt)) 1105 panic("Failed to config secure memory"); 1106 } 1107 #else /*CFG_DT*/ 1108 void *get_external_dt(void) 1109 { 1110 return NULL; 1111 } 1112 1113 static void init_external_dt(unsigned long phys_dt __unused) 1114 { 1115 } 1116 1117 static void update_external_dt(void) 1118 { 1119 } 1120 1121 #ifdef CFG_CORE_DYN_SHM 1122 static struct core_mmu_phys_mem *get_nsec_memory(void *fdt __unused, 1123 size_t *nelems __unused) 1124 { 1125 return NULL; 1126 } 1127 #endif /*CFG_CORE_DYN_SHM*/ 1128 #endif /*!CFG_DT*/ 1129 1130 #ifdef CFG_CORE_DYN_SHM 1131 static void discover_nsec_memory(void) 1132 { 1133 struct core_mmu_phys_mem *mem; 1134 const struct core_mmu_phys_mem *mem_begin = NULL; 1135 const struct core_mmu_phys_mem *mem_end = NULL; 1136 size_t nelems; 1137 void *fdt = get_external_dt(); 1138 1139 if (fdt) { 1140 mem = get_nsec_memory(fdt, &nelems); 1141 if (mem) { 1142 core_mmu_set_discovered_nsec_ddr(mem, nelems); 1143 return; 1144 } 1145 1146 DMSG("No non-secure memory found in FDT"); 1147 } 1148 1149 mem_begin = phys_ddr_overall_begin; 1150 mem_end = phys_ddr_overall_end; 1151 nelems = mem_end - mem_begin; 1152 if (nelems) { 1153 /* 1154 * Platform cannot use both register_ddr() and the now 1155 * deprecated register_dynamic_shm(). 1156 */ 1157 assert(phys_ddr_overall_compat_begin == 1158 phys_ddr_overall_compat_end); 1159 } else { 1160 mem_begin = phys_ddr_overall_compat_begin; 1161 mem_end = phys_ddr_overall_compat_end; 1162 nelems = mem_end - mem_begin; 1163 if (!nelems) 1164 return; 1165 DMSG("Warning register_dynamic_shm() is deprecated, please use register_ddr() instead"); 1166 } 1167 1168 mem = nex_calloc(nelems, sizeof(*mem)); 1169 if (!mem) 1170 panic(); 1171 1172 memcpy(mem, phys_ddr_overall_begin, sizeof(*mem) * nelems); 1173 core_mmu_set_discovered_nsec_ddr(mem, nelems); 1174 } 1175 #else /*CFG_CORE_DYN_SHM*/ 1176 static void discover_nsec_memory(void) 1177 { 1178 } 1179 #endif /*!CFG_CORE_DYN_SHM*/ 1180 1181 void init_tee_runtime(void) 1182 { 1183 #ifdef CFG_VIRTUALIZATION 1184 /* We need to initialize pool for every virtual guest partition */ 1185 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start); 1186 #endif 1187 1188 #ifndef CFG_WITH_PAGER 1189 /* Pager initializes TA RAM early */ 1190 core_mmu_init_ta_ram(); 1191 #endif 1192 call_initcalls(); 1193 } 1194 1195 static void init_primary(unsigned long pageable_part, unsigned long nsec_entry) 1196 { 1197 /* 1198 * Mask asynchronous exceptions before switch to the thread vector 1199 * as the thread handler requires those to be masked while 1200 * executing with the temporary stack. The thread subsystem also 1201 * asserts that the foreign interrupts are blocked when using most of 1202 * its functions. 1203 */ 1204 thread_set_exceptions(THREAD_EXCP_ALL); 1205 primary_save_cntfrq(); 1206 init_vfp_sec(); 1207 /* 1208 * Pager: init_runtime() calls thread_kernel_enable_vfp() so we must 1209 * set a current thread right now to avoid a chicken-and-egg problem 1210 * (thread_init_boot_thread() sets the current thread but needs 1211 * things set by init_runtime()). 1212 */ 1213 thread_get_core_local()->curr_thread = 0; 1214 init_runtime(pageable_part); 1215 1216 if (IS_ENABLED(CFG_VIRTUALIZATION)) { 1217 /* 1218 * Virtualization: We can't initialize threads right now because 1219 * threads belong to "tee" part and will be initialized 1220 * separately per each new virtual guest. So, we'll clear 1221 * "curr_thread" and call it done. 1222 */ 1223 thread_get_core_local()->curr_thread = -1; 1224 } else { 1225 thread_init_boot_thread(); 1226 } 1227 thread_init_primary(); 1228 thread_init_per_cpu(); 1229 init_sec_mon(nsec_entry); 1230 } 1231 1232 /* 1233 * Note: this function is weak just to make it possible to exclude it from 1234 * the unpaged area. 1235 */ 1236 void __weak boot_init_primary_late(unsigned long fdt) 1237 { 1238 init_external_dt(fdt); 1239 tpm_map_log_area(get_external_dt()); 1240 discover_nsec_memory(); 1241 update_external_dt(); 1242 configure_console_from_dt(); 1243 1244 IMSG("OP-TEE version: %s", core_v_str); 1245 IMSG("Primary CPU initializing"); 1246 #ifdef CFG_CORE_ASLR 1247 DMSG("Executing at offset %#lx with virtual load address %#"PRIxVA, 1248 (unsigned long)boot_mmu_config.load_offset, VCORE_START_VA); 1249 #endif 1250 1251 main_init_gic(); 1252 init_vfp_nsec(); 1253 #ifndef CFG_VIRTUALIZATION 1254 init_tee_runtime(); 1255 #endif 1256 #ifdef CFG_VIRTUALIZATION 1257 IMSG("Initializing virtualization support"); 1258 core_mmu_init_virtualization(); 1259 #endif 1260 call_finalcalls(); 1261 IMSG("Primary CPU switching to normal world boot"); 1262 } 1263 1264 static void init_secondary_helper(unsigned long nsec_entry) 1265 { 1266 IMSG("Secondary CPU %zu initializing", get_core_pos()); 1267 1268 /* 1269 * Mask asynchronous exceptions before switch to the thread vector 1270 * as the thread handler requires those to be masked while 1271 * executing with the temporary stack. The thread subsystem also 1272 * asserts that the foreign interrupts are blocked when using most of 1273 * its functions. 1274 */ 1275 thread_set_exceptions(THREAD_EXCP_ALL); 1276 1277 secondary_init_cntfrq(); 1278 thread_init_per_cpu(); 1279 init_sec_mon(nsec_entry); 1280 main_secondary_init_gic(); 1281 init_vfp_sec(); 1282 init_vfp_nsec(); 1283 1284 IMSG("Secondary CPU %zu switching to normal world boot", get_core_pos()); 1285 } 1286 1287 /* 1288 * Note: this function is weak just to make it possible to exclude it from 1289 * the unpaged area so that it lies in the init area. 1290 */ 1291 void __weak boot_init_primary_early(unsigned long pageable_part, 1292 unsigned long nsec_entry __maybe_unused) 1293 { 1294 unsigned long e = PADDR_INVALID; 1295 1296 #if !defined(CFG_WITH_ARM_TRUSTED_FW) 1297 e = nsec_entry; 1298 #endif 1299 1300 init_primary(pageable_part, e); 1301 } 1302 1303 #if defined(CFG_WITH_ARM_TRUSTED_FW) 1304 unsigned long boot_cpu_on_handler(unsigned long a0 __maybe_unused, 1305 unsigned long a1 __unused) 1306 { 1307 init_secondary_helper(PADDR_INVALID); 1308 return 0; 1309 } 1310 #else 1311 void boot_init_secondary(unsigned long nsec_entry) 1312 { 1313 init_secondary_helper(nsec_entry); 1314 } 1315 #endif 1316 1317 #if defined(CFG_BOOT_SECONDARY_REQUEST) 1318 void boot_set_core_ns_entry(size_t core_idx, uintptr_t entry, 1319 uintptr_t context_id) 1320 { 1321 ns_entry_contexts[core_idx].entry_point = entry; 1322 ns_entry_contexts[core_idx].context_id = context_id; 1323 dsb_ishst(); 1324 } 1325 1326 int boot_core_release(size_t core_idx, paddr_t entry) 1327 { 1328 if (!core_idx || core_idx >= CFG_TEE_CORE_NB_CORE) 1329 return -1; 1330 1331 ns_entry_contexts[core_idx].entry_point = entry; 1332 dmb(); 1333 spin_table[core_idx] = 1; 1334 dsb(); 1335 sev(); 1336 1337 return 0; 1338 } 1339 1340 /* 1341 * spin until secondary boot request, then returns with 1342 * the secondary core entry address. 1343 */ 1344 struct ns_entry_context *boot_core_hpen(void) 1345 { 1346 #ifdef CFG_PSCI_ARM32 1347 return &ns_entry_contexts[get_core_pos()]; 1348 #else 1349 do { 1350 wfe(); 1351 } while (!spin_table[get_core_pos()]); 1352 dmb(); 1353 return &ns_entry_contexts[get_core_pos()]; 1354 #endif 1355 } 1356 #endif 1357 1358 #if defined(CFG_CORE_ASLR) 1359 #if defined(CFG_DT) 1360 unsigned long __weak get_aslr_seed(void *fdt) 1361 { 1362 int rc = fdt_check_header(fdt); 1363 const uint64_t *seed = NULL; 1364 int offs = 0; 1365 int len = 0; 1366 1367 if (rc) { 1368 DMSG("Bad fdt: %d", rc); 1369 goto err; 1370 } 1371 1372 offs = fdt_path_offset(fdt, "/secure-chosen"); 1373 if (offs < 0) { 1374 DMSG("Cannot find /secure-chosen"); 1375 goto err; 1376 } 1377 seed = fdt_getprop(fdt, offs, "kaslr-seed", &len); 1378 if (!seed || len != sizeof(*seed)) { 1379 DMSG("Cannot find valid kaslr-seed"); 1380 goto err; 1381 } 1382 1383 return fdt64_to_cpu(*seed); 1384 1385 err: 1386 /* Try platform implementation */ 1387 return plat_get_aslr_seed(); 1388 } 1389 #else /*!CFG_DT*/ 1390 unsigned long __weak get_aslr_seed(void *fdt __unused) 1391 { 1392 /* Try platform implementation */ 1393 return plat_get_aslr_seed(); 1394 } 1395 #endif /*!CFG_DT*/ 1396 #endif /*CFG_CORE_ASLR*/ 1397