xref: /optee_os/core/kernel/tee_ta_manager.c (revision 1e24465fb6950ff4921f75f19dc8b94e78124bc2)
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 
28 #include <types_ext.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <arm.h>
34 #include <assert.h>
35 #include <kernel/mutex.h>
36 #include <kernel/panic.h>
37 #include <kernel/pseudo_ta.h>
38 #include <kernel/tee_common.h>
39 #include <kernel/tee_misc.h>
40 #include <kernel/tee_ta_manager.h>
41 #include <kernel/tee_time.h>
42 #include <kernel/thread.h>
43 #include <kernel/user_ta.h>
44 #include <mm/core_mmu.h>
45 #include <mm/core_memprot.h>
46 #include <mm/mobj.h>
47 #include <mm/tee_mmu.h>
48 #include <tee/tee_svc_cryp.h>
49 #include <tee/tee_obj.h>
50 #include <tee/tee_svc_storage.h>
51 #include <tee_api_types.h>
52 #include <trace.h>
53 #include <utee_types.h>
54 #include <util.h>
55 
56 /* This mutex protects the critical section in tee_ta_init_session */
57 struct mutex tee_ta_mutex = MUTEX_INITIALIZER;
58 struct tee_ta_ctx_head tee_ctxes = TAILQ_HEAD_INITIALIZER(tee_ctxes);
59 
60 #ifndef CFG_CONCURRENT_SINGLE_INSTANCE_TA
61 static struct condvar tee_ta_cv = CONDVAR_INITIALIZER;
62 static int tee_ta_single_instance_thread = THREAD_ID_INVALID;
63 static size_t tee_ta_single_instance_count;
64 #endif
65 
66 #ifdef CFG_CONCURRENT_SINGLE_INSTANCE_TA
67 static void lock_single_instance(void)
68 {
69 }
70 
71 static void unlock_single_instance(void)
72 {
73 }
74 
75 static bool has_single_instance_lock(void)
76 {
77 	return false;
78 }
79 #else
80 static void lock_single_instance(void)
81 {
82 	/* Requires tee_ta_mutex to be held */
83 	if (tee_ta_single_instance_thread != thread_get_id()) {
84 		/* Wait until the single-instance lock is available. */
85 		while (tee_ta_single_instance_thread != THREAD_ID_INVALID)
86 			condvar_wait(&tee_ta_cv, &tee_ta_mutex);
87 
88 		tee_ta_single_instance_thread = thread_get_id();
89 		assert(tee_ta_single_instance_count == 0);
90 	}
91 
92 	tee_ta_single_instance_count++;
93 }
94 
95 static void unlock_single_instance(void)
96 {
97 	/* Requires tee_ta_mutex to be held */
98 	assert(tee_ta_single_instance_thread == thread_get_id());
99 	assert(tee_ta_single_instance_count > 0);
100 
101 	tee_ta_single_instance_count--;
102 	if (tee_ta_single_instance_count == 0) {
103 		tee_ta_single_instance_thread = THREAD_ID_INVALID;
104 		condvar_signal(&tee_ta_cv);
105 	}
106 }
107 
108 static bool has_single_instance_lock(void)
109 {
110 	/* Requires tee_ta_mutex to be held */
111 	return tee_ta_single_instance_thread == thread_get_id();
112 }
113 #endif
114 
115 static bool tee_ta_try_set_busy(struct tee_ta_ctx *ctx)
116 {
117 	bool rc = true;
118 
119 	mutex_lock(&tee_ta_mutex);
120 
121 	if (ctx->flags & TA_FLAG_SINGLE_INSTANCE)
122 		lock_single_instance();
123 
124 	if (has_single_instance_lock()) {
125 		if (ctx->busy) {
126 			/*
127 			 * We're holding the single-instance lock and the
128 			 * TA is busy, as waiting now would only cause a
129 			 * dead-lock, we release the lock and return false.
130 			 */
131 			rc = false;
132 			if (ctx->flags & TA_FLAG_SINGLE_INSTANCE)
133 				unlock_single_instance();
134 		}
135 	} else {
136 		/*
137 		 * We're not holding the single-instance lock, we're free to
138 		 * wait for the TA to become available.
139 		 */
140 		while (ctx->busy)
141 			condvar_wait(&ctx->busy_cv, &tee_ta_mutex);
142 	}
143 
144 	/* Either it's already true or we should set it to true */
145 	ctx->busy = true;
146 
147 	mutex_unlock(&tee_ta_mutex);
148 	return rc;
149 }
150 
151 static void tee_ta_set_busy(struct tee_ta_ctx *ctx)
152 {
153 	if (!tee_ta_try_set_busy(ctx))
154 		panic();
155 }
156 
157 static void tee_ta_clear_busy(struct tee_ta_ctx *ctx)
158 {
159 	mutex_lock(&tee_ta_mutex);
160 
161 	assert(ctx->busy);
162 	ctx->busy = false;
163 	condvar_signal(&ctx->busy_cv);
164 
165 	if (ctx->flags & TA_FLAG_SINGLE_INSTANCE)
166 		unlock_single_instance();
167 
168 	mutex_unlock(&tee_ta_mutex);
169 }
170 
171 static void dec_session_ref_count(struct tee_ta_session *s)
172 {
173 	assert(s->ref_count > 0);
174 	s->ref_count--;
175 	if (s->ref_count == 1)
176 		condvar_signal(&s->refc_cv);
177 }
178 
179 void tee_ta_put_session(struct tee_ta_session *s)
180 {
181 	mutex_lock(&tee_ta_mutex);
182 
183 	if (s->lock_thread == thread_get_id()) {
184 		s->lock_thread = THREAD_ID_INVALID;
185 		condvar_signal(&s->lock_cv);
186 	}
187 	dec_session_ref_count(s);
188 
189 	mutex_unlock(&tee_ta_mutex);
190 }
191 
192 static struct tee_ta_session *find_session(uint32_t id,
193 			struct tee_ta_session_head *open_sessions)
194 {
195 	struct tee_ta_session *s;
196 
197 	TAILQ_FOREACH(s, open_sessions, link) {
198 		if ((vaddr_t)s == id)
199 			return s;
200 	}
201 	return NULL;
202 }
203 
204 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive,
205 			struct tee_ta_session_head *open_sessions)
206 {
207 	struct tee_ta_session *s;
208 
209 	mutex_lock(&tee_ta_mutex);
210 
211 	while (true) {
212 		s = find_session(id, open_sessions);
213 		if (!s)
214 			break;
215 		if (s->unlink) {
216 			s = NULL;
217 			break;
218 		}
219 		s->ref_count++;
220 		if (!exclusive)
221 			break;
222 
223 		assert(s->lock_thread != thread_get_id());
224 
225 		while (s->lock_thread != THREAD_ID_INVALID && !s->unlink)
226 			condvar_wait(&s->lock_cv, &tee_ta_mutex);
227 
228 		if (s->unlink) {
229 			dec_session_ref_count(s);
230 			s = NULL;
231 			break;
232 		}
233 
234 		s->lock_thread = thread_get_id();
235 		break;
236 	}
237 
238 	mutex_unlock(&tee_ta_mutex);
239 	return s;
240 }
241 
242 static void tee_ta_unlink_session(struct tee_ta_session *s,
243 			struct tee_ta_session_head *open_sessions)
244 {
245 	mutex_lock(&tee_ta_mutex);
246 
247 	assert(s->ref_count >= 1);
248 	assert(s->lock_thread == thread_get_id());
249 	assert(!s->unlink);
250 
251 	s->unlink = true;
252 	condvar_broadcast(&s->lock_cv);
253 
254 	while (s->ref_count != 1)
255 		condvar_wait(&s->refc_cv, &tee_ta_mutex);
256 
257 	TAILQ_REMOVE(open_sessions, s, link);
258 
259 	mutex_unlock(&tee_ta_mutex);
260 }
261 
262 /*
263  * tee_ta_context_find - Find TA in session list based on a UUID (input)
264  * Returns a pointer to the session
265  */
266 static struct tee_ta_ctx *tee_ta_context_find(const TEE_UUID *uuid)
267 {
268 	struct tee_ta_ctx *ctx;
269 
270 	TAILQ_FOREACH(ctx, &tee_ctxes, link) {
271 		if (memcmp(&ctx->uuid, uuid, sizeof(TEE_UUID)) == 0)
272 			return ctx;
273 	}
274 
275 	return NULL;
276 }
277 
278 /* check if requester (client ID) matches session initial client */
279 static TEE_Result check_client(struct tee_ta_session *s, const TEE_Identity *id)
280 {
281 	if (id == KERN_IDENTITY)
282 		return TEE_SUCCESS;
283 
284 	if (id == NSAPP_IDENTITY) {
285 		if (s->clnt_id.login == TEE_LOGIN_TRUSTED_APP) {
286 			DMSG("nsec tries to hijack TA session");
287 			return TEE_ERROR_ACCESS_DENIED;
288 		}
289 		return TEE_SUCCESS;
290 	}
291 
292 	if (memcmp(&s->clnt_id, id, sizeof(TEE_Identity)) != 0) {
293 		DMSG("client id mismatch");
294 		return TEE_ERROR_ACCESS_DENIED;
295 	}
296 	return TEE_SUCCESS;
297 }
298 
299 /*
300  * Check if invocation parameters matches TA properties
301  *
302  * @s - current session handle
303  * @param - already identified memory references hold a valid 'mobj'.
304  *
305  * Policy:
306  * - All TAs can access 'non-secure' shared memory.
307  * - All TAs can access TEE private memory (seccpy)
308  * - Only SDP flagged TAs can accept SDP memory references.
309  */
310 #ifndef CFG_SECURE_DATA_PATH
311 static bool check_params(struct tee_ta_session *sess __unused,
312 			 struct tee_ta_param *param __unused)
313 {
314 	/*
315 	 * When CFG_SECURE_DATA_PATH is not enabled, SDP memory references
316 	 * are rejected at OP-TEE core entry. Hence here all TAs have same
317 	 * permissions regarding memory reference parameters.
318 	 */
319 	return true;
320 }
321 #else
322 static bool check_params(struct tee_ta_session *sess,
323 			 struct tee_ta_param *param)
324 {
325 	int n;
326 
327 	/*
328 	 * When CFG_SECURE_DATA_PATH is enabled, OP-TEE entry allows SHM and
329 	 * SDP memory references. Only TAs flagged SDP can access SDP memory.
330 	 */
331 	if (sess->ctx->flags & TA_FLAG_SECURE_DATA_PATH)
332 		return true;
333 
334 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
335 		uint32_t param_type = TEE_PARAM_TYPE_GET(param->types, n);
336 		struct param_mem *mem = &param->u[n].mem;
337 
338 		if (param_type != TEE_PARAM_TYPE_MEMREF_INPUT &&
339 		    param_type != TEE_PARAM_TYPE_MEMREF_OUTPUT &&
340 		    param_type != TEE_PARAM_TYPE_MEMREF_INOUT)
341 			continue;
342 		if (!mem->size)
343 			continue;
344 		if (mobj_is_sdp_mem(mem->mobj))
345 			return false;
346 	}
347 	return true;
348 }
349 #endif
350 
351 static void set_invoke_timeout(struct tee_ta_session *sess,
352 				      uint32_t cancel_req_to)
353 {
354 	TEE_Time current_time;
355 	TEE_Time cancel_time;
356 
357 	if (cancel_req_to == TEE_TIMEOUT_INFINITE)
358 		goto infinite;
359 
360 	if (tee_time_get_sys_time(&current_time) != TEE_SUCCESS)
361 		goto infinite;
362 
363 	if (ADD_OVERFLOW(current_time.seconds, cancel_req_to / 1000,
364 			 &cancel_time.seconds))
365 		goto infinite;
366 
367 	cancel_time.millis = current_time.millis + cancel_req_to % 1000;
368 	if (cancel_time.millis > 1000) {
369 		if (ADD_OVERFLOW(current_time.seconds, 1,
370 				 &cancel_time.seconds))
371 			goto infinite;
372 
373 		cancel_time.seconds++;
374 		cancel_time.millis -= 1000;
375 	}
376 
377 	sess->cancel_time = cancel_time;
378 	return;
379 
380 infinite:
381 	sess->cancel_time.seconds = UINT32_MAX;
382 	sess->cancel_time.millis = UINT32_MAX;
383 }
384 
385 /*-----------------------------------------------------------------------------
386  * Close a Trusted Application and free available resources
387  *---------------------------------------------------------------------------*/
388 TEE_Result tee_ta_close_session(struct tee_ta_session *csess,
389 				struct tee_ta_session_head *open_sessions,
390 				const TEE_Identity *clnt_id)
391 {
392 	struct tee_ta_session *sess;
393 	struct tee_ta_ctx *ctx;
394 	bool keep_alive;
395 
396 	DMSG("tee_ta_close_session(0x%" PRIxVA ")",  (vaddr_t)csess);
397 
398 	if (!csess)
399 		return TEE_ERROR_ITEM_NOT_FOUND;
400 
401 	sess = tee_ta_get_session((vaddr_t)csess, true, open_sessions);
402 
403 	if (!sess) {
404 		EMSG("session 0x%" PRIxVA " to be removed is not found",
405 		     (vaddr_t)csess);
406 		return TEE_ERROR_ITEM_NOT_FOUND;
407 	}
408 
409 	if (check_client(sess, clnt_id) != TEE_SUCCESS) {
410 		tee_ta_put_session(sess);
411 		return TEE_ERROR_BAD_PARAMETERS; /* intentional generic error */
412 	}
413 
414 	ctx = sess->ctx;
415 	DMSG("Destroy session");
416 
417 	tee_ta_set_busy(ctx);
418 
419 	if (!ctx->panicked) {
420 		set_invoke_timeout(sess, TEE_TIMEOUT_INFINITE);
421 		ctx->ops->enter_close_session(sess);
422 	}
423 
424 	tee_ta_unlink_session(sess, open_sessions);
425 #if defined(CFG_TA_GPROF_SUPPORT)
426 	free(sess->sbuf);
427 #endif
428 	free(sess);
429 
430 	tee_ta_clear_busy(ctx);
431 
432 	mutex_lock(&tee_ta_mutex);
433 
434 	if (ctx->ref_count <= 0)
435 		panic();
436 
437 	ctx->ref_count--;
438 	keep_alive = (ctx->flags & TA_FLAG_INSTANCE_KEEP_ALIVE) &&
439 			(ctx->flags & TA_FLAG_SINGLE_INSTANCE);
440 	if (!ctx->ref_count && !keep_alive) {
441 		DMSG("Destroy TA ctx");
442 
443 		TAILQ_REMOVE(&tee_ctxes, ctx, link);
444 		mutex_unlock(&tee_ta_mutex);
445 
446 		condvar_destroy(&ctx->busy_cv);
447 
448 		pgt_flush_ctx(ctx);
449 		ctx->ops->destroy(ctx);
450 	} else
451 		mutex_unlock(&tee_ta_mutex);
452 
453 	return TEE_SUCCESS;
454 }
455 
456 static TEE_Result tee_ta_init_session_with_context(struct tee_ta_ctx *ctx,
457 			struct tee_ta_session *s)
458 {
459 	/*
460 	 * If TA isn't single instance it should be loaded as new
461 	 * instance instead of doing anything with this instance.
462 	 * So tell the caller that we didn't find the TA it the
463 	 * caller will load a new instance.
464 	 */
465 	if ((ctx->flags & TA_FLAG_SINGLE_INSTANCE) == 0)
466 		return TEE_ERROR_ITEM_NOT_FOUND;
467 
468 	/*
469 	 * The TA is single instance, if it isn't multi session we
470 	 * can't create another session unless it's the first
471 	 * new session towards a keepAlive TA.
472 	 */
473 
474 	if (((ctx->flags & TA_FLAG_MULTI_SESSION) == 0) &&
475 	    !(((ctx->flags & TA_FLAG_INSTANCE_KEEP_ALIVE) != 0) &&
476 	      (ctx->ref_count == 0)))
477 		return TEE_ERROR_BUSY;
478 
479 	DMSG("Re-open TA %pUl", (void *)&ctx->uuid);
480 
481 	ctx->ref_count++;
482 	s->ctx = ctx;
483 	return TEE_SUCCESS;
484 }
485 
486 
487 static TEE_Result tee_ta_init_session(TEE_ErrorOrigin *err,
488 				struct tee_ta_session_head *open_sessions,
489 				const TEE_UUID *uuid,
490 				struct tee_ta_session **sess)
491 {
492 	TEE_Result res;
493 	struct tee_ta_ctx *ctx;
494 	struct tee_ta_session *s = calloc(1, sizeof(struct tee_ta_session));
495 
496 	*err = TEE_ORIGIN_TEE;
497 	if (!s)
498 		return TEE_ERROR_OUT_OF_MEMORY;
499 
500 	s->cancel_mask = true;
501 	condvar_init(&s->refc_cv);
502 	condvar_init(&s->lock_cv);
503 	s->lock_thread = THREAD_ID_INVALID;
504 	s->ref_count = 1;
505 
506 
507 	/*
508 	 * We take the global TA mutex here and hold it while doing
509 	 * RPC to load the TA. This big critical section should be broken
510 	 * down into smaller pieces.
511 	 */
512 
513 
514 	mutex_lock(&tee_ta_mutex);
515 	TAILQ_INSERT_TAIL(open_sessions, s, link);
516 
517 	/* Look for already loaded TA */
518 	ctx = tee_ta_context_find(uuid);
519 	if (ctx) {
520 		res = tee_ta_init_session_with_context(ctx, s);
521 		if (res == TEE_SUCCESS || res != TEE_ERROR_ITEM_NOT_FOUND)
522 			goto out;
523 	}
524 
525 	/* Look for static TA */
526 	res = tee_ta_init_pseudo_ta_session(uuid, s);
527 	if (res == TEE_SUCCESS || res != TEE_ERROR_ITEM_NOT_FOUND)
528 		goto out;
529 
530 	/* Look for user TA */
531 	res = tee_ta_init_user_ta_session(uuid, s);
532 
533 out:
534 	if (res == TEE_SUCCESS) {
535 		*sess = s;
536 	} else {
537 		TAILQ_REMOVE(open_sessions, s, link);
538 		free(s);
539 	}
540 	mutex_unlock(&tee_ta_mutex);
541 	return res;
542 }
543 
544 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err,
545 			       struct tee_ta_session **sess,
546 			       struct tee_ta_session_head *open_sessions,
547 			       const TEE_UUID *uuid,
548 			       const TEE_Identity *clnt_id,
549 			       uint32_t cancel_req_to,
550 			       struct tee_ta_param *param)
551 {
552 	TEE_Result res;
553 	struct tee_ta_session *s = NULL;
554 	struct tee_ta_ctx *ctx;
555 	bool panicked;
556 	bool was_busy = false;
557 
558 	res = tee_ta_init_session(err, open_sessions, uuid, &s);
559 	if (res != TEE_SUCCESS) {
560 		DMSG("init session failed 0x%x", res);
561 		return res;
562 	}
563 
564 	if (!check_params(s, param))
565 		return TEE_ERROR_BAD_PARAMETERS;
566 
567 	ctx = s->ctx;
568 
569 	if (ctx->panicked) {
570 		DMSG("panicked, call tee_ta_close_session()");
571 		tee_ta_close_session(s, open_sessions, KERN_IDENTITY);
572 		*err = TEE_ORIGIN_TEE;
573 		return TEE_ERROR_TARGET_DEAD;
574 	}
575 
576 	*sess = s;
577 	/* Save identity of the owner of the session */
578 	s->clnt_id = *clnt_id;
579 
580 	if (tee_ta_try_set_busy(ctx)) {
581 		set_invoke_timeout(s, cancel_req_to);
582 		res = ctx->ops->enter_open_session(s, param, err);
583 		tee_ta_clear_busy(ctx);
584 	} else {
585 		/* Deadlock avoided */
586 		res = TEE_ERROR_BUSY;
587 		was_busy = true;
588 	}
589 
590 	panicked = ctx->panicked;
591 
592 	tee_ta_put_session(s);
593 	if (panicked || (res != TEE_SUCCESS))
594 		tee_ta_close_session(s, open_sessions, KERN_IDENTITY);
595 
596 	/*
597 	 * Origin error equal to TEE_ORIGIN_TRUSTED_APP for "regular" error,
598 	 * apart from panicking.
599 	 */
600 	if (panicked || was_busy)
601 		*err = TEE_ORIGIN_TEE;
602 	else
603 		*err = TEE_ORIGIN_TRUSTED_APP;
604 
605 	if (res != TEE_SUCCESS)
606 		EMSG("Failed. Return error 0x%x", res);
607 
608 	return res;
609 }
610 
611 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err,
612 				 struct tee_ta_session *sess,
613 				 const TEE_Identity *clnt_id,
614 				 uint32_t cancel_req_to, uint32_t cmd,
615 				 struct tee_ta_param *param)
616 {
617 	TEE_Result res;
618 
619 	if (check_client(sess, clnt_id) != TEE_SUCCESS)
620 		return TEE_ERROR_BAD_PARAMETERS; /* intentional generic error */
621 
622 	if (!check_params(sess, param))
623 		return TEE_ERROR_BAD_PARAMETERS;
624 
625 	if (sess->ctx->panicked) {
626 		DMSG("Panicked !");
627 		*err = TEE_ORIGIN_TEE;
628 		return TEE_ERROR_TARGET_DEAD;
629 	}
630 
631 	tee_ta_set_busy(sess->ctx);
632 
633 	set_invoke_timeout(sess, cancel_req_to);
634 	res = sess->ctx->ops->enter_invoke_cmd(sess, cmd, param, err);
635 
636 	if (sess->ctx->panicked) {
637 		*err = TEE_ORIGIN_TEE;
638 		res = TEE_ERROR_TARGET_DEAD;
639 	}
640 
641 	tee_ta_clear_busy(sess->ctx);
642 	if (res != TEE_SUCCESS)
643 		DMSG("Error: %x of %d\n", res, *err);
644 	return res;
645 }
646 
647 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err,
648 				 struct tee_ta_session *sess,
649 				 const TEE_Identity *clnt_id)
650 {
651 	*err = TEE_ORIGIN_TEE;
652 
653 	if (check_client(sess, clnt_id) != TEE_SUCCESS)
654 		return TEE_ERROR_BAD_PARAMETERS; /* intentional generic error */
655 
656 	sess->cancel = true;
657 	return TEE_SUCCESS;
658 }
659 
660 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time)
661 {
662 	TEE_Time current_time;
663 
664 	if (s->cancel_mask)
665 		return false;
666 
667 	if (s->cancel)
668 		return true;
669 
670 	if (s->cancel_time.seconds == UINT32_MAX)
671 		return false;
672 
673 	if (curr_time != NULL)
674 		current_time = *curr_time;
675 	else if (tee_time_get_sys_time(&current_time) != TEE_SUCCESS)
676 		return false;
677 
678 	if (current_time.seconds > s->cancel_time.seconds ||
679 	    (current_time.seconds == s->cancel_time.seconds &&
680 	     current_time.millis >= s->cancel_time.millis)) {
681 		return true;
682 	}
683 
684 	return false;
685 }
686 
687 static void update_current_ctx(struct thread_specific_data *tsd)
688 {
689 	struct tee_ta_ctx *ctx = NULL;
690 	struct tee_ta_session *s = TAILQ_FIRST(&tsd->sess_stack);
691 
692 	if (s) {
693 		if (is_pseudo_ta_ctx(s->ctx))
694 			s = TAILQ_NEXT(s, link_tsd);
695 
696 		if (s)
697 			ctx = s->ctx;
698 	}
699 
700 	if (tsd->ctx != ctx)
701 		tee_mmu_set_ctx(ctx);
702 	/*
703 	 * If ctx->mmu == NULL we must not have user mapping active,
704 	 * if ctx->mmu != NULL we must have user mapping active.
705 	 */
706 	if (((ctx && is_user_ta_ctx(ctx) ?
707 			to_user_ta_ctx(ctx)->mmu : NULL) == NULL) ==
708 					core_mmu_user_mapping_is_active())
709 		panic("unexpected active mapping");
710 }
711 
712 void tee_ta_push_current_session(struct tee_ta_session *sess)
713 {
714 	struct thread_specific_data *tsd = thread_get_tsd();
715 
716 	TAILQ_INSERT_HEAD(&tsd->sess_stack, sess, link_tsd);
717 	update_current_ctx(tsd);
718 }
719 
720 struct tee_ta_session *tee_ta_pop_current_session(void)
721 {
722 	struct thread_specific_data *tsd = thread_get_tsd();
723 	struct tee_ta_session *s = TAILQ_FIRST(&tsd->sess_stack);
724 
725 	if (s) {
726 		TAILQ_REMOVE(&tsd->sess_stack, s, link_tsd);
727 		update_current_ctx(tsd);
728 	}
729 	return s;
730 }
731 
732 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess)
733 {
734 	struct tee_ta_session *s = TAILQ_FIRST(&thread_get_tsd()->sess_stack);
735 
736 	if (!s)
737 		return TEE_ERROR_BAD_STATE;
738 	*sess = s;
739 	return TEE_SUCCESS;
740 }
741 
742 struct tee_ta_session *tee_ta_get_calling_session(void)
743 {
744 	struct tee_ta_session *s = TAILQ_FIRST(&thread_get_tsd()->sess_stack);
745 
746 	if (s)
747 		s = TAILQ_NEXT(s, link_tsd);
748 	return s;
749 }
750 
751 TEE_Result tee_ta_get_client_id(TEE_Identity *id)
752 {
753 	TEE_Result res;
754 	struct tee_ta_session *sess;
755 
756 	res = tee_ta_get_current_session(&sess);
757 	if (res != TEE_SUCCESS)
758 		return res;
759 
760 	if (id == NULL)
761 		return TEE_ERROR_BAD_PARAMETERS;
762 
763 	*id = sess->clnt_id;
764 	return TEE_SUCCESS;
765 }
766 
767 /*
768  * dump_state - Display TA state as an error log.
769  */
770 static void dump_state(struct tee_ta_ctx *ctx)
771 {
772 	struct tee_ta_session *s = NULL;
773 	bool active __maybe_unused;
774 
775 	active = ((tee_ta_get_current_session(&s) == TEE_SUCCESS) &&
776 		  s && s->ctx == ctx);
777 
778 	EMSG_RAW("Status of TA %pUl (%p) %s", (void *)&ctx->uuid, (void *)ctx,
779 		active ? "(active)" : "");
780 	ctx->ops->dump_state(ctx);
781 }
782 
783 void tee_ta_dump_current(void)
784 {
785 	struct tee_ta_session *s = NULL;
786 
787 	if (tee_ta_get_current_session(&s) != TEE_SUCCESS) {
788 		EMSG("no valid session found, cannot log TA status");
789 		return;
790 	}
791 
792 	dump_state(s->ctx);
793 }
794 
795 #if defined(CFG_TA_GPROF_SUPPORT)
796 void tee_ta_gprof_sample_pc(vaddr_t pc)
797 {
798 	struct tee_ta_session *s;
799 	struct sample_buf *sbuf;
800 	size_t idx;
801 
802 	if (tee_ta_get_current_session(&s) != TEE_SUCCESS)
803 		return;
804 	sbuf = s->sbuf;
805 	if (!sbuf || !sbuf->enabled)
806 		return; /* PC sampling is not enabled */
807 
808 	idx = (((uint64_t)pc - sbuf->offset)/2 * sbuf->scale)/65536;
809 	if (idx < sbuf->nsamples)
810 		sbuf->samples[idx]++;
811 	sbuf->count++;
812 }
813 
814 /*
815  * Update user-mode CPU time for the current session
816  * @suspend: true if session is being suspended (leaving user mode), false if
817  * it is resumed (entering user mode)
818  */
819 static void tee_ta_update_session_utime(bool suspend)
820 {
821 	struct tee_ta_session *s;
822 	struct sample_buf *sbuf;
823 	uint64_t now;
824 
825 	if (tee_ta_get_current_session(&s) != TEE_SUCCESS)
826 		return;
827 	sbuf = s->sbuf;
828 	if (!sbuf)
829 		return;
830 	now = read_cntpct();
831 	if (suspend) {
832 		assert(sbuf->usr_entered);
833 		sbuf->usr += now - sbuf->usr_entered;
834 		sbuf->usr_entered = 0;
835 	} else {
836 		assert(!sbuf->usr_entered);
837 		if (!now)
838 			now++; /* 0 is reserved */
839 		sbuf->usr_entered = now;
840 	}
841 }
842 
843 void tee_ta_update_session_utime_suspend(void)
844 {
845 	tee_ta_update_session_utime(true);
846 }
847 
848 void tee_ta_update_session_utime_resume(void)
849 {
850 	tee_ta_update_session_utime(false);
851 }
852 #endif
853