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