xref: /optee_os/core/tee/tee_svc.c (revision 769cbbd704592f9fa940f52dabb0511ff10cf78a)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2020-2022 Linaro Limited
5  */
6 
7 #include <compiler.h>
8 #include <kernel/chip_services.h>
9 #include <kernel/pseudo_ta.h>
10 #include <kernel/tee_common.h>
11 #include <kernel/tee_common_otp.h>
12 #include <kernel/tee_ta_manager.h>
13 #include <kernel/tee_time.h>
14 #include <kernel/trace_ta.h>
15 #include <kernel/user_access.h>
16 #include <memtag.h>
17 #include <mm/core_memprot.h>
18 #include <mm/mobj.h>
19 #include <mm/tee_mm.h>
20 #include <mm/vm.h>
21 #include <stdlib_ext.h>
22 #include <tee_api_types.h>
23 #include <tee/tee_cryp_utl.h>
24 #include <tee/tee_svc.h>
25 #include <trace.h>
26 #include <user_ta_header.h>
27 #include <utee_types.h>
28 #include <util.h>
29 
30 vaddr_t tee_svc_uref_base;
31 
32 void syscall_log(const void *buf, size_t len)
33 {
34 	if (IS_ENABLED(CFG_TEE_CORE_TA_TRACE)) {
35 		char *kbuf = NULL;
36 		size_t sz = 0;
37 
38 		if (!len || ADD_OVERFLOW(len, 1, &sz))
39 			return;
40 
41 		kbuf = malloc(sz);
42 		if (!kbuf)
43 			return;
44 
45 		if (copy_from_user(kbuf, buf, len) == TEE_SUCCESS) {
46 			kbuf[len] = '\0';
47 			trace_ext_puts(kbuf);
48 		}
49 
50 		free_wipe(kbuf);
51 	}
52 }
53 
54 TEE_Result syscall_not_supported(void)
55 {
56 	return TEE_ERROR_NOT_SUPPORTED;
57 }
58 
59 /* Configuration properties */
60 /* API implementation version */
61 static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION);
62 
63 /* Implementation description (implementation-dependent) */
64 static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR);
65 
66 /*
67  * TA persistent time protection level
68  * 100: Persistent time based on an REE-controlled real-time clock
69  * and on the TEE Trusted Storage for the storage of origins (default).
70  * 1000: Persistent time based on a TEE-controlled real-time clock
71  * and the TEE Trusted Storage.
72  * The real-time clock MUST be out of reach of software attacks
73  * from the REE.
74  */
75 static const uint32_t ta_time_prot_lvl = 100;
76 
77 /* Elliptic Curve Cryptographic support */
78 #ifdef CFG_CRYPTO_ECC
79 static const bool crypto_ecc_en = 1;
80 #else
81 static const bool crypto_ecc_en;
82 #endif
83 
84 /*
85  * Trusted storage anti rollback protection level
86  * 100: Antirollback enforced at REE level
87  * 1000: Antirollback TEE-controlled hardware
88  */
89 #ifdef CFG_RPMB_FS
90 static const uint32_t ts_antiroll_prot_lvl = 1000;
91 #else
92 static const uint32_t ts_antiroll_prot_lvl = 100;
93 #endif
94 
95 /* Trusted OS implementation version */
96 static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION);
97 
98 /* Trusted OS implementation version (binary value) */
99 static const uint32_t trustedos_impl_bin_version; /* 0 by default */
100 
101 /* Trusted OS implementation manufacturer name */
102 static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER);
103 
104 /* Trusted firmware version */
105 static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION);
106 
107 /* Trusted firmware version (binary value) */
108 static const uint32_t fw_impl_bin_version; /* 0 by default */
109 
110 /* Trusted firmware manufacturer name */
111 static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER);
112 
113 static TEE_Result get_prop_tee_dev_id(struct ts_session *sess __unused,
114 				      void *buf, size_t *blen)
115 {
116 	TEE_Result res;
117 	TEE_UUID uuid;
118 	const size_t nslen = 5;
119 	uint8_t data[5 + FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = {
120 	    'O', 'P', 'T', 'E', 'E' };
121 
122 	if (*blen < sizeof(uuid)) {
123 		*blen = sizeof(uuid);
124 		return TEE_ERROR_SHORT_BUFFER;
125 	}
126 	*blen = sizeof(uuid);
127 
128 	if (tee_otp_get_die_id(data + nslen, sizeof(data) - nslen))
129 		return TEE_ERROR_BAD_STATE;
130 
131 	res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data),
132 				    (uint8_t *)&uuid, sizeof(uuid));
133 	if (res != TEE_SUCCESS)
134 		return TEE_ERROR_BAD_STATE;
135 
136 	/*
137 	 * Changes the random value into and UUID as specifiec
138 	 * in RFC 4122. The magic values are from the example
139 	 * code in the RFC.
140 	 *
141 	 * TEE_UUID is defined slightly different from the RFC,
142 	 * but close enough for our purpose.
143 	 */
144 
145 	uuid.timeHiAndVersion &= 0x0fff;
146 	uuid.timeHiAndVersion |= 5 << 12;
147 
148 	/* uuid.clock_seq_hi_and_reserved in the RFC */
149 	uuid.clockSeqAndNode[0] &= 0x3f;
150 	uuid.clockSeqAndNode[0] |= 0x80;
151 
152 	return copy_to_user(buf, &uuid, sizeof(TEE_UUID));
153 }
154 
155 static TEE_Result
156 get_prop_tee_sys_time_prot_level(struct ts_session *sess __unused,
157 				 void *buf, size_t *blen)
158 {
159 	uint32_t prot;
160 
161 	if (*blen < sizeof(prot)) {
162 		*blen = sizeof(prot);
163 		return TEE_ERROR_SHORT_BUFFER;
164 	}
165 	*blen = sizeof(prot);
166 	prot = tee_time_get_sys_time_protection_level();
167 	return copy_to_user(buf, &prot, sizeof(prot));
168 }
169 
170 static TEE_Result get_prop_client_id(struct ts_session *sess,
171 				     void *buf, size_t *blen)
172 {
173 	if (*blen < sizeof(TEE_Identity)) {
174 		*blen = sizeof(TEE_Identity);
175 		return TEE_ERROR_SHORT_BUFFER;
176 	}
177 	*blen = sizeof(TEE_Identity);
178 	return copy_to_user(buf, &to_ta_session(sess)->clnt_id,
179 			    sizeof(TEE_Identity));
180 }
181 
182 static TEE_Result get_prop_client_endian(struct ts_session *sess __unused,
183 					 void *buf, size_t *blen)
184 {
185 	const uint32_t endian = 0; /* assume little-endian */
186 
187 	if (*blen < sizeof(endian)) {
188 		*blen = sizeof(endian);
189 		return TEE_ERROR_SHORT_BUFFER;
190 	}
191 	*blen = sizeof(endian);
192 	return copy_to_user(buf, &endian, sizeof(endian));
193 }
194 
195 static TEE_Result get_prop_ta_app_id(struct ts_session *sess,
196 				     void *buf, size_t *blen)
197 {
198 	if (*blen < sizeof(TEE_UUID)) {
199 		*blen = sizeof(TEE_UUID);
200 		return TEE_ERROR_SHORT_BUFFER;
201 	}
202 	*blen = sizeof(TEE_UUID);
203 	return copy_to_user(buf, &sess->ctx->uuid, sizeof(TEE_UUID));
204 }
205 
206 #ifdef CFG_TA_BTI
207 static TEE_Result
208 get_prop_feat_bti_implemented(struct ts_session *sess __unused, void *buf,
209 			      size_t *blen)
210 {
211 	bool bti_impl = false;
212 
213 	if (*blen < sizeof(bti_impl)) {
214 		*blen = sizeof(bti_impl);
215 		return TEE_ERROR_SHORT_BUFFER;
216 	}
217 	*blen = sizeof(bti_impl);
218 	bti_impl = feat_bti_is_implemented();
219 
220 	return copy_to_user(buf, &bti_impl, sizeof(bti_impl));
221 }
222 #endif
223 
224 #ifdef CFG_TA_PAUTH
225 static TEE_Result
226 get_prop_feat_pauth_implemented(struct ts_session *sess __unused, void *buf,
227 				size_t *blen)
228 {
229 	bool pauth_impl = false;
230 
231 	if (*blen < sizeof(pauth_impl)) {
232 		*blen = sizeof(pauth_impl);
233 		return TEE_ERROR_SHORT_BUFFER;
234 	}
235 	*blen = sizeof(pauth_impl);
236 	pauth_impl = feat_pauth_is_implemented();
237 
238 	return copy_to_user(buf, &pauth_impl, sizeof(pauth_impl));
239 }
240 #endif
241 
242 #if MEMTAG_IS_ENABLED
243 static TEE_Result
244 get_prop_feat_memtag_implemented(struct ts_session *sess __unused, void *buf,
245 				 size_t *blen)
246 {
247 	uint32_t v = 0;
248 
249 	if (*blen < sizeof(v)) {
250 		*blen = sizeof(v);
251 		return TEE_ERROR_SHORT_BUFFER;
252 	}
253 	*blen = sizeof(v);
254 	if (memtag_is_enabled())
255 		v = feat_mte_implemented();
256 
257 	return copy_to_user(buf, &v, sizeof(v));
258 }
259 #endif
260 
261 /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */
262 const struct tee_props tee_propset_client[] = {
263 	{
264 		.name = "gpd.client.identity",
265 		.prop_type = USER_TA_PROP_TYPE_IDENTITY,
266 		.get_prop_func = get_prop_client_id
267 	},
268 	{
269 		.name = "gpd.client.endian",
270 		.prop_type = USER_TA_PROP_TYPE_U32,
271 		.get_prop_func = get_prop_client_endian
272 	},
273 };
274 
275 /* Properties of the set TEE_PROPSET_CURRENT_TA */
276 const struct tee_props tee_propset_ta[] = {
277 	{
278 		.name = "gpd.ta.appID",
279 		.prop_type = USER_TA_PROP_TYPE_UUID,
280 		.get_prop_func = get_prop_ta_app_id
281 	},
282 
283 	/*
284 	 * Following properties are processed directly in libutee:
285 	 *	TA_PROP_STR_SINGLE_INSTANCE
286 	 *	TA_PROP_STR_MULTI_SESSION
287 	 *	TA_PROP_STR_KEEP_ALIVE
288 	 *	TA_PROP_STR_DATA_SIZE
289 	 *	TA_PROP_STR_STACK_SIZE
290 	 *	TA_PROP_STR_VERSION
291 	 *	TA_PROP_STR_DESCRIPTION
292 	 *	USER_TA_PROP_TYPE_STRING,
293 	 *	TA_DESCRIPTION
294 	 */
295 };
296 
297 /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */
298 const struct tee_props tee_propset_tee[] = {
299 	{
300 		.name = "gpd.tee.apiversion",
301 		.prop_type = USER_TA_PROP_TYPE_STRING,
302 		.data = api_vers,
303 		.len = sizeof(api_vers),
304 	},
305 	{
306 		.name = "gpd.tee.description",
307 		.prop_type = USER_TA_PROP_TYPE_STRING,
308 		.data = descr, .len = sizeof(descr)
309 	},
310 	{
311 		.name = "gpd.tee.deviceID",
312 		.prop_type = USER_TA_PROP_TYPE_UUID,
313 		.get_prop_func = get_prop_tee_dev_id
314 	},
315 	{
316 		.name = "gpd.tee.systemTime.protectionLevel",
317 		.prop_type = USER_TA_PROP_TYPE_U32,
318 		.get_prop_func = get_prop_tee_sys_time_prot_level
319 	},
320 	{
321 		.name = "gpd.tee.TAPersistentTime.protectionLevel",
322 		.prop_type = USER_TA_PROP_TYPE_U32,
323 		.data = &ta_time_prot_lvl,
324 		.len = sizeof(ta_time_prot_lvl)
325 	},
326 	{
327 		.name = "gpd.tee.cryptography.ecc",
328 		.prop_type = USER_TA_PROP_TYPE_BOOL,
329 		.data = &crypto_ecc_en,
330 		.len = sizeof(crypto_ecc_en)
331 	},
332 	{
333 		.name = "gpd.tee.trustedStorage.antiRollback.protectionLevel",
334 		.prop_type = USER_TA_PROP_TYPE_U32,
335 		.data = &ts_antiroll_prot_lvl,
336 		.len = sizeof(ts_antiroll_prot_lvl)
337 	},
338 	{
339 		.name = "gpd.tee.trustedos.implementation.version",
340 		.prop_type = USER_TA_PROP_TYPE_STRING,
341 		.data = trustedos_impl_version,
342 		.len = sizeof(trustedos_impl_version)
343 	},
344 	{
345 		.name = "gpd.tee.trustedos.implementation.binaryversion",
346 		.prop_type = USER_TA_PROP_TYPE_U32,
347 		.data = &trustedos_impl_bin_version,
348 		.len = sizeof(trustedos_impl_bin_version)
349 	},
350 	{
351 		.name = "gpd.tee.trustedos.manufacturer",
352 		.prop_type = USER_TA_PROP_TYPE_STRING,
353 		.data = trustedos_manufacturer,
354 		.len = sizeof(trustedos_manufacturer)
355 	},
356 	{
357 		.name = "gpd.tee.firmware.implementation.version",
358 		.prop_type = USER_TA_PROP_TYPE_STRING,
359 		.data = fw_impl_version,
360 		.len = sizeof(fw_impl_version)
361 	},
362 	{
363 		.name = "gpd.tee.firmware.implementation.binaryversion",
364 		.prop_type = USER_TA_PROP_TYPE_U32,
365 		.data = &fw_impl_bin_version,
366 		.len = sizeof(fw_impl_bin_version)
367 	},
368 	{
369 		.name = "gpd.tee.firmware.manufacturer",
370 		.prop_type = USER_TA_PROP_TYPE_STRING,
371 		.data = fw_manufacturer,
372 		.len = sizeof(fw_manufacturer)
373 	},
374 #ifdef CFG_TA_BTI
375 	{
376 		.name = "org.trustedfirmware.optee.cpu.feat_bti_implemented",
377 		.prop_type = USER_TA_PROP_TYPE_BOOL,
378 		.get_prop_func = get_prop_feat_bti_implemented
379 	},
380 #endif
381 #ifdef CFG_TA_PAUTH
382 	{
383 		.name = "org.trustedfirmware.optee.cpu.feat_pauth_implemented",
384 		.prop_type = USER_TA_PROP_TYPE_BOOL,
385 		.get_prop_func = get_prop_feat_pauth_implemented
386 	},
387 #endif
388 #if MEMTAG_IS_ENABLED
389 	{
390 		.name = "org.trustedfirmware.optee.cpu.feat_memtag_implemented",
391 		.prop_type = USER_TA_PROP_TYPE_U32,
392 		.get_prop_func = get_prop_feat_memtag_implemented
393 	}
394 #endif
395 
396 	/*
397 	 * Following properties are processed directly in libutee:
398 	 *	gpd.tee.arith.maxBigIntSize
399 	 */
400 };
401 
402 __weak const struct tee_vendor_props vendor_props_client;
403 __weak const struct tee_vendor_props vendor_props_ta;
404 __weak const struct tee_vendor_props vendor_props_tee;
405 
406 static void get_prop_set(unsigned long prop_set,
407 			 const struct tee_props **props,
408 			 size_t *size,
409 			 const struct tee_props **vendor_props,
410 			 size_t *vendor_size)
411 {
412 	if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) {
413 		*props = tee_propset_client;
414 		*size = ARRAY_SIZE(tee_propset_client);
415 		*vendor_props = vendor_props_client.props;
416 		*vendor_size = vendor_props_client.len;
417 	} else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) {
418 		*props = tee_propset_ta;
419 		*size = ARRAY_SIZE(tee_propset_ta);
420 		*vendor_props = vendor_props_ta.props;
421 		*vendor_size = vendor_props_ta.len;
422 	} else if ((TEE_PropSetHandle)prop_set ==
423 		   TEE_PROPSET_TEE_IMPLEMENTATION) {
424 		*props = tee_propset_tee;
425 		*size = ARRAY_SIZE(tee_propset_tee);
426 		*vendor_props = vendor_props_tee.props;
427 		*vendor_size = vendor_props_tee.len;
428 	} else {
429 		*props = NULL;
430 		*size = 0;
431 		*vendor_props = NULL;
432 		*vendor_size = 0;
433 	}
434 }
435 
436 static const struct tee_props *get_prop_struct(unsigned long prop_set,
437 					       unsigned long index)
438 {
439 	const struct tee_props *props;
440 	const struct tee_props *vendor_props;
441 	size_t size;
442 	size_t vendor_size;
443 
444 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
445 
446 	if (index < size)
447 		return &(props[index]);
448 	index -= size;
449 
450 	if (index < vendor_size)
451 		return &(vendor_props[index]);
452 
453 	return NULL;
454 }
455 
456 /*
457  * prop_set is part of TEE_PROPSET_xxx
458  * index is the index in the Property Set to retrieve
459  * if name is not NULL, the name of "index" property is returned
460  * if buf is not NULL, the property is returned
461  */
462 TEE_Result syscall_get_property(unsigned long prop_set,
463 				unsigned long index,
464 				void *name, uint32_t *name_len,
465 				void *buf, uint32_t *blen,
466 				uint32_t *prop_type)
467 {
468 	struct ts_session *sess = ts_get_current_session();
469 	TEE_Result res = TEE_SUCCESS;
470 	TEE_Result res2 = TEE_SUCCESS;
471 	const struct tee_props *prop = NULL;
472 	uint32_t klen = 0;
473 	size_t klen_size = 0;
474 	uint32_t elen = 0;
475 
476 	prop = get_prop_struct(prop_set, index);
477 	if (!prop)
478 		return TEE_ERROR_ITEM_NOT_FOUND;
479 
480 	/* Get the property type */
481 	if (prop_type) {
482 		res = copy_to_user(prop_type, &prop->prop_type,
483 				   sizeof(*prop_type));
484 		if (res != TEE_SUCCESS)
485 			return res;
486 	}
487 
488 	/* Get the property */
489 	if (buf && blen) {
490 		res = copy_from_user(&klen, blen, sizeof(klen));
491 		if (res != TEE_SUCCESS)
492 			return res;
493 
494 		if (prop->get_prop_func) {
495 			klen_size = klen;
496 			res = prop->get_prop_func(sess, buf, &klen_size);
497 			klen = klen_size;
498 			res2 = copy_to_user(blen, &klen, sizeof(*blen));
499 		} else {
500 			if (klen < prop->len)
501 				res = TEE_ERROR_SHORT_BUFFER;
502 			else
503 				res = copy_to_user(buf, prop->data, prop->len);
504 			res2 = copy_to_user(blen, &prop->len, sizeof(*blen));
505 		}
506 		if (res2 != TEE_SUCCESS)
507 			return res2;
508 		if (res != TEE_SUCCESS)
509 			return res;
510 	}
511 
512 	/* Get the property name */
513 	if (name && name_len) {
514 		res = copy_from_user(&klen, name_len, sizeof(klen));
515 		if (res != TEE_SUCCESS)
516 			return res;
517 
518 		elen = strlen(prop->name) + 1;
519 
520 		if (klen < elen)
521 			res = TEE_ERROR_SHORT_BUFFER;
522 		else
523 			res = copy_to_user(name, prop->name, elen);
524 		res2 = copy_to_user(name_len, &elen, sizeof(*name_len));
525 		if (res2 != TEE_SUCCESS)
526 			return res2;
527 		if (res != TEE_SUCCESS)
528 			return res;
529 	}
530 
531 	return res;
532 }
533 
534 /*
535  * prop_set is part of TEE_PROPSET_xxx
536  */
537 TEE_Result syscall_get_property_name_to_index(unsigned long prop_set,
538 					      void *name,
539 					      unsigned long name_len,
540 					      uint32_t *index)
541 {
542 	TEE_Result res = TEE_SUCCESS;
543 	const struct tee_props *props = NULL;
544 	size_t size = 0;
545 	const struct tee_props *vendor_props = NULL;
546 	size_t vendor_size = 0;
547 	char *kname = NULL;
548 	uint32_t i = 0;
549 
550 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
551 	if (!props)
552 		return TEE_ERROR_ITEM_NOT_FOUND;
553 
554 	if (!name || !name_len) {
555 		res = TEE_ERROR_BAD_PARAMETERS;
556 		goto out;
557 	}
558 
559 	kname = malloc(name_len);
560 	if (!kname)
561 		return TEE_ERROR_OUT_OF_MEMORY;
562 	res = copy_from_user(kname, name, name_len);
563 	if (res != TEE_SUCCESS)
564 		goto out;
565 	kname[name_len - 1] = 0;
566 
567 	res = TEE_ERROR_ITEM_NOT_FOUND;
568 	for (i = 0; i < size; i++) {
569 		if (!strcmp(kname, props[i].name)) {
570 			res = copy_to_user(index, &i, sizeof(*index));
571 			goto out;
572 		}
573 	}
574 	for (i = size; i < size + vendor_size; i++) {
575 		if (!strcmp(kname, vendor_props[i - size].name)) {
576 			res = copy_to_user(index, &i, sizeof(*index));
577 			goto out;
578 		}
579 	}
580 
581 out:
582 	free_wipe(kname);
583 	return res;
584 }
585 
586 static TEE_Result utee_param_to_param(struct user_ta_ctx *utc,
587 				      struct tee_ta_param *p,
588 				      struct utee_params *up)
589 {
590 	size_t n = 0;
591 	uint32_t types = up->types;
592 
593 	p->types = types;
594 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
595 		uintptr_t a = up->vals[n * 2];
596 		size_t b = up->vals[n * 2 + 1];
597 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
598 				 TEE_MEMORY_ACCESS_ANY_OWNER;
599 
600 		switch (TEE_PARAM_TYPE_GET(types, n)) {
601 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
602 		case TEE_PARAM_TYPE_MEMREF_INOUT:
603 			flags |= TEE_MEMORY_ACCESS_WRITE;
604 			fallthrough;
605 		case TEE_PARAM_TYPE_MEMREF_INPUT:
606 			p->u[n].mem.offs = memtag_strip_tag_vaddr((void *)a);
607 			p->u[n].mem.size = b;
608 
609 			if (!p->u[n].mem.offs) {
610 				/* Allow NULL memrefs if of size 0 */
611 				if (p->u[n].mem.size)
612 					return TEE_ERROR_BAD_PARAMETERS;
613 				p->u[n].mem.mobj = NULL;
614 				break;
615 			}
616 
617 			p->u[n].mem.mobj = &mobj_virt;
618 
619 			if (vm_check_access_rights(&utc->uctx, flags, a, b))
620 				return TEE_ERROR_ACCESS_DENIED;
621 			break;
622 		case TEE_PARAM_TYPE_VALUE_INPUT:
623 		case TEE_PARAM_TYPE_VALUE_INOUT:
624 			p->u[n].val.a = a;
625 			p->u[n].val.b = b;
626 			break;
627 		default:
628 			memset(&p->u[n], 0, sizeof(p->u[n]));
629 			break;
630 		}
631 	}
632 
633 	return TEE_SUCCESS;
634 }
635 
636 static TEE_Result alloc_temp_sec_mem(size_t size, struct mobj **mobj,
637 				     uint8_t **va)
638 {
639 	struct mobj *m = NULL;
640 	void *v = NULL;
641 
642 	/* Allocate section in secure DDR */
643 #ifdef CFG_PAGED_USER_TA
644 	m = mobj_seccpy_shm_alloc(size);
645 #else
646 	m = mobj_mm_alloc(mobj_sec_ddr, size, &tee_mm_sec_ddr);
647 #endif
648 	if (!m)
649 		return TEE_ERROR_GENERIC;
650 
651 	v = mobj_get_va(*mobj, 0, size);
652 	if (!v) {
653 		mobj_put(m);
654 		return TEE_ERROR_GENERIC;
655 	}
656 
657 	*mobj = m;
658 	*va = v;
659 	return TEE_SUCCESS;
660 }
661 
662 /*
663  * TA invokes some TA with parameter.
664  * If some parameters are memory references:
665  * - either the memref is inside TA private RAM: TA is not allowed to expose
666  *   its private RAM: use a temporary memory buffer and copy the data.
667  * - or the memref is not in the TA private RAM:
668  *   - if the memref was mapped to the TA, TA is allowed to expose it.
669  *   - if so, converts memref virtual address into a physical address.
670  */
671 static TEE_Result tee_svc_copy_param(struct ts_session *sess,
672 				     struct ts_session *called_sess,
673 				     struct utee_params *callee_params,
674 				     struct tee_ta_param *param,
675 				     void *tmp_buf_va[TEE_NUM_PARAMS],
676 				     size_t tmp_buf_size[TEE_NUM_PARAMS],
677 				     struct mobj **mobj_tmp)
678 {
679 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
680 	bool ta_private_memref[TEE_NUM_PARAMS] = { false, };
681 	TEE_Result res = TEE_SUCCESS;
682 	size_t dst_offs = 0;
683 	size_t req_mem = 0;
684 	uint8_t *dst = 0;
685 	void *va = NULL;
686 	size_t n = 0;
687 	size_t s = 0;
688 
689 	callee_params = memtag_strip_tag(callee_params);
690 
691 	/* fill 'param' input struct with caller params description buffer */
692 	if (!callee_params) {
693 		memset(param, 0, sizeof(*param));
694 	} else {
695 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
696 				 TEE_MEMORY_ACCESS_WRITE |
697 				 TEE_MEMORY_ACCESS_ANY_OWNER;
698 
699 		res = vm_check_access_rights(&utc->uctx, flags,
700 					     (uaddr_t)callee_params,
701 					     sizeof(struct utee_params));
702 		if (res != TEE_SUCCESS)
703 			return res;
704 		res = utee_param_to_param(utc, param, callee_params);
705 		if (res != TEE_SUCCESS)
706 			return res;
707 	}
708 
709 	if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) {
710 		/* pseudo TA borrows the mapping of the calling TA */
711 		return TEE_SUCCESS;
712 	}
713 
714 	/* All mobj in param are of type MOJB_TYPE_VIRT */
715 
716 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
717 
718 		ta_private_memref[n] = false;
719 
720 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
721 		case TEE_PARAM_TYPE_MEMREF_INPUT:
722 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
723 		case TEE_PARAM_TYPE_MEMREF_INOUT:
724 			va = (void *)param->u[n].mem.offs;
725 			s = param->u[n].mem.size;
726 			if (!va) {
727 				if (s)
728 					return TEE_ERROR_BAD_PARAMETERS;
729 				break;
730 			}
731 			/* uTA cannot expose its private memory */
732 			if (vm_buf_is_inside_um_private(&utc->uctx, va, s)) {
733 				s = ROUNDUP(s, sizeof(uint32_t));
734 				if (ADD_OVERFLOW(req_mem, s, &req_mem))
735 					return TEE_ERROR_BAD_PARAMETERS;
736 				ta_private_memref[n] = true;
737 				break;
738 			}
739 
740 			res = vm_buf_to_mboj_offs(&utc->uctx, va, s,
741 						  &param->u[n].mem.mobj,
742 						  &param->u[n].mem.offs);
743 			if (res != TEE_SUCCESS)
744 				return res;
745 			break;
746 		default:
747 			break;
748 		}
749 	}
750 
751 	if (req_mem == 0)
752 		return TEE_SUCCESS;
753 
754 	res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst);
755 	if (res != TEE_SUCCESS)
756 		return res;
757 	dst_offs = 0;
758 
759 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
760 
761 		if (!ta_private_memref[n])
762 			continue;
763 
764 		s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t));
765 
766 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
767 		case TEE_PARAM_TYPE_MEMREF_INPUT:
768 		case TEE_PARAM_TYPE_MEMREF_INOUT:
769 			va = (void *)param->u[n].mem.offs;
770 			if (va) {
771 				res = copy_from_user(dst, va,
772 						     param->u[n].mem.size);
773 				if (res != TEE_SUCCESS)
774 					return res;
775 				param->u[n].mem.offs = dst_offs;
776 				param->u[n].mem.mobj = *mobj_tmp;
777 				tmp_buf_va[n] = dst;
778 				tmp_buf_size[n] = param->u[n].mem.size;
779 				dst += s;
780 				dst_offs += s;
781 			}
782 			break;
783 
784 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
785 			va = (void *)param->u[n].mem.offs;
786 			if (va) {
787 				param->u[n].mem.offs = dst_offs;
788 				param->u[n].mem.mobj = *mobj_tmp;
789 				tmp_buf_va[n] = dst;
790 				tmp_buf_size[n] = param->u[n].mem.size;
791 				dst += s;
792 				dst_offs += s;
793 			}
794 			break;
795 
796 		default:
797 			continue;
798 		}
799 	}
800 
801 	return TEE_SUCCESS;
802 }
803 
804 /*
805  * Back from execution of service: update parameters passed from TA:
806  * If some parameters were memory references:
807  * - either the memref was temporary: copy back data and update size
808  * - or it was the original TA memref: update only the size value.
809  */
810 static TEE_Result tee_svc_update_out_param(
811 		struct tee_ta_param *param,
812 		void *tmp_buf_va[TEE_NUM_PARAMS],
813 		size_t tmp_buf_size[TEE_NUM_PARAMS],
814 		struct utee_params *usr_param)
815 {
816 	size_t n;
817 	uint64_t *vals = usr_param->vals;
818 	size_t sz = 0;
819 
820 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
821 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
822 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
823 		case TEE_PARAM_TYPE_MEMREF_INOUT:
824 			/*
825 			 * Memory copy is only needed if there's a temporary
826 			 * buffer involved, tmp_buf_va[n] is only update if
827 			 * a temporary buffer is used. Otherwise only the
828 			 * size needs to be updated.
829 			 */
830 			sz = param->u[n].mem.size;
831 			if (tmp_buf_va[n] && sz <= vals[n * 2 + 1]) {
832 				void *src = tmp_buf_va[n];
833 				void *dst = (void *)(uintptr_t)vals[n * 2];
834 				TEE_Result res = TEE_SUCCESS;
835 
836 				/*
837 				 * TA is allowed to return a size larger than
838 				 * the original size. However, in such cases no
839 				 * data should be synchronized as per TEE Client
840 				 * API spec.
841 				 */
842 				if (sz <= tmp_buf_size[n]) {
843 					res = copy_to_user(dst, src, sz);
844 					if (res != TEE_SUCCESS)
845 						return res;
846 				}
847 			}
848 			usr_param->vals[n * 2 + 1] = sz;
849 			break;
850 
851 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
852 		case TEE_PARAM_TYPE_VALUE_INOUT:
853 			vals[n * 2] = param->u[n].val.a;
854 			vals[n * 2 + 1] = param->u[n].val.b;
855 			break;
856 
857 		default:
858 			continue;
859 		}
860 	}
861 
862 	return TEE_SUCCESS;
863 }
864 
865 /* Called when a TA calls an OpenSession on another TA */
866 TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
867 			unsigned long cancel_req_to,
868 			struct utee_params *usr_param, uint32_t *ta_sess,
869 			uint32_t *ret_orig)
870 {
871 	struct ts_session *sess = ts_get_current_session();
872 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
873 	TEE_Result res = TEE_SUCCESS;
874 	uint32_t ret_o = TEE_ORIGIN_TEE;
875 	struct tee_ta_session *s = NULL;
876 	struct mobj *mobj_param = NULL;
877 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
878 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
879 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
880 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
881 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { 0 };
882 
883 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
884 		res = TEE_ERROR_OUT_OF_MEMORY;
885 		goto out_free_only;
886 	}
887 
888 	memset(param, 0, sizeof(struct tee_ta_param));
889 
890 	res = copy_from_user_private(uuid, dest, sizeof(TEE_UUID));
891 	if (res != TEE_SUCCESS)
892 		goto function_exit;
893 
894 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
895 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
896 
897 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va,
898 				 tmp_buf_size, &mobj_param);
899 	if (res != TEE_SUCCESS)
900 		goto function_exit;
901 
902 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
903 				  clnt_id, cancel_req_to, param);
904 	vm_set_ctx(&utc->ta_ctx.ts_ctx);
905 	if (res != TEE_SUCCESS)
906 		goto function_exit;
907 
908 	res = tee_svc_update_out_param(param, tmp_buf_va, tmp_buf_size,
909 				       usr_param);
910 
911 function_exit:
912 	mobj_put_wipe(mobj_param);
913 	if (res == TEE_SUCCESS)
914 		copy_to_user_private(ta_sess, &s->id, sizeof(s->id));
915 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
916 
917 out_free_only:
918 	free_wipe(param);
919 	free_wipe(uuid);
920 	free_wipe(clnt_id);
921 	return res;
922 }
923 
924 TEE_Result syscall_close_ta_session(unsigned long ta_sess)
925 {
926 	struct ts_session *sess = ts_get_current_session();
927 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
928 	TEE_Identity clnt_id = { };
929 	struct tee_ta_session *s = NULL;
930 
931 	s = tee_ta_find_session(ta_sess, &utc->open_sessions);
932 
933 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
934 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
935 
936 	return tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
937 }
938 
939 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
940 			unsigned long cancel_req_to, unsigned long cmd_id,
941 			struct utee_params *usr_param, uint32_t *ret_orig)
942 {
943 	struct ts_session *sess = ts_get_current_session();
944 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
945 	TEE_Result res = TEE_SUCCESS;
946 	TEE_Result res2 = TEE_SUCCESS;
947 	uint32_t ret_o = TEE_ORIGIN_TEE;
948 	struct tee_ta_param param = { 0 };
949 	TEE_Identity clnt_id = { };
950 	struct tee_ta_session *called_sess = NULL;
951 	struct mobj *mobj_param = NULL;
952 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
953 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { };
954 
955 	called_sess = tee_ta_get_session((uint32_t)ta_sess, true,
956 				&utc->open_sessions);
957 	if (!called_sess)
958 		return TEE_ERROR_BAD_PARAMETERS;
959 
960 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
961 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
962 
963 	res = tee_svc_copy_param(sess, &called_sess->ts_sess, usr_param, &param,
964 				 tmp_buf_va, tmp_buf_size, &mobj_param);
965 	if (res != TEE_SUCCESS)
966 		goto function_exit;
967 
968 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
969 				    cancel_req_to, cmd_id, &param);
970 	if (res == TEE_ERROR_TARGET_DEAD)
971 		goto function_exit;
972 
973 	res2 = tee_svc_update_out_param(&param, tmp_buf_va, tmp_buf_size,
974 					usr_param);
975 	if (res2 != TEE_SUCCESS) {
976 		/*
977 		 * Spec for TEE_InvokeTACommand() says:
978 		 * "If the return origin is different from
979 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
980 		 * before it could reach the destination Trusted
981 		 * Application."
982 		 *
983 		 * But if we can't update params to the caller we have no
984 		 * choice we need to return some error to indicate that
985 		 * parameters aren't updated as expected.
986 		 */
987 		ret_o = TEE_ORIGIN_TEE;
988 		res = res2;
989 	}
990 
991 function_exit:
992 	tee_ta_put_session(called_sess);
993 	mobj_put_wipe(mobj_param);
994 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
995 	return res;
996 }
997 
998 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
999 				       size_t len)
1000 {
1001 	struct ts_session *s = ts_get_current_session();
1002 
1003 	return vm_check_access_rights(&to_user_ta_ctx(s->ctx)->uctx, flags,
1004 				      memtag_strip_tag_vaddr(buf), len);
1005 }
1006 
1007 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
1008 {
1009 	struct ts_session *s = ts_get_current_session();
1010 	uint32_t c = 0;
1011 
1012 	c = tee_ta_session_is_cancelled(to_ta_session(s), NULL);
1013 
1014 	return copy_to_user(cancel, &c, sizeof(c));
1015 }
1016 
1017 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
1018 {
1019 	struct ts_session *s = ts_get_current_session();
1020 	struct tee_ta_session *sess = NULL;
1021 	uint32_t m = 0;
1022 
1023 	sess = to_ta_session(s);
1024 	m = sess->cancel_mask;
1025 	sess->cancel_mask = false;
1026 	return copy_to_user(old_mask, &m, sizeof(m));
1027 }
1028 
1029 TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
1030 {
1031 	struct ts_session *s = ts_get_current_session();
1032 	struct tee_ta_session *sess = NULL;
1033 	uint32_t m = 0;
1034 
1035 	sess = to_ta_session(s);
1036 	m = sess->cancel_mask;
1037 	sess->cancel_mask = true;
1038 	return copy_to_user(old_mask, &m, sizeof(m));
1039 }
1040 
1041 TEE_Result syscall_wait(unsigned long timeout)
1042 {
1043 	struct ts_session *s = ts_get_current_session();
1044 	TEE_Result res = TEE_SUCCESS;
1045 	uint32_t mytime = 0;
1046 	TEE_Time base_time = { };
1047 	TEE_Time current_time = { };
1048 
1049 	res = tee_time_get_sys_time(&base_time);
1050 	if (res != TEE_SUCCESS)
1051 		return res;
1052 
1053 	while (true) {
1054 		res = tee_time_get_sys_time(&current_time);
1055 		if (res != TEE_SUCCESS)
1056 			return res;
1057 
1058 		if (tee_ta_session_is_cancelled(to_ta_session(s),
1059 						&current_time))
1060 			return TEE_ERROR_CANCEL;
1061 
1062 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
1063 		    (int)current_time.millis - (int)base_time.millis;
1064 		if (mytime >= timeout)
1065 			return TEE_SUCCESS;
1066 
1067 		tee_time_wait(timeout - mytime);
1068 	}
1069 
1070 	return res;
1071 }
1072 
1073 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
1074 {
1075 	struct ts_session *s = ts_get_current_session();
1076 	TEE_Result res = TEE_SUCCESS;
1077 	TEE_Result res2 = TEE_SUCCESS;
1078 	TEE_Time t = { };
1079 
1080 	switch (cat) {
1081 	case UTEE_TIME_CAT_SYSTEM:
1082 		res = tee_time_get_sys_time(&t);
1083 		break;
1084 	case UTEE_TIME_CAT_TA_PERSISTENT:
1085 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
1086 		break;
1087 	case UTEE_TIME_CAT_REE:
1088 		res = tee_time_get_ree_time(&t);
1089 		break;
1090 	default:
1091 		res = TEE_ERROR_BAD_PARAMETERS;
1092 		break;
1093 	}
1094 
1095 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
1096 		res2 = copy_to_user_private(mytime, &t, sizeof(t));
1097 		if (res2 != TEE_SUCCESS)
1098 			res = res2;
1099 	}
1100 
1101 	return res;
1102 }
1103 
1104 TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1105 {
1106 	struct ts_session *s = ts_get_current_session();
1107 	TEE_Result res = TEE_SUCCESS;
1108 	TEE_Time t = { };
1109 
1110 	res = copy_from_user_private(&t, mytime, sizeof(t));
1111 	if (res != TEE_SUCCESS)
1112 		return res;
1113 
1114 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1115 }
1116