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