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