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 static struct guest_partition *find_guest_by_id_unlocked(uint16_t guest_id) 356 { 357 struct guest_partition *prtn = NULL; 358 359 LIST_FOREACH(prtn, &prtn_list, link) 360 if (!prtn->shutting_down && prtn->id == guest_id) 361 return prtn; 362 363 return NULL; 364 } 365 366 struct guest_partition *virt_get_current_guest(void) 367 { 368 struct guest_partition *prtn = get_current_prtn(); 369 370 if (prtn) 371 get_prtn(prtn); 372 return prtn; 373 } 374 375 struct guest_partition *virt_get_guest(uint16_t guest_id) 376 { 377 struct guest_partition *prtn = NULL; 378 uint32_t exceptions = 0; 379 380 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 381 prtn = find_guest_by_id_unlocked(guest_id); 382 if (prtn) 383 get_prtn(prtn); 384 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 385 386 return prtn; 387 } 388 389 void virt_put_guest(struct guest_partition *prtn) 390 { 391 if (prtn && refcount_dec(&prtn->refc)) { 392 uint32_t exceptions = 0; 393 bool do_free = true; 394 395 assert(prtn->shutting_down); 396 397 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 398 LIST_REMOVE(prtn, link); 399 if (prtn_have_remaining_resources(prtn)) { 400 LIST_INSERT_HEAD(&prtn_destroy_list, prtn, link); 401 /* 402 * Delay the nex_free() until 403 * virt_reclaim_cookie_from_destroyed_guest() 404 * is done with this partition. 405 */ 406 do_free = false; 407 } 408 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 409 410 tee_mm_free(prtn->tee_ram); 411 prtn->tee_ram = NULL; 412 tee_mm_free(prtn->ta_ram); 413 prtn->ta_ram = NULL; 414 tee_mm_free(prtn->tables); 415 prtn->tables = NULL; 416 core_free_mmu_prtn(prtn->mmu_prtn); 417 prtn->mmu_prtn = NULL; 418 nex_free(prtn->memory_map); 419 prtn->memory_map = NULL; 420 if (do_free) 421 nex_free(prtn); 422 } 423 } 424 425 TEE_Result virt_guest_destroyed(uint16_t guest_id) 426 { 427 struct guest_partition *prtn = NULL; 428 uint32_t exceptions = 0; 429 430 IMSG("Removing guest %"PRId16, guest_id); 431 432 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 433 434 prtn = find_guest_by_id_unlocked(guest_id); 435 if (prtn) 436 prtn->shutting_down = true; 437 438 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 439 440 virt_put_guest(prtn); 441 if (!prtn) 442 EMSG("Client with id %d is not found", guest_id); 443 444 return TEE_SUCCESS; 445 } 446 447 TEE_Result virt_set_guest(uint16_t guest_id) 448 { 449 struct guest_partition *prtn = get_current_prtn(); 450 451 /* This can be true only if we return from IRQ RPC */ 452 if (prtn && prtn->id == guest_id) 453 return TEE_SUCCESS; 454 455 if (prtn) 456 panic("Virtual guest partition is already set"); 457 458 prtn = virt_get_guest(guest_id); 459 if (!prtn) 460 return TEE_ERROR_ITEM_NOT_FOUND; 461 462 set_current_prtn(prtn); 463 core_mmu_set_prtn(prtn->mmu_prtn); 464 465 return TEE_SUCCESS; 466 } 467 468 void virt_unset_guest(void) 469 { 470 struct guest_partition *prtn = get_current_prtn(); 471 472 if (!prtn) 473 return; 474 475 set_current_prtn(NULL); 476 core_mmu_set_default_prtn(); 477 virt_put_guest(prtn); 478 } 479 480 void virt_on_stdcall(void) 481 { 482 struct guest_partition *prtn = get_current_prtn(); 483 484 /* Initialize runtime on first std call */ 485 if (!prtn->runtime_initialized) { 486 mutex_lock(&prtn->mutex); 487 if (!prtn->runtime_initialized) { 488 init_tee_runtime(); 489 prtn->runtime_initialized = true; 490 } 491 mutex_unlock(&prtn->mutex); 492 } 493 } 494 495 struct tee_mmap_region *virt_get_memory_map(void) 496 { 497 struct guest_partition *prtn; 498 499 prtn = get_current_prtn(); 500 501 if (!prtn) 502 return NULL; 503 504 return prtn->memory_map; 505 } 506 507 void virt_get_ta_ram(vaddr_t *start, vaddr_t *end) 508 { 509 struct guest_partition *prtn = get_current_prtn(); 510 511 *start = (vaddr_t)phys_to_virt(tee_mm_get_smem(prtn->ta_ram), 512 MEM_AREA_TA_RAM, 513 tee_mm_get_bytes(prtn->ta_ram)); 514 *end = *start + tee_mm_get_bytes(prtn->ta_ram); 515 } 516 517 #ifdef CFG_CORE_SEL1_SPMC 518 static int find_cookie(struct guest_partition *prtn, uint64_t cookie) 519 { 520 int i = 0; 521 522 for (i = 0; i < prtn->cookie_count; i++) 523 if (prtn->cookies[i] == cookie) 524 return i; 525 return -1; 526 } 527 528 static struct guest_partition *find_prtn_cookie(uint64_t cookie, int *idx) 529 { 530 struct guest_partition *prtn = NULL; 531 int i = 0; 532 533 LIST_FOREACH(prtn, &prtn_list, link) { 534 i = find_cookie(prtn, cookie); 535 if (i >= 0) { 536 if (idx) 537 *idx = i; 538 return prtn; 539 } 540 } 541 542 return NULL; 543 } 544 545 TEE_Result virt_add_cookie_to_current_guest(uint64_t cookie) 546 { 547 TEE_Result res = TEE_ERROR_ACCESS_DENIED; 548 struct guest_partition *prtn = NULL; 549 uint32_t exceptions = 0; 550 551 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 552 if (find_prtn_cookie(cookie, NULL)) 553 goto out; 554 555 prtn = current_partition[get_core_pos()]; 556 if (prtn->cookie_count < ARRAY_SIZE(prtn->cookies)) { 557 prtn->cookies[prtn->cookie_count] = cookie; 558 prtn->cookie_count++; 559 res = TEE_SUCCESS; 560 } 561 out: 562 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 563 564 return res; 565 } 566 567 void virt_remove_cookie(uint64_t cookie) 568 { 569 struct guest_partition *prtn = NULL; 570 uint32_t exceptions = 0; 571 int i = 0; 572 573 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 574 prtn = find_prtn_cookie(cookie, &i); 575 if (prtn) { 576 memmove(prtn->cookies + i, prtn->cookies + i + 1, 577 sizeof(uint64_t) * (prtn->cookie_count - i - 1)); 578 prtn->cookie_count--; 579 } 580 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 581 } 582 583 uint16_t virt_find_guest_by_cookie(uint64_t cookie) 584 { 585 struct guest_partition *prtn = NULL; 586 uint32_t exceptions = 0; 587 uint16_t ret = 0; 588 589 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 590 prtn = find_prtn_cookie(cookie, NULL); 591 if (prtn) 592 ret = prtn->id; 593 594 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 595 596 return ret; 597 } 598 599 bitstr_t *virt_get_shm_bits(void) 600 { 601 return get_current_prtn()->shm_bits; 602 } 603 604 static TEE_Result reclaim_cookie(struct guest_partition *prtn, uint64_t cookie) 605 { 606 if (cookie & FFA_MEMORY_HANDLE_HYPERVISOR_BIT) { 607 size_t n = 0; 608 609 for (n = 0; n < prtn->cookie_count; n++) { 610 if (prtn->cookies[n] == cookie) { 611 memmove(prtn->cookies + n, 612 prtn->cookies + n + 1, 613 sizeof(uint64_t) * 614 (prtn->cookie_count - n - 1)); 615 prtn->cookie_count--; 616 return TEE_SUCCESS; 617 } 618 } 619 } else { 620 uint64_t mask = FFA_MEMORY_HANDLE_NON_SECURE_BIT | 621 SHIFT_U64(FFA_MEMORY_HANDLE_PRTN_MASK, 622 FFA_MEMORY_HANDLE_PRTN_SHIFT); 623 int64_t i = cookie & ~mask; 624 625 if (i >= 0 && i < SPMC_CORE_SEL1_MAX_SHM_COUNT && 626 bit_test(prtn->shm_bits, i)) { 627 bit_clear(prtn->shm_bits, i); 628 return TEE_SUCCESS; 629 } 630 } 631 632 return TEE_ERROR_ITEM_NOT_FOUND; 633 } 634 635 TEE_Result virt_reclaim_cookie_from_destroyed_guest(uint16_t guest_id, 636 uint64_t cookie) 637 638 { 639 struct guest_partition *prtn = NULL; 640 TEE_Result res = TEE_ERROR_ITEM_NOT_FOUND; 641 uint32_t exceptions = 0; 642 643 exceptions = cpu_spin_lock_xsave(&prtn_list_lock); 644 LIST_FOREACH(prtn, &prtn_destroy_list, link) { 645 if (prtn->id == guest_id) { 646 res = reclaim_cookie(prtn, cookie); 647 if (prtn_have_remaining_resources(prtn)) 648 prtn = NULL; 649 else 650 LIST_REMOVE(prtn, link); 651 break; 652 } 653 } 654 cpu_spin_unlock_xrestore(&prtn_list_lock, exceptions); 655 656 nex_free(prtn); 657 658 return res; 659 } 660 #endif /*CFG_CORE_SEL1_SPMC*/ 661