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