xref: /optee_os/core/arch/arm/kernel/boot.c (revision c282ebd61200b0cb0830399c1c33514dbd129dfd)
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 	/* Add heap2 first as heap1 may be too small as initial bget pool */
420 	malloc_add_pool(__heap2_start, __heap2_end - __heap2_start);
421 	malloc_add_pool(__heap1_start, __heap1_end - __heap1_start);
422 
423 	/*
424 	 * This needs to be initialized early to support address lookup
425 	 * in MEM_AREA_TEE_RAM
426 	 */
427 	tee_pager_early_init();
428 
429 	hashes = malloc(hash_size);
430 	IMSG_RAW("\n");
431 	IMSG("Pager is enabled. Hashes: %zu bytes", hash_size);
432 	assert(hashes);
433 	asan_memcpy_unchecked(hashes, tmp_hashes, hash_size);
434 
435 	/*
436 	 * Need tee_mm_sec_ddr initialized to be able to allocate secure
437 	 * DDR below.
438 	 */
439 	core_mmu_init_ta_ram();
440 
441 	carve_out_asan_mem(&tee_mm_sec_ddr);
442 
443 	mm = tee_mm_alloc(&tee_mm_sec_ddr, pageable_size);
444 	assert(mm);
445 	paged_store = phys_to_virt(tee_mm_get_smem(mm), MEM_AREA_TA_RAM,
446 				   pageable_size);
447 	/*
448 	 * Load pageable part in the dedicated allocated area:
449 	 * - Move pageable non-init part into pageable area. Note bootloader
450 	 *   may have loaded it anywhere in TA RAM hence use memmove().
451 	 * - Copy pageable init part from current location into pageable area.
452 	 */
453 	memmove(paged_store + init_size,
454 		phys_to_virt(pageable_part,
455 			     core_mmu_get_type_by_pa(pageable_part),
456 			     __pageable_part_end - __pageable_part_start),
457 		__pageable_part_end - __pageable_part_start);
458 	asan_memcpy_unchecked(paged_store, __init_start, init_size);
459 	/*
460 	 * Undo eventual relocation for the init part so the hash checks
461 	 * can pass.
462 	 */
463 	undo_init_relocation(paged_store);
464 
465 	/* Check that hashes of what's in pageable area is OK */
466 	DMSG("Checking hashes of pageable area");
467 	for (n = 0; (n * SMALL_PAGE_SIZE) < pageable_size; n++) {
468 		const uint8_t *hash = hashes + n * TEE_SHA256_HASH_SIZE;
469 		const uint8_t *page = paged_store + n * SMALL_PAGE_SIZE;
470 		TEE_Result res;
471 
472 		DMSG("hash pg_idx %zu hash %p page %p", n, hash, page);
473 		res = hash_sha256_check(hash, page, SMALL_PAGE_SIZE);
474 		if (res != TEE_SUCCESS) {
475 			EMSG("Hash failed for page %zu at %p: res 0x%x",
476 			     n, (void *)page, res);
477 			panic();
478 		}
479 	}
480 
481 	/*
482 	 * Assert prepaged init sections are page aligned so that nothing
483 	 * trails uninited at the end of the premapped init area.
484 	 */
485 	assert(!(init_size & SMALL_PAGE_MASK));
486 
487 	/*
488 	 * Initialize the virtual memory pool used for main_mmu_l2_ttb which
489 	 * is supplied to tee_pager_init() below.
490 	 */
491 	init_vcore(&tee_mm_vcore);
492 
493 	/*
494 	 * Assign alias area for pager end of the small page block the rest
495 	 * of the binary is loaded into. We're taking more than needed, but
496 	 * we're guaranteed to not need more than the physical amount of
497 	 * TZSRAM.
498 	 */
499 	mm = tee_mm_alloc2(&tee_mm_vcore,
500 		(vaddr_t)tee_mm_vcore.hi - TZSRAM_SIZE, TZSRAM_SIZE);
501 	assert(mm);
502 	tee_pager_set_alias_area(mm);
503 
504 	/*
505 	 * Claim virtual memory which isn't paged.
506 	 * Linear memory (flat map core memory) ends there.
507 	 */
508 	mm = tee_mm_alloc2(&tee_mm_vcore, VCORE_UNPG_RX_PA,
509 			   (vaddr_t)(__pageable_start - VCORE_UNPG_RX_PA));
510 	assert(mm);
511 
512 	/*
513 	 * Allocate virtual memory for the pageable area and let the pager
514 	 * take charge of all the pages already assigned to that memory.
515 	 */
516 	mm = tee_mm_alloc2(&tee_mm_vcore, (vaddr_t)__pageable_start,
517 			   pageable_size);
518 	assert(mm);
519 	fobj = ro_paged_alloc(mm, hashes, paged_store);
520 	assert(fobj);
521 	tee_pager_add_core_region(tee_mm_get_smem(mm), PAGED_REGION_TYPE_RO,
522 				  fobj);
523 	fobj_put(fobj);
524 
525 	tee_pager_add_pages(pageable_start, init_size / SMALL_PAGE_SIZE, false);
526 	tee_pager_add_pages(pageable_start + init_size,
527 			    (pageable_size - init_size) / SMALL_PAGE_SIZE,
528 			    true);
529 	if (pageable_end < tzsram_end)
530 		tee_pager_add_pages(pageable_end, (tzsram_end - pageable_end) /
531 						   SMALL_PAGE_SIZE, true);
532 
533 	/*
534 	 * There may be physical pages in TZSRAM before the core load address.
535 	 * These pages can be added to the physical pages pool of the pager.
536 	 * This setup may happen when a the secure bootloader runs in TZRAM
537 	 * and its memory can be reused by OP-TEE once boot stages complete.
538 	 */
539 	tee_pager_add_pages(tee_mm_vcore.lo,
540 			(VCORE_UNPG_RX_PA - tee_mm_vcore.lo) / SMALL_PAGE_SIZE,
541 			true);
542 
543 	print_pager_pool_size();
544 }
545 #else
546 
547 static void init_runtime(unsigned long pageable_part __unused)
548 {
549 	init_asan();
550 
551 	/*
552 	 * By default whole OP-TEE uses malloc, so we need to initialize
553 	 * it early. But, when virtualization is enabled, malloc is used
554 	 * only by TEE runtime, so malloc should be initialized later, for
555 	 * every virtual partition separately. Core code uses nex_malloc
556 	 * instead.
557 	 */
558 #ifdef CFG_VIRTUALIZATION
559 	nex_malloc_add_pool(__nex_heap_start, __nex_heap_end -
560 					      __nex_heap_start);
561 #else
562 	malloc_add_pool(__heap1_start, __heap1_end - __heap1_start);
563 #endif
564 
565 	IMSG_RAW("\n");
566 }
567 #endif
568 
569 void *get_dt(void)
570 {
571 	void *fdt = get_embedded_dt();
572 
573 	if (!fdt)
574 		fdt = get_external_dt();
575 
576 	return fdt;
577 }
578 
579 #if defined(CFG_EMBED_DTB)
580 void *get_embedded_dt(void)
581 {
582 	static bool checked;
583 
584 	assert(cpu_mmu_enabled());
585 
586 	if (!checked) {
587 		IMSG("Embedded DTB found");
588 
589 		if (fdt_check_header(embedded_secure_dtb))
590 			panic("Invalid embedded DTB");
591 
592 		checked = true;
593 	}
594 
595 	return embedded_secure_dtb;
596 }
597 #else
598 void *get_embedded_dt(void)
599 {
600 	return NULL;
601 }
602 #endif /*CFG_EMBED_DTB*/
603 
604 #if defined(CFG_DT)
605 void *get_external_dt(void)
606 {
607 	assert(cpu_mmu_enabled());
608 	return external_dt.blob;
609 }
610 
611 static TEE_Result release_external_dt(void)
612 {
613 	int ret = 0;
614 
615 	if (!external_dt.blob)
616 		return TEE_SUCCESS;
617 
618 	ret = fdt_pack(external_dt.blob);
619 	if (ret < 0) {
620 		EMSG("Failed to pack Device Tree at 0x%" PRIxPA ": error %d",
621 		     virt_to_phys(external_dt.blob), ret);
622 		panic();
623 	}
624 
625 	if (core_mmu_remove_mapping(MEM_AREA_EXT_DT, external_dt.blob,
626 				    CFG_DTB_MAX_SIZE))
627 		panic("Failed to remove temporary Device Tree mapping");
628 
629 	/* External DTB no more reached, reset pointer to invalid */
630 	external_dt.blob = NULL;
631 
632 	return TEE_SUCCESS;
633 }
634 boot_final(release_external_dt);
635 
636 #ifdef _CFG_USE_DTB_OVERLAY
637 static int add_dt_overlay_fragment(struct dt_descriptor *dt, int ioffs)
638 {
639 	char frag[32];
640 	int offs;
641 	int ret;
642 
643 	snprintf(frag, sizeof(frag), "fragment@%d", dt->frag_id);
644 	offs = fdt_add_subnode(dt->blob, ioffs, frag);
645 	if (offs < 0)
646 		return offs;
647 
648 	dt->frag_id += 1;
649 
650 	ret = fdt_setprop_string(dt->blob, offs, "target-path", "/");
651 	if (ret < 0)
652 		return -1;
653 
654 	return fdt_add_subnode(dt->blob, offs, "__overlay__");
655 }
656 
657 static int init_dt_overlay(struct dt_descriptor *dt, int __maybe_unused dt_size)
658 {
659 	int fragment;
660 
661 	if (IS_ENABLED(CFG_EXTERNAL_DTB_OVERLAY)) {
662 		if (!fdt_check_header(dt->blob)) {
663 			fdt_for_each_subnode(fragment, dt->blob, 0)
664 				dt->frag_id += 1;
665 			return 0;
666 		}
667 	}
668 
669 	return fdt_create_empty_tree(dt->blob, dt_size);
670 }
671 #else
672 static int add_dt_overlay_fragment(struct dt_descriptor *dt __unused, int offs)
673 {
674 	return offs;
675 }
676 
677 static int init_dt_overlay(struct dt_descriptor *dt __unused,
678 			   int dt_size __unused)
679 {
680 	return 0;
681 }
682 #endif /* _CFG_USE_DTB_OVERLAY */
683 
684 static int add_dt_path_subnode(struct dt_descriptor *dt, const char *path,
685 			       const char *subnode)
686 {
687 	int offs;
688 
689 	offs = fdt_path_offset(dt->blob, path);
690 	if (offs < 0)
691 		return -1;
692 	offs = add_dt_overlay_fragment(dt, offs);
693 	if (offs < 0)
694 		return -1;
695 	offs = fdt_add_subnode(dt->blob, offs, subnode);
696 	if (offs < 0)
697 		return -1;
698 	return offs;
699 }
700 
701 static int add_optee_dt_node(struct dt_descriptor *dt)
702 {
703 	int offs;
704 	int ret;
705 
706 	if (fdt_path_offset(dt->blob, "/firmware/optee") >= 0) {
707 		DMSG("OP-TEE Device Tree node already exists!");
708 		return 0;
709 	}
710 
711 	offs = fdt_path_offset(dt->blob, "/firmware");
712 	if (offs < 0) {
713 		offs = add_dt_path_subnode(dt, "/", "firmware");
714 		if (offs < 0)
715 			return -1;
716 	}
717 
718 	offs = fdt_add_subnode(dt->blob, offs, "optee");
719 	if (offs < 0)
720 		return -1;
721 
722 	ret = fdt_setprop_string(dt->blob, offs, "compatible",
723 				 "linaro,optee-tz");
724 	if (ret < 0)
725 		return -1;
726 	ret = fdt_setprop_string(dt->blob, offs, "method", "smc");
727 	if (ret < 0)
728 		return -1;
729 	return 0;
730 }
731 
732 #ifdef CFG_PSCI_ARM32
733 static int append_psci_compatible(void *fdt, int offs, const char *str)
734 {
735 	return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1);
736 }
737 
738 static int dt_add_psci_node(struct dt_descriptor *dt)
739 {
740 	int offs;
741 
742 	if (fdt_path_offset(dt->blob, "/psci") >= 0) {
743 		DMSG("PSCI Device Tree node already exists!");
744 		return 0;
745 	}
746 
747 	offs = add_dt_path_subnode(dt, "/", "psci");
748 	if (offs < 0)
749 		return -1;
750 	if (append_psci_compatible(dt->blob, offs, "arm,psci-1.0"))
751 		return -1;
752 	if (append_psci_compatible(dt->blob, offs, "arm,psci-0.2"))
753 		return -1;
754 	if (append_psci_compatible(dt->blob, offs, "arm,psci"))
755 		return -1;
756 	if (fdt_setprop_string(dt->blob, offs, "method", "smc"))
757 		return -1;
758 	if (fdt_setprop_u32(dt->blob, offs, "cpu_suspend", PSCI_CPU_SUSPEND))
759 		return -1;
760 	if (fdt_setprop_u32(dt->blob, offs, "cpu_off", PSCI_CPU_OFF))
761 		return -1;
762 	if (fdt_setprop_u32(dt->blob, offs, "cpu_on", PSCI_CPU_ON))
763 		return -1;
764 	if (fdt_setprop_u32(dt->blob, offs, "sys_poweroff", PSCI_SYSTEM_OFF))
765 		return -1;
766 	if (fdt_setprop_u32(dt->blob, offs, "sys_reset", PSCI_SYSTEM_RESET))
767 		return -1;
768 	return 0;
769 }
770 
771 static int check_node_compat_prefix(struct dt_descriptor *dt, int offs,
772 				    const char *prefix)
773 {
774 	const size_t prefix_len = strlen(prefix);
775 	size_t l;
776 	int plen;
777 	const char *prop;
778 
779 	prop = fdt_getprop(dt->blob, offs, "compatible", &plen);
780 	if (!prop)
781 		return -1;
782 
783 	while (plen > 0) {
784 		if (memcmp(prop, prefix, prefix_len) == 0)
785 			return 0; /* match */
786 
787 		l = strlen(prop) + 1;
788 		prop += l;
789 		plen -= l;
790 	}
791 
792 	return -1;
793 }
794 
795 static int dt_add_psci_cpu_enable_methods(struct dt_descriptor *dt)
796 {
797 	int offs = 0;
798 
799 	while (1) {
800 		offs = fdt_next_node(dt->blob, offs, NULL);
801 		if (offs < 0)
802 			break;
803 		if (fdt_getprop(dt->blob, offs, "enable-method", NULL))
804 			continue; /* already set */
805 		if (check_node_compat_prefix(dt, offs, "arm,cortex-a"))
806 			continue; /* no compatible */
807 		if (fdt_setprop_string(dt->blob, offs, "enable-method", "psci"))
808 			return -1;
809 		/* Need to restart scanning as offsets may have changed */
810 		offs = 0;
811 	}
812 	return 0;
813 }
814 
815 static int config_psci(struct dt_descriptor *dt)
816 {
817 	if (dt_add_psci_node(dt))
818 		return -1;
819 	return dt_add_psci_cpu_enable_methods(dt);
820 }
821 #else
822 static int config_psci(struct dt_descriptor *dt __unused)
823 {
824 	return 0;
825 }
826 #endif /*CFG_PSCI_ARM32*/
827 
828 static void set_dt_val(void *data, uint32_t cell_size, uint64_t val)
829 {
830 	if (cell_size == 1) {
831 		fdt32_t v = cpu_to_fdt32((uint32_t)val);
832 
833 		memcpy(data, &v, sizeof(v));
834 	} else {
835 		fdt64_t v = cpu_to_fdt64(val);
836 
837 		memcpy(data, &v, sizeof(v));
838 	}
839 }
840 
841 static int add_res_mem_dt_node(struct dt_descriptor *dt, const char *name,
842 			       paddr_t pa, size_t size)
843 {
844 	int offs = 0;
845 	int ret = 0;
846 	int addr_size = -1;
847 	int len_size = -1;
848 	bool found = true;
849 	char subnode_name[80] = { 0 };
850 
851 	offs = fdt_path_offset(dt->blob, "/reserved-memory");
852 
853 	if (offs < 0) {
854 		found = false;
855 		offs = 0;
856 	}
857 
858 	if (IS_ENABLED(_CFG_USE_DTB_OVERLAY)) {
859 		len_size = sizeof(paddr_t) / sizeof(uint32_t);
860 		addr_size = sizeof(paddr_t) / sizeof(uint32_t);
861 	} else {
862 		len_size = fdt_size_cells(dt->blob, offs);
863 		if (len_size < 0)
864 			return -1;
865 		addr_size = fdt_address_cells(dt->blob, offs);
866 		if (addr_size < 0)
867 			return -1;
868 	}
869 
870 	if (!found) {
871 		offs = add_dt_path_subnode(dt, "/", "reserved-memory");
872 		if (offs < 0)
873 			return -1;
874 		ret = fdt_setprop_cell(dt->blob, offs, "#address-cells",
875 				       addr_size);
876 		if (ret < 0)
877 			return -1;
878 		ret = fdt_setprop_cell(dt->blob, offs, "#size-cells", len_size);
879 		if (ret < 0)
880 			return -1;
881 		ret = fdt_setprop(dt->blob, offs, "ranges", NULL, 0);
882 		if (ret < 0)
883 			return -1;
884 	}
885 
886 	ret = snprintf(subnode_name, sizeof(subnode_name),
887 		       "%s@%" PRIxPA, name, pa);
888 	if (ret < 0 || ret >= (int)sizeof(subnode_name))
889 		DMSG("truncated node \"%s@%" PRIxPA"\"", name, pa);
890 	offs = fdt_add_subnode(dt->blob, offs, subnode_name);
891 	if (offs >= 0) {
892 		uint32_t data[FDT_MAX_NCELLS * 2];
893 
894 		set_dt_val(data, addr_size, pa);
895 		set_dt_val(data + addr_size, len_size, size);
896 		ret = fdt_setprop(dt->blob, offs, "reg", data,
897 				  sizeof(uint32_t) * (addr_size + len_size));
898 		if (ret < 0)
899 			return -1;
900 		ret = fdt_setprop(dt->blob, offs, "no-map", NULL, 0);
901 		if (ret < 0)
902 			return -1;
903 	} else {
904 		return -1;
905 	}
906 	return 0;
907 }
908 
909 #ifdef CFG_CORE_DYN_SHM
910 static uint64_t get_dt_val_and_advance(const void *data, size_t *offs,
911 				       uint32_t cell_size)
912 {
913 	uint64_t rv = 0;
914 
915 	if (cell_size == 1) {
916 		uint32_t v;
917 
918 		memcpy(&v, (const uint8_t *)data + *offs, sizeof(v));
919 		*offs += sizeof(v);
920 		rv = fdt32_to_cpu(v);
921 	} else {
922 		uint64_t v;
923 
924 		memcpy(&v, (const uint8_t *)data + *offs, sizeof(v));
925 		*offs += sizeof(v);
926 		rv = fdt64_to_cpu(v);
927 	}
928 
929 	return rv;
930 }
931 
932 /*
933  * Find all non-secure memory from DT. Memory marked inaccessible by Secure
934  * World is ignored since it could not be mapped to be used as dynamic shared
935  * memory.
936  */
937 static int get_nsec_memory_helper(void *fdt, struct core_mmu_phys_mem *mem)
938 {
939 	const uint8_t *prop = NULL;
940 	uint64_t a = 0;
941 	uint64_t l = 0;
942 	size_t prop_offs = 0;
943 	size_t prop_len = 0;
944 	int elems_total = 0;
945 	int addr_size = 0;
946 	int len_size = 0;
947 	int offs = 0;
948 	size_t n = 0;
949 	int len = 0;
950 
951 	addr_size = fdt_address_cells(fdt, 0);
952 	if (addr_size < 0)
953 		return 0;
954 
955 	len_size = fdt_size_cells(fdt, 0);
956 	if (len_size < 0)
957 		return 0;
958 
959 	while (true) {
960 		offs = fdt_node_offset_by_prop_value(fdt, offs, "device_type",
961 						     "memory",
962 						     sizeof("memory"));
963 		if (offs < 0)
964 			break;
965 
966 		if (_fdt_get_status(fdt, offs) != (DT_STATUS_OK_NSEC |
967 						   DT_STATUS_OK_SEC))
968 			continue;
969 
970 		prop = fdt_getprop(fdt, offs, "reg", &len);
971 		if (!prop)
972 			continue;
973 
974 		prop_len = len;
975 		for (n = 0, prop_offs = 0; prop_offs < prop_len; n++) {
976 			a = get_dt_val_and_advance(prop, &prop_offs, addr_size);
977 			if (prop_offs >= prop_len) {
978 				n--;
979 				break;
980 			}
981 
982 			l = get_dt_val_and_advance(prop, &prop_offs, len_size);
983 			if (mem) {
984 				mem->type = MEM_AREA_DDR_OVERALL;
985 				mem->addr = a;
986 				mem->size = l;
987 				mem++;
988 			}
989 		}
990 
991 		elems_total += n;
992 	}
993 
994 	return elems_total;
995 }
996 
997 static struct core_mmu_phys_mem *get_nsec_memory(void *fdt, size_t *nelems)
998 {
999 	struct core_mmu_phys_mem *mem = NULL;
1000 	int elems_total = 0;
1001 
1002 	elems_total = get_nsec_memory_helper(fdt, NULL);
1003 	if (elems_total <= 0)
1004 		return NULL;
1005 
1006 	mem = nex_calloc(elems_total, sizeof(*mem));
1007 	if (!mem)
1008 		panic();
1009 
1010 	elems_total = get_nsec_memory_helper(fdt, mem);
1011 	assert(elems_total > 0);
1012 
1013 	*nelems = elems_total;
1014 
1015 	return mem;
1016 }
1017 #endif /*CFG_CORE_DYN_SHM*/
1018 
1019 #ifdef CFG_CORE_RESERVED_SHM
1020 static int mark_static_shm_as_reserved(struct dt_descriptor *dt)
1021 {
1022 	vaddr_t shm_start;
1023 	vaddr_t shm_end;
1024 
1025 	core_mmu_get_mem_by_type(MEM_AREA_NSEC_SHM, &shm_start, &shm_end);
1026 	if (shm_start != shm_end)
1027 		return add_res_mem_dt_node(dt, "optee_shm",
1028 					   virt_to_phys((void *)shm_start),
1029 					   shm_end - shm_start);
1030 
1031 	DMSG("No SHM configured");
1032 	return -1;
1033 }
1034 #endif /*CFG_CORE_RESERVED_SHM*/
1035 
1036 static void init_external_dt(unsigned long phys_dt)
1037 {
1038 	struct dt_descriptor *dt = &external_dt;
1039 	void *fdt;
1040 	int ret;
1041 
1042 	if (!phys_dt) {
1043 		/*
1044 		 * No need to panic as we're not using the DT in OP-TEE
1045 		 * yet, we're only adding some nodes for normal world use.
1046 		 * This makes the switch to using DT easier as we can boot
1047 		 * a newer OP-TEE with older boot loaders. Once we start to
1048 		 * initialize devices based on DT we'll likely panic
1049 		 * instead of returning here.
1050 		 */
1051 		IMSG("No non-secure external DT");
1052 		return;
1053 	}
1054 
1055 	fdt = core_mmu_add_mapping(MEM_AREA_EXT_DT, phys_dt, CFG_DTB_MAX_SIZE);
1056 	if (!fdt)
1057 		panic("Failed to map external DTB");
1058 
1059 	dt->blob = fdt;
1060 
1061 	ret = init_dt_overlay(dt, CFG_DTB_MAX_SIZE);
1062 	if (ret < 0) {
1063 		EMSG("Device Tree Overlay init fail @ %#lx: error %d", phys_dt,
1064 		     ret);
1065 		panic();
1066 	}
1067 
1068 	ret = fdt_open_into(fdt, fdt, CFG_DTB_MAX_SIZE);
1069 	if (ret < 0) {
1070 		EMSG("Invalid Device Tree at %#lx: error %d", phys_dt, ret);
1071 		panic();
1072 	}
1073 
1074 	IMSG("Non-secure external DT found");
1075 }
1076 
1077 static int mark_tzdram_as_reserved(struct dt_descriptor *dt)
1078 {
1079 	return add_res_mem_dt_node(dt, "optee_core", CFG_TZDRAM_START,
1080 				   CFG_TZDRAM_SIZE);
1081 }
1082 
1083 static void update_external_dt(void)
1084 {
1085 	struct dt_descriptor *dt = &external_dt;
1086 
1087 	if (!dt->blob)
1088 		return;
1089 
1090 	if (!IS_ENABLED(CFG_CORE_FFA) && add_optee_dt_node(dt))
1091 		panic("Failed to add OP-TEE Device Tree node");
1092 
1093 	if (config_psci(dt))
1094 		panic("Failed to config PSCI");
1095 
1096 #ifdef CFG_CORE_RESERVED_SHM
1097 	if (mark_static_shm_as_reserved(dt))
1098 		panic("Failed to config non-secure memory");
1099 #endif
1100 
1101 	if (mark_tzdram_as_reserved(dt))
1102 		panic("Failed to config secure memory");
1103 }
1104 #else /*CFG_DT*/
1105 void *get_external_dt(void)
1106 {
1107 	return NULL;
1108 }
1109 
1110 static void init_external_dt(unsigned long phys_dt __unused)
1111 {
1112 }
1113 
1114 static void update_external_dt(void)
1115 {
1116 }
1117 
1118 #ifdef CFG_CORE_DYN_SHM
1119 static struct core_mmu_phys_mem *get_nsec_memory(void *fdt __unused,
1120 						 size_t *nelems __unused)
1121 {
1122 	return NULL;
1123 }
1124 #endif /*CFG_CORE_DYN_SHM*/
1125 #endif /*!CFG_DT*/
1126 
1127 #ifdef CFG_CORE_DYN_SHM
1128 static void discover_nsec_memory(void)
1129 {
1130 	struct core_mmu_phys_mem *mem;
1131 	const struct core_mmu_phys_mem *mem_begin = NULL;
1132 	const struct core_mmu_phys_mem *mem_end = NULL;
1133 	size_t nelems;
1134 	void *fdt = get_external_dt();
1135 
1136 	if (fdt) {
1137 		mem = get_nsec_memory(fdt, &nelems);
1138 		if (mem) {
1139 			core_mmu_set_discovered_nsec_ddr(mem, nelems);
1140 			return;
1141 		}
1142 
1143 		DMSG("No non-secure memory found in FDT");
1144 	}
1145 
1146 	mem_begin = phys_ddr_overall_begin;
1147 	mem_end = phys_ddr_overall_end;
1148 	nelems = mem_end - mem_begin;
1149 	if (nelems) {
1150 		/*
1151 		 * Platform cannot use both register_ddr() and the now
1152 		 * deprecated register_dynamic_shm().
1153 		 */
1154 		assert(phys_ddr_overall_compat_begin ==
1155 		       phys_ddr_overall_compat_end);
1156 	} else {
1157 		mem_begin = phys_ddr_overall_compat_begin;
1158 		mem_end = phys_ddr_overall_compat_end;
1159 		nelems = mem_end - mem_begin;
1160 		if (!nelems)
1161 			return;
1162 		DMSG("Warning register_dynamic_shm() is deprecated, please use register_ddr() instead");
1163 	}
1164 
1165 	mem = nex_calloc(nelems, sizeof(*mem));
1166 	if (!mem)
1167 		panic();
1168 
1169 	memcpy(mem, phys_ddr_overall_begin, sizeof(*mem) * nelems);
1170 	core_mmu_set_discovered_nsec_ddr(mem, nelems);
1171 }
1172 #else /*CFG_CORE_DYN_SHM*/
1173 static void discover_nsec_memory(void)
1174 {
1175 }
1176 #endif /*!CFG_CORE_DYN_SHM*/
1177 
1178 #ifdef CFG_VIRTUALIZATION
1179 static TEE_Result virt_init_heap(void)
1180 {
1181 	/* We need to initialize pool for every virtual guest partition */
1182 	malloc_add_pool(__heap1_start, __heap1_end - __heap1_start);
1183 
1184 	return TEE_SUCCESS;
1185 }
1186 preinit_early(virt_init_heap);
1187 #endif
1188 
1189 void init_tee_runtime(void)
1190 {
1191 #ifndef CFG_WITH_PAGER
1192 	/* Pager initializes TA RAM early */
1193 	core_mmu_init_ta_ram();
1194 #endif
1195 	/*
1196 	 * With virtualization we call this function when creating the
1197 	 * OP-TEE partition instead.
1198 	 */
1199 	if (!IS_ENABLED(CFG_VIRTUALIZATION))
1200 		call_preinitcalls();
1201 	call_initcalls();
1202 }
1203 
1204 static void init_primary(unsigned long pageable_part, unsigned long nsec_entry)
1205 {
1206 	/*
1207 	 * Mask asynchronous exceptions before switch to the thread vector
1208 	 * as the thread handler requires those to be masked while
1209 	 * executing with the temporary stack. The thread subsystem also
1210 	 * asserts that the foreign interrupts are blocked when using most of
1211 	 * its functions.
1212 	 */
1213 	thread_set_exceptions(THREAD_EXCP_ALL);
1214 	primary_save_cntfrq();
1215 	init_vfp_sec();
1216 	/*
1217 	 * Pager: init_runtime() calls thread_kernel_enable_vfp() so we must
1218 	 * set a current thread right now to avoid a chicken-and-egg problem
1219 	 * (thread_init_boot_thread() sets the current thread but needs
1220 	 * things set by init_runtime()).
1221 	 */
1222 	thread_get_core_local()->curr_thread = 0;
1223 	init_runtime(pageable_part);
1224 
1225 	if (IS_ENABLED(CFG_VIRTUALIZATION)) {
1226 		/*
1227 		 * Virtualization: We can't initialize threads right now because
1228 		 * threads belong to "tee" part and will be initialized
1229 		 * separately per each new virtual guest. So, we'll clear
1230 		 * "curr_thread" and call it done.
1231 		 */
1232 		thread_get_core_local()->curr_thread = -1;
1233 	} else {
1234 		thread_init_boot_thread();
1235 	}
1236 	thread_init_primary();
1237 	thread_init_per_cpu();
1238 	init_sec_mon(nsec_entry);
1239 }
1240 
1241 /*
1242  * Note: this function is weak just to make it possible to exclude it from
1243  * the unpaged area.
1244  */
1245 void __weak boot_init_primary_late(unsigned long fdt)
1246 {
1247 	init_external_dt(fdt);
1248 	tpm_map_log_area(get_external_dt());
1249 	discover_nsec_memory();
1250 	update_external_dt();
1251 	configure_console_from_dt();
1252 
1253 	IMSG("OP-TEE version: %s", core_v_str);
1254 	IMSG("Primary CPU initializing");
1255 #ifdef CFG_CORE_ASLR
1256 	DMSG("Executing at offset %#lx with virtual load address %#"PRIxVA,
1257 	     (unsigned long)boot_mmu_config.load_offset, VCORE_START_VA);
1258 #endif
1259 
1260 	main_init_gic();
1261 	init_vfp_nsec();
1262 	if (IS_ENABLED(CFG_VIRTUALIZATION)) {
1263 		IMSG("Initializing virtualization support");
1264 		core_mmu_init_virtualization();
1265 	} else {
1266 		init_tee_runtime();
1267 	}
1268 	call_finalcalls();
1269 	IMSG("Primary CPU switching to normal world boot");
1270 }
1271 
1272 static void init_secondary_helper(unsigned long nsec_entry)
1273 {
1274 	IMSG("Secondary CPU %zu initializing", get_core_pos());
1275 
1276 	/*
1277 	 * Mask asynchronous exceptions before switch to the thread vector
1278 	 * as the thread handler requires those to be masked while
1279 	 * executing with the temporary stack. The thread subsystem also
1280 	 * asserts that the foreign interrupts are blocked when using most of
1281 	 * its functions.
1282 	 */
1283 	thread_set_exceptions(THREAD_EXCP_ALL);
1284 
1285 	secondary_init_cntfrq();
1286 	thread_init_per_cpu();
1287 	init_sec_mon(nsec_entry);
1288 	main_secondary_init_gic();
1289 	init_vfp_sec();
1290 	init_vfp_nsec();
1291 
1292 	IMSG("Secondary CPU %zu switching to normal world boot", get_core_pos());
1293 }
1294 
1295 /*
1296  * Note: this function is weak just to make it possible to exclude it from
1297  * the unpaged area so that it lies in the init area.
1298  */
1299 void __weak boot_init_primary_early(unsigned long pageable_part,
1300 				    unsigned long nsec_entry __maybe_unused)
1301 {
1302 	unsigned long e = PADDR_INVALID;
1303 
1304 #if !defined(CFG_WITH_ARM_TRUSTED_FW)
1305 	e = nsec_entry;
1306 #endif
1307 
1308 	init_primary(pageable_part, e);
1309 }
1310 
1311 #if defined(CFG_WITH_ARM_TRUSTED_FW)
1312 unsigned long boot_cpu_on_handler(unsigned long a0 __maybe_unused,
1313 				  unsigned long a1 __unused)
1314 {
1315 	init_secondary_helper(PADDR_INVALID);
1316 	return 0;
1317 }
1318 #else
1319 void boot_init_secondary(unsigned long nsec_entry)
1320 {
1321 	init_secondary_helper(nsec_entry);
1322 }
1323 #endif
1324 
1325 #if defined(CFG_BOOT_SECONDARY_REQUEST)
1326 void boot_set_core_ns_entry(size_t core_idx, uintptr_t entry,
1327 			    uintptr_t context_id)
1328 {
1329 	ns_entry_contexts[core_idx].entry_point = entry;
1330 	ns_entry_contexts[core_idx].context_id = context_id;
1331 	dsb_ishst();
1332 }
1333 
1334 int boot_core_release(size_t core_idx, paddr_t entry)
1335 {
1336 	if (!core_idx || core_idx >= CFG_TEE_CORE_NB_CORE)
1337 		return -1;
1338 
1339 	ns_entry_contexts[core_idx].entry_point = entry;
1340 	dmb();
1341 	spin_table[core_idx] = 1;
1342 	dsb();
1343 	sev();
1344 
1345 	return 0;
1346 }
1347 
1348 /*
1349  * spin until secondary boot request, then returns with
1350  * the secondary core entry address.
1351  */
1352 struct ns_entry_context *boot_core_hpen(void)
1353 {
1354 #ifdef CFG_PSCI_ARM32
1355 	return &ns_entry_contexts[get_core_pos()];
1356 #else
1357 	do {
1358 		wfe();
1359 	} while (!spin_table[get_core_pos()]);
1360 	dmb();
1361 	return &ns_entry_contexts[get_core_pos()];
1362 #endif
1363 }
1364 #endif
1365 
1366 #if defined(CFG_CORE_ASLR)
1367 #if defined(CFG_DT)
1368 unsigned long __weak get_aslr_seed(void *fdt)
1369 {
1370 	int rc = fdt_check_header(fdt);
1371 	const uint64_t *seed = NULL;
1372 	int offs = 0;
1373 	int len = 0;
1374 
1375 	if (rc) {
1376 		DMSG("Bad fdt: %d", rc);
1377 		goto err;
1378 	}
1379 
1380 	offs =  fdt_path_offset(fdt, "/secure-chosen");
1381 	if (offs < 0) {
1382 		DMSG("Cannot find /secure-chosen");
1383 		goto err;
1384 	}
1385 	seed = fdt_getprop(fdt, offs, "kaslr-seed", &len);
1386 	if (!seed || len != sizeof(*seed)) {
1387 		DMSG("Cannot find valid kaslr-seed");
1388 		goto err;
1389 	}
1390 
1391 	return fdt64_to_cpu(*seed);
1392 
1393 err:
1394 	/* Try platform implementation */
1395 	return plat_get_aslr_seed();
1396 }
1397 #else /*!CFG_DT*/
1398 unsigned long __weak get_aslr_seed(void *fdt __unused)
1399 {
1400 	/* Try platform implementation */
1401 	return plat_get_aslr_seed();
1402 }
1403 #endif /*!CFG_DT*/
1404 #endif /*CFG_CORE_ASLR*/
1405