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