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