xref: /optee_os/core/tee/tee_svc.c (revision 03c42787bbb0d574300fdc79ef82316837aef3a3)
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 <kernel/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 <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 <kernel/tee_core_trace.h>
43 #include <kernel/tee_kta_trace.h>
44 #include <kernel/chip_services.h>
45 #include <tee/tee_hash.h>
46 
47 
48 void tee_svc_sys_log(const void *buf, size_t len)
49 {
50 	char *kbuf;
51 
52 	if (len == 0)
53 		return;
54 
55 	kbuf = malloc(len);
56 	if (kbuf == NULL)
57 		return;
58 	*kbuf = '\0';
59 
60 	/* log as Info/Raw traces */
61 	if (tee_svc_copy_from_user(NULL, kbuf, buf, len) == TEE_SUCCESS)
62 		ATAMSG_RAW("%s", kbuf);
63 
64 	free(kbuf);
65 }
66 
67 void tee_svc_sys_panic(uint32_t code)
68 {
69 	struct tee_ta_session *sess;
70 
71 	if (tee_ta_get_current_session(&sess) == TEE_SUCCESS) {
72 		EMSG("Set session %p to panicked", (void *)sess);
73 		sess->ctx->panicked = 1;
74 		sess->ctx->panic_code = code;
75 
76 		{
77 			int *p = 0;
78 
79 			/*
80 			 * Force panicking. This memory error will be trapped by
81 			 * the error exception handler myErrorHandler()
82 			 */
83 			EMSG("Following 'DTLB exception in bundle'");
84 			EMSG("   is generated with code %d", code);
85 			*p = 1;
86 		}
87 	} else {
88 		DMSG("Panic called from unknown TA");
89 	}
90 }
91 
92 uint32_t tee_svc_sys_dummy(uint32_t *a __unused)
93 {
94 	DMSG("tee_svc_sys_dummy: a 0x%x", (unsigned int)a);
95 	return 0;
96 }
97 
98 uint32_t tee_svc_sys_dummy_7args(uint32_t a1 __unused, uint32_t a2 __unused,
99 				uint32_t a3 __unused, uint32_t a4 __unused,
100 				uint32_t a5 __unused, uint32_t a6 __unused,
101 				uint32_t a7 __unused)
102 {
103 	DMSG("tee_svc_sys_dummy_7args: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, %x, %x\n",
104 	     a1, a2, a3, a4, a5, a6, a7);
105 	return 0;
106 }
107 
108 uint32_t tee_svc_sys_nocall(void)
109 {
110 	DMSG("No syscall");
111 	return 0x1;
112 }
113 
114 TEE_Result tee_svc_sys_get_property(uint32_t prop, tee_uaddr_t buf, size_t blen)
115 {
116 	static const char api_vers[] = "1.0";
117 	static const char descr[] = "Version N.N";
118 	/*
119 	 * Value 100 means:
120 	 * System time based on REE-controlled timers. Can be tampered by the
121 	 * REE.  The implementation must still guarantee that the system time
122 	 * is monotonous, i.e., successive calls to TEE_GetSystemTime must
123 	 * return increasing values of the system time.
124 	 */
125 	static const uint32_t sys_time_prot_lvl = 100;
126 	static const uint32_t ta_time_prot_lvl = 100;
127 	struct tee_ta_session *sess;
128 	TEE_Result res;
129 
130 	res = tee_ta_get_current_session(&sess);
131 	if (res != TEE_SUCCESS)
132 		return res;
133 
134 	switch (prop) {
135 	case UTEE_PROP_TEE_API_VERSION:
136 		if (blen < sizeof(api_vers))
137 			return TEE_ERROR_SHORT_BUFFER;
138 		return tee_svc_copy_to_user(sess, (void *)buf, api_vers,
139 					    sizeof(api_vers));
140 
141 	case UTEE_PROP_TEE_DESCR:
142 		if (blen < sizeof(descr))
143 			return TEE_ERROR_SHORT_BUFFER;
144 		return tee_svc_copy_to_user(sess, (void *)buf, descr,
145 					    sizeof(descr));
146 
147 	case UTEE_PROP_TEE_DEV_ID:
148 		{
149 			TEE_UUID uuid;
150 			const size_t nslen = 4;
151 			uint8_t data[4 +
152 				     FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = {
153 			    'S', 'T', 'E', 'E' };
154 
155 			if (blen < sizeof(uuid))
156 				return TEE_ERROR_SHORT_BUFFER;
157 
158 			if (tee_otp_get_die_id
159 					(data + nslen, sizeof(data) - nslen))
160 				return TEE_ERROR_BAD_STATE;
161 
162 			res = tee_hash_createdigest(
163 					TEE_ALG_SHA256,
164 					data, sizeof(data),
165 					(uint8_t *)&uuid, sizeof(uuid));
166 			if (res != TEE_SUCCESS)
167 				return TEE_ERROR_BAD_STATE;
168 
169 			/*
170 			 * Changes the random value into and UUID as specifiec
171 			 * in RFC 4122. The magic values are from the example
172 			 * code in the RFC.
173 			 *
174 			 * TEE_UUID is defined slightly different from the RFC,
175 			 * but close enough for our purpose.
176 			 */
177 
178 			uuid.timeHiAndVersion &= 0x0fff;
179 			uuid.timeHiAndVersion |= 5 << 12;
180 
181 			/* uuid.clock_seq_hi_and_reserved in the RFC */
182 			uuid.clockSeqAndNode[0] &= 0x3f;
183 			uuid.clockSeqAndNode[0] |= 0x80;
184 
185 			return tee_svc_copy_to_user(sess, (void *)buf, &uuid,
186 						    sizeof(TEE_UUID));
187 		}
188 
189 	case UTEE_PROP_TEE_SYS_TIME_PROT_LEVEL:
190 		if (blen < sizeof(sys_time_prot_lvl))
191 			return TEE_ERROR_SHORT_BUFFER;
192 		return tee_svc_copy_to_user(sess, (void *)buf,
193 					    &sys_time_prot_lvl,
194 					    sizeof(sys_time_prot_lvl));
195 
196 	case UTEE_PROP_TEE_TA_TIME_PROT_LEVEL:
197 		if (blen < sizeof(ta_time_prot_lvl))
198 			return TEE_ERROR_SHORT_BUFFER;
199 		return tee_svc_copy_to_user(sess, (void *)buf,
200 					    &ta_time_prot_lvl,
201 					    sizeof(ta_time_prot_lvl));
202 
203 	case UTEE_PROP_CLIENT_ID:
204 		{
205 			if (blen < sizeof(TEE_Identity))
206 				return TEE_ERROR_SHORT_BUFFER;
207 
208 			return tee_svc_copy_to_user(sess, (void *)buf,
209 						    &sess->clnt_id,
210 						    sizeof(TEE_Identity));
211 		}
212 	case UTEE_PROP_TA_APP_ID:
213 		{
214 			if (blen < sizeof(TEE_UUID))
215 				return TEE_ERROR_SHORT_BUFFER;
216 
217 			return tee_svc_copy_to_user(sess, (void *)buf,
218 						    &sess->ctx->head->uuid,
219 						    sizeof(TEE_UUID));
220 		}
221 
222 	default:
223 		break;
224 	}
225 	return TEE_ERROR_NOT_IMPLEMENTED;
226 }
227 
228 /*
229  * TA invokes some TA with parameter.
230  * If some parameters are memory references:
231  * - either the memref is inside TA private RAM: TA is not allowed to expose
232  *   its private RAM: use a temporary memory buffer and copy the data.
233  * - or the memref is not in the TA private RAM:
234  *   - if the memref was mapped to the TA, TA is allowed to expose it.
235  *   - if so, converts memref virtual address into a physical address.
236  */
237 static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess,
238 				     struct tee_ta_session *called_sess,
239 				     uint32_t param_types,
240 				     TEE_Param params[TEE_NUM_PARAMS],
241 				     struct tee_ta_param *param,
242 				     tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
243 				     tee_mm_entry_t **mm)
244 {
245 	size_t n;
246 	TEE_Result res;
247 	size_t req_mem = 0;
248 	size_t s;
249 	uint8_t *dst = 0;
250 	tee_paddr_t dst_pa, src_pa = 0;
251 	bool ta_private_memref[TEE_NUM_PARAMS];
252 
253 	param->types = param_types;
254 	if (params == NULL) {
255 		if (param->types != 0)
256 			return TEE_ERROR_BAD_PARAMETERS;
257 		memset(param->params, 0, sizeof(param->params));
258 	} else {
259 		tee_svc_copy_from_user(sess, param->params, params,
260 				       sizeof(param->params));
261 	}
262 
263 	if ((called_sess != NULL) &&
264 		(called_sess->ctx->static_ta == NULL) &&
265 		(called_sess->ctx->flags & TA_FLAG_USER_MODE) == 0) {
266 		/*
267 		 * kernel TA, borrow the mapping of the calling
268 		 * during this call.
269 		 */
270 		called_sess->calling_sess = sess;
271 		return TEE_SUCCESS;
272 	}
273 
274 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
275 
276 		ta_private_memref[n] = false;
277 
278 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
279 		case TEE_PARAM_TYPE_MEMREF_INPUT:
280 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
281 		case TEE_PARAM_TYPE_MEMREF_INOUT:
282 			if (param->params[n].memref.buffer == NULL) {
283 				if (param->params[n].memref.size != 0)
284 					return TEE_ERROR_BAD_PARAMETERS;
285 				break;
286 			}
287 			/* uTA cannot expose its private memory */
288 			if (tee_mmu_is_vbuf_inside_ta_private(sess->ctx,
289 				    (uintptr_t)param->params[n].memref.buffer,
290 				    param->params[n].memref.size)) {
291 
292 				s = ROUNDUP(param->params[n].memref.size,
293 						sizeof(uint32_t));
294 				/* Check overflow */
295 				if (req_mem + s < req_mem)
296 					return TEE_ERROR_BAD_PARAMETERS;
297 				req_mem += s;
298 				ta_private_memref[n] = true;
299 				break;
300 			}
301 			if (!tee_mmu_is_vbuf_outside_ta_private(sess->ctx,
302 				    (uintptr_t)param->params[n].memref.buffer,
303 				    param->params[n].memref.size))
304 				return TEE_ERROR_BAD_PARAMETERS;
305 
306 			if (tee_mmu_user_va2pa(sess->ctx,
307 					(void *)param->params[n].memref.buffer,
308 					&src_pa) != TEE_SUCCESS)
309 				return TEE_ERROR_BAD_PARAMETERS;
310 
311 			param->param_attr[n] = tee_mmu_user_get_cache_attr(
312 				sess->ctx,
313 				(void *)param->params[n].memref.buffer);
314 
315 			param->params[n].memref.buffer = (void *)src_pa;
316 			break;
317 
318 		default:
319 			break;
320 		}
321 	}
322 
323 	if (req_mem == 0)
324 		return TEE_SUCCESS;
325 
326 	/* Allocate section in secure DDR */
327 	*mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem);
328 	if (*mm == NULL) {
329 		DMSG("tee_mm_alloc TEE_ERROR_GENERIC");
330 		return TEE_ERROR_GENERIC;
331 	}
332 
333 	/* Get the virtual address for the section in secure DDR */
334 	res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst);
335 	if (res != TEE_SUCCESS)
336 		return res;
337 	dst_pa = tee_mm_get_smem(*mm);
338 
339 	for (n = 0; n < 4; n++) {
340 
341 		if (ta_private_memref[n] == false)
342 			continue;
343 
344 		s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t));
345 
346 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
347 		case TEE_PARAM_TYPE_MEMREF_INPUT:
348 		case TEE_PARAM_TYPE_MEMREF_INOUT:
349 			if (param->params[n].memref.buffer != NULL) {
350 				res = tee_svc_copy_from_user(sess, dst,
351 							     param->params[n].
352 							     memref.buffer,
353 							     param->params[n].
354 							     memref.size);
355 				if (res != TEE_SUCCESS)
356 					return res;
357 
358 				param->param_attr[n] =
359 					tee_mmu_kmap_get_cache_attr(dst);
360 				param->params[n].memref.buffer = (void *)dst_pa;
361 				tmp_buf_pa[n] = dst_pa;
362 				dst += s;
363 				dst_pa += s;
364 			}
365 			break;
366 
367 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
368 			if (param->params[n].memref.buffer != NULL) {
369 				param->param_attr[n] =
370 					tee_mmu_kmap_get_cache_attr(dst);
371 				param->params[n].memref.buffer = (void *)dst_pa;
372 				tmp_buf_pa[n] = dst_pa;
373 				dst += s;
374 				dst_pa += s;
375 			}
376 			break;
377 
378 		default:
379 			continue;
380 		}
381 	}
382 
383 	tee_mmu_kunmap(dst, req_mem);
384 
385 	return TEE_SUCCESS;
386 }
387 
388 /*
389  * Back from execution of service: update parameters passed from TA:
390  * If some parameters were memory references:
391  * - either the memref was temporary: copy back data and update size
392  * - or it was the original TA memref: update only the size value.
393  */
394 static TEE_Result tee_svc_update_out_param(
395 		struct tee_ta_session *sess,
396 		struct tee_ta_session *called_sess,
397 		struct tee_ta_param *param,
398 		tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
399 		TEE_Param params[TEE_NUM_PARAMS])
400 {
401 	size_t n;
402 	bool have_private_mem_map = (called_sess == NULL) ||
403 		(called_sess->ctx->static_ta != NULL) ||
404 		((called_sess->ctx->flags & TA_FLAG_USER_MODE) != 0);
405 
406 	tee_ta_set_current_session(sess);
407 
408 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
409 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
410 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
411 		case TEE_PARAM_TYPE_MEMREF_INOUT:
412 
413 			/* outside TA private => memref is valid, update size */
414 			if (!tee_mmu_is_vbuf_inside_ta_private(sess->ctx,
415 					(uintptr_t)params[n].memref.buffer,
416 					param->params[n].memref.size)) {
417 				params[n].memref.size =
418 					param->params[n].memref.size;
419 				break;
420 			}
421 
422 			/*
423 			 * If we called a kernel TA the parameters are in shared
424 			 * memory and no copy is needed.
425 			 */
426 			if (have_private_mem_map &&
427 			    param->params[n].memref.size <=
428 			    params[n].memref.size) {
429 				uint8_t *src = 0;
430 				TEE_Result res;
431 
432 				/* FIXME: TA_RAM is already mapped ! */
433 				res = tee_mmu_kmap(tmp_buf_pa[n],
434 					param->params[n].memref.size, &src);
435 				if (res != TEE_SUCCESS)
436 					return TEE_ERROR_GENERIC;
437 
438 				res = tee_svc_copy_to_user(sess,
439 							 params[n].memref.
440 							 buffer, src,
441 							 param->params[n].
442 							 memref.size);
443 				if (res != TEE_SUCCESS)
444 					return res;
445 				tee_mmu_kunmap(src,
446 					       param->params[n].memref.size);
447 
448 			}
449 			params[n].memref.size = param->params[n].memref.size;
450 			break;
451 
452 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
453 		case TEE_PARAM_TYPE_VALUE_INOUT:
454 			params[n].value = param->params[n].value;
455 			break;
456 
457 		default:
458 			continue;
459 		}
460 	}
461 
462 	return TEE_SUCCESS;
463 }
464 
465 /* Called when a TA calls an OpenSession on another TA */
466 TEE_Result tee_svc_open_ta_session(const TEE_UUID *dest,
467 				   uint32_t cancel_req_to, uint32_t param_types,
468 				   TEE_Param params[4],
469 				   TEE_TASessionHandle *ta_sess,
470 				   uint32_t *ret_orig)
471 {
472 	TEE_Result res;
473 	uint32_t ret_o = TEE_ORIGIN_TEE;
474 	struct tee_ta_session *s = NULL;
475 	struct tee_ta_session *sess;
476 	tee_mm_entry_t *mm_param = NULL;
477 
478 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
479 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
480 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
481 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
482 
483 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
484 		res = TEE_ERROR_OUT_OF_MEMORY;
485 		goto out_free_only;
486 	}
487 
488 	memset(param, 0, sizeof(struct tee_ta_param));
489 
490 	res = tee_ta_get_current_session(&sess);
491 	if (res != TEE_SUCCESS)
492 		goto out_free_only;
493 
494 	res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID));
495 	if (res != TEE_SUCCESS)
496 		goto function_exit;
497 
498 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
499 	memcpy(&clnt_id->uuid, &sess->ctx->head->uuid, sizeof(TEE_UUID));
500 
501 	res = tee_svc_copy_param(sess, NULL, param_types, params, param,
502 				 tmp_buf_pa, &mm_param);
503 	if (res != TEE_SUCCESS)
504 		goto function_exit;
505 
506 	/*
507 	 * Find session of a multi session TA or a static TA
508 	 * In such a case, there is no need to ask the supplicant for the TA
509 	 * code
510 	 */
511 	res = tee_ta_open_session(&ret_o, &s, &sess->ctx->open_sessions, uuid,
512 				  clnt_id, cancel_req_to, param);
513 	if (res != TEE_SUCCESS)
514 		goto function_exit;
515 
516 	res = tee_svc_update_out_param(sess, NULL, param, tmp_buf_pa, params);
517 	if (res != TEE_SUCCESS)
518 		goto function_exit;
519 
520 function_exit:
521 	tee_ta_set_current_session(sess);
522 
523 	if (mm_param != NULL) {
524 		TEE_Result res2;
525 		void *va = 0;
526 
527 		res2 =
528 		    tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va);
529 		if (res2 == TEE_SUCCESS)
530 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
531 	}
532 	tee_mm_free(mm_param);
533 	tee_svc_copy_to_user(sess, ta_sess, &s, sizeof(s));
534 	tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
535 
536 out_free_only:
537 	free(param);
538 	free(uuid);
539 	free(clnt_id);
540 	return res;
541 }
542 
543 TEE_Result tee_svc_close_ta_session(TEE_TASessionHandle ta_sess)
544 {
545 	TEE_Result res;
546 	struct tee_ta_session *sess;
547 
548 	res = tee_ta_get_current_session(&sess);
549 	if (res != TEE_SUCCESS)
550 		return res;
551 
552 	tee_ta_set_current_session(NULL);
553 
554 	res =
555 	    tee_ta_close_session((uint32_t)ta_sess, &sess->ctx->open_sessions);
556 	tee_ta_set_current_session(sess);
557 	return res;
558 }
559 
560 TEE_Result tee_svc_invoke_ta_command(TEE_TASessionHandle ta_sess,
561 				     uint32_t cancel_req_to, uint32_t cmd_id,
562 				     uint32_t param_types, TEE_Param params[4],
563 				     uint32_t *ret_orig)
564 {
565 	TEE_Result res;
566 	uint32_t ret_o = TEE_ORIGIN_TEE;
567 	struct tee_ta_param param = { 0 };
568 	struct tee_ta_session *sess;
569 	struct tee_ta_session *called_sess = (struct tee_ta_session *)ta_sess;
570 	tee_mm_entry_t *mm_param = NULL;
571 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
572 
573 	res = tee_ta_get_current_session(&sess);
574 	if (res != TEE_SUCCESS)
575 		return res;
576 
577 	res =
578 	    tee_ta_verify_session_pointer(called_sess,
579 					  &sess->ctx->open_sessions);
580 	if (res != TEE_SUCCESS)
581 		return res;
582 
583 	res = tee_svc_copy_param(sess, called_sess, param_types, params,
584 				 &param, tmp_buf_pa, &mm_param);
585 	if (res != TEE_SUCCESS)
586 		goto function_exit;
587 
588 	res =
589 	    tee_ta_invoke_command(&ret_o, called_sess, cancel_req_to,
590 				  cmd_id, &param);
591 	if (res != TEE_SUCCESS)
592 		goto function_exit;
593 
594 	res = tee_svc_update_out_param(sess, called_sess, &param, tmp_buf_pa,
595 				       params);
596 	if (res != TEE_SUCCESS)
597 		goto function_exit;
598 
599 function_exit:
600 	tee_ta_set_current_session(sess);
601 	called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */
602 
603 	if (mm_param != NULL) {
604 		TEE_Result res2;
605 		void *va = 0;
606 
607 		res2 =
608 		    tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va);
609 		if (res2 == TEE_SUCCESS)
610 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
611 	}
612 	tee_mm_free(mm_param);
613 	if (ret_orig)
614 		tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
615 	return res;
616 }
617 
618 TEE_Result tee_svc_check_access_rights(uint32_t flags, const void *buf,
619 				       size_t len)
620 {
621 	TEE_Result res;
622 	struct tee_ta_session *s;
623 
624 	res = tee_ta_get_current_session(&s);
625 	if (res != TEE_SUCCESS)
626 		return res;
627 
628 	return tee_mmu_check_access_rights(s->ctx, flags, (tee_uaddr_t)buf,
629 					   len);
630 }
631 
632 TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr,
633 				  const void *uaddr, size_t len)
634 {
635 	TEE_Result res;
636 	struct tee_ta_session *s;
637 
638 	if (sess == NULL) {
639 		res = tee_ta_get_current_session(&s);
640 		if (res != TEE_SUCCESS)
641 			return res;
642 	} else {
643 		s = sess;
644 		tee_ta_set_current_session(s);
645 	}
646 	res =
647 	    tee_mmu_check_access_rights(s->ctx,
648 					TEE_MEMORY_ACCESS_READ |
649 					TEE_MEMORY_ACCESS_ANY_OWNER,
650 					(tee_uaddr_t)uaddr, len);
651 	if (res != TEE_SUCCESS)
652 		return res;
653 
654 	memcpy(kaddr, uaddr, len);
655 	return TEE_SUCCESS;
656 }
657 
658 TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr,
659 				const void *kaddr, size_t len)
660 {
661 	TEE_Result res;
662 	struct tee_ta_session *s;
663 
664 	if (sess == NULL) {
665 		res = tee_ta_get_current_session(&s);
666 		if (res != TEE_SUCCESS)
667 			return res;
668 	} else {
669 		s = sess;
670 		tee_ta_set_current_session(s);
671 	}
672 
673 	res =
674 	    tee_mmu_check_access_rights(s->ctx,
675 					TEE_MEMORY_ACCESS_WRITE |
676 					TEE_MEMORY_ACCESS_ANY_OWNER,
677 					(tee_uaddr_t)uaddr, len);
678 	if (res != TEE_SUCCESS)
679 		return res;
680 
681 	memcpy(uaddr, kaddr, len);
682 	return TEE_SUCCESS;
683 }
684 
685 static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time)
686 {
687 	TEE_Time current_time;
688 
689 	if (s->cancel_mask)
690 		return false;
691 
692 	if (s->cancel)
693 		return true;
694 
695 	if (s->cancel_time.seconds == UINT32_MAX)
696 		return false;
697 
698 	if (curr_time != NULL)
699 		current_time = *curr_time;
700 	else if (tee_time_get_sys_time(&current_time) != TEE_SUCCESS)
701 		return false;
702 
703 	if (current_time.seconds > s->cancel_time.seconds ||
704 	    (current_time.seconds == s->cancel_time.seconds &&
705 	     current_time.millis >= s->cancel_time.millis)) {
706 		return true;
707 	}
708 
709 	return false;
710 }
711 
712 TEE_Result tee_svc_get_cancellation_flag(bool *cancel)
713 {
714 	TEE_Result res;
715 	struct tee_ta_session *s = NULL;
716 	bool c;
717 
718 	res = tee_ta_get_current_session(&s);
719 	if (res != TEE_SUCCESS)
720 		return res;
721 
722 	c = session_is_cancelled(s, NULL);
723 
724 	return tee_svc_copy_to_user(s, cancel, &c, sizeof(c));
725 }
726 
727 TEE_Result tee_svc_unmask_cancellation(bool *old_mask)
728 {
729 	TEE_Result res;
730 	struct tee_ta_session *s = NULL;
731 	bool m;
732 
733 	res = tee_ta_get_current_session(&s);
734 	if (res != TEE_SUCCESS)
735 		return res;
736 
737 	m = s->cancel_mask;
738 	s->cancel_mask = false;
739 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
740 }
741 
742 TEE_Result tee_svc_mask_cancellation(bool *old_mask)
743 {
744 	TEE_Result res;
745 	struct tee_ta_session *s = NULL;
746 	bool m;
747 
748 	res = tee_ta_get_current_session(&s);
749 	if (res != TEE_SUCCESS)
750 		return res;
751 
752 	m = s->cancel_mask;
753 	s->cancel_mask = true;
754 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
755 }
756 
757 TEE_Result tee_svc_wait(uint32_t timeout)
758 {
759 	TEE_Result res = TEE_SUCCESS;
760 	uint32_t mytime = 0;
761 	struct tee_ta_session *s;
762 	TEE_Time base_time;
763 	TEE_Time current_time;
764 
765 	res = tee_ta_get_current_session(&s);
766 	if (res != TEE_SUCCESS)
767 		return res;
768 
769 	res = tee_time_get_sys_time(&base_time);
770 	if (res != TEE_SUCCESS)
771 		return res;
772 
773 	while (true) {
774 		res = tee_time_get_sys_time(&current_time);
775 		if (res != TEE_SUCCESS)
776 			return res;
777 
778 		if (session_is_cancelled(s, &current_time))
779 			return TEE_ERROR_CANCEL;
780 
781 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
782 		    (int)current_time.millis - (int)base_time.millis;
783 		if (mytime >= timeout)
784 			return TEE_SUCCESS;
785 
786 		tee_time_wait(timeout - mytime);
787 	}
788 
789 	return res;
790 }
791 
792 TEE_Result tee_svc_get_time(enum utee_time_category cat, TEE_Time *mytime)
793 {
794 	TEE_Result res, res2;
795 	struct tee_ta_session *s = NULL;
796 	TEE_Time t;
797 
798 	res = tee_ta_get_current_session(&s);
799 	if (res != TEE_SUCCESS)
800 		return res;
801 
802 	switch (cat) {
803 	case UTEE_TIME_CAT_SYSTEM:
804 		res = tee_time_get_sys_time(&t);
805 		break;
806 	case UTEE_TIME_CAT_TA_PERSISTENT:
807 		res =
808 		    tee_time_get_ta_time((const void *)&s->ctx->head->uuid, &t);
809 		break;
810 	case UTEE_TIME_CAT_REE:
811 		res = tee_time_get_ree_time(&t);
812 		break;
813 	default:
814 		res = TEE_ERROR_BAD_PARAMETERS;
815 		break;
816 	}
817 
818 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
819 		res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t));
820 		if (res2 != TEE_SUCCESS)
821 			res = res2;
822 	}
823 
824 	return res;
825 }
826 
827 TEE_Result tee_svc_set_ta_time(const TEE_Time *mytime)
828 {
829 	TEE_Result res;
830 	struct tee_ta_session *s = NULL;
831 	TEE_Time t;
832 
833 	res = tee_ta_get_current_session(&s);
834 	if (res != TEE_SUCCESS)
835 		return res;
836 
837 	res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t));
838 	if (res != TEE_SUCCESS)
839 		return res;
840 
841 	return tee_time_set_ta_time((const void *)&s->ctx->head->uuid, &t);
842 }
843