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