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