xref: /optee_os/core/tee/tee_svc.c (revision 3f4b5250f820ec567422230370fa94e3d6277265)
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((struct tee_ta_session *)ta_sess,
616 			&sess->ctx->open_sessions,
617 				   &clnt_id);
618 	tee_ta_set_current_session(sess);
619 	return res;
620 }
621 
622 TEE_Result tee_svc_invoke_ta_command(TEE_TASessionHandle ta_sess,
623 				     uint32_t cancel_req_to, uint32_t cmd_id,
624 				     uint32_t param_types,
625 				     struct abi_user32_param *usr_param,
626 				     uint32_t *ret_orig)
627 {
628 	TEE_Result res;
629 	uint32_t ret_o = TEE_ORIGIN_TEE;
630 	struct tee_ta_param param = { 0 };
631 	TEE_Identity clnt_id;
632 	struct tee_ta_session *sess;
633 	struct tee_ta_session *called_sess = (struct tee_ta_session *)ta_sess;
634 	tee_mm_entry_t *mm_param = NULL;
635 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
636 
637 	res = tee_ta_get_current_session(&sess);
638 	if (res != TEE_SUCCESS)
639 		return res;
640 
641 	res =
642 	    tee_ta_verify_session_pointer(called_sess,
643 					  &sess->ctx->open_sessions);
644 	if (res != TEE_SUCCESS)
645 		return res;
646 
647 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
648 	memcpy(&clnt_id.uuid, &sess->ctx->head->uuid, sizeof(TEE_UUID));
649 
650 	res = tee_svc_copy_param(sess, called_sess, param_types, usr_param,
651 				 &param, tmp_buf_pa, &mm_param);
652 	if (res != TEE_SUCCESS)
653 		goto function_exit;
654 
655 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
656 				    cancel_req_to, cmd_id, &param);
657 
658 	if (res != TEE_SUCCESS)
659 		goto function_exit;
660 
661 	res = tee_svc_update_out_param(sess, called_sess, &param, tmp_buf_pa,
662 				       usr_param);
663 	if (res != TEE_SUCCESS)
664 		goto function_exit;
665 
666 function_exit:
667 	tee_ta_set_current_session(sess);
668 	called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */
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 tee_svc_check_access_rights(uint32_t 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 /*
753  * Copy a kernel address into a 32-bit user buffer. In 64-bit mode, this will
754  * fail if the address is not in the lower 4 GiB.
755  */
756 TEE_Result tee_svc_copy_kaddr_to_user32(struct tee_ta_session *sess,
757 					uint32_t *uaddr, const void *kaddr)
758 {
759 	uint32_t lo = (long)kaddr & 0xFFFFFFFF;
760 
761 	if ((long)lo != (long)kaddr) {
762 		EMSG("Unexpected high kernel address\n");
763 		return TEE_ERROR_GENERIC;
764 	}
765 	return tee_svc_copy_to_user(sess, uaddr, &lo, sizeof(lo));
766 }
767 
768 static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time)
769 {
770 	TEE_Time current_time;
771 
772 	if (s->cancel_mask)
773 		return false;
774 
775 	if (s->cancel)
776 		return true;
777 
778 	if (s->cancel_time.seconds == UINT32_MAX)
779 		return false;
780 
781 	if (curr_time != NULL)
782 		current_time = *curr_time;
783 	else if (tee_time_get_sys_time(&current_time) != TEE_SUCCESS)
784 		return false;
785 
786 	if (current_time.seconds > s->cancel_time.seconds ||
787 	    (current_time.seconds == s->cancel_time.seconds &&
788 	     current_time.millis >= s->cancel_time.millis)) {
789 		return true;
790 	}
791 
792 	return false;
793 }
794 
795 TEE_Result tee_svc_get_cancellation_flag(bool *cancel)
796 {
797 	TEE_Result res;
798 	struct tee_ta_session *s = NULL;
799 	bool c;
800 
801 	res = tee_ta_get_current_session(&s);
802 	if (res != TEE_SUCCESS)
803 		return res;
804 
805 	c = session_is_cancelled(s, NULL);
806 
807 	return tee_svc_copy_to_user(s, cancel, &c, sizeof(c));
808 }
809 
810 TEE_Result tee_svc_unmask_cancellation(bool *old_mask)
811 {
812 	TEE_Result res;
813 	struct tee_ta_session *s = NULL;
814 	bool m;
815 
816 	res = tee_ta_get_current_session(&s);
817 	if (res != TEE_SUCCESS)
818 		return res;
819 
820 	m = s->cancel_mask;
821 	s->cancel_mask = false;
822 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
823 }
824 
825 TEE_Result tee_svc_mask_cancellation(bool *old_mask)
826 {
827 	TEE_Result res;
828 	struct tee_ta_session *s = NULL;
829 	bool m;
830 
831 	res = tee_ta_get_current_session(&s);
832 	if (res != TEE_SUCCESS)
833 		return res;
834 
835 	m = s->cancel_mask;
836 	s->cancel_mask = true;
837 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
838 }
839 
840 TEE_Result tee_svc_wait(uint32_t timeout)
841 {
842 	TEE_Result res = TEE_SUCCESS;
843 	uint32_t mytime = 0;
844 	struct tee_ta_session *s;
845 	TEE_Time base_time;
846 	TEE_Time current_time;
847 
848 	res = tee_ta_get_current_session(&s);
849 	if (res != TEE_SUCCESS)
850 		return res;
851 
852 	res = tee_time_get_sys_time(&base_time);
853 	if (res != TEE_SUCCESS)
854 		return res;
855 
856 	while (true) {
857 		res = tee_time_get_sys_time(&current_time);
858 		if (res != TEE_SUCCESS)
859 			return res;
860 
861 		if (session_is_cancelled(s, &current_time))
862 			return TEE_ERROR_CANCEL;
863 
864 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
865 		    (int)current_time.millis - (int)base_time.millis;
866 		if (mytime >= timeout)
867 			return TEE_SUCCESS;
868 
869 		tee_time_wait(timeout - mytime);
870 	}
871 
872 	return res;
873 }
874 
875 TEE_Result tee_svc_get_time(enum utee_time_category cat, TEE_Time *mytime)
876 {
877 	TEE_Result res, res2;
878 	struct tee_ta_session *s = NULL;
879 	TEE_Time t;
880 
881 	res = tee_ta_get_current_session(&s);
882 	if (res != TEE_SUCCESS)
883 		return res;
884 
885 	switch (cat) {
886 	case UTEE_TIME_CAT_SYSTEM:
887 		res = tee_time_get_sys_time(&t);
888 		break;
889 	case UTEE_TIME_CAT_TA_PERSISTENT:
890 		res = tee_time_get_ta_time((const void *)&s->ctx->head->uuid,
891 					   &t);
892 		break;
893 	case UTEE_TIME_CAT_REE:
894 		res = tee_time_get_ree_time(&t);
895 		break;
896 	default:
897 		res = TEE_ERROR_BAD_PARAMETERS;
898 		break;
899 	}
900 
901 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
902 		res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t));
903 		if (res2 != TEE_SUCCESS)
904 			res = res2;
905 	}
906 
907 	return res;
908 }
909 
910 TEE_Result tee_svc_set_ta_time(const TEE_Time *mytime)
911 {
912 	TEE_Result res;
913 	struct tee_ta_session *s = NULL;
914 	TEE_Time t;
915 
916 	res = tee_ta_get_current_session(&s);
917 	if (res != TEE_SUCCESS)
918 		return res;
919 
920 	res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t));
921 	if (res != TEE_SUCCESS)
922 		return res;
923 
924 	return tee_time_set_ta_time((const void *)&s->ctx->head->uuid, &t);
925 }
926 
927 #ifdef CFG_CACHE_API
928 TEE_Result tee_svc_cache_operation(void *va, size_t len,
929 				   enum utee_cache_operation op)
930 {
931 	TEE_Result res;
932 	struct tee_ta_session *s = NULL;
933 
934 	res = tee_ta_get_current_session(&s);
935 	if (res != TEE_SUCCESS)
936 		return res;
937 
938 	if ((s->ctx->flags & TA_FLAG_CACHE_MAINTENANCE) == 0)
939 		return TEE_ERROR_NOT_SUPPORTED;
940 
941 	return tee_uta_cache_operation(s, op, va, len);
942 }
943 #endif
944