1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2020 Google LLC 4 * Author: Quentin Perret <qperret@google.com> 5 */ 6 7 #include <linux/kvm_host.h> 8 9 #include <asm/kvm_emulate.h> 10 #include <asm/kvm_hyp.h> 11 #include <asm/kvm_mmu.h> 12 #include <asm/kvm_pgtable.h> 13 #include <asm/kvm_pkvm.h> 14 #include <asm/stage2_pgtable.h> 15 16 #include <hyp/fault.h> 17 18 #include <nvhe/arm-smccc.h> 19 #include <nvhe/gfp.h> 20 #include <nvhe/memory.h> 21 #include <nvhe/mem_protect.h> 22 #include <nvhe/mm.h> 23 #include <nvhe/trap_handler.h> 24 25 #define KVM_HOST_S2_FLAGS (KVM_PGTABLE_S2_AS_S1 | KVM_PGTABLE_S2_IDMAP) 26 27 struct host_mmu host_mmu; 28 29 static struct hyp_pool host_s2_pool; 30 31 static DEFINE_PER_CPU(struct pkvm_hyp_vm *, __current_vm); 32 #define current_vm (*this_cpu_ptr(&__current_vm)) 33 34 static void pkvm_sme_dvmsync_fw_call(void) 35 { 36 if (alternative_has_cap_unlikely(ARM64_WORKAROUND_4193714)) { 37 struct arm_smccc_res res; 38 39 /* 40 * Ignore the return value. Probing for the workaround 41 * availability took place in init_hyp_mode(). 42 */ 43 hyp_smccc_1_1_smc(ARM_SMCCC_CPU_WORKAROUND_4193714, &res); 44 } 45 } 46 47 static void guest_lock_component(struct pkvm_hyp_vm *vm) 48 { 49 hyp_spin_lock(&vm->lock); 50 current_vm = vm; 51 } 52 53 static void guest_unlock_component(struct pkvm_hyp_vm *vm) 54 { 55 current_vm = NULL; 56 hyp_spin_unlock(&vm->lock); 57 } 58 59 static void host_lock_component(void) 60 { 61 hyp_spin_lock(&host_mmu.lock); 62 } 63 64 static void host_unlock_component(void) 65 { 66 hyp_spin_unlock(&host_mmu.lock); 67 } 68 69 static void hyp_lock_component(void) 70 { 71 hyp_spin_lock(&pkvm_pgd_lock); 72 } 73 74 static void hyp_unlock_component(void) 75 { 76 hyp_spin_unlock(&pkvm_pgd_lock); 77 } 78 79 #define for_each_hyp_page(__p, __st, __sz) \ 80 for (struct hyp_page *__p = hyp_phys_to_page(__st), \ 81 *__e = __p + ((__sz) >> PAGE_SHIFT); \ 82 __p < __e; __p++) 83 84 static void *host_s2_zalloc_pages_exact(size_t size) 85 { 86 void *addr = hyp_alloc_pages(&host_s2_pool, get_order(size)); 87 88 hyp_split_page(hyp_virt_to_page(addr)); 89 90 /* 91 * The size of concatenated PGDs is always a power of two of PAGE_SIZE, 92 * so there should be no need to free any of the tail pages to make the 93 * allocation exact. 94 */ 95 WARN_ON(size != (PAGE_SIZE << get_order(size))); 96 97 return addr; 98 } 99 100 static void *host_s2_zalloc_page(void *pool) 101 { 102 return hyp_alloc_pages(pool, 0); 103 } 104 105 static void host_s2_get_page(void *addr) 106 { 107 hyp_get_page(&host_s2_pool, addr); 108 } 109 110 static void host_s2_put_page(void *addr) 111 { 112 hyp_put_page(&host_s2_pool, addr); 113 } 114 115 static void host_s2_free_unlinked_table(void *addr, s8 level) 116 { 117 kvm_pgtable_stage2_free_unlinked(&host_mmu.mm_ops, addr, level); 118 } 119 120 static int prepare_s2_pool(void *pgt_pool_base) 121 { 122 unsigned long nr_pages, pfn; 123 int ret; 124 125 pfn = hyp_virt_to_pfn(pgt_pool_base); 126 nr_pages = host_s2_pgtable_pages(); 127 ret = hyp_pool_init(&host_s2_pool, pfn, nr_pages, 0); 128 if (ret) 129 return ret; 130 131 host_mmu.mm_ops = (struct kvm_pgtable_mm_ops) { 132 .zalloc_pages_exact = host_s2_zalloc_pages_exact, 133 .zalloc_page = host_s2_zalloc_page, 134 .free_unlinked_table = host_s2_free_unlinked_table, 135 .phys_to_virt = hyp_phys_to_virt, 136 .virt_to_phys = hyp_virt_to_phys, 137 .page_count = hyp_page_count, 138 .get_page = host_s2_get_page, 139 .put_page = host_s2_put_page, 140 }; 141 142 return 0; 143 } 144 145 static void prepare_host_vtcr(void) 146 { 147 u32 parange, phys_shift; 148 149 /* The host stage 2 is id-mapped, so use parange for T0SZ */ 150 parange = kvm_get_parange(id_aa64mmfr0_el1_sys_val); 151 phys_shift = id_aa64mmfr0_parange_to_phys_shift(parange); 152 153 host_mmu.arch.mmu.vtcr = kvm_get_vtcr(id_aa64mmfr0_el1_sys_val, 154 id_aa64mmfr1_el1_sys_val, phys_shift); 155 } 156 157 static bool host_stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot prot); 158 159 int kvm_host_prepare_stage2(void *pgt_pool_base) 160 { 161 struct kvm_s2_mmu *mmu = &host_mmu.arch.mmu; 162 int ret; 163 164 prepare_host_vtcr(); 165 hyp_spin_lock_init(&host_mmu.lock); 166 mmu->arch = &host_mmu.arch; 167 168 ret = prepare_s2_pool(pgt_pool_base); 169 if (ret) 170 return ret; 171 172 ret = __kvm_pgtable_stage2_init(&host_mmu.pgt, mmu, 173 &host_mmu.mm_ops, KVM_HOST_S2_FLAGS, 174 host_stage2_force_pte_cb); 175 if (ret) 176 return ret; 177 178 mmu->pgd_phys = __hyp_pa(host_mmu.pgt.pgd); 179 mmu->pgt = &host_mmu.pgt; 180 atomic64_set(&mmu->vmid.id, 0); 181 182 return 0; 183 } 184 185 static void *guest_s2_zalloc_pages_exact(size_t size) 186 { 187 void *addr = hyp_alloc_pages(¤t_vm->pool, get_order(size)); 188 189 WARN_ON(size != (PAGE_SIZE << get_order(size))); 190 hyp_split_page(hyp_virt_to_page(addr)); 191 192 return addr; 193 } 194 195 static void guest_s2_free_pages_exact(void *addr, unsigned long size) 196 { 197 u8 order = get_order(size); 198 unsigned int i; 199 200 for (i = 0; i < (1 << order); i++) 201 hyp_put_page(¤t_vm->pool, addr + (i * PAGE_SIZE)); 202 } 203 204 static void *guest_s2_zalloc_page(void *mc) 205 { 206 struct hyp_page *p; 207 void *addr; 208 209 addr = hyp_alloc_pages(¤t_vm->pool, 0); 210 if (addr) 211 return addr; 212 213 addr = pop_hyp_memcache(mc, hyp_phys_to_virt); 214 if (!addr) 215 return addr; 216 217 memset(addr, 0, PAGE_SIZE); 218 p = hyp_virt_to_page(addr); 219 p->refcount = 1; 220 221 return addr; 222 } 223 224 static void guest_s2_get_page(void *addr) 225 { 226 hyp_get_page(¤t_vm->pool, addr); 227 } 228 229 static void guest_s2_put_page(void *addr) 230 { 231 hyp_put_page(¤t_vm->pool, addr); 232 } 233 234 static void __apply_guest_page(void *va, size_t size, 235 void (*func)(void *addr, size_t size)) 236 { 237 size += va - PTR_ALIGN_DOWN(va, PAGE_SIZE); 238 va = PTR_ALIGN_DOWN(va, PAGE_SIZE); 239 size = PAGE_ALIGN(size); 240 241 while (size) { 242 size_t map_size = PAGE_SIZE; 243 void *map; 244 245 if (IS_ALIGNED((unsigned long)va, PMD_SIZE) && size >= PMD_SIZE) 246 map = hyp_fixblock_map(__hyp_pa(va), &map_size); 247 else 248 map = hyp_fixmap_map(__hyp_pa(va)); 249 250 func(map, map_size); 251 252 if (map_size == PMD_SIZE) 253 hyp_fixblock_unmap(); 254 else 255 hyp_fixmap_unmap(); 256 257 size -= map_size; 258 va += map_size; 259 } 260 } 261 262 static void clean_dcache_guest_page(void *va, size_t size) 263 { 264 /* See comment in __clean_dcache_guest_page() */ 265 if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) 266 return; 267 268 __apply_guest_page(va, size, __clean_dcache_guest_page); 269 } 270 271 static void invalidate_icache_guest_page(void *va, size_t size) 272 { 273 if (alternative_has_cap_unlikely(ARM64_HAS_CACHE_DIC)) 274 return; 275 276 __apply_guest_page(va, size, __invalidate_icache_guest_page); 277 } 278 279 int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd) 280 { 281 struct kvm_s2_mmu *mmu = &vm->kvm.arch.mmu; 282 unsigned long nr_pages; 283 int ret; 284 285 nr_pages = kvm_pgtable_stage2_pgd_size(mmu->vtcr) >> PAGE_SHIFT; 286 ret = hyp_pool_init(&vm->pool, hyp_virt_to_pfn(pgd), nr_pages, 0); 287 if (ret) 288 return ret; 289 290 hyp_spin_lock_init(&vm->lock); 291 vm->mm_ops = (struct kvm_pgtable_mm_ops) { 292 .zalloc_pages_exact = guest_s2_zalloc_pages_exact, 293 .free_pages_exact = guest_s2_free_pages_exact, 294 .zalloc_page = guest_s2_zalloc_page, 295 .phys_to_virt = hyp_phys_to_virt, 296 .virt_to_phys = hyp_virt_to_phys, 297 .page_count = hyp_page_count, 298 .get_page = guest_s2_get_page, 299 .put_page = guest_s2_put_page, 300 .dcache_clean_inval_poc = clean_dcache_guest_page, 301 .icache_inval_pou = invalidate_icache_guest_page, 302 }; 303 304 guest_lock_component(vm); 305 ret = __kvm_pgtable_stage2_init(mmu->pgt, mmu, &vm->mm_ops, 0, NULL); 306 guest_unlock_component(vm); 307 if (ret) 308 return ret; 309 310 vm->kvm.arch.mmu.pgd_phys = __hyp_pa(vm->pgt.pgd); 311 312 return 0; 313 } 314 315 void kvm_guest_destroy_stage2(struct pkvm_hyp_vm *vm) 316 { 317 guest_lock_component(vm); 318 kvm_pgtable_stage2_destroy(&vm->pgt); 319 vm->kvm.arch.mmu.pgd_phys = 0ULL; 320 guest_unlock_component(vm); 321 } 322 323 void reclaim_pgtable_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc) 324 { 325 struct hyp_page *page; 326 void *addr; 327 328 /* Dump all pgtable pages in the hyp_pool */ 329 kvm_guest_destroy_stage2(vm); 330 331 /* Drain the hyp_pool into the memcache */ 332 addr = hyp_alloc_pages(&vm->pool, 0); 333 while (addr) { 334 page = hyp_virt_to_page(addr); 335 page->refcount = 0; 336 push_hyp_memcache(mc, addr, hyp_virt_to_phys); 337 WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(addr), 1)); 338 addr = hyp_alloc_pages(&vm->pool, 0); 339 } 340 } 341 342 int __pkvm_prot_finalize(void) 343 { 344 struct kvm_s2_mmu *mmu = &host_mmu.arch.mmu; 345 struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params); 346 347 if (params->hcr_el2 & HCR_EL2_VM) 348 return -EPERM; 349 350 params->vttbr = kvm_get_vttbr(mmu); 351 params->vtcr = mmu->vtcr; 352 params->hcr_el2 |= HCR_EL2_VM; 353 if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) 354 params->hcr_el2 |= HCR_EL2_FWB; 355 356 /* 357 * The CMO below not only cleans the updated params to the 358 * PoC, but also provides the DSB that ensures ongoing 359 * page-table walks that have started before we trapped to EL2 360 * have completed. 361 */ 362 kvm_flush_dcache_to_poc(params, sizeof(*params)); 363 364 write_sysreg_hcr(params->hcr_el2); 365 __load_stage2(&host_mmu.arch.mmu); 366 367 /* 368 * Make sure to have an ISB before the TLB maintenance below but only 369 * when __load_stage2() doesn't include one already. 370 */ 371 asm(ALTERNATIVE("isb", "nop", ARM64_WORKAROUND_SPECULATIVE_AT)); 372 373 /* Invalidate stale HCR bits that may be cached in TLBs */ 374 __tlbi(vmalls12e1); 375 dsb(nsh); 376 isb(); 377 378 return 0; 379 } 380 381 static int host_stage2_unmap_dev_all(void) 382 { 383 struct kvm_pgtable *pgt = &host_mmu.pgt; 384 struct memblock_region *reg; 385 u64 addr = 0; 386 int i, ret; 387 388 /* Unmap all non-memory regions to recycle the pages */ 389 for (i = 0; i < hyp_memblock_nr; i++, addr = reg->base + reg->size) { 390 reg = &hyp_memory[i]; 391 ret = kvm_pgtable_stage2_unmap(pgt, addr, reg->base - addr); 392 if (ret) 393 return ret; 394 } 395 return kvm_pgtable_stage2_unmap(pgt, addr, BIT(pgt->ia_bits) - addr); 396 } 397 398 /* 399 * Ensure the PFN range is contained within PA-range. 400 * 401 * This check is also robust to overflows and is therefore a requirement before 402 * using a pfn/nr_pages pair from an untrusted source. 403 */ 404 static bool pfn_range_is_valid(u64 pfn, u64 nr_pages) 405 { 406 u64 limit = BIT(kvm_phys_shift(&host_mmu.arch.mmu) - PAGE_SHIFT); 407 408 return pfn < limit && ((limit - pfn) >= nr_pages); 409 } 410 411 struct kvm_mem_range { 412 u64 start; 413 u64 end; 414 }; 415 416 static struct memblock_region *find_mem_range(phys_addr_t addr, struct kvm_mem_range *range) 417 { 418 int cur, left = 0, right = hyp_memblock_nr; 419 struct memblock_region *reg; 420 phys_addr_t end; 421 422 range->start = 0; 423 range->end = ULONG_MAX; 424 425 /* The list of memblock regions is sorted, binary search it */ 426 while (left < right) { 427 cur = (left + right) >> 1; 428 reg = &hyp_memory[cur]; 429 end = reg->base + reg->size; 430 if (addr < reg->base) { 431 right = cur; 432 range->end = reg->base; 433 } else if (addr >= end) { 434 left = cur + 1; 435 range->start = end; 436 } else { 437 range->start = reg->base; 438 range->end = end; 439 return reg; 440 } 441 } 442 443 return NULL; 444 } 445 446 bool addr_is_memory(phys_addr_t phys) 447 { 448 struct kvm_mem_range range; 449 450 return !!find_mem_range(phys, &range); 451 } 452 453 bool addr_is_hyp_text(phys_addr_t phys) 454 { 455 phys_addr_t start = ALIGN_DOWN(__hyp_pa(__hyp_text_start), PAGE_SIZE); 456 phys_addr_t end = PAGE_ALIGN(__hyp_pa(__hyp_text_end)); 457 458 return phys >= start && phys < end; 459 } 460 461 static bool is_in_mem_range(u64 addr, struct kvm_mem_range *range) 462 { 463 return range->start <= addr && addr < range->end; 464 } 465 466 static int check_range_allowed_memory(u64 start, u64 end) 467 { 468 struct memblock_region *reg; 469 struct kvm_mem_range range; 470 471 /* 472 * Callers can't check the state of a range that overlaps memory and 473 * MMIO regions, so ensure [start, end[ is in the same kvm_mem_range. 474 */ 475 reg = find_mem_range(start, &range); 476 if (!is_in_mem_range(end - 1, &range)) 477 return -EINVAL; 478 479 if (!reg || reg->flags & MEMBLOCK_NOMAP) 480 return -EPERM; 481 482 return 0; 483 } 484 485 static bool range_is_memory(u64 start, u64 end) 486 { 487 struct kvm_mem_range r; 488 489 if (!find_mem_range(start, &r)) 490 return false; 491 492 return is_in_mem_range(end - 1, &r); 493 } 494 495 static inline int __host_stage2_idmap(u64 start, u64 end, 496 enum kvm_pgtable_prot prot) 497 { 498 /* 499 * We don't make permission changes to the host idmap after 500 * initialisation, so we can squash -EAGAIN to save callers 501 * having to treat it like success in the case that they try to 502 * map something that is already mapped. 503 */ 504 return kvm_pgtable_stage2_map(&host_mmu.pgt, start, end - start, start, 505 prot, &host_s2_pool, 506 KVM_PGTABLE_WALK_IGNORE_EAGAIN); 507 } 508 509 /* 510 * The pool has been provided with enough pages to cover all of memory with 511 * page granularity, but it is difficult to know how much of the MMIO range 512 * we will need to cover upfront, so we may need to 'recycle' the pages if we 513 * run out. 514 */ 515 #define host_stage2_try(fn, ...) \ 516 ({ \ 517 int __ret; \ 518 hyp_assert_lock_held(&host_mmu.lock); \ 519 __ret = fn(__VA_ARGS__); \ 520 if (__ret == -ENOMEM) { \ 521 __ret = host_stage2_unmap_dev_all(); \ 522 if (!__ret) \ 523 __ret = fn(__VA_ARGS__); \ 524 } \ 525 __ret; \ 526 }) 527 528 static inline bool range_included(struct kvm_mem_range *child, 529 struct kvm_mem_range *parent) 530 { 531 return parent->start <= child->start && child->end <= parent->end; 532 } 533 534 static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range) 535 { 536 struct kvm_mem_range cur; 537 kvm_pte_t pte; 538 u64 granule; 539 s8 level; 540 int ret; 541 542 hyp_assert_lock_held(&host_mmu.lock); 543 ret = kvm_pgtable_get_leaf(&host_mmu.pgt, addr, &pte, &level, 0); 544 if (ret) 545 return ret; 546 547 if (kvm_pte_valid(pte)) 548 return -EEXIST; 549 550 if (pte) { 551 WARN_ON(addr_is_memory(addr) && 552 get_host_state(hyp_phys_to_page(addr)) != PKVM_NOPAGE); 553 return -EPERM; 554 } 555 556 for (; level <= KVM_PGTABLE_LAST_LEVEL; level++) { 557 if (!kvm_level_supports_block_mapping(level)) 558 continue; 559 granule = kvm_granule_size(level); 560 cur.start = ALIGN_DOWN(addr, granule); 561 cur.end = cur.start + granule; 562 if (!range_included(&cur, range) && level < KVM_PGTABLE_LAST_LEVEL) 563 continue; 564 *range = cur; 565 return 0; 566 } 567 568 WARN_ON(1); 569 570 return -EINVAL; 571 } 572 573 int host_stage2_idmap_locked(phys_addr_t addr, u64 size, 574 enum kvm_pgtable_prot prot) 575 { 576 return host_stage2_try(__host_stage2_idmap, addr, addr + size, prot); 577 } 578 579 static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_state state) 580 { 581 for_each_hyp_page(page, addr, size) 582 set_host_state(page, state); 583 } 584 585 #define KVM_HOST_DONATION_PTE_OWNER_MASK GENMASK(3, 1) 586 #define KVM_HOST_DONATION_PTE_EXTRA_MASK GENMASK(59, 4) 587 static int host_stage2_set_owner_metadata_locked(phys_addr_t addr, u64 size, 588 u8 owner_id, u64 meta) 589 { 590 kvm_pte_t annotation; 591 int ret; 592 593 if (owner_id == PKVM_ID_HOST) 594 return -EINVAL; 595 596 if (!range_is_memory(addr, addr + size)) 597 return -EPERM; 598 599 if (!FIELD_FIT(KVM_HOST_DONATION_PTE_OWNER_MASK, owner_id)) 600 return -EINVAL; 601 602 if (!FIELD_FIT(KVM_HOST_DONATION_PTE_EXTRA_MASK, meta)) 603 return -EINVAL; 604 605 annotation = FIELD_PREP(KVM_HOST_DONATION_PTE_OWNER_MASK, owner_id) | 606 FIELD_PREP(KVM_HOST_DONATION_PTE_EXTRA_MASK, meta); 607 ret = host_stage2_try(kvm_pgtable_stage2_annotate, &host_mmu.pgt, 608 addr, size, &host_s2_pool, 609 KVM_HOST_INVALID_PTE_TYPE_DONATION, annotation); 610 if (!ret) { 611 /* 612 * After stage2 maintenance has happened, but before the page 613 * owner has changed. 614 */ 615 pkvm_sme_dvmsync_fw_call(); 616 __host_update_page_state(addr, size, PKVM_NOPAGE); 617 } 618 619 return ret; 620 } 621 622 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id) 623 { 624 int ret = -EINVAL; 625 626 switch (owner_id) { 627 case PKVM_ID_HOST: 628 if (!range_is_memory(addr, addr + size)) 629 return -EPERM; 630 631 ret = host_stage2_idmap_locked(addr, size, PKVM_HOST_MEM_PROT); 632 if (!ret) 633 __host_update_page_state(addr, size, PKVM_PAGE_OWNED); 634 break; 635 case PKVM_ID_HYP: 636 ret = host_stage2_set_owner_metadata_locked(addr, size, 637 owner_id, 0); 638 break; 639 } 640 641 return ret; 642 } 643 644 bool host_stage2_pte_is_hyp_owned(kvm_pte_t pte) 645 { 646 if (kvm_pte_valid(pte)) 647 return false; 648 649 if (FIELD_GET(KVM_INVALID_PTE_TYPE_MASK, pte) != 650 KVM_HOST_INVALID_PTE_TYPE_DONATION) 651 return false; 652 653 return FIELD_GET(KVM_HOST_DONATION_PTE_OWNER_MASK, pte) == PKVM_ID_HYP; 654 } 655 656 #define KVM_HOST_PTE_OWNER_GUEST_HANDLE_MASK GENMASK(15, 0) 657 /* We need 40 bits for the GFN to cover a 52-bit IPA with 4k pages and LPA2 */ 658 #define KVM_HOST_PTE_OWNER_GUEST_GFN_MASK GENMASK(55, 16) 659 static u64 host_stage2_encode_gfn_meta(struct pkvm_hyp_vm *vm, u64 gfn) 660 { 661 pkvm_handle_t handle = vm->kvm.arch.pkvm.handle; 662 663 BUILD_BUG_ON((pkvm_handle_t)-1 > KVM_HOST_PTE_OWNER_GUEST_HANDLE_MASK); 664 WARN_ON(!FIELD_FIT(KVM_HOST_PTE_OWNER_GUEST_GFN_MASK, gfn)); 665 666 return FIELD_PREP(KVM_HOST_PTE_OWNER_GUEST_HANDLE_MASK, handle) | 667 FIELD_PREP(KVM_HOST_PTE_OWNER_GUEST_GFN_MASK, gfn); 668 } 669 670 static int host_stage2_decode_gfn_meta(kvm_pte_t pte, struct pkvm_hyp_vm **vm, 671 u64 *gfn) 672 { 673 pkvm_handle_t handle; 674 u64 meta; 675 676 if (WARN_ON(kvm_pte_valid(pte))) 677 return -EINVAL; 678 679 if (FIELD_GET(KVM_INVALID_PTE_TYPE_MASK, pte) != 680 KVM_HOST_INVALID_PTE_TYPE_DONATION) { 681 return -EINVAL; 682 } 683 684 if (FIELD_GET(KVM_HOST_DONATION_PTE_OWNER_MASK, pte) != PKVM_ID_GUEST) 685 return -EPERM; 686 687 meta = FIELD_GET(KVM_HOST_DONATION_PTE_EXTRA_MASK, pte); 688 handle = FIELD_GET(KVM_HOST_PTE_OWNER_GUEST_HANDLE_MASK, meta); 689 *vm = get_vm_by_handle(handle); 690 if (!*vm) { 691 /* We probably raced with teardown; try again */ 692 return -EAGAIN; 693 } 694 695 *gfn = FIELD_GET(KVM_HOST_PTE_OWNER_GUEST_GFN_MASK, meta); 696 return 0; 697 } 698 699 static bool host_stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot prot) 700 { 701 /* 702 * Block mappings must be used with care in the host stage-2 as a 703 * kvm_pgtable_stage2_map() operation targeting a page in the range of 704 * an existing block will delete the block under the assumption that 705 * mappings in the rest of the block range can always be rebuilt lazily. 706 * That assumption is correct for the host stage-2 with RWX mappings 707 * targeting memory or RW mappings targeting MMIO ranges (see 708 * host_stage2_idmap() below which implements some of the host memory 709 * abort logic). However, this is not safe for any other mappings where 710 * the host stage-2 page-table is in fact the only place where this 711 * state is stored. In all those cases, it is safer to use page-level 712 * mappings, hence avoiding to lose the state because of side-effects in 713 * kvm_pgtable_stage2_map(). 714 */ 715 if (range_is_memory(addr, end)) 716 return prot != PKVM_HOST_MEM_PROT; 717 else 718 return prot != PKVM_HOST_MMIO_PROT; 719 } 720 721 static int host_stage2_idmap(u64 addr) 722 { 723 struct kvm_mem_range range; 724 bool is_memory = !!find_mem_range(addr, &range); 725 enum kvm_pgtable_prot prot; 726 int ret; 727 728 prot = is_memory ? PKVM_HOST_MEM_PROT : PKVM_HOST_MMIO_PROT; 729 730 host_lock_component(); 731 ret = host_stage2_adjust_range(addr, &range); 732 if (ret) 733 goto unlock; 734 735 ret = host_stage2_idmap_locked(range.start, range.end - range.start, prot); 736 unlock: 737 host_unlock_component(); 738 739 return ret; 740 } 741 742 static void host_inject_mem_abort(struct kvm_cpu_context *host_ctxt) 743 { 744 u64 ec, esr, spsr; 745 746 esr = read_sysreg_el2(SYS_ESR); 747 spsr = read_sysreg_el2(SYS_SPSR); 748 749 /* Repaint the ESR to report a same-level fault if taken from EL1 */ 750 if ((spsr & PSR_MODE_MASK) != PSR_MODE_EL0t) { 751 ec = ESR_ELx_EC(esr); 752 if (ec == ESR_ELx_EC_DABT_LOW) 753 ec = ESR_ELx_EC_DABT_CUR; 754 else if (ec == ESR_ELx_EC_IABT_LOW) 755 ec = ESR_ELx_EC_IABT_CUR; 756 else 757 WARN_ON(1); 758 esr &= ~ESR_ELx_EC_MASK; 759 esr |= ec << ESR_ELx_EC_SHIFT; 760 } 761 762 /* 763 * Since S1PTW should only ever be set for stage-2 faults, we're pretty 764 * much guaranteed that it won't be set in ESR_EL1 by the hardware. So, 765 * let's use that bit to allow the host abort handler to differentiate 766 * this abort from normal userspace faults. 767 * 768 * Note: although S1PTW is RES0 at EL1, it is guaranteed by the 769 * architecture to be backed by flops, so it should be safe to use. 770 */ 771 esr |= ESR_ELx_S1PTW; 772 inject_host_exception(esr); 773 } 774 775 void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt) 776 { 777 struct kvm_vcpu_fault_info fault; 778 u64 esr, addr; 779 780 esr = read_sysreg_el2(SYS_ESR); 781 if (!__get_fault_info(esr, &fault)) { 782 /* 783 * We've presumably raced with a page-table change which caused 784 * AT to fail, try again. 785 */ 786 return; 787 } 788 789 790 /* 791 * Yikes, we couldn't resolve the fault IPA. This should reinject an 792 * abort into the host when we figure out how to do that. 793 */ 794 BUG_ON(!(fault.hpfar_el2 & HPFAR_EL2_NS)); 795 addr = FIELD_GET(HPFAR_EL2_FIPA, fault.hpfar_el2) << 12; 796 797 switch (host_stage2_idmap(addr)) { 798 case -EPERM: 799 host_inject_mem_abort(host_ctxt); 800 fallthrough; 801 case -EEXIST: 802 case 0: 803 break; 804 default: 805 BUG(); 806 } 807 } 808 809 struct check_walk_data { 810 enum pkvm_page_state desired; 811 enum pkvm_page_state (*get_page_state)(kvm_pte_t pte, u64 addr); 812 }; 813 814 static int __check_page_state_visitor(const struct kvm_pgtable_visit_ctx *ctx, 815 enum kvm_pgtable_walk_flags visit) 816 { 817 struct check_walk_data *d = ctx->arg; 818 819 return d->get_page_state(ctx->old, ctx->addr) == d->desired ? 0 : -EPERM; 820 } 821 822 static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size, 823 struct check_walk_data *data) 824 { 825 struct kvm_pgtable_walker walker = { 826 .cb = __check_page_state_visitor, 827 .arg = data, 828 .flags = KVM_PGTABLE_WALK_LEAF, 829 }; 830 831 return kvm_pgtable_walk(pgt, addr, size, &walker); 832 } 833 834 static int __host_check_page_state_range(u64 addr, u64 size, 835 enum pkvm_page_state state) 836 { 837 int ret; 838 839 ret = check_range_allowed_memory(addr, addr + size); 840 if (ret) 841 return ret; 842 843 hyp_assert_lock_held(&host_mmu.lock); 844 845 for_each_hyp_page(page, addr, size) { 846 if (get_host_state(page) != state) 847 return -EPERM; 848 } 849 850 return 0; 851 } 852 853 static int __host_set_page_state_range(u64 addr, u64 size, 854 enum pkvm_page_state state) 855 { 856 if (get_host_state(hyp_phys_to_page(addr)) == PKVM_NOPAGE) { 857 int ret = host_stage2_idmap_locked(addr, size, PKVM_HOST_MEM_PROT); 858 859 if (ret) 860 return ret; 861 } 862 863 __host_update_page_state(addr, size, state); 864 865 return 0; 866 } 867 868 static void __hyp_set_page_state_range(phys_addr_t phys, u64 size, enum pkvm_page_state state) 869 { 870 for_each_hyp_page(page, phys, size) 871 set_hyp_state(page, state); 872 } 873 874 static int __hyp_check_page_state_range(phys_addr_t phys, u64 size, enum pkvm_page_state state) 875 { 876 for_each_hyp_page(page, phys, size) { 877 if (get_hyp_state(page) != state) 878 return -EPERM; 879 } 880 881 return 0; 882 } 883 884 static int __hyp_check_page_count_range(phys_addr_t phys, u64 size) 885 { 886 for_each_hyp_page(page, phys, size) { 887 if (page->refcount) 888 return -EBUSY; 889 } 890 891 return 0; 892 } 893 894 static bool guest_pte_is_poisoned(kvm_pte_t pte) 895 { 896 if (kvm_pte_valid(pte)) 897 return false; 898 899 return FIELD_GET(KVM_INVALID_PTE_TYPE_MASK, pte) == 900 KVM_GUEST_INVALID_PTE_TYPE_POISONED; 901 } 902 903 static enum pkvm_page_state guest_get_page_state(kvm_pte_t pte, u64 addr) 904 { 905 if (guest_pte_is_poisoned(pte)) 906 return PKVM_POISON; 907 908 if (!kvm_pte_valid(pte)) 909 return PKVM_NOPAGE; 910 911 return pkvm_getstate(kvm_pgtable_stage2_pte_prot(pte)); 912 } 913 914 static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr, 915 u64 size, enum pkvm_page_state state) 916 { 917 struct check_walk_data d = { 918 .desired = state, 919 .get_page_state = guest_get_page_state, 920 }; 921 922 hyp_assert_lock_held(&vm->lock); 923 return check_page_state_range(&vm->pgt, addr, size, &d); 924 } 925 926 static int get_valid_guest_pte(struct pkvm_hyp_vm *vm, u64 ipa, kvm_pte_t *ptep, u64 *physp) 927 { 928 kvm_pte_t pte; 929 u64 phys; 930 s8 level; 931 int ret; 932 933 ret = kvm_pgtable_get_leaf(&vm->pgt, ipa, &pte, &level, 0); 934 if (ret) 935 return ret; 936 if (guest_pte_is_poisoned(pte)) 937 return -EHWPOISON; 938 if (!kvm_pte_valid(pte)) 939 return -ENOENT; 940 if (level != KVM_PGTABLE_LAST_LEVEL) 941 return -E2BIG; 942 943 phys = kvm_pte_to_phys(pte); 944 ret = check_range_allowed_memory(phys, phys + PAGE_SIZE); 945 if (WARN_ON(ret)) 946 return ret; 947 948 *ptep = pte; 949 *physp = phys; 950 951 return 0; 952 } 953 954 int __pkvm_vcpu_in_poison_fault(struct pkvm_hyp_vcpu *hyp_vcpu) 955 { 956 struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(hyp_vcpu); 957 kvm_pte_t pte; 958 s8 level; 959 u64 ipa; 960 int ret; 961 962 switch (kvm_vcpu_trap_get_class(&hyp_vcpu->vcpu)) { 963 case ESR_ELx_EC_DABT_LOW: 964 case ESR_ELx_EC_IABT_LOW: 965 if (kvm_vcpu_trap_is_translation_fault(&hyp_vcpu->vcpu)) 966 break; 967 fallthrough; 968 default: 969 return -EINVAL; 970 } 971 972 /* 973 * The host has the faulting IPA when it calls us from the guest 974 * fault handler but we retrieve it ourselves from the FAR so as 975 * to avoid exposing an "oracle" that could reveal data access 976 * patterns of the guest after initial donation of its pages. 977 */ 978 ipa = kvm_vcpu_get_fault_ipa(&hyp_vcpu->vcpu); 979 ipa |= FAR_TO_FIPA_OFFSET(kvm_vcpu_get_hfar(&hyp_vcpu->vcpu)); 980 981 guest_lock_component(vm); 982 ret = kvm_pgtable_get_leaf(&vm->pgt, ipa, &pte, &level, 0); 983 if (ret) 984 goto unlock; 985 986 if (level != KVM_PGTABLE_LAST_LEVEL) { 987 ret = -EINVAL; 988 goto unlock; 989 } 990 991 ret = guest_pte_is_poisoned(pte); 992 unlock: 993 guest_unlock_component(vm); 994 return ret; 995 } 996 997 int __pkvm_host_share_hyp(u64 pfn) 998 { 999 u64 phys = hyp_pfn_to_phys(pfn); 1000 u64 size = PAGE_SIZE; 1001 int ret; 1002 1003 host_lock_component(); 1004 hyp_lock_component(); 1005 1006 ret = __host_check_page_state_range(phys, size, PKVM_PAGE_OWNED); 1007 if (ret) 1008 goto unlock; 1009 ret = __hyp_check_page_state_range(phys, size, PKVM_NOPAGE); 1010 if (ret) 1011 goto unlock; 1012 1013 __hyp_set_page_state_range(phys, size, PKVM_PAGE_SHARED_BORROWED); 1014 WARN_ON(__host_set_page_state_range(phys, size, PKVM_PAGE_SHARED_OWNED)); 1015 1016 unlock: 1017 hyp_unlock_component(); 1018 host_unlock_component(); 1019 1020 return ret; 1021 } 1022 1023 int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn) 1024 { 1025 struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu); 1026 u64 phys, ipa = hyp_pfn_to_phys(gfn); 1027 kvm_pte_t pte; 1028 int ret; 1029 1030 host_lock_component(); 1031 guest_lock_component(vm); 1032 1033 ret = get_valid_guest_pte(vm, ipa, &pte, &phys); 1034 if (ret) 1035 goto unlock; 1036 1037 ret = -EPERM; 1038 if (pkvm_getstate(kvm_pgtable_stage2_pte_prot(pte)) != PKVM_PAGE_OWNED) 1039 goto unlock; 1040 if (__host_check_page_state_range(phys, PAGE_SIZE, PKVM_NOPAGE)) 1041 goto unlock; 1042 1043 ret = 0; 1044 WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, PAGE_SIZE, phys, 1045 pkvm_mkstate(KVM_PGTABLE_PROT_RWX, PKVM_PAGE_SHARED_OWNED), 1046 &vcpu->vcpu.arch.stage2_mc, 0)); 1047 WARN_ON(__host_set_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_BORROWED)); 1048 unlock: 1049 guest_unlock_component(vm); 1050 host_unlock_component(); 1051 1052 return ret; 1053 } 1054 1055 int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn) 1056 { 1057 struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu); 1058 u64 meta, phys, ipa = hyp_pfn_to_phys(gfn); 1059 kvm_pte_t pte; 1060 int ret; 1061 1062 host_lock_component(); 1063 guest_lock_component(vm); 1064 1065 ret = get_valid_guest_pte(vm, ipa, &pte, &phys); 1066 if (ret) 1067 goto unlock; 1068 1069 ret = -EPERM; 1070 if (pkvm_getstate(kvm_pgtable_stage2_pte_prot(pte)) != PKVM_PAGE_SHARED_OWNED) 1071 goto unlock; 1072 if (__host_check_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_BORROWED)) 1073 goto unlock; 1074 1075 ret = 0; 1076 meta = host_stage2_encode_gfn_meta(vm, gfn); 1077 WARN_ON(host_stage2_set_owner_metadata_locked(phys, PAGE_SIZE, 1078 PKVM_ID_GUEST, meta)); 1079 WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, PAGE_SIZE, phys, 1080 pkvm_mkstate(KVM_PGTABLE_PROT_RWX, PKVM_PAGE_OWNED), 1081 &vcpu->vcpu.arch.stage2_mc, 0)); 1082 unlock: 1083 guest_unlock_component(vm); 1084 host_unlock_component(); 1085 1086 return ret; 1087 } 1088 1089 int __pkvm_host_unshare_hyp(u64 pfn) 1090 { 1091 u64 phys = hyp_pfn_to_phys(pfn); 1092 u64 size = PAGE_SIZE; 1093 int ret; 1094 1095 host_lock_component(); 1096 hyp_lock_component(); 1097 1098 ret = __host_check_page_state_range(phys, size, PKVM_PAGE_SHARED_OWNED); 1099 if (ret) 1100 goto unlock; 1101 ret = __hyp_check_page_state_range(phys, size, PKVM_PAGE_SHARED_BORROWED); 1102 if (ret) 1103 goto unlock; 1104 ret = __hyp_check_page_count_range(phys, size); 1105 if (ret) 1106 goto unlock; 1107 1108 __hyp_set_page_state_range(phys, size, PKVM_NOPAGE); 1109 WARN_ON(__host_set_page_state_range(phys, size, PKVM_PAGE_OWNED)); 1110 1111 unlock: 1112 hyp_unlock_component(); 1113 host_unlock_component(); 1114 1115 return ret; 1116 } 1117 1118 int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) 1119 { 1120 u64 phys = hyp_pfn_to_phys(pfn); 1121 u64 size = PAGE_SIZE * nr_pages; 1122 void *virt = __hyp_va(phys); 1123 int ret; 1124 1125 if (!pfn_range_is_valid(pfn, nr_pages)) 1126 return -EINVAL; 1127 1128 host_lock_component(); 1129 hyp_lock_component(); 1130 1131 ret = __host_check_page_state_range(phys, size, PKVM_PAGE_OWNED); 1132 if (ret) 1133 goto unlock; 1134 ret = __hyp_check_page_state_range(phys, size, PKVM_NOPAGE); 1135 if (ret) 1136 goto unlock; 1137 1138 __hyp_set_page_state_range(phys, size, PKVM_PAGE_OWNED); 1139 WARN_ON(pkvm_create_mappings_locked(virt, virt + size, PAGE_HYP)); 1140 WARN_ON(host_stage2_set_owner_locked(phys, size, PKVM_ID_HYP)); 1141 1142 unlock: 1143 hyp_unlock_component(); 1144 host_unlock_component(); 1145 1146 return ret; 1147 } 1148 1149 int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages) 1150 { 1151 u64 phys = hyp_pfn_to_phys(pfn); 1152 u64 size = PAGE_SIZE * nr_pages; 1153 u64 virt = (u64)__hyp_va(phys); 1154 int ret; 1155 1156 if (!pfn_range_is_valid(pfn, nr_pages)) 1157 return -EINVAL; 1158 1159 host_lock_component(); 1160 hyp_lock_component(); 1161 1162 ret = __hyp_check_page_state_range(phys, size, PKVM_PAGE_OWNED); 1163 if (ret) 1164 goto unlock; 1165 ret = __host_check_page_state_range(phys, size, PKVM_NOPAGE); 1166 if (ret) 1167 goto unlock; 1168 1169 ret = __hyp_check_page_count_range(phys, size); 1170 if (ret) 1171 goto unlock; 1172 1173 __hyp_set_page_state_range(phys, size, PKVM_NOPAGE); 1174 WARN_ON(kvm_pgtable_hyp_unmap(&pkvm_pgtable, virt, size) != size); 1175 WARN_ON(host_stage2_set_owner_locked(phys, size, PKVM_ID_HOST)); 1176 1177 unlock: 1178 hyp_unlock_component(); 1179 host_unlock_component(); 1180 1181 return ret; 1182 } 1183 1184 int hyp_pin_shared_mem(void *from, void *to) 1185 { 1186 u64 cur, start = ALIGN_DOWN((u64)from, PAGE_SIZE); 1187 u64 end = PAGE_ALIGN((u64)to); 1188 u64 phys = __hyp_pa(start); 1189 u64 size = end - start; 1190 struct hyp_page *p; 1191 int ret; 1192 1193 host_lock_component(); 1194 hyp_lock_component(); 1195 1196 ret = __host_check_page_state_range(phys, size, PKVM_PAGE_SHARED_OWNED); 1197 if (ret) 1198 goto unlock; 1199 1200 ret = __hyp_check_page_state_range(phys, size, PKVM_PAGE_SHARED_BORROWED); 1201 if (ret) 1202 goto unlock; 1203 1204 for (cur = start; cur < end; cur += PAGE_SIZE) { 1205 p = hyp_virt_to_page(cur); 1206 hyp_page_ref_inc(p); 1207 if (p->refcount == 1) 1208 WARN_ON(pkvm_create_mappings_locked((void *)cur, 1209 (void *)cur + PAGE_SIZE, 1210 PAGE_HYP)); 1211 } 1212 1213 /* 1214 * Only the first pin of a page maps it, and that CPU's barriers order 1215 * only its own accesses. Order this CPU's accesses through the range 1216 * after the mapping, whichever pin made it. 1217 */ 1218 dsb(ish); 1219 isb(); 1220 1221 unlock: 1222 hyp_unlock_component(); 1223 host_unlock_component(); 1224 1225 return ret; 1226 } 1227 1228 void hyp_unpin_shared_mem(void *from, void *to) 1229 { 1230 u64 cur, start = ALIGN_DOWN((u64)from, PAGE_SIZE); 1231 u64 end = PAGE_ALIGN((u64)to); 1232 struct hyp_page *p; 1233 1234 host_lock_component(); 1235 hyp_lock_component(); 1236 1237 for (cur = start; cur < end; cur += PAGE_SIZE) { 1238 p = hyp_virt_to_page(cur); 1239 if (p->refcount == 1) 1240 WARN_ON(kvm_pgtable_hyp_unmap(&pkvm_pgtable, cur, PAGE_SIZE) != PAGE_SIZE); 1241 hyp_page_ref_dec(p); 1242 } 1243 1244 hyp_unlock_component(); 1245 host_unlock_component(); 1246 } 1247 1248 int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages) 1249 { 1250 u64 phys = hyp_pfn_to_phys(pfn); 1251 u64 size = PAGE_SIZE * nr_pages; 1252 int ret; 1253 1254 if (!pfn_range_is_valid(pfn, nr_pages)) 1255 return -EINVAL; 1256 1257 host_lock_component(); 1258 ret = __host_check_page_state_range(phys, size, PKVM_PAGE_OWNED); 1259 if (!ret) 1260 ret = __host_set_page_state_range(phys, size, PKVM_PAGE_SHARED_OWNED); 1261 host_unlock_component(); 1262 1263 return ret; 1264 } 1265 1266 int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) 1267 { 1268 u64 phys = hyp_pfn_to_phys(pfn); 1269 u64 size = PAGE_SIZE * nr_pages; 1270 int ret; 1271 1272 if (!pfn_range_is_valid(pfn, nr_pages)) 1273 return -EINVAL; 1274 1275 host_lock_component(); 1276 ret = __host_check_page_state_range(phys, size, PKVM_PAGE_SHARED_OWNED); 1277 if (!ret) 1278 ret = __host_set_page_state_range(phys, size, PKVM_PAGE_OWNED); 1279 host_unlock_component(); 1280 1281 return ret; 1282 } 1283 1284 static int __guest_check_transition_size(u64 phys, u64 ipa, u64 nr_pages, u64 *size) 1285 { 1286 size_t block_size; 1287 1288 if (nr_pages == 1) { 1289 *size = PAGE_SIZE; 1290 return 0; 1291 } 1292 1293 /* We solely support second to last level huge mapping */ 1294 block_size = kvm_granule_size(KVM_PGTABLE_LAST_LEVEL - 1); 1295 1296 if (nr_pages != block_size >> PAGE_SHIFT) 1297 return -EINVAL; 1298 1299 if (!IS_ALIGNED(phys | ipa, block_size)) 1300 return -EINVAL; 1301 1302 *size = block_size; 1303 return 0; 1304 } 1305 1306 static void hyp_poison_page(phys_addr_t phys) 1307 { 1308 void *addr = hyp_fixmap_map(phys); 1309 1310 memset(addr, 0, PAGE_SIZE); 1311 /* 1312 * Prefer kvm_flush_dcache_to_poc() over __clean_dcache_guest_page() 1313 * here as the latter may elide the CMO under the assumption that FWB 1314 * will be enabled on CPUs that support it. This is incorrect for the 1315 * host stage-2 and would otherwise lead to a malicious host potentially 1316 * being able to read the contents of newly reclaimed guest pages. 1317 */ 1318 kvm_flush_dcache_to_poc(addr, PAGE_SIZE); 1319 hyp_fixmap_unmap(); 1320 } 1321 1322 static int host_stage2_get_guest_info(phys_addr_t phys, struct pkvm_hyp_vm **vm, 1323 u64 *gfn) 1324 { 1325 enum pkvm_page_state state; 1326 kvm_pte_t pte; 1327 s8 level; 1328 int ret; 1329 1330 if (!addr_is_memory(phys)) 1331 return -EFAULT; 1332 1333 state = get_host_state(hyp_phys_to_page(phys)); 1334 switch (state) { 1335 case PKVM_PAGE_OWNED: 1336 case PKVM_PAGE_SHARED_OWNED: 1337 case PKVM_PAGE_SHARED_BORROWED: 1338 /* The access should no longer fault; try again. */ 1339 return -EAGAIN; 1340 case PKVM_NOPAGE: 1341 break; 1342 default: 1343 return -EPERM; 1344 } 1345 1346 ret = kvm_pgtable_get_leaf(&host_mmu.pgt, phys, &pte, &level, 0); 1347 if (ret) 1348 return ret; 1349 1350 if (WARN_ON(level != KVM_PGTABLE_LAST_LEVEL)) 1351 return -EINVAL; 1352 1353 return host_stage2_decode_gfn_meta(pte, vm, gfn); 1354 } 1355 1356 int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys) 1357 { 1358 struct pkvm_hyp_vm *vm; 1359 u64 gfn, ipa, pa; 1360 kvm_pte_t pte; 1361 int ret; 1362 1363 phys &= PAGE_MASK; 1364 1365 hyp_spin_lock(&vm_table_lock); 1366 host_lock_component(); 1367 1368 ret = host_stage2_get_guest_info(phys, &vm, &gfn); 1369 if (ret) 1370 goto unlock_host; 1371 1372 ipa = hyp_pfn_to_phys(gfn); 1373 guest_lock_component(vm); 1374 ret = get_valid_guest_pte(vm, ipa, &pte, &pa); 1375 if (ret) 1376 goto unlock_guest; 1377 1378 WARN_ON(pa != phys); 1379 if (guest_get_page_state(pte, ipa) != PKVM_PAGE_OWNED) { 1380 ret = -EPERM; 1381 goto unlock_guest; 1382 } 1383 1384 /* We really shouldn't be allocating, so don't pass a memcache */ 1385 ret = kvm_pgtable_stage2_annotate(&vm->pgt, ipa, PAGE_SIZE, NULL, 1386 KVM_GUEST_INVALID_PTE_TYPE_POISONED, 1387 0); 1388 if (ret) 1389 goto unlock_guest; 1390 1391 hyp_poison_page(phys); 1392 WARN_ON(host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HOST)); 1393 unlock_guest: 1394 guest_unlock_component(vm); 1395 unlock_host: 1396 host_unlock_component(); 1397 hyp_spin_unlock(&vm_table_lock); 1398 1399 return ret; 1400 } 1401 1402 int __pkvm_host_reclaim_page_guest(u64 gfn, struct pkvm_hyp_vm *vm) 1403 { 1404 u64 ipa = hyp_pfn_to_phys(gfn); 1405 kvm_pte_t pte; 1406 u64 phys; 1407 int ret; 1408 1409 host_lock_component(); 1410 guest_lock_component(vm); 1411 1412 ret = get_valid_guest_pte(vm, ipa, &pte, &phys); 1413 if (ret) 1414 goto unlock; 1415 1416 switch (guest_get_page_state(pte, ipa)) { 1417 case PKVM_PAGE_OWNED: 1418 WARN_ON(__host_check_page_state_range(phys, PAGE_SIZE, PKVM_NOPAGE)); 1419 hyp_poison_page(phys); 1420 break; 1421 case PKVM_PAGE_SHARED_OWNED: 1422 WARN_ON(__host_check_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_BORROWED)); 1423 break; 1424 default: 1425 ret = -EPERM; 1426 goto unlock; 1427 } 1428 1429 WARN_ON(kvm_pgtable_stage2_unmap(&vm->pgt, ipa, PAGE_SIZE)); 1430 WARN_ON(host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HOST)); 1431 1432 unlock: 1433 guest_unlock_component(vm); 1434 host_unlock_component(); 1435 1436 /* 1437 * -EHWPOISON implies that the page was forcefully reclaimed already 1438 * so return success for the GUP pin to be dropped. 1439 */ 1440 return ret && ret != -EHWPOISON ? ret : 0; 1441 } 1442 1443 /* 1444 * share/donate install at most one stage-2 leaf (PAGE_SIZE, or one 1445 * KVM_PGTABLE_LAST_LEVEL - 1 block for share). kvm_mmu_cache_min_pages() 1446 * bounds the worst-case allocation: exact for the PAGE_SIZE leaf, 1447 * conservative by one for the block. 1448 */ 1449 static int __guest_check_pgtable_memcache(struct pkvm_hyp_vcpu *vcpu) 1450 { 1451 struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu); 1452 1453 if (vcpu->vcpu.arch.stage2_mc.nr_pages < kvm_mmu_cache_min_pages(vm->pgt.mmu)) 1454 return -ENOMEM; 1455 1456 return 0; 1457 } 1458 1459 int __pkvm_host_donate_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu) 1460 { 1461 struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu); 1462 u64 phys = hyp_pfn_to_phys(pfn); 1463 u64 ipa = hyp_pfn_to_phys(gfn); 1464 u64 meta; 1465 int ret; 1466 1467 host_lock_component(); 1468 guest_lock_component(vm); 1469 1470 ret = __host_check_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_OWNED); 1471 if (ret) 1472 goto unlock; 1473 1474 ret = __guest_check_page_state_range(vm, ipa, PAGE_SIZE, PKVM_NOPAGE); 1475 if (ret) 1476 goto unlock; 1477 1478 ret = __guest_check_pgtable_memcache(vcpu); 1479 if (ret) 1480 goto unlock; 1481 1482 meta = host_stage2_encode_gfn_meta(vm, gfn); 1483 WARN_ON(host_stage2_set_owner_metadata_locked(phys, PAGE_SIZE, 1484 PKVM_ID_GUEST, meta)); 1485 WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, PAGE_SIZE, phys, 1486 pkvm_mkstate(KVM_PGTABLE_PROT_RWX, PKVM_PAGE_OWNED), 1487 &vcpu->vcpu.arch.stage2_mc, 0)); 1488 1489 unlock: 1490 guest_unlock_component(vm); 1491 host_unlock_component(); 1492 1493 return ret; 1494 } 1495 1496 int __pkvm_host_share_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu *vcpu, 1497 enum kvm_pgtable_prot prot) 1498 { 1499 struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu); 1500 u64 phys = hyp_pfn_to_phys(pfn); 1501 u64 ipa = hyp_pfn_to_phys(gfn); 1502 u64 size; 1503 int ret; 1504 1505 if (prot & ~KVM_PGTABLE_PROT_RWX) 1506 return -EINVAL; 1507 1508 if (!pfn_range_is_valid(pfn, nr_pages)) 1509 return -EINVAL; 1510 1511 ret = __guest_check_transition_size(phys, ipa, nr_pages, &size); 1512 if (ret) 1513 return ret; 1514 1515 ret = check_range_allowed_memory(phys, phys + size); 1516 if (ret) 1517 return ret; 1518 1519 host_lock_component(); 1520 guest_lock_component(vm); 1521 1522 ret = __guest_check_page_state_range(vm, ipa, size, PKVM_NOPAGE); 1523 if (ret) 1524 goto unlock; 1525 1526 for_each_hyp_page(page, phys, size) { 1527 switch (get_host_state(page)) { 1528 case PKVM_PAGE_OWNED: 1529 continue; 1530 case PKVM_PAGE_SHARED_OWNED: 1531 if (page->host_share_guest_count == U32_MAX) { 1532 ret = -EBUSY; 1533 goto unlock; 1534 } 1535 1536 /* Only host to np-guest multi-sharing is tolerated */ 1537 if (page->host_share_guest_count) 1538 continue; 1539 1540 fallthrough; 1541 default: 1542 ret = -EPERM; 1543 goto unlock; 1544 } 1545 } 1546 1547 ret = __guest_check_pgtable_memcache(vcpu); 1548 if (ret) 1549 goto unlock; 1550 1551 for_each_hyp_page(page, phys, size) { 1552 set_host_state(page, PKVM_PAGE_SHARED_OWNED); 1553 page->host_share_guest_count++; 1554 } 1555 1556 WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, size, phys, 1557 pkvm_mkstate(prot, PKVM_PAGE_SHARED_BORROWED), 1558 &vcpu->vcpu.arch.stage2_mc, 0)); 1559 1560 unlock: 1561 guest_unlock_component(vm); 1562 host_unlock_component(); 1563 1564 return ret; 1565 } 1566 1567 static int __check_host_shared_guest(struct pkvm_hyp_vm *vm, u64 *__phys, u64 ipa, u64 size) 1568 { 1569 enum pkvm_page_state state; 1570 kvm_pte_t pte; 1571 u64 phys; 1572 s8 level; 1573 int ret; 1574 1575 ret = kvm_pgtable_get_leaf(&vm->pgt, ipa, &pte, &level, 0); 1576 if (ret) 1577 return ret; 1578 if (!kvm_pte_valid(pte)) 1579 return -ENOENT; 1580 if (size && kvm_granule_size(level) != size) 1581 return -E2BIG; 1582 1583 if (!size) 1584 size = kvm_granule_size(level); 1585 1586 state = guest_get_page_state(pte, ipa); 1587 if (state != PKVM_PAGE_SHARED_BORROWED) 1588 return -EPERM; 1589 1590 phys = kvm_pte_to_phys(pte); 1591 ret = check_range_allowed_memory(phys, phys + size); 1592 if (WARN_ON(ret)) 1593 return ret; 1594 1595 for_each_hyp_page(page, phys, size) { 1596 if (get_host_state(page) != PKVM_PAGE_SHARED_OWNED) 1597 return -EPERM; 1598 if (WARN_ON(!page->host_share_guest_count)) 1599 return -EINVAL; 1600 } 1601 1602 *__phys = phys; 1603 1604 return 0; 1605 } 1606 1607 int __pkvm_host_unshare_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm) 1608 { 1609 u64 ipa = hyp_pfn_to_phys(gfn); 1610 u64 size, phys; 1611 int ret; 1612 1613 ret = __guest_check_transition_size(0, ipa, nr_pages, &size); 1614 if (ret) 1615 return ret; 1616 1617 host_lock_component(); 1618 guest_lock_component(vm); 1619 1620 ret = __check_host_shared_guest(vm, &phys, ipa, size); 1621 if (ret) 1622 goto unlock; 1623 1624 ret = kvm_pgtable_stage2_unmap(&vm->pgt, ipa, size); 1625 if (ret) 1626 goto unlock; 1627 1628 for_each_hyp_page(page, phys, size) { 1629 /* __check_host_shared_guest() protects against underflow */ 1630 page->host_share_guest_count--; 1631 if (!page->host_share_guest_count) 1632 set_host_state(page, PKVM_PAGE_OWNED); 1633 } 1634 1635 unlock: 1636 guest_unlock_component(vm); 1637 host_unlock_component(); 1638 1639 return ret; 1640 } 1641 1642 static void assert_host_shared_guest(struct pkvm_hyp_vm *vm, u64 ipa, u64 size) 1643 { 1644 u64 phys; 1645 int ret; 1646 1647 if (!IS_ENABLED(CONFIG_NVHE_EL2_DEBUG)) 1648 return; 1649 1650 host_lock_component(); 1651 guest_lock_component(vm); 1652 1653 ret = __check_host_shared_guest(vm, &phys, ipa, size); 1654 1655 guest_unlock_component(vm); 1656 host_unlock_component(); 1657 1658 WARN_ON(ret && ret != -ENOENT); 1659 } 1660 1661 int __pkvm_host_relax_perms_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu, enum kvm_pgtable_prot prot) 1662 { 1663 struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu); 1664 u64 ipa = hyp_pfn_to_phys(gfn); 1665 int ret; 1666 1667 if (pkvm_hyp_vm_is_protected(vm)) 1668 return -EPERM; 1669 1670 if (prot & ~KVM_PGTABLE_PROT_RWX) 1671 return -EINVAL; 1672 1673 assert_host_shared_guest(vm, ipa, 0); 1674 guest_lock_component(vm); 1675 ret = kvm_pgtable_stage2_relax_perms(&vm->pgt, ipa, prot, 0); 1676 guest_unlock_component(vm); 1677 1678 return ret; 1679 } 1680 1681 int __pkvm_host_wrprotect_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm) 1682 { 1683 u64 size, ipa = hyp_pfn_to_phys(gfn); 1684 int ret; 1685 1686 if (pkvm_hyp_vm_is_protected(vm)) 1687 return -EPERM; 1688 1689 ret = __guest_check_transition_size(0, ipa, nr_pages, &size); 1690 if (ret) 1691 return ret; 1692 1693 assert_host_shared_guest(vm, ipa, size); 1694 guest_lock_component(vm); 1695 ret = kvm_pgtable_stage2_wrprotect(&vm->pgt, ipa, size); 1696 guest_unlock_component(vm); 1697 1698 return ret; 1699 } 1700 1701 int __pkvm_host_test_clear_young_guest(u64 gfn, u64 nr_pages, bool mkold, struct pkvm_hyp_vm *vm) 1702 { 1703 u64 size, ipa = hyp_pfn_to_phys(gfn); 1704 int ret; 1705 1706 if (pkvm_hyp_vm_is_protected(vm)) 1707 return -EPERM; 1708 1709 ret = __guest_check_transition_size(0, ipa, nr_pages, &size); 1710 if (ret) 1711 return ret; 1712 1713 assert_host_shared_guest(vm, ipa, size); 1714 guest_lock_component(vm); 1715 ret = kvm_pgtable_stage2_test_clear_young(&vm->pgt, ipa, size, mkold); 1716 guest_unlock_component(vm); 1717 1718 return ret; 1719 } 1720 1721 int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu) 1722 { 1723 struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu); 1724 u64 ipa = hyp_pfn_to_phys(gfn); 1725 1726 if (pkvm_hyp_vm_is_protected(vm)) 1727 return -EPERM; 1728 1729 assert_host_shared_guest(vm, ipa, 0); 1730 guest_lock_component(vm); 1731 kvm_pgtable_stage2_mkyoung(&vm->pgt, ipa, 0); 1732 guest_unlock_component(vm); 1733 1734 return 0; 1735 } 1736 1737 #ifdef CONFIG_NVHE_EL2_DEBUG 1738 struct pkvm_expected_state { 1739 enum pkvm_page_state host; 1740 enum pkvm_page_state hyp; 1741 enum pkvm_page_state guest[2]; /* [ gfn, gfn + 1 ] */ 1742 }; 1743 1744 static struct pkvm_expected_state selftest_state; 1745 static struct hyp_page *selftest_page; 1746 static struct pkvm_hyp_vcpu *selftest_vcpu; 1747 1748 static u64 selftest_ipa(void) 1749 { 1750 return BIT(selftest_vcpu->vcpu.arch.hw_mmu->pgt->ia_bits - 1); 1751 } 1752 1753 static void assert_page_state(void) 1754 { 1755 void *virt = hyp_page_to_virt(selftest_page); 1756 u64 size = PAGE_SIZE << selftest_page->order; 1757 struct pkvm_hyp_vcpu *vcpu = selftest_vcpu; 1758 u64 phys = hyp_virt_to_phys(virt); 1759 u64 ipa[2] = { selftest_ipa(), selftest_ipa() + PAGE_SIZE }; 1760 struct pkvm_hyp_vm *vm; 1761 1762 vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu); 1763 1764 host_lock_component(); 1765 WARN_ON(__host_check_page_state_range(phys, size, selftest_state.host)); 1766 host_unlock_component(); 1767 1768 hyp_lock_component(); 1769 WARN_ON(__hyp_check_page_state_range(phys, size, selftest_state.hyp)); 1770 hyp_unlock_component(); 1771 1772 guest_lock_component(vm); 1773 WARN_ON(__guest_check_page_state_range(vm, ipa[0], size, selftest_state.guest[0])); 1774 WARN_ON(__guest_check_page_state_range(vm, ipa[1], size, selftest_state.guest[1])); 1775 guest_unlock_component(vm); 1776 } 1777 1778 #define assert_transition_res(res, fn, ...) \ 1779 do { \ 1780 WARN_ON(fn(__VA_ARGS__) != res); \ 1781 assert_page_state(); \ 1782 } while (0) 1783 1784 void pkvm_ownership_selftest(void *base) 1785 { 1786 enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_RWX; 1787 void *virt = hyp_alloc_pages(&host_s2_pool, 0); 1788 struct pkvm_hyp_vcpu *vcpu; 1789 u64 phys, size, pfn, gfn; 1790 struct pkvm_hyp_vm *vm; 1791 1792 WARN_ON(!virt); 1793 selftest_page = hyp_virt_to_page(virt); 1794 selftest_page->refcount = 0; 1795 selftest_vcpu = vcpu = init_selftest_vm(base); 1796 vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu); 1797 1798 size = PAGE_SIZE << selftest_page->order; 1799 phys = hyp_virt_to_phys(virt); 1800 pfn = hyp_phys_to_pfn(phys); 1801 gfn = hyp_phys_to_pfn(selftest_ipa()); 1802 1803 selftest_state.host = PKVM_NOPAGE; 1804 selftest_state.hyp = PKVM_PAGE_OWNED; 1805 selftest_state.guest[0] = selftest_state.guest[1] = PKVM_NOPAGE; 1806 assert_page_state(); 1807 assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); 1808 assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); 1809 assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); 1810 assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); 1811 assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1); 1812 assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size); 1813 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot); 1814 assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm); 1815 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu); 1816 1817 selftest_state.host = PKVM_PAGE_OWNED; 1818 selftest_state.hyp = PKVM_NOPAGE; 1819 assert_transition_res(0, __pkvm_hyp_donate_host, pfn, 1); 1820 assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); 1821 assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); 1822 assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1); 1823 assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm); 1824 assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size); 1825 1826 selftest_state.host = PKVM_PAGE_SHARED_OWNED; 1827 selftest_state.hyp = PKVM_PAGE_SHARED_BORROWED; 1828 assert_transition_res(0, __pkvm_host_share_hyp, pfn); 1829 assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); 1830 assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); 1831 assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); 1832 assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); 1833 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot); 1834 assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm); 1835 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu); 1836 1837 assert_transition_res(0, hyp_pin_shared_mem, virt, virt + size); 1838 assert_transition_res(0, hyp_pin_shared_mem, virt, virt + size); 1839 hyp_unpin_shared_mem(virt, virt + size); 1840 WARN_ON(hyp_page_count(virt) != 1); 1841 assert_transition_res(-EBUSY, __pkvm_host_unshare_hyp, pfn); 1842 assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); 1843 assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); 1844 assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); 1845 assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); 1846 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot); 1847 assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm); 1848 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu); 1849 1850 hyp_unpin_shared_mem(virt, virt + size); 1851 assert_page_state(); 1852 WARN_ON(hyp_page_count(virt)); 1853 1854 selftest_state.host = PKVM_PAGE_OWNED; 1855 selftest_state.hyp = PKVM_NOPAGE; 1856 assert_transition_res(0, __pkvm_host_unshare_hyp, pfn); 1857 1858 selftest_state.host = PKVM_PAGE_SHARED_OWNED; 1859 selftest_state.hyp = PKVM_NOPAGE; 1860 assert_transition_res(0, __pkvm_host_share_ffa, pfn, 1); 1861 assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); 1862 assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); 1863 assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); 1864 assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); 1865 assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); 1866 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot); 1867 assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm); 1868 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu); 1869 assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size); 1870 1871 selftest_state.host = PKVM_PAGE_OWNED; 1872 selftest_state.hyp = PKVM_NOPAGE; 1873 assert_transition_res(0, __pkvm_host_unshare_ffa, pfn, 1); 1874 assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1); 1875 1876 selftest_state.host = PKVM_PAGE_SHARED_OWNED; 1877 selftest_state.guest[0] = PKVM_PAGE_SHARED_BORROWED; 1878 assert_transition_res(0, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot); 1879 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot); 1880 assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); 1881 assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); 1882 assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); 1883 assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); 1884 assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); 1885 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu); 1886 assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size); 1887 1888 selftest_state.guest[1] = PKVM_PAGE_SHARED_BORROWED; 1889 assert_transition_res(0, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot); 1890 WARN_ON(hyp_virt_to_page(virt)->host_share_guest_count != 2); 1891 1892 selftest_state.guest[0] = PKVM_NOPAGE; 1893 assert_transition_res(0, __pkvm_host_unshare_guest, gfn, 1, vm); 1894 1895 selftest_state.guest[1] = PKVM_NOPAGE; 1896 selftest_state.host = PKVM_PAGE_OWNED; 1897 assert_transition_res(0, __pkvm_host_unshare_guest, gfn + 1, 1, vm); 1898 1899 selftest_state.host = PKVM_NOPAGE; 1900 selftest_state.guest[0] = PKVM_PAGE_OWNED; 1901 assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn, vcpu); 1902 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu); 1903 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, vcpu); 1904 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot); 1905 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot); 1906 assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); 1907 assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); 1908 assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); 1909 assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); 1910 assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); 1911 1912 selftest_state.host = PKVM_PAGE_SHARED_BORROWED; 1913 selftest_state.guest[0] = PKVM_PAGE_SHARED_OWNED; 1914 assert_transition_res(0, __pkvm_guest_share_host, vcpu, gfn); 1915 assert_transition_res(-EPERM, __pkvm_guest_share_host, vcpu, gfn); 1916 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu); 1917 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, vcpu); 1918 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot); 1919 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot); 1920 assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); 1921 assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); 1922 assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); 1923 assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); 1924 assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); 1925 1926 selftest_state.host = PKVM_NOPAGE; 1927 selftest_state.guest[0] = PKVM_PAGE_OWNED; 1928 assert_transition_res(0, __pkvm_guest_unshare_host, vcpu, gfn); 1929 assert_transition_res(-EPERM, __pkvm_guest_unshare_host, vcpu, gfn); 1930 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu); 1931 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, vcpu); 1932 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot); 1933 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot); 1934 assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); 1935 assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); 1936 assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); 1937 assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); 1938 assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); 1939 1940 selftest_state.host = PKVM_PAGE_OWNED; 1941 selftest_state.guest[0] = PKVM_POISON; 1942 assert_transition_res(0, __pkvm_host_force_reclaim_page_guest, phys); 1943 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu); 1944 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot); 1945 assert_transition_res(-EHWPOISON, __pkvm_guest_share_host, vcpu, gfn); 1946 assert_transition_res(-EHWPOISON, __pkvm_guest_unshare_host, vcpu, gfn); 1947 1948 selftest_state.host = PKVM_NOPAGE; 1949 selftest_state.guest[1] = PKVM_PAGE_OWNED; 1950 assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn + 1, vcpu); 1951 1952 selftest_state.host = PKVM_PAGE_OWNED; 1953 selftest_state.guest[1] = PKVM_NOPAGE; 1954 assert_transition_res(0, __pkvm_host_reclaim_page_guest, gfn + 1, vm); 1955 assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu); 1956 assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot); 1957 1958 selftest_state.host = PKVM_NOPAGE; 1959 selftest_state.hyp = PKVM_PAGE_OWNED; 1960 assert_transition_res(0, __pkvm_host_donate_hyp, pfn, 1); 1961 1962 teardown_selftest_vm(); 1963 selftest_page->refcount = 1; 1964 hyp_put_page(&host_s2_pool, virt); 1965 } 1966 #endif 1967