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, NULL, (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 /* 559 * Make the SP ready for its first run. 560 * Set state to busy to prevent other endpoints from sending messages to 561 * the SP before its boot phase is done. 562 */ 563 s->state = sp_busy; 564 s->caller_id = 0; 565 sp_init_set_registers(ctx); 566 memcpy(&s->ffa_uuid, ffa_uuid, sizeof(*ffa_uuid)); 567 ts_pop_current_session(); 568 569 return TEE_SUCCESS; 570 } 571 572 static TEE_Result fdt_get_uuid(const void * const fdt, TEE_UUID *uuid) 573 { 574 const struct fdt_property *description = NULL; 575 int description_name_len = 0; 576 577 if (fdt_node_check_compatible(fdt, 0, "arm,ffa-manifest-1.0")) { 578 EMSG("Failed loading SP, manifest not found"); 579 return TEE_ERROR_BAD_PARAMETERS; 580 } 581 582 description = fdt_get_property(fdt, 0, "description", 583 &description_name_len); 584 if (description) 585 DMSG("Loading SP: %s", description->data); 586 587 if (sp_dt_get_uuid(fdt, 0, "uuid", uuid)) { 588 EMSG("Missing or invalid UUID in SP manifest"); 589 return TEE_ERROR_BAD_FORMAT; 590 } 591 592 return TEE_SUCCESS; 593 } 594 595 static TEE_Result copy_and_map_fdt(struct sp_ctx *ctx, const void * const fdt, 596 void **fdt_copy, size_t *mapped_size) 597 { 598 size_t total_size = ROUNDUP(fdt_totalsize(fdt), SMALL_PAGE_SIZE); 599 size_t num_pages = total_size / SMALL_PAGE_SIZE; 600 uint32_t perm = TEE_MATTR_UR | TEE_MATTR_PRW; 601 TEE_Result res = TEE_SUCCESS; 602 struct mobj *m = NULL; 603 struct fobj *f = NULL; 604 vaddr_t va = 0; 605 606 f = fobj_sec_mem_alloc(num_pages); 607 m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED); 608 fobj_put(f); 609 if (!m) 610 return TEE_ERROR_OUT_OF_MEMORY; 611 612 res = vm_map(&ctx->uctx, &va, total_size, perm, 0, m, 0); 613 mobj_put(m); 614 if (res) 615 return res; 616 617 if (fdt_open_into(fdt, (void *)va, total_size)) 618 return TEE_ERROR_GENERIC; 619 620 *fdt_copy = (void *)va; 621 *mapped_size = total_size; 622 623 return res; 624 } 625 626 static void fill_boot_info_1_0(vaddr_t buf, const void *fdt) 627 { 628 struct ffa_boot_info_1_0 *info = (struct ffa_boot_info_1_0 *)buf; 629 static const char fdt_name[16] = "TYPE_DT\0\0\0\0\0\0\0\0"; 630 631 memcpy(&info->magic, "FF-A", 4); 632 info->count = 1; 633 634 COMPILE_TIME_ASSERT(sizeof(info->nvp[0].name) == sizeof(fdt_name)); 635 memcpy(info->nvp[0].name, fdt_name, sizeof(fdt_name)); 636 info->nvp[0].value = (uintptr_t)fdt; 637 info->nvp[0].size = fdt_totalsize(fdt); 638 } 639 640 static void fill_boot_info_1_1(vaddr_t buf, const void *fdt) 641 { 642 size_t desc_offs = ROUNDUP(sizeof(struct ffa_boot_info_header_1_1), 8); 643 struct ffa_boot_info_header_1_1 *header = 644 (struct ffa_boot_info_header_1_1 *)buf; 645 struct ffa_boot_info_1_1 *desc = 646 (struct ffa_boot_info_1_1 *)(buf + desc_offs); 647 648 header->signature = FFA_BOOT_INFO_SIGNATURE; 649 header->version = FFA_BOOT_INFO_VERSION; 650 header->blob_size = desc_offs + sizeof(struct ffa_boot_info_1_1); 651 header->desc_size = sizeof(struct ffa_boot_info_1_1); 652 header->desc_count = 1; 653 header->desc_offset = desc_offs; 654 655 memset(&desc[0].name, 0, sizeof(desc[0].name)); 656 /* Type: Standard boot info (bit[7] == 0), FDT type */ 657 desc[0].type = FFA_BOOT_INFO_TYPE_ID_FDT; 658 /* Flags: Contents field contains an address */ 659 desc[0].flags = FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR << 660 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT; 661 desc[0].size = fdt_totalsize(fdt); 662 desc[0].contents = (uintptr_t)fdt; 663 } 664 665 static TEE_Result create_and_map_boot_info(struct sp_ctx *ctx, const void *fdt, 666 struct thread_smc_args *args, 667 vaddr_t *va, size_t *mapped_size) 668 { 669 size_t total_size = ROUNDUP(CFG_SP_INIT_INFO_MAX_SIZE, SMALL_PAGE_SIZE); 670 size_t num_pages = total_size / SMALL_PAGE_SIZE; 671 uint32_t perm = TEE_MATTR_UR | TEE_MATTR_PRW; 672 TEE_Result res = TEE_SUCCESS; 673 uint32_t sp_ffa_version = 0; 674 struct fobj *f = NULL; 675 struct mobj *m = NULL; 676 uint32_t info_reg = 0; 677 678 res = sp_dt_get_u32(fdt, 0, "ffa-version", &sp_ffa_version); 679 if (res) 680 return res; 681 682 f = fobj_sec_mem_alloc(num_pages); 683 m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED); 684 fobj_put(f); 685 if (!m) 686 return TEE_ERROR_OUT_OF_MEMORY; 687 688 res = vm_map(&ctx->uctx, va, total_size, perm, 0, m, 0); 689 mobj_put(m); 690 if (res) 691 return res; 692 693 *mapped_size = total_size; 694 695 switch (sp_ffa_version) { 696 case MAKE_FFA_VERSION(1, 0): 697 fill_boot_info_1_0(*va, fdt); 698 break; 699 case MAKE_FFA_VERSION(1, 1): 700 fill_boot_info_1_1(*va, fdt); 701 break; 702 default: 703 EMSG("Unknown FF-A version: %#"PRIx32, sp_ffa_version); 704 return TEE_ERROR_NOT_SUPPORTED; 705 } 706 707 res = sp_dt_get_u32(fdt, 0, "gp-register-num", &info_reg); 708 if (res) { 709 if (res == TEE_ERROR_ITEM_NOT_FOUND) { 710 /* If the property is not present, set default to x0 */ 711 info_reg = 0; 712 } else { 713 return TEE_ERROR_BAD_FORMAT; 714 } 715 } 716 717 switch (info_reg) { 718 case 0: 719 args->a0 = *va; 720 break; 721 case 1: 722 args->a1 = *va; 723 break; 724 case 2: 725 args->a2 = *va; 726 break; 727 case 3: 728 args->a3 = *va; 729 break; 730 default: 731 EMSG("Invalid register selected for passing boot info"); 732 return TEE_ERROR_BAD_FORMAT; 733 } 734 735 return TEE_SUCCESS; 736 } 737 738 static TEE_Result handle_fdt_load_relative_mem_regions(struct sp_ctx *ctx, 739 const void *fdt) 740 { 741 int node = 0; 742 int subnode = 0; 743 tee_mm_entry_t *mm = NULL; 744 TEE_Result res = TEE_SUCCESS; 745 746 /* 747 * Memory regions are optional in the SP manifest, it's not an error if 748 * we don't find any. 749 */ 750 node = fdt_node_offset_by_compatible(fdt, 0, 751 "arm,ffa-manifest-memory-regions"); 752 if (node < 0) 753 return TEE_SUCCESS; 754 755 fdt_for_each_subnode(subnode, fdt, node) { 756 uint64_t load_rel_offset = 0; 757 uint32_t attributes = 0; 758 uint64_t base_addr = 0; 759 uint32_t pages_cnt = 0; 760 uint32_t flags = 0; 761 uint32_t perm = 0; 762 size_t size = 0; 763 vaddr_t va = 0; 764 765 mm = NULL; 766 767 /* Load address relative offset of a memory region */ 768 if (!sp_dt_get_u64(fdt, subnode, "load-address-relative-offset", 769 &load_rel_offset)) { 770 va = ctx->uctx.load_addr + load_rel_offset; 771 } else { 772 /* Skip non load address relative memory regions */ 773 continue; 774 } 775 776 if (!sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) { 777 EMSG("Both base-address and load-address-relative-offset fields are set"); 778 return TEE_ERROR_BAD_FORMAT; 779 } 780 781 /* Size of memory region as count of 4K pages */ 782 if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) { 783 EMSG("Mandatory field is missing: pages-count"); 784 return TEE_ERROR_BAD_FORMAT; 785 } 786 787 if (MUL_OVERFLOW(pages_cnt, SMALL_PAGE_SIZE, &size)) 788 return TEE_ERROR_OVERFLOW; 789 790 /* Memory region attributes */ 791 if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) { 792 EMSG("Mandatory field is missing: attributes"); 793 return TEE_ERROR_BAD_FORMAT; 794 } 795 796 /* Check instruction and data access permissions */ 797 switch (attributes & SP_MANIFEST_ATTR_RWX) { 798 case SP_MANIFEST_ATTR_RO: 799 perm = TEE_MATTR_UR; 800 break; 801 case SP_MANIFEST_ATTR_RW: 802 perm = TEE_MATTR_URW; 803 break; 804 case SP_MANIFEST_ATTR_RX: 805 perm = TEE_MATTR_URX; 806 break; 807 default: 808 EMSG("Invalid memory access permissions"); 809 return TEE_ERROR_BAD_FORMAT; 810 } 811 812 res = sp_dt_get_u32(fdt, subnode, "load-flags", &flags); 813 if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND) { 814 EMSG("Optional field with invalid value: flags"); 815 return TEE_ERROR_BAD_FORMAT; 816 } 817 818 /* Load relative regions must be secure */ 819 if (attributes & SP_MANIFEST_ATTR_NSEC) { 820 EMSG("Invalid memory security attribute"); 821 return TEE_ERROR_BAD_FORMAT; 822 } 823 824 if (flags & SP_MANIFEST_FLAG_NOBITS) { 825 /* 826 * NOBITS flag is set, which means that loaded binary 827 * doesn't contain this area, so it's need to be 828 * allocated. 829 */ 830 struct mobj *m = NULL; 831 unsigned int idx = 0; 832 833 mm = tee_mm_alloc(&tee_mm_sec_ddr, size); 834 if (!mm) 835 return TEE_ERROR_OUT_OF_MEMORY; 836 837 base_addr = tee_mm_get_smem(mm); 838 839 m = sp_mem_new_mobj(pages_cnt, 840 TEE_MATTR_MEM_TYPE_CACHED, true); 841 if (!m) { 842 res = TEE_ERROR_OUT_OF_MEMORY; 843 goto err_mm_free; 844 } 845 846 res = sp_mem_add_pages(m, &idx, base_addr, pages_cnt); 847 if (res) { 848 mobj_put(m); 849 goto err_mm_free; 850 } 851 852 res = vm_map(&ctx->uctx, &va, size, perm, 0, m, 0); 853 mobj_put(m); 854 if (res) 855 goto err_mm_free; 856 } else { 857 /* 858 * If NOBITS is not present the memory area is already 859 * mapped and only need to set the correct permissions. 860 */ 861 res = vm_set_prot(&ctx->uctx, va, size, perm); 862 if (res) 863 return res; 864 } 865 } 866 867 return TEE_SUCCESS; 868 869 err_mm_free: 870 tee_mm_free(mm); 871 return res; 872 } 873 874 static TEE_Result handle_fdt_dev_regions(struct sp_ctx *ctx, void *fdt) 875 { 876 int node = 0; 877 int subnode = 0; 878 TEE_Result res = TEE_SUCCESS; 879 const char *dt_device_match_table = { 880 "arm,ffa-manifest-device-regions", 881 }; 882 883 /* 884 * Device regions are optional in the SP manifest, it's not an error if 885 * we don't find any 886 */ 887 node = fdt_node_offset_by_compatible(fdt, 0, dt_device_match_table); 888 if (node < 0) 889 return TEE_SUCCESS; 890 891 fdt_for_each_subnode(subnode, fdt, node) { 892 uint64_t base_addr = 0; 893 uint32_t pages_cnt = 0; 894 uint32_t attributes = 0; 895 struct mobj *m = NULL; 896 bool is_secure = true; 897 uint32_t perm = 0; 898 vaddr_t va = 0; 899 unsigned int idx = 0; 900 901 /* 902 * Physical base address of a device MMIO region. 903 * Currently only physically contiguous region is supported. 904 */ 905 if (sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) { 906 EMSG("Mandatory field is missing: base-address"); 907 return TEE_ERROR_BAD_FORMAT; 908 } 909 910 /* Total size of MMIO region as count of 4K pages */ 911 if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) { 912 EMSG("Mandatory field is missing: pages-count"); 913 return TEE_ERROR_BAD_FORMAT; 914 } 915 916 /* Data access, instruction access and security attributes */ 917 if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) { 918 EMSG("Mandatory field is missing: attributes"); 919 return TEE_ERROR_BAD_FORMAT; 920 } 921 922 /* Check instruction and data access permissions */ 923 switch (attributes & SP_MANIFEST_ATTR_RWX) { 924 case SP_MANIFEST_ATTR_RO: 925 perm = TEE_MATTR_UR; 926 break; 927 case SP_MANIFEST_ATTR_RW: 928 perm = TEE_MATTR_URW; 929 break; 930 default: 931 EMSG("Invalid memory access permissions"); 932 return TEE_ERROR_BAD_FORMAT; 933 } 934 935 /* 936 * The SP is a secure endpoint, security attribute can be 937 * secure or non-secure 938 */ 939 if (attributes & SP_MANIFEST_ATTR_NSEC) 940 is_secure = false; 941 942 /* Memory attributes must be Device-nGnRnE */ 943 m = sp_mem_new_mobj(pages_cnt, TEE_MATTR_MEM_TYPE_STRONGLY_O, 944 is_secure); 945 if (!m) 946 return TEE_ERROR_OUT_OF_MEMORY; 947 948 res = sp_mem_add_pages(m, &idx, (paddr_t)base_addr, pages_cnt); 949 if (res) { 950 mobj_put(m); 951 return res; 952 } 953 954 res = vm_map(&ctx->uctx, &va, pages_cnt * SMALL_PAGE_SIZE, 955 perm, 0, m, 0); 956 mobj_put(m); 957 if (res) 958 return res; 959 960 /* 961 * Overwrite the device region's PA in the fdt with the VA. This 962 * fdt will be passed to the SP. 963 */ 964 res = fdt_setprop_u64(fdt, subnode, "base-address", va); 965 966 /* 967 * Unmap the region if the overwrite failed since the SP won't 968 * be able to access it without knowing the VA. 969 */ 970 if (res) { 971 vm_unmap(&ctx->uctx, va, pages_cnt * SMALL_PAGE_SIZE); 972 return res; 973 } 974 } 975 976 return TEE_SUCCESS; 977 } 978 979 static TEE_Result swap_sp_endpoints(uint32_t endpoint_id, 980 uint32_t new_endpoint_id) 981 { 982 struct sp_session *session = sp_get_session(endpoint_id); 983 uint32_t manifest_endpoint_id = 0; 984 985 /* 986 * We don't know in which order the SPs are loaded. The endpoint ID 987 * defined in the manifest could already be generated by 988 * new_session_id() and used by another SP. If this is the case, we swap 989 * the ID's of the two SPs. We also have to make sure that the ID's are 990 * not defined twice in the manifest. 991 */ 992 993 /* The endpoint ID was not assigned yet */ 994 if (!session) 995 return TEE_SUCCESS; 996 997 /* 998 * Read the manifest file from the SP who originally had the endpoint. 999 * We can safely swap the endpoint ID's if the manifest file doesn't 1000 * have an endpoint ID defined. 1001 */ 1002 if (!sp_dt_get_u32(session->fdt, 0, "id", &manifest_endpoint_id)) { 1003 assert(manifest_endpoint_id == endpoint_id); 1004 EMSG("SP: Found duplicated endpoint ID %#"PRIx32, endpoint_id); 1005 return TEE_ERROR_ACCESS_CONFLICT; 1006 } 1007 1008 session->endpoint_id = new_endpoint_id; 1009 1010 return TEE_SUCCESS; 1011 } 1012 1013 static TEE_Result read_manifest_endpoint_id(struct sp_session *s) 1014 { 1015 uint32_t endpoint_id = 0; 1016 1017 /* 1018 * The endpoint ID can be optionally defined in the manifest file. We 1019 * have to map the ID inside the manifest to the SP if it's defined. 1020 * If not, the endpoint ID generated inside new_session_id() will be 1021 * used. 1022 */ 1023 if (!sp_dt_get_u32(s->fdt, 0, "id", &endpoint_id)) { 1024 TEE_Result res = TEE_ERROR_GENERIC; 1025 1026 if (endpoint_id <= SPMC_ENDPOINT_ID) 1027 return TEE_ERROR_BAD_FORMAT; 1028 1029 res = swap_sp_endpoints(endpoint_id, s->endpoint_id); 1030 if (res) 1031 return res; 1032 1033 DMSG("SP: endpoint ID (0x%"PRIx32") found in manifest", 1034 endpoint_id); 1035 /* Assign the endpoint ID to the current SP */ 1036 s->endpoint_id = endpoint_id; 1037 } 1038 return TEE_SUCCESS; 1039 } 1040 1041 static TEE_Result handle_fdt_mem_regions(struct sp_ctx *ctx, void *fdt) 1042 { 1043 int node = 0; 1044 int subnode = 0; 1045 tee_mm_entry_t *mm = NULL; 1046 TEE_Result res = TEE_SUCCESS; 1047 1048 /* 1049 * Memory regions are optional in the SP manifest, it's not an error if 1050 * we don't find any. 1051 */ 1052 node = fdt_node_offset_by_compatible(fdt, 0, 1053 "arm,ffa-manifest-memory-regions"); 1054 if (node < 0) 1055 return TEE_SUCCESS; 1056 1057 fdt_for_each_subnode(subnode, fdt, node) { 1058 uint64_t load_rel_offset = 0; 1059 bool alloc_needed = false; 1060 uint32_t attributes = 0; 1061 uint64_t base_addr = 0; 1062 uint32_t pages_cnt = 0; 1063 bool is_secure = true; 1064 struct mobj *m = NULL; 1065 unsigned int idx = 0; 1066 uint32_t perm = 0; 1067 size_t size = 0; 1068 vaddr_t va = 0; 1069 1070 mm = NULL; 1071 1072 /* Load address relative offset of a memory region */ 1073 if (!sp_dt_get_u64(fdt, subnode, "load-address-relative-offset", 1074 &load_rel_offset)) { 1075 /* 1076 * At this point the memory region is already mapped by 1077 * handle_fdt_load_relative_mem_regions. 1078 * Only need to set the base-address in the manifest and 1079 * then skip the rest of the mapping process. 1080 */ 1081 va = ctx->uctx.load_addr + load_rel_offset; 1082 res = fdt_setprop_u64(fdt, subnode, "base-address", va); 1083 if (res) 1084 return res; 1085 1086 continue; 1087 } 1088 1089 /* 1090 * Base address of a memory region. 1091 * If not present, we have to allocate the specified memory. 1092 * If present, this field could specify a PA or VA. Currently 1093 * only a PA is supported. 1094 */ 1095 if (sp_dt_get_u64(fdt, subnode, "base-address", &base_addr)) 1096 alloc_needed = true; 1097 1098 /* Size of memory region as count of 4K pages */ 1099 if (sp_dt_get_u32(fdt, subnode, "pages-count", &pages_cnt)) { 1100 EMSG("Mandatory field is missing: pages-count"); 1101 return TEE_ERROR_BAD_FORMAT; 1102 } 1103 1104 if (MUL_OVERFLOW(pages_cnt, SMALL_PAGE_SIZE, &size)) 1105 return TEE_ERROR_OVERFLOW; 1106 1107 /* 1108 * Memory region attributes: 1109 * - Instruction/data access permissions 1110 * - Cacheability/shareability attributes 1111 * - Security attributes 1112 * 1113 * Cacheability/shareability attributes can be ignored for now. 1114 * OP-TEE only supports a single type for normal cached memory 1115 * and currently there is no use case that would require to 1116 * change this. 1117 */ 1118 if (sp_dt_get_u32(fdt, subnode, "attributes", &attributes)) { 1119 EMSG("Mandatory field is missing: attributes"); 1120 return TEE_ERROR_BAD_FORMAT; 1121 } 1122 1123 /* Check instruction and data access permissions */ 1124 switch (attributes & SP_MANIFEST_ATTR_RWX) { 1125 case SP_MANIFEST_ATTR_RO: 1126 perm = TEE_MATTR_UR; 1127 break; 1128 case SP_MANIFEST_ATTR_RW: 1129 perm = TEE_MATTR_URW; 1130 break; 1131 case SP_MANIFEST_ATTR_RX: 1132 perm = TEE_MATTR_URX; 1133 break; 1134 default: 1135 EMSG("Invalid memory access permissions"); 1136 return TEE_ERROR_BAD_FORMAT; 1137 } 1138 1139 /* 1140 * The SP is a secure endpoint, security attribute can be 1141 * secure or non-secure. 1142 * The SPMC cannot allocate non-secure memory, i.e. if the base 1143 * address is missing this attribute must be secure. 1144 */ 1145 if (attributes & SP_MANIFEST_ATTR_NSEC) { 1146 if (alloc_needed) { 1147 EMSG("Invalid memory security attribute"); 1148 return TEE_ERROR_BAD_FORMAT; 1149 } 1150 is_secure = false; 1151 } 1152 1153 if (alloc_needed) { 1154 /* Base address is missing, we have to allocate */ 1155 mm = tee_mm_alloc(&tee_mm_sec_ddr, size); 1156 if (!mm) 1157 return TEE_ERROR_OUT_OF_MEMORY; 1158 1159 base_addr = tee_mm_get_smem(mm); 1160 } 1161 1162 m = sp_mem_new_mobj(pages_cnt, TEE_MATTR_MEM_TYPE_CACHED, 1163 is_secure); 1164 if (!m) { 1165 res = TEE_ERROR_OUT_OF_MEMORY; 1166 goto err_mm_free; 1167 } 1168 1169 res = sp_mem_add_pages(m, &idx, base_addr, pages_cnt); 1170 if (res) { 1171 mobj_put(m); 1172 goto err_mm_free; 1173 } 1174 1175 res = vm_map(&ctx->uctx, &va, size, perm, 0, m, 0); 1176 mobj_put(m); 1177 if (res) 1178 goto err_mm_free; 1179 1180 /* 1181 * Overwrite the memory region's base address in the fdt with 1182 * the VA. This fdt will be passed to the SP. 1183 * If the base-address field was not present in the original 1184 * fdt, this function will create it. This doesn't cause issues 1185 * since the necessary extra space has been allocated when 1186 * opening the fdt. 1187 */ 1188 res = fdt_setprop_u64(fdt, subnode, "base-address", va); 1189 1190 /* 1191 * Unmap the region if the overwrite failed since the SP won't 1192 * be able to access it without knowing the VA. 1193 */ 1194 if (res) { 1195 vm_unmap(&ctx->uctx, va, size); 1196 goto err_mm_free; 1197 } 1198 } 1199 1200 return TEE_SUCCESS; 1201 1202 err_mm_free: 1203 tee_mm_free(mm); 1204 return res; 1205 } 1206 1207 static TEE_Result handle_tpm_event_log(struct sp_ctx *ctx, void *fdt) 1208 { 1209 uint32_t perm = TEE_MATTR_URW | TEE_MATTR_PRW; 1210 uint32_t dummy_size __maybe_unused = 0; 1211 TEE_Result res = TEE_SUCCESS; 1212 size_t page_count = 0; 1213 struct fobj *f = NULL; 1214 struct mobj *m = NULL; 1215 vaddr_t log_addr = 0; 1216 size_t log_size = 0; 1217 int node = 0; 1218 1219 node = fdt_node_offset_by_compatible(fdt, 0, "arm,tpm_event_log"); 1220 if (node < 0) 1221 return TEE_SUCCESS; 1222 1223 /* Checking the existence and size of the event log properties */ 1224 if (sp_dt_get_u64(fdt, node, "tpm_event_log_addr", &log_addr)) { 1225 EMSG("tpm_event_log_addr not found or has invalid size"); 1226 return TEE_ERROR_BAD_FORMAT; 1227 } 1228 1229 if (sp_dt_get_u32(fdt, node, "tpm_event_log_size", &dummy_size)) { 1230 EMSG("tpm_event_log_size not found or has invalid size"); 1231 return TEE_ERROR_BAD_FORMAT; 1232 } 1233 1234 /* Validating event log */ 1235 res = tpm_get_event_log_size(&log_size); 1236 if (res) 1237 return res; 1238 1239 if (!log_size) { 1240 EMSG("Empty TPM event log was provided"); 1241 return TEE_ERROR_ITEM_NOT_FOUND; 1242 } 1243 1244 /* Allocating memory area for the event log to share with the SP */ 1245 page_count = ROUNDUP_DIV(log_size, SMALL_PAGE_SIZE); 1246 1247 f = fobj_sec_mem_alloc(page_count); 1248 m = mobj_with_fobj_alloc(f, NULL, TEE_MATTR_MEM_TYPE_TAGGED); 1249 fobj_put(f); 1250 if (!m) 1251 return TEE_ERROR_OUT_OF_MEMORY; 1252 1253 res = vm_map(&ctx->uctx, &log_addr, log_size, perm, 0, m, 0); 1254 mobj_put(m); 1255 if (res) 1256 return res; 1257 1258 /* Copy event log */ 1259 res = tpm_get_event_log((void *)log_addr, &log_size); 1260 if (res) 1261 goto err_unmap; 1262 1263 /* Setting event log details in the manifest */ 1264 res = fdt_setprop_u64(fdt, node, "tpm_event_log_addr", log_addr); 1265 if (res) 1266 goto err_unmap; 1267 1268 res = fdt_setprop_u32(fdt, node, "tpm_event_log_size", log_size); 1269 if (res) 1270 goto err_unmap; 1271 1272 return TEE_SUCCESS; 1273 1274 err_unmap: 1275 vm_unmap(&ctx->uctx, log_addr, log_size); 1276 1277 return res; 1278 } 1279 1280 /* 1281 * Note: this function is called only on the primary CPU. It assumes that the 1282 * features present on the primary CPU are available on all of the secondary 1283 * CPUs as well. 1284 */ 1285 static TEE_Result handle_hw_features(void *fdt) 1286 { 1287 uint32_t val __maybe_unused = 0; 1288 TEE_Result res = TEE_SUCCESS; 1289 int node = 0; 1290 1291 /* 1292 * HW feature descriptions are optional in the SP manifest, it's not an 1293 * error if we don't find any. 1294 */ 1295 node = fdt_node_offset_by_compatible(fdt, 0, "arm,hw-features"); 1296 if (node < 0) 1297 return TEE_SUCCESS; 1298 1299 /* Modify the crc32 property only if it's already present */ 1300 if (!sp_dt_get_u32(fdt, node, "crc32", &val)) { 1301 res = fdt_setprop_u32(fdt, node, "crc32", 1302 feat_crc32_implemented()); 1303 if (res) 1304 return res; 1305 } 1306 1307 return TEE_SUCCESS; 1308 } 1309 1310 static TEE_Result read_ns_interrupts_action(const void *fdt, 1311 struct sp_session *s) 1312 { 1313 TEE_Result res = TEE_ERROR_BAD_PARAMETERS; 1314 1315 res = sp_dt_get_u32(fdt, 0, "ns-interrupts-action", &s->ns_int_mode); 1316 1317 if (res) { 1318 EMSG("Mandatory property is missing: ns-interrupts-action"); 1319 return res; 1320 } 1321 1322 switch (s->ns_int_mode) { 1323 case SP_MANIFEST_NS_INT_QUEUED: 1324 case SP_MANIFEST_NS_INT_SIGNALED: 1325 /* OK */ 1326 break; 1327 1328 case SP_MANIFEST_NS_INT_MANAGED_EXIT: 1329 EMSG("Managed exit is not implemented"); 1330 return TEE_ERROR_NOT_IMPLEMENTED; 1331 1332 default: 1333 EMSG("Invalid ns-interrupts-action value: %"PRIu32, 1334 s->ns_int_mode); 1335 return TEE_ERROR_BAD_PARAMETERS; 1336 } 1337 1338 return TEE_SUCCESS; 1339 } 1340 1341 static TEE_Result sp_init_uuid(const TEE_UUID *bin_uuid, const void * const fdt) 1342 { 1343 TEE_Result res = TEE_SUCCESS; 1344 struct sp_session *sess = NULL; 1345 TEE_UUID ffa_uuid = {}; 1346 1347 res = fdt_get_uuid(fdt, &ffa_uuid); 1348 if (res) 1349 return res; 1350 1351 res = sp_open_session(&sess, 1352 &open_sp_sessions, 1353 &ffa_uuid, bin_uuid, fdt); 1354 if (res) 1355 return res; 1356 1357 sess->fdt = fdt; 1358 res = read_manifest_endpoint_id(sess); 1359 if (res) 1360 return res; 1361 DMSG("endpoint is 0x%"PRIx16, sess->endpoint_id); 1362 1363 res = read_ns_interrupts_action(fdt, sess); 1364 if (res) 1365 return res; 1366 1367 return TEE_SUCCESS; 1368 } 1369 1370 static TEE_Result sp_first_run(struct sp_session *sess) 1371 { 1372 TEE_Result res = TEE_SUCCESS; 1373 struct thread_smc_args args = { }; 1374 struct sp_ctx *ctx = NULL; 1375 vaddr_t boot_info_va = 0; 1376 size_t boot_info_size = 0; 1377 void *fdt_copy = NULL; 1378 size_t fdt_size = 0; 1379 1380 ctx = to_sp_ctx(sess->ts_sess.ctx); 1381 ts_push_current_session(&sess->ts_sess); 1382 sess->is_initialized = false; 1383 1384 /* 1385 * Load relative memory regions must be handled before doing any other 1386 * mapping to prevent conflicts in the VA space. 1387 */ 1388 res = handle_fdt_load_relative_mem_regions(ctx, sess->fdt); 1389 if (res) { 1390 ts_pop_current_session(); 1391 return res; 1392 } 1393 1394 res = copy_and_map_fdt(ctx, sess->fdt, &fdt_copy, &fdt_size); 1395 if (res) 1396 goto out; 1397 1398 res = handle_fdt_dev_regions(ctx, fdt_copy); 1399 if (res) 1400 goto out; 1401 1402 res = handle_fdt_mem_regions(ctx, fdt_copy); 1403 if (res) 1404 goto out; 1405 1406 if (IS_ENABLED(CFG_CORE_TPM_EVENT_LOG)) { 1407 res = handle_tpm_event_log(ctx, fdt_copy); 1408 if (res) 1409 goto out; 1410 } 1411 1412 res = handle_hw_features(fdt_copy); 1413 if (res) 1414 goto out; 1415 1416 res = create_and_map_boot_info(ctx, fdt_copy, &args, &boot_info_va, 1417 &boot_info_size); 1418 if (res) 1419 goto out; 1420 1421 ts_pop_current_session(); 1422 1423 res = sp_enter(&args, sess); 1424 if (res) { 1425 ts_push_current_session(&sess->ts_sess); 1426 goto out; 1427 } 1428 1429 spmc_sp_msg_handler(&args, sess); 1430 1431 ts_push_current_session(&sess->ts_sess); 1432 sess->is_initialized = true; 1433 1434 out: 1435 /* Free the boot info page from the SP memory */ 1436 vm_unmap(&ctx->uctx, boot_info_va, boot_info_size); 1437 vm_unmap(&ctx->uctx, (vaddr_t)fdt_copy, fdt_size); 1438 ts_pop_current_session(); 1439 1440 return res; 1441 } 1442 1443 TEE_Result sp_enter(struct thread_smc_args *args, struct sp_session *sp) 1444 { 1445 TEE_Result res = TEE_SUCCESS; 1446 struct sp_ctx *ctx = to_sp_ctx(sp->ts_sess.ctx); 1447 1448 ctx->sp_regs.x[0] = args->a0; 1449 ctx->sp_regs.x[1] = args->a1; 1450 ctx->sp_regs.x[2] = args->a2; 1451 ctx->sp_regs.x[3] = args->a3; 1452 ctx->sp_regs.x[4] = args->a4; 1453 ctx->sp_regs.x[5] = args->a5; 1454 ctx->sp_regs.x[6] = args->a6; 1455 ctx->sp_regs.x[7] = args->a7; 1456 1457 res = sp->ts_sess.ctx->ops->enter_invoke_cmd(&sp->ts_sess, 0); 1458 1459 args->a0 = ctx->sp_regs.x[0]; 1460 args->a1 = ctx->sp_regs.x[1]; 1461 args->a2 = ctx->sp_regs.x[2]; 1462 args->a3 = ctx->sp_regs.x[3]; 1463 args->a4 = ctx->sp_regs.x[4]; 1464 args->a5 = ctx->sp_regs.x[5]; 1465 args->a6 = ctx->sp_regs.x[6]; 1466 args->a7 = ctx->sp_regs.x[7]; 1467 1468 return res; 1469 } 1470 1471 /* 1472 * According to FF-A v1.1 section 8.3.1.4 if a caller requires less permissive 1473 * active on NS interrupt than the callee, the callee must inherit the caller's 1474 * configuration. 1475 * Each SP's own NS action setting is stored in ns_int_mode. The effective 1476 * action will be MIN([self action], [caller's action]) which is stored in the 1477 * ns_int_mode_inherited field. 1478 */ 1479 static void sp_cpsr_configure_foreign_interrupts(struct sp_session *s, 1480 struct ts_session *caller, 1481 uint64_t *cpsr) 1482 { 1483 if (caller) { 1484 struct sp_session *caller_sp = to_sp_session(caller); 1485 1486 s->ns_int_mode_inherited = MIN(caller_sp->ns_int_mode_inherited, 1487 s->ns_int_mode); 1488 } else { 1489 s->ns_int_mode_inherited = s->ns_int_mode; 1490 } 1491 1492 if (s->ns_int_mode_inherited == SP_MANIFEST_NS_INT_QUEUED) 1493 *cpsr |= SHIFT_U32(THREAD_EXCP_FOREIGN_INTR, 1494 ARM32_CPSR_F_SHIFT); 1495 else 1496 *cpsr &= ~SHIFT_U32(THREAD_EXCP_FOREIGN_INTR, 1497 ARM32_CPSR_F_SHIFT); 1498 } 1499 1500 static TEE_Result sp_enter_invoke_cmd(struct ts_session *s, 1501 uint32_t cmd __unused) 1502 { 1503 struct sp_ctx *ctx = to_sp_ctx(s->ctx); 1504 TEE_Result res = TEE_SUCCESS; 1505 uint32_t exceptions = 0; 1506 struct sp_session *sp_s = to_sp_session(s); 1507 struct ts_session *sess = NULL; 1508 struct thread_ctx_regs *sp_regs = NULL; 1509 uint32_t thread_id = THREAD_ID_INVALID; 1510 struct ts_session *caller = NULL; 1511 uint32_t rpc_target_info = 0; 1512 uint32_t panicked = false; 1513 uint32_t panic_code = 0; 1514 1515 bm_timestamp(); 1516 1517 sp_regs = &ctx->sp_regs; 1518 ts_push_current_session(s); 1519 1520 exceptions = thread_mask_exceptions(THREAD_EXCP_ALL); 1521 1522 /* Enable/disable foreign interrupts in CPSR/SPSR */ 1523 caller = ts_get_calling_session(); 1524 sp_cpsr_configure_foreign_interrupts(sp_s, caller, &sp_regs->cpsr); 1525 1526 /* 1527 * Store endpoint ID and thread ID in rpc_target_info. This will be used 1528 * as w1 in FFA_INTERRUPT in case of a foreign interrupt. 1529 */ 1530 rpc_target_info = thread_get_tsd()->rpc_target_info; 1531 thread_id = thread_get_id(); 1532 assert(thread_id <= UINT16_MAX); 1533 thread_get_tsd()->rpc_target_info = 1534 FFA_TARGET_INFO_SET(sp_s->endpoint_id, thread_id); 1535 1536 __thread_enter_user_mode(sp_regs, &panicked, &panic_code); 1537 1538 /* Restore rpc_target_info */ 1539 thread_get_tsd()->rpc_target_info = rpc_target_info; 1540 1541 thread_unmask_exceptions(exceptions); 1542 1543 thread_user_clear_vfp(&ctx->uctx); 1544 1545 if (panicked) { 1546 DMSG("SP panicked with code %#"PRIx32, panic_code); 1547 abort_print_current_ts(); 1548 1549 sess = ts_pop_current_session(); 1550 cpu_spin_lock(&sp_s->spinlock); 1551 sp_s->state = sp_dead; 1552 cpu_spin_unlock(&sp_s->spinlock); 1553 1554 return TEE_ERROR_TARGET_DEAD; 1555 } 1556 1557 sess = ts_pop_current_session(); 1558 assert(sess == s); 1559 1560 bm_timestamp(); 1561 1562 return res; 1563 } 1564 1565 /* We currently don't support 32 bits */ 1566 #ifdef ARM64 1567 static void sp_svc_store_registers(struct thread_scall_regs *regs, 1568 struct thread_ctx_regs *sp_regs) 1569 { 1570 COMPILE_TIME_ASSERT(sizeof(sp_regs->x[0]) == sizeof(regs->x0)); 1571 memcpy(sp_regs->x, ®s->x0, 31 * sizeof(regs->x0)); 1572 sp_regs->pc = regs->elr; 1573 sp_regs->sp = regs->sp_el0; 1574 } 1575 #endif 1576 1577 static bool sp_handle_scall(struct thread_scall_regs *regs) 1578 { 1579 struct ts_session *ts = ts_get_current_session(); 1580 struct sp_ctx *uctx = to_sp_ctx(ts->ctx); 1581 struct sp_session *s = uctx->open_session; 1582 1583 assert(s); 1584 1585 sp_svc_store_registers(regs, &uctx->sp_regs); 1586 1587 regs->x0 = 0; 1588 regs->x1 = 0; /* panic */ 1589 regs->x2 = 0; /* panic code */ 1590 1591 /* 1592 * All the registers of the SP are saved in the SP session by the SVC 1593 * handler. 1594 * We always return to S-El1 after handling the SVC. We will continue 1595 * in sp_enter_invoke_cmd() (return from __thread_enter_user_mode). 1596 * The sp_enter() function copies the FF-A parameters (a0-a7) from the 1597 * saved registers to the thread_smc_args. The thread_smc_args object is 1598 * afterward used by the spmc_sp_msg_handler() to handle the 1599 * FF-A message send by the SP. 1600 */ 1601 return false; 1602 } 1603 1604 static void sp_dump_state(struct ts_ctx *ctx) 1605 { 1606 struct sp_ctx *utc = to_sp_ctx(ctx); 1607 1608 if (utc->uctx.dump_entry_func) { 1609 TEE_Result res = ldelf_dump_state(&utc->uctx); 1610 1611 if (!res || res == TEE_ERROR_TARGET_DEAD) 1612 return; 1613 } 1614 1615 user_mode_ctx_print_mappings(&utc->uctx); 1616 } 1617 1618 static const struct ts_ops sp_ops = { 1619 .enter_invoke_cmd = sp_enter_invoke_cmd, 1620 .handle_scall = sp_handle_scall, 1621 .dump_state = sp_dump_state, 1622 }; 1623 1624 static TEE_Result process_sp_pkg(uint64_t sp_pkg_pa, TEE_UUID *sp_uuid) 1625 { 1626 enum teecore_memtypes mtype = MEM_AREA_TA_RAM; 1627 struct sp_pkg_header *sp_pkg_hdr = NULL; 1628 struct fip_sp *sp = NULL; 1629 uint64_t sp_fdt_end = 0; 1630 size_t sp_pkg_size = 0; 1631 vaddr_t sp_pkg_va = 0; 1632 1633 /* Process the first page which contains the SP package header */ 1634 sp_pkg_va = (vaddr_t)phys_to_virt(sp_pkg_pa, mtype, SMALL_PAGE_SIZE); 1635 if (!sp_pkg_va) { 1636 EMSG("Cannot find mapping for PA %#" PRIxPA, sp_pkg_pa); 1637 return TEE_ERROR_GENERIC; 1638 } 1639 1640 sp_pkg_hdr = (struct sp_pkg_header *)sp_pkg_va; 1641 1642 if (sp_pkg_hdr->magic != SP_PKG_HEADER_MAGIC) { 1643 EMSG("Invalid SP package magic"); 1644 return TEE_ERROR_BAD_FORMAT; 1645 } 1646 1647 if (sp_pkg_hdr->version != SP_PKG_HEADER_VERSION_V1 && 1648 sp_pkg_hdr->version != SP_PKG_HEADER_VERSION_V2) { 1649 EMSG("Invalid SP header version"); 1650 return TEE_ERROR_BAD_FORMAT; 1651 } 1652 1653 if (ADD_OVERFLOW(sp_pkg_hdr->img_offset, sp_pkg_hdr->img_size, 1654 &sp_pkg_size)) { 1655 EMSG("Invalid SP package size"); 1656 return TEE_ERROR_BAD_FORMAT; 1657 } 1658 1659 if (ADD_OVERFLOW(sp_pkg_hdr->pm_offset, sp_pkg_hdr->pm_size, 1660 &sp_fdt_end) || sp_fdt_end > sp_pkg_hdr->img_offset) { 1661 EMSG("Invalid SP manifest size"); 1662 return TEE_ERROR_BAD_FORMAT; 1663 } 1664 1665 /* Process the whole SP package now that the size is known */ 1666 sp_pkg_va = (vaddr_t)phys_to_virt(sp_pkg_pa, mtype, sp_pkg_size); 1667 if (!sp_pkg_va) { 1668 EMSG("Cannot find mapping for PA %#" PRIxPA, sp_pkg_pa); 1669 return TEE_ERROR_GENERIC; 1670 } 1671 1672 sp_pkg_hdr = (struct sp_pkg_header *)sp_pkg_va; 1673 1674 sp = calloc(1, sizeof(struct fip_sp)); 1675 if (!sp) 1676 return TEE_ERROR_OUT_OF_MEMORY; 1677 1678 memcpy(&sp->sp_img.image.uuid, sp_uuid, sizeof(*sp_uuid)); 1679 sp->sp_img.image.ts = (uint8_t *)(sp_pkg_va + sp_pkg_hdr->img_offset); 1680 sp->sp_img.image.size = sp_pkg_hdr->img_size; 1681 sp->sp_img.image.flags = 0; 1682 sp->sp_img.fdt = (uint8_t *)(sp_pkg_va + sp_pkg_hdr->pm_offset); 1683 1684 STAILQ_INSERT_TAIL(&fip_sp_list, sp, link); 1685 1686 return TEE_SUCCESS; 1687 } 1688 1689 static TEE_Result fip_sp_init_all(void) 1690 { 1691 TEE_Result res = TEE_SUCCESS; 1692 uint64_t sp_pkg_addr = 0; 1693 const void *fdt = NULL; 1694 TEE_UUID sp_uuid = { }; 1695 int sp_pkgs_node = 0; 1696 int subnode = 0; 1697 int root = 0; 1698 1699 fdt = get_manifest_dt(); 1700 if (!fdt) { 1701 EMSG("No SPMC manifest found"); 1702 return TEE_ERROR_GENERIC; 1703 } 1704 1705 root = fdt_path_offset(fdt, "/"); 1706 if (root < 0) 1707 return TEE_ERROR_BAD_FORMAT; 1708 1709 if (fdt_node_check_compatible(fdt, root, "arm,ffa-core-manifest-1.0")) 1710 return TEE_ERROR_BAD_FORMAT; 1711 1712 /* SP packages are optional, it's not an error if we don't find any */ 1713 sp_pkgs_node = fdt_node_offset_by_compatible(fdt, root, "arm,sp_pkg"); 1714 if (sp_pkgs_node < 0) 1715 return TEE_SUCCESS; 1716 1717 fdt_for_each_subnode(subnode, fdt, sp_pkgs_node) { 1718 res = sp_dt_get_u64(fdt, subnode, "load-address", &sp_pkg_addr); 1719 if (res) { 1720 EMSG("Invalid FIP SP load address"); 1721 return res; 1722 } 1723 1724 res = sp_dt_get_uuid(fdt, subnode, "uuid", &sp_uuid); 1725 if (res) { 1726 EMSG("Invalid FIP SP uuid"); 1727 return res; 1728 } 1729 1730 res = process_sp_pkg(sp_pkg_addr, &sp_uuid); 1731 if (res) { 1732 EMSG("Invalid FIP SP package"); 1733 return res; 1734 } 1735 } 1736 1737 return TEE_SUCCESS; 1738 } 1739 1740 static void fip_sp_deinit_all(void) 1741 { 1742 while (!STAILQ_EMPTY(&fip_sp_list)) { 1743 struct fip_sp *sp = STAILQ_FIRST(&fip_sp_list); 1744 1745 STAILQ_REMOVE_HEAD(&fip_sp_list, link); 1746 free(sp); 1747 } 1748 } 1749 1750 static TEE_Result sp_init_all(void) 1751 { 1752 TEE_Result res = TEE_SUCCESS; 1753 const struct sp_image *sp = NULL; 1754 const struct fip_sp *fip_sp = NULL; 1755 char __maybe_unused msg[60] = { '\0', }; 1756 struct sp_session *s = NULL; 1757 1758 for_each_secure_partition(sp) { 1759 if (sp->image.uncompressed_size) 1760 snprintf(msg, sizeof(msg), 1761 " (compressed, uncompressed %u)", 1762 sp->image.uncompressed_size); 1763 else 1764 msg[0] = '\0'; 1765 DMSG("SP %pUl size %u%s", (void *)&sp->image.uuid, 1766 sp->image.size, msg); 1767 1768 res = sp_init_uuid(&sp->image.uuid, sp->fdt); 1769 1770 if (res != TEE_SUCCESS) { 1771 EMSG("Failed initializing SP(%pUl) err:%#"PRIx32, 1772 &sp->image.uuid, res); 1773 if (!IS_ENABLED(CFG_SP_SKIP_FAILED)) 1774 panic(); 1775 } 1776 } 1777 1778 res = fip_sp_init_all(); 1779 if (res) 1780 panic("Failed initializing FIP SPs"); 1781 1782 for_each_fip_sp(fip_sp) { 1783 sp = &fip_sp->sp_img; 1784 1785 DMSG("SP %pUl size %u", (void *)&sp->image.uuid, 1786 sp->image.size); 1787 1788 res = sp_init_uuid(&sp->image.uuid, sp->fdt); 1789 1790 if (res != TEE_SUCCESS) { 1791 EMSG("Failed initializing SP(%pUl) err:%#"PRIx32, 1792 &sp->image.uuid, res); 1793 if (!IS_ENABLED(CFG_SP_SKIP_FAILED)) 1794 panic(); 1795 } 1796 } 1797 1798 /* 1799 * At this point all FIP SPs are loaded by ldelf or by the raw binary SP 1800 * loader, so the original images (loaded by BL2) are not needed anymore 1801 */ 1802 fip_sp_deinit_all(); 1803 1804 /* Continue the initialization and run the SP */ 1805 TAILQ_FOREACH(s, &open_sp_sessions, link) { 1806 res = sp_first_run(s); 1807 if (res != TEE_SUCCESS) { 1808 EMSG("Failed starting SP(0x%"PRIx16") err:%#"PRIx32, 1809 s->endpoint_id, res); 1810 if (!IS_ENABLED(CFG_SP_SKIP_FAILED)) 1811 panic(); 1812 } 1813 } 1814 1815 return TEE_SUCCESS; 1816 } 1817 1818 boot_final(sp_init_all); 1819 1820 static TEE_Result secure_partition_open(const TEE_UUID *uuid, 1821 struct ts_store_handle **h) 1822 { 1823 return emb_ts_open(uuid, h, find_secure_partition); 1824 } 1825 1826 REGISTER_SP_STORE(2) = { 1827 .description = "SP store", 1828 .open = secure_partition_open, 1829 .get_size = emb_ts_get_size, 1830 .get_tag = emb_ts_get_tag, 1831 .read = emb_ts_read, 1832 .close = emb_ts_close, 1833 }; 1834