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