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