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