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