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