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