1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2015-2023, Linaro Limited 4 * Copyright (c) 2023, Arm Limited 5 */ 6 7 #include <arm.h> 8 #include <assert.h> 9 #include <compiler.h> 10 #include <config.h> 11 #include <console.h> 12 #include <crypto/crypto.h> 13 #include <drivers/gic.h> 14 #include <dt-bindings/interrupt-controller/arm-gic.h> 15 #include <ffa.h> 16 #include <initcall.h> 17 #include <inttypes.h> 18 #include <io.h> 19 #include <keep.h> 20 #include <kernel/asan.h> 21 #include <kernel/boot.h> 22 #include <kernel/dt.h> 23 #include <kernel/linker.h> 24 #include <kernel/misc.h> 25 #include <kernel/panic.h> 26 #include <kernel/tee_misc.h> 27 #include <kernel/thread.h> 28 #include <kernel/tpm.h> 29 #include <kernel/transfer_list.h> 30 #include <libfdt.h> 31 #include <malloc.h> 32 #include <memtag.h> 33 #include <mm/core_memprot.h> 34 #include <mm/core_mmu.h> 35 #include <mm/fobj.h> 36 #include <mm/tee_mm.h> 37 #include <mm/tee_pager.h> 38 #include <sm/psci.h> 39 #include <trace.h> 40 #include <utee_defines.h> 41 #include <util.h> 42 43 #include <platform_config.h> 44 45 #if !defined(CFG_WITH_ARM_TRUSTED_FW) 46 #include <sm/sm.h> 47 #endif 48 49 #if defined(CFG_WITH_VFP) 50 #include <kernel/vfp.h> 51 #endif 52 53 /* 54 * In this file we're using unsigned long to represent physical pointers as 55 * they are received in a single register when OP-TEE is initially entered. 56 * This limits 32-bit systems to only use make use of the lower 32 bits 57 * of a physical address for initial parameters. 58 * 59 * 64-bit systems on the other hand can use full 64-bit physical pointers. 60 */ 61 #define PADDR_INVALID ULONG_MAX 62 63 #if defined(CFG_BOOT_SECONDARY_REQUEST) 64 struct ns_entry_context { 65 uintptr_t entry_point; 66 uintptr_t context_id; 67 }; 68 struct ns_entry_context ns_entry_contexts[CFG_TEE_CORE_NB_CORE]; 69 static uint32_t spin_table[CFG_TEE_CORE_NB_CORE]; 70 #endif 71 72 #ifdef CFG_BOOT_SYNC_CPU 73 /* 74 * Array used when booting, to synchronize cpu. 75 * When 0, the cpu has not started. 76 * When 1, it has started 77 */ 78 uint32_t sem_cpu_sync[CFG_TEE_CORE_NB_CORE]; 79 DECLARE_KEEP_PAGER(sem_cpu_sync); 80 #endif 81 82 static unsigned long boot_arg_fdt __nex_bss; 83 static unsigned long boot_arg_nsec_entry __nex_bss; 84 static unsigned long boot_arg_pageable_part __nex_bss; 85 static unsigned long boot_arg_transfer_list __nex_bss; 86 static struct transfer_list_header *mapped_tl __nex_bss; 87 88 #ifdef CFG_SECONDARY_INIT_CNTFRQ 89 static uint32_t cntfrq; 90 #endif 91 92 /* May be overridden in plat-$(PLATFORM)/main.c */ 93 __weak void plat_primary_init_early(void) 94 { 95 } 96 DECLARE_KEEP_PAGER(plat_primary_init_early); 97 98 /* May be overridden in plat-$(PLATFORM)/main.c */ 99 __weak void boot_primary_init_intc(void) 100 { 101 } 102 103 /* May be overridden in plat-$(PLATFORM)/main.c */ 104 __weak void boot_secondary_init_intc(void) 105 { 106 } 107 108 /* May be overridden in plat-$(PLATFORM)/main.c */ 109 __weak unsigned long plat_get_aslr_seed(void) 110 { 111 DMSG("Warning: no ASLR seed"); 112 113 return 0; 114 } 115 116 #if defined(_CFG_CORE_STACK_PROTECTOR) || defined(CFG_WITH_STACK_CANARIES) 117 /* Generate random stack canary value on boot up */ 118 __weak void plat_get_random_stack_canaries(void *buf, size_t ncan, size_t size) 119 { 120 TEE_Result ret = TEE_ERROR_GENERIC; 121 size_t i = 0; 122 123 assert(buf && ncan && size); 124 125 /* 126 * With virtualization the RNG is not initialized in Nexus core. 127 * Need to override with platform specific implementation. 128 */ 129 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) { 130 IMSG("WARNING: Using fixed value for stack canary"); 131 memset(buf, 0xab, ncan * size); 132 goto out; 133 } 134 135 ret = crypto_rng_read(buf, ncan * size); 136 if (ret != TEE_SUCCESS) 137 panic("Failed to generate random stack canary"); 138 139 out: 140 /* Leave null byte in canary to prevent string base exploit */ 141 for (i = 0; i < ncan; i++) 142 *((uint8_t *)buf + size * i) = 0; 143 } 144 #endif /* _CFG_CORE_STACK_PROTECTOR || CFG_WITH_STACK_CANARIES */ 145 146 /* 147 * This function is called as a guard after each smc call which is not 148 * supposed to return. 149 */ 150 void __panic_at_smc_return(void) 151 { 152 panic(); 153 } 154 155 #if defined(CFG_WITH_ARM_TRUSTED_FW) 156 void init_sec_mon(unsigned long nsec_entry __maybe_unused) 157 { 158 assert(nsec_entry == PADDR_INVALID); 159 /* Do nothing as we don't have a secure monitor */ 160 } 161 #else 162 /* May be overridden in plat-$(PLATFORM)/main.c */ 163 __weak void init_sec_mon(unsigned long nsec_entry) 164 { 165 struct sm_nsec_ctx *nsec_ctx; 166 167 assert(nsec_entry != PADDR_INVALID); 168 169 /* Initialize secure monitor */ 170 nsec_ctx = sm_get_nsec_ctx(); 171 nsec_ctx->mon_lr = nsec_entry; 172 nsec_ctx->mon_spsr = CPSR_MODE_SVC | CPSR_I; 173 if (nsec_entry & 1) 174 nsec_ctx->mon_spsr |= CPSR_T; 175 } 176 #endif 177 178 #if defined(CFG_WITH_ARM_TRUSTED_FW) 179 static void init_vfp_nsec(void) 180 { 181 } 182 #else 183 static void init_vfp_nsec(void) 184 { 185 /* Normal world can use CP10 and CP11 (SIMD/VFP) */ 186 write_nsacr(read_nsacr() | NSACR_CP10 | NSACR_CP11); 187 } 188 #endif 189 190 static void check_crypto_extensions(void) 191 { 192 bool ce_supported = true; 193 194 if (!feat_aes_implemented() && 195 IS_ENABLED(CFG_CRYPTO_AES_ARM_CE)) { 196 EMSG("AES instructions are not supported"); 197 ce_supported = false; 198 } 199 200 if (!feat_sha1_implemented() && 201 IS_ENABLED(CFG_CRYPTO_SHA1_ARM_CE)) { 202 EMSG("SHA1 instructions are not supported"); 203 ce_supported = false; 204 } 205 206 if (!feat_sha256_implemented() && 207 IS_ENABLED(CFG_CRYPTO_SHA256_ARM_CE)) { 208 EMSG("SHA256 instructions are not supported"); 209 ce_supported = false; 210 } 211 212 /* Check aarch64 specific instructions */ 213 if (IS_ENABLED(CFG_ARM64_core)) { 214 if (!feat_sha512_implemented() && 215 IS_ENABLED(CFG_CRYPTO_SHA512_ARM_CE)) { 216 EMSG("SHA512 instructions are not supported"); 217 ce_supported = false; 218 } 219 220 if (!feat_sha3_implemented() && 221 IS_ENABLED(CFG_CRYPTO_SHA3_ARM_CE)) { 222 EMSG("SHA3 instructions are not supported"); 223 ce_supported = false; 224 } 225 226 if (!feat_sm3_implemented() && 227 IS_ENABLED(CFG_CRYPTO_SM3_ARM_CE)) { 228 EMSG("SM3 instructions are not supported"); 229 ce_supported = false; 230 } 231 232 if (!feat_sm4_implemented() && 233 IS_ENABLED(CFG_CRYPTO_SM4_ARM_CE)) { 234 EMSG("SM4 instructions are not supported"); 235 ce_supported = false; 236 } 237 } 238 239 if (!ce_supported) 240 panic("HW doesn't support CE instructions"); 241 } 242 243 #if defined(CFG_WITH_VFP) 244 245 #ifdef ARM32 246 static void init_vfp_sec(void) 247 { 248 uint32_t cpacr = read_cpacr(); 249 250 /* 251 * Enable Advanced SIMD functionality. 252 * Enable use of D16-D31 of the Floating-point Extension register 253 * file. 254 */ 255 cpacr &= ~(CPACR_ASEDIS | CPACR_D32DIS); 256 /* 257 * Enable usage of CP10 and CP11 (SIMD/VFP) (both kernel and user 258 * mode. 259 */ 260 cpacr |= CPACR_CP(10, CPACR_CP_ACCESS_FULL); 261 cpacr |= CPACR_CP(11, CPACR_CP_ACCESS_FULL); 262 write_cpacr(cpacr); 263 } 264 #endif /* ARM32 */ 265 266 #ifdef ARM64 267 static void init_vfp_sec(void) 268 { 269 /* Not using VFP until thread_kernel_enable_vfp() */ 270 vfp_disable(); 271 } 272 #endif /* ARM64 */ 273 274 #else /* CFG_WITH_VFP */ 275 276 static void init_vfp_sec(void) 277 { 278 /* Not using VFP */ 279 } 280 #endif 281 282 #ifdef CFG_SECONDARY_INIT_CNTFRQ 283 static void primary_save_cntfrq(void) 284 { 285 assert(cntfrq == 0); 286 287 /* 288 * CNTFRQ should be initialized on the primary CPU by a 289 * previous boot stage 290 */ 291 cntfrq = read_cntfrq(); 292 } 293 294 static void secondary_init_cntfrq(void) 295 { 296 assert(cntfrq != 0); 297 write_cntfrq(cntfrq); 298 } 299 #else /* CFG_SECONDARY_INIT_CNTFRQ */ 300 static void primary_save_cntfrq(void) 301 { 302 } 303 304 static void secondary_init_cntfrq(void) 305 { 306 } 307 #endif 308 309 #ifdef CFG_CORE_SANITIZE_KADDRESS 310 static void init_run_constructors(void) 311 { 312 const vaddr_t *ctor; 313 314 for (ctor = &__ctor_list; ctor < &__ctor_end; ctor++) 315 ((void (*)(void))(*ctor))(); 316 } 317 318 static void init_asan(void) 319 { 320 321 /* 322 * CFG_ASAN_SHADOW_OFFSET is also supplied as 323 * -fasan-shadow-offset=$(CFG_ASAN_SHADOW_OFFSET) to the compiler. 324 * Since all the needed values to calculate the value of 325 * CFG_ASAN_SHADOW_OFFSET isn't available in to make we need to 326 * calculate it in advance and hard code it into the platform 327 * conf.mk. Here where we have all the needed values we double 328 * check that the compiler is supplied the correct value. 329 */ 330 331 #define __ASAN_SHADOW_START \ 332 ROUNDUP(TEE_RAM_START + (TEE_RAM_VA_SIZE * 8) / 9 - 8, 8) 333 assert(__ASAN_SHADOW_START == (vaddr_t)&__asan_shadow_start); 334 #define __CFG_ASAN_SHADOW_OFFSET \ 335 (__ASAN_SHADOW_START - (TEE_RAM_START / 8)) 336 COMPILE_TIME_ASSERT(CFG_ASAN_SHADOW_OFFSET == __CFG_ASAN_SHADOW_OFFSET); 337 #undef __ASAN_SHADOW_START 338 #undef __CFG_ASAN_SHADOW_OFFSET 339 340 /* 341 * Assign area covered by the shadow area, everything from start up 342 * to the beginning of the shadow area. 343 */ 344 asan_set_shadowed((void *)TEE_LOAD_ADDR, &__asan_shadow_start); 345 346 /* 347 * Add access to areas that aren't opened automatically by a 348 * constructor. 349 */ 350 asan_tag_access(&__ctor_list, &__ctor_end); 351 asan_tag_access(__rodata_start, __rodata_end); 352 #ifdef CFG_WITH_PAGER 353 asan_tag_access(__pageable_start, __pageable_end); 354 #endif /*CFG_WITH_PAGER*/ 355 asan_tag_access(__nozi_start, __nozi_end); 356 #ifdef ARM32 357 asan_tag_access(__exidx_start, __exidx_end); 358 asan_tag_access(__extab_start, __extab_end); 359 #endif 360 361 init_run_constructors(); 362 363 /* Everything is tagged correctly, let's start address sanitizing. */ 364 asan_start(); 365 } 366 #else /*CFG_CORE_SANITIZE_KADDRESS*/ 367 static void init_asan(void) 368 { 369 } 370 #endif /*CFG_CORE_SANITIZE_KADDRESS*/ 371 372 #if defined(CFG_MEMTAG) 373 /* Called from entry_a64.S only when MEMTAG is configured */ 374 void boot_init_memtag(void) 375 { 376 memtag_init_ops(feat_mte_implemented()); 377 } 378 379 /* Called from entry_a64.S only when MEMTAG is configured */ 380 void boot_clear_memtag(void) 381 { 382 enum teecore_memtypes mtypes[] = { 383 MEM_AREA_TEE_RAM, MEM_AREA_TEE_RAM_RW, MEM_AREA_NEX_RAM_RO, 384 MEM_AREA_NEX_RAM_RW, MEM_AREA_TEE_ASAN, MEM_AREA_TA_RAM 385 }; 386 vaddr_t s = 0; 387 vaddr_t e = 0; 388 size_t n = 0; 389 390 for (n = 0; n < ARRAY_SIZE(mtypes); n++) { 391 core_mmu_get_mem_by_type(mtypes[n], &s, &e); 392 if (e > s) { 393 DMSG("Clearing tags for VA %#"PRIxVA"..%#"PRIxVA, 394 s, e - 1); 395 memtag_set_tags((void *)s, e - s, 0); 396 } 397 } 398 } 399 #endif 400 401 #ifdef CFG_WITH_PAGER 402 403 #ifdef CFG_CORE_SANITIZE_KADDRESS 404 static void carve_out_asan_mem(tee_mm_pool_t *pool) 405 { 406 const size_t s = pool->hi - pool->lo; 407 tee_mm_entry_t *mm; 408 paddr_t apa = ASAN_MAP_PA; 409 size_t asz = ASAN_MAP_SZ; 410 411 if (core_is_buffer_outside(apa, asz, pool->lo, s)) 412 return; 413 414 /* Reserve the shadow area */ 415 if (!core_is_buffer_inside(apa, asz, pool->lo, s)) { 416 if (apa < pool->lo) { 417 /* 418 * ASAN buffer is overlapping with the beginning of 419 * the pool. 420 */ 421 asz -= pool->lo - apa; 422 apa = pool->lo; 423 } else { 424 /* 425 * ASAN buffer is overlapping with the end of the 426 * pool. 427 */ 428 asz = pool->hi - apa; 429 } 430 } 431 mm = tee_mm_alloc2(pool, apa, asz); 432 assert(mm); 433 } 434 #else 435 static void carve_out_asan_mem(tee_mm_pool_t *pool __unused) 436 { 437 } 438 #endif 439 440 static void print_pager_pool_size(void) 441 { 442 struct tee_pager_stats __maybe_unused stats; 443 444 tee_pager_get_stats(&stats); 445 IMSG("Pager pool size: %zukB", 446 stats.npages_all * SMALL_PAGE_SIZE / 1024); 447 } 448 449 static void init_virt_pool(tee_mm_pool_t *virt_pool) 450 { 451 const vaddr_t begin = VCORE_START_VA; 452 size_t size = TEE_RAM_VA_SIZE; 453 454 #ifdef CFG_CORE_SANITIZE_KADDRESS 455 /* Carve out asan memory, flat maped after core memory */ 456 if (begin + size > ASAN_SHADOW_PA) 457 size = ASAN_MAP_PA - begin; 458 #endif 459 460 if (!tee_mm_init(virt_pool, begin, size, SMALL_PAGE_SHIFT, 461 TEE_MM_POOL_NO_FLAGS)) 462 panic("core_virt_mem_pool init failed"); 463 } 464 465 /* 466 * With CFG_CORE_ASLR=y the init part is relocated very early during boot. 467 * The init part is also paged just as the rest of the normal paged code, with 468 * the difference that it's preloaded during boot. When the backing store 469 * is configured the entire paged binary is copied in place and then also 470 * the init part. Since the init part has been relocated (references to 471 * addresses updated to compensate for the new load address) this has to be 472 * undone for the hashes of those pages to match with the original binary. 473 * 474 * If CFG_CORE_ASLR=n, nothing needs to be done as the code/ro pages are 475 * unchanged. 476 */ 477 static void undo_init_relocation(uint8_t *paged_store __maybe_unused) 478 { 479 #ifdef CFG_CORE_ASLR 480 unsigned long *ptr = NULL; 481 const uint32_t *reloc = NULL; 482 const uint32_t *reloc_end = NULL; 483 unsigned long offs = boot_mmu_config.map_offset; 484 const struct boot_embdata *embdata = (const void *)__init_end; 485 vaddr_t addr_end = (vaddr_t)__init_end - offs - TEE_LOAD_ADDR; 486 vaddr_t addr_start = (vaddr_t)__init_start - offs - TEE_LOAD_ADDR; 487 488 reloc = (const void *)((vaddr_t)embdata + embdata->reloc_offset); 489 reloc_end = reloc + embdata->reloc_len / sizeof(*reloc); 490 491 for (; reloc < reloc_end; reloc++) { 492 if (*reloc < addr_start) 493 continue; 494 if (*reloc >= addr_end) 495 break; 496 ptr = (void *)(paged_store + *reloc - addr_start); 497 *ptr -= offs; 498 } 499 #endif 500 } 501 502 static struct fobj *ro_paged_alloc(tee_mm_entry_t *mm, void *hashes, 503 void *store) 504 { 505 const unsigned int num_pages = tee_mm_get_bytes(mm) / SMALL_PAGE_SIZE; 506 #ifdef CFG_CORE_ASLR 507 unsigned int reloc_offs = (vaddr_t)__pageable_start - VCORE_START_VA; 508 const struct boot_embdata *embdata = (const void *)__init_end; 509 const void *reloc = __init_end + embdata->reloc_offset; 510 511 return fobj_ro_reloc_paged_alloc(num_pages, hashes, reloc_offs, 512 reloc, embdata->reloc_len, store); 513 #else 514 return fobj_ro_paged_alloc(num_pages, hashes, store); 515 #endif 516 } 517 518 static void init_runtime(unsigned long pageable_part) 519 { 520 size_t n; 521 size_t init_size = (size_t)(__init_end - __init_start); 522 size_t pageable_start = (size_t)__pageable_start; 523 size_t pageable_end = (size_t)__pageable_end; 524 size_t pageable_size = pageable_end - pageable_start; 525 vaddr_t tzsram_end = TZSRAM_BASE + TZSRAM_SIZE - TEE_LOAD_ADDR + 526 VCORE_START_VA; 527 size_t hash_size = (pageable_size / SMALL_PAGE_SIZE) * 528 TEE_SHA256_HASH_SIZE; 529 const struct boot_embdata *embdata = (const void *)__init_end; 530 const void *tmp_hashes = NULL; 531 tee_mm_entry_t *mm = NULL; 532 struct fobj *fobj = NULL; 533 uint8_t *paged_store = NULL; 534 uint8_t *hashes = NULL; 535 536 assert(pageable_size % SMALL_PAGE_SIZE == 0); 537 assert(embdata->total_len >= embdata->hashes_offset + 538 embdata->hashes_len); 539 assert(hash_size == embdata->hashes_len); 540 541 tmp_hashes = __init_end + embdata->hashes_offset; 542 543 init_asan(); 544 545 /* Add heap2 first as heap1 may be too small as initial bget pool */ 546 malloc_add_pool(__heap2_start, __heap2_end - __heap2_start); 547 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start); 548 549 /* 550 * This needs to be initialized early to support address lookup 551 * in MEM_AREA_TEE_RAM 552 */ 553 tee_pager_early_init(); 554 555 hashes = malloc(hash_size); 556 IMSG_RAW("\n"); 557 IMSG("Pager is enabled. Hashes: %zu bytes", hash_size); 558 assert(hashes); 559 asan_memcpy_unchecked(hashes, tmp_hashes, hash_size); 560 561 /* 562 * Need tee_mm_sec_ddr initialized to be able to allocate secure 563 * DDR below. 564 */ 565 core_mmu_init_ta_ram(); 566 567 carve_out_asan_mem(&tee_mm_sec_ddr); 568 569 mm = tee_mm_alloc(&tee_mm_sec_ddr, pageable_size); 570 assert(mm); 571 paged_store = phys_to_virt(tee_mm_get_smem(mm), MEM_AREA_TA_RAM, 572 pageable_size); 573 /* 574 * Load pageable part in the dedicated allocated area: 575 * - Move pageable non-init part into pageable area. Note bootloader 576 * may have loaded it anywhere in TA RAM hence use memmove(). 577 * - Copy pageable init part from current location into pageable area. 578 */ 579 memmove(paged_store + init_size, 580 phys_to_virt(pageable_part, 581 core_mmu_get_type_by_pa(pageable_part), 582 __pageable_part_end - __pageable_part_start), 583 __pageable_part_end - __pageable_part_start); 584 asan_memcpy_unchecked(paged_store, __init_start, init_size); 585 /* 586 * Undo eventual relocation for the init part so the hash checks 587 * can pass. 588 */ 589 undo_init_relocation(paged_store); 590 591 /* Check that hashes of what's in pageable area is OK */ 592 DMSG("Checking hashes of pageable area"); 593 for (n = 0; (n * SMALL_PAGE_SIZE) < pageable_size; n++) { 594 const uint8_t *hash = hashes + n * TEE_SHA256_HASH_SIZE; 595 const uint8_t *page = paged_store + n * SMALL_PAGE_SIZE; 596 TEE_Result res; 597 598 DMSG("hash pg_idx %zu hash %p page %p", n, hash, page); 599 res = hash_sha256_check(hash, page, SMALL_PAGE_SIZE); 600 if (res != TEE_SUCCESS) { 601 EMSG("Hash failed for page %zu at %p: res 0x%x", 602 n, (void *)page, res); 603 panic(); 604 } 605 } 606 607 /* 608 * Assert prepaged init sections are page aligned so that nothing 609 * trails uninited at the end of the premapped init area. 610 */ 611 assert(!(init_size & SMALL_PAGE_MASK)); 612 613 /* 614 * Initialize the virtual memory pool used for main_mmu_l2_ttb which 615 * is supplied to tee_pager_init() below. 616 */ 617 init_virt_pool(&core_virt_mem_pool); 618 619 /* 620 * Assign alias area for pager end of the small page block the rest 621 * of the binary is loaded into. We're taking more than needed, but 622 * we're guaranteed to not need more than the physical amount of 623 * TZSRAM. 624 */ 625 mm = tee_mm_alloc2(&core_virt_mem_pool, 626 (vaddr_t)core_virt_mem_pool.lo + 627 core_virt_mem_pool.size - TZSRAM_SIZE, 628 TZSRAM_SIZE); 629 assert(mm); 630 tee_pager_set_alias_area(mm); 631 632 /* 633 * Claim virtual memory which isn't paged. 634 * Linear memory (flat map core memory) ends there. 635 */ 636 mm = tee_mm_alloc2(&core_virt_mem_pool, VCORE_UNPG_RX_PA, 637 (vaddr_t)(__pageable_start - VCORE_UNPG_RX_PA)); 638 assert(mm); 639 640 /* 641 * Allocate virtual memory for the pageable area and let the pager 642 * take charge of all the pages already assigned to that memory. 643 */ 644 mm = tee_mm_alloc2(&core_virt_mem_pool, (vaddr_t)__pageable_start, 645 pageable_size); 646 assert(mm); 647 fobj = ro_paged_alloc(mm, hashes, paged_store); 648 assert(fobj); 649 tee_pager_add_core_region(tee_mm_get_smem(mm), PAGED_REGION_TYPE_RO, 650 fobj); 651 fobj_put(fobj); 652 653 tee_pager_add_pages(pageable_start, init_size / SMALL_PAGE_SIZE, false); 654 tee_pager_add_pages(pageable_start + init_size, 655 (pageable_size - init_size) / SMALL_PAGE_SIZE, 656 true); 657 if (pageable_end < tzsram_end) 658 tee_pager_add_pages(pageable_end, (tzsram_end - pageable_end) / 659 SMALL_PAGE_SIZE, true); 660 661 /* 662 * There may be physical pages in TZSRAM before the core load address. 663 * These pages can be added to the physical pages pool of the pager. 664 * This setup may happen when a the secure bootloader runs in TZRAM 665 * and its memory can be reused by OP-TEE once boot stages complete. 666 */ 667 tee_pager_add_pages(core_virt_mem_pool.lo, 668 (VCORE_UNPG_RX_PA - core_virt_mem_pool.lo) / 669 SMALL_PAGE_SIZE, 670 true); 671 672 print_pager_pool_size(); 673 } 674 #else 675 676 static void init_runtime(unsigned long pageable_part __unused) 677 { 678 init_asan(); 679 680 /* 681 * By default whole OP-TEE uses malloc, so we need to initialize 682 * it early. But, when virtualization is enabled, malloc is used 683 * only by TEE runtime, so malloc should be initialized later, for 684 * every virtual partition separately. Core code uses nex_malloc 685 * instead. 686 */ 687 #ifdef CFG_NS_VIRTUALIZATION 688 nex_malloc_add_pool(__nex_heap_start, __nex_heap_end - 689 __nex_heap_start); 690 #else 691 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start); 692 #endif 693 694 IMSG_RAW("\n"); 695 } 696 #endif 697 698 #if defined(CFG_DT) 699 static int add_optee_dt_node(struct dt_descriptor *dt) 700 { 701 int offs; 702 int ret; 703 704 if (fdt_path_offset(dt->blob, "/firmware/optee") >= 0) { 705 DMSG("OP-TEE Device Tree node already exists!"); 706 return 0; 707 } 708 709 offs = fdt_path_offset(dt->blob, "/firmware"); 710 if (offs < 0) { 711 offs = add_dt_path_subnode(dt, "/", "firmware"); 712 if (offs < 0) 713 return -1; 714 } 715 716 offs = fdt_add_subnode(dt->blob, offs, "optee"); 717 if (offs < 0) 718 return -1; 719 720 ret = fdt_setprop_string(dt->blob, offs, "compatible", 721 "linaro,optee-tz"); 722 if (ret < 0) 723 return -1; 724 ret = fdt_setprop_string(dt->blob, offs, "method", "smc"); 725 if (ret < 0) 726 return -1; 727 728 if (CFG_CORE_ASYNC_NOTIF_GIC_INTID) { 729 /* 730 * The format of the interrupt property is defined by the 731 * binding of the interrupt domain root. In this case it's 732 * one Arm GIC v1, v2 or v3 so we must be compatible with 733 * these. 734 * 735 * An SPI type of interrupt is indicated with a 0 in the 736 * first cell. A PPI type is indicated with value 1. 737 * 738 * The interrupt number goes in the second cell where 739 * SPIs ranges from 0 to 987 and PPI ranges from 0 to 15. 740 * 741 * Flags are passed in the third cells. 742 */ 743 uint32_t itr_trigger = 0; 744 uint32_t itr_type = 0; 745 uint32_t itr_id = 0; 746 uint32_t val[3] = { }; 747 748 /* PPI are visible only in current CPU cluster */ 749 static_assert(IS_ENABLED(CFG_CORE_FFA) || 750 !CFG_CORE_ASYNC_NOTIF_GIC_INTID || 751 (CFG_CORE_ASYNC_NOTIF_GIC_INTID >= 752 GIC_SPI_BASE) || 753 ((CFG_TEE_CORE_NB_CORE <= 8) && 754 (CFG_CORE_ASYNC_NOTIF_GIC_INTID >= 755 GIC_PPI_BASE))); 756 757 if (CFG_CORE_ASYNC_NOTIF_GIC_INTID >= GIC_SPI_BASE) { 758 itr_type = GIC_SPI; 759 itr_id = CFG_CORE_ASYNC_NOTIF_GIC_INTID - GIC_SPI_BASE; 760 itr_trigger = IRQ_TYPE_EDGE_RISING; 761 } else { 762 itr_type = GIC_PPI; 763 itr_id = CFG_CORE_ASYNC_NOTIF_GIC_INTID - GIC_PPI_BASE; 764 itr_trigger = IRQ_TYPE_EDGE_RISING | 765 GIC_CPU_MASK_SIMPLE(CFG_TEE_CORE_NB_CORE); 766 } 767 768 val[0] = TEE_U32_TO_BIG_ENDIAN(itr_type); 769 val[1] = TEE_U32_TO_BIG_ENDIAN(itr_id); 770 val[2] = TEE_U32_TO_BIG_ENDIAN(itr_trigger); 771 772 ret = fdt_setprop(dt->blob, offs, "interrupts", val, 773 sizeof(val)); 774 if (ret < 0) 775 return -1; 776 } 777 return 0; 778 } 779 780 #ifdef CFG_PSCI_ARM32 781 static int append_psci_compatible(void *fdt, int offs, const char *str) 782 { 783 return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1); 784 } 785 786 static int dt_add_psci_node(struct dt_descriptor *dt) 787 { 788 int offs; 789 790 if (fdt_path_offset(dt->blob, "/psci") >= 0) { 791 DMSG("PSCI Device Tree node already exists!"); 792 return 0; 793 } 794 795 offs = add_dt_path_subnode(dt, "/", "psci"); 796 if (offs < 0) 797 return -1; 798 if (append_psci_compatible(dt->blob, offs, "arm,psci-1.0")) 799 return -1; 800 if (append_psci_compatible(dt->blob, offs, "arm,psci-0.2")) 801 return -1; 802 if (append_psci_compatible(dt->blob, offs, "arm,psci")) 803 return -1; 804 if (fdt_setprop_string(dt->blob, offs, "method", "smc")) 805 return -1; 806 if (fdt_setprop_u32(dt->blob, offs, "cpu_suspend", PSCI_CPU_SUSPEND)) 807 return -1; 808 if (fdt_setprop_u32(dt->blob, offs, "cpu_off", PSCI_CPU_OFF)) 809 return -1; 810 if (fdt_setprop_u32(dt->blob, offs, "cpu_on", PSCI_CPU_ON)) 811 return -1; 812 if (fdt_setprop_u32(dt->blob, offs, "sys_poweroff", PSCI_SYSTEM_OFF)) 813 return -1; 814 if (fdt_setprop_u32(dt->blob, offs, "sys_reset", PSCI_SYSTEM_RESET)) 815 return -1; 816 return 0; 817 } 818 819 static int check_node_compat_prefix(struct dt_descriptor *dt, int offs, 820 const char *prefix) 821 { 822 const size_t prefix_len = strlen(prefix); 823 size_t l; 824 int plen; 825 const char *prop; 826 827 prop = fdt_getprop(dt->blob, offs, "compatible", &plen); 828 if (!prop) 829 return -1; 830 831 while (plen > 0) { 832 if (memcmp(prop, prefix, prefix_len) == 0) 833 return 0; /* match */ 834 835 l = strlen(prop) + 1; 836 prop += l; 837 plen -= l; 838 } 839 840 return -1; 841 } 842 843 static int dt_add_psci_cpu_enable_methods(struct dt_descriptor *dt) 844 { 845 int offs = 0; 846 847 while (1) { 848 offs = fdt_next_node(dt->blob, offs, NULL); 849 if (offs < 0) 850 break; 851 if (fdt_getprop(dt->blob, offs, "enable-method", NULL)) 852 continue; /* already set */ 853 if (check_node_compat_prefix(dt, offs, "arm,cortex-a")) 854 continue; /* no compatible */ 855 if (fdt_setprop_string(dt->blob, offs, "enable-method", "psci")) 856 return -1; 857 /* Need to restart scanning as offsets may have changed */ 858 offs = 0; 859 } 860 return 0; 861 } 862 863 static int config_psci(struct dt_descriptor *dt) 864 { 865 if (dt_add_psci_node(dt)) 866 return -1; 867 return dt_add_psci_cpu_enable_methods(dt); 868 } 869 #else 870 static int config_psci(struct dt_descriptor *dt __unused) 871 { 872 return 0; 873 } 874 #endif /*CFG_PSCI_ARM32*/ 875 876 static int mark_tzdram_as_reserved(struct dt_descriptor *dt) 877 { 878 return add_res_mem_dt_node(dt, "optee_core", CFG_TZDRAM_START, 879 CFG_TZDRAM_SIZE); 880 } 881 882 static void update_external_dt(void) 883 { 884 struct dt_descriptor *dt = get_external_dt_desc(); 885 886 if (!dt || !dt->blob) 887 return; 888 889 if (!IS_ENABLED(CFG_CORE_FFA) && add_optee_dt_node(dt)) 890 panic("Failed to add OP-TEE Device Tree node"); 891 892 if (config_psci(dt)) 893 panic("Failed to config PSCI"); 894 895 #ifdef CFG_CORE_RESERVED_SHM 896 if (mark_static_shm_as_reserved(dt)) 897 panic("Failed to config non-secure memory"); 898 #endif 899 900 if (mark_tzdram_as_reserved(dt)) 901 panic("Failed to config secure memory"); 902 } 903 #else /*CFG_DT*/ 904 static void update_external_dt(void) 905 { 906 } 907 #endif /*!CFG_DT*/ 908 909 #ifdef CFG_NS_VIRTUALIZATION 910 static TEE_Result virt_init_heap(void) 911 { 912 /* We need to initialize pool for every virtual guest partition */ 913 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start); 914 915 return TEE_SUCCESS; 916 } 917 preinit_early(virt_init_heap); 918 #endif 919 920 void init_tee_runtime(void) 921 { 922 #ifndef CFG_WITH_PAGER 923 /* Pager initializes TA RAM early */ 924 core_mmu_init_ta_ram(); 925 #endif 926 /* 927 * With virtualization we call this function when creating the 928 * OP-TEE partition instead. 929 */ 930 if (!IS_ENABLED(CFG_NS_VIRTUALIZATION)) 931 call_preinitcalls(); 932 call_initcalls(); 933 934 /* 935 * These two functions uses crypto_rng_read() to initialize the 936 * pauth keys. Once call_initcalls() returns we're guaranteed that 937 * crypto_rng_read() is ready to be used. 938 */ 939 thread_init_core_local_pauth_keys(); 940 thread_init_thread_pauth_keys(); 941 942 /* 943 * Reinitialize canaries around the stacks with crypto_rng_read(). 944 * 945 * TODO: Updating canaries when CFG_NS_VIRTUALIZATION is enabled will 946 * require synchronization between thread_check_canaries() and 947 * thread_update_canaries(). 948 */ 949 if (!IS_ENABLED(CFG_NS_VIRTUALIZATION)) 950 thread_update_canaries(); 951 } 952 953 static void init_primary(unsigned long pageable_part, unsigned long nsec_entry) 954 { 955 thread_init_core_local_stacks(); 956 /* 957 * Mask asynchronous exceptions before switch to the thread vector 958 * as the thread handler requires those to be masked while 959 * executing with the temporary stack. The thread subsystem also 960 * asserts that the foreign interrupts are blocked when using most of 961 * its functions. 962 */ 963 thread_set_exceptions(THREAD_EXCP_ALL); 964 primary_save_cntfrq(); 965 init_vfp_sec(); 966 967 if (IS_ENABLED(CFG_CRYPTO_WITH_CE)) 968 check_crypto_extensions(); 969 970 /* 971 * Pager: init_runtime() calls thread_kernel_enable_vfp() so we must 972 * set a current thread right now to avoid a chicken-and-egg problem 973 * (thread_init_boot_thread() sets the current thread but needs 974 * things set by init_runtime()). 975 */ 976 thread_get_core_local()->curr_thread = 0; 977 init_runtime(pageable_part); 978 979 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) { 980 /* 981 * Virtualization: We can't initialize threads right now because 982 * threads belong to "tee" part and will be initialized 983 * separately per each new virtual guest. So, we'll clear 984 * "curr_thread" and call it done. 985 */ 986 thread_get_core_local()->curr_thread = -1; 987 } else { 988 thread_init_boot_thread(); 989 } 990 thread_init_primary(); 991 thread_init_per_cpu(); 992 init_sec_mon(nsec_entry); 993 } 994 995 static bool cpu_nmfi_enabled(void) 996 { 997 #if defined(ARM32) 998 return read_sctlr() & SCTLR_NMFI; 999 #else 1000 /* Note: ARM64 does not feature non-maskable FIQ support. */ 1001 return false; 1002 #endif 1003 } 1004 1005 /* 1006 * Note: this function is weak just to make it possible to exclude it from 1007 * the unpaged area. 1008 */ 1009 void __weak boot_init_primary_late(unsigned long fdt __unused, 1010 unsigned long manifest __unused) 1011 { 1012 size_t fdt_size = CFG_DTB_MAX_SIZE; 1013 1014 if (IS_ENABLED(CFG_TRANSFER_LIST) && mapped_tl) { 1015 struct transfer_list_entry *tl_e = NULL; 1016 1017 tl_e = transfer_list_find(mapped_tl, TL_TAG_FDT); 1018 if (tl_e) 1019 fdt_size = tl_e->data_size; 1020 } 1021 1022 init_external_dt(boot_arg_fdt, fdt_size); 1023 reinit_manifest_dt(); 1024 #ifdef CFG_CORE_SEL1_SPMC 1025 tpm_map_log_area(get_manifest_dt()); 1026 #else 1027 tpm_map_log_area(get_external_dt()); 1028 #endif 1029 discover_nsec_memory(); 1030 update_external_dt(); 1031 configure_console_from_dt(); 1032 1033 IMSG("OP-TEE version: %s", core_v_str); 1034 if (IS_ENABLED(CFG_INSECURE)) { 1035 IMSG("WARNING: This OP-TEE configuration might be insecure!"); 1036 IMSG("WARNING: Please check https://optee.readthedocs.io/en/latest/architecture/porting_guidelines.html"); 1037 } 1038 IMSG("Primary CPU initializing"); 1039 #ifdef CFG_CORE_ASLR 1040 DMSG("Executing at offset %#lx with virtual load address %#"PRIxVA, 1041 (unsigned long)boot_mmu_config.map_offset, VCORE_START_VA); 1042 #endif 1043 if (IS_ENABLED(CFG_MEMTAG)) 1044 DMSG("Memory tagging %s", 1045 memtag_is_enabled() ? "enabled" : "disabled"); 1046 1047 /* Check if platform needs NMFI workaround */ 1048 if (cpu_nmfi_enabled()) { 1049 if (!IS_ENABLED(CFG_CORE_WORKAROUND_ARM_NMFI)) 1050 IMSG("WARNING: This ARM core has NMFI enabled, please apply workaround!"); 1051 } else { 1052 if (IS_ENABLED(CFG_CORE_WORKAROUND_ARM_NMFI)) 1053 IMSG("WARNING: This ARM core does not have NMFI enabled, no need for workaround"); 1054 } 1055 1056 boot_primary_init_intc(); 1057 init_vfp_nsec(); 1058 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) { 1059 IMSG("Initializing virtualization support"); 1060 core_mmu_init_virtualization(); 1061 } else { 1062 init_tee_runtime(); 1063 } 1064 call_finalcalls(); 1065 IMSG("Primary CPU switching to normal world boot"); 1066 } 1067 1068 static void init_secondary_helper(unsigned long nsec_entry) 1069 { 1070 IMSG("Secondary CPU %zu initializing", get_core_pos()); 1071 1072 /* 1073 * Mask asynchronous exceptions before switch to the thread vector 1074 * as the thread handler requires those to be masked while 1075 * executing with the temporary stack. The thread subsystem also 1076 * asserts that the foreign interrupts are blocked when using most of 1077 * its functions. 1078 */ 1079 thread_set_exceptions(THREAD_EXCP_ALL); 1080 1081 secondary_init_cntfrq(); 1082 thread_init_per_cpu(); 1083 init_sec_mon(nsec_entry); 1084 boot_secondary_init_intc(); 1085 init_vfp_sec(); 1086 init_vfp_nsec(); 1087 1088 IMSG("Secondary CPU %zu switching to normal world boot", get_core_pos()); 1089 } 1090 1091 /* 1092 * Note: this function is weak just to make it possible to exclude it from 1093 * the unpaged area so that it lies in the init area. 1094 */ 1095 void __weak boot_init_primary_early(void) 1096 { 1097 unsigned long pageable_part = 0; 1098 unsigned long e = PADDR_INVALID; 1099 struct transfer_list_entry *tl_e = NULL; 1100 1101 if (!IS_ENABLED(CFG_WITH_ARM_TRUSTED_FW)) 1102 e = boot_arg_nsec_entry; 1103 1104 if (IS_ENABLED(CFG_TRANSFER_LIST) && boot_arg_transfer_list) { 1105 /* map and save the TL */ 1106 mapped_tl = transfer_list_map(boot_arg_transfer_list); 1107 if (!mapped_tl) 1108 panic("Failed to map transfer list"); 1109 1110 transfer_list_dump(mapped_tl); 1111 tl_e = transfer_list_find(mapped_tl, TL_TAG_FDT); 1112 if (tl_e) { 1113 /* 1114 * Expand the data size of the DTB entry to the maximum 1115 * allocable mapped memory to reserve sufficient space 1116 * for inserting new nodes, avoid potentially corrupting 1117 * next entries. 1118 */ 1119 uint32_t dtb_max_sz = mapped_tl->max_size - 1120 mapped_tl->size + tl_e->data_size; 1121 1122 if (!transfer_list_set_data_size(mapped_tl, tl_e, 1123 dtb_max_sz)) { 1124 EMSG("Failed to extend DTB size to %#"PRIx32, 1125 dtb_max_sz); 1126 panic(); 1127 } 1128 } 1129 tl_e = transfer_list_find(mapped_tl, TL_TAG_OPTEE_PAGABLE_PART); 1130 } 1131 1132 if (IS_ENABLED(CFG_WITH_PAGER)) { 1133 if (IS_ENABLED(CFG_TRANSFER_LIST) && tl_e) 1134 pageable_part = 1135 get_le64(transfer_list_entry_data(tl_e)); 1136 else 1137 pageable_part = boot_arg_pageable_part; 1138 } 1139 1140 init_primary(pageable_part, e); 1141 } 1142 1143 static void boot_save_transfer_list(unsigned long zero_reg, 1144 unsigned long transfer_list, 1145 unsigned long fdt) 1146 { 1147 struct transfer_list_header *tl = (void *)transfer_list; 1148 struct transfer_list_entry *tl_e = NULL; 1149 1150 if (zero_reg != 0) 1151 panic("Incorrect transfer list register convention"); 1152 1153 if (!IS_ALIGNED_WITH_TYPE(transfer_list, struct transfer_list_header) || 1154 !IS_ALIGNED(transfer_list, TL_ALIGNMENT_FROM_ORDER(tl->alignment))) 1155 panic("Transfer list base address is not aligned"); 1156 1157 if (transfer_list_check_header(tl) == TL_OPS_NONE) 1158 panic("Invalid transfer list"); 1159 1160 tl_e = transfer_list_find(tl, TL_TAG_FDT); 1161 if (fdt != (unsigned long)transfer_list_entry_data(tl_e)) 1162 panic("DT does not match to the DT entry of the TL"); 1163 1164 boot_arg_transfer_list = transfer_list; 1165 } 1166 1167 #if defined(CFG_WITH_ARM_TRUSTED_FW) 1168 unsigned long boot_cpu_on_handler(unsigned long a0 __maybe_unused, 1169 unsigned long a1 __unused) 1170 { 1171 init_secondary_helper(PADDR_INVALID); 1172 return 0; 1173 } 1174 #else 1175 void boot_init_secondary(unsigned long nsec_entry) 1176 { 1177 init_secondary_helper(nsec_entry); 1178 } 1179 #endif 1180 1181 #if defined(CFG_BOOT_SECONDARY_REQUEST) 1182 void boot_set_core_ns_entry(size_t core_idx, uintptr_t entry, 1183 uintptr_t context_id) 1184 { 1185 ns_entry_contexts[core_idx].entry_point = entry; 1186 ns_entry_contexts[core_idx].context_id = context_id; 1187 dsb_ishst(); 1188 } 1189 1190 int boot_core_release(size_t core_idx, paddr_t entry) 1191 { 1192 if (!core_idx || core_idx >= CFG_TEE_CORE_NB_CORE) 1193 return -1; 1194 1195 ns_entry_contexts[core_idx].entry_point = entry; 1196 dmb(); 1197 spin_table[core_idx] = 1; 1198 dsb(); 1199 sev(); 1200 1201 return 0; 1202 } 1203 1204 /* 1205 * spin until secondary boot request, then returns with 1206 * the secondary core entry address. 1207 */ 1208 struct ns_entry_context *boot_core_hpen(void) 1209 { 1210 #ifdef CFG_PSCI_ARM32 1211 return &ns_entry_contexts[get_core_pos()]; 1212 #else 1213 do { 1214 wfe(); 1215 } while (!spin_table[get_core_pos()]); 1216 dmb(); 1217 return &ns_entry_contexts[get_core_pos()]; 1218 #endif 1219 } 1220 #endif 1221 1222 #if defined(CFG_CORE_ASLR) 1223 #if defined(CFG_DT) 1224 unsigned long __weak get_aslr_seed(void) 1225 { 1226 void *fdt = NULL; 1227 int rc = 0; 1228 const uint64_t *seed = NULL; 1229 int offs = 0; 1230 int len = 0; 1231 1232 if (!IS_ENABLED(CFG_CORE_SEL2_SPMC)) 1233 fdt = (void *)boot_arg_fdt; 1234 1235 if (!fdt) { 1236 DMSG("No fdt"); 1237 goto err; 1238 } 1239 1240 rc = fdt_check_header(fdt); 1241 if (rc) { 1242 DMSG("Bad fdt: %d", rc); 1243 goto err; 1244 } 1245 1246 offs = fdt_path_offset(fdt, "/secure-chosen"); 1247 if (offs < 0) { 1248 DMSG("Cannot find /secure-chosen"); 1249 goto err; 1250 } 1251 seed = fdt_getprop(fdt, offs, "kaslr-seed", &len); 1252 if (!seed || len != sizeof(*seed)) { 1253 DMSG("Cannot find valid kaslr-seed"); 1254 goto err; 1255 } 1256 1257 return fdt64_to_cpu(fdt64_ld(seed)); 1258 1259 err: 1260 /* Try platform implementation */ 1261 return plat_get_aslr_seed(); 1262 } 1263 #else /*!CFG_DT*/ 1264 unsigned long __weak get_aslr_seed(void) 1265 { 1266 /* Try platform implementation */ 1267 return plat_get_aslr_seed(); 1268 } 1269 #endif /*!CFG_DT*/ 1270 #endif /*CFG_CORE_ASLR*/ 1271 1272 static void *get_fdt_from_boot_info(struct ffa_boot_info_header_1_1 *hdr) 1273 { 1274 struct ffa_boot_info_1_1 *desc = NULL; 1275 uint8_t content_fmt = 0; 1276 uint8_t name_fmt = 0; 1277 void *fdt = NULL; 1278 int ret = 0; 1279 1280 if (hdr->signature != FFA_BOOT_INFO_SIGNATURE) { 1281 EMSG("Bad boot info signature %#"PRIx32, hdr->signature); 1282 panic(); 1283 } 1284 if (hdr->version != FFA_BOOT_INFO_VERSION) { 1285 EMSG("Bad boot info version %#"PRIx32, hdr->version); 1286 panic(); 1287 } 1288 if (hdr->desc_count != 1) { 1289 EMSG("Bad boot info descriptor count %#"PRIx32, 1290 hdr->desc_count); 1291 panic(); 1292 } 1293 desc = (void *)((vaddr_t)hdr + hdr->desc_offset); 1294 name_fmt = desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK; 1295 if (name_fmt == FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING) 1296 DMSG("Boot info descriptor name \"%16s\"", desc->name); 1297 else if (name_fmt == FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID) 1298 DMSG("Boot info descriptor UUID %pUl", (void *)desc->name); 1299 else 1300 DMSG("Boot info descriptor: unknown name format %"PRIu8, 1301 name_fmt); 1302 1303 content_fmt = (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >> 1304 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT; 1305 if (content_fmt != FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR) { 1306 EMSG("Bad boot info content format %"PRIu8", expected %u (address)", 1307 content_fmt, FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR); 1308 panic(); 1309 } 1310 1311 fdt = (void *)(vaddr_t)desc->contents; 1312 ret = fdt_check_full(fdt, desc->size); 1313 if (ret < 0) { 1314 EMSG("Invalid Device Tree at %p: error %d", fdt, ret); 1315 panic(); 1316 } 1317 return fdt; 1318 } 1319 1320 static void get_sec_mem_from_manifest(void *fdt, paddr_t *base, size_t *size) 1321 { 1322 int ret = 0; 1323 uint64_t num = 0; 1324 1325 ret = fdt_node_check_compatible(fdt, 0, "arm,ffa-manifest-1.0"); 1326 if (ret < 0) { 1327 EMSG("Invalid FF-A manifest at %p: error %d", fdt, ret); 1328 panic(); 1329 } 1330 ret = dt_getprop_as_number(fdt, 0, "load-address", &num); 1331 if (ret < 0) { 1332 EMSG("Can't read \"load-address\" from FF-A manifest at %p: error %d", 1333 fdt, ret); 1334 panic(); 1335 } 1336 *base = num; 1337 /* "mem-size" is currently an undocumented extension to the spec. */ 1338 ret = dt_getprop_as_number(fdt, 0, "mem-size", &num); 1339 if (ret < 0) { 1340 EMSG("Can't read \"mem-size\" from FF-A manifest at %p: error %d", 1341 fdt, ret); 1342 panic(); 1343 } 1344 *size = num; 1345 } 1346 1347 void __weak boot_save_args(unsigned long a0, unsigned long a1, 1348 unsigned long a2, unsigned long a3, 1349 unsigned long a4 __maybe_unused) 1350 { 1351 /* 1352 * Register use: 1353 * 1354 * Scenario A: Default arguments 1355 * a0 - CFG_CORE_FFA=y && CFG_CORE_SEL2_SPMC=n: 1356 * if non-NULL holds the TOS FW config [1] address 1357 * - CFG_CORE_FFA=y && 1358 (CFG_CORE_SEL2_SPMC=y || CFG_CORE_EL3_SPMC=y): 1359 * address of FF-A Boot Information Blob 1360 * - CFG_CORE_FFA=n: 1361 * if non-NULL holds the pagable part address 1362 * a1 - CFG_WITH_ARM_TRUSTED_FW=n (Armv7): 1363 * Armv7 standard bootarg #1 (kept track of in entry_a32.S) 1364 * a2 - CFG_CORE_SEL2_SPMC=n: 1365 * if non-NULL holds the system DTB address 1366 * - CFG_WITH_ARM_TRUSTED_FW=n (Armv7): 1367 * Armv7 standard bootarg #2 (system DTB address, kept track 1368 * of in entry_a32.S) 1369 * a3 - Not used 1370 * a4 - CFG_WITH_ARM_TRUSTED_FW=n: 1371 * Non-secure entry address 1372 * 1373 * [1] A TF-A concept: TOS_FW_CONFIG - Trusted OS Firmware 1374 * configuration file. Used by Trusted OS (BL32), that is, OP-TEE 1375 * here. This is also called Manifest DT, related to the Manifest DT 1376 * passed in the FF-A Boot Information Blob, but with a different 1377 * compatible string. 1378 1379 * Scenario B: FW Handoff via Transfer List 1380 * Note: FF-A and non-secure entry are not yet supported with 1381 * Transfer List 1382 * a0 - DTB address or 0 (AArch64) 1383 * - must be 0 (AArch32) 1384 * a1 - TRANSFER_LIST_SIGNATURE | REG_CONVENTION_VER_MASK 1385 * a2 - must be 0 (AArch64) 1386 * - DTB address or 0 (AArch32) 1387 * a3 - Transfer list base address 1388 * a4 - Not used 1389 */ 1390 1391 if (IS_ENABLED(CFG_TRANSFER_LIST) && 1392 a1 == (TRANSFER_LIST_SIGNATURE | REG_CONVENTION_VER_MASK)) { 1393 if (IS_ENABLED(CFG_ARM64_core)) { 1394 boot_save_transfer_list(a2, a3, a0); 1395 boot_arg_fdt = a0; 1396 } else { 1397 boot_save_transfer_list(a0, a3, a2); 1398 boot_arg_fdt = a2; 1399 } 1400 return; 1401 } 1402 1403 if (!IS_ENABLED(CFG_CORE_SEL2_SPMC)) { 1404 #if defined(CFG_DT_ADDR) 1405 boot_arg_fdt = CFG_DT_ADDR; 1406 #else 1407 boot_arg_fdt = a2; 1408 #endif 1409 } 1410 1411 if (IS_ENABLED(CFG_CORE_FFA)) { 1412 if (IS_ENABLED(CFG_CORE_SEL2_SPMC) || 1413 IS_ENABLED(CFG_CORE_EL3_SPMC)) 1414 init_manifest_dt(get_fdt_from_boot_info((void *)a0)); 1415 else 1416 init_manifest_dt((void *)a0); 1417 if (IS_ENABLED(CFG_CORE_SEL2_SPMC) && 1418 IS_ENABLED(CFG_CORE_PHYS_RELOCATABLE)) { 1419 paddr_t base = 0; 1420 size_t size = 0; 1421 1422 get_sec_mem_from_manifest(get_manifest_dt(), 1423 &base, &size); 1424 core_mmu_set_secure_memory(base, size); 1425 } 1426 } else { 1427 if (IS_ENABLED(CFG_WITH_PAGER)) { 1428 #if defined(CFG_PAGEABLE_ADDR) 1429 boot_arg_pageable_part = CFG_PAGEABLE_ADDR; 1430 #else 1431 boot_arg_pageable_part = a0; 1432 #endif 1433 } 1434 if (!IS_ENABLED(CFG_WITH_ARM_TRUSTED_FW)) { 1435 #if defined(CFG_NS_ENTRY_ADDR) 1436 boot_arg_nsec_entry = CFG_NS_ENTRY_ADDR; 1437 #else 1438 boot_arg_nsec_entry = a4; 1439 #endif 1440 } 1441 } 1442 } 1443 1444 #if defined(CFG_TRANSFER_LIST) 1445 static TEE_Result release_transfer_list(void) 1446 { 1447 struct dt_descriptor *dt = get_external_dt_desc(); 1448 1449 if (!mapped_tl) 1450 return TEE_SUCCESS; 1451 1452 if (dt) { 1453 int ret = 0; 1454 struct transfer_list_entry *tl_e = NULL; 1455 1456 /* 1457 * Pack the DTB and update the transfer list before un-mapping 1458 */ 1459 ret = fdt_pack(dt->blob); 1460 if (ret < 0) { 1461 EMSG("Failed to pack Device Tree at 0x%" PRIxPA 1462 ": error %d", virt_to_phys(dt->blob), ret); 1463 panic(); 1464 } 1465 1466 tl_e = transfer_list_find(mapped_tl, TL_TAG_FDT); 1467 assert(dt->blob == transfer_list_entry_data(tl_e)); 1468 transfer_list_set_data_size(mapped_tl, tl_e, 1469 fdt_totalsize(dt->blob)); 1470 dt->blob = NULL; 1471 } 1472 1473 transfer_list_unmap_sync(mapped_tl); 1474 mapped_tl = NULL; 1475 1476 return TEE_SUCCESS; 1477 } 1478 1479 boot_final(release_transfer_list); 1480 #endif 1481