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