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