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 #include <kernel/static_ta.h> 46 47 #include <assert.h> 48 49 vaddr_t tee_svc_uref_base; 50 51 void syscall_log(const void *buf __unused, size_t len __unused) 52 { 53 #ifdef CFG_TEE_CORE_TA_TRACE 54 char *kbuf; 55 56 if (len == 0) 57 return; 58 59 kbuf = malloc(len); 60 if (kbuf == NULL) 61 return; 62 *kbuf = '\0'; 63 64 /* log as Info/Raw traces */ 65 if (tee_svc_copy_from_user(NULL, kbuf, buf, len) == TEE_SUCCESS) 66 TAMSG_RAW("%.*s", (int)len, kbuf); 67 68 free(kbuf); 69 #endif 70 } 71 72 TEE_Result syscall_not_supported(void) 73 { 74 return TEE_ERROR_NOT_SUPPORTED; 75 } 76 77 uint32_t syscall_dummy(uint32_t *a __unused) 78 { 79 DMSG("tee_svc_sys_dummy: a 0x%" PRIxVA, (vaddr_t)a); 80 return 0; 81 } 82 83 uint32_t syscall_dummy_7args(unsigned long a1 __unused, 84 unsigned long a2 __unused, unsigned long a3 __unused, 85 unsigned long a4 __unused, unsigned long a5 __unused, 86 unsigned long a6 __unused, unsigned long a7 __unused) 87 { 88 DMSG("tee_svc_sys_dummy_7args: 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, %lx, %lx\n", 89 a1, a2, a3, a4, a5, a6, a7); 90 return 0; 91 } 92 93 uint32_t syscall_nocall(void) 94 { 95 DMSG("No syscall"); 96 return 0x1; 97 } 98 99 /* Configuration properties */ 100 /* API implementation version */ 101 static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION); 102 103 /* Implementation description (implementation-dependent) */ 104 static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR); 105 106 /* 107 * TA persistent time protection level 108 * 100: Persistent time based on an REE-controlled real-time clock 109 * and on the TEE Trusted Storage for the storage of origins (default). 110 * 1000: Persistent time based on a TEE-controlled real-time clock 111 * and the TEE Trusted Storage. 112 * The real-time clock MUST be out of reach of software attacks 113 * from the REE. 114 */ 115 static const uint32_t ta_time_prot_lvl = 100; 116 117 /* Elliptic Curve Cryptographic support (false by default) */ 118 static const bool crypto_ecc_en; 119 120 /* 121 * Trusted storage anti rollback protection level 122 * 0 (or missing): No antirollback protection (default) 123 * 100: Antirollback enforced at REE level 124 * 1000: Antirollback TEE-controlled hardware 125 */ 126 static const uint32_t ts_antiroll_prot_lvl; 127 128 /* Trusted OS implementation version */ 129 static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION); 130 131 /* Trusted OS implementation version (binary value) */ 132 static const uint32_t trustedos_impl_bin_version; /* 0 by default */ 133 134 /* Trusted OS implementation manufacturer name */ 135 static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER); 136 137 /* Trusted firmware version */ 138 static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION); 139 140 /* Trusted firmware version (binary value) */ 141 static const uint32_t fw_impl_bin_version; /* 0 by default */ 142 143 /* Trusted firmware manufacturer name */ 144 static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER); 145 146 struct tee_props { 147 const void *data; 148 const size_t len; 149 }; 150 151 /* Consistent with enum utee_property */ 152 const struct tee_props tee_props_lut[] = { 153 {api_vers, sizeof(api_vers)}, 154 {descr, sizeof(descr)}, 155 {0, 0}, /* dev_id */ 156 {0, 0}, /* system time protection level */ 157 {&ta_time_prot_lvl, sizeof(ta_time_prot_lvl)}, 158 {&crypto_ecc_en, sizeof(crypto_ecc_en)}, 159 {&ts_antiroll_prot_lvl, sizeof(ts_antiroll_prot_lvl)}, 160 {trustedos_impl_version, sizeof(trustedos_impl_version)}, 161 {&trustedos_impl_bin_version, 162 sizeof(trustedos_impl_bin_version)}, 163 {trustedos_manufacturer, sizeof(trustedos_manufacturer)}, 164 {fw_impl_version, sizeof(fw_impl_version)}, 165 {&fw_impl_bin_version, sizeof(fw_impl_bin_version)}, 166 {fw_manufacturer, sizeof(fw_manufacturer)}, 167 {0, 0}, /* client_id */ 168 {0, 0}, /* ta_app_id */ 169 }; 170 171 TEE_Result syscall_get_property(unsigned long prop, void *buf, size_t blen) 172 { 173 struct tee_ta_session *sess; 174 TEE_Result res; 175 176 if (prop > ARRAY_SIZE(tee_props_lut)-1) 177 return TEE_ERROR_NOT_IMPLEMENTED; 178 179 res = tee_ta_get_current_session(&sess); 180 if (res != TEE_SUCCESS) 181 return res; 182 183 switch (prop) { 184 case UTEE_PROP_TEE_DEV_ID: 185 { 186 TEE_UUID uuid; 187 const size_t nslen = 5; 188 uint8_t data[5 + 189 FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = { 190 'O', 'P', 'T', 'E', 'E' }; 191 192 if (blen < sizeof(uuid)) 193 return TEE_ERROR_SHORT_BUFFER; 194 195 if (tee_otp_get_die_id 196 (data + nslen, sizeof(data) - nslen)) 197 return TEE_ERROR_BAD_STATE; 198 199 res = tee_hash_createdigest(TEE_ALG_SHA256, data, 200 sizeof(data), 201 (uint8_t *)&uuid, 202 sizeof(uuid)); 203 if (res != TEE_SUCCESS) 204 return TEE_ERROR_BAD_STATE; 205 206 /* 207 * Changes the random value into and UUID as specifiec 208 * in RFC 4122. The magic values are from the example 209 * code in the RFC. 210 * 211 * TEE_UUID is defined slightly different from the RFC, 212 * but close enough for our purpose. 213 */ 214 215 uuid.timeHiAndVersion &= 0x0fff; 216 uuid.timeHiAndVersion |= 5 << 12; 217 218 /* uuid.clock_seq_hi_and_reserved in the RFC */ 219 uuid.clockSeqAndNode[0] &= 0x3f; 220 uuid.clockSeqAndNode[0] |= 0x80; 221 222 return tee_svc_copy_to_user(sess, buf, &uuid, 223 sizeof(TEE_UUID)); 224 } 225 226 case UTEE_PROP_TEE_SYS_TIME_PROT_LEVEL: 227 { 228 uint32_t prot; 229 230 if (blen < sizeof(prot)) 231 return TEE_ERROR_SHORT_BUFFER; 232 prot = tee_time_get_sys_time_protection_level(); 233 return tee_svc_copy_to_user(sess, (void *)buf, 234 &prot, sizeof(prot)); 235 } 236 237 case UTEE_PROP_CLIENT_ID: 238 if (blen < sizeof(TEE_Identity)) 239 return TEE_ERROR_SHORT_BUFFER; 240 return tee_svc_copy_to_user(sess, buf, &sess->clnt_id, 241 sizeof(TEE_Identity)); 242 243 case UTEE_PROP_TA_APP_ID: 244 if (blen < sizeof(TEE_UUID)) 245 return TEE_ERROR_SHORT_BUFFER; 246 return tee_svc_copy_to_user(sess, buf, &sess->ctx->uuid, 247 sizeof(TEE_UUID)); 248 default: 249 if (blen < tee_props_lut[prop].len) 250 return TEE_ERROR_SHORT_BUFFER; 251 return tee_svc_copy_to_user(sess, buf, tee_props_lut[prop].data, 252 tee_props_lut[prop].len); 253 } 254 } 255 256 static void utee_param_to_param(struct tee_ta_param *p, struct utee_params *up) 257 { 258 size_t n; 259 uint32_t types = up->types; 260 261 p->types = types; 262 for (n = 0; n < TEE_NUM_PARAMS; n++) { 263 uintptr_t a = up->vals[n * 2]; 264 size_t b = up->vals[n * 2 + 1]; 265 266 switch (TEE_PARAM_TYPE_GET(types, n)) { 267 case TEE_PARAM_TYPE_MEMREF_INPUT: 268 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 269 case TEE_PARAM_TYPE_MEMREF_INOUT: 270 p->params[n].memref.buffer = (void *)a; 271 p->params[n].memref.size = b; 272 break; 273 case TEE_PARAM_TYPE_VALUE_INPUT: 274 case TEE_PARAM_TYPE_VALUE_INOUT: 275 p->params[n].value.a = a; 276 p->params[n].value.b = b; 277 break; 278 default: 279 p->params[n].value.a = 0; 280 p->params[n].value.b = 0; 281 break; 282 } 283 } 284 } 285 286 /* 287 * TA invokes some TA with parameter. 288 * If some parameters are memory references: 289 * - either the memref is inside TA private RAM: TA is not allowed to expose 290 * its private RAM: use a temporary memory buffer and copy the data. 291 * - or the memref is not in the TA private RAM: 292 * - if the memref was mapped to the TA, TA is allowed to expose it. 293 * - if so, converts memref virtual address into a physical address. 294 */ 295 static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess, 296 struct tee_ta_session *called_sess, 297 struct utee_params *callee_params, 298 struct tee_ta_param *param, 299 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 300 tee_mm_entry_t **mm) 301 { 302 size_t n; 303 TEE_Result res; 304 size_t req_mem = 0; 305 size_t s; 306 uint8_t *dst = 0; 307 tee_paddr_t dst_pa, src_pa = 0; 308 bool ta_private_memref[TEE_NUM_PARAMS]; 309 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 310 311 /* fill 'param' input struct with caller params description buffer */ 312 if (!callee_params) { 313 memset(param, 0, sizeof(*param)); 314 } else { 315 res = tee_mmu_check_access_rights(utc, 316 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 317 (tee_uaddr_t)callee_params, sizeof(struct utee_params)); 318 if (res != TEE_SUCCESS) 319 return res; 320 utee_param_to_param(param, callee_params); 321 } 322 323 if (called_sess && is_static_ta_ctx(called_sess->ctx)) { 324 /* 325 * static 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(utc, 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(utc, 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(utc, 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 utc, (void *)param->params[n].memref.buffer); 371 372 param->params[n].memref.buffer = (void *)src_pa; 373 break; 374 375 default: 376 break; 377 } 378 } 379 380 if (req_mem == 0) 381 return TEE_SUCCESS; 382 383 /* Allocate section in secure DDR */ 384 *mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem); 385 if (*mm == NULL) { 386 DMSG("tee_mm_alloc TEE_ERROR_GENERIC"); 387 return TEE_ERROR_GENERIC; 388 } 389 390 /* Get the virtual address for the section in secure DDR */ 391 res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst); 392 if (res != TEE_SUCCESS) 393 return res; 394 dst_pa = tee_mm_get_smem(*mm); 395 396 for (n = 0; n < 4; n++) { 397 398 if (ta_private_memref[n] == false) 399 continue; 400 401 s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t)); 402 403 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 404 case TEE_PARAM_TYPE_MEMREF_INPUT: 405 case TEE_PARAM_TYPE_MEMREF_INOUT: 406 if (param->params[n].memref.buffer != NULL) { 407 res = tee_svc_copy_from_user(sess, dst, 408 param->params[n].memref.buffer, 409 param->params[n].memref.size); 410 if (res != TEE_SUCCESS) 411 return res; 412 param->param_attr[n] = 413 tee_mmu_kmap_get_cache_attr(dst); 414 param->params[n].memref.buffer = (void *)dst_pa; 415 tmp_buf_pa[n] = dst_pa; 416 dst += s; 417 dst_pa += s; 418 } 419 break; 420 421 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 422 if (param->params[n].memref.buffer != NULL) { 423 param->param_attr[n] = 424 tee_mmu_kmap_get_cache_attr(dst); 425 param->params[n].memref.buffer = (void *)dst_pa; 426 tmp_buf_pa[n] = dst_pa; 427 dst += s; 428 dst_pa += s; 429 } 430 break; 431 432 default: 433 continue; 434 } 435 } 436 437 tee_mmu_kunmap(dst, req_mem); 438 439 return TEE_SUCCESS; 440 } 441 442 /* 443 * Back from execution of service: update parameters passed from TA: 444 * If some parameters were memory references: 445 * - either the memref was temporary: copy back data and update size 446 * - or it was the original TA memref: update only the size value. 447 */ 448 static TEE_Result tee_svc_update_out_param( 449 struct tee_ta_session *sess, 450 struct tee_ta_session *called_sess, 451 struct tee_ta_param *param, 452 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 453 struct utee_params *usr_param) 454 { 455 size_t n; 456 void *p; 457 struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 458 bool have_private_mem_map = is_user_ta_ctx(called_sess->ctx); 459 460 tee_ta_set_current_session(sess); 461 462 for (n = 0; n < TEE_NUM_PARAMS; n++) { 463 switch (TEE_PARAM_TYPE_GET(param->types, n)) { 464 case TEE_PARAM_TYPE_MEMREF_OUTPUT: 465 case TEE_PARAM_TYPE_MEMREF_INOUT: 466 p = (void *)(uintptr_t)usr_param->vals[n * 2]; 467 468 /* outside TA private => memref is valid, update size */ 469 if (!tee_mmu_is_vbuf_inside_ta_private(utc, p, 470 param->params[n].memref.size)) { 471 usr_param->vals[n * 2 + 1] = 472 param->params[n].memref.size; 473 break; 474 } 475 476 /* 477 * If we called a kernel TA the parameters are in shared 478 * memory and no copy is needed. 479 */ 480 if (have_private_mem_map && 481 param->params[n].memref.size <= 482 usr_param->vals[n * 2 + 1]) { 483 uint8_t *src = 0; 484 TEE_Result res; 485 486 /* FIXME: TA_RAM is already mapped ! */ 487 res = tee_mmu_kmap(tmp_buf_pa[n], 488 param->params[n].memref.size, &src); 489 if (res != TEE_SUCCESS) 490 return TEE_ERROR_GENERIC; 491 492 res = tee_svc_copy_to_user(sess, p, src, 493 param->params[n].memref.size); 494 if (res != TEE_SUCCESS) 495 return res; 496 tee_mmu_kunmap(src, 497 param->params[n].memref.size); 498 499 } 500 usr_param->vals[n * 2 + 1] = 501 param->params[n].memref.size; 502 break; 503 504 case TEE_PARAM_TYPE_VALUE_OUTPUT: 505 case TEE_PARAM_TYPE_VALUE_INOUT: 506 usr_param->vals[n * 2] = param->params[n].value.a; 507 usr_param->vals[n * 2 + 1] = param->params[n].value.b; 508 break; 509 510 default: 511 continue; 512 } 513 } 514 515 return TEE_SUCCESS; 516 } 517 518 /* Called when a TA calls an OpenSession on another TA */ 519 TEE_Result syscall_open_ta_session(const TEE_UUID *dest, 520 unsigned long cancel_req_to, 521 struct utee_params *usr_param, uint32_t *ta_sess, 522 uint32_t *ret_orig) 523 { 524 TEE_Result res; 525 uint32_t ret_o = TEE_ORIGIN_TEE; 526 struct tee_ta_session *s = NULL; 527 struct tee_ta_session *sess; 528 tee_mm_entry_t *mm_param = NULL; 529 TEE_UUID *uuid = malloc(sizeof(TEE_UUID)); 530 struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param)); 531 TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity)); 532 tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 533 struct user_ta_ctx *utc; 534 535 if (uuid == NULL || param == NULL || clnt_id == NULL) { 536 res = TEE_ERROR_OUT_OF_MEMORY; 537 goto out_free_only; 538 } 539 540 memset(param, 0, sizeof(struct tee_ta_param)); 541 542 res = tee_ta_get_current_session(&sess); 543 if (res != TEE_SUCCESS) 544 goto out_free_only; 545 utc = to_user_ta_ctx(sess->ctx); 546 547 res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID)); 548 if (res != TEE_SUCCESS) 549 goto function_exit; 550 551 clnt_id->login = TEE_LOGIN_TRUSTED_APP; 552 memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 553 554 res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_pa, 555 &mm_param); 556 if (res != TEE_SUCCESS) 557 goto function_exit; 558 559 /* 560 * Find session of a multi session TA or a static TA 561 * In such a case, there is no need to ask the supplicant for the TA 562 * code 563 */ 564 res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid, 565 clnt_id, cancel_req_to, param); 566 if (res != TEE_SUCCESS) 567 goto function_exit; 568 569 res = tee_svc_update_out_param(sess, s, param, tmp_buf_pa, usr_param); 570 571 function_exit: 572 tee_ta_set_current_session(sess); 573 sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 574 575 if (mm_param != NULL) { 576 TEE_Result res2; 577 void *va = 0; 578 579 res2 = 580 tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va); 581 if (res2 == TEE_SUCCESS) 582 tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 583 } 584 tee_mm_free(mm_param); 585 if (res == TEE_SUCCESS) 586 tee_svc_copy_kaddr_to_uref(sess, ta_sess, s); 587 tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 588 589 out_free_only: 590 free(param); 591 free(uuid); 592 free(clnt_id); 593 return res; 594 } 595 596 TEE_Result syscall_close_ta_session(unsigned long ta_sess) 597 { 598 TEE_Result res; 599 struct tee_ta_session *sess; 600 TEE_Identity clnt_id; 601 struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess); 602 struct user_ta_ctx *utc; 603 604 res = tee_ta_get_current_session(&sess); 605 if (res != TEE_SUCCESS) 606 return res; 607 utc = to_user_ta_ctx(sess->ctx); 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, &utc->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 struct user_ta_ctx *utc; 631 632 res = tee_ta_get_current_session(&sess); 633 if (res != TEE_SUCCESS) 634 return res; 635 utc = to_user_ta_ctx(sess->ctx); 636 637 called_sess = tee_ta_get_session( 638 (vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true, 639 &utc->open_sessions); 640 if (!called_sess) 641 return TEE_ERROR_BAD_PARAMETERS; 642 643 clnt_id.login = TEE_LOGIN_TRUSTED_APP; 644 memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 645 646 res = tee_svc_copy_param(sess, called_sess, usr_param, ¶m, 647 tmp_buf_pa, &mm_param); 648 if (res != TEE_SUCCESS) 649 goto function_exit; 650 651 res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id, 652 cancel_req_to, cmd_id, ¶m); 653 654 if (res != TEE_SUCCESS) 655 goto function_exit; 656 657 res = tee_svc_update_out_param(sess, called_sess, ¶m, tmp_buf_pa, 658 usr_param); 659 if (res != TEE_SUCCESS) 660 goto function_exit; 661 662 function_exit: 663 tee_ta_set_current_session(sess); 664 called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 665 tee_ta_put_session(called_sess); 666 667 if (mm_param != NULL) { 668 TEE_Result res2; 669 void *va = 0; 670 671 res2 = 672 tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va); 673 if (res2 == TEE_SUCCESS) 674 tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 675 } 676 tee_mm_free(mm_param); 677 if (ret_orig) 678 tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 679 return res; 680 } 681 682 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf, 683 size_t len) 684 { 685 TEE_Result res; 686 struct tee_ta_session *s; 687 688 res = tee_ta_get_current_session(&s); 689 if (res != TEE_SUCCESS) 690 return res; 691 692 return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags, 693 (tee_uaddr_t)buf, len); 694 } 695 696 TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr, 697 const void *uaddr, size_t len) 698 { 699 TEE_Result res; 700 struct tee_ta_session *s; 701 702 if (sess == NULL) { 703 res = tee_ta_get_current_session(&s); 704 if (res != TEE_SUCCESS) 705 return res; 706 } else { 707 s = sess; 708 tee_ta_set_current_session(s); 709 } 710 res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 711 TEE_MEMORY_ACCESS_READ | 712 TEE_MEMORY_ACCESS_ANY_OWNER, 713 (tee_uaddr_t)uaddr, len); 714 if (res != TEE_SUCCESS) 715 return res; 716 717 memcpy(kaddr, uaddr, len); 718 return TEE_SUCCESS; 719 } 720 721 TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr, 722 const void *kaddr, size_t len) 723 { 724 TEE_Result res; 725 struct tee_ta_session *s; 726 727 if (sess == NULL) { 728 res = tee_ta_get_current_session(&s); 729 if (res != TEE_SUCCESS) 730 return res; 731 } else { 732 s = sess; 733 tee_ta_set_current_session(s); 734 } 735 736 res = tee_mmu_check_access_rights(to_user_ta_ctx(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