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 = ¶m->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(¤t_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 it's the first 477 * new session towards a keepAlive TA. 478 */ 479 480 if (((ctx->flags & TA_FLAG_MULTI_SESSION) == 0) && 481 !(((ctx->flags & TA_FLAG_INSTANCE_KEEP_ALIVE) != 0) && 482 (ctx->ref_count == 0))) 483 return TEE_ERROR_BUSY; 484 485 DMSG("Re-open TA %pUl", (void *)&ctx->uuid); 486 487 ctx->ref_count++; 488 s->ctx = ctx; 489 return TEE_SUCCESS; 490 } 491 492 493 static TEE_Result tee_ta_init_session(TEE_ErrorOrigin *err, 494 struct tee_ta_session_head *open_sessions, 495 const TEE_UUID *uuid, 496 struct tee_ta_session **sess) 497 { 498 TEE_Result res; 499 struct tee_ta_ctx *ctx; 500 struct tee_ta_session *s = calloc(1, sizeof(struct tee_ta_session)); 501 502 *err = TEE_ORIGIN_TEE; 503 if (!s) 504 return TEE_ERROR_OUT_OF_MEMORY; 505 506 s->cancel_mask = true; 507 condvar_init(&s->refc_cv); 508 condvar_init(&s->lock_cv); 509 s->lock_thread = THREAD_ID_INVALID; 510 s->ref_count = 1; 511 512 513 /* 514 * We take the global TA mutex here and hold it while doing 515 * RPC to load the TA. This big critical section should be broken 516 * down into smaller pieces. 517 */ 518 519 520 mutex_lock(&tee_ta_mutex); 521 TAILQ_INSERT_TAIL(open_sessions, s, link); 522 523 /* Look for already loaded TA */ 524 ctx = tee_ta_context_find(uuid); 525 if (ctx) { 526 res = tee_ta_init_session_with_context(ctx, s); 527 if (res == TEE_SUCCESS || res != TEE_ERROR_ITEM_NOT_FOUND) 528 goto out; 529 } 530 531 /* Look for static TA */ 532 res = tee_ta_init_pseudo_ta_session(uuid, s); 533 if (res == TEE_SUCCESS || res != TEE_ERROR_ITEM_NOT_FOUND) 534 goto out; 535 536 /* Look for user TA */ 537 res = tee_ta_init_user_ta_session(uuid, s); 538 539 out: 540 if (res == TEE_SUCCESS) { 541 *sess = s; 542 } else { 543 TAILQ_REMOVE(open_sessions, s, link); 544 free(s); 545 } 546 mutex_unlock(&tee_ta_mutex); 547 return res; 548 } 549 550 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err, 551 struct tee_ta_session **sess, 552 struct tee_ta_session_head *open_sessions, 553 const TEE_UUID *uuid, 554 const TEE_Identity *clnt_id, 555 uint32_t cancel_req_to, 556 struct tee_ta_param *param) 557 { 558 TEE_Result res; 559 struct tee_ta_session *s = NULL; 560 struct tee_ta_ctx *ctx; 561 bool panicked; 562 bool was_busy = false; 563 564 res = tee_ta_init_session(err, open_sessions, uuid, &s); 565 if (res != TEE_SUCCESS) { 566 DMSG("init session failed 0x%x", res); 567 return res; 568 } 569 570 if (!check_params(s, param)) 571 return TEE_ERROR_BAD_PARAMETERS; 572 573 ctx = s->ctx; 574 575 if (ctx->panicked) { 576 DMSG("panicked, call tee_ta_close_session()"); 577 tee_ta_close_session(s, open_sessions, KERN_IDENTITY); 578 *err = TEE_ORIGIN_TEE; 579 return TEE_ERROR_TARGET_DEAD; 580 } 581 582 *sess = s; 583 /* Save identity of the owner of the session */ 584 s->clnt_id = *clnt_id; 585 586 if (tee_ta_try_set_busy(ctx)) { 587 set_invoke_timeout(s, cancel_req_to); 588 res = ctx->ops->enter_open_session(s, param, err); 589 tee_ta_clear_busy(ctx); 590 } else { 591 /* Deadlock avoided */ 592 res = TEE_ERROR_BUSY; 593 was_busy = true; 594 } 595 596 panicked = ctx->panicked; 597 598 tee_ta_put_session(s); 599 if (panicked || (res != TEE_SUCCESS)) 600 tee_ta_close_session(s, open_sessions, KERN_IDENTITY); 601 602 /* 603 * Origin error equal to TEE_ORIGIN_TRUSTED_APP for "regular" error, 604 * apart from panicking. 605 */ 606 if (panicked || was_busy) 607 *err = TEE_ORIGIN_TEE; 608 else 609 *err = TEE_ORIGIN_TRUSTED_APP; 610 611 if (res != TEE_SUCCESS) 612 EMSG("Failed. Return error 0x%x", res); 613 614 return res; 615 } 616 617 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err, 618 struct tee_ta_session *sess, 619 const TEE_Identity *clnt_id, 620 uint32_t cancel_req_to, uint32_t cmd, 621 struct tee_ta_param *param) 622 { 623 TEE_Result res; 624 625 if (check_client(sess, clnt_id) != TEE_SUCCESS) 626 return TEE_ERROR_BAD_PARAMETERS; /* intentional generic error */ 627 628 if (!check_params(sess, param)) 629 return TEE_ERROR_BAD_PARAMETERS; 630 631 if (sess->ctx->panicked) { 632 DMSG("Panicked !"); 633 *err = TEE_ORIGIN_TEE; 634 return TEE_ERROR_TARGET_DEAD; 635 } 636 637 tee_ta_set_busy(sess->ctx); 638 639 set_invoke_timeout(sess, cancel_req_to); 640 res = sess->ctx->ops->enter_invoke_cmd(sess, cmd, param, err); 641 642 if (sess->ctx->panicked) { 643 *err = TEE_ORIGIN_TEE; 644 res = TEE_ERROR_TARGET_DEAD; 645 } 646 647 tee_ta_clear_busy(sess->ctx); 648 if (res != TEE_SUCCESS) 649 DMSG("Error: %x of %d\n", res, *err); 650 return res; 651 } 652 653 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err, 654 struct tee_ta_session *sess, 655 const TEE_Identity *clnt_id) 656 { 657 *err = TEE_ORIGIN_TEE; 658 659 if (check_client(sess, clnt_id) != TEE_SUCCESS) 660 return TEE_ERROR_BAD_PARAMETERS; /* intentional generic error */ 661 662 sess->cancel = true; 663 return TEE_SUCCESS; 664 } 665 666 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time) 667 { 668 TEE_Time current_time; 669 670 if (s->cancel_mask) 671 return false; 672 673 if (s->cancel) 674 return true; 675 676 if (s->cancel_time.seconds == UINT32_MAX) 677 return false; 678 679 if (curr_time != NULL) 680 current_time = *curr_time; 681 else if (tee_time_get_sys_time(¤t_time) != TEE_SUCCESS) 682 return false; 683 684 if (current_time.seconds > s->cancel_time.seconds || 685 (current_time.seconds == s->cancel_time.seconds && 686 current_time.millis >= s->cancel_time.millis)) { 687 return true; 688 } 689 690 return false; 691 } 692 693 static void update_current_ctx(struct thread_specific_data *tsd) 694 { 695 struct tee_ta_ctx *ctx = NULL; 696 struct tee_ta_session *s = TAILQ_FIRST(&tsd->sess_stack); 697 698 if (s) { 699 if (is_pseudo_ta_ctx(s->ctx)) 700 s = TAILQ_NEXT(s, link_tsd); 701 702 if (s) 703 ctx = s->ctx; 704 } 705 706 if (tsd->ctx != ctx) 707 tee_mmu_set_ctx(ctx); 708 /* 709 * If ctx->mmu == NULL we must not have user mapping active, 710 * if ctx->mmu != NULL we must have user mapping active. 711 */ 712 if (((ctx && is_user_ta_ctx(ctx) ? 713 to_user_ta_ctx(ctx)->mmu : NULL) == NULL) == 714 core_mmu_user_mapping_is_active()) 715 panic("unexpected active mapping"); 716 } 717 718 void tee_ta_push_current_session(struct tee_ta_session *sess) 719 { 720 struct thread_specific_data *tsd = thread_get_tsd(); 721 722 TAILQ_INSERT_HEAD(&tsd->sess_stack, sess, link_tsd); 723 update_current_ctx(tsd); 724 } 725 726 struct tee_ta_session *tee_ta_pop_current_session(void) 727 { 728 struct thread_specific_data *tsd = thread_get_tsd(); 729 struct tee_ta_session *s = TAILQ_FIRST(&tsd->sess_stack); 730 731 if (s) { 732 TAILQ_REMOVE(&tsd->sess_stack, s, link_tsd); 733 update_current_ctx(tsd); 734 } 735 return s; 736 } 737 738 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess) 739 { 740 struct tee_ta_session *s = TAILQ_FIRST(&thread_get_tsd()->sess_stack); 741 742 if (!s) 743 return TEE_ERROR_BAD_STATE; 744 *sess = s; 745 return TEE_SUCCESS; 746 } 747 748 struct tee_ta_session *tee_ta_get_calling_session(void) 749 { 750 struct tee_ta_session *s = TAILQ_FIRST(&thread_get_tsd()->sess_stack); 751 752 if (s) 753 s = TAILQ_NEXT(s, link_tsd); 754 return s; 755 } 756 757 TEE_Result tee_ta_get_client_id(TEE_Identity *id) 758 { 759 TEE_Result res; 760 struct tee_ta_session *sess; 761 762 res = tee_ta_get_current_session(&sess); 763 if (res != TEE_SUCCESS) 764 return res; 765 766 if (id == NULL) 767 return TEE_ERROR_BAD_PARAMETERS; 768 769 *id = sess->clnt_id; 770 return TEE_SUCCESS; 771 } 772 773 /* 774 * dump_state - Display TA state as an error log. 775 */ 776 static void dump_state(struct tee_ta_ctx *ctx) 777 { 778 struct tee_ta_session *s = NULL; 779 bool active __maybe_unused; 780 781 active = ((tee_ta_get_current_session(&s) == TEE_SUCCESS) && 782 s && s->ctx == ctx); 783 784 EMSG_RAW("Status of TA %pUl (%p) %s", (void *)&ctx->uuid, (void *)ctx, 785 active ? "(active)" : ""); 786 ctx->ops->dump_state(ctx); 787 } 788 789 void tee_ta_dump_current(void) 790 { 791 struct tee_ta_session *s = NULL; 792 793 if (tee_ta_get_current_session(&s) != TEE_SUCCESS) { 794 EMSG("no valid session found, cannot log TA status"); 795 return; 796 } 797 798 dump_state(s->ctx); 799 } 800 801 #if defined(CFG_TA_GPROF_SUPPORT) 802 void tee_ta_gprof_sample_pc(vaddr_t pc) 803 { 804 struct tee_ta_session *s; 805 struct sample_buf *sbuf; 806 size_t idx; 807 808 if (tee_ta_get_current_session(&s) != TEE_SUCCESS) 809 return; 810 sbuf = s->sbuf; 811 if (!sbuf || !sbuf->enabled) 812 return; /* PC sampling is not enabled */ 813 814 idx = (((uint64_t)pc - sbuf->offset)/2 * sbuf->scale)/65536; 815 if (idx < sbuf->nsamples) 816 sbuf->samples[idx]++; 817 sbuf->count++; 818 } 819 820 /* 821 * Update user-mode CPU time for the current session 822 * @suspend: true if session is being suspended (leaving user mode), false if 823 * it is resumed (entering user mode) 824 */ 825 static void tee_ta_update_session_utime(bool suspend) 826 { 827 struct tee_ta_session *s; 828 struct sample_buf *sbuf; 829 uint64_t now; 830 831 if (tee_ta_get_current_session(&s) != TEE_SUCCESS) 832 return; 833 sbuf = s->sbuf; 834 if (!sbuf) 835 return; 836 now = read_cntpct(); 837 if (suspend) { 838 assert(sbuf->usr_entered); 839 sbuf->usr += now - sbuf->usr_entered; 840 sbuf->usr_entered = 0; 841 } else { 842 assert(!sbuf->usr_entered); 843 if (!now) 844 now++; /* 0 is reserved */ 845 sbuf->usr_entered = now; 846 } 847 } 848 849 void tee_ta_update_session_utime_suspend(void) 850 { 851 tee_ta_update_session_utime(true); 852 } 853 854 void tee_ta_update_session_utime_resume(void) 855 { 856 tee_ta_update_session_utime(false); 857 } 858 #endif 859