1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2020-2023, Arm Limited. 4 */ 5 #include <bench.h> 6 #include <crypto/crypto.h> 7 #include <initcall.h> 8 #include <kernel/boot.h> 9 #include <kernel/embedded_ts.h> 10 #include <kernel/ldelf_loader.h> 11 #include <kernel/secure_partition.h> 12 #include <kernel/spinlock.h> 13 #include <kernel/spmc_sp_handler.h> 14 #include <kernel/thread_private.h> 15 #include <kernel/thread_spmc.h> 16 #include <kernel/tpm.h> 17 #include <kernel/ts_store.h> 18 #include <ldelf.h> 19 #include <libfdt.h> 20 #include <mm/core_mmu.h> 21 #include <mm/fobj.h> 22 #include <mm/mobj.h> 23 #include <mm/vm.h> 24 #include <optee_ffa.h> 25 #include <stdio.h> 26 #include <string.h> 27 #include <tee_api_types.h> 28 #include <tee/uuid.h> 29 #include <trace.h> 30 #include <types_ext.h> 31 #include <utee_defines.h> 32 #include <util.h> 33 #include <zlib.h> 34 35 #define SP_MANIFEST_ATTR_READ BIT(0) 36 #define SP_MANIFEST_ATTR_WRITE BIT(1) 37 #define SP_MANIFEST_ATTR_EXEC BIT(2) 38 #define SP_MANIFEST_ATTR_NSEC BIT(3) 39 40 #define SP_MANIFEST_ATTR_RO (SP_MANIFEST_ATTR_READ) 41 #define SP_MANIFEST_ATTR_RW (SP_MANIFEST_ATTR_READ | \ 42 SP_MANIFEST_ATTR_WRITE) 43 #define SP_MANIFEST_ATTR_RX (SP_MANIFEST_ATTR_READ | \ 44 SP_MANIFEST_ATTR_EXEC) 45 #define SP_MANIFEST_ATTR_RWX (SP_MANIFEST_ATTR_READ | \ 46 SP_MANIFEST_ATTR_WRITE | \ 47 SP_MANIFEST_ATTR_EXEC) 48 49 #define SP_MANIFEST_FLAG_NOBITS BIT(0) 50 51 #define SP_MANIFEST_NS_INT_QUEUED (0x0) 52 #define SP_MANIFEST_NS_INT_MANAGED_EXIT (0x1) 53 #define SP_MANIFEST_NS_INT_SIGNALED (0x2) 54 55 #define SP_PKG_HEADER_MAGIC (0x474b5053) 56 #define SP_PKG_HEADER_VERSION_V1 (0x1) 57 #define SP_PKG_HEADER_VERSION_V2 (0x2) 58 59 struct sp_pkg_header { 60 uint32_t magic; 61 uint32_t version; 62 uint32_t pm_offset; 63 uint32_t pm_size; 64 uint32_t img_offset; 65 uint32_t img_size; 66 }; 67 68 struct fip_sp_head fip_sp_list = STAILQ_HEAD_INITIALIZER(fip_sp_list); 69 70 static const struct ts_ops sp_ops; 71 72 /* List that holds all of the loaded SP's */ 73 static struct sp_sessions_head open_sp_sessions = 74 TAILQ_HEAD_INITIALIZER(open_sp_sessions); 75 76 static const struct embedded_ts *find_secure_partition(const TEE_UUID *uuid) 77 { 78 const struct sp_image *sp = NULL; 79 const struct fip_sp *fip_sp = NULL; 80 81 for_each_secure_partition(sp) { 82 if (!memcmp(&sp->image.uuid, uuid, sizeof(*uuid))) 83 return &sp->image; 84 } 85 86 for_each_fip_sp(fip_sp) { 87 if (!memcmp(&fip_sp->sp_img.image.uuid, uuid, sizeof(*uuid))) 88 return &fip_sp->sp_img.image; 89 } 90 91 return NULL; 92 } 93 94 bool is_sp_ctx(struct ts_ctx *ctx) 95 { 96 return ctx && (ctx->ops == &sp_ops); 97 } 98 99 static void set_sp_ctx_ops(struct ts_ctx *ctx) 100 { 101 ctx->ops = &sp_ops; 102 } 103 104 struct sp_session *sp_get_session(uint32_t session_id) 105 { 106 struct sp_session *s = NULL; 107 108 TAILQ_FOREACH(s, &open_sp_sessions, link) { 109 if (s->endpoint_id == session_id) 110 return s; 111 } 112 113 return NULL; 114 } 115 116 TEE_Result sp_partition_info_get(uint32_t ffa_vers, void *buf, size_t buf_size, 117 const TEE_UUID *ffa_uuid, size_t *elem_count, 118 bool count_only) 119 { 120 TEE_Result res = TEE_SUCCESS; 121 uint32_t part_props = FFA_PART_PROP_DIRECT_REQ_RECV | 122 FFA_PART_PROP_DIRECT_REQ_SEND; 123 struct sp_session *s = NULL; 124 125 TAILQ_FOREACH(s, &open_sp_sessions, link) { 126 if (ffa_uuid && 127 memcmp(&s->ffa_uuid, ffa_uuid, sizeof(*ffa_uuid))) 128 continue; 129 130 if (s->state == sp_dead) 131 continue; 132 if (!count_only && !res) { 133 uint32_t uuid_words[4] = { 0 }; 134 135 tee_uuid_to_octets((uint8_t *)uuid_words, &s->ffa_uuid); 136 res = spmc_fill_partition_entry(ffa_vers, buf, buf_size, 137 *elem_count, 138 s->endpoint_id, 1, 139 part_props, uuid_words); 140 } 141 *elem_count += 1; 142 } 143 144 return res; 145 } 146 147 bool sp_has_exclusive_access(struct sp_mem_map_region *mem, 148 struct user_mode_ctx *uctx) 149 { 150 /* 151 * Check that we have access to the region if it is supposed to be 152 * mapped to the current context. 153 */ 154 if (uctx) { 155 struct vm_region *region = NULL; 156 157 /* Make sure that each mobj belongs to the SP */ 158 TAILQ_FOREACH(region, &uctx->vm_info.regions, link) { 159 if (region->mobj == mem->mobj) 160 break; 161 } 162 163 if (!region) 164 return false; 165 } 166 167 /* Check that it is not shared with another SP */ 168 return !sp_mem_is_shared(mem); 169 } 170 171 static uint16_t new_session_id(struct sp_sessions_head *open_sessions) 172 { 173 struct sp_session *last = NULL; 174 uint16_t id = SPMC_ENDPOINT_ID + 1; 175 176 last = TAILQ_LAST(open_sessions, sp_sessions_head); 177 if (last) 178 id = last->endpoint_id + 1; 179 180 assert(id > SPMC_ENDPOINT_ID); 181 return id; 182 } 183 184 static TEE_Result sp_create_ctx(const TEE_UUID *bin_uuid, struct sp_session *s) 185 { 186 TEE_Result res = TEE_SUCCESS; 187 struct sp_ctx *spc = NULL; 188 189 /* Register context */ 190 spc = calloc(1, sizeof(struct sp_ctx)); 191 if (!spc) 192 return TEE_ERROR_OUT_OF_MEMORY; 193 194 spc->open_session = s; 195 s->ts_sess.ctx = &spc->ts_ctx; 196 spc->ts_ctx.uuid = *bin_uuid; 197 198 res = vm_info_init(&spc->uctx, &spc->ts_ctx); 199 if (res) 200 goto err; 201 202 set_sp_ctx_ops(&spc->ts_ctx); 203 204 return TEE_SUCCESS; 205 206 err: 207 free(spc); 208 return res; 209 } 210 211 static TEE_Result sp_create_session(struct sp_sessions_head *open_sessions, 212 const TEE_UUID *bin_uuid, 213 struct sp_session **sess) 214 { 215 TEE_Result res = TEE_SUCCESS; 216 struct sp_session *s = calloc(1, sizeof(struct sp_session)); 217 218 if (!s) 219 return TEE_ERROR_OUT_OF_MEMORY; 220 221 s->endpoint_id = new_session_id(open_sessions); 222 if (!s->endpoint_id) { 223 res = TEE_ERROR_OVERFLOW; 224 goto err; 225 } 226 227 DMSG("Loading Secure Partition %pUl", (void *)bin_uuid); 228 res = sp_create_ctx(bin_uuid, s); 229 if (res) 230 goto err; 231 232 TAILQ_INSERT_TAIL(open_sessions, s, link); 233 *sess = s; 234 return TEE_SUCCESS; 235 236 err: 237 free(s); 238 return res; 239 } 240 241 static TEE_Result sp_init_set_registers(struct sp_ctx *ctx) 242 { 243 struct thread_ctx_regs *sp_regs = &ctx->sp_regs; 244 245 memset(sp_regs, 0, sizeof(*sp_regs)); 246 sp_regs->sp = ctx->uctx.stack_ptr; 247 sp_regs->pc = ctx->uctx.entry_func; 248 249 return TEE_SUCCESS; 250 } 251 252 TEE_Result sp_map_shared(struct sp_session *s, 253 struct sp_mem_receiver *receiver, 254 struct sp_mem *smem, 255 uint64_t *va) 256 { 257 TEE_Result res = TEE_SUCCESS; 258 struct sp_ctx *ctx = NULL; 259 uint32_t perm = TEE_MATTR_UR; 260 struct sp_mem_map_region *reg = NULL; 261 262 ctx = to_sp_ctx(s->ts_sess.ctx); 263 264 /* Get the permission */ 265 if (receiver->perm.perm & FFA_MEM_ACC_EXE) 266 perm |= TEE_MATTR_UX; 267 268 if (receiver->perm.perm & FFA_MEM_ACC_RW) { 269 if (receiver->perm.perm & FFA_MEM_ACC_EXE) 270 return TEE_ERROR_ACCESS_CONFLICT; 271 272 perm |= TEE_MATTR_UW; 273 } 274 /* 275 * Currently we don't support passing a va. We can't guarantee that the 276 * full region will be mapped in a contiguous region. A smem->region can 277 * have multiple mobj for one share. Currently there doesn't seem to be 278 * an option to guarantee that these will be mapped in a contiguous va 279 * space. 280 */ 281 if (*va) 282 return TEE_ERROR_NOT_SUPPORTED; 283 284 SLIST_FOREACH(reg, &smem->regions, link) { 285 res = vm_map(&ctx->uctx, va, reg->page_count * SMALL_PAGE_SIZE, 286 perm, 0, reg->mobj, reg->page_offset); 287 288 if (res != TEE_SUCCESS) { 289 EMSG("Failed to map memory region %#"PRIx32, res); 290 return res; 291 } 292 } 293 return TEE_SUCCESS; 294 } 295 296 TEE_Result sp_unmap_ffa_regions(struct sp_session *s, struct sp_mem *smem) 297 { 298 TEE_Result res = TEE_SUCCESS; 299 vaddr_t vaddr = 0; 300 size_t len = 0; 301 struct sp_ctx *ctx = to_sp_ctx(s->ts_sess.ctx); 302 struct sp_mem_map_region *reg = NULL; 303 304 SLIST_FOREACH(reg, &smem->regions, link) { 305 vaddr = (vaddr_t)sp_mem_get_va(&ctx->uctx, reg->page_offset, 306 reg->mobj); 307 len = reg->page_count * SMALL_PAGE_SIZE; 308 309 res = vm_unmap(&ctx->uctx, vaddr, len); 310 if (res != TEE_SUCCESS) 311 return res; 312 } 313 314 return TEE_SUCCESS; 315 } 316 317 static TEE_Result sp_dt_get_u64(const void *fdt, int node, const char *property, 318 uint64_t *value) 319 { 320 const fdt64_t *p = NULL; 321 int len = 0; 322 323 p = fdt_getprop(fdt, node, property, &len); 324 if (!p) 325 return TEE_ERROR_ITEM_NOT_FOUND; 326 327 if (len != sizeof(*p)) 328 return TEE_ERROR_BAD_FORMAT; 329 330 *value = fdt64_ld(p); 331 332 return TEE_SUCCESS; 333 } 334 335 static TEE_Result sp_dt_get_u32(const void *fdt, int node, const char *property, 336 uint32_t *value) 337 { 338 const fdt32_t *p = NULL; 339 int len = 0; 340 341 p = fdt_getprop(fdt, node, property, &len); 342 if (!p) 343 return TEE_ERROR_ITEM_NOT_FOUND; 344 345 if (len != sizeof(*p)) 346 return TEE_ERROR_BAD_FORMAT; 347 348 *value = fdt32_to_cpu(*p); 349 350 return TEE_SUCCESS; 351 } 352 353 static TEE_Result sp_dt_get_uuid(const void *fdt, int node, 354 const char *property, TEE_UUID *uuid) 355 { 356 uint32_t uuid_array[4] = { 0 }; 357 const fdt32_t *p = NULL; 358 int len = 0; 359 int i = 0; 360 361 p = fdt_getprop(fdt, node, property, &len); 362 if (!p) 363 return TEE_ERROR_ITEM_NOT_FOUND; 364 365 if (len != sizeof(TEE_UUID)) 366 return TEE_ERROR_BAD_FORMAT; 367 368 for (i = 0; i < 4; i++) 369 uuid_array[i] = fdt32_to_cpu(p[i]); 370 371 tee_uuid_from_octets(uuid, (uint8_t *)uuid_array); 372 373 return TEE_SUCCESS; 374 } 375 376 static TEE_Result sp_is_elf_format(const void *fdt, int sp_node, 377 bool *is_elf_format) 378 { 379 TEE_Result res = TEE_SUCCESS; 380 uint32_t elf_format = 0; 381 382 res = sp_dt_get_u32(fdt, sp_node, "elf-format", &elf_format); 383 if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND) 384 return res; 385 386 *is_elf_format = (elf_format != 0); 387 388 return TEE_SUCCESS; 389 } 390 391 static TEE_Result sp_binary_open(const TEE_UUID *uuid, 392 const struct ts_store_ops **ops, 393 struct ts_store_handle **handle) 394 { 395 TEE_Result res = TEE_ERROR_ITEM_NOT_FOUND; 396 397 SCATTERED_ARRAY_FOREACH(*ops, sp_stores, struct ts_store_ops) { 398 res = (*ops)->open(uuid, handle); 399 if (res != TEE_ERROR_ITEM_NOT_FOUND && 400 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 401 break; 402 } 403 404 return res; 405 } 406 407 static TEE_Result load_binary_sp(struct ts_session *s, 408 struct user_mode_ctx *uctx) 409 { 410 size_t bin_size = 0, bin_size_rounded = 0, bin_page_count = 0; 411 const struct ts_store_ops *store_ops = NULL; 412 struct ts_store_handle *handle = NULL; 413 TEE_Result res = TEE_SUCCESS; 414 tee_mm_entry_t *mm = NULL; 415 struct mobj *mobj = NULL; 416 uaddr_t base_addr = 0; 417 uint32_t vm_flags = 0; 418 unsigned int idx = 0; 419 vaddr_t va = 0; 420 421 if (!s || !uctx) 422 return TEE_ERROR_BAD_PARAMETERS; 423 424 DMSG("Loading raw binary format SP %pUl", &uctx->ts_ctx->uuid); 425 426 vm_set_ctx(uctx->ts_ctx); 427 428 /* Find TS store and open SP binary */ 429 res = sp_binary_open(&uctx->ts_ctx->uuid, &store_ops, &handle); 430 if (res != TEE_SUCCESS) { 431 EMSG("Failed to open SP binary"); 432 return res; 433 } 434 435 /* Query binary size and calculate page count */ 436 res = store_ops->get_size(handle, &bin_size); 437 if (res != TEE_SUCCESS) 438 goto err; 439 440 if (ROUNDUP_OVERFLOW(bin_size, SMALL_PAGE_SIZE, &bin_size_rounded)) { 441 res = TEE_ERROR_OVERFLOW; 442 goto err; 443 } 444 445 bin_page_count = bin_size_rounded / SMALL_PAGE_SIZE; 446 447 /* Allocate memory */ 448 mm = tee_mm_alloc(&tee_mm_sec_ddr, bin_size_rounded); 449 if (!mm) { 450 res = TEE_ERROR_OUT_OF_MEMORY; 451 goto err; 452 } 453 454 base_addr = tee_mm_get_smem(mm); 455 456 /* Create mobj */ 457 mobj = sp_mem_new_mobj(bin_page_count, TEE_MATTR_MEM_TYPE_CACHED, true); 458 if (!mobj) { 459 res = TEE_ERROR_OUT_OF_MEMORY; 460 goto err_free_tee_mm; 461 } 462 463 res = sp_mem_add_pages(mobj, &idx, base_addr, bin_page_count); 464 if (res) 465 goto err_free_mobj; 466 467 /* Map memory area for the SP binary */ 468 res = vm_map(uctx, &va, bin_size_rounded, TEE_MATTR_URWX, 469 vm_flags, mobj, 0); 470 if (res) 471 goto err_free_mobj; 472 473 /* Read SP binary into the previously mapped memory area */ 474 res = store_ops->read(handle, (void *)va, bin_size); 475 if (res) 476 goto err_unmap; 477 478 /* Set memory protection to allow execution */ 479 res = vm_set_prot(uctx, va, bin_size_rounded, TEE_MATTR_UX); 480 if (res) 481 goto err_unmap; 482 483 mobj_put(mobj); 484 store_ops->close(handle); 485 486 /* The entry point must be at the beginning of the SP binary. */ 487 uctx->entry_func = va; 488 uctx->load_addr = va; 489 uctx->is_32bit = false; 490 491 s->handle_scall = s->ctx->ops->handle_scall; 492 493 return TEE_SUCCESS; 494 495 err_unmap: 496 vm_unmap(uctx, va, bin_size_rounded); 497 498 err_free_mobj: 499 mobj_put(mobj); 500 501 err_free_tee_mm: 502 tee_mm_free(mm); 503 504 err: 505 store_ops->close(handle); 506 507 return res; 508 } 509 510 static TEE_Result sp_open_session(struct sp_session **sess, 511 struct sp_sessions_head *open_sessions, 512 const TEE_UUID *ffa_uuid, 513 const TEE_UUID *bin_uuid, 514 const void *fdt) 515 { 516 TEE_Result res = TEE_SUCCESS; 517 struct sp_session *s = NULL; 518 struct sp_ctx *ctx = NULL; 519 bool is_elf_format = false; 520 521 if (!find_secure_partition(bin_uuid)) 522 return TEE_ERROR_ITEM_NOT_FOUND; 523 524 res = sp_create_session(open_sessions, bin_uuid, &s); 525 if (res != TEE_SUCCESS) { 526 DMSG("sp_create_session failed %#"PRIx32, res); 527 return res; 528 } 529 530 ctx = to_sp_ctx(s->ts_sess.ctx); 531 assert(ctx); 532 if (!ctx) 533 return TEE_ERROR_TARGET_DEAD; 534 *sess = s; 535 536 ts_push_current_session(&s->ts_sess); 537 538 res = sp_is_elf_format(fdt, 0, &is_elf_format); 539 if (res == TEE_SUCCESS) { 540 if (is_elf_format) { 541 /* Load the SP using ldelf. */ 542 ldelf_load_ldelf(&ctx->uctx); 543 res = ldelf_init_with_ldelf(&s->ts_sess, &ctx->uctx); 544 } else { 545 /* Raw binary format SP */ 546 res = load_binary_sp(&s->ts_sess, &ctx->uctx); 547 } 548 } else { 549 EMSG("Failed to detect SP format"); 550 } 551 552 if (res != TEE_SUCCESS) { 553 EMSG("Failed loading SP %#"PRIx32, res); 554 ts_pop_current_session(); 555 return TEE_ERROR_TARGET_DEAD; 556 } 557 558 /* Make the SP ready for its first run */ 559 s->state = sp_idle; 560 s->caller_id = 0; 561 sp_init_set_registers(ctx); 562 memcpy(&s->ffa_uuid, ffa_uuid, sizeof(*ffa_uuid)); 563 ts_pop_current_session(); 564 565 return TEE_SUCCESS; 566 } 567 568 static TEE_Result fdt_get_uuid(const void * const fdt, TEE_UUID *uuid) 569 { 570 const struct fdt_property *description = NULL; 571 int description_name_len = 0; 572 573 if (fdt_node_check_compatible(fdt, 0, "arm,ffa-manifest-1.0")) { 574 EMSG("Failed loading SP, manifest not found"); 575 return TEE_ERROR_BAD_PARAMETERS; 576 } 577 578 description = fdt_get_property(fdt, 0, "description", 579 &description_name_len); 580 if (description) 581 DMSG("Loading SP: %s", description->data); 582 583 if (sp_dt_get_uuid(fdt, 0, "uuid", uuid)) { 584 EMSG("Missing or invalid UUID in SP manifest"); 585 return TEE_ERROR_BAD_FORMAT; 586 } 587 588 return TEE_SUCCESS; 589 } 590 591 /* 592 * sp_init_info allocates and maps the sp_ffa_init_info for the SP. It will copy 593 * the fdt into the allocated page(s) and return a pointer to the new location 594 * of the fdt. This pointer can be used to update data inside the fdt. 595 */ 596 static TEE_Result sp_init_info(struct sp_ctx *ctx, struct thread_smc_args *args, 597 const void * const input_fdt, vaddr_t *va, 598 size_t *num_pgs, void **fdt_copy) 599 { 600 struct sp_ffa_init_info *info = NULL; 601 int nvp_count = 1; 602 size_t total_size = ROUNDUP(CFG_SP_INIT_INFO_MAX_SIZE, SMALL_PAGE_SIZE); 603 size_t nvp_size = sizeof(struct sp_name_value_pair) * nvp_count; 604 size_t info_size = sizeof(*info) + nvp_size; 605 size_t fdt_size = total_size - info_size; 606 TEE_Result res = TEE_SUCCESS; 607 uint32_t perm = TEE_MATTR_URW | TEE_MATTR_PRW; 608 struct fobj *f = NULL; 609 struct mobj *m = NULL; 610 static const char fdt_name[16] = "TYPE_DT\0\0\0\0\0\0\0\0"; 611 612 *num_pgs = total_size / SMALL_PAGE_SIZE; 613 614 f = fobj_sec_mem_alloc(*num_pgs); 615 m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED); 616 617 fobj_put(f); 618 if (!m) 619 return TEE_ERROR_OUT_OF_MEMORY; 620 621 res = vm_map(&ctx->uctx, va, total_size, perm, 0, m, 0); 622 mobj_put(m); 623 if (res) 624 return res; 625 626 info = (struct sp_ffa_init_info *)*va; 627 628 /* magic field is 4 bytes, we don't copy /0 byte. */ 629 memcpy(&info->magic, "FF-A", 4); 630 info->count = nvp_count; 631 args->a0 = (vaddr_t)info; 632 633 /* 634 * Store the fdt after the boot_info and store the pointer in the 635 * first element. 636 */ 637 COMPILE_TIME_ASSERT(sizeof(info->nvp[0].name) == sizeof(fdt_name)); 638 memcpy(info->nvp[0].name, fdt_name, sizeof(fdt_name)); 639 info->nvp[0].value = *va + info_size; 640 info->nvp[0].size = fdt_size; 641 *fdt_copy = (void *)info->nvp[0].value; 642 643 if (fdt_open_into(input_fdt, *fdt_copy, fdt_size)) 644 return TEE_ERROR_GENERIC; 645 646 return TEE_SUCCESS; 647 } 648 649 static TEE_Result handle_fdt_load_relative_mem_regions(struct sp_ctx *ctx, 650 const void *fdt) 651 { 652 int node = 0; 653 int subnode = 0; 654 tee_mm_entry_t *mm = NULL; 655 TEE_Result res = TEE_SUCCESS; 656 657 /* 658 * Memory regions are optional in the SP manifest, it's not an error if 659 * we don't find any. 660 */ 661 node = fdt_node_offset_by_compatible(fdt, 0, 662 "arm,ffa-manifest-memory-regions"); 663 if (node < 0) 664 return TEE_SUCCESS; 665 666 fdt_for_each_subnode(subnode, fdt, node) { 667 uint64_t load_rel_offset = 0; 668 uint32_t attributes = 0; 669 uint64_t base_addr = 0; 670 uint32_t pages_cnt = 0; 671 uint32_t flags = 0; 672 uint32_t perm = 0; 673 size_t size = 0; 674 vaddr_t va = 0; 675 676 mm = NULL; 677 678 /* Load address relative offset of a memory region */ 679 if (!sp_dt_get_u64(fdt, subnode, "load-address-relative-offset", 680 &load_rel_offset)) { 681 va = ctx->uctx.load_addr + load_rel_offset; 682 } else { 683 /* Skip non load address relative memory regions */ 684 continue; 685 } 686 687 if (!sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) { 688 EMSG("Both base-address and load-address-relative-offset fields are set"); 689 return TEE_ERROR_BAD_FORMAT; 690 } 691 692 /* Size of memory region as count of 4K pages */ 693 if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) { 694 EMSG("Mandatory field is missing: pages-count"); 695 return TEE_ERROR_BAD_FORMAT; 696 } 697 698 if (MUL_OVERFLOW(pages_cnt, SMALL_PAGE_SIZE, &size)) 699 return TEE_ERROR_OVERFLOW; 700 701 /* Memory region attributes */ 702 if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) { 703 EMSG("Mandatory field is missing: attributes"); 704 return TEE_ERROR_BAD_FORMAT; 705 } 706 707 /* Check instruction and data access permissions */ 708 switch (attributes & SP_MANIFEST_ATTR_RWX) { 709 case SP_MANIFEST_ATTR_RO: 710 perm = TEE_MATTR_UR; 711 break; 712 case SP_MANIFEST_ATTR_RW: 713 perm = TEE_MATTR_URW; 714 break; 715 case SP_MANIFEST_ATTR_RX: 716 perm = TEE_MATTR_URX; 717 break; 718 default: 719 EMSG("Invalid memory access permissions"); 720 return TEE_ERROR_BAD_FORMAT; 721 } 722 723 res = sp_dt_get_u32(fdt, subnode, "load-flags", &flags); 724 if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND) { 725 EMSG("Optional field with invalid value: flags"); 726 return TEE_ERROR_BAD_FORMAT; 727 } 728 729 /* Load relative regions must be secure */ 730 if (attributes & SP_MANIFEST_ATTR_NSEC) { 731 EMSG("Invalid memory security attribute"); 732 return TEE_ERROR_BAD_FORMAT; 733 } 734 735 if (flags & SP_MANIFEST_FLAG_NOBITS) { 736 /* 737 * NOBITS flag is set, which means that loaded binary 738 * doesn't contain this area, so it's need to be 739 * allocated. 740 */ 741 struct mobj *m = NULL; 742 unsigned int idx = 0; 743 744 mm = tee_mm_alloc(&tee_mm_sec_ddr, size); 745 if (!mm) 746 return TEE_ERROR_OUT_OF_MEMORY; 747 748 base_addr = tee_mm_get_smem(mm); 749 750 m = sp_mem_new_mobj(pages_cnt, 751 TEE_MATTR_MEM_TYPE_CACHED, true); 752 if (!m) { 753 res = TEE_ERROR_OUT_OF_MEMORY; 754 goto err_mm_free; 755 } 756 757 res = sp_mem_add_pages(m, &idx, base_addr, pages_cnt); 758 if (res) { 759 mobj_put(m); 760 goto err_mm_free; 761 } 762 763 res = vm_map(&ctx->uctx, &va, size, perm, 0, m, 0); 764 mobj_put(m); 765 if (res) 766 goto err_mm_free; 767 } else { 768 /* 769 * If NOBITS is not present the memory area is already 770 * mapped and only need to set the correct permissions. 771 */ 772 res = vm_set_prot(&ctx->uctx, va, size, perm); 773 if (res) 774 return res; 775 } 776 } 777 778 return TEE_SUCCESS; 779 780 err_mm_free: 781 tee_mm_free(mm); 782 return res; 783 } 784 785 static TEE_Result handle_fdt_dev_regions(struct sp_ctx *ctx, void *fdt) 786 { 787 int node = 0; 788 int subnode = 0; 789 TEE_Result res = TEE_SUCCESS; 790 const char *dt_device_match_table = { 791 "arm,ffa-manifest-device-regions", 792 }; 793 794 /* 795 * Device regions are optional in the SP manifest, it's not an error if 796 * we don't find any 797 */ 798 node = fdt_node_offset_by_compatible(fdt, 0, dt_device_match_table); 799 if (node < 0) 800 return TEE_SUCCESS; 801 802 fdt_for_each_subnode(subnode, fdt, node) { 803 uint64_t base_addr = 0; 804 uint32_t pages_cnt = 0; 805 uint32_t attributes = 0; 806 struct mobj *m = NULL; 807 bool is_secure = true; 808 uint32_t perm = 0; 809 vaddr_t va = 0; 810 unsigned int idx = 0; 811 812 /* 813 * Physical base address of a device MMIO region. 814 * Currently only physically contiguous region is supported. 815 */ 816 if (sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) { 817 EMSG("Mandatory field is missing: base-address"); 818 return TEE_ERROR_BAD_FORMAT; 819 } 820 821 /* Total size of MMIO region as count of 4K pages */ 822 if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) { 823 EMSG("Mandatory field is missing: pages-count"); 824 return TEE_ERROR_BAD_FORMAT; 825 } 826 827 /* Data access, instruction access and security attributes */ 828 if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) { 829 EMSG("Mandatory field is missing: attributes"); 830 return TEE_ERROR_BAD_FORMAT; 831 } 832 833 /* Check instruction and data access permissions */ 834 switch (attributes & SP_MANIFEST_ATTR_RWX) { 835 case SP_MANIFEST_ATTR_RO: 836 perm = TEE_MATTR_UR; 837 break; 838 case SP_MANIFEST_ATTR_RW: 839 perm = TEE_MATTR_URW; 840 break; 841 default: 842 EMSG("Invalid memory access permissions"); 843 return TEE_ERROR_BAD_FORMAT; 844 } 845 846 /* 847 * The SP is a secure endpoint, security attribute can be 848 * secure or non-secure 849 */ 850 if (attributes & SP_MANIFEST_ATTR_NSEC) 851 is_secure = false; 852 853 /* Memory attributes must be Device-nGnRnE */ 854 m = sp_mem_new_mobj(pages_cnt, TEE_MATTR_MEM_TYPE_STRONGLY_O, 855 is_secure); 856 if (!m) 857 return TEE_ERROR_OUT_OF_MEMORY; 858 859 res = sp_mem_add_pages(m, &idx, (paddr_t)base_addr, pages_cnt); 860 if (res) { 861 mobj_put(m); 862 return res; 863 } 864 865 res = vm_map(&ctx->uctx, &va, pages_cnt * SMALL_PAGE_SIZE, 866 perm, 0, m, 0); 867 mobj_put(m); 868 if (res) 869 return res; 870 871 /* 872 * Overwrite the device region's PA in the fdt with the VA. This 873 * fdt will be passed to the SP. 874 */ 875 res = fdt_setprop_u64(fdt, subnode, "base-address", va); 876 877 /* 878 * Unmap the region if the overwrite failed since the SP won't 879 * be able to access it without knowing the VA. 880 */ 881 if (res) { 882 vm_unmap(&ctx->uctx, va, pages_cnt * SMALL_PAGE_SIZE); 883 return res; 884 } 885 } 886 887 return TEE_SUCCESS; 888 } 889 890 static TEE_Result swap_sp_endpoints(uint32_t endpoint_id, 891 uint32_t new_endpoint_id) 892 { 893 struct sp_session *session = sp_get_session(endpoint_id); 894 uint32_t manifest_endpoint_id = 0; 895 896 /* 897 * We don't know in which order the SPs are loaded. The endpoint ID 898 * defined in the manifest could already be generated by 899 * new_session_id() and used by another SP. If this is the case, we swap 900 * the ID's of the two SPs. We also have to make sure that the ID's are 901 * not defined twice in the manifest. 902 */ 903 904 /* The endpoint ID was not assigned yet */ 905 if (!session) 906 return TEE_SUCCESS; 907 908 /* 909 * Read the manifest file from the SP who originally had the endpoint. 910 * We can safely swap the endpoint ID's if the manifest file doesn't 911 * have an endpoint ID defined. 912 */ 913 if (!sp_dt_get_u32(session->fdt, 0, "id", &manifest_endpoint_id)) { 914 assert(manifest_endpoint_id == endpoint_id); 915 EMSG("SP: Found duplicated endpoint ID %#"PRIx32, endpoint_id); 916 return TEE_ERROR_ACCESS_CONFLICT; 917 } 918 919 session->endpoint_id = new_endpoint_id; 920 921 return TEE_SUCCESS; 922 } 923 924 static TEE_Result read_manifest_endpoint_id(struct sp_session *s) 925 { 926 uint32_t endpoint_id = 0; 927 928 /* 929 * The endpoint ID can be optionally defined in the manifest file. We 930 * have to map the ID inside the manifest to the SP if it's defined. 931 * If not, the endpoint ID generated inside new_session_id() will be 932 * used. 933 */ 934 if (!sp_dt_get_u32(s->fdt, 0, "id", &endpoint_id)) { 935 TEE_Result res = TEE_ERROR_GENERIC; 936 937 if (endpoint_id <= SPMC_ENDPOINT_ID) 938 return TEE_ERROR_BAD_FORMAT; 939 940 res = swap_sp_endpoints(endpoint_id, s->endpoint_id); 941 if (res) 942 return res; 943 944 DMSG("SP: endpoint ID (0x%"PRIx32") found in manifest", 945 endpoint_id); 946 /* Assign the endpoint ID to the current SP */ 947 s->endpoint_id = endpoint_id; 948 } 949 return TEE_SUCCESS; 950 } 951 952 static TEE_Result handle_fdt_mem_regions(struct sp_ctx *ctx, void *fdt) 953 { 954 int node = 0; 955 int subnode = 0; 956 tee_mm_entry_t *mm = NULL; 957 TEE_Result res = TEE_SUCCESS; 958 959 /* 960 * Memory regions are optional in the SP manifest, it's not an error if 961 * we don't find any. 962 */ 963 node = fdt_node_offset_by_compatible(fdt, 0, 964 "arm,ffa-manifest-memory-regions"); 965 if (node < 0) 966 return TEE_SUCCESS; 967 968 fdt_for_each_subnode(subnode, fdt, node) { 969 uint64_t load_rel_offset = 0; 970 bool alloc_needed = false; 971 uint32_t attributes = 0; 972 uint64_t base_addr = 0; 973 uint32_t pages_cnt = 0; 974 bool is_secure = true; 975 struct mobj *m = NULL; 976 unsigned int idx = 0; 977 uint32_t perm = 0; 978 size_t size = 0; 979 vaddr_t va = 0; 980 981 mm = NULL; 982 983 /* Load address relative offset of a memory region */ 984 if (!sp_dt_get_u64(fdt, subnode, "load-address-relative-offset", 985 &load_rel_offset)) { 986 /* 987 * At this point the memory region is already mapped by 988 * handle_fdt_load_relative_mem_regions. 989 * Only need to set the base-address in the manifest and 990 * then skip the rest of the mapping process. 991 */ 992 va = ctx->uctx.load_addr + load_rel_offset; 993 res = fdt_setprop_u64(fdt, subnode, "base-address", va); 994 if (res) 995 return res; 996 997 continue; 998 } 999 1000 /* 1001 * Base address of a memory region. 1002 * If not present, we have to allocate the specified memory. 1003 * If present, this field could specify a PA or VA. Currently 1004 * only a PA is supported. 1005 */ 1006 if (sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) 1007 alloc_needed = true; 1008 1009 /* Size of memory region as count of 4K pages */ 1010 if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) { 1011 EMSG("Mandatory field is missing: pages-count"); 1012 return TEE_ERROR_BAD_FORMAT; 1013 } 1014 1015 if (MUL_OVERFLOW(pages_cnt, SMALL_PAGE_SIZE, &size)) 1016 return TEE_ERROR_OVERFLOW; 1017 1018 /* 1019 * Memory region attributes: 1020 * - Instruction/data access permissions 1021 * - Cacheability/shareability attributes 1022 * - Security attributes 1023 * 1024 * Cacheability/shareability attributes can be ignored for now. 1025 * OP-TEE only supports a single type for normal cached memory 1026 * and currently there is no use case that would require to 1027 * change this. 1028 */ 1029 if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) { 1030 EMSG("Mandatory field is missing: attributes"); 1031 return TEE_ERROR_BAD_FORMAT; 1032 } 1033 1034 /* Check instruction and data access permissions */ 1035 switch (attributes & SP_MANIFEST_ATTR_RWX) { 1036 case SP_MANIFEST_ATTR_RO: 1037 perm = TEE_MATTR_UR; 1038 break; 1039 case SP_MANIFEST_ATTR_RW: 1040 perm = TEE_MATTR_URW; 1041 break; 1042 case SP_MANIFEST_ATTR_RX: 1043 perm = TEE_MATTR_URX; 1044 break; 1045 default: 1046 EMSG("Invalid memory access permissions"); 1047 return TEE_ERROR_BAD_FORMAT; 1048 } 1049 1050 /* 1051 * The SP is a secure endpoint, security attribute can be 1052 * secure or non-secure. 1053 * The SPMC cannot allocate non-secure memory, i.e. if the base 1054 * address is missing this attribute must be secure. 1055 */ 1056 if (attributes & SP_MANIFEST_ATTR_NSEC) { 1057 if (alloc_needed) { 1058 EMSG("Invalid memory security attribute"); 1059 return TEE_ERROR_BAD_FORMAT; 1060 } 1061 is_secure = false; 1062 } 1063 1064 if (alloc_needed) { 1065 /* Base address is missing, we have to allocate */ 1066 mm = tee_mm_alloc(&tee_mm_sec_ddr, size); 1067 if (!mm) 1068 return TEE_ERROR_OUT_OF_MEMORY; 1069 1070 base_addr = tee_mm_get_smem(mm); 1071 } 1072 1073 m = sp_mem_new_mobj(pages_cnt, TEE_MATTR_MEM_TYPE_CACHED, 1074 is_secure); 1075 if (!m) { 1076 res = TEE_ERROR_OUT_OF_MEMORY; 1077 goto err_mm_free; 1078 } 1079 1080 res = sp_mem_add_pages(m, &idx, base_addr, pages_cnt); 1081 if (res) { 1082 mobj_put(m); 1083 goto err_mm_free; 1084 } 1085 1086 res = vm_map(&ctx->uctx, &va, size, perm, 0, m, 0); 1087 mobj_put(m); 1088 if (res) 1089 goto err_mm_free; 1090 1091 /* 1092 * Overwrite the memory region's base address in the fdt with 1093 * the VA. This fdt will be passed to the SP. 1094 * If the base-address field was not present in the original 1095 * fdt, this function will create it. This doesn't cause issues 1096 * since the necessary extra space has been allocated when 1097 * opening the fdt. 1098 */ 1099 res = fdt_setprop_u64(fdt, subnode, "base-address", va); 1100 1101 /* 1102 * Unmap the region if the overwrite failed since the SP won't 1103 * be able to access it without knowing the VA. 1104 */ 1105 if (res) { 1106 vm_unmap(&ctx->uctx, va, size); 1107 goto err_mm_free; 1108 } 1109 } 1110 1111 return TEE_SUCCESS; 1112 1113 err_mm_free: 1114 tee_mm_free(mm); 1115 return res; 1116 } 1117 1118 static TEE_Result handle_tpm_event_log(struct sp_ctx *ctx, void *fdt) 1119 { 1120 uint32_t perm = TEE_MATTR_URW | TEE_MATTR_PRW; 1121 uint32_t dummy_size __maybe_unused = 0; 1122 TEE_Result res = TEE_SUCCESS; 1123 size_t page_count = 0; 1124 struct fobj *f = NULL; 1125 struct mobj *m = NULL; 1126 vaddr_t log_addr = 0; 1127 size_t log_size = 0; 1128 int node = 0; 1129 1130 node = fdt_node_offset_by_compatible(fdt, 0, "arm,tpm_event_log"); 1131 if (node < 0) 1132 return TEE_SUCCESS; 1133 1134 /* Checking the existence and size of the event log properties */ 1135 if (sp_dt_get_u64(fdt, node, "tpm_event_log_addr", &log_addr)) { 1136 EMSG("tpm_event_log_addr not found or has invalid size"); 1137 return TEE_ERROR_BAD_FORMAT; 1138 } 1139 1140 if (sp_dt_get_u32(fdt, node, "tpm_event_log_size", &dummy_size)) { 1141 EMSG("tpm_event_log_size not found or has invalid size"); 1142 return TEE_ERROR_BAD_FORMAT; 1143 } 1144 1145 /* Validating event log */ 1146 res = tpm_get_event_log_size(&log_size); 1147 if (res) 1148 return res; 1149 1150 if (!log_size) { 1151 EMSG("Empty TPM event log was provided"); 1152 return TEE_ERROR_ITEM_NOT_FOUND; 1153 } 1154 1155 /* Allocating memory area for the event log to share with the SP */ 1156 page_count = ROUNDUP_DIV(log_size, SMALL_PAGE_SIZE); 1157 1158 f = fobj_sec_mem_alloc(page_count); 1159 m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED); 1160 fobj_put(f); 1161 if (!m) 1162 return TEE_ERROR_OUT_OF_MEMORY; 1163 1164 res = vm_map(&ctx->uctx, &log_addr, log_size, perm, 0, m, 0); 1165 mobj_put(m); 1166 if (res) 1167 return res; 1168 1169 /* Copy event log */ 1170 res = tpm_get_event_log((void *)log_addr, &log_size); 1171 if (res) 1172 goto err_unmap; 1173 1174 /* Setting event log details in the manifest */ 1175 res = fdt_setprop_u64(fdt, node, "tpm_event_log_addr", log_addr); 1176 if (res) 1177 goto err_unmap; 1178 1179 res = fdt_setprop_u32(fdt, node, "tpm_event_log_size", log_size); 1180 if (res) 1181 goto err_unmap; 1182 1183 return TEE_SUCCESS; 1184 1185 err_unmap: 1186 vm_unmap(&ctx->uctx, log_addr, log_size); 1187 1188 return res; 1189 } 1190 1191 /* 1192 * Note: this function is called only on the primary CPU. It assumes that the 1193 * features present on the primary CPU are available on all of the secondary 1194 * CPUs as well. 1195 */ 1196 static TEE_Result handle_hw_features(void *fdt) 1197 { 1198 uint32_t val __maybe_unused = 0; 1199 TEE_Result res = TEE_SUCCESS; 1200 int node = 0; 1201 1202 /* 1203 * HW feature descriptions are optional in the SP manifest, it's not an 1204 * error if we don't find any. 1205 */ 1206 node = fdt_node_offset_by_compatible(fdt, 0, "arm,hw-features"); 1207 if (node < 0) 1208 return TEE_SUCCESS; 1209 1210 /* Modify the crc32 property only if it's already present */ 1211 if (!sp_dt_get_u32(fdt, node, "crc32", &val)) { 1212 res = fdt_setprop_u32(fdt, node, "crc32", 1213 feat_crc32_implemented()); 1214 if (res) 1215 return res; 1216 } 1217 1218 return TEE_SUCCESS; 1219 } 1220 1221 static TEE_Result read_ns_interrupts_action(const void *fdt, 1222 struct sp_session *s) 1223 { 1224 TEE_Result res = TEE_ERROR_BAD_PARAMETERS; 1225 1226 res = sp_dt_get_u32(fdt, 0, "ns-interrupts-action", &s->ns_int_mode); 1227 1228 if (res) { 1229 EMSG("Mandatory property is missing: ns-interrupts-action"); 1230 return res; 1231 } 1232 1233 switch (s->ns_int_mode) { 1234 case SP_MANIFEST_NS_INT_QUEUED: 1235 case SP_MANIFEST_NS_INT_SIGNALED: 1236 /* OK */ 1237 break; 1238 1239 case SP_MANIFEST_NS_INT_MANAGED_EXIT: 1240 EMSG("Managed exit is not implemented"); 1241 return TEE_ERROR_NOT_IMPLEMENTED; 1242 1243 default: 1244 EMSG("Invalid ns-interrupts-action value: %"PRIu32, 1245 s->ns_int_mode); 1246 return TEE_ERROR_BAD_PARAMETERS; 1247 } 1248 1249 return TEE_SUCCESS; 1250 } 1251 1252 static TEE_Result sp_init_uuid(const TEE_UUID *bin_uuid, const void * const fdt) 1253 { 1254 TEE_Result res = TEE_SUCCESS; 1255 struct sp_session *sess = NULL; 1256 TEE_UUID ffa_uuid = {}; 1257 1258 res = fdt_get_uuid(fdt, &ffa_uuid); 1259 if (res) 1260 return res; 1261 1262 res = sp_open_session(&sess, 1263 &open_sp_sessions, 1264 &ffa_uuid, bin_uuid, fdt); 1265 if (res) 1266 return res; 1267 1268 sess->fdt = fdt; 1269 res = read_manifest_endpoint_id(sess); 1270 if (res) 1271 return res; 1272 DMSG("endpoint is 0x%"PRIx16, sess->endpoint_id); 1273 1274 res = read_ns_interrupts_action(fdt, sess); 1275 if (res) 1276 return res; 1277 1278 return TEE_SUCCESS; 1279 } 1280 1281 static TEE_Result sp_first_run(struct sp_session *sess) 1282 { 1283 TEE_Result res = TEE_SUCCESS; 1284 struct thread_smc_args args = { }; 1285 vaddr_t va = 0; 1286 size_t num_pgs = 0; 1287 struct sp_ctx *ctx = NULL; 1288 void *fdt_copy = NULL; 1289 1290 ctx = to_sp_ctx(sess->ts_sess.ctx); 1291 ts_push_current_session(&sess->ts_sess); 1292 1293 /* 1294 * Load relative memory regions must be handled before doing any other 1295 * mapping to prevent conflicts in the VA space. 1296 */ 1297 res = handle_fdt_load_relative_mem_regions(ctx, sess->fdt); 1298 if (res) { 1299 ts_pop_current_session(); 1300 return res; 1301 } 1302 1303 res = sp_init_info(ctx, &args, sess->fdt, &va, &num_pgs, &fdt_copy); 1304 if (res) 1305 goto out; 1306 1307 res = handle_fdt_dev_regions(ctx, fdt_copy); 1308 if (res) 1309 goto out; 1310 1311 res = handle_fdt_mem_regions(ctx, fdt_copy); 1312 if (res) 1313 goto out; 1314 1315 if (IS_ENABLED(CFG_CORE_TPM_EVENT_LOG)) { 1316 res = handle_tpm_event_log(ctx, fdt_copy); 1317 if (res) 1318 goto out; 1319 } 1320 1321 res = handle_hw_features(fdt_copy); 1322 if (res) 1323 goto out; 1324 1325 ts_pop_current_session(); 1326 1327 sess->is_initialized = false; 1328 if (sp_enter(&args, sess)) { 1329 vm_unmap(&ctx->uctx, va, num_pgs); 1330 return FFA_ABORTED; 1331 } 1332 1333 spmc_sp_msg_handler(&args, sess); 1334 1335 sess->is_initialized = true; 1336 1337 ts_push_current_session(&sess->ts_sess); 1338 out: 1339 /* Free the boot info page from the SP memory */ 1340 vm_unmap(&ctx->uctx, va, num_pgs); 1341 ts_pop_current_session(); 1342 1343 return res; 1344 } 1345 1346 TEE_Result sp_enter(struct thread_smc_args *args, struct sp_session *sp) 1347 { 1348 TEE_Result res = FFA_OK; 1349 struct sp_ctx *ctx = to_sp_ctx(sp->ts_sess.ctx); 1350 1351 ctx->sp_regs.x[0] = args->a0; 1352 ctx->sp_regs.x[1] = args->a1; 1353 ctx->sp_regs.x[2] = args->a2; 1354 ctx->sp_regs.x[3] = args->a3; 1355 ctx->sp_regs.x[4] = args->a4; 1356 ctx->sp_regs.x[5] = args->a5; 1357 ctx->sp_regs.x[6] = args->a6; 1358 ctx->sp_regs.x[7] = args->a7; 1359 1360 res = sp->ts_sess.ctx->ops->enter_invoke_cmd(&sp->ts_sess, 0); 1361 1362 args->a0 = ctx->sp_regs.x[0]; 1363 args->a1 = ctx->sp_regs.x[1]; 1364 args->a2 = ctx->sp_regs.x[2]; 1365 args->a3 = ctx->sp_regs.x[3]; 1366 args->a4 = ctx->sp_regs.x[4]; 1367 args->a5 = ctx->sp_regs.x[5]; 1368 args->a6 = ctx->sp_regs.x[6]; 1369 args->a7 = ctx->sp_regs.x[7]; 1370 1371 return res; 1372 } 1373 1374 /* 1375 * According to FF-A v1.1 section 8.3.1.4 if a caller requires less permissive 1376 * active on NS interrupt than the callee, the callee must inherit the caller's 1377 * configuration. 1378 * Each SP's own NS action setting is stored in ns_int_mode. The effective 1379 * action will be MIN([self action], [caller's action]) which is stored in the 1380 * ns_int_mode_inherited field. 1381 */ 1382 static void sp_cpsr_configure_foreign_interrupts(struct sp_session *s, 1383 struct ts_session *caller, 1384 uint64_t *cpsr) 1385 { 1386 if (caller) { 1387 struct sp_session *caller_sp = to_sp_session(caller); 1388 1389 s->ns_int_mode_inherited = MIN(caller_sp->ns_int_mode_inherited, 1390 s->ns_int_mode); 1391 } else { 1392 s->ns_int_mode_inherited = s->ns_int_mode; 1393 } 1394 1395 if (s->ns_int_mode_inherited == SP_MANIFEST_NS_INT_QUEUED) 1396 *cpsr |= SHIFT_U32(THREAD_EXCP_FOREIGN_INTR, 1397 ARM32_CPSR_F_SHIFT); 1398 else 1399 *cpsr &= ~SHIFT_U32(THREAD_EXCP_FOREIGN_INTR, 1400 ARM32_CPSR_F_SHIFT); 1401 } 1402 1403 static TEE_Result sp_enter_invoke_cmd(struct ts_session *s, 1404 uint32_t cmd __unused) 1405 { 1406 struct sp_ctx *ctx = to_sp_ctx(s->ctx); 1407 TEE_Result res = TEE_SUCCESS; 1408 uint32_t exceptions = 0; 1409 struct sp_session *sp_s = to_sp_session(s); 1410 struct ts_session *sess = NULL; 1411 struct thread_ctx_regs *sp_regs = NULL; 1412 uint32_t thread_id = THREAD_ID_INVALID; 1413 struct ts_session *caller = NULL; 1414 uint32_t rpc_target_info = 0; 1415 uint32_t panicked = false; 1416 uint32_t panic_code = 0; 1417 1418 bm_timestamp(); 1419 1420 sp_regs = &ctx->sp_regs; 1421 ts_push_current_session(s); 1422 1423 exceptions = thread_mask_exceptions(THREAD_EXCP_ALL); 1424 1425 /* Enable/disable foreign interrupts in CPSR/SPSR */ 1426 caller = ts_get_calling_session(); 1427 sp_cpsr_configure_foreign_interrupts(sp_s, caller, &sp_regs->cpsr); 1428 1429 /* 1430 * Store endpoint ID and thread ID in rpc_target_info. This will be used 1431 * as w1 in FFA_INTERRUPT in case of a foreign interrupt. 1432 */ 1433 rpc_target_info = thread_get_tsd()->rpc_target_info; 1434 thread_id = thread_get_id(); 1435 assert(thread_id <= UINT16_MAX); 1436 thread_get_tsd()->rpc_target_info = 1437 FFA_TARGET_INFO_SET(sp_s->endpoint_id, thread_id); 1438 1439 __thread_enter_user_mode(sp_regs, &panicked, &panic_code); 1440 1441 /* Restore rpc_target_info */ 1442 thread_get_tsd()->rpc_target_info = rpc_target_info; 1443 1444 thread_unmask_exceptions(exceptions); 1445 1446 thread_user_clear_vfp(&ctx->uctx); 1447 1448 if (panicked) { 1449 DMSG("SP panicked with code %#"PRIx32, panic_code); 1450 abort_print_current_ts(); 1451 1452 sess = ts_pop_current_session(); 1453 cpu_spin_lock(&sp_s->spinlock); 1454 sp_s->state = sp_dead; 1455 cpu_spin_unlock(&sp_s->spinlock); 1456 1457 return TEE_ERROR_TARGET_DEAD; 1458 } 1459 1460 sess = ts_pop_current_session(); 1461 assert(sess == s); 1462 1463 bm_timestamp(); 1464 1465 return res; 1466 } 1467 1468 /* We currently don't support 32 bits */ 1469 #ifdef ARM64 1470 static void sp_svc_store_registers(struct thread_scall_regs *regs, 1471 struct thread_ctx_regs *sp_regs) 1472 { 1473 COMPILE_TIME_ASSERT(sizeof(sp_regs->x[0]) == sizeof(regs->x0)); 1474 memcpy(sp_regs->x, ®s->x0, 31 * sizeof(regs->x0)); 1475 sp_regs->pc = regs->elr; 1476 sp_regs->sp = regs->sp_el0; 1477 } 1478 #endif 1479 1480 static bool sp_handle_scall(struct thread_scall_regs *regs) 1481 { 1482 struct ts_session *ts = ts_get_current_session(); 1483 struct sp_ctx *uctx = to_sp_ctx(ts->ctx); 1484 struct sp_session *s = uctx->open_session; 1485 1486 assert(s); 1487 1488 sp_svc_store_registers(regs, &uctx->sp_regs); 1489 1490 regs->x0 = 0; 1491 regs->x1 = 0; /* panic */ 1492 regs->x2 = 0; /* panic code */ 1493 1494 /* 1495 * All the registers of the SP are saved in the SP session by the SVC 1496 * handler. 1497 * We always return to S-El1 after handling the SVC. We will continue 1498 * in sp_enter_invoke_cmd() (return from __thread_enter_user_mode). 1499 * The sp_enter() function copies the FF-A parameters (a0-a7) from the 1500 * saved registers to the thread_smc_args. The thread_smc_args object is 1501 * afterward used by the spmc_sp_msg_handler() to handle the 1502 * FF-A message send by the SP. 1503 */ 1504 return false; 1505 } 1506 1507 static void sp_dump_state(struct ts_ctx *ctx) 1508 { 1509 struct sp_ctx *utc = to_sp_ctx(ctx); 1510 1511 if (utc->uctx.dump_entry_func) { 1512 TEE_Result res = ldelf_dump_state(&utc->uctx); 1513 1514 if (!res || res == TEE_ERROR_TARGET_DEAD) 1515 return; 1516 } 1517 1518 user_mode_ctx_print_mappings(&utc->uctx); 1519 } 1520 1521 static const struct ts_ops sp_ops = { 1522 .enter_invoke_cmd = sp_enter_invoke_cmd, 1523 .handle_scall = sp_handle_scall, 1524 .dump_state = sp_dump_state, 1525 }; 1526 1527 static TEE_Result process_sp_pkg(uint64_t sp_pkg_pa, TEE_UUID *sp_uuid) 1528 { 1529 enum teecore_memtypes mtype = MEM_AREA_TA_RAM; 1530 struct sp_pkg_header *sp_pkg_hdr = NULL; 1531 struct fip_sp *sp = NULL; 1532 uint64_t sp_fdt_end = 0; 1533 size_t sp_pkg_size = 0; 1534 vaddr_t sp_pkg_va = 0; 1535 1536 /* Process the first page which contains the SP package header */ 1537 sp_pkg_va = (vaddr_t)phys_to_virt(sp_pkg_pa, mtype, SMALL_PAGE_SIZE); 1538 if (!sp_pkg_va) { 1539 EMSG("Cannot find mapping for PA %#" PRIxPA, sp_pkg_pa); 1540 return TEE_ERROR_GENERIC; 1541 } 1542 1543 sp_pkg_hdr = (struct sp_pkg_header *)sp_pkg_va; 1544 1545 if (sp_pkg_hdr->magic != SP_PKG_HEADER_MAGIC) { 1546 EMSG("Invalid SP package magic"); 1547 return TEE_ERROR_BAD_FORMAT; 1548 } 1549 1550 if (sp_pkg_hdr->version != SP_PKG_HEADER_VERSION_V1 && 1551 sp_pkg_hdr->version != SP_PKG_HEADER_VERSION_V2) { 1552 EMSG("Invalid SP header version"); 1553 return TEE_ERROR_BAD_FORMAT; 1554 } 1555 1556 if (ADD_OVERFLOW(sp_pkg_hdr->img_offset, sp_pkg_hdr->img_size, 1557 &sp_pkg_size)) { 1558 EMSG("Invalid SP package size"); 1559 return TEE_ERROR_BAD_FORMAT; 1560 } 1561 1562 if (ADD_OVERFLOW(sp_pkg_hdr->pm_offset, sp_pkg_hdr->pm_size, 1563 &sp_fdt_end) || sp_fdt_end > sp_pkg_hdr->img_offset) { 1564 EMSG("Invalid SP manifest size"); 1565 return TEE_ERROR_BAD_FORMAT; 1566 } 1567 1568 /* Process the whole SP package now that the size is known */ 1569 sp_pkg_va = (vaddr_t)phys_to_virt(sp_pkg_pa, mtype, sp_pkg_size); 1570 if (!sp_pkg_va) { 1571 EMSG("Cannot find mapping for PA %#" PRIxPA, sp_pkg_pa); 1572 return TEE_ERROR_GENERIC; 1573 } 1574 1575 sp_pkg_hdr = (struct sp_pkg_header *)sp_pkg_va; 1576 1577 sp = calloc(1, sizeof(struct fip_sp)); 1578 if (!sp) 1579 return TEE_ERROR_OUT_OF_MEMORY; 1580 1581 memcpy(&sp->sp_img.image.uuid, sp_uuid, sizeof(*sp_uuid)); 1582 sp->sp_img.image.ts = (uint8_t *)(sp_pkg_va + sp_pkg_hdr->img_offset); 1583 sp->sp_img.image.size = sp_pkg_hdr->img_size; 1584 sp->sp_img.image.flags = 0; 1585 sp->sp_img.fdt = (uint8_t *)(sp_pkg_va + sp_pkg_hdr->pm_offset); 1586 1587 STAILQ_INSERT_TAIL(&fip_sp_list, sp, link); 1588 1589 return TEE_SUCCESS; 1590 } 1591 1592 static TEE_Result fip_sp_init_all(void) 1593 { 1594 TEE_Result res = TEE_SUCCESS; 1595 uint64_t sp_pkg_addr = 0; 1596 const void *fdt = NULL; 1597 TEE_UUID sp_uuid = { }; 1598 int sp_pkgs_node = 0; 1599 int subnode = 0; 1600 int root = 0; 1601 1602 fdt = get_tos_fw_config_dt(); 1603 if (!fdt) { 1604 EMSG("No SPMC manifest found"); 1605 return TEE_ERROR_GENERIC; 1606 } 1607 1608 root = fdt_path_offset(fdt, "/"); 1609 if (root < 0) 1610 return TEE_ERROR_BAD_FORMAT; 1611 1612 if (fdt_node_check_compatible(fdt, root, "arm,ffa-core-manifest-1.0")) 1613 return TEE_ERROR_BAD_FORMAT; 1614 1615 /* SP packages are optional, it's not an error if we don't find any */ 1616 sp_pkgs_node = fdt_node_offset_by_compatible(fdt, root, "arm,sp_pkg"); 1617 if (sp_pkgs_node < 0) 1618 return TEE_SUCCESS; 1619 1620 fdt_for_each_subnode(subnode, fdt, sp_pkgs_node) { 1621 res = sp_dt_get_u64(fdt, subnode, "load-address", &sp_pkg_addr); 1622 if (res) { 1623 EMSG("Invalid FIP SP load address"); 1624 return res; 1625 } 1626 1627 res = sp_dt_get_uuid(fdt, subnode, "uuid", &sp_uuid); 1628 if (res) { 1629 EMSG("Invalid FIP SP uuid"); 1630 return res; 1631 } 1632 1633 res = process_sp_pkg(sp_pkg_addr, &sp_uuid); 1634 if (res) { 1635 EMSG("Invalid FIP SP package"); 1636 return res; 1637 } 1638 } 1639 1640 return TEE_SUCCESS; 1641 } 1642 1643 static void fip_sp_deinit_all(void) 1644 { 1645 while (!STAILQ_EMPTY(&fip_sp_list)) { 1646 struct fip_sp *sp = STAILQ_FIRST(&fip_sp_list); 1647 1648 STAILQ_REMOVE_HEAD(&fip_sp_list, link); 1649 free(sp); 1650 } 1651 } 1652 1653 static TEE_Result sp_init_all(void) 1654 { 1655 TEE_Result res = TEE_SUCCESS; 1656 const struct sp_image *sp = NULL; 1657 const struct fip_sp *fip_sp = NULL; 1658 char __maybe_unused msg[60] = { '\0', }; 1659 struct sp_session *s = NULL; 1660 1661 for_each_secure_partition(sp) { 1662 if (sp->image.uncompressed_size) 1663 snprintf(msg, sizeof(msg), 1664 " (compressed, uncompressed %u)", 1665 sp->image.uncompressed_size); 1666 else 1667 msg[0] = '\0'; 1668 DMSG("SP %pUl size %u%s", (void *)&sp->image.uuid, 1669 sp->image.size, msg); 1670 1671 res = sp_init_uuid(&sp->image.uuid, sp->fdt); 1672 1673 if (res != TEE_SUCCESS) { 1674 EMSG("Failed initializing SP(%pUl) err:%#"PRIx32, 1675 &sp->image.uuid, res); 1676 if (!IS_ENABLED(CFG_SP_SKIP_FAILED)) 1677 panic(); 1678 } 1679 } 1680 1681 res = fip_sp_init_all(); 1682 if (res) 1683 panic("Failed initializing FIP SPs"); 1684 1685 for_each_fip_sp(fip_sp) { 1686 sp = &fip_sp->sp_img; 1687 1688 DMSG("SP %pUl size %u", (void *)&sp->image.uuid, 1689 sp->image.size); 1690 1691 res = sp_init_uuid(&sp->image.uuid, sp->fdt); 1692 1693 if (res != TEE_SUCCESS) { 1694 EMSG("Failed initializing SP(%pUl) err:%#"PRIx32, 1695 &sp->image.uuid, res); 1696 if (!IS_ENABLED(CFG_SP_SKIP_FAILED)) 1697 panic(); 1698 } 1699 } 1700 1701 /* 1702 * At this point all FIP SPs are loaded by ldelf or by the raw binary SP 1703 * loader, so the original images (loaded by BL2) are not needed anymore 1704 */ 1705 fip_sp_deinit_all(); 1706 1707 /* Continue the initialization and run the SP */ 1708 TAILQ_FOREACH(s, &open_sp_sessions, link) { 1709 res = sp_first_run(s); 1710 if (res != TEE_SUCCESS) { 1711 EMSG("Failed starting SP(0x%"PRIx16") err:%#"PRIx32, 1712 s->endpoint_id, res); 1713 if (!IS_ENABLED(CFG_SP_SKIP_FAILED)) 1714 panic(); 1715 } 1716 } 1717 1718 return TEE_SUCCESS; 1719 } 1720 1721 boot_final(sp_init_all); 1722 1723 static TEE_Result secure_partition_open(const TEE_UUID *uuid, 1724 struct ts_store_handle **h) 1725 { 1726 return emb_ts_open(uuid, h, find_secure_partition); 1727 } 1728 1729 REGISTER_SP_STORE(2) = { 1730 .description = "SP store", 1731 .open = secure_partition_open, 1732 .get_size = emb_ts_get_size, 1733 .get_tag = emb_ts_get_tag, 1734 .read = emb_ts_read, 1735 .close = emb_ts_close, 1736 }; 1737