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