xref: /optee_os/core/arch/arm/kernel/stmm_sp.c (revision 145ae446b961c32d566596c90d6a9e167c6591e0)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2019, Linaro Limited
4  * Copyright (c) 2020, Arm Limited.
5  */
6 
7 #include <crypto/crypto.h>
8 #include <ffa.h>
9 #include <kernel/abort.h>
10 #include <kernel/stmm_sp.h>
11 #include <kernel/user_mode_ctx.h>
12 #include <mm/fobj.h>
13 #include <mm/mobj.h>
14 #include <mm/vm.h>
15 #include <pta_stmm.h>
16 #include <tee_api_defines_extensions.h>
17 #include <tee/tee_pobj.h>
18 #include <tee/tee_svc.h>
19 #include <tee/tee_svc_storage.h>
20 #include <zlib.h>
21 
22 #include "thread_private.h"
23 
24 #ifdef ARM64
25 #define SVC_REGS_A0(_regs)	((_regs)->x0)
26 #define SVC_REGS_A1(_regs)	((_regs)->x1)
27 #define SVC_REGS_A2(_regs)	((_regs)->x2)
28 #define SVC_REGS_A3(_regs)	((_regs)->x3)
29 #define SVC_REGS_A4(_regs)	((_regs)->x4)
30 #define SVC_REGS_A5(_regs)	((_regs)->x5)
31 #define SVC_REGS_A6(_regs)	((_regs)->x6)
32 #define SVC_REGS_A7(_regs)	((_regs)->x7)
33 #define __FFA_SVC_RPMB_READ		FFA_SVC_RPMB_READ
34 #define __FFA_SVC_RPMB_WRITE		FFA_SVC_RPMB_WRITE
35 #define __FFA_SVC_MEMORY_ATTRIBUTES_GET	FFA_SVC_MEMORY_ATTRIBUTES_GET_64
36 #define __FFA_SVC_MEMORY_ATTRIBUTES_SET	FFA_SVC_MEMORY_ATTRIBUTES_SET_64
37 #define __FFA_MSG_SEND_DIRECT_RESP	FFA_MSG_SEND_DIRECT_RESP_64
38 #define __FFA_MSG_SEND_DIRECT_REQ	FFA_MSG_SEND_DIRECT_REQ_64
39 #endif
40 #ifdef ARM32
41 #define SVC_REGS_A0(_regs)	((_regs)->r0)
42 #define SVC_REGS_A1(_regs)	((_regs)->r1)
43 #define SVC_REGS_A2(_regs)	((_regs)->r2)
44 #define SVC_REGS_A3(_regs)	((_regs)->r3)
45 #define SVC_REGS_A4(_regs)	((_regs)->r4)
46 #define SVC_REGS_A5(_regs)	((_regs)->r5)
47 #define SVC_REGS_A6(_regs)	((_regs)->r6)
48 #define SVC_REGS_A7(_regs)	((_regs)->r7)
49 #define __FFA_SVC_RPMB_READ		FFA_SVC_RPMB_READ_32
50 #define __FFA_SVC_RPMB_WRITE		FFA_SVC_RPMB_WRITE_32
51 #define __FFA_SVC_MEMORY_ATTRIBUTES_GET	FFA_SVC_MEMORY_ATTRIBUTES_GET_32
52 #define __FFA_SVC_MEMORY_ATTRIBUTES_SET	FFA_SVC_MEMORY_ATTRIBUTES_SET_32
53 #define __FFA_MSG_SEND_DIRECT_RESP	FFA_MSG_SEND_DIRECT_RESP_32
54 #define __FFA_MSG_SEND_DIRECT_REQ	FFA_MSG_SEND_DIRECT_REQ_32
55 #endif
56 
57 static const TEE_UUID stmm_uuid = PTA_STMM_UUID;
58 
59 /*
60  * Once a complete FFA spec is added, these will become discoverable.
61  * Until then these are considered part of the internal ABI between
62  * OP-TEE and StMM.
63  */
64 static const uint16_t stmm_id = 1U;
65 static const uint16_t stmm_pta_id = 2U;
66 static const uint16_t mem_mgr_id = 3U;
67 static const uint16_t ffa_storage_id = 4U;
68 
69 static const unsigned int stmm_stack_size = 4 * SMALL_PAGE_SIZE;
70 static const unsigned int stmm_heap_size = 398 * SMALL_PAGE_SIZE;
71 static const unsigned int stmm_sec_buf_size = SMALL_PAGE_SIZE;
72 static const unsigned int stmm_ns_comm_buf_size = SMALL_PAGE_SIZE;
73 
74 extern unsigned char stmm_image[];
75 extern const unsigned int stmm_image_size;
76 extern const unsigned int stmm_image_uncompressed_size;
77 
78 static struct stmm_ctx *stmm_alloc_ctx(const TEE_UUID *uuid)
79 {
80 	TEE_Result res = TEE_SUCCESS;
81 	struct stmm_ctx *spc = NULL;
82 
83 	spc = calloc(1, sizeof(*spc));
84 	if (!spc)
85 		return NULL;
86 
87 	spc->ta_ctx.ts_ctx.ops = &stmm_sp_ops;
88 	spc->ta_ctx.ts_ctx.uuid = *uuid;
89 	spc->ta_ctx.flags = TA_FLAG_SINGLE_INSTANCE |
90 			    TA_FLAG_INSTANCE_KEEP_ALIVE;
91 	spc->uctx.ts_ctx = &spc->ta_ctx.ts_ctx;
92 
93 	res = vm_info_init(&spc->uctx);
94 	if (res) {
95 		free(spc);
96 		return NULL;
97 	}
98 
99 	spc->ta_ctx.ref_count = 1;
100 	condvar_init(&spc->ta_ctx.busy_cv);
101 
102 	return spc;
103 }
104 
105 static TEE_Result stmm_enter_user_mode(struct stmm_ctx *spc)
106 {
107 	uint32_t exceptions = 0;
108 	uint32_t panic_code = 0;
109 	uint32_t panicked = 0;
110 	uint64_t cntkctl = 0;
111 
112 	exceptions = thread_mask_exceptions(THREAD_EXCP_ALL);
113 	cntkctl = read_cntkctl();
114 	write_cntkctl(cntkctl | CNTKCTL_PL0PCTEN);
115 	__thread_enter_user_mode(&spc->regs, &panicked, &panic_code);
116 	write_cntkctl(cntkctl);
117 	thread_unmask_exceptions(exceptions);
118 
119 	thread_user_clear_vfp(&spc->uctx);
120 
121 	if (panicked) {
122 		abort_print_current_ta();
123 		DMSG("stmm panicked with code %#"PRIx32, panic_code);
124 		return TEE_ERROR_TARGET_DEAD;
125 	}
126 
127 	return TEE_SUCCESS;
128 }
129 
130 #ifdef ARM64
131 static void init_stmm_regs(struct stmm_ctx *spc, unsigned long a0,
132 			   unsigned long a1, unsigned long sp, unsigned long pc)
133 {
134 	spc->regs.x[0] = a0;
135 	spc->regs.x[1] = a1;
136 	spc->regs.sp = sp;
137 	spc->regs.pc = pc;
138 }
139 #endif
140 
141 #ifdef ARM32
142 static uint32_t __maybe_unused get_spsr(void)
143 {
144 	uint32_t s = 0;
145 
146 	s = read_cpsr();
147 	s &= ~(CPSR_MODE_MASK | CPSR_T | ARM32_CPSR_IT_MASK);
148 	s |= CPSR_MODE_USR;
149 
150 	return s;
151 }
152 
153 static void init_stmm_regs(struct stmm_ctx *spc, unsigned long a0,
154 			   unsigned long a1, unsigned long sp, unsigned long pc)
155 {
156 	spc->regs.r0 = a0;
157 	spc->regs.r1 = a1;
158 	spc->regs.usr_sp = sp;
159 	spc->regs.cpsr = get_spsr();
160 	spc->regs.pc = pc;
161 }
162 #endif
163 
164 static TEE_Result alloc_and_map_sp_fobj(struct stmm_ctx *spc, size_t sz,
165 					uint32_t prot, vaddr_t *va)
166 {
167 	size_t num_pgs = ROUNDUP(sz, SMALL_PAGE_SIZE) / SMALL_PAGE_SIZE;
168 	struct fobj *fobj = fobj_ta_mem_alloc(num_pgs);
169 	struct mobj *mobj = mobj_with_fobj_alloc(fobj, NULL);
170 	TEE_Result res = TEE_SUCCESS;
171 
172 	fobj_put(fobj);
173 	if (!mobj)
174 		return TEE_ERROR_OUT_OF_MEMORY;
175 
176 	res = vm_map(&spc->uctx, va, num_pgs * SMALL_PAGE_SIZE,
177 		     prot, 0, mobj, 0);
178 	if (res)
179 		mobj_put(mobj);
180 
181 	return TEE_SUCCESS;
182 }
183 
184 static void *zalloc(void *opaque __unused, unsigned int items,
185 		    unsigned int size)
186 {
187 	return malloc(items * size);
188 }
189 
190 static void zfree(void *opaque __unused, void *address)
191 {
192 	free(address);
193 }
194 
195 static void uncompress_image(void *dst, size_t dst_size, void *src,
196 			     size_t src_size)
197 {
198 	z_stream strm = {
199 		.next_in = src,
200 		.avail_in = src_size,
201 		.next_out = dst,
202 		.avail_out = dst_size,
203 		.zalloc = zalloc,
204 		.zfree = zfree,
205 	};
206 
207 	if (inflateInit(&strm) != Z_OK)
208 		panic("inflateInit");
209 
210 	if (inflate(&strm, Z_SYNC_FLUSH) != Z_STREAM_END)
211 		panic("inflate");
212 
213 	if (inflateEnd(&strm) != Z_OK)
214 		panic("inflateEnd");
215 }
216 
217 static TEE_Result load_stmm(struct stmm_ctx *spc)
218 {
219 	struct stmm_boot_info *boot_info = NULL;
220 	struct stmm_mp_info *mp_info = NULL;
221 	TEE_Result res = TEE_SUCCESS;
222 	vaddr_t sp_addr = 0;
223 	vaddr_t image_addr = 0;
224 	vaddr_t heap_addr = 0;
225 	vaddr_t stack_addr = 0;
226 	vaddr_t sec_buf_addr = 0;
227 	vaddr_t comm_buf_addr = 0;
228 	unsigned int sp_size = 0;
229 	unsigned int uncompressed_size_roundup = 0;
230 
231 	uncompressed_size_roundup = ROUNDUP(stmm_image_uncompressed_size,
232 					    SMALL_PAGE_SIZE);
233 	sp_size = uncompressed_size_roundup + stmm_stack_size +
234 		  stmm_heap_size + stmm_sec_buf_size;
235 	res = alloc_and_map_sp_fobj(spc, sp_size,
236 				    TEE_MATTR_PRW, &sp_addr);
237 	if (res)
238 		return res;
239 
240 	res = alloc_and_map_sp_fobj(spc, stmm_ns_comm_buf_size,
241 				    TEE_MATTR_URW | TEE_MATTR_PRW,
242 				    &comm_buf_addr);
243 	/*
244 	 * We don't need to free the previous instance here, they'll all be
245 	 * handled during the destruction call (stmm_ctx_destroy())
246 	 */
247 	if (res)
248 		return res;
249 
250 	image_addr = sp_addr;
251 	heap_addr = image_addr + uncompressed_size_roundup;
252 	stack_addr = heap_addr + stmm_heap_size;
253 	sec_buf_addr = stack_addr + stmm_stack_size;
254 
255 	vm_set_ctx(&spc->ta_ctx.ts_ctx);
256 	uncompress_image((void *)image_addr, stmm_image_uncompressed_size,
257 			 stmm_image, stmm_image_size);
258 
259 	res = vm_set_prot(&spc->uctx, image_addr, uncompressed_size_roundup,
260 			  TEE_MATTR_URX | TEE_MATTR_PR);
261 	if (res)
262 		return res;
263 
264 	res = vm_set_prot(&spc->uctx, heap_addr, stmm_heap_size,
265 			  TEE_MATTR_URW | TEE_MATTR_PRW);
266 	if (res)
267 		return res;
268 
269 	res = vm_set_prot(&spc->uctx, stack_addr, stmm_stack_size,
270 			  TEE_MATTR_URW | TEE_MATTR_PRW);
271 	if (res)
272 		return res;
273 
274 	res = vm_set_prot(&spc->uctx, sec_buf_addr, stmm_sec_buf_size,
275 			  TEE_MATTR_URW | TEE_MATTR_PRW);
276 	if (res)
277 		return res;
278 
279 	DMSG("stmm load address %#"PRIxVA, image_addr);
280 
281 	boot_info = (struct stmm_boot_info *)sec_buf_addr;
282 	mp_info = (struct stmm_mp_info *)(boot_info + 1);
283 	*boot_info = (struct stmm_boot_info){
284 		.h.type = STMM_PARAM_SP_IMAGE_BOOT_INFO,
285 		.h.version = STMM_PARAM_VERSION_1,
286 		.h.size = sizeof(struct stmm_boot_info),
287 		.h.attr = 0,
288 		.sp_mem_base = sp_addr,
289 		.sp_mem_limit = sp_addr + sp_size,
290 		.sp_image_base = image_addr,
291 		.sp_stack_base = stack_addr,
292 		.sp_heap_base = heap_addr,
293 		.sp_ns_comm_buf_base = comm_buf_addr,
294 		.sp_shared_buf_base = sec_buf_addr,
295 		.sp_image_size = stmm_image_size,
296 		.sp_pcpu_stack_size = stmm_stack_size,
297 		.sp_heap_size = stmm_heap_size,
298 		.sp_ns_comm_buf_size = stmm_ns_comm_buf_size,
299 		.sp_shared_buf_size = stmm_sec_buf_size,
300 		.num_sp_mem_regions = 6,
301 		.num_cpus = 1,
302 		.mp_info = mp_info,
303 	};
304 	mp_info->mpidr = read_mpidr();
305 	mp_info->linear_id = 0;
306 	mp_info->flags = MP_INFO_FLAG_PRIMARY_CPU;
307 	spc->ns_comm_buf_addr = comm_buf_addr;
308 	spc->ns_comm_buf_size = stmm_ns_comm_buf_size;
309 
310 	init_stmm_regs(spc, sec_buf_addr,
311 		       (vaddr_t)(mp_info + 1) - sec_buf_addr,
312 		       stack_addr + stmm_stack_size, image_addr);
313 
314 	return stmm_enter_user_mode(spc);
315 }
316 
317 TEE_Result stmm_init_session(const TEE_UUID *uuid, struct tee_ta_session *sess)
318 {
319 	struct stmm_ctx *spc = NULL;
320 	TEE_Result res = TEE_SUCCESS;
321 
322 	if (memcmp(uuid, &stmm_uuid, sizeof(*uuid)))
323 		return TEE_ERROR_ITEM_NOT_FOUND;
324 
325 	spc = stmm_alloc_ctx(uuid);
326 	if (!spc)
327 		return TEE_ERROR_OUT_OF_MEMORY;
328 
329 	spc->is_initializing = true;
330 
331 	mutex_lock(&tee_ta_mutex);
332 	sess->ts_sess.ctx = &spc->ta_ctx.ts_ctx;
333 	mutex_unlock(&tee_ta_mutex);
334 
335 	ts_push_current_session(&sess->ts_sess);
336 	res = load_stmm(spc);
337 	ts_pop_current_session();
338 	vm_set_ctx(NULL);
339 	if (res) {
340 		sess->ts_sess.ctx = NULL;
341 		spc->ta_ctx.ts_ctx.ops->destroy(&spc->ta_ctx.ts_ctx);
342 
343 		return res;
344 	}
345 
346 	mutex_lock(&tee_ta_mutex);
347 	spc->is_initializing = false;
348 	TAILQ_INSERT_TAIL(&tee_ctxes, &spc->ta_ctx, link);
349 	mutex_unlock(&tee_ta_mutex);
350 
351 	return TEE_SUCCESS;
352 }
353 
354 static TEE_Result stmm_enter_open_session(struct ts_session *s)
355 {
356 	struct stmm_ctx *spc = to_stmm_ctx(s->ctx);
357 	struct tee_ta_session *ta_sess = to_ta_session(s);
358 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE,
359 						TEE_PARAM_TYPE_NONE,
360 						TEE_PARAM_TYPE_NONE,
361 						TEE_PARAM_TYPE_NONE);
362 
363 	if (ta_sess->param->types != exp_pt)
364 		return TEE_ERROR_BAD_PARAMETERS;
365 
366 	if (spc->is_initializing) {
367 		/* StMM is initialized in stmm_init_session() */
368 		ta_sess->err_origin = TEE_ORIGIN_TEE;
369 		return TEE_ERROR_BAD_STATE;
370 	}
371 
372 	return TEE_SUCCESS;
373 }
374 
375 static TEE_Result stmm_enter_invoke_cmd(struct ts_session *s, uint32_t cmd)
376 {
377 	struct stmm_ctx *spc = to_stmm_ctx(s->ctx);
378 	struct tee_ta_session *ta_sess = to_ta_session(s);
379 	TEE_Result res = TEE_SUCCESS;
380 	TEE_Result __maybe_unused tmp_res = TEE_SUCCESS;
381 	unsigned int ns_buf_size = 0;
382 	struct param_mem *mem = NULL;
383 	void *va = NULL;
384 	const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
385 						TEE_PARAM_TYPE_VALUE_OUTPUT,
386 						TEE_PARAM_TYPE_NONE,
387 						TEE_PARAM_TYPE_NONE);
388 
389 	if (cmd != PTA_STMM_CMD_COMMUNICATE)
390 		return TEE_ERROR_BAD_PARAMETERS;
391 
392 	if (ta_sess->param->types != exp_pt)
393 		return TEE_ERROR_BAD_PARAMETERS;
394 
395 	mem = &ta_sess->param->u[0].mem;
396 	ns_buf_size = mem->size;
397 	if (ns_buf_size > spc->ns_comm_buf_size) {
398 		mem->size = spc->ns_comm_buf_size;
399 		return TEE_ERROR_EXCESS_DATA;
400 	}
401 
402 	res = mobj_inc_map(mem->mobj);
403 	if (res)
404 		return res;
405 
406 	va = mobj_get_va(mem->mobj, mem->offs);
407 	if (!va) {
408 		EMSG("Can't get a valid VA for NS buffer");
409 		res = TEE_ERROR_BAD_PARAMETERS;
410 		goto out_va;
411 	}
412 
413 #ifdef ARM64
414 	spc->regs.x[0] = __FFA_MSG_SEND_DIRECT_REQ;
415 	spc->regs.x[1] = (stmm_pta_id << 16) | stmm_id;
416 	spc->regs.x[2] = FFA_PARAM_MBZ;
417 	spc->regs.x[3] = spc->ns_comm_buf_addr;
418 	spc->regs.x[4] = ns_buf_size;
419 	spc->regs.x[5] = 0;
420 	spc->regs.x[6] = 0;
421 	spc->regs.x[7] = 0;
422 #endif
423 #ifdef ARM32
424 	spc->regs.r0 = __FFA_MSG_SEND_DIRECT_REQ;
425 	spc->regs.r1 = (stmm_pta_id << 16) | stmm_id;
426 	spc->regs.r2 = FFA_PARAM_MBZ;
427 	spc->regs.r3 = spc->ns_comm_buf_addr;
428 	spc->regs.r4 = ns_buf_size;
429 	spc->regs.r5 = 0;
430 	spc->regs.r6 = 0;
431 	spc->regs.r7 = 0;
432 #endif
433 
434 	ts_push_current_session(s);
435 
436 	memcpy((void *)spc->ns_comm_buf_addr, va, ns_buf_size);
437 
438 	res = stmm_enter_user_mode(spc);
439 	if (res)
440 		goto out_session;
441 	/*
442 	 * Copy the SPM response from secure partition back to the non-secure
443 	 * buffer of the client that called us.
444 	 */
445 #ifdef ARM64
446 	ta_sess->param->u[1].val.a = spc->regs.x[4];
447 #endif
448 #ifdef ARM32
449 	ta_sess->param->u[1].val.a = spc->regs.r4;
450 #endif
451 
452 	memcpy(va, (void *)spc->ns_comm_buf_addr, ns_buf_size);
453 
454 out_session:
455 	ts_pop_current_session();
456 out_va:
457 	tmp_res = mobj_dec_map(mem->mobj);
458 	assert(!tmp_res);
459 
460 	return res;
461 }
462 
463 static void stmm_enter_close_session(struct ts_session *s __unused)
464 {
465 }
466 
467 static void stmm_dump_state(struct ts_ctx *ctx)
468 {
469 	user_mode_ctx_print_mappings(to_user_mode_ctx(ctx));
470 }
471 
472 static uint32_t stmm_get_instance_id(struct ts_ctx *ctx)
473 {
474 	return to_stmm_ctx(ctx)->uctx.vm_info.asid;
475 }
476 
477 static void stmm_ctx_destroy(struct ts_ctx *ctx)
478 {
479 	struct stmm_ctx *spc = to_stmm_ctx(ctx);
480 
481 	tee_pager_rem_um_areas(&spc->uctx);
482 	vm_info_final(&spc->uctx);
483 	free(spc);
484 }
485 
486 static uint32_t sp_svc_get_mem_attr(vaddr_t va)
487 {
488 	TEE_Result res = TEE_ERROR_BAD_PARAMETERS;
489 	struct ts_session *sess = NULL;
490 	struct stmm_ctx *spc = NULL;
491 	uint16_t attrs = 0;
492 	uint16_t perm = 0;
493 
494 	if (!va)
495 		goto err;
496 
497 	sess = ts_get_current_session();
498 	spc = to_stmm_ctx(sess->ctx);
499 
500 	res = vm_get_prot(&spc->uctx, va, SMALL_PAGE_SIZE, &attrs);
501 	if (res)
502 		goto err;
503 
504 	if (attrs & TEE_MATTR_UR)
505 		perm |= STMM_MEM_ATTR_ACCESS_RO;
506 	else if (attrs & TEE_MATTR_UW)
507 		perm |= STMM_MEM_ATTR_ACCESS_RW;
508 
509 	if (attrs & TEE_MATTR_UX)
510 		perm |= STMM_MEM_ATTR_EXEC;
511 
512 	return perm;
513 err:
514 	return STMM_RET_DENIED;
515 }
516 
517 static int sp_svc_set_mem_attr(vaddr_t va, unsigned int nr_pages, uint32_t perm)
518 {
519 	TEE_Result res = TEE_ERROR_BAD_PARAMETERS;
520 	struct ts_session *sess = NULL;
521 	struct stmm_ctx *spc = NULL;
522 	size_t sz = 0;
523 	uint32_t prot = 0;
524 
525 	if (!va || !nr_pages || MUL_OVERFLOW(nr_pages, SMALL_PAGE_SIZE, &sz))
526 		return STMM_RET_INVALID_PARAM;
527 
528 	if (perm & ~STMM_MEM_ATTR_ALL)
529 		return STMM_RET_INVALID_PARAM;
530 
531 	sess = ts_get_current_session();
532 	spc = to_stmm_ctx(sess->ctx);
533 
534 	if ((perm & STMM_MEM_ATTR_ACCESS_MASK) == STMM_MEM_ATTR_ACCESS_RO)
535 		prot |= TEE_MATTR_UR;
536 	else if ((perm & STMM_MEM_ATTR_ACCESS_MASK) == STMM_MEM_ATTR_ACCESS_RW)
537 		prot |= TEE_MATTR_URW;
538 
539 	if ((perm & STMM_MEM_ATTR_EXEC_NEVER) == STMM_MEM_ATTR_EXEC)
540 		prot |= TEE_MATTR_UX;
541 
542 	res = vm_set_prot(&spc->uctx, va, sz, prot);
543 	if (res)
544 		return STMM_RET_DENIED;
545 
546 	return STMM_RET_SUCCESS;
547 }
548 
549 #ifdef ARM64
550 static void save_sp_ctx(struct stmm_ctx *spc, struct thread_svc_regs *svc_regs)
551 {
552 	size_t n = 0;
553 
554 	/* Save the return values from StMM */
555 	for (n = 0; n <= 7; n++)
556 		spc->regs.x[n] = *(&svc_regs->x0 + n);
557 
558 	spc->regs.sp = svc_regs->sp_el0;
559 	spc->regs.pc = svc_regs->elr;
560 	spc->regs.cpsr = svc_regs->spsr;
561 }
562 #endif
563 
564 #ifdef ARM32
565 static void save_sp_ctx(struct stmm_ctx *spc, struct thread_svc_regs *svc_regs)
566 {
567 	spc->regs.r0 = svc_regs->r0;
568 	spc->regs.r1 = svc_regs->r1;
569 	spc->regs.r2 = svc_regs->r2;
570 	spc->regs.r3 = svc_regs->r3;
571 	spc->regs.r4 = svc_regs->r4;
572 	spc->regs.r5 = svc_regs->r5;
573 	spc->regs.r6 = svc_regs->r6;
574 	spc->regs.r7 = svc_regs->r7;
575 	spc->regs.pc = svc_regs->lr;
576 	spc->regs.cpsr = svc_regs->spsr;
577 	spc->regs.usr_sp = thread_get_usr_sp();
578 }
579 #endif
580 
581 static void return_from_sp_helper(bool panic, uint32_t panic_code,
582 				  struct thread_svc_regs *svc_regs)
583 {
584 	struct ts_session *sess = ts_get_current_session();
585 	struct stmm_ctx *spc = to_stmm_ctx(sess->ctx);
586 
587 	if (panic)
588 		spc->ta_ctx.panicked = true;
589 	else
590 		save_sp_ctx(spc, svc_regs);
591 
592 	SVC_REGS_A0(svc_regs) = 0;
593 	SVC_REGS_A1(svc_regs) = panic;
594 	SVC_REGS_A2(svc_regs) = panic_code;
595 }
596 
597 static void service_compose_direct_resp(struct thread_svc_regs *regs,
598 					uint32_t ret_val)
599 {
600 	uint16_t src_id = 0;
601 	uint16_t dst_id = 0;
602 
603 	/* extract from request */
604 	src_id = (SVC_REGS_A1(regs) >> 16) & UINT16_MAX;
605 	dst_id = SVC_REGS_A1(regs) & UINT16_MAX;
606 
607 	/* compose message */
608 	SVC_REGS_A0(regs) = __FFA_MSG_SEND_DIRECT_RESP;
609 	/* swap endpoint ids */
610 	SVC_REGS_A1(regs) = SHIFT_U32(dst_id, 16) | src_id;
611 	SVC_REGS_A2(regs) = FFA_PARAM_MBZ;
612 	SVC_REGS_A3(regs) = ret_val;
613 	SVC_REGS_A4(regs) = 0;
614 	SVC_REGS_A5(regs) = 0;
615 	SVC_REGS_A6(regs) = 0;
616 	SVC_REGS_A7(regs) = 0;
617 }
618 
619 /*
620  * Combined read from secure partition, this will open, read and
621  * close the file object.
622  */
623 static TEE_Result sec_storage_obj_read(unsigned long storage_id, char *obj_id,
624 				       unsigned long obj_id_len, void *data,
625 				       unsigned long len, unsigned long offset,
626 				       unsigned long flags)
627 {
628 	const struct tee_file_operations *fops = NULL;
629 	TEE_Result res = TEE_ERROR_BAD_STATE;
630 	struct ts_session *sess = NULL;
631 	struct tee_file_handle *fh = NULL;
632 	struct stmm_ctx *spc = NULL;
633 	struct tee_pobj *po = NULL;
634 	size_t file_size = 0;
635 	size_t read_len = 0;
636 
637 	fops = tee_svc_storage_file_ops(storage_id);
638 	if (!fops)
639 		return TEE_ERROR_ITEM_NOT_FOUND;
640 
641 	if (obj_id_len > TEE_OBJECT_ID_MAX_LEN)
642 		return TEE_ERROR_BAD_PARAMETERS;
643 
644 	sess = ts_get_current_session();
645 	spc = to_stmm_ctx(sess->ctx);
646 	res = vm_check_access_rights(&spc->uctx,
647 				     TEE_MEMORY_ACCESS_WRITE |
648 				     TEE_MEMORY_ACCESS_ANY_OWNER,
649 				     (uaddr_t)data, len);
650 	if (res != TEE_SUCCESS)
651 		return res;
652 
653 	res = tee_pobj_get(&sess->ctx->uuid, obj_id, obj_id_len, flags,
654 			   false, fops, &po);
655 	if (res != TEE_SUCCESS)
656 		return res;
657 
658 	res = po->fops->open(po, &file_size, &fh);
659 	if (res != TEE_SUCCESS)
660 		goto out;
661 
662 	read_len = len;
663 	res = po->fops->read(fh, offset, data, &read_len);
664 	if (res == TEE_ERROR_CORRUPT_OBJECT) {
665 		EMSG("Object corrupt");
666 		po->fops->remove(po);
667 	} else if (res == TEE_SUCCESS && len != read_len) {
668 		res = TEE_ERROR_CORRUPT_OBJECT;
669 	}
670 
671 	po->fops->close(&fh);
672 
673 out:
674 	tee_pobj_release(po);
675 
676 	return res;
677 }
678 
679 /*
680  * Combined write from secure partition, this will create/open, write and
681  * close the file object.
682  */
683 static TEE_Result sec_storage_obj_write(unsigned long storage_id, char *obj_id,
684 					unsigned long obj_id_len, void *data,
685 					unsigned long len, unsigned long offset,
686 					unsigned long flags)
687 
688 {
689 	const struct tee_file_operations *fops = NULL;
690 	struct ts_session *sess = NULL;
691 	struct tee_file_handle *fh = NULL;
692 	struct stmm_ctx *spc = NULL;
693 	TEE_Result res = TEE_SUCCESS;
694 	struct tee_pobj *po = NULL;
695 
696 	fops = tee_svc_storage_file_ops(storage_id);
697 	if (!fops)
698 		return TEE_ERROR_ITEM_NOT_FOUND;
699 
700 	if (obj_id_len > TEE_OBJECT_ID_MAX_LEN)
701 		return TEE_ERROR_BAD_PARAMETERS;
702 
703 	sess = ts_get_current_session();
704 	spc = to_stmm_ctx(sess->ctx);
705 	res = vm_check_access_rights(&spc->uctx,
706 				     TEE_MEMORY_ACCESS_READ |
707 				     TEE_MEMORY_ACCESS_ANY_OWNER,
708 				     (uaddr_t)data, len);
709 	if (res != TEE_SUCCESS)
710 		return res;
711 
712 	res = tee_pobj_get(&sess->ctx->uuid, obj_id, obj_id_len, flags,
713 			   false, fops, &po);
714 	if (res != TEE_SUCCESS)
715 		return res;
716 
717 	res = po->fops->open(po, NULL, &fh);
718 	if (res == TEE_ERROR_ITEM_NOT_FOUND)
719 		res = po->fops->create(po, false, NULL, 0, NULL, 0, NULL, 0,
720 				       &fh);
721 	if (res == TEE_SUCCESS) {
722 		res = po->fops->write(fh, offset, data, len);
723 		po->fops->close(&fh);
724 	}
725 
726 	tee_pobj_release(po);
727 
728 	return res;
729 }
730 
731 static void stmm_handle_mem_mgr_service(struct thread_svc_regs *regs)
732 {
733 	uint32_t action = SVC_REGS_A3(regs);
734 	uintptr_t va = SVC_REGS_A4(regs);
735 	uint32_t nr_pages = SVC_REGS_A5(regs);
736 	uint32_t perm = SVC_REGS_A6(regs);
737 
738 	switch (action) {
739 	case __FFA_SVC_MEMORY_ATTRIBUTES_GET:
740 		service_compose_direct_resp(regs, sp_svc_get_mem_attr(va));
741 		break;
742 	case __FFA_SVC_MEMORY_ATTRIBUTES_SET:
743 		service_compose_direct_resp(regs,
744 					    sp_svc_set_mem_attr(va, nr_pages,
745 								perm));
746 		break;
747 	default:
748 		EMSG("Undefined service id %#"PRIx32, action);
749 		service_compose_direct_resp(regs, STMM_RET_INVALID_PARAM);
750 		break;
751 	}
752 }
753 
754 static uint32_t tee2stmm_ret_val(TEE_Result res)
755 {
756 	switch (res) {
757 	case TEE_SUCCESS:
758 		return STMM_RET_SUCCESS;
759 	case TEE_ERROR_NOT_SUPPORTED:
760 		return STMM_RET_NOT_SUPPORTED;
761 	case TEE_ERROR_ACCESS_DENIED:
762 		return STMM_RET_DENIED;
763 	case TEE_ERROR_OUT_OF_MEMORY:
764 		return STMM_RET_NO_MEM;
765 	case TEE_ERROR_BAD_PARAMETERS:
766 	default:
767 		return STMM_RET_INVALID_PARAM;
768 	}
769 }
770 
771 #define FILENAME "EFI_VARS"
772 static void stmm_handle_storage_service(struct thread_svc_regs *regs)
773 {
774 	uint32_t flags = TEE_DATA_FLAG_ACCESS_READ |
775 			 TEE_DATA_FLAG_ACCESS_WRITE |
776 			 TEE_DATA_FLAG_SHARE_READ |
777 			 TEE_DATA_FLAG_SHARE_WRITE;
778 	uint32_t action = SVC_REGS_A3(regs);
779 	void *va = (void *)SVC_REGS_A4(regs);
780 	unsigned long len = SVC_REGS_A5(regs);
781 	unsigned long offset = SVC_REGS_A6(regs);
782 	char obj_id[] = FILENAME;
783 	size_t obj_id_len = strlen(obj_id);
784 	TEE_Result res = TEE_SUCCESS;
785 	uint32_t stmm_rc = STMM_RET_INVALID_PARAM;
786 
787 	switch (action) {
788 	case __FFA_SVC_RPMB_READ:
789 		DMSG("RPMB read");
790 		res = sec_storage_obj_read(TEE_STORAGE_PRIVATE_RPMB, obj_id,
791 					   obj_id_len, va, len, offset, flags);
792 		stmm_rc = tee2stmm_ret_val(res);
793 		break;
794 	case __FFA_SVC_RPMB_WRITE:
795 		DMSG("RPMB write");
796 		res = sec_storage_obj_write(TEE_STORAGE_PRIVATE_RPMB, obj_id,
797 					    obj_id_len, va, len, offset, flags);
798 		stmm_rc = tee2stmm_ret_val(res);
799 		break;
800 	default:
801 		EMSG("Undefined service id %#"PRIx32, action);
802 		break;
803 	}
804 
805 	service_compose_direct_resp(regs, stmm_rc);
806 }
807 
808 static void spm_eret_error(int32_t error_code, struct thread_svc_regs *regs)
809 {
810 	SVC_REGS_A0(regs) = FFA_ERROR;
811 	SVC_REGS_A1(regs) = FFA_PARAM_MBZ;
812 	SVC_REGS_A2(regs) = error_code;
813 	SVC_REGS_A3(regs) = FFA_PARAM_MBZ;
814 	SVC_REGS_A4(regs) = FFA_PARAM_MBZ;
815 	SVC_REGS_A5(regs) = FFA_PARAM_MBZ;
816 	SVC_REGS_A6(regs) = FFA_PARAM_MBZ;
817 	SVC_REGS_A7(regs) = FFA_PARAM_MBZ;
818 }
819 
820 static void spm_handle_direct_req(struct thread_svc_regs *regs)
821 {
822 	uint16_t dst_id = SVC_REGS_A1(regs) & UINT16_MAX;
823 
824 	if (dst_id == mem_mgr_id) {
825 		stmm_handle_mem_mgr_service(regs);
826 	} else if (dst_id == ffa_storage_id) {
827 		stmm_handle_storage_service(regs);
828 	} else {
829 		EMSG("Undefined endpoint id %#"PRIx16, dst_id);
830 		spm_eret_error(STMM_RET_INVALID_PARAM, regs);
831 	}
832 }
833 
834 /* Return true if returning to SP, false if returning to caller */
835 static bool spm_handle_svc(struct thread_svc_regs *regs)
836 {
837 #ifdef ARM64
838 	uint64_t *a0 = &regs->x0;
839 #endif
840 #ifdef ARM32
841 	uint32_t *a0 = &regs->r0;
842 #endif
843 
844 	switch (*a0) {
845 	case FFA_VERSION:
846 		DMSG("Received FFA version");
847 		*a0 = MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR);
848 		return true;
849 	case __FFA_MSG_SEND_DIRECT_RESP:
850 		DMSG("Received FFA direct response");
851 		return_from_sp_helper(false, 0, regs);
852 		return false;
853 	case __FFA_MSG_SEND_DIRECT_REQ:
854 		DMSG("Received FFA direct request");
855 		spm_handle_direct_req(regs);
856 		return true;
857 	default:
858 		EMSG("Undefined syscall %#"PRIx32, (uint32_t)*a0);
859 		return_from_sp_helper(true /*panic*/, 0xabcd, regs);
860 		return false;
861 	}
862 }
863 
864 const struct ts_ops stmm_sp_ops __rodata_unpaged = {
865 	.enter_open_session = stmm_enter_open_session,
866 	.enter_invoke_cmd = stmm_enter_invoke_cmd,
867 	.enter_close_session = stmm_enter_close_session,
868 	.dump_state = stmm_dump_state,
869 	.destroy = stmm_ctx_destroy,
870 	.get_instance_id = stmm_get_instance_id,
871 	.handle_svc = spm_handle_svc,
872 };
873