1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2020-2022, Arm Limited. 4 */ 5 #include <bench.h> 6 #include <crypto/crypto.h> 7 #include <initcall.h> 8 #include <kernel/embedded_ts.h> 9 #include <kernel/ldelf_loader.h> 10 #include <kernel/secure_partition.h> 11 #include <kernel/spinlock.h> 12 #include <kernel/spmc_sp_handler.h> 13 #include <kernel/thread_private.h> 14 #include <kernel/thread_spmc.h> 15 #include <kernel/tpm.h> 16 #include <kernel/ts_store.h> 17 #include <ldelf.h> 18 #include <libfdt.h> 19 #include <mm/core_mmu.h> 20 #include <mm/fobj.h> 21 #include <mm/mobj.h> 22 #include <mm/vm.h> 23 #include <optee_ffa.h> 24 #include <stdio.h> 25 #include <string.h> 26 #include <tee_api_types.h> 27 #include <tee/uuid.h> 28 #include <trace.h> 29 #include <types_ext.h> 30 #include <utee_defines.h> 31 #include <util.h> 32 #include <zlib.h> 33 34 #define SP_MANIFEST_ATTR_READ BIT(0) 35 #define SP_MANIFEST_ATTR_WRITE BIT(1) 36 #define SP_MANIFEST_ATTR_EXEC BIT(2) 37 #define SP_MANIFEST_ATTR_NSEC BIT(3) 38 39 #define SP_MANIFEST_ATTR_RO (SP_MANIFEST_ATTR_READ) 40 #define SP_MANIFEST_ATTR_RW (SP_MANIFEST_ATTR_READ | \ 41 SP_MANIFEST_ATTR_WRITE) 42 #define SP_MANIFEST_ATTR_RX (SP_MANIFEST_ATTR_READ | \ 43 SP_MANIFEST_ATTR_EXEC) 44 #define SP_MANIFEST_ATTR_RWX (SP_MANIFEST_ATTR_READ | \ 45 SP_MANIFEST_ATTR_WRITE | \ 46 SP_MANIFEST_ATTR_EXEC) 47 48 const struct ts_ops sp_ops; 49 50 /* List that holds all of the loaded SP's */ 51 static struct sp_sessions_head open_sp_sessions = 52 TAILQ_HEAD_INITIALIZER(open_sp_sessions); 53 54 static const struct embedded_ts *find_secure_partition(const TEE_UUID *uuid) 55 { 56 const struct sp_image *sp = NULL; 57 58 for_each_secure_partition(sp) { 59 if (!memcmp(&sp->image.uuid, uuid, sizeof(*uuid))) 60 return &sp->image; 61 } 62 return NULL; 63 } 64 65 bool is_sp_ctx(struct ts_ctx *ctx) 66 { 67 return ctx && (ctx->ops == &sp_ops); 68 } 69 70 static void set_sp_ctx_ops(struct ts_ctx *ctx) 71 { 72 ctx->ops = &sp_ops; 73 } 74 75 TEE_Result sp_find_session_id(const TEE_UUID *uuid, uint32_t *session_id) 76 { 77 struct sp_session *s = NULL; 78 79 TAILQ_FOREACH(s, &open_sp_sessions, link) { 80 if (!memcmp(&s->ts_sess.ctx->uuid, uuid, sizeof(*uuid))) { 81 if (s->state == sp_dead) 82 return TEE_ERROR_TARGET_DEAD; 83 84 *session_id = s->endpoint_id; 85 return TEE_SUCCESS; 86 } 87 } 88 89 return TEE_ERROR_ITEM_NOT_FOUND; 90 } 91 92 struct sp_session *sp_get_session(uint32_t session_id) 93 { 94 struct sp_session *s = NULL; 95 96 TAILQ_FOREACH(s, &open_sp_sessions, link) { 97 if (s->endpoint_id == session_id) 98 return s; 99 } 100 101 return NULL; 102 } 103 104 TEE_Result sp_partition_info_get_all(struct ffa_partition_info *fpi, 105 size_t *elem_count) 106 { 107 size_t in_count = *elem_count; 108 struct sp_session *s = NULL; 109 size_t count = 0; 110 111 TAILQ_FOREACH(s, &open_sp_sessions, link) { 112 if (s->state == sp_dead) 113 continue; 114 if (count < in_count) { 115 spmc_fill_partition_entry(fpi, s->endpoint_id, 1); 116 fpi++; 117 } 118 count++; 119 } 120 121 *elem_count = count; 122 if (count > in_count) 123 return TEE_ERROR_SHORT_BUFFER; 124 125 return TEE_SUCCESS; 126 } 127 128 bool sp_has_exclusive_access(struct sp_mem_map_region *mem, 129 struct user_mode_ctx *uctx) 130 { 131 /* 132 * Check that we have access to the region if it is supposed to be 133 * mapped to the current context. 134 */ 135 if (uctx) { 136 struct vm_region *region = NULL; 137 138 /* Make sure that each mobj belongs to the SP */ 139 TAILQ_FOREACH(region, &uctx->vm_info.regions, link) { 140 if (region->mobj == mem->mobj) 141 break; 142 } 143 144 if (!region) 145 return false; 146 } 147 148 /* Check that it is not shared with another SP */ 149 return !sp_mem_is_shared(mem); 150 } 151 152 static uint16_t new_session_id(struct sp_sessions_head *open_sessions) 153 { 154 struct sp_session *last = NULL; 155 uint16_t id = SPMC_ENDPOINT_ID + 1; 156 157 last = TAILQ_LAST(open_sessions, sp_sessions_head); 158 if (last) 159 id = last->endpoint_id + 1; 160 161 assert(id > SPMC_ENDPOINT_ID); 162 return id; 163 } 164 165 static TEE_Result sp_create_ctx(const TEE_UUID *uuid, struct sp_session *s) 166 { 167 TEE_Result res = TEE_SUCCESS; 168 struct sp_ctx *spc = NULL; 169 170 /* Register context */ 171 spc = calloc(1, sizeof(struct sp_ctx)); 172 if (!spc) 173 return TEE_ERROR_OUT_OF_MEMORY; 174 175 spc->uctx.ts_ctx = &spc->ts_ctx; 176 spc->open_session = s; 177 s->ts_sess.ctx = &spc->ts_ctx; 178 spc->ts_ctx.uuid = *uuid; 179 180 res = vm_info_init(&spc->uctx); 181 if (res) 182 goto err; 183 184 set_sp_ctx_ops(&spc->ts_ctx); 185 186 return TEE_SUCCESS; 187 188 err: 189 free(spc); 190 return res; 191 } 192 193 static TEE_Result sp_create_session(struct sp_sessions_head *open_sessions, 194 const TEE_UUID *uuid, 195 struct sp_session **sess) 196 { 197 TEE_Result res = TEE_SUCCESS; 198 struct sp_session *s = calloc(1, sizeof(struct sp_session)); 199 200 if (!s) 201 return TEE_ERROR_OUT_OF_MEMORY; 202 203 s->endpoint_id = new_session_id(open_sessions); 204 if (!s->endpoint_id) { 205 res = TEE_ERROR_OVERFLOW; 206 goto err; 207 } 208 209 DMSG("Loading Secure Partition %pUl", (void *)uuid); 210 res = sp_create_ctx(uuid, s); 211 if (res) 212 goto err; 213 214 TAILQ_INSERT_TAIL(open_sessions, s, link); 215 *sess = s; 216 return TEE_SUCCESS; 217 218 err: 219 free(s); 220 return res; 221 } 222 223 static TEE_Result sp_init_set_registers(struct sp_ctx *ctx) 224 { 225 struct thread_ctx_regs *sp_regs = &ctx->sp_regs; 226 227 memset(sp_regs, 0, sizeof(*sp_regs)); 228 sp_regs->sp = ctx->uctx.stack_ptr; 229 sp_regs->pc = ctx->uctx.entry_func; 230 231 return TEE_SUCCESS; 232 } 233 234 TEE_Result sp_map_shared(struct sp_session *s, 235 struct sp_mem_receiver *receiver, 236 struct sp_mem *smem, 237 uint64_t *va) 238 { 239 TEE_Result res = TEE_SUCCESS; 240 struct sp_ctx *ctx = NULL; 241 uint32_t perm = TEE_MATTR_UR; 242 struct sp_mem_map_region *reg = NULL; 243 244 ctx = to_sp_ctx(s->ts_sess.ctx); 245 246 /* Get the permission */ 247 if (receiver->perm.perm & FFA_MEM_ACC_EXE) 248 perm |= TEE_MATTR_UX; 249 250 if (receiver->perm.perm & FFA_MEM_ACC_RW) { 251 if (receiver->perm.perm & FFA_MEM_ACC_EXE) 252 return TEE_ERROR_ACCESS_CONFLICT; 253 254 perm |= TEE_MATTR_UW; 255 } 256 /* 257 * Currently we don't support passing a va. We can't guarantee that the 258 * full region will be mapped in a contiguous region. A smem->region can 259 * have multiple mobj for one share. Currently there doesn't seem to be 260 * an option to guarantee that these will be mapped in a contiguous va 261 * space. 262 */ 263 if (*va) 264 return TEE_ERROR_NOT_SUPPORTED; 265 266 SLIST_FOREACH(reg, &smem->regions, link) { 267 res = vm_map(&ctx->uctx, va, reg->page_count * SMALL_PAGE_SIZE, 268 perm, 0, reg->mobj, reg->page_offset); 269 270 if (res != TEE_SUCCESS) { 271 EMSG("Failed to map memory region %#"PRIx32, res); 272 return res; 273 } 274 } 275 return TEE_SUCCESS; 276 } 277 278 TEE_Result sp_unmap_ffa_regions(struct sp_session *s, struct sp_mem *smem) 279 { 280 TEE_Result res = TEE_SUCCESS; 281 vaddr_t vaddr = 0; 282 size_t len = 0; 283 struct sp_ctx *ctx = to_sp_ctx(s->ts_sess.ctx); 284 struct sp_mem_map_region *reg = NULL; 285 286 SLIST_FOREACH(reg, &smem->regions, link) { 287 vaddr = (vaddr_t)sp_mem_get_va(&ctx->uctx, reg->page_offset, 288 reg->mobj); 289 len = reg->page_count * SMALL_PAGE_SIZE; 290 291 res = vm_unmap(&ctx->uctx, vaddr, len); 292 if (res != TEE_SUCCESS) 293 return res; 294 } 295 296 return TEE_SUCCESS; 297 } 298 299 static TEE_Result sp_open_session(struct sp_session **sess, 300 struct sp_sessions_head *open_sessions, 301 const TEE_UUID *uuid) 302 { 303 TEE_Result res = TEE_SUCCESS; 304 struct sp_session *s = NULL; 305 struct sp_ctx *ctx = NULL; 306 307 if (!find_secure_partition(uuid)) 308 return TEE_ERROR_ITEM_NOT_FOUND; 309 310 res = sp_create_session(open_sessions, uuid, &s); 311 if (res != TEE_SUCCESS) { 312 DMSG("sp_create_session failed %#"PRIx32, res); 313 return res; 314 } 315 316 ctx = to_sp_ctx(s->ts_sess.ctx); 317 assert(ctx); 318 if (!ctx) 319 return TEE_ERROR_TARGET_DEAD; 320 *sess = s; 321 322 ts_push_current_session(&s->ts_sess); 323 /* Load the SP using ldelf. */ 324 ldelf_load_ldelf(&ctx->uctx); 325 res = ldelf_init_with_ldelf(&s->ts_sess, &ctx->uctx); 326 327 if (res != TEE_SUCCESS) { 328 EMSG("Failed. loading SP using ldelf %#"PRIx32, res); 329 ts_pop_current_session(); 330 return TEE_ERROR_TARGET_DEAD; 331 } 332 333 /* Make the SP ready for its first run */ 334 s->state = sp_idle; 335 s->caller_id = 0; 336 sp_init_set_registers(ctx); 337 ts_pop_current_session(); 338 339 return TEE_SUCCESS; 340 } 341 342 static TEE_Result sp_dt_get_u64(const void *fdt, int node, const char *property, 343 uint64_t *value) 344 { 345 const fdt64_t *p = NULL; 346 int len = 0; 347 348 p = fdt_getprop(fdt, node, property, &len); 349 if (!p || len != sizeof(*p)) 350 return TEE_ERROR_ITEM_NOT_FOUND; 351 352 *value = fdt64_to_cpu(*p); 353 354 return TEE_SUCCESS; 355 } 356 357 static TEE_Result sp_dt_get_u32(const void *fdt, int node, const char *property, 358 uint32_t *value) 359 { 360 const fdt32_t *p = NULL; 361 int len = 0; 362 363 p = fdt_getprop(fdt, node, property, &len); 364 if (!p || len != sizeof(*p)) 365 return TEE_ERROR_ITEM_NOT_FOUND; 366 367 *value = fdt32_to_cpu(*p); 368 369 return TEE_SUCCESS; 370 } 371 372 static TEE_Result check_fdt(const void * const fdt, const TEE_UUID *uuid) 373 { 374 int len = 0; 375 const fdt32_t *prop = NULL; 376 int i = 0; 377 const struct fdt_property *description = NULL; 378 int description_name_len = 0; 379 uint32_t uuid_array[4] = { 0 }; 380 TEE_UUID fdt_uuid = { }; 381 382 if (fdt_node_check_compatible(fdt, 0, "arm,ffa-manifest-1.0")) { 383 EMSG("Failed loading SP, manifest not found"); 384 return TEE_ERROR_BAD_PARAMETERS; 385 } 386 387 description = fdt_get_property(fdt, 0, "description", 388 &description_name_len); 389 if (description) 390 DMSG("Loading SP: %s", description->data); 391 392 prop = fdt_getprop(fdt, 0, "uuid", &len); 393 if (!prop || len != 16) { 394 EMSG("Missing or invalid UUID in SP manifest"); 395 return TEE_ERROR_BAD_FORMAT; 396 } 397 398 for (i = 0; i < 4; i++) 399 uuid_array[i] = fdt32_to_cpu(prop[i]); 400 tee_uuid_from_octets(&fdt_uuid, (uint8_t *)uuid_array); 401 402 if (memcmp(uuid, &fdt_uuid, sizeof(fdt_uuid))) { 403 EMSG("Failed loading SP, UUID mismatch"); 404 return TEE_ERROR_BAD_FORMAT; 405 } 406 407 return TEE_SUCCESS; 408 } 409 410 /* 411 * sp_init_info allocates and maps the sp_ffa_init_info for the SP. It will copy 412 * the fdt into the allocated page(s) and return a pointer to the new location 413 * of the fdt. This pointer can be used to update data inside the fdt. 414 */ 415 static TEE_Result sp_init_info(struct sp_ctx *ctx, struct thread_smc_args *args, 416 const void * const input_fdt, vaddr_t *va, 417 size_t *num_pgs, void **fdt_copy) 418 { 419 struct sp_ffa_init_info *info = NULL; 420 int nvp_count = 1; 421 size_t total_size = ROUNDUP(CFG_SP_INIT_INFO_MAX_SIZE, SMALL_PAGE_SIZE); 422 size_t nvp_size = sizeof(struct sp_name_value_pair) * nvp_count; 423 size_t info_size = sizeof(*info) + nvp_size; 424 size_t fdt_size = total_size - info_size; 425 TEE_Result res = TEE_SUCCESS; 426 uint32_t perm = TEE_MATTR_URW | TEE_MATTR_PRW; 427 struct fobj *f = NULL; 428 struct mobj *m = NULL; 429 static const char fdt_name[16] = "TYPE_DT\0\0\0\0\0\0\0\0"; 430 431 *num_pgs = total_size / SMALL_PAGE_SIZE; 432 433 f = fobj_sec_mem_alloc(*num_pgs); 434 m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED); 435 436 fobj_put(f); 437 if (!m) 438 return TEE_ERROR_OUT_OF_MEMORY; 439 440 res = vm_map(&ctx->uctx, va, total_size, perm, 0, m, 0); 441 mobj_put(m); 442 if (res) 443 return res; 444 445 info = (struct sp_ffa_init_info *)*va; 446 447 /* magic field is 4 bytes, we don't copy /0 byte. */ 448 memcpy(&info->magic, "FF-A", 4); 449 info->count = nvp_count; 450 args->a0 = (vaddr_t)info; 451 452 /* 453 * Store the fdt after the boot_info and store the pointer in the 454 * first element. 455 */ 456 COMPILE_TIME_ASSERT(sizeof(info->nvp[0].name) == sizeof(fdt_name)); 457 memcpy(info->nvp[0].name, fdt_name, sizeof(fdt_name)); 458 info->nvp[0].value = *va + info_size; 459 info->nvp[0].size = fdt_size; 460 *fdt_copy = (void *)info->nvp[0].value; 461 462 if (fdt_open_into(input_fdt, *fdt_copy, fdt_size)) 463 return TEE_ERROR_GENERIC; 464 465 return TEE_SUCCESS; 466 } 467 468 static TEE_Result handle_fdt_dev_regions(struct sp_ctx *ctx, void *fdt) 469 { 470 int node = 0; 471 int subnode = 0; 472 TEE_Result res = TEE_SUCCESS; 473 const char *dt_device_match_table = { 474 "arm,ffa-manifest-device-regions", 475 }; 476 477 /* 478 * Device regions are optional in the SP manifest, it's not an error if 479 * we don't find any 480 */ 481 node = fdt_node_offset_by_compatible(fdt, 0, dt_device_match_table); 482 if (node < 0) 483 return TEE_SUCCESS; 484 485 fdt_for_each_subnode(subnode, fdt, node) { 486 uint64_t base_addr = 0; 487 uint32_t pages_cnt = 0; 488 uint32_t attributes = 0; 489 struct mobj *m = NULL; 490 bool is_secure = true; 491 uint32_t perm = 0; 492 vaddr_t va = 0; 493 unsigned int idx = 0; 494 495 /* 496 * Physical base address of a device MMIO region. 497 * Currently only physically contiguous region is supported. 498 */ 499 if (sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) { 500 EMSG("Mandatory field is missing: base-address"); 501 return TEE_ERROR_BAD_FORMAT; 502 } 503 504 /* Total size of MMIO region as count of 4K pages */ 505 if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) { 506 EMSG("Mandatory field is missing: pages-count"); 507 return TEE_ERROR_BAD_FORMAT; 508 } 509 510 /* Data access, instruction access and security attributes */ 511 if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) { 512 EMSG("Mandatory field is missing: attributes"); 513 return TEE_ERROR_BAD_FORMAT; 514 } 515 516 /* Check instruction and data access permissions */ 517 switch (attributes & SP_MANIFEST_ATTR_RWX) { 518 case SP_MANIFEST_ATTR_RO: 519 perm = TEE_MATTR_UR; 520 break; 521 case SP_MANIFEST_ATTR_RW: 522 perm = TEE_MATTR_URW; 523 break; 524 default: 525 EMSG("Invalid memory access permissions"); 526 return TEE_ERROR_BAD_FORMAT; 527 } 528 529 /* 530 * The SP is a secure endpoint, security attribute can be 531 * secure or non-secure 532 */ 533 if (attributes & SP_MANIFEST_ATTR_NSEC) 534 is_secure = false; 535 536 /* Memory attributes must be Device-nGnRnE */ 537 m = sp_mem_new_mobj(pages_cnt, TEE_MATTR_MEM_TYPE_STRONGLY_O, 538 is_secure); 539 if (!m) 540 return TEE_ERROR_OUT_OF_MEMORY; 541 542 res = sp_mem_add_pages(m, &idx, (paddr_t)base_addr, pages_cnt); 543 if (res) { 544 mobj_put(m); 545 return res; 546 } 547 548 res = vm_map(&ctx->uctx, &va, pages_cnt * SMALL_PAGE_SIZE, 549 perm, 0, m, 0); 550 mobj_put(m); 551 if (res) 552 return res; 553 554 /* 555 * Overwrite the device region's PA in the fdt with the VA. This 556 * fdt will be passed to the SP. 557 */ 558 res = fdt_setprop_u64(fdt, subnode, "base-address", va); 559 560 /* 561 * Unmap the region if the overwrite failed since the SP won't 562 * be able to access it without knowing the VA. 563 */ 564 if (res) { 565 vm_unmap(&ctx->uctx, va, pages_cnt * SMALL_PAGE_SIZE); 566 return res; 567 } 568 } 569 570 return TEE_SUCCESS; 571 } 572 573 static TEE_Result handle_fdt_mem_regions(struct sp_ctx *ctx, void *fdt) 574 { 575 int node = 0; 576 int subnode = 0; 577 tee_mm_entry_t *mm = NULL; 578 TEE_Result res = TEE_SUCCESS; 579 580 /* 581 * Memory regions are optional in the SP manifest, it's not an error if 582 * we don't find any. 583 */ 584 node = fdt_node_offset_by_compatible(fdt, 0, 585 "arm,ffa-manifest-memory-regions"); 586 if (node < 0) 587 return TEE_SUCCESS; 588 589 fdt_for_each_subnode(subnode, fdt, node) { 590 bool alloc_needed = false; 591 uint32_t attributes = 0; 592 uint64_t base_addr = 0; 593 uint32_t pages_cnt = 0; 594 bool is_secure = true; 595 struct mobj *m = NULL; 596 unsigned int idx = 0; 597 uint32_t perm = 0; 598 size_t size = 0; 599 vaddr_t va = 0; 600 601 mm = NULL; 602 603 /* 604 * Base address of a memory region. 605 * If not present, we have to allocate the specified memory. 606 * If present, this field could specify a PA or VA. Currently 607 * only a PA is supported. 608 */ 609 if (sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) 610 alloc_needed = true; 611 612 /* Size of memory region as count of 4K pages */ 613 if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) { 614 EMSG("Mandatory field is missing: pages-count"); 615 return TEE_ERROR_BAD_FORMAT; 616 } 617 618 if (MUL_OVERFLOW(pages_cnt, SMALL_PAGE_SIZE, &size)) 619 return TEE_ERROR_OVERFLOW; 620 621 /* 622 * Memory region attributes: 623 * - Instruction/data access permissions 624 * - Cacheability/shareability attributes 625 * - Security attributes 626 * 627 * Cacheability/shareability attributes can be ignored for now. 628 * OP-TEE only supports a single type for normal cached memory 629 * and currently there is no use case that would require to 630 * change this. 631 */ 632 if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) { 633 EMSG("Mandatory field is missing: attributes"); 634 return TEE_ERROR_BAD_FORMAT; 635 } 636 637 /* Check instruction and data access permissions */ 638 switch (attributes & SP_MANIFEST_ATTR_RWX) { 639 case SP_MANIFEST_ATTR_RO: 640 perm = TEE_MATTR_UR; 641 break; 642 case SP_MANIFEST_ATTR_RW: 643 perm = TEE_MATTR_URW; 644 break; 645 case SP_MANIFEST_ATTR_RX: 646 perm = TEE_MATTR_URX; 647 break; 648 default: 649 EMSG("Invalid memory access permissions"); 650 return TEE_ERROR_BAD_FORMAT; 651 } 652 653 /* 654 * The SP is a secure endpoint, security attribute can be 655 * secure or non-secure. 656 * The SPMC cannot allocate non-secure memory, i.e. if the base 657 * address is missing this attribute must be secure. 658 */ 659 if (attributes & SP_MANIFEST_ATTR_NSEC) { 660 if (alloc_needed) { 661 EMSG("Invalid memory security attribute"); 662 return TEE_ERROR_BAD_FORMAT; 663 } 664 is_secure = false; 665 } 666 667 if (alloc_needed) { 668 /* Base address is missing, we have to allocate */ 669 mm = tee_mm_alloc(&tee_mm_sec_ddr, size); 670 if (!mm) 671 return TEE_ERROR_OUT_OF_MEMORY; 672 673 base_addr = tee_mm_get_smem(mm); 674 } 675 676 m = sp_mem_new_mobj(pages_cnt, TEE_MATTR_MEM_TYPE_CACHED, 677 is_secure); 678 if (!m) { 679 res = TEE_ERROR_OUT_OF_MEMORY; 680 goto err_mm_free; 681 } 682 683 res = sp_mem_add_pages(m, &idx, base_addr, pages_cnt); 684 if (res) { 685 mobj_put(m); 686 goto err_mm_free; 687 } 688 689 res = vm_map(&ctx->uctx, &va, size, perm, 0, m, 0); 690 mobj_put(m); 691 if (res) 692 goto err_mm_free; 693 694 /* 695 * Overwrite the memory region's base address in the fdt with 696 * the VA. This fdt will be passed to the SP. 697 * If the base-address field was not present in the original 698 * fdt, this function will create it. This doesn't cause issues 699 * since the necessary extra space has been allocated when 700 * opening the fdt. 701 */ 702 res = fdt_setprop_u64(fdt, subnode, "base-address", va); 703 704 /* 705 * Unmap the region if the overwrite failed since the SP won't 706 * be able to access it without knowing the VA. 707 */ 708 if (res) { 709 vm_unmap(&ctx->uctx, va, size); 710 goto err_mm_free; 711 } 712 } 713 714 return TEE_SUCCESS; 715 716 err_mm_free: 717 tee_mm_free(mm); 718 return res; 719 } 720 721 static TEE_Result handle_tpm_event_log(struct sp_ctx *ctx, void *fdt) 722 { 723 uint32_t perm = TEE_MATTR_URW | TEE_MATTR_PRW; 724 uint32_t dummy_size __maybe_unused = 0; 725 TEE_Result res = TEE_SUCCESS; 726 size_t page_count = 0; 727 struct fobj *f = NULL; 728 struct mobj *m = NULL; 729 vaddr_t log_addr = 0; 730 size_t log_size = 0; 731 int node = 0; 732 733 node = fdt_node_offset_by_compatible(fdt, 0, "arm,tpm_event_log"); 734 if (node < 0) 735 return TEE_SUCCESS; 736 737 /* Checking the existence and size of the event log properties */ 738 if (sp_dt_get_u64(fdt, node, "tpm_event_log_addr", &log_addr)) { 739 EMSG("tpm_event_log_addr not found or has invalid size"); 740 return TEE_ERROR_BAD_FORMAT; 741 } 742 743 if (sp_dt_get_u32(fdt, node, "tpm_event_log_size", &dummy_size)) { 744 EMSG("tpm_event_log_size not found or has invalid size"); 745 return TEE_ERROR_BAD_FORMAT; 746 } 747 748 /* Validating event log */ 749 res = tpm_get_event_log_size(&log_size); 750 if (res) 751 return res; 752 753 if (!log_size) { 754 EMSG("Empty TPM event log was provided"); 755 return TEE_ERROR_ITEM_NOT_FOUND; 756 } 757 758 /* Allocating memory area for the event log to share with the SP */ 759 page_count = ROUNDUP_DIV(log_size, SMALL_PAGE_SIZE); 760 761 f = fobj_sec_mem_alloc(page_count); 762 m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED); 763 fobj_put(f); 764 if (!m) 765 return TEE_ERROR_OUT_OF_MEMORY; 766 767 res = vm_map(&ctx->uctx, &log_addr, log_size, perm, 0, m, 0); 768 mobj_put(m); 769 if (res) 770 return res; 771 772 /* Copy event log */ 773 res = tpm_get_event_log((void *)log_addr, &log_size); 774 if (res) 775 goto err_unmap; 776 777 /* Setting event log details in the manifest */ 778 res = fdt_setprop_u64(fdt, node, "tpm_event_log_addr", log_addr); 779 if (res) 780 goto err_unmap; 781 782 res = fdt_setprop_u32(fdt, node, "tpm_event_log_size", log_size); 783 if (res) 784 goto err_unmap; 785 786 return TEE_SUCCESS; 787 788 err_unmap: 789 vm_unmap(&ctx->uctx, log_addr, log_size); 790 791 return res; 792 } 793 794 static TEE_Result sp_init_uuid(const TEE_UUID *uuid, const void * const fdt) 795 { 796 TEE_Result res = TEE_SUCCESS; 797 struct sp_session *sess = NULL; 798 struct thread_smc_args args = { }; 799 vaddr_t va = 0; 800 size_t num_pgs = 0; 801 struct sp_ctx *ctx = NULL; 802 void *fdt_copy = NULL; 803 804 res = sp_open_session(&sess, 805 &open_sp_sessions, 806 uuid); 807 if (res) 808 return res; 809 810 res = check_fdt(fdt, uuid); 811 if (res) 812 return res; 813 814 ctx = to_sp_ctx(sess->ts_sess.ctx); 815 ts_push_current_session(&sess->ts_sess); 816 817 res = sp_init_info(ctx, &args, fdt, &va, &num_pgs, &fdt_copy); 818 if (res) 819 goto out; 820 821 res = handle_fdt_dev_regions(ctx, fdt_copy); 822 if (res) 823 goto out; 824 825 res = handle_fdt_mem_regions(ctx, fdt_copy); 826 if (res) 827 goto out; 828 829 if (IS_ENABLED(CFG_CORE_TPM_EVENT_LOG)) { 830 res = handle_tpm_event_log(ctx, fdt_copy); 831 if (res) 832 goto out; 833 } 834 835 ts_pop_current_session(); 836 837 if (sp_enter(&args, sess)) { 838 vm_unmap(&ctx->uctx, va, num_pgs); 839 return FFA_ABORTED; 840 } 841 842 spmc_sp_msg_handler(&args, sess); 843 844 ts_push_current_session(&sess->ts_sess); 845 out: 846 /* Free the boot info page from the SP memory */ 847 vm_unmap(&ctx->uctx, va, num_pgs); 848 ts_pop_current_session(); 849 850 return res; 851 } 852 853 TEE_Result sp_enter(struct thread_smc_args *args, struct sp_session *sp) 854 { 855 TEE_Result res = FFA_OK; 856 struct sp_ctx *ctx = to_sp_ctx(sp->ts_sess.ctx); 857 858 ctx->sp_regs.x[0] = args->a0; 859 ctx->sp_regs.x[1] = args->a1; 860 ctx->sp_regs.x[2] = args->a2; 861 ctx->sp_regs.x[3] = args->a3; 862 ctx->sp_regs.x[4] = args->a4; 863 ctx->sp_regs.x[5] = args->a5; 864 ctx->sp_regs.x[6] = args->a6; 865 ctx->sp_regs.x[7] = args->a7; 866 867 res = sp->ts_sess.ctx->ops->enter_invoke_cmd(&sp->ts_sess, 0); 868 869 args->a0 = ctx->sp_regs.x[0]; 870 args->a1 = ctx->sp_regs.x[1]; 871 args->a2 = ctx->sp_regs.x[2]; 872 args->a3 = ctx->sp_regs.x[3]; 873 args->a4 = ctx->sp_regs.x[4]; 874 args->a5 = ctx->sp_regs.x[5]; 875 args->a6 = ctx->sp_regs.x[6]; 876 args->a7 = ctx->sp_regs.x[7]; 877 878 return res; 879 } 880 881 static TEE_Result sp_enter_invoke_cmd(struct ts_session *s, 882 uint32_t cmd __unused) 883 { 884 struct sp_ctx *ctx = to_sp_ctx(s->ctx); 885 TEE_Result res = TEE_SUCCESS; 886 uint32_t exceptions = 0; 887 uint64_t cpsr = 0; 888 struct sp_session *sp_s = to_sp_session(s); 889 struct ts_session *sess = NULL; 890 struct thread_ctx_regs *sp_regs = NULL; 891 uint32_t panicked = false; 892 uint32_t panic_code = 0; 893 894 bm_timestamp(); 895 896 sp_regs = &ctx->sp_regs; 897 ts_push_current_session(s); 898 899 cpsr = sp_regs->cpsr; 900 sp_regs->cpsr = read_daif() & (SPSR_64_DAIF_MASK << SPSR_64_DAIF_SHIFT); 901 902 exceptions = thread_mask_exceptions(THREAD_EXCP_ALL); 903 __thread_enter_user_mode(sp_regs, &panicked, &panic_code); 904 sp_regs->cpsr = cpsr; 905 thread_unmask_exceptions(exceptions); 906 907 thread_user_clear_vfp(&ctx->uctx); 908 909 if (panicked) { 910 DMSG("SP panicked with code %#"PRIx32, panic_code); 911 abort_print_current_ts(); 912 913 sess = ts_pop_current_session(); 914 cpu_spin_lock(&sp_s->spinlock); 915 sp_s->state = sp_dead; 916 cpu_spin_unlock(&sp_s->spinlock); 917 918 return TEE_ERROR_TARGET_DEAD; 919 } 920 921 sess = ts_pop_current_session(); 922 assert(sess == s); 923 924 bm_timestamp(); 925 926 return res; 927 } 928 929 /* We currently don't support 32 bits */ 930 #ifdef ARM64 931 static void sp_svc_store_registers(struct thread_svc_regs *regs, 932 struct thread_ctx_regs *sp_regs) 933 { 934 COMPILE_TIME_ASSERT(sizeof(sp_regs->x[0]) == sizeof(regs->x0)); 935 memcpy(sp_regs->x, ®s->x0, 31 * sizeof(regs->x0)); 936 sp_regs->pc = regs->elr; 937 sp_regs->sp = regs->sp_el0; 938 } 939 #endif 940 941 static bool sp_handle_svc(struct thread_svc_regs *regs) 942 { 943 struct ts_session *ts = ts_get_current_session(); 944 struct sp_ctx *uctx = to_sp_ctx(ts->ctx); 945 struct sp_session *s = uctx->open_session; 946 947 assert(s); 948 949 sp_svc_store_registers(regs, &uctx->sp_regs); 950 951 regs->x0 = 0; 952 regs->x1 = 0; /* panic */ 953 regs->x2 = 0; /* panic code */ 954 955 /* 956 * All the registers of the SP are saved in the SP session by the SVC 957 * handler. 958 * We always return to S-El1 after handling the SVC. We will continue 959 * in sp_enter_invoke_cmd() (return from __thread_enter_user_mode). 960 * The sp_enter() function copies the FF-A parameters (a0-a7) from the 961 * saved registers to the thread_smc_args. The thread_smc_args object is 962 * afterward used by the spmc_sp_msg_handler() to handle the 963 * FF-A message send by the SP. 964 */ 965 return false; 966 } 967 968 static void sp_dump_state(struct ts_ctx *ctx) 969 { 970 struct sp_ctx *utc = to_sp_ctx(ctx); 971 972 if (utc->uctx.dump_entry_func) { 973 TEE_Result res = ldelf_dump_state(&utc->uctx); 974 975 if (!res || res == TEE_ERROR_TARGET_DEAD) 976 return; 977 } 978 979 user_mode_ctx_print_mappings(&utc->uctx); 980 } 981 982 /* 983 * Note: this variable is weak just to ease breaking its dependency chain 984 * when added to the unpaged area. 985 */ 986 const struct ts_ops sp_ops __weak __relrodata_unpaged("sp_ops") = { 987 .enter_invoke_cmd = sp_enter_invoke_cmd, 988 .handle_svc = sp_handle_svc, 989 .dump_state = sp_dump_state, 990 }; 991 992 static TEE_Result sp_init_all(void) 993 { 994 TEE_Result res = TEE_SUCCESS; 995 const struct sp_image *sp = NULL; 996 char __maybe_unused msg[60] = { '\0', }; 997 998 for_each_secure_partition(sp) { 999 if (sp->image.uncompressed_size) 1000 snprintf(msg, sizeof(msg), 1001 " (compressed, uncompressed %u)", 1002 sp->image.uncompressed_size); 1003 else 1004 msg[0] = '\0'; 1005 DMSG("SP %pUl size %u%s", (void *)&sp->image.uuid, 1006 sp->image.size, msg); 1007 1008 res = sp_init_uuid(&sp->image.uuid, sp->fdt); 1009 1010 if (res != TEE_SUCCESS) { 1011 EMSG("Failed initializing SP(%pUl) err:%#"PRIx32, 1012 &sp->image.uuid, res); 1013 if (!IS_ENABLED(CFG_SP_SKIP_FAILED)) 1014 panic(); 1015 } 1016 } 1017 1018 return TEE_SUCCESS; 1019 } 1020 1021 boot_final(sp_init_all); 1022 1023 static TEE_Result secure_partition_open(const TEE_UUID *uuid, 1024 struct ts_store_handle **h) 1025 { 1026 return emb_ts_open(uuid, h, find_secure_partition); 1027 } 1028 1029 REGISTER_SP_STORE(2) = { 1030 .description = "SP store", 1031 .open = secure_partition_open, 1032 .get_size = emb_ts_get_size, 1033 .get_tag = emb_ts_get_tag, 1034 .read = emb_ts_read, 1035 .close = emb_ts_close, 1036 }; 1037