xref: /optee_os/core/arch/arm/kernel/secure_partition.c (revision 3da1a076941a1f9989741ad192188812bac3120a)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2020-2022, Arm Limited.
4  */
5 #include <bench.h>
6 #include <crypto/crypto.h>
7 #include <initcall.h>
8 #include <kernel/embedded_ts.h>
9 #include <kernel/ldelf_loader.h>
10 #include <kernel/secure_partition.h>
11 #include <kernel/spinlock.h>
12 #include <kernel/spmc_sp_handler.h>
13 #include <kernel/thread_private.h>
14 #include <kernel/thread_spmc.h>
15 #include <kernel/tpm.h>
16 #include <kernel/ts_store.h>
17 #include <ldelf.h>
18 #include <libfdt.h>
19 #include <mm/core_mmu.h>
20 #include <mm/fobj.h>
21 #include <mm/mobj.h>
22 #include <mm/vm.h>
23 #include <optee_ffa.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <tee_api_types.h>
27 #include <tee/uuid.h>
28 #include <trace.h>
29 #include <types_ext.h>
30 #include <utee_defines.h>
31 #include <util.h>
32 #include <zlib.h>
33 
34 #define SP_MANIFEST_ATTR_READ		BIT(0)
35 #define SP_MANIFEST_ATTR_WRITE		BIT(1)
36 #define SP_MANIFEST_ATTR_EXEC		BIT(2)
37 #define SP_MANIFEST_ATTR_NSEC		BIT(3)
38 
39 #define SP_MANIFEST_ATTR_RO		(SP_MANIFEST_ATTR_READ)
40 #define SP_MANIFEST_ATTR_RW		(SP_MANIFEST_ATTR_READ | \
41 					 SP_MANIFEST_ATTR_WRITE)
42 #define SP_MANIFEST_ATTR_RX		(SP_MANIFEST_ATTR_READ | \
43 					 SP_MANIFEST_ATTR_EXEC)
44 #define SP_MANIFEST_ATTR_RWX		(SP_MANIFEST_ATTR_READ  | \
45 					 SP_MANIFEST_ATTR_WRITE | \
46 					 SP_MANIFEST_ATTR_EXEC)
47 
48 const struct ts_ops sp_ops;
49 
50 /* List that holds all of the loaded SP's */
51 static struct sp_sessions_head open_sp_sessions =
52 	TAILQ_HEAD_INITIALIZER(open_sp_sessions);
53 
54 static const struct embedded_ts *find_secure_partition(const TEE_UUID *uuid)
55 {
56 	const struct sp_image *sp = NULL;
57 
58 	for_each_secure_partition(sp) {
59 		if (!memcmp(&sp->image.uuid, uuid, sizeof(*uuid)))
60 			return &sp->image;
61 	}
62 	return NULL;
63 }
64 
65 bool is_sp_ctx(struct ts_ctx *ctx)
66 {
67 	return ctx && (ctx->ops == &sp_ops);
68 }
69 
70 static void set_sp_ctx_ops(struct ts_ctx *ctx)
71 {
72 	ctx->ops = &sp_ops;
73 }
74 
75 TEE_Result sp_find_session_id(const TEE_UUID *uuid, uint32_t *session_id)
76 {
77 	struct sp_session *s = NULL;
78 
79 	TAILQ_FOREACH(s, &open_sp_sessions, link) {
80 		if (!memcmp(&s->ts_sess.ctx->uuid, uuid, sizeof(*uuid))) {
81 			if (s->state == sp_dead)
82 				return TEE_ERROR_TARGET_DEAD;
83 
84 			*session_id  = s->endpoint_id;
85 			return TEE_SUCCESS;
86 		}
87 	}
88 
89 	return TEE_ERROR_ITEM_NOT_FOUND;
90 }
91 
92 struct sp_session *sp_get_session(uint32_t session_id)
93 {
94 	struct sp_session *s = NULL;
95 
96 	TAILQ_FOREACH(s, &open_sp_sessions, link) {
97 		if (s->endpoint_id == session_id)
98 			return s;
99 	}
100 
101 	return NULL;
102 }
103 
104 TEE_Result sp_partition_info_get_all(struct ffa_partition_info *fpi,
105 				     size_t *elem_count)
106 {
107 	size_t in_count = *elem_count;
108 	struct sp_session *s = NULL;
109 	size_t count = 0;
110 
111 	TAILQ_FOREACH(s, &open_sp_sessions, link) {
112 		if (s->state == sp_dead)
113 			continue;
114 		if (count < in_count) {
115 			spmc_fill_partition_entry(fpi, s->endpoint_id, 1);
116 			fpi++;
117 		}
118 		count++;
119 	}
120 
121 	*elem_count = count;
122 	if (count > in_count)
123 		return TEE_ERROR_SHORT_BUFFER;
124 
125 	return TEE_SUCCESS;
126 }
127 
128 bool sp_has_exclusive_access(struct sp_mem_map_region *mem,
129 			     struct user_mode_ctx *uctx)
130 {
131 	/*
132 	 * Check that we have access to the region if it is supposed to be
133 	 * mapped to the current context.
134 	 */
135 	if (uctx) {
136 		struct vm_region *region = NULL;
137 
138 		/* Make sure that each mobj belongs to the SP */
139 		TAILQ_FOREACH(region, &uctx->vm_info.regions, link) {
140 			if (region->mobj == mem->mobj)
141 				break;
142 		}
143 
144 		if (!region)
145 			return false;
146 	}
147 
148 	/* Check that it is not shared with another SP */
149 	return !sp_mem_is_shared(mem);
150 }
151 
152 static uint16_t new_session_id(struct sp_sessions_head *open_sessions)
153 {
154 	struct sp_session *last = NULL;
155 	uint16_t id = SPMC_ENDPOINT_ID + 1;
156 
157 	last = TAILQ_LAST(open_sessions, sp_sessions_head);
158 	if (last)
159 		id = last->endpoint_id + 1;
160 
161 	assert(id > SPMC_ENDPOINT_ID);
162 	return id;
163 }
164 
165 static TEE_Result sp_create_ctx(const TEE_UUID *uuid, struct sp_session *s)
166 {
167 	TEE_Result res = TEE_SUCCESS;
168 	struct sp_ctx *spc = NULL;
169 
170 	/* Register context */
171 	spc = calloc(1, sizeof(struct sp_ctx));
172 	if (!spc)
173 		return TEE_ERROR_OUT_OF_MEMORY;
174 
175 	spc->uctx.ts_ctx = &spc->ts_ctx;
176 	spc->open_session = s;
177 	s->ts_sess.ctx = &spc->ts_ctx;
178 	spc->ts_ctx.uuid = *uuid;
179 
180 	res = vm_info_init(&spc->uctx);
181 	if (res)
182 		goto err;
183 
184 	set_sp_ctx_ops(&spc->ts_ctx);
185 
186 	return TEE_SUCCESS;
187 
188 err:
189 	free(spc);
190 	return res;
191 }
192 
193 static TEE_Result sp_create_session(struct sp_sessions_head *open_sessions,
194 				    const TEE_UUID *uuid,
195 				    struct sp_session **sess)
196 {
197 	TEE_Result res = TEE_SUCCESS;
198 	struct sp_session *s = calloc(1, sizeof(struct sp_session));
199 
200 	if (!s)
201 		return TEE_ERROR_OUT_OF_MEMORY;
202 
203 	s->endpoint_id = new_session_id(open_sessions);
204 	if (!s->endpoint_id) {
205 		res = TEE_ERROR_OVERFLOW;
206 		goto err;
207 	}
208 
209 	DMSG("Loading Secure Partition %pUl", (void *)uuid);
210 	res = sp_create_ctx(uuid, s);
211 	if (res)
212 		goto err;
213 
214 	TAILQ_INSERT_TAIL(open_sessions, s, link);
215 	*sess = s;
216 	return TEE_SUCCESS;
217 
218 err:
219 	free(s);
220 	return res;
221 }
222 
223 static TEE_Result sp_init_set_registers(struct sp_ctx *ctx)
224 {
225 	struct thread_ctx_regs *sp_regs = &ctx->sp_regs;
226 
227 	memset(sp_regs, 0, sizeof(*sp_regs));
228 	sp_regs->sp = ctx->uctx.stack_ptr;
229 	sp_regs->pc = ctx->uctx.entry_func;
230 
231 	return TEE_SUCCESS;
232 }
233 
234 TEE_Result sp_map_shared(struct sp_session *s,
235 			 struct sp_mem_receiver *receiver,
236 			 struct sp_mem *smem,
237 			 uint64_t *va)
238 {
239 	TEE_Result res = TEE_SUCCESS;
240 	struct sp_ctx *ctx = NULL;
241 	uint32_t perm = TEE_MATTR_UR;
242 	struct sp_mem_map_region *reg = NULL;
243 
244 	ctx = to_sp_ctx(s->ts_sess.ctx);
245 
246 	/* Get the permission */
247 	if (receiver->perm.perm & FFA_MEM_ACC_EXE)
248 		perm |= TEE_MATTR_UX;
249 
250 	if (receiver->perm.perm & FFA_MEM_ACC_RW) {
251 		if (receiver->perm.perm & FFA_MEM_ACC_EXE)
252 			return TEE_ERROR_ACCESS_CONFLICT;
253 
254 		perm |= TEE_MATTR_UW;
255 	}
256 	/*
257 	 * Currently we don't support passing a va. We can't guarantee that the
258 	 * full region will be mapped in a contiguous region. A smem->region can
259 	 * have multiple mobj for one share. Currently there doesn't seem to be
260 	 * an option to guarantee that these will be mapped in a contiguous va
261 	 * space.
262 	 */
263 	if (*va)
264 		return TEE_ERROR_NOT_SUPPORTED;
265 
266 	SLIST_FOREACH(reg, &smem->regions, link) {
267 		res = vm_map(&ctx->uctx, va, reg->page_count * SMALL_PAGE_SIZE,
268 			     perm, 0, reg->mobj, reg->page_offset);
269 
270 		if (res != TEE_SUCCESS) {
271 			EMSG("Failed to map memory region %#"PRIx32, res);
272 			return res;
273 		}
274 	}
275 	return TEE_SUCCESS;
276 }
277 
278 TEE_Result sp_unmap_ffa_regions(struct sp_session *s, struct sp_mem *smem)
279 {
280 	TEE_Result res = TEE_SUCCESS;
281 	vaddr_t vaddr = 0;
282 	size_t len = 0;
283 	struct sp_ctx *ctx = to_sp_ctx(s->ts_sess.ctx);
284 	struct sp_mem_map_region *reg = NULL;
285 
286 	SLIST_FOREACH(reg, &smem->regions, link) {
287 		vaddr = (vaddr_t)sp_mem_get_va(&ctx->uctx, reg->page_offset,
288 					       reg->mobj);
289 		len = reg->page_count * SMALL_PAGE_SIZE;
290 
291 		res = vm_unmap(&ctx->uctx, vaddr, len);
292 		if (res != TEE_SUCCESS)
293 			return res;
294 	}
295 
296 	return TEE_SUCCESS;
297 }
298 
299 static TEE_Result sp_open_session(struct sp_session **sess,
300 				  struct sp_sessions_head *open_sessions,
301 				  const TEE_UUID *uuid)
302 {
303 	TEE_Result res = TEE_SUCCESS;
304 	struct sp_session *s = NULL;
305 	struct sp_ctx *ctx = NULL;
306 
307 	if (!find_secure_partition(uuid))
308 		return TEE_ERROR_ITEM_NOT_FOUND;
309 
310 	res = sp_create_session(open_sessions, uuid, &s);
311 	if (res != TEE_SUCCESS) {
312 		DMSG("sp_create_session failed %#"PRIx32, res);
313 		return res;
314 	}
315 
316 	ctx = to_sp_ctx(s->ts_sess.ctx);
317 	assert(ctx);
318 	if (!ctx)
319 		return TEE_ERROR_TARGET_DEAD;
320 	*sess = s;
321 
322 	ts_push_current_session(&s->ts_sess);
323 	/* Load the SP using ldelf. */
324 	ldelf_load_ldelf(&ctx->uctx);
325 	res = ldelf_init_with_ldelf(&s->ts_sess, &ctx->uctx);
326 
327 	if (res != TEE_SUCCESS) {
328 		EMSG("Failed. loading SP using ldelf %#"PRIx32, res);
329 		ts_pop_current_session();
330 		return TEE_ERROR_TARGET_DEAD;
331 	}
332 
333 	/* Make the SP ready for its first run */
334 	s->state = sp_idle;
335 	s->caller_id = 0;
336 	sp_init_set_registers(ctx);
337 	ts_pop_current_session();
338 
339 	return TEE_SUCCESS;
340 }
341 
342 static TEE_Result sp_dt_get_u64(const void *fdt, int node, const char *property,
343 				uint64_t *value)
344 {
345 	const fdt64_t *p = NULL;
346 	int len = 0;
347 
348 	p = fdt_getprop(fdt, node, property, &len);
349 	if (!p || len != sizeof(*p))
350 		return TEE_ERROR_ITEM_NOT_FOUND;
351 
352 	*value = fdt64_to_cpu(*p);
353 
354 	return TEE_SUCCESS;
355 }
356 
357 static TEE_Result sp_dt_get_u32(const void *fdt, int node, const char *property,
358 				uint32_t *value)
359 {
360 	const fdt32_t *p = NULL;
361 	int len = 0;
362 
363 	p = fdt_getprop(fdt, node, property, &len);
364 	if (!p || len != sizeof(*p))
365 		return TEE_ERROR_ITEM_NOT_FOUND;
366 
367 	*value = fdt32_to_cpu(*p);
368 
369 	return TEE_SUCCESS;
370 }
371 
372 static TEE_Result check_fdt(const void * const fdt, const TEE_UUID *uuid)
373 {
374 	int len = 0;
375 	const fdt32_t *prop = NULL;
376 	int i = 0;
377 	const struct fdt_property *description = NULL;
378 	int description_name_len = 0;
379 	uint32_t uuid_array[4] = { 0 };
380 	TEE_UUID fdt_uuid = { };
381 
382 	if (fdt_node_check_compatible(fdt, 0, "arm,ffa-manifest-1.0")) {
383 		EMSG("Failed loading SP, manifest not found");
384 		return TEE_ERROR_BAD_PARAMETERS;
385 	}
386 
387 	description = fdt_get_property(fdt, 0, "description",
388 				       &description_name_len);
389 	if (description)
390 		DMSG("Loading SP: %s", description->data);
391 
392 	prop = fdt_getprop(fdt, 0, "uuid", &len);
393 	if (!prop || len != 16) {
394 		EMSG("Missing or invalid UUID in SP manifest");
395 		return TEE_ERROR_BAD_FORMAT;
396 	}
397 
398 	for (i = 0; i < 4; i++)
399 		uuid_array[i] = fdt32_to_cpu(prop[i]);
400 	tee_uuid_from_octets(&fdt_uuid, (uint8_t *)uuid_array);
401 
402 	if (memcmp(uuid, &fdt_uuid, sizeof(fdt_uuid))) {
403 		EMSG("Failed loading SP, UUID mismatch");
404 		return TEE_ERROR_BAD_FORMAT;
405 	}
406 
407 	return TEE_SUCCESS;
408 }
409 
410 /*
411  * sp_init_info allocates and maps the sp_ffa_init_info for the SP. It will copy
412  * the fdt into the allocated page(s) and return a pointer to the new location
413  * of the fdt. This pointer can be used to update data inside the fdt.
414  */
415 static TEE_Result sp_init_info(struct sp_ctx *ctx, struct thread_smc_args *args,
416 			       const void * const input_fdt, vaddr_t *va,
417 			       size_t *num_pgs, void **fdt_copy)
418 {
419 	struct sp_ffa_init_info *info = NULL;
420 	int nvp_count = 1;
421 	size_t nvp_size = sizeof(struct sp_name_value_pair) * nvp_count;
422 	size_t info_size = sizeof(*info) + nvp_size;
423 	size_t fdt_size = fdt_totalsize(input_fdt);
424 	TEE_Result res = TEE_SUCCESS;
425 	uint32_t perm = TEE_MATTR_URW | TEE_MATTR_PRW;
426 	struct fobj *f = NULL;
427 	struct mobj *m = NULL;
428 	static const char fdt_name[16] = "TYPE_DT\0\0\0\0\0\0\0\0";
429 
430 	*num_pgs = ROUNDUP(fdt_size + info_size, SMALL_PAGE_SIZE) /
431 		   SMALL_PAGE_SIZE;
432 
433 	f = fobj_sec_mem_alloc(*num_pgs);
434 	m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED);
435 
436 	fobj_put(f);
437 	if (!m)
438 		return TEE_ERROR_OUT_OF_MEMORY;
439 
440 	res = vm_map(&ctx->uctx, va, fdt_size + info_size,
441 		     perm, 0, m, 0);
442 	mobj_put(m);
443 	if (res)
444 		return res;
445 
446 	info = (struct sp_ffa_init_info *)*va;
447 
448 	/* magic field is 4 bytes, we don't copy /0 byte. */
449 	memcpy(&info->magic, "FF-A", 4);
450 	info->count = nvp_count;
451 	args->a0 = (vaddr_t)info;
452 
453 	/*
454 	 * Store the fdt after the boot_info and store the pointer in the
455 	 * first element.
456 	 */
457 	COMPILE_TIME_ASSERT(sizeof(info->nvp[0].name) == sizeof(fdt_name));
458 	memcpy(info->nvp[0].name, fdt_name, sizeof(fdt_name));
459 	info->nvp[0].value = *va + info_size;
460 	info->nvp[0].size = fdt_size;
461 	memcpy((void *)info->nvp[0].value, input_fdt, fdt_size);
462 	*fdt_copy = (void *)info->nvp[0].value;
463 
464 	return TEE_SUCCESS;
465 }
466 
467 static TEE_Result handle_fdt_dev_regions(struct sp_ctx *ctx, void *fdt)
468 {
469 	int node = 0;
470 	int subnode = 0;
471 	TEE_Result res = TEE_SUCCESS;
472 	const char *dt_device_match_table = {
473 		"arm,ffa-manifest-device-regions",
474 	};
475 
476 	/*
477 	 * Device regions are optional in the SP manifest, it's not an error if
478 	 * we don't find any
479 	 */
480 	node = fdt_node_offset_by_compatible(fdt, 0, dt_device_match_table);
481 	if (node < 0)
482 		return TEE_SUCCESS;
483 
484 	fdt_for_each_subnode(subnode, fdt, node) {
485 		uint64_t base_addr = 0;
486 		uint32_t pages_cnt = 0;
487 		uint32_t attributes = 0;
488 		struct mobj *m = NULL;
489 		bool is_secure = true;
490 		uint32_t perm = 0;
491 		vaddr_t va = 0;
492 		unsigned int idx = 0;
493 
494 		/*
495 		 * Physical base address of a device MMIO region.
496 		 * Currently only physically contiguous region is supported.
497 		 */
498 		if (sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) {
499 			EMSG("Mandatory field is missing: base-address");
500 			return TEE_ERROR_BAD_FORMAT;
501 		}
502 
503 		/* Total size of MMIO region as count of 4K pages */
504 		if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) {
505 			EMSG("Mandatory field is missing: pages-count");
506 			return TEE_ERROR_BAD_FORMAT;
507 		}
508 
509 		/* Data access, instruction access and security attributes */
510 		if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) {
511 			EMSG("Mandatory field is missing: attributes");
512 			return TEE_ERROR_BAD_FORMAT;
513 		}
514 
515 		/* Check instruction and data access permissions */
516 		switch (attributes & SP_MANIFEST_ATTR_RWX) {
517 		case SP_MANIFEST_ATTR_RO:
518 			perm = TEE_MATTR_UR;
519 			break;
520 		case SP_MANIFEST_ATTR_RW:
521 			perm = TEE_MATTR_URW;
522 			break;
523 		default:
524 			EMSG("Invalid memory access permissions");
525 			return TEE_ERROR_BAD_FORMAT;
526 		}
527 
528 		/*
529 		 * The SP is a secure endpoint, security attribute can be
530 		 * secure or non-secure
531 		 */
532 		if (attributes & SP_MANIFEST_ATTR_NSEC)
533 			is_secure = false;
534 
535 		/* Memory attributes must be Device-nGnRnE */
536 		m = sp_mem_new_mobj(pages_cnt, TEE_MATTR_MEM_TYPE_STRONGLY_O,
537 				    is_secure);
538 		if (!m)
539 			return TEE_ERROR_OUT_OF_MEMORY;
540 
541 		res = sp_mem_add_pages(m, &idx, (paddr_t)base_addr, pages_cnt);
542 		if (res) {
543 			mobj_put(m);
544 			return res;
545 		}
546 
547 		res = vm_map(&ctx->uctx, &va, pages_cnt * SMALL_PAGE_SIZE,
548 			     perm, 0, m, 0);
549 		mobj_put(m);
550 		if (res)
551 			return res;
552 
553 		/*
554 		 * Overwrite the device region's PA in the fdt with the VA. This
555 		 * fdt will be passed to the SP.
556 		 */
557 		res = fdt_setprop_u64(fdt, subnode, "base-address", va);
558 
559 		/*
560 		 * Unmap the region if the overwrite failed since the SP won't
561 		 * be able to access it without knowing the VA.
562 		 */
563 		if (res) {
564 			vm_unmap(&ctx->uctx, va, pages_cnt * SMALL_PAGE_SIZE);
565 			return res;
566 		}
567 	}
568 
569 	return TEE_SUCCESS;
570 }
571 
572 static TEE_Result handle_tpm_event_log(struct sp_ctx *ctx, void *fdt)
573 {
574 	uint32_t perm = TEE_MATTR_URW | TEE_MATTR_PRW;
575 	uint32_t dummy_size __maybe_unused = 0;
576 	TEE_Result res = TEE_SUCCESS;
577 	size_t page_count = 0;
578 	struct fobj *f = NULL;
579 	struct mobj *m = NULL;
580 	vaddr_t log_addr = 0;
581 	size_t log_size = 0;
582 	int node = 0;
583 
584 	node = fdt_node_offset_by_compatible(fdt, 0, "arm,tpm_event_log");
585 	if (node < 0)
586 		return TEE_SUCCESS;
587 
588 	/* Checking the existence and size of the event log properties */
589 	if (sp_dt_get_u64(fdt, node, "tpm_event_log_addr", &log_addr)) {
590 		EMSG("tpm_event_log_addr not found or has invalid size");
591 		return TEE_ERROR_BAD_FORMAT;
592 	}
593 
594 	if (sp_dt_get_u32(fdt, node, "tpm_event_log_size", &dummy_size)) {
595 		EMSG("tpm_event_log_size not found or has invalid size");
596 		return TEE_ERROR_BAD_FORMAT;
597 	}
598 
599 	/* Validating event log */
600 	res = tpm_get_event_log_size(&log_size);
601 	if (res)
602 		return res;
603 
604 	if (!log_size) {
605 		EMSG("Empty TPM event log was provided");
606 		return TEE_ERROR_ITEM_NOT_FOUND;
607 	}
608 
609 	/* Allocating memory area for the event log to share with the SP */
610 	page_count = ROUNDUP_DIV(log_size, SMALL_PAGE_SIZE);
611 
612 	f = fobj_sec_mem_alloc(page_count);
613 	m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED);
614 	fobj_put(f);
615 	if (!m)
616 		return TEE_ERROR_OUT_OF_MEMORY;
617 
618 	res = vm_map(&ctx->uctx, &log_addr, log_size, perm, 0, m, 0);
619 	mobj_put(m);
620 	if (res)
621 		return res;
622 
623 	/* Copy event log */
624 	res = tpm_get_event_log((void *)log_addr, &log_size);
625 	if (res)
626 		goto err_unmap;
627 
628 	/* Setting event log details in the manifest */
629 	res = fdt_setprop_u64(fdt, node, "tpm_event_log_addr", log_addr);
630 	if (res)
631 		goto err_unmap;
632 
633 	res = fdt_setprop_u32(fdt, node, "tpm_event_log_size", log_size);
634 	if (res)
635 		goto err_unmap;
636 
637 	return TEE_SUCCESS;
638 
639 err_unmap:
640 	vm_unmap(&ctx->uctx, log_addr, log_size);
641 
642 	return res;
643 }
644 
645 static TEE_Result sp_init_uuid(const TEE_UUID *uuid, const void * const fdt)
646 {
647 	TEE_Result res = TEE_SUCCESS;
648 	struct sp_session *sess = NULL;
649 	struct thread_smc_args args = { };
650 	vaddr_t va = 0;
651 	size_t num_pgs = 0;
652 	struct sp_ctx *ctx = NULL;
653 	void *fdt_copy = NULL;
654 
655 	res = sp_open_session(&sess,
656 			      &open_sp_sessions,
657 			      uuid);
658 	if (res)
659 		return res;
660 
661 	res = check_fdt(fdt, uuid);
662 	if (res)
663 		return res;
664 
665 	ctx = to_sp_ctx(sess->ts_sess.ctx);
666 	ts_push_current_session(&sess->ts_sess);
667 
668 	res = sp_init_info(ctx, &args, fdt, &va, &num_pgs, &fdt_copy);
669 	if (res)
670 		goto out;
671 
672 	res = handle_fdt_dev_regions(ctx, fdt_copy);
673 	if (res)
674 		goto out;
675 
676 	if (IS_ENABLED(CFG_CORE_TPM_EVENT_LOG)) {
677 		res = handle_tpm_event_log(ctx, fdt_copy);
678 		if (res)
679 			goto out;
680 	}
681 
682 	ts_pop_current_session();
683 
684 	if (sp_enter(&args, sess)) {
685 		vm_unmap(&ctx->uctx, va, num_pgs);
686 		return FFA_ABORTED;
687 	}
688 
689 	spmc_sp_msg_handler(&args, sess);
690 
691 	ts_push_current_session(&sess->ts_sess);
692 out:
693 	/* Free the boot info page from the SP memory */
694 	vm_unmap(&ctx->uctx, va, num_pgs);
695 	ts_pop_current_session();
696 
697 	return res;
698 }
699 
700 TEE_Result sp_enter(struct thread_smc_args *args, struct sp_session *sp)
701 {
702 	TEE_Result res = FFA_OK;
703 	struct sp_ctx *ctx = to_sp_ctx(sp->ts_sess.ctx);
704 
705 	ctx->sp_regs.x[0] = args->a0;
706 	ctx->sp_regs.x[1] = args->a1;
707 	ctx->sp_regs.x[2] = args->a2;
708 	ctx->sp_regs.x[3] = args->a3;
709 	ctx->sp_regs.x[4] = args->a4;
710 	ctx->sp_regs.x[5] = args->a5;
711 	ctx->sp_regs.x[6] = args->a6;
712 	ctx->sp_regs.x[7] = args->a7;
713 
714 	res = sp->ts_sess.ctx->ops->enter_invoke_cmd(&sp->ts_sess, 0);
715 
716 	args->a0 = ctx->sp_regs.x[0];
717 	args->a1 = ctx->sp_regs.x[1];
718 	args->a2 = ctx->sp_regs.x[2];
719 	args->a3 = ctx->sp_regs.x[3];
720 	args->a4 = ctx->sp_regs.x[4];
721 	args->a5 = ctx->sp_regs.x[5];
722 	args->a6 = ctx->sp_regs.x[6];
723 	args->a7 = ctx->sp_regs.x[7];
724 
725 	return res;
726 }
727 
728 static TEE_Result sp_enter_invoke_cmd(struct ts_session *s,
729 				      uint32_t cmd __unused)
730 {
731 	struct sp_ctx *ctx = to_sp_ctx(s->ctx);
732 	TEE_Result res = TEE_SUCCESS;
733 	uint32_t exceptions = 0;
734 	uint64_t cpsr = 0;
735 	struct sp_session *sp_s = to_sp_session(s);
736 	struct ts_session *sess = NULL;
737 	struct thread_ctx_regs *sp_regs = NULL;
738 	uint32_t panicked = false;
739 	uint32_t panic_code = 0;
740 
741 	bm_timestamp();
742 
743 	sp_regs = &ctx->sp_regs;
744 	ts_push_current_session(s);
745 
746 	cpsr = sp_regs->cpsr;
747 	sp_regs->cpsr = read_daif() & (SPSR_64_DAIF_MASK << SPSR_64_DAIF_SHIFT);
748 
749 	exceptions = thread_mask_exceptions(THREAD_EXCP_ALL);
750 	__thread_enter_user_mode(sp_regs, &panicked, &panic_code);
751 	sp_regs->cpsr = cpsr;
752 	thread_unmask_exceptions(exceptions);
753 
754 	thread_user_clear_vfp(&ctx->uctx);
755 
756 	if (panicked) {
757 		DMSG("SP panicked with code  %#"PRIx32, panic_code);
758 		abort_print_current_ts();
759 
760 		sess = ts_pop_current_session();
761 		cpu_spin_lock(&sp_s->spinlock);
762 		sp_s->state = sp_dead;
763 		cpu_spin_unlock(&sp_s->spinlock);
764 
765 		return TEE_ERROR_TARGET_DEAD;
766 	}
767 
768 	sess = ts_pop_current_session();
769 	assert(sess == s);
770 
771 	bm_timestamp();
772 
773 	return res;
774 }
775 
776 /* We currently don't support 32 bits */
777 #ifdef ARM64
778 static void sp_svc_store_registers(struct thread_svc_regs *regs,
779 				   struct thread_ctx_regs *sp_regs)
780 {
781 	COMPILE_TIME_ASSERT(sizeof(sp_regs->x[0]) == sizeof(regs->x0));
782 	memcpy(sp_regs->x, &regs->x0, 31 * sizeof(regs->x0));
783 	sp_regs->pc = regs->elr;
784 	sp_regs->sp = regs->sp_el0;
785 }
786 #endif
787 
788 static bool sp_handle_svc(struct thread_svc_regs *regs)
789 {
790 	struct ts_session *ts = ts_get_current_session();
791 	struct sp_ctx *uctx = to_sp_ctx(ts->ctx);
792 	struct sp_session *s = uctx->open_session;
793 
794 	assert(s);
795 
796 	sp_svc_store_registers(regs, &uctx->sp_regs);
797 
798 	regs->x0 = 0;
799 	regs->x1 = 0; /* panic */
800 	regs->x2 = 0; /* panic code */
801 
802 	/*
803 	 * All the registers of the SP are saved in the SP session by the SVC
804 	 * handler.
805 	 * We always return to S-El1 after handling the SVC. We will continue
806 	 * in sp_enter_invoke_cmd() (return from __thread_enter_user_mode).
807 	 * The sp_enter() function copies the FF-A parameters (a0-a7) from the
808 	 * saved registers to the thread_smc_args. The thread_smc_args object is
809 	 * afterward used by the spmc_sp_msg_handler() to handle the
810 	 * FF-A message send by the SP.
811 	 */
812 	return false;
813 }
814 
815 static void sp_dump_state(struct ts_ctx *ctx)
816 {
817 	struct sp_ctx *utc = to_sp_ctx(ctx);
818 
819 	if (utc->uctx.dump_entry_func) {
820 		TEE_Result res = ldelf_dump_state(&utc->uctx);
821 
822 		if (!res || res == TEE_ERROR_TARGET_DEAD)
823 			return;
824 	}
825 
826 	user_mode_ctx_print_mappings(&utc->uctx);
827 }
828 
829 /*
830  * Note: this variable is weak just to ease breaking its dependency chain
831  * when added to the unpaged area.
832  */
833 const struct ts_ops sp_ops __weak __relrodata_unpaged("sp_ops") = {
834 	.enter_invoke_cmd = sp_enter_invoke_cmd,
835 	.handle_svc = sp_handle_svc,
836 	.dump_state = sp_dump_state,
837 };
838 
839 static TEE_Result sp_init_all(void)
840 {
841 	TEE_Result res = TEE_SUCCESS;
842 	const struct sp_image *sp = NULL;
843 	char __maybe_unused msg[60] = { '\0', };
844 
845 	for_each_secure_partition(sp) {
846 		if (sp->image.uncompressed_size)
847 			snprintf(msg, sizeof(msg),
848 				 " (compressed, uncompressed %u)",
849 				 sp->image.uncompressed_size);
850 		else
851 			msg[0] = '\0';
852 		DMSG("SP %pUl size %u%s", (void *)&sp->image.uuid,
853 		     sp->image.size, msg);
854 
855 		res = sp_init_uuid(&sp->image.uuid, sp->fdt);
856 
857 		if (res != TEE_SUCCESS) {
858 			EMSG("Failed initializing SP(%pUl) err:%#"PRIx32,
859 			     &sp->image.uuid, res);
860 			if (!IS_ENABLED(CFG_SP_SKIP_FAILED))
861 				panic();
862 		}
863 	}
864 
865 	return TEE_SUCCESS;
866 }
867 
868 boot_final(sp_init_all);
869 
870 static TEE_Result secure_partition_open(const TEE_UUID *uuid,
871 					struct ts_store_handle **h)
872 {
873 	return emb_ts_open(uuid, h, find_secure_partition);
874 }
875 
876 REGISTER_SP_STORE(2) = {
877 	.description = "SP store",
878 	.open = secure_partition_open,
879 	.get_size = emb_ts_get_size,
880 	.get_tag = emb_ts_get_tag,
881 	.read = emb_ts_read,
882 	.close = emb_ts_close,
883 };
884