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