xref: /optee_os/core/tee/tee_svc.c (revision 7215f52de31c2803716c742189f7332e0324573e)
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 <kernel/tee_compat.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 <kernel/tee_rpc.h>
39 #include <kernel/tee_rpc_types.h>
40 #include <kernel/tee_time.h>
41 
42 #include <user_ta_header.h>
43 #include <trace.h>
44 #include <kernel/trace_ta.h>
45 #include <kernel/chip_services.h>
46 
47 #include <assert.h>
48 
49 vaddr_t tee_svc_uref_base;
50 
51 void syscall_log(const void *buf __unused, size_t len __unused)
52 {
53 #ifdef CFG_TEE_CORE_TA_TRACE
54 	char *kbuf;
55 
56 	if (len == 0)
57 		return;
58 
59 	kbuf = malloc(len);
60 	if (kbuf == NULL)
61 		return;
62 	*kbuf = '\0';
63 
64 	/* log as Info/Raw traces */
65 	if (tee_svc_copy_from_user(NULL, kbuf, buf, len) == TEE_SUCCESS)
66 		TAMSG_RAW("%.*s", (int)len, kbuf);
67 
68 	free(kbuf);
69 #endif
70 }
71 
72 TEE_Result syscall_reserved(void)
73 {
74 	return TEE_ERROR_GENERIC;
75 }
76 
77 TEE_Result syscall_not_supported(void)
78 {
79 	return TEE_ERROR_NOT_SUPPORTED;
80 }
81 
82 uint32_t syscall_dummy(uint32_t *a __unused)
83 {
84 	DMSG("tee_svc_sys_dummy: a 0x%" PRIxVA, (vaddr_t)a);
85 	return 0;
86 }
87 
88 uint32_t syscall_dummy_7args(unsigned long a1 __unused,
89 			unsigned long a2 __unused, unsigned long a3 __unused,
90 			unsigned long a4 __unused, unsigned long a5 __unused,
91 			unsigned long a6 __unused, unsigned long a7 __unused)
92 {
93 	DMSG("tee_svc_sys_dummy_7args: 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, %lx, %lx\n",
94 	     a1, a2, a3, a4, a5, a6, a7);
95 	return 0;
96 }
97 
98 uint32_t syscall_nocall(void)
99 {
100 	DMSG("No syscall");
101 	return 0x1;
102 }
103 
104 /* Configuration properties */
105 /* API implementation version */
106 static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION);
107 
108 /* Implementation description (implementation-dependent) */
109 static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR);
110 
111 /*
112  * TA persistent time protection level
113  * 100: Persistent time based on an REE-controlled real-time clock
114  * and on the TEE Trusted Storage for the storage of origins (default).
115  * 1000: Persistent time based on a TEE-controlled real-time clock
116  * and the TEE Trusted Storage.
117  * The real-time clock MUST be out of reach of software attacks
118  * from the REE.
119  */
120 static const uint32_t ta_time_prot_lvl = 100;
121 
122 /* Elliptic Curve Cryptographic support (false by default) */
123 static const bool crypto_ecc_en;
124 
125 /*
126  * Trusted storage anti rollback protection level
127  * 0 (or missing): No antirollback protection (default)
128  * 100: Antirollback enforced at REE level
129  * 1000: Antirollback TEE-controlled hardware
130  */
131 static const uint32_t ts_antiroll_prot_lvl;
132 
133 /* Trusted OS implementation version */
134 static const char trustedos_impl_version[] = TO_STR(CFG_TEE_IMPL_VERSION);
135 
136 /* Trusted OS implementation version (binary value) */
137 static const uint32_t trustedos_impl_bin_version; /* 0 by default */
138 
139 /* Trusted OS implementation manufacturer name */
140 static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER);
141 
142 /* Trusted firmware version */
143 static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION);
144 
145 /* Trusted firmware version (binary value) */
146 static const uint32_t fw_impl_bin_version; /* 0 by default */
147 
148 /* Trusted firmware manufacturer name */
149 static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER);
150 
151 struct tee_props {
152 	const void *data;
153 	const size_t len;
154 };
155 
156 /* Consistent with enum utee_property */
157 const struct tee_props tee_props_lut[] = {
158 	{api_vers, sizeof(api_vers)},
159 	{descr, sizeof(descr)},
160 	{0, 0}, /* dev_id */
161 	{0, 0}, /* system time protection level */
162 	{&ta_time_prot_lvl, sizeof(ta_time_prot_lvl)},
163 	{&crypto_ecc_en, sizeof(crypto_ecc_en)},
164 	{&ts_antiroll_prot_lvl, sizeof(ts_antiroll_prot_lvl)},
165 	{trustedos_impl_version, sizeof(trustedos_impl_version)},
166 	{&trustedos_impl_bin_version,
167 		sizeof(trustedos_impl_bin_version)},
168 	{trustedos_manufacturer, sizeof(trustedos_manufacturer)},
169 	{fw_impl_version, sizeof(fw_impl_version)},
170 	{&fw_impl_bin_version, sizeof(fw_impl_bin_version)},
171 	{fw_manufacturer, sizeof(fw_manufacturer)},
172 	{0, 0}, /* client_id */
173 	{0, 0}, /* ta_app_id */
174 };
175 
176 TEE_Result syscall_get_property(unsigned long prop, void *buf, size_t blen)
177 {
178 	struct tee_ta_session *sess;
179 	TEE_Result res;
180 
181 	if (prop > ARRAY_SIZE(tee_props_lut)-1)
182 		return TEE_ERROR_NOT_IMPLEMENTED;
183 
184 	res = tee_ta_get_current_session(&sess);
185 	if (res != TEE_SUCCESS)
186 		return res;
187 
188 	switch (prop) {
189 	case UTEE_PROP_TEE_DEV_ID:
190 		{
191 			TEE_UUID uuid;
192 			const size_t nslen = 5;
193 			uint8_t data[5 +
194 				     FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = {
195 			    'O', 'P', 'T', 'E', 'E' };
196 
197 			if (blen < sizeof(uuid))
198 				return TEE_ERROR_SHORT_BUFFER;
199 
200 			if (tee_otp_get_die_id
201 					(data + nslen, sizeof(data) - nslen))
202 				return TEE_ERROR_BAD_STATE;
203 
204 			res = tee_hash_createdigest(TEE_ALG_SHA256, data,
205 						    sizeof(data),
206 						    (uint8_t *)&uuid,
207 						    sizeof(uuid));
208 			if (res != TEE_SUCCESS)
209 				return TEE_ERROR_BAD_STATE;
210 
211 			/*
212 			 * Changes the random value into and UUID as specifiec
213 			 * in RFC 4122. The magic values are from the example
214 			 * code in the RFC.
215 			 *
216 			 * TEE_UUID is defined slightly different from the RFC,
217 			 * but close enough for our purpose.
218 			 */
219 
220 			uuid.timeHiAndVersion &= 0x0fff;
221 			uuid.timeHiAndVersion |= 5 << 12;
222 
223 			/* uuid.clock_seq_hi_and_reserved in the RFC */
224 			uuid.clockSeqAndNode[0] &= 0x3f;
225 			uuid.clockSeqAndNode[0] |= 0x80;
226 
227 			return tee_svc_copy_to_user(sess, buf, &uuid,
228 						    sizeof(TEE_UUID));
229 		}
230 
231 	case UTEE_PROP_TEE_SYS_TIME_PROT_LEVEL:
232 		{
233 			uint32_t prot;
234 
235 			if (blen < sizeof(prot))
236 				return TEE_ERROR_SHORT_BUFFER;
237 			prot = tee_time_get_sys_time_protection_level();
238 			return tee_svc_copy_to_user(sess, (void *)buf,
239 						    &prot, sizeof(prot));
240 		}
241 
242 	case UTEE_PROP_CLIENT_ID:
243 		if (blen < sizeof(TEE_Identity))
244 			return TEE_ERROR_SHORT_BUFFER;
245 		return tee_svc_copy_to_user(sess, buf, &sess->clnt_id,
246 					    sizeof(TEE_Identity));
247 
248 	case UTEE_PROP_TA_APP_ID:
249 		if (blen < sizeof(TEE_UUID))
250 			return TEE_ERROR_SHORT_BUFFER;
251 		return tee_svc_copy_to_user(sess, buf, &sess->ctx->uuid,
252 					    sizeof(TEE_UUID));
253 	default:
254 		if (blen < tee_props_lut[prop].len)
255 			return TEE_ERROR_SHORT_BUFFER;
256 		return tee_svc_copy_to_user(sess, buf, tee_props_lut[prop].data,
257 					    tee_props_lut[prop].len);
258 	}
259 }
260 
261 static void utee_param_to_param(struct tee_ta_param *p, struct utee_params *up)
262 {
263 	size_t n;
264 	uint32_t types = up->types;
265 
266 	p->types = types;
267 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
268 		uintptr_t a = up->vals[n * 2];
269 		size_t b = up->vals[n * 2 + 1];
270 
271 		switch (TEE_PARAM_TYPE_GET(types, n)) {
272 		case TEE_PARAM_TYPE_MEMREF_INPUT:
273 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
274 		case TEE_PARAM_TYPE_MEMREF_INOUT:
275 			p->params[n].memref.buffer = (void *)a;
276 			p->params[n].memref.size = b;
277 			break;
278 		case TEE_PARAM_TYPE_VALUE_INPUT:
279 		case TEE_PARAM_TYPE_VALUE_INOUT:
280 			p->params[n].value.a = a;
281 			p->params[n].value.b = b;
282 			break;
283 		default:
284 			p->params[n].value.a = 0;
285 			p->params[n].value.b = 0;
286 			break;
287 		}
288 	}
289 }
290 
291 /*
292  * TA invokes some TA with parameter.
293  * If some parameters are memory references:
294  * - either the memref is inside TA private RAM: TA is not allowed to expose
295  *   its private RAM: use a temporary memory buffer and copy the data.
296  * - or the memref is not in the TA private RAM:
297  *   - if the memref was mapped to the TA, TA is allowed to expose it.
298  *   - if so, converts memref virtual address into a physical address.
299  */
300 static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess,
301 				     struct tee_ta_session *called_sess,
302 				     struct utee_params *callee_params,
303 				     struct tee_ta_param *param,
304 				     tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
305 				     tee_mm_entry_t **mm)
306 {
307 	size_t n;
308 	TEE_Result res;
309 	size_t req_mem = 0;
310 	size_t s;
311 	uint8_t *dst = 0;
312 	tee_paddr_t dst_pa, src_pa = 0;
313 	bool ta_private_memref[TEE_NUM_PARAMS];
314 
315 	/* fill 'param' input struct with caller params description buffer */
316 	if (!callee_params) {
317 		memset(param, 0, sizeof(*param));
318 	} else {
319 		res = tee_mmu_check_access_rights(sess->ctx,
320 			TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER,
321 			(tee_uaddr_t)callee_params, sizeof(struct utee_params));
322 		if (res != TEE_SUCCESS)
323 			return res;
324 		utee_param_to_param(param, callee_params);
325 	}
326 
327 	if ((called_sess != NULL) &&
328 		(called_sess->ctx->static_ta == NULL) &&
329 		(called_sess->ctx->flags & TA_FLAG_USER_MODE) == 0) {
330 		/*
331 		 * kernel TA, borrow the mapping of the calling
332 		 * during this call.
333 		 */
334 		called_sess->calling_sess = sess;
335 		return TEE_SUCCESS;
336 	}
337 
338 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
339 
340 		ta_private_memref[n] = false;
341 
342 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
343 		case TEE_PARAM_TYPE_MEMREF_INPUT:
344 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
345 		case TEE_PARAM_TYPE_MEMREF_INOUT:
346 			if (param->params[n].memref.buffer == NULL) {
347 				if (param->params[n].memref.size != 0)
348 					return TEE_ERROR_BAD_PARAMETERS;
349 				break;
350 			}
351 			/* uTA cannot expose its private memory */
352 			if (tee_mmu_is_vbuf_inside_ta_private(sess->ctx,
353 				    param->params[n].memref.buffer,
354 				    param->params[n].memref.size)) {
355 
356 				s = ROUNDUP(param->params[n].memref.size,
357 						sizeof(uint32_t));
358 				/* Check overflow */
359 				if (req_mem + s < req_mem)
360 					return TEE_ERROR_BAD_PARAMETERS;
361 				req_mem += s;
362 				ta_private_memref[n] = true;
363 				break;
364 			}
365 			if (tee_mmu_is_vbuf_intersect_ta_private(sess->ctx,
366 				    param->params[n].memref.buffer,
367 				    param->params[n].memref.size))
368 				return TEE_ERROR_BAD_PARAMETERS;
369 
370 			if (tee_mmu_user_va2pa(sess->ctx,
371 					(void *)param->params[n].memref.buffer,
372 					&src_pa) != TEE_SUCCESS)
373 				return TEE_ERROR_BAD_PARAMETERS;
374 
375 			param->param_attr[n] = tee_mmu_user_get_cache_attr(
376 				sess->ctx,
377 				(void *)param->params[n].memref.buffer);
378 
379 			param->params[n].memref.buffer = (void *)src_pa;
380 			break;
381 
382 		default:
383 			break;
384 		}
385 	}
386 
387 	if (req_mem == 0)
388 		return TEE_SUCCESS;
389 
390 	/* Allocate section in secure DDR */
391 	*mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem);
392 	if (*mm == NULL) {
393 		DMSG("tee_mm_alloc TEE_ERROR_GENERIC");
394 		return TEE_ERROR_GENERIC;
395 	}
396 
397 	/* Get the virtual address for the section in secure DDR */
398 	res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst);
399 	if (res != TEE_SUCCESS)
400 		return res;
401 	dst_pa = tee_mm_get_smem(*mm);
402 
403 	for (n = 0; n < 4; n++) {
404 
405 		if (ta_private_memref[n] == false)
406 			continue;
407 
408 		s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t));
409 
410 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
411 		case TEE_PARAM_TYPE_MEMREF_INPUT:
412 		case TEE_PARAM_TYPE_MEMREF_INOUT:
413 			if (param->params[n].memref.buffer != NULL) {
414 				res = tee_svc_copy_from_user(sess, dst,
415 						param->params[n].memref.buffer,
416 						param->params[n].memref.size);
417 				if (res != TEE_SUCCESS)
418 					return res;
419 				param->param_attr[n] =
420 					tee_mmu_kmap_get_cache_attr(dst);
421 				param->params[n].memref.buffer = (void *)dst_pa;
422 				tmp_buf_pa[n] = dst_pa;
423 				dst += s;
424 				dst_pa += s;
425 			}
426 			break;
427 
428 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
429 			if (param->params[n].memref.buffer != NULL) {
430 				param->param_attr[n] =
431 					tee_mmu_kmap_get_cache_attr(dst);
432 				param->params[n].memref.buffer = (void *)dst_pa;
433 				tmp_buf_pa[n] = dst_pa;
434 				dst += s;
435 				dst_pa += s;
436 			}
437 			break;
438 
439 		default:
440 			continue;
441 		}
442 	}
443 
444 	tee_mmu_kunmap(dst, req_mem);
445 
446 	return TEE_SUCCESS;
447 }
448 
449 /*
450  * Back from execution of service: update parameters passed from TA:
451  * If some parameters were memory references:
452  * - either the memref was temporary: copy back data and update size
453  * - or it was the original TA memref: update only the size value.
454  */
455 static TEE_Result tee_svc_update_out_param(
456 		struct tee_ta_session *sess,
457 		struct tee_ta_session *called_sess,
458 		struct tee_ta_param *param,
459 		tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
460 		struct utee_params *usr_param)
461 {
462 	size_t n;
463 	void *p;
464 	bool have_private_mem_map = (called_sess == NULL) ||
465 		(called_sess->ctx->static_ta != NULL) ||
466 		((called_sess->ctx->flags & TA_FLAG_USER_MODE) != 0);
467 
468 
469 	tee_ta_set_current_session(sess);
470 
471 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
472 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
473 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
474 		case TEE_PARAM_TYPE_MEMREF_INOUT:
475 			p = (void *)(uintptr_t)usr_param->vals[n * 2];
476 
477 			/* outside TA private => memref is valid, update size */
478 			if (!tee_mmu_is_vbuf_inside_ta_private(sess->ctx, p,
479 					param->params[n].memref.size)) {
480 				usr_param->vals[n * 2 + 1] =
481 					param->params[n].memref.size;
482 				break;
483 			}
484 
485 			/*
486 			 * If we called a kernel TA the parameters are in shared
487 			 * memory and no copy is needed.
488 			 */
489 			if (have_private_mem_map &&
490 			    param->params[n].memref.size <=
491 			    usr_param->vals[n * 2 + 1]) {
492 				uint8_t *src = 0;
493 				TEE_Result res;
494 
495 				/* FIXME: TA_RAM is already mapped ! */
496 				res = tee_mmu_kmap(tmp_buf_pa[n],
497 					param->params[n].memref.size, &src);
498 				if (res != TEE_SUCCESS)
499 					return TEE_ERROR_GENERIC;
500 
501 				res = tee_svc_copy_to_user(sess, p, src,
502 						 param->params[n].memref.size);
503 				if (res != TEE_SUCCESS)
504 					return res;
505 				tee_mmu_kunmap(src,
506 					       param->params[n].memref.size);
507 
508 			}
509 			usr_param->vals[n * 2 + 1] =
510 				param->params[n].memref.size;
511 			break;
512 
513 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
514 		case TEE_PARAM_TYPE_VALUE_INOUT:
515 			usr_param->vals[n * 2] = param->params[n].value.a;
516 			usr_param->vals[n * 2 + 1] = param->params[n].value.b;
517 			break;
518 
519 		default:
520 			continue;
521 		}
522 	}
523 
524 	return TEE_SUCCESS;
525 }
526 
527 /* Called when a TA calls an OpenSession on another TA */
528 TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
529 			unsigned long cancel_req_to,
530 			struct utee_params *usr_param, uint32_t *ta_sess,
531 			uint32_t *ret_orig)
532 {
533 	TEE_Result res;
534 	uint32_t ret_o = TEE_ORIGIN_TEE;
535 	struct tee_ta_session *s = NULL;
536 	struct tee_ta_session *sess;
537 	tee_mm_entry_t *mm_param = NULL;
538 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
539 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
540 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
541 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
542 
543 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
544 		res = TEE_ERROR_OUT_OF_MEMORY;
545 		goto out_free_only;
546 	}
547 
548 	memset(param, 0, sizeof(struct tee_ta_param));
549 
550 	res = tee_ta_get_current_session(&sess);
551 	if (res != TEE_SUCCESS)
552 		goto out_free_only;
553 
554 	res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID));
555 	if (res != TEE_SUCCESS)
556 		goto function_exit;
557 
558 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
559 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
560 
561 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_pa,
562 				 &mm_param);
563 	if (res != TEE_SUCCESS)
564 		goto function_exit;
565 
566 	/*
567 	 * Find session of a multi session TA or a static TA
568 	 * In such a case, there is no need to ask the supplicant for the TA
569 	 * code
570 	 */
571 	res = tee_ta_open_session(&ret_o, &s, &sess->ctx->open_sessions, uuid,
572 				  clnt_id, cancel_req_to, param);
573 	if (res != TEE_SUCCESS)
574 		goto function_exit;
575 
576 	res = tee_svc_update_out_param(sess, NULL, param, tmp_buf_pa,
577 				       usr_param);
578 
579 function_exit:
580 	tee_ta_set_current_session(sess);
581 
582 	if (mm_param != NULL) {
583 		TEE_Result res2;
584 		void *va = 0;
585 
586 		res2 =
587 		    tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va);
588 		if (res2 == TEE_SUCCESS)
589 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
590 	}
591 	tee_mm_free(mm_param);
592 	if (res == TEE_SUCCESS)
593 		tee_svc_copy_kaddr_to_uref(sess, ta_sess, s);
594 	tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
595 
596 out_free_only:
597 	free(param);
598 	free(uuid);
599 	free(clnt_id);
600 	return res;
601 }
602 
603 TEE_Result syscall_close_ta_session(unsigned long ta_sess)
604 {
605 	TEE_Result res;
606 	struct tee_ta_session *sess;
607 	TEE_Identity clnt_id;
608 	struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess);
609 
610 	res = tee_ta_get_current_session(&sess);
611 	if (res != TEE_SUCCESS)
612 		return res;
613 
614 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
615 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
616 
617 	tee_ta_set_current_session(NULL);
618 	res = tee_ta_close_session(s, &sess->ctx->open_sessions, &clnt_id);
619 	tee_ta_set_current_session(sess);
620 	return res;
621 }
622 
623 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
624 			unsigned long cancel_req_to, unsigned long cmd_id,
625 			struct utee_params *usr_param, uint32_t *ret_orig)
626 {
627 	TEE_Result res;
628 	uint32_t ret_o = TEE_ORIGIN_TEE;
629 	struct tee_ta_param param = { 0 };
630 	TEE_Identity clnt_id;
631 	struct tee_ta_session *sess;
632 	struct tee_ta_session *called_sess;
633 	tee_mm_entry_t *mm_param = NULL;
634 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
635 
636 	res = tee_ta_get_current_session(&sess);
637 	if (res != TEE_SUCCESS)
638 		return res;
639 
640 	called_sess = tee_ta_get_session(
641 				(vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true,
642 				&sess->ctx->open_sessions);
643 	if (!called_sess)
644 		return TEE_ERROR_BAD_PARAMETERS;
645 
646 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
647 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
648 
649 	res = tee_svc_copy_param(sess, called_sess, usr_param, &param,
650 				 tmp_buf_pa, &mm_param);
651 	if (res != TEE_SUCCESS)
652 		goto function_exit;
653 
654 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
655 				    cancel_req_to, cmd_id, &param);
656 
657 	if (res != TEE_SUCCESS)
658 		goto function_exit;
659 
660 	res = tee_svc_update_out_param(sess, called_sess, &param, tmp_buf_pa,
661 				       usr_param);
662 	if (res != TEE_SUCCESS)
663 		goto function_exit;
664 
665 function_exit:
666 	tee_ta_set_current_session(sess);
667 	called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */
668 	tee_ta_put_session(called_sess);
669 
670 	if (mm_param != NULL) {
671 		TEE_Result res2;
672 		void *va = 0;
673 
674 		res2 =
675 		    tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va);
676 		if (res2 == TEE_SUCCESS)
677 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
678 	}
679 	tee_mm_free(mm_param);
680 	if (ret_orig)
681 		tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
682 	return res;
683 }
684 
685 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
686 				       size_t len)
687 {
688 	TEE_Result res;
689 	struct tee_ta_session *s;
690 
691 	res = tee_ta_get_current_session(&s);
692 	if (res != TEE_SUCCESS)
693 		return res;
694 
695 	return tee_mmu_check_access_rights(s->ctx, flags, (tee_uaddr_t)buf,
696 					   len);
697 }
698 
699 TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr,
700 				  const void *uaddr, size_t len)
701 {
702 	TEE_Result res;
703 	struct tee_ta_session *s;
704 
705 	if (sess == NULL) {
706 		res = tee_ta_get_current_session(&s);
707 		if (res != TEE_SUCCESS)
708 			return res;
709 	} else {
710 		s = sess;
711 		tee_ta_set_current_session(s);
712 	}
713 	res =
714 	    tee_mmu_check_access_rights(s->ctx,
715 					TEE_MEMORY_ACCESS_READ |
716 					TEE_MEMORY_ACCESS_ANY_OWNER,
717 					(tee_uaddr_t)uaddr, len);
718 	if (res != TEE_SUCCESS)
719 		return res;
720 
721 	memcpy(kaddr, uaddr, len);
722 	return TEE_SUCCESS;
723 }
724 
725 TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr,
726 				const void *kaddr, size_t len)
727 {
728 	TEE_Result res;
729 	struct tee_ta_session *s;
730 
731 	if (sess == NULL) {
732 		res = tee_ta_get_current_session(&s);
733 		if (res != TEE_SUCCESS)
734 			return res;
735 	} else {
736 		s = sess;
737 		tee_ta_set_current_session(s);
738 	}
739 
740 	res =
741 	    tee_mmu_check_access_rights(s->ctx,
742 					TEE_MEMORY_ACCESS_WRITE |
743 					TEE_MEMORY_ACCESS_ANY_OWNER,
744 					(tee_uaddr_t)uaddr, len);
745 	if (res != TEE_SUCCESS)
746 		return res;
747 
748 	memcpy(uaddr, kaddr, len);
749 	return TEE_SUCCESS;
750 }
751 
752 TEE_Result tee_svc_copy_kaddr_to_uref(struct tee_ta_session *sess,
753 			uint32_t *uref, void *kaddr)
754 {
755 	uint32_t ref = tee_svc_kaddr_to_uref(kaddr);
756 
757 	return tee_svc_copy_to_user(sess, uref, &ref, sizeof(ref));
758 }
759 
760 static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time)
761 {
762 	TEE_Time current_time;
763 
764 	if (s->cancel_mask)
765 		return false;
766 
767 	if (s->cancel)
768 		return true;
769 
770 	if (s->cancel_time.seconds == UINT32_MAX)
771 		return false;
772 
773 	if (curr_time != NULL)
774 		current_time = *curr_time;
775 	else if (tee_time_get_sys_time(&current_time) != TEE_SUCCESS)
776 		return false;
777 
778 	if (current_time.seconds > s->cancel_time.seconds ||
779 	    (current_time.seconds == s->cancel_time.seconds &&
780 	     current_time.millis >= s->cancel_time.millis)) {
781 		return true;
782 	}
783 
784 	return false;
785 }
786 
787 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
788 {
789 	TEE_Result res;
790 	struct tee_ta_session *s = NULL;
791 	uint32_t c;
792 
793 	res = tee_ta_get_current_session(&s);
794 	if (res != TEE_SUCCESS)
795 		return res;
796 
797 	c = session_is_cancelled(s, NULL);
798 
799 	return tee_svc_copy_to_user(s, cancel, &c, sizeof(c));
800 }
801 
802 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
803 {
804 	TEE_Result res;
805 	struct tee_ta_session *s = NULL;
806 	uint32_t m;
807 
808 	res = tee_ta_get_current_session(&s);
809 	if (res != TEE_SUCCESS)
810 		return res;
811 
812 	m = s->cancel_mask;
813 	s->cancel_mask = false;
814 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
815 }
816 
817 TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
818 {
819 	TEE_Result res;
820 	struct tee_ta_session *s = NULL;
821 	uint32_t m;
822 
823 	res = tee_ta_get_current_session(&s);
824 	if (res != TEE_SUCCESS)
825 		return res;
826 
827 	m = s->cancel_mask;
828 	s->cancel_mask = true;
829 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
830 }
831 
832 TEE_Result syscall_wait(unsigned long timeout)
833 {
834 	TEE_Result res = TEE_SUCCESS;
835 	uint32_t mytime = 0;
836 	struct tee_ta_session *s;
837 	TEE_Time base_time;
838 	TEE_Time current_time;
839 
840 	res = tee_ta_get_current_session(&s);
841 	if (res != TEE_SUCCESS)
842 		return res;
843 
844 	res = tee_time_get_sys_time(&base_time);
845 	if (res != TEE_SUCCESS)
846 		return res;
847 
848 	while (true) {
849 		res = tee_time_get_sys_time(&current_time);
850 		if (res != TEE_SUCCESS)
851 			return res;
852 
853 		if (session_is_cancelled(s, &current_time))
854 			return TEE_ERROR_CANCEL;
855 
856 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
857 		    (int)current_time.millis - (int)base_time.millis;
858 		if (mytime >= timeout)
859 			return TEE_SUCCESS;
860 
861 		tee_time_wait(timeout - mytime);
862 	}
863 
864 	return res;
865 }
866 
867 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
868 {
869 	TEE_Result res, res2;
870 	struct tee_ta_session *s = NULL;
871 	TEE_Time t;
872 
873 	res = tee_ta_get_current_session(&s);
874 	if (res != TEE_SUCCESS)
875 		return res;
876 
877 	switch (cat) {
878 	case UTEE_TIME_CAT_SYSTEM:
879 		res = tee_time_get_sys_time(&t);
880 		break;
881 	case UTEE_TIME_CAT_TA_PERSISTENT:
882 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
883 		break;
884 	case UTEE_TIME_CAT_REE:
885 		res = tee_time_get_ree_time(&t);
886 		break;
887 	default:
888 		res = TEE_ERROR_BAD_PARAMETERS;
889 		break;
890 	}
891 
892 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
893 		res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t));
894 		if (res2 != TEE_SUCCESS)
895 			res = res2;
896 	}
897 
898 	return res;
899 }
900 
901 TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
902 {
903 	TEE_Result res;
904 	struct tee_ta_session *s = NULL;
905 	TEE_Time t;
906 
907 	res = tee_ta_get_current_session(&s);
908 	if (res != TEE_SUCCESS)
909 		return res;
910 
911 	res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t));
912 	if (res != TEE_SUCCESS)
913 		return res;
914 
915 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
916 }
917 
918 #ifdef CFG_CACHE_API
919 TEE_Result syscall_cache_operation(void *va, size_t len, unsigned long op)
920 {
921 	TEE_Result res;
922 	struct tee_ta_session *s = NULL;
923 
924 	res = tee_ta_get_current_session(&s);
925 	if (res != TEE_SUCCESS)
926 		return res;
927 
928 	if ((s->ctx->flags & TA_FLAG_CACHE_MAINTENANCE) == 0)
929 		return TEE_ERROR_NOT_SUPPORTED;
930 
931 	return tee_uta_cache_operation(s, op, va, len);
932 }
933 #endif
934