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