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