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