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