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