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