1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2018, EPAM Systems. All rights reserved. 4 * Copyright (c) 2023-2024, Linaro Limited 5 */ 6 7 #include <bitstring.h> 8 #include <compiler.h> 9 #include <kernel/boot.h> 10 #include <kernel/linker.h> 11 #include <kernel/misc.h> 12 #include <kernel/mutex.h> 13 #include <kernel/panic.h> 14 #include <kernel/refcount.h> 15 #include <kernel/spinlock.h> 16 #include <kernel/thread_spmc.h> 17 #include <kernel/virtualization.h> 18 #include <mm/core_memprot.h> 19 #include <mm/core_mmu.h> 20 #include <mm/tee_mm.h> 21 #include <platform_config.h> 22 #include <sm/optee_smc.h> 23 #include <string.h> 24 #include <util.h> 25 26 LIST_HEAD(prtn_list_head, guest_partition); 27 28 static unsigned int prtn_list_lock __nex_data = SPINLOCK_UNLOCK; 29 30 static struct prtn_list_head prtn_list __nex_data = 31 LIST_HEAD_INITIALIZER(prtn_list); 32 static struct prtn_list_head prtn_destroy_list __nex_data = 33 LIST_HEAD_INITIALIZER(prtn_destroy_list); 34 35 /* Free pages used for guest partitions */ 36 tee_mm_pool_t virt_mapper_pool __nex_bss; 37 38 /* Memory used by OP-TEE core */ 39 struct tee_mmap_region *kmemory_map __nex_bss; 40 41 struct guest_partition { 42 LIST_ENTRY(guest_partition) link; 43 struct mmu_partition *mmu_prtn; 44 struct tee_mmap_region *memory_map; 45 struct mutex mutex; 46 void *tables_va; 47 tee_mm_entry_t *tee_ram; 48 tee_mm_entry_t *ta_ram; 49 tee_mm_entry_t *tables; 50 bool runtime_initialized; 51 bool shutting_down; 52 uint16_t id; 53 struct refcount refc; 54 #ifdef CFG_CORE_SEL1_SPMC 55 uint64_t cookies[SPMC_CORE_SEL1_MAX_SHM_COUNT]; 56 uint8_t cookie_count; 57 bitstr_t bit_decl(shm_bits, SPMC_CORE_SEL1_MAX_SHM_COUNT); 58 #endif 59 }; 60 61 struct guest_partition *current_partition[CFG_TEE_CORE_NB_CORE] __nex_bss; 62 63 static struct guest_partition *get_current_prtn(void) 64 { 65 struct guest_partition *ret; 66 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 67 68 ret = current_partition[get_core_pos()]; 69 70 thread_unmask_exceptions(exceptions); 71 72 return ret; 73 } 74 75 uint16_t virt_get_current_guest_id(void) 76 { 77 struct guest_partition *prtn = get_current_prtn(); 78 79 if (!prtn) 80 return 0; 81 return prtn->id; 82 } 83 84 static void set_current_prtn(struct guest_partition *prtn) 85 { 86 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); 87 88 current_partition[get_core_pos()] = prtn; 89 90 thread_unmask_exceptions(exceptions); 91 } 92 93 static size_t get_ta_ram_size(void) 94 { 95 size_t ta_size = 0; 96 97 core_mmu_get_ta_range(NULL, &ta_size); 98 return ROUNDDOWN(ta_size / CFG_VIRT_GUEST_COUNT - VCORE_UNPG_RW_SZ - 99 core_mmu_get_total_pages_size(), SMALL_PAGE_SIZE); 100 } 101 102 static struct tee_mmap_region *prepare_memory_map(paddr_t tee_data, 103 paddr_t ta_ram) 104 { 105 int i, entries; 106 vaddr_t max_va = 0; 107 struct tee_mmap_region *map; 108 /* 109 * This function assumes that at time of operation, 110 * kmemory_map (aka static_memory_map from core_mmu.c) 111 * will not be altered. This is true, because all 112 * changes to static_memory_map are done during 113 * OP-TEE initialization, while this function will 114 * called when hypervisor creates a guest. 115 */ 116 117 /* Count number of entries in nexus memory map */ 118 for (map = kmemory_map, entries = 1; map->type != MEM_AREA_END; 119 map++, entries++) 120 ; 121 122 /* Allocate entries for virtual guest map */ 123 map = nex_calloc(entries + 1, sizeof(struct tee_mmap_region)); 124 if (!map) 125 return NULL; 126 127 memcpy(map, kmemory_map, sizeof(*map) * entries); 128 129 /* Map TEE .data and .bss sections */ 130 for (i = 0; i < entries; i++) { 131 if (map[i].va == (vaddr_t)(VCORE_UNPG_RW_PA)) { 132 map[i].type = MEM_AREA_TEE_RAM_RW; 133 map[i].attr = core_mmu_type_to_attr(map[i].type); 134 map[i].pa = tee_data; 135 } 136 if (map[i].va + map[i].size > max_va) 137 max_va = map[i].va + map[i].size; 138 } 139 140 /* Map TA_RAM */ 141 assert(map[entries - 1].type == MEM_AREA_END); 142 map[entries] = map[entries - 1]; 143 map[entries - 1].region_size = SMALL_PAGE_SIZE; 144 map[entries - 1].va = ROUNDUP(max_va, map[entries - 1].region_size); 145 map[entries - 1].va += 146 (ta_ram - map[entries - 1].va) & CORE_MMU_PGDIR_MASK; 147 map[entries - 1].pa = ta_ram; 148 map[entries - 1].size = get_ta_ram_size(); 149 map[entries - 1].type = MEM_AREA_TA_RAM; 150 map[entries - 1].attr = core_mmu_type_to_attr(map[entries - 1].type); 151 152 DMSG("New map (%08lx):", (vaddr_t)(VCORE_UNPG_RW_PA)); 153 154 for (i = 0; i < entries; i++) 155 DMSG("T: %-16s rsz: %08x, pa: %08lx, va: %08lx, sz: %08lx attr: %x", 156 teecore_memtype_name(map[i].type), 157 map[i].region_size, map[i].pa, map[i].va, 158 map[i].size, map[i].attr); 159 return map; 160 } 161 162 void virt_init_memory(struct tee_mmap_region *memory_map, paddr_t secmem0_base, 163 paddr_size_t secmem0_size, paddr_t secmem1_base, 164 paddr_size_t secmem1_size) 165 { 166 struct tee_mmap_region *map = NULL; 167 paddr_size_t size = secmem0_size; 168 paddr_t base = secmem0_base; 169 170 if (secmem1_size) { 171 assert(secmem0_base + secmem0_size <= secmem1_base); 172 size = secmem1_base + secmem1_size - base; 173 } 174 175 /* Init page pool that covers all secure RAM */ 176 if (!tee_mm_init(&virt_mapper_pool, base, size, 177 SMALL_PAGE_SHIFT, TEE_MM_POOL_NEX_MALLOC)) 178 panic("Can't create pool with free pages"); 179 DMSG("Created virtual mapper pool from %"PRIxPA" to %"PRIxPA, 180 base, base + size); 181 182 if (secmem1_size) { 183 /* Carve out an eventual gap between secmem0 and secmem1 */ 184 base = secmem0_base + secmem0_size; 185 size = secmem1_base - base; 186 if (size) { 187 DMSG("Carving out gap between secmem0 and secmem1 (0x%"PRIxPA":0x%"PRIxPASZ")", 188 base, size); 189 if (!tee_mm_alloc2(&virt_mapper_pool, base, size)) 190 panic("Can't carve out secmem gap"); 191 } 192 } 193 194 195 /* Carve out areas that are used by OP-TEE core */ 196 for (map = memory_map; map->type != MEM_AREA_END; map++) { 197 switch (map->type) { 198 case MEM_AREA_TEE_RAM_RX: 199 case MEM_AREA_TEE_RAM_RO: 200 case MEM_AREA_NEX_RAM_RO: 201 case MEM_AREA_NEX_RAM_RW: 202 DMSG("Carving out area of type %d (0x%08lx-0x%08lx)", 203 map->type, map->pa, map->pa + map->size); 204 if (!tee_mm_alloc2(&virt_mapper_pool, map->pa, 205 map->size)) 206 panic("Can't carve out used area"); 207 break; 208 default: 209 continue; 210 } 211 } 212 213 kmemory_map = memory_map; 214 } 215 216 217 static TEE_Result configure_guest_prtn_mem(struct guest_partition *prtn) 218 { 219 TEE_Result res = TEE_SUCCESS; 220 paddr_t original_data_pa = 0; 221 222 prtn->tee_ram = tee_mm_alloc(&virt_mapper_pool, VCORE_UNPG_RW_SZ); 223 if (!prtn->tee_ram) { 224 EMSG("Can't allocate memory for TEE runtime context"); 225 res = TEE_ERROR_OUT_OF_MEMORY; 226 goto err; 227 } 228 DMSG("TEE RAM: %08" PRIxPA, tee_mm_get_smem(prtn->tee_ram)); 229 230 prtn->ta_ram = tee_mm_alloc(&virt_mapper_pool, get_ta_ram_size()); 231 if (!prtn->ta_ram) { 232 EMSG("Can't allocate memory for TA data"); 233 res = TEE_ERROR_OUT_OF_MEMORY; 234 goto err; 235 } 236 DMSG("TA RAM: %08" PRIxPA, tee_mm_get_smem(prtn->ta_ram)); 237 238 prtn->tables = tee_mm_alloc(&virt_mapper_pool, 239 core_mmu_get_total_pages_size()); 240 if (!prtn->tables) { 241 EMSG("Can't allocate memory for page tables"); 242 res = TEE_ERROR_OUT_OF_MEMORY; 243 goto err; 244 } 245 246 prtn->tables_va = phys_to_virt(tee_mm_get_smem(prtn->tables), 247 MEM_AREA_SEC_RAM_OVERALL, 248 core_mmu_get_total_pages_size()); 249 assert(prtn->tables_va); 250 251 prtn->mmu_prtn = core_alloc_mmu_prtn(prtn->tables_va); 252 if (!prtn->mmu_prtn) { 253 res = TEE_ERROR_OUT_OF_MEMORY; 254 goto err; 255 } 256 257 prtn->memory_map = prepare_memory_map(tee_mm_get_smem(prtn->tee_ram), 258 tee_mm_get_smem(prtn->ta_ram)); 259 if (!prtn->memory_map) { 260 res = TEE_ERROR_OUT_OF_MEMORY; 261 goto err; 262 } 263 264 core_init_mmu_prtn(prtn->mmu_prtn, prtn->memory_map); 265 266 original_data_pa = virt_to_phys(__data_start); 267 /* Switch to guest's mappings */ 268 core_mmu_set_prtn(prtn->mmu_prtn); 269 270 /* clear .bss */ 271 memset((void *)(VCORE_UNPG_RW_PA), 0, VCORE_UNPG_RW_SZ); 272 273 /* copy .data section from R/O original */ 274 memcpy(__data_start, 275 phys_to_virt(original_data_pa, MEM_AREA_SEC_RAM_OVERALL, 276 __data_end - __data_start), 277 __data_end - __data_start); 278 279 return TEE_SUCCESS; 280 281 err: 282 if (prtn->tee_ram) 283 tee_mm_free(prtn->tee_ram); 284 if (prtn->ta_ram) 285 tee_mm_free(prtn->ta_ram); 286 if (prtn->tables) 287 tee_mm_free(prtn->tables); 288 nex_free(prtn->mmu_prtn); 289 nex_free(prtn->memory_map); 290 291 return res; 292 } 293 294 TEE_Result virt_guest_created(uint16_t guest_id) 295 { 296 struct guest_partition *prtn = NULL; 297 TEE_Result res = TEE_SUCCESS; 298 uint32_t exceptions = 0; 299 300 prtn = nex_calloc(1, sizeof(*prtn)); 301 if (!prtn) 302 return TEE_ERROR_OUT_OF_MEMORY; 303 304 prtn->id = guest_id; 305 mutex_init(&prtn->mutex); 306 refcount_set(&prtn->refc, 1); 307 res = configure_guest_prtn_mem(prtn); 308 if (res) 309 goto err_free_prtn; 310 311 set_current_prtn(prtn); 312 313 /* Initialize threads */ 314 thread_init_threads(); 315 /* Do the preinitcalls */ 316 call_preinitcalls(); 317 318 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 319 LIST_INSERT_HEAD(&prtn_list, prtn, link); 320 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 321 322 IMSG("Added guest %d", guest_id); 323 324 set_current_prtn(NULL); 325 core_mmu_set_default_prtn(); 326 327 return TEE_SUCCESS; 328 329 err_free_prtn: 330 nex_free(prtn); 331 return res; 332 } 333 334 static bool 335 prtn_have_remaining_resources(struct guest_partition *prtn __maybe_unused) 336 { 337 #ifdef CFG_CORE_SEL1_SPMC 338 int i = 0; 339 340 if (prtn->cookie_count) 341 return true; 342 bit_ffs(prtn->shm_bits, SPMC_CORE_SEL1_MAX_SHM_COUNT, &i); 343 return i >= 0; 344 #else 345 return false; 346 #endif 347 } 348 349 static void get_prtn(struct guest_partition *prtn) 350 { 351 if (!refcount_inc(&prtn->refc)) 352 panic(); 353 } 354 355 uint16_t virt_get_guest_id(struct guest_partition *prtn) 356 { 357 if (!prtn) 358 return 0; 359 return prtn->id; 360 } 361 362 static struct guest_partition *find_guest_by_id_unlocked(uint16_t guest_id) 363 { 364 struct guest_partition *prtn = NULL; 365 366 LIST_FOREACH(prtn, &prtn_list, link) 367 if (!prtn->shutting_down && prtn->id == guest_id) 368 return prtn; 369 370 return NULL; 371 } 372 373 struct guest_partition *virt_get_current_guest(void) 374 { 375 struct guest_partition *prtn = get_current_prtn(); 376 377 if (prtn) 378 get_prtn(prtn); 379 return prtn; 380 } 381 382 struct guest_partition *virt_get_guest(uint16_t guest_id) 383 { 384 struct guest_partition *prtn = NULL; 385 uint32_t exceptions = 0; 386 387 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 388 prtn = find_guest_by_id_unlocked(guest_id); 389 if (prtn) 390 get_prtn(prtn); 391 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 392 393 return prtn; 394 } 395 396 void virt_put_guest(struct guest_partition *prtn) 397 { 398 if (prtn && refcount_dec(&prtn->refc)) { 399 uint32_t exceptions = 0; 400 bool do_free = true; 401 402 assert(prtn->shutting_down); 403 404 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 405 LIST_REMOVE(prtn, link); 406 if (prtn_have_remaining_resources(prtn)) { 407 LIST_INSERT_HEAD(&prtn_destroy_list, prtn, link); 408 /* 409 * Delay the nex_free() until 410 * virt_reclaim_cookie_from_destroyed_guest() 411 * is done with this partition. 412 */ 413 do_free = false; 414 } 415 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 416 417 tee_mm_free(prtn->tee_ram); 418 prtn->tee_ram = NULL; 419 tee_mm_free(prtn->ta_ram); 420 prtn->ta_ram = NULL; 421 tee_mm_free(prtn->tables); 422 prtn->tables = NULL; 423 core_free_mmu_prtn(prtn->mmu_prtn); 424 prtn->mmu_prtn = NULL; 425 nex_free(prtn->memory_map); 426 prtn->memory_map = NULL; 427 if (do_free) 428 nex_free(prtn); 429 } 430 } 431 432 TEE_Result virt_guest_destroyed(uint16_t guest_id) 433 { 434 struct guest_partition *prtn = NULL; 435 uint32_t exceptions = 0; 436 437 IMSG("Removing guest %"PRId16, guest_id); 438 439 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 440 441 prtn = find_guest_by_id_unlocked(guest_id); 442 if (prtn) 443 prtn->shutting_down = true; 444 445 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 446 447 virt_put_guest(prtn); 448 if (!prtn) 449 EMSG("Client with id %d is not found", guest_id); 450 451 return TEE_SUCCESS; 452 } 453 454 TEE_Result virt_set_guest(uint16_t guest_id) 455 { 456 struct guest_partition *prtn = get_current_prtn(); 457 458 /* This can be true only if we return from IRQ RPC */ 459 if (prtn && prtn->id == guest_id) 460 return TEE_SUCCESS; 461 462 if (prtn) 463 panic("Virtual guest partition is already set"); 464 465 prtn = virt_get_guest(guest_id); 466 if (!prtn) 467 return TEE_ERROR_ITEM_NOT_FOUND; 468 469 set_current_prtn(prtn); 470 core_mmu_set_prtn(prtn->mmu_prtn); 471 472 return TEE_SUCCESS; 473 } 474 475 void virt_unset_guest(void) 476 { 477 struct guest_partition *prtn = get_current_prtn(); 478 479 if (!prtn) 480 return; 481 482 set_current_prtn(NULL); 483 core_mmu_set_default_prtn(); 484 virt_put_guest(prtn); 485 } 486 487 void virt_on_stdcall(void) 488 { 489 struct guest_partition *prtn = get_current_prtn(); 490 491 /* Initialize runtime on first std call */ 492 if (!prtn->runtime_initialized) { 493 mutex_lock(&prtn->mutex); 494 if (!prtn->runtime_initialized) { 495 init_tee_runtime(); 496 prtn->runtime_initialized = true; 497 } 498 mutex_unlock(&prtn->mutex); 499 } 500 } 501 502 struct tee_mmap_region *virt_get_memory_map(void) 503 { 504 struct guest_partition *prtn; 505 506 prtn = get_current_prtn(); 507 508 if (!prtn) 509 return NULL; 510 511 return prtn->memory_map; 512 } 513 514 void virt_get_ta_ram(vaddr_t *start, vaddr_t *end) 515 { 516 struct guest_partition *prtn = get_current_prtn(); 517 518 *start = (vaddr_t)phys_to_virt(tee_mm_get_smem(prtn->ta_ram), 519 MEM_AREA_TA_RAM, 520 tee_mm_get_bytes(prtn->ta_ram)); 521 *end = *start + tee_mm_get_bytes(prtn->ta_ram); 522 } 523 524 #ifdef CFG_CORE_SEL1_SPMC 525 static int find_cookie(struct guest_partition *prtn, uint64_t cookie) 526 { 527 int i = 0; 528 529 for (i = 0; i < prtn->cookie_count; i++) 530 if (prtn->cookies[i] == cookie) 531 return i; 532 return -1; 533 } 534 535 static struct guest_partition *find_prtn_cookie(uint64_t cookie, int *idx) 536 { 537 struct guest_partition *prtn = NULL; 538 int i = 0; 539 540 LIST_FOREACH(prtn, &prtn_list, link) { 541 i = find_cookie(prtn, cookie); 542 if (i >= 0) { 543 if (idx) 544 *idx = i; 545 return prtn; 546 } 547 } 548 549 return NULL; 550 } 551 552 TEE_Result virt_add_cookie_to_current_guest(uint64_t cookie) 553 { 554 TEE_Result res = TEE_ERROR_ACCESS_DENIED; 555 struct guest_partition *prtn = NULL; 556 uint32_t exceptions = 0; 557 558 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 559 if (find_prtn_cookie(cookie, NULL)) 560 goto out; 561 562 prtn = current_partition[get_core_pos()]; 563 if (prtn->cookie_count < ARRAY_SIZE(prtn->cookies)) { 564 prtn->cookies[prtn->cookie_count] = cookie; 565 prtn->cookie_count++; 566 res = TEE_SUCCESS; 567 } 568 out: 569 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 570 571 return res; 572 } 573 574 void virt_remove_cookie(uint64_t cookie) 575 { 576 struct guest_partition *prtn = NULL; 577 uint32_t exceptions = 0; 578 int i = 0; 579 580 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 581 prtn = find_prtn_cookie(cookie, &i); 582 if (prtn) { 583 memmove(prtn->cookies + i, prtn->cookies + i + 1, 584 sizeof(uint64_t) * (prtn->cookie_count - i - 1)); 585 prtn->cookie_count--; 586 } 587 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 588 } 589 590 uint16_t virt_find_guest_by_cookie(uint64_t cookie) 591 { 592 struct guest_partition *prtn = NULL; 593 uint32_t exceptions = 0; 594 uint16_t ret = 0; 595 596 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 597 prtn = find_prtn_cookie(cookie, NULL); 598 if (prtn) 599 ret = prtn->id; 600 601 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 602 603 return ret; 604 } 605 606 bitstr_t *virt_get_shm_bits(void) 607 { 608 return get_current_prtn()->shm_bits; 609 } 610 611 static TEE_Result reclaim_cookie(struct guest_partition *prtn, uint64_t cookie) 612 { 613 if (cookie & FFA_MEMORY_HANDLE_HYPERVISOR_BIT) { 614 size_t n = 0; 615 616 for (n = 0; n < prtn->cookie_count; n++) { 617 if (prtn->cookies[n] == cookie) { 618 memmove(prtn->cookies + n, 619 prtn->cookies + n + 1, 620 sizeof(uint64_t) * 621 (prtn->cookie_count - n - 1)); 622 prtn->cookie_count--; 623 return TEE_SUCCESS; 624 } 625 } 626 } else { 627 uint64_t mask = FFA_MEMORY_HANDLE_NON_SECURE_BIT | 628 SHIFT_U64(FFA_MEMORY_HANDLE_PRTN_MASK, 629 FFA_MEMORY_HANDLE_PRTN_SHIFT); 630 int64_t i = cookie & ~mask; 631 632 if (i >= 0 && i < SPMC_CORE_SEL1_MAX_SHM_COUNT && 633 bit_test(prtn->shm_bits, i)) { 634 bit_clear(prtn->shm_bits, i); 635 return TEE_SUCCESS; 636 } 637 } 638 639 return TEE_ERROR_ITEM_NOT_FOUND; 640 } 641 642 TEE_Result virt_reclaim_cookie_from_destroyed_guest(uint16_t guest_id, 643 uint64_t cookie) 644 645 { 646 struct guest_partition *prtn = NULL; 647 TEE_Result res = TEE_ERROR_ITEM_NOT_FOUND; 648 uint32_t exceptions = 0; 649 650 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 651 LIST_FOREACH(prtn, &prtn_destroy_list, link) { 652 if (prtn->id == guest_id) { 653 res = reclaim_cookie(prtn, cookie); 654 if (prtn_have_remaining_resources(prtn)) 655 prtn = NULL; 656 else 657 LIST_REMOVE(prtn, link); 658 break; 659 } 660 } 661 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 662 663 nex_free(prtn); 664 665 return res; 666 } 667 #endif /*CFG_CORE_SEL1_SPMC*/ 668