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