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 sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 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 if (res == TEE_SUCCESS) 588 tee_svc_copy_kaddr_to_uref(sess, ta_sess, s); 589 tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 590 591 out_free_only: 592 free(param); 593 free(uuid); 594 free(clnt_id); 595 return res; 596 } 597 598 TEE_Result syscall_close_ta_session(unsigned long ta_sess) 599 { 600 TEE_Result res; 601 struct tee_ta_session *sess; 602 TEE_Identity clnt_id; 603 struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess); 604 605 res = tee_ta_get_current_session(&sess); 606 if (res != TEE_SUCCESS) 607 return res; 608 609 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 610 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 611 612 tee_ta_set_current_session(NULL); 613 res = tee_ta_close_session(s, &sess->ctx->open_sessions, &clnt_id); 614 tee_ta_set_current_session(sess); 615 return res; 616 } 617 618 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess, 619 unsigned long cancel_req_to, unsigned long cmd_id, 620 struct utee_params *usr_param, uint32_t *ret_orig) 621 { 622 TEE_Result res; 623 uint32_t ret_o = TEE_ORIGIN_TEE; 624 struct tee_ta_param param = { 0 }; 625 TEE_Identity clnt_id; 626 struct tee_ta_session *sess; 627 struct tee_ta_session *called_sess; 628 tee_mm_entry_t *mm_param = NULL; 629 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 630 631 res = tee_ta_get_current_session(&sess); 632 if (res != TEE_SUCCESS) 633 return res; 634 635 called_sess = tee_ta_get_session( 636 (vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true, 637 &sess->ctx->open_sessions); 638 if (!called_sess) 639 return TEE_ERROR_BAD_PARAMETERS; 640 641 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 642 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 643 644 res = tee_svc_copy_param(sess, called_sess, usr_param, ¶m, 645 tmp_buf_pa, &mm_param); 646 if (res != TEE_SUCCESS) 647 goto function_exit; 648 649 res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id, 650 cancel_req_to, cmd_id, ¶m); 651 652 if (res != TEE_SUCCESS) 653 goto function_exit; 654 655 res = tee_svc_update_out_param(sess, called_sess, ¶m, tmp_buf_pa, 656 usr_param); 657 if (res != TEE_SUCCESS) 658 goto function_exit; 659 660 function_exit: 661 tee_ta_set_current_session(sess); 662 called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 663 tee_ta_put_session(called_sess); 664 665 if (mm_param != NULL) { 666 TEE_Result res2; 667 void *va = 0; 668 669 res2 = 670 tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va); 671 if (res2 == TEE_SUCCESS) 672 tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 673 } 674 tee_mm_free(mm_param); 675 if (ret_orig) 676 tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 677 return res; 678 } 679 680 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf, 681 size_t len) 682 { 683 TEE_Result res; 684 struct tee_ta_session *s; 685 686 res = tee_ta_get_current_session(&s); 687 if (res != TEE_SUCCESS) 688 return res; 689 690 return tee_mmu_check_access_rights(s->ctx, flags, (tee_uaddr_t)buf, 691 len); 692 } 693 694 TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr, 695 const void *uaddr, size_t len) 696 { 697 TEE_Result res; 698 struct tee_ta_session *s; 699 700 if (sess == NULL) { 701 res = tee_ta_get_current_session(&s); 702 if (res != TEE_SUCCESS) 703 return res; 704 } else { 705 s = sess; 706 tee_ta_set_current_session(s); 707 } 708 res = 709 tee_mmu_check_access_rights(s->ctx, 710 TEE_MEMORY_ACCESS_READ | 711 TEE_MEMORY_ACCESS_ANY_OWNER, 712 (tee_uaddr_t)uaddr, len); 713 if (res != TEE_SUCCESS) 714 return res; 715 716 memcpy(kaddr, uaddr, len); 717 return TEE_SUCCESS; 718 } 719 720 TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr, 721 const void *kaddr, size_t len) 722 { 723 TEE_Result res; 724 struct tee_ta_session *s; 725 726 if (sess == NULL) { 727 res = tee_ta_get_current_session(&s); 728 if (res != TEE_SUCCESS) 729 return res; 730 } else { 731 s = sess; 732 tee_ta_set_current_session(s); 733 } 734 735 res = 736 tee_mmu_check_access_rights(s->ctx, 737 TEE_MEMORY_ACCESS_WRITE | 738 TEE_MEMORY_ACCESS_ANY_OWNER, 739 (tee_uaddr_t)uaddr, len); 740 if (res != TEE_SUCCESS) 741 return res; 742 743 memcpy(uaddr, kaddr, len); 744 return TEE_SUCCESS; 745 } 746 747 TEE_Result tee_svc_copy_kaddr_to_uref(struct tee_ta_session *sess, 748 uint32_t *uref, void *kaddr) 749 { 750 uint32_t ref = tee_svc_kaddr_to_uref(kaddr); 751 752 return tee_svc_copy_to_user(sess, uref, &ref, sizeof(ref)); 753 } 754 755 static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time) 756 { 757 TEE_Time current_time; 758 759 if (s->cancel_mask) 760 return false; 761 762 if (s->cancel) 763 return true; 764 765 if (s->cancel_time.seconds == UINT32_MAX) 766 return false; 767 768 if (curr_time != NULL) 769 current_time = *curr_time; 770 else if (tee_time_get_sys_time(¤t_time) != TEE_SUCCESS) 771 return false; 772 773 if (current_time.seconds > s->cancel_time.seconds || 774 (current_time.seconds == s->cancel_time.seconds && 775 current_time.millis >= s->cancel_time.millis)) { 776 return true; 777 } 778 779 return false; 780 } 781 782 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel) 783 { 784 TEE_Result res; 785 struct tee_ta_session *s = NULL; 786 uint32_t c; 787 788 res = tee_ta_get_current_session(&s); 789 if (res != TEE_SUCCESS) 790 return res; 791 792 c = session_is_cancelled(s, NULL); 793 794 return tee_svc_copy_to_user(s, cancel, &c, sizeof(c)); 795 } 796 797 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask) 798 { 799 TEE_Result res; 800 struct tee_ta_session *s = NULL; 801 uint32_t m; 802 803 res = tee_ta_get_current_session(&s); 804 if (res != TEE_SUCCESS) 805 return res; 806 807 m = s->cancel_mask; 808 s->cancel_mask = false; 809 return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 810 } 811 812 TEE_Result syscall_mask_cancellation(uint32_t *old_mask) 813 { 814 TEE_Result res; 815 struct tee_ta_session *s = NULL; 816 uint32_t m; 817 818 res = tee_ta_get_current_session(&s); 819 if (res != TEE_SUCCESS) 820 return res; 821 822 m = s->cancel_mask; 823 s->cancel_mask = true; 824 return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 825 } 826 827 TEE_Result syscall_wait(unsigned long timeout) 828 { 829 TEE_Result res = TEE_SUCCESS; 830 uint32_t mytime = 0; 831 struct tee_ta_session *s; 832 TEE_Time base_time; 833 TEE_Time current_time; 834 835 res = tee_ta_get_current_session(&s); 836 if (res != TEE_SUCCESS) 837 return res; 838 839 res = tee_time_get_sys_time(&base_time); 840 if (res != TEE_SUCCESS) 841 return res; 842 843 while (true) { 844 res = tee_time_get_sys_time(¤t_time); 845 if (res != TEE_SUCCESS) 846 return res; 847 848 if (session_is_cancelled(s, ¤t_time)) 849 return TEE_ERROR_CANCEL; 850 851 mytime = (current_time.seconds - base_time.seconds) * 1000 + 852 (int)current_time.millis - (int)base_time.millis; 853 if (mytime >= timeout) 854 return TEE_SUCCESS; 855 856 tee_time_wait(timeout - mytime); 857 } 858 859 return res; 860 } 861 862 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime) 863 { 864 TEE_Result res, res2; 865 struct tee_ta_session *s = NULL; 866 TEE_Time t; 867 868 res = tee_ta_get_current_session(&s); 869 if (res != TEE_SUCCESS) 870 return res; 871 872 switch (cat) { 873 case UTEE_TIME_CAT_SYSTEM: 874 res = tee_time_get_sys_time(&t); 875 break; 876 case UTEE_TIME_CAT_TA_PERSISTENT: 877 res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t); 878 break; 879 case UTEE_TIME_CAT_REE: 880 res = tee_time_get_ree_time(&t); 881 break; 882 default: 883 res = TEE_ERROR_BAD_PARAMETERS; 884 break; 885 } 886 887 if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 888 res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t)); 889 if (res2 != TEE_SUCCESS) 890 res = res2; 891 } 892 893 return res; 894 } 895 896 TEE_Result syscall_set_ta_time(const TEE_Time *mytime) 897 { 898 TEE_Result res; 899 struct tee_ta_session *s = NULL; 900 TEE_Time t; 901 902 res = tee_ta_get_current_session(&s); 903 if (res != TEE_SUCCESS) 904 return res; 905 906 res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t)); 907 if (res != TEE_SUCCESS) 908 return res; 909 910 return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t); 911 } 912 913 #ifdef CFG_CACHE_API 914 TEE_Result syscall_cache_operation(void *va, size_t len, unsigned long op) 915 { 916 TEE_Result res; 917 struct tee_ta_session *s = NULL; 918 919 res = tee_ta_get_current_session(&s); 920 if (res != TEE_SUCCESS) 921 return res; 922 923 if ((s->ctx->flags & TA_FLAG_CACHE_MAINTENANCE) == 0) 924 return TEE_ERROR_NOT_SUPPORTED; 925 926 return tee_uta_cache_operation(s, op, va, len); 927 } 928 #endif 929