xref: /optee_os/core/tee/tee_svc.c (revision 1bb929836182ecb96d2d9d268daa807c67596396)
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 	mutex_lock(&tee_ta_mutex);
555 #ifdef CFG_PAGED_USER_TA
556 	*mobj = mobj_seccpy_shm_alloc(size);
557 #else
558 	*mobj = mobj_mm_alloc(mobj_sec_ddr, size, &tee_mm_sec_ddr);
559 #endif
560 	mutex_unlock(&tee_ta_mutex);
561 	if (!*mobj)
562 		return TEE_ERROR_GENERIC;
563 
564 	*va = mobj_get_va(*mobj, 0);
565 	return TEE_SUCCESS;
566 }
567 
568 /*
569  * TA invokes some TA with parameter.
570  * If some parameters are memory references:
571  * - either the memref is inside TA private RAM: TA is not allowed to expose
572  *   its private RAM: use a temporary memory buffer and copy the data.
573  * - or the memref is not in the TA private RAM:
574  *   - if the memref was mapped to the TA, TA is allowed to expose it.
575  *   - if so, converts memref virtual address into a physical address.
576  */
577 static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess,
578 				     struct tee_ta_session *called_sess,
579 				     struct utee_params *callee_params,
580 				     struct tee_ta_param *param,
581 				     void *tmp_buf_va[TEE_NUM_PARAMS],
582 				     struct mobj **mobj_tmp)
583 {
584 	size_t n;
585 	TEE_Result res;
586 	size_t req_mem = 0;
587 	size_t s;
588 	uint8_t *dst = 0;
589 	bool ta_private_memref[TEE_NUM_PARAMS];
590 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
591 	void *va;
592 	size_t dst_offs;
593 
594 	/* fill 'param' input struct with caller params description buffer */
595 	if (!callee_params) {
596 		memset(param, 0, sizeof(*param));
597 	} else {
598 		res = tee_mmu_check_access_rights(utc,
599 			TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER,
600 			(uaddr_t)callee_params, sizeof(struct utee_params));
601 		if (res != TEE_SUCCESS)
602 			return res;
603 		utee_param_to_param(param, callee_params);
604 	}
605 
606 	if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) {
607 		/*
608 		 * static TA, borrow the mapping of the calling
609 		 * during this call.
610 		 */
611 		return TEE_SUCCESS;
612 	}
613 
614 	/* All mobj in param are of type MOJB_TYPE_VIRT */
615 
616 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
617 
618 		ta_private_memref[n] = false;
619 
620 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
621 		case TEE_PARAM_TYPE_MEMREF_INPUT:
622 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
623 		case TEE_PARAM_TYPE_MEMREF_INOUT:
624 			va = (void *)param->u[n].mem.offs;
625 			s = param->u[n].mem.size;
626 			if (!va) {
627 				if (!s)
628 					return TEE_ERROR_BAD_PARAMETERS;
629 				break;
630 			}
631 			/* uTA cannot expose its private memory */
632 			if (tee_mmu_is_vbuf_inside_ta_private(utc, va, s)) {
633 
634 				s = ROUNDUP(s, sizeof(uint32_t));
635 				if (ADD_OVERFLOW(req_mem, s, &req_mem))
636 					return TEE_ERROR_BAD_PARAMETERS;
637 				ta_private_memref[n] = true;
638 				break;
639 			}
640 
641 			res = tee_mmu_vbuf_to_mobj_offs(utc, va, s,
642 							&param->u[n].mem.mobj,
643 							&param->u[n].mem.offs);
644 			if (res != TEE_SUCCESS)
645 				return res;
646 			break;
647 		default:
648 			break;
649 		}
650 	}
651 
652 	if (req_mem == 0)
653 		return TEE_SUCCESS;
654 
655 	res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst);
656 	if (res != TEE_SUCCESS)
657 		return res;
658 	dst_offs = 0;
659 
660 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
661 
662 		if (!ta_private_memref[n])
663 			continue;
664 
665 		s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t));
666 
667 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
668 		case TEE_PARAM_TYPE_MEMREF_INPUT:
669 		case TEE_PARAM_TYPE_MEMREF_INOUT:
670 			va = (void *)param->u[n].mem.offs;
671 			if (va) {
672 				res = tee_svc_copy_from_user(dst, va,
673 						param->u[n].mem.size);
674 				if (res != TEE_SUCCESS)
675 					return res;
676 				param->u[n].mem.offs = dst_offs;
677 				param->u[n].mem.mobj = *mobj_tmp;
678 				tmp_buf_va[n] = dst;
679 				dst += s;
680 				dst_offs += s;
681 			}
682 			break;
683 
684 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
685 			va = (void *)param->u[n].mem.offs;
686 			if (va) {
687 				param->u[n].mem.offs = dst_offs;
688 				param->u[n].mem.mobj = *mobj_tmp;
689 				tmp_buf_va[n] = dst;
690 				dst += s;
691 				dst_offs += s;
692 			}
693 			break;
694 
695 		default:
696 			continue;
697 		}
698 	}
699 
700 	return TEE_SUCCESS;
701 }
702 
703 /*
704  * Back from execution of service: update parameters passed from TA:
705  * If some parameters were memory references:
706  * - either the memref was temporary: copy back data and update size
707  * - or it was the original TA memref: update only the size value.
708  */
709 static TEE_Result tee_svc_update_out_param(
710 		struct tee_ta_session *sess,
711 		struct tee_ta_session *called_sess,
712 		struct tee_ta_param *param,
713 		void *tmp_buf_va[TEE_NUM_PARAMS],
714 		struct utee_params *usr_param)
715 {
716 	size_t n;
717 	void *p;
718 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
719 	bool have_private_mem_map = is_user_ta_ctx(called_sess->ctx);
720 
721 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
722 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
723 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
724 		case TEE_PARAM_TYPE_MEMREF_INOUT:
725 			p = (void *)(uintptr_t)usr_param->vals[n * 2];
726 
727 			/* outside TA private => memref is valid, update size */
728 			if (!tee_mmu_is_vbuf_inside_ta_private(utc, p,
729 					param->u[n].mem.size)) {
730 				usr_param->vals[n * 2 + 1] =
731 					param->u[n].mem.size;
732 				break;
733 			}
734 
735 			/*
736 			 * If we called a kernel TA the parameters are in shared
737 			 * memory and no copy is needed.
738 			 */
739 			if (have_private_mem_map &&
740 			    param->u[n].mem.size <=
741 			    usr_param->vals[n * 2 + 1]) {
742 				uint8_t *src = tmp_buf_va[n];
743 				TEE_Result res;
744 
745 				res = tee_svc_copy_to_user(p, src,
746 						 param->u[n].mem.size);
747 				if (res != TEE_SUCCESS)
748 					return res;
749 
750 			}
751 			usr_param->vals[n * 2 + 1] = param->u[n].mem.size;
752 			break;
753 
754 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
755 		case TEE_PARAM_TYPE_VALUE_INOUT:
756 			usr_param->vals[n * 2] = param->u[n].val.a;
757 			usr_param->vals[n * 2 + 1] = param->u[n].val.b;
758 			break;
759 
760 		default:
761 			continue;
762 		}
763 	}
764 
765 	return TEE_SUCCESS;
766 }
767 
768 /* Called when a TA calls an OpenSession on another TA */
769 TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
770 			unsigned long cancel_req_to,
771 			struct utee_params *usr_param, uint32_t *ta_sess,
772 			uint32_t *ret_orig)
773 {
774 	TEE_Result res;
775 	uint32_t ret_o = TEE_ORIGIN_TEE;
776 	struct tee_ta_session *s = NULL;
777 	struct tee_ta_session *sess;
778 	struct mobj *mobj_param = NULL;
779 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
780 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
781 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
782 	void *tmp_buf_va[TEE_NUM_PARAMS];
783 	struct user_ta_ctx *utc;
784 
785 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
786 		res = TEE_ERROR_OUT_OF_MEMORY;
787 		goto out_free_only;
788 	}
789 
790 	memset(param, 0, sizeof(struct tee_ta_param));
791 
792 	res = tee_ta_get_current_session(&sess);
793 	if (res != TEE_SUCCESS)
794 		goto out_free_only;
795 	utc = to_user_ta_ctx(sess->ctx);
796 
797 	res = tee_svc_copy_from_user(uuid, dest, sizeof(TEE_UUID));
798 	if (res != TEE_SUCCESS)
799 		goto function_exit;
800 
801 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
802 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
803 
804 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va,
805 				 &mobj_param);
806 	if (res != TEE_SUCCESS)
807 		goto function_exit;
808 
809 	/*
810 	 * Find session of a multi session TA or a static TA
811 	 * In such a case, there is no need to ask the supplicant for the TA
812 	 * code
813 	 */
814 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
815 				  clnt_id, cancel_req_to, param);
816 	if (res != TEE_SUCCESS)
817 		goto function_exit;
818 
819 	res = tee_svc_update_out_param(sess, s, param, tmp_buf_va, usr_param);
820 
821 function_exit:
822 	if (mobj_param) {
823 		mutex_lock(&tee_ta_mutex);
824 		mobj_free(mobj_param);
825 		mutex_unlock(&tee_ta_mutex);
826 	}
827 	if (res == TEE_SUCCESS)
828 		tee_svc_copy_kaddr_to_uref(ta_sess, s);
829 	tee_svc_copy_to_user(ret_orig, &ret_o, sizeof(ret_o));
830 
831 out_free_only:
832 	free(param);
833 	free(uuid);
834 	free(clnt_id);
835 	return res;
836 }
837 
838 TEE_Result syscall_close_ta_session(unsigned long ta_sess)
839 {
840 	TEE_Result res;
841 	struct tee_ta_session *sess;
842 	TEE_Identity clnt_id;
843 	struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess);
844 	struct user_ta_ctx *utc;
845 
846 	res = tee_ta_get_current_session(&sess);
847 	if (res != TEE_SUCCESS)
848 		return res;
849 	utc = to_user_ta_ctx(sess->ctx);
850 
851 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
852 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
853 
854 	return tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
855 }
856 
857 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
858 			unsigned long cancel_req_to, unsigned long cmd_id,
859 			struct utee_params *usr_param, uint32_t *ret_orig)
860 {
861 	TEE_Result res;
862 	TEE_Result res2;
863 	uint32_t ret_o = TEE_ORIGIN_TEE;
864 	struct tee_ta_param param = { 0 };
865 	TEE_Identity clnt_id;
866 	struct tee_ta_session *sess;
867 	struct tee_ta_session *called_sess;
868 	struct mobj *mobj_param = NULL;
869 	void *tmp_buf_va[TEE_NUM_PARAMS];
870 	struct user_ta_ctx *utc;
871 
872 	res = tee_ta_get_current_session(&sess);
873 	if (res != TEE_SUCCESS)
874 		return res;
875 	utc = to_user_ta_ctx(sess->ctx);
876 
877 	called_sess = tee_ta_get_session(
878 				(vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true,
879 				&utc->open_sessions);
880 	if (!called_sess)
881 		return TEE_ERROR_BAD_PARAMETERS;
882 
883 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
884 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
885 
886 	res = tee_svc_copy_param(sess, called_sess, usr_param, &param,
887 				 tmp_buf_va, &mobj_param);
888 	if (res != TEE_SUCCESS)
889 		goto function_exit;
890 
891 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
892 				    cancel_req_to, cmd_id, &param);
893 
894 	res2 = tee_svc_update_out_param(sess, called_sess, &param, tmp_buf_va,
895 					usr_param);
896 	if (res2 != TEE_SUCCESS) {
897 		/*
898 		 * Spec for TEE_InvokeTACommand() says:
899 		 * "If the return origin is different from
900 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
901 		 * before it could reach the destination Trusted
902 		 * Application."
903 		 *
904 		 * But if we can't update params to the caller we have no
905 		 * choice we need to return some error to indicate that
906 		 * parameters aren't updated as expected.
907 		 */
908 		ret_o = TEE_ORIGIN_TEE;
909 		res = res2;
910 	}
911 
912 function_exit:
913 	tee_ta_put_session(called_sess);
914 	if (mobj_param) {
915 		mutex_lock(&tee_ta_mutex);
916 		mobj_free(mobj_param);
917 		mutex_unlock(&tee_ta_mutex);
918 	}
919 	if (ret_orig)
920 		tee_svc_copy_to_user(ret_orig, &ret_o, sizeof(ret_o));
921 	return res;
922 }
923 
924 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
925 				       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 	return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags,
935 					   (uaddr_t)buf, len);
936 }
937 
938 TEE_Result tee_svc_copy_from_user(void *kaddr, const void *uaddr, size_t len)
939 {
940 	TEE_Result res;
941 	struct tee_ta_session *s;
942 
943 	res = tee_ta_get_current_session(&s);
944 	if (res != TEE_SUCCESS)
945 		return res;
946 
947 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
948 					TEE_MEMORY_ACCESS_READ |
949 					TEE_MEMORY_ACCESS_ANY_OWNER,
950 					(uaddr_t)uaddr, len);
951 	if (res != TEE_SUCCESS)
952 		return res;
953 
954 	memcpy(kaddr, uaddr, len);
955 	return TEE_SUCCESS;
956 }
957 
958 TEE_Result tee_svc_copy_to_user(void *uaddr, const void *kaddr, size_t len)
959 {
960 	TEE_Result res;
961 	struct tee_ta_session *s;
962 
963 	res = tee_ta_get_current_session(&s);
964 	if (res != TEE_SUCCESS)
965 		return res;
966 
967 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
968 					TEE_MEMORY_ACCESS_WRITE |
969 					TEE_MEMORY_ACCESS_ANY_OWNER,
970 					(uaddr_t)uaddr, len);
971 	if (res != TEE_SUCCESS)
972 		return res;
973 
974 	memcpy(uaddr, kaddr, len);
975 	return TEE_SUCCESS;
976 }
977 
978 TEE_Result tee_svc_copy_kaddr_to_uref(uint32_t *uref, void *kaddr)
979 {
980 	uint32_t ref = tee_svc_kaddr_to_uref(kaddr);
981 
982 	return tee_svc_copy_to_user(uref, &ref, sizeof(ref));
983 }
984 
985 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
986 {
987 	TEE_Result res;
988 	struct tee_ta_session *s = NULL;
989 	uint32_t c;
990 
991 	res = tee_ta_get_current_session(&s);
992 	if (res != TEE_SUCCESS)
993 		return res;
994 
995 	c = tee_ta_session_is_cancelled(s, NULL);
996 
997 	return tee_svc_copy_to_user(cancel, &c, sizeof(c));
998 }
999 
1000 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
1001 {
1002 	TEE_Result res;
1003 	struct tee_ta_session *s = NULL;
1004 	uint32_t m;
1005 
1006 	res = tee_ta_get_current_session(&s);
1007 	if (res != TEE_SUCCESS)
1008 		return res;
1009 
1010 	m = s->cancel_mask;
1011 	s->cancel_mask = false;
1012 	return tee_svc_copy_to_user(old_mask, &m, sizeof(m));
1013 }
1014 
1015 TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
1016 {
1017 	TEE_Result res;
1018 	struct tee_ta_session *s = NULL;
1019 	uint32_t m;
1020 
1021 	res = tee_ta_get_current_session(&s);
1022 	if (res != TEE_SUCCESS)
1023 		return res;
1024 
1025 	m = s->cancel_mask;
1026 	s->cancel_mask = true;
1027 	return tee_svc_copy_to_user(old_mask, &m, sizeof(m));
1028 }
1029 
1030 TEE_Result syscall_wait(unsigned long timeout)
1031 {
1032 	TEE_Result res = TEE_SUCCESS;
1033 	uint32_t mytime = 0;
1034 	struct tee_ta_session *s;
1035 	TEE_Time base_time;
1036 	TEE_Time current_time;
1037 
1038 	res = tee_ta_get_current_session(&s);
1039 	if (res != TEE_SUCCESS)
1040 		return res;
1041 
1042 	res = tee_time_get_sys_time(&base_time);
1043 	if (res != TEE_SUCCESS)
1044 		return res;
1045 
1046 	while (true) {
1047 		res = tee_time_get_sys_time(&current_time);
1048 		if (res != TEE_SUCCESS)
1049 			return res;
1050 
1051 		if (tee_ta_session_is_cancelled(s, &current_time))
1052 			return TEE_ERROR_CANCEL;
1053 
1054 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
1055 		    (int)current_time.millis - (int)base_time.millis;
1056 		if (mytime >= timeout)
1057 			return TEE_SUCCESS;
1058 
1059 		tee_time_wait(timeout - mytime);
1060 	}
1061 
1062 	return res;
1063 }
1064 
1065 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
1066 {
1067 	TEE_Result res, res2;
1068 	struct tee_ta_session *s = NULL;
1069 	TEE_Time t;
1070 
1071 	res = tee_ta_get_current_session(&s);
1072 	if (res != TEE_SUCCESS)
1073 		return res;
1074 
1075 	switch (cat) {
1076 	case UTEE_TIME_CAT_SYSTEM:
1077 		res = tee_time_get_sys_time(&t);
1078 		break;
1079 	case UTEE_TIME_CAT_TA_PERSISTENT:
1080 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
1081 		break;
1082 	case UTEE_TIME_CAT_REE:
1083 		res = tee_time_get_ree_time(&t);
1084 		break;
1085 	default:
1086 		res = TEE_ERROR_BAD_PARAMETERS;
1087 		break;
1088 	}
1089 
1090 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
1091 		res2 = tee_svc_copy_to_user(mytime, &t, sizeof(t));
1092 		if (res2 != TEE_SUCCESS)
1093 			res = res2;
1094 	}
1095 
1096 	return res;
1097 }
1098 
1099 TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1100 {
1101 	TEE_Result res;
1102 	struct tee_ta_session *s = NULL;
1103 	TEE_Time t;
1104 
1105 	res = tee_ta_get_current_session(&s);
1106 	if (res != TEE_SUCCESS)
1107 		return res;
1108 
1109 	res = tee_svc_copy_from_user(&t, mytime, sizeof(t));
1110 	if (res != TEE_SUCCESS)
1111 		return res;
1112 
1113 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1114 }
1115