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