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