1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2020 - Google LLC 4 * Author: Quentin Perret <qperret@google.com> 5 */ 6 7 #include <linux/init.h> 8 #include <linux/interval_tree_generic.h> 9 #include <linux/kmemleak.h> 10 #include <linux/kvm_host.h> 11 #include <asm/kvm_mmu.h> 12 #include <linux/memblock.h> 13 #include <linux/mutex.h> 14 15 #include <asm/kvm_pkvm.h> 16 17 #include "hyp_constants.h" 18 19 DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized); 20 21 static struct memblock_region *hyp_memory = kvm_nvhe_sym(hyp_memory); 22 static unsigned int *hyp_memblock_nr_ptr = &kvm_nvhe_sym(hyp_memblock_nr); 23 24 phys_addr_t hyp_mem_base; 25 phys_addr_t hyp_mem_size; 26 27 static int __init register_memblock_regions(void) 28 { 29 struct memblock_region *reg; 30 31 for_each_mem_region(reg) { 32 if (*hyp_memblock_nr_ptr >= HYP_MEMBLOCK_REGIONS) 33 return -ENOMEM; 34 35 hyp_memory[*hyp_memblock_nr_ptr] = *reg; 36 (*hyp_memblock_nr_ptr)++; 37 } 38 39 return 0; 40 } 41 42 void __init kvm_hyp_reserve(void) 43 { 44 u64 hyp_mem_pages = 0; 45 int ret; 46 47 if (!is_hyp_mode_available() || is_kernel_in_hyp_mode()) 48 return; 49 50 if (kvm_get_mode() != KVM_MODE_PROTECTED) 51 return; 52 53 ret = register_memblock_regions(); 54 if (ret) { 55 *hyp_memblock_nr_ptr = 0; 56 kvm_err("Failed to register hyp memblocks: %d\n", ret); 57 return; 58 } 59 60 hyp_mem_pages += hyp_s1_pgtable_pages(); 61 hyp_mem_pages += host_s2_pgtable_pages(); 62 hyp_mem_pages += hyp_vm_table_pages(); 63 hyp_mem_pages += hyp_vmemmap_pages(STRUCT_HYP_PAGE_SIZE); 64 hyp_mem_pages += pkvm_selftest_pages(); 65 hyp_mem_pages += hyp_ffa_proxy_pages(); 66 67 /* 68 * Try to allocate a PMD-aligned region to reduce TLB pressure once 69 * this is unmapped from the host stage-2, and fallback to PAGE_SIZE. 70 */ 71 hyp_mem_size = hyp_mem_pages << PAGE_SHIFT; 72 hyp_mem_base = memblock_phys_alloc(ALIGN(hyp_mem_size, PMD_SIZE), 73 PMD_SIZE); 74 if (!hyp_mem_base) 75 hyp_mem_base = memblock_phys_alloc(hyp_mem_size, PAGE_SIZE); 76 else 77 hyp_mem_size = ALIGN(hyp_mem_size, PMD_SIZE); 78 79 if (!hyp_mem_base) { 80 kvm_err("Failed to reserve hyp memory\n"); 81 return; 82 } 83 84 kvm_info("Reserved %lld MiB at 0x%llx\n", hyp_mem_size >> 20, 85 hyp_mem_base); 86 } 87 88 static void __pkvm_destroy_hyp_vm(struct kvm *kvm) 89 { 90 if (pkvm_hyp_vm_is_created(kvm)) { 91 WARN_ON(kvm_call_hyp_nvhe(__pkvm_finalize_teardown_vm, 92 kvm->arch.pkvm.handle)); 93 } else if (kvm->arch.pkvm.handle) { 94 /* 95 * The VM could have been reserved but hyp initialization has 96 * failed. Make sure to unreserve it. 97 */ 98 kvm_call_hyp_nvhe(__pkvm_unreserve_vm, kvm->arch.pkvm.handle); 99 } 100 101 kvm->arch.pkvm.handle = 0; 102 kvm->arch.pkvm.is_created = false; 103 free_hyp_memcache(&kvm->arch.pkvm.teardown_mc); 104 free_hyp_memcache(&kvm->arch.pkvm.stage2_teardown_mc); 105 } 106 107 static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu) 108 { 109 size_t hyp_vcpu_sz = PAGE_ALIGN(PKVM_HYP_VCPU_SIZE); 110 pkvm_handle_t handle = vcpu->kvm->arch.pkvm.handle; 111 void *hyp_vcpu; 112 int ret; 113 114 vcpu->arch.pkvm_memcache.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2; 115 116 hyp_vcpu = alloc_pages_exact(hyp_vcpu_sz, GFP_KERNEL_ACCOUNT); 117 if (!hyp_vcpu) 118 return -ENOMEM; 119 120 ret = kvm_call_hyp_nvhe(__pkvm_init_vcpu, handle, vcpu, hyp_vcpu); 121 if (!ret) 122 vcpu_set_flag(vcpu, VCPU_PKVM_FINALIZED); 123 else 124 free_pages_exact(hyp_vcpu, hyp_vcpu_sz); 125 126 return ret; 127 } 128 129 /* 130 * Allocates and donates memory for hypervisor VM structs at EL2. 131 * 132 * Allocates space for the VM state, which includes the hyp vm as well as 133 * the hyp vcpus. 134 * 135 * Stores an opaque handler in the kvm struct for future reference. 136 * 137 * Return 0 on success, negative error code on failure. 138 */ 139 static int __pkvm_create_hyp_vm(struct kvm *kvm) 140 { 141 size_t pgd_sz, hyp_vm_sz; 142 void *pgd, *hyp_vm; 143 int ret; 144 145 if (kvm->created_vcpus < 1) 146 return -EINVAL; 147 148 pgd_sz = kvm_pgtable_stage2_pgd_size(kvm->arch.mmu.vtcr); 149 150 /* 151 * The PGD pages will be reclaimed using a hyp_memcache which implies 152 * page granularity. So, use alloc_pages_exact() to get individual 153 * refcounts. 154 */ 155 pgd = alloc_pages_exact(pgd_sz, GFP_KERNEL_ACCOUNT); 156 if (!pgd) 157 return -ENOMEM; 158 159 /* Allocate memory to donate to hyp for vm and vcpu pointers. */ 160 hyp_vm_sz = PAGE_ALIGN(size_add(PKVM_HYP_VM_SIZE, 161 size_mul(sizeof(void *), 162 kvm->created_vcpus))); 163 hyp_vm = alloc_pages_exact(hyp_vm_sz, GFP_KERNEL_ACCOUNT); 164 if (!hyp_vm) { 165 ret = -ENOMEM; 166 goto free_pgd; 167 } 168 169 /* Donate the VM memory to hyp and let hyp initialize it. */ 170 ret = kvm_call_hyp_nvhe(__pkvm_init_vm, kvm, hyp_vm, pgd); 171 if (ret) 172 goto free_vm; 173 174 kvm->arch.pkvm.is_created = true; 175 kvm->arch.pkvm.stage2_teardown_mc.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2; 176 kvm_account_pgtable_pages(pgd, pgd_sz / PAGE_SIZE); 177 178 return 0; 179 free_vm: 180 free_pages_exact(hyp_vm, hyp_vm_sz); 181 free_pgd: 182 free_pages_exact(pgd, pgd_sz); 183 return ret; 184 } 185 186 bool pkvm_hyp_vm_is_created(struct kvm *kvm) 187 { 188 /* 189 * Serialised by config_lock/slots_lock, or by VM lifecycle at 190 * teardown, so a plain read suffices. 191 */ 192 return kvm->arch.pkvm.is_created; 193 } 194 195 int pkvm_create_hyp_vm(struct kvm *kvm) 196 { 197 int ret = 0; 198 199 /* 200 * Synchronise with kvm_arch_prepare_memory_region(), as we 201 * prevent memslot modifications on a pVM that has been run. 202 */ 203 mutex_lock(&kvm->slots_lock); 204 mutex_lock(&kvm->arch.config_lock); 205 if (!pkvm_hyp_vm_is_created(kvm)) 206 ret = __pkvm_create_hyp_vm(kvm); 207 mutex_unlock(&kvm->arch.config_lock); 208 mutex_unlock(&kvm->slots_lock); 209 210 return ret; 211 } 212 213 int pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu) 214 { 215 int ret = 0; 216 217 mutex_lock(&vcpu->kvm->arch.config_lock); 218 if (!vcpu_get_flag(vcpu, VCPU_PKVM_FINALIZED)) 219 ret = __pkvm_create_hyp_vcpu(vcpu); 220 mutex_unlock(&vcpu->kvm->arch.config_lock); 221 222 return ret; 223 } 224 225 void pkvm_destroy_hyp_vm(struct kvm *kvm) 226 { 227 mutex_lock(&kvm->arch.config_lock); 228 __pkvm_destroy_hyp_vm(kvm); 229 mutex_unlock(&kvm->arch.config_lock); 230 } 231 232 int pkvm_init_host_vm(struct kvm *kvm, unsigned long type) 233 { 234 int ret; 235 bool protected = type & KVM_VM_TYPE_ARM_PROTECTED; 236 237 /* Reserve the VM in hyp and obtain a hyp handle for the VM. */ 238 ret = kvm_call_hyp_nvhe(__pkvm_reserve_vm); 239 if (ret < 0) 240 return ret; 241 242 kvm->arch.pkvm.handle = ret; 243 kvm->arch.pkvm.is_protected = protected; 244 if (protected) { 245 pr_warn_once("kvm: protected VMs are experimental and for development only, tainting kernel\n"); 246 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 247 } 248 249 return 0; 250 } 251 252 static void __init _kvm_host_prot_finalize(void *arg) 253 { 254 int *err = arg; 255 256 if (WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize))) 257 WRITE_ONCE(*err, -EINVAL); 258 } 259 260 static int __init pkvm_drop_host_privileges(void) 261 { 262 int ret = 0; 263 264 /* 265 * Flip the static key upfront as that may no longer be possible 266 * once the host stage 2 is installed. 267 */ 268 static_branch_enable(&kvm_protected_mode_initialized); 269 on_each_cpu(_kvm_host_prot_finalize, &ret, 1); 270 return ret; 271 } 272 273 static int __init finalize_pkvm(void) 274 { 275 int ret; 276 277 if (!is_protected_kvm_enabled() || !is_kvm_arm_initialised()) 278 return 0; 279 280 /* 281 * Exclude HYP sections from kmemleak so that they don't get peeked 282 * at, which would end badly once inaccessible. 283 */ 284 kmemleak_free_part(__hyp_bss_start, __hyp_bss_end - __hyp_bss_start); 285 kmemleak_free_part(__hyp_data_start, __hyp_data_end - __hyp_data_start); 286 kmemleak_free_part(__hyp_rodata_start, __hyp_rodata_end - __hyp_rodata_start); 287 kmemleak_free_part_phys(hyp_mem_base, hyp_mem_size); 288 289 ret = pkvm_drop_host_privileges(); 290 if (ret) 291 pr_err("Failed to finalize Hyp protection: %d\n", ret); 292 293 return ret; 294 } 295 device_initcall_sync(finalize_pkvm); 296 297 static u64 __pkvm_mapping_start(struct pkvm_mapping *m) 298 { 299 return m->gfn * PAGE_SIZE; 300 } 301 302 static u64 __pkvm_mapping_end(struct pkvm_mapping *m) 303 { 304 return (m->gfn + m->nr_pages) * PAGE_SIZE - 1; 305 } 306 307 INTERVAL_TREE_DEFINE(struct pkvm_mapping, node, u64, __subtree_last, 308 __pkvm_mapping_start, __pkvm_mapping_end, static, 309 pkvm_mapping); 310 311 /* 312 * __tmp is updated to iter_first(pkvm_mappings) *before* entering the body of the loop to allow 313 * freeing of __map inline. 314 */ 315 #define for_each_mapping_in_range_safe(__pgt, __start, __end, __map) \ 316 for (struct pkvm_mapping *__tmp = pkvm_mapping_iter_first(&(__pgt)->pkvm_mappings, \ 317 __start, __end - 1); \ 318 __tmp && ({ \ 319 __map = __tmp; \ 320 __tmp = pkvm_mapping_iter_next(__map, __start, __end - 1); \ 321 true; \ 322 }); \ 323 ) 324 325 int pkvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu, 326 struct kvm_pgtable_mm_ops *mm_ops) 327 { 328 pgt->pkvm_mappings = RB_ROOT_CACHED; 329 pgt->mmu = mmu; 330 331 return 0; 332 } 333 334 static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64 end) 335 { 336 struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu); 337 pkvm_handle_t handle = kvm->arch.pkvm.handle; 338 struct pkvm_mapping *mapping; 339 int ret; 340 341 for_each_mapping_in_range_safe(pgt, start, end, mapping) { 342 struct page *page; 343 344 ret = kvm_call_hyp_nvhe(__pkvm_reclaim_dying_guest_page, 345 handle, mapping->gfn); 346 if (WARN_ON(ret)) 347 continue; 348 349 page = pfn_to_page(mapping->pfn); 350 WARN_ON_ONCE(mapping->nr_pages != 1); 351 unpin_user_pages_dirty_lock(&page, 1, true); 352 account_locked_vm(kvm->mm, 1, false); 353 pkvm_mapping_remove(mapping, &pgt->pkvm_mappings); 354 kfree(mapping); 355 } 356 357 return 0; 358 } 359 360 static int __pkvm_pgtable_stage2_unshare(struct kvm_pgtable *pgt, u64 start, u64 end) 361 { 362 struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu); 363 pkvm_handle_t handle = kvm->arch.pkvm.handle; 364 struct pkvm_mapping *mapping; 365 int ret; 366 367 for_each_mapping_in_range_safe(pgt, start, end, mapping) { 368 ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_guest, handle, mapping->gfn, 369 (u64)mapping->nr_pages); 370 if (WARN_ON(ret)) 371 return ret; 372 pkvm_mapping_remove(mapping, &pgt->pkvm_mappings); 373 kfree(mapping); 374 } 375 376 return 0; 377 } 378 379 void pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt, 380 u64 addr, u64 size) 381 { 382 struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu); 383 pkvm_handle_t handle = kvm->arch.pkvm.handle; 384 385 if (!handle) 386 return; 387 388 if (pkvm_hyp_vm_is_created(kvm) && !kvm->arch.pkvm.is_dying) { 389 WARN_ON(kvm_call_hyp_nvhe(__pkvm_start_teardown_vm, handle)); 390 kvm->arch.pkvm.is_dying = true; 391 } 392 393 if (kvm_vm_is_protected(kvm)) 394 __pkvm_pgtable_stage2_reclaim(pgt, addr, addr + size); 395 else 396 __pkvm_pgtable_stage2_unshare(pgt, addr, addr + size); 397 } 398 399 void pkvm_pgtable_stage2_destroy_pgd(struct kvm_pgtable *pgt) 400 { 401 /* Expected to be called after all pKVM mappings have been released. */ 402 WARN_ON_ONCE(!RB_EMPTY_ROOT(&pgt->pkvm_mappings.rb_root)); 403 } 404 405 int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, 406 u64 phys, enum kvm_pgtable_prot prot, 407 void *mc, enum kvm_pgtable_walk_flags flags) 408 { 409 struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu); 410 struct pkvm_mapping *mapping = NULL; 411 struct kvm_hyp_memcache *cache = mc; 412 u64 gfn = addr >> PAGE_SHIFT; 413 u64 pfn = phys >> PAGE_SHIFT; 414 u64 end = addr + size; 415 int ret; 416 417 lockdep_assert_held_write(&kvm->mmu_lock); 418 mapping = pkvm_mapping_iter_first(&pgt->pkvm_mappings, addr, end - 1); 419 420 if (kvm_vm_is_protected(kvm)) { 421 /* Protected VMs are mapped using RWX page-granular mappings */ 422 if (WARN_ON_ONCE(size != PAGE_SIZE)) 423 return -EINVAL; 424 425 if (WARN_ON_ONCE(prot != KVM_PGTABLE_PROT_RWX)) 426 return -EINVAL; 427 428 /* 429 * We either raced with another vCPU or the guest PTE 430 * has been poisoned by an erroneous host access. 431 */ 432 if (mapping) { 433 ret = kvm_call_hyp_nvhe(__pkvm_vcpu_in_poison_fault); 434 return ret ? -EFAULT : -EAGAIN; 435 } 436 437 ret = kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn); 438 } else { 439 if (WARN_ON_ONCE(size != PAGE_SIZE && size != PMD_SIZE)) 440 return -EINVAL; 441 442 /* 443 * We either raced with another vCPU or we're changing between 444 * page and block mappings. As per user_mem_abort(), same-size 445 * permission faults are handled in the relax_perms() path. 446 */ 447 if (mapping) { 448 if (size == (mapping->nr_pages * PAGE_SIZE)) 449 return -EAGAIN; 450 451 /* 452 * Remove _any_ pkvm_mapping overlapping with the range, 453 * bigger or smaller. 454 */ 455 ret = __pkvm_pgtable_stage2_unshare(pgt, addr, end); 456 if (ret) 457 return ret; 458 459 mapping = NULL; 460 } 461 462 ret = kvm_call_hyp_nvhe(__pkvm_host_share_guest, pfn, gfn, 463 size / PAGE_SIZE, prot); 464 } 465 466 if (ret) 467 return ret; 468 469 swap(mapping, cache->mapping); 470 mapping->gfn = gfn; 471 mapping->pfn = pfn; 472 mapping->nr_pages = size / PAGE_SIZE; 473 mapping->nc = !!(prot & (KVM_PGTABLE_PROT_DEVICE | KVM_PGTABLE_PROT_NORMAL_NC)); 474 pkvm_mapping_insert(mapping, &pgt->pkvm_mappings); 475 476 return ret; 477 } 478 479 int pkvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size) 480 { 481 struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu); 482 483 if (WARN_ON(kvm_vm_is_protected(kvm))) 484 return -EPERM; 485 486 lockdep_assert_held_write(&kvm->mmu_lock); 487 488 return __pkvm_pgtable_stage2_unshare(pgt, addr, addr + size); 489 } 490 491 int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size) 492 { 493 struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu); 494 pkvm_handle_t handle = kvm->arch.pkvm.handle; 495 struct pkvm_mapping *mapping; 496 int ret = 0; 497 498 if (WARN_ON(kvm_vm_is_protected(kvm))) 499 return -EPERM; 500 501 lockdep_assert_held(&kvm->mmu_lock); 502 for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) { 503 ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn, 504 (u64)mapping->nr_pages); 505 if (WARN_ON(ret)) 506 break; 507 } 508 509 return ret; 510 } 511 512 int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size) 513 { 514 struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu); 515 struct pkvm_mapping *mapping; 516 517 lockdep_assert_held(&kvm->mmu_lock); 518 519 if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) 520 return 0; 521 522 for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) { 523 if (!mapping->nc) 524 __clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn), 525 PAGE_SIZE * mapping->nr_pages); 526 } 527 528 return 0; 529 } 530 531 bool pkvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64 size, bool mkold) 532 { 533 struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu); 534 pkvm_handle_t handle = kvm->arch.pkvm.handle; 535 struct pkvm_mapping *mapping; 536 bool young = false; 537 538 if (WARN_ON(kvm_vm_is_protected(kvm))) 539 return false; 540 541 lockdep_assert_held(&kvm->mmu_lock); 542 for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) 543 young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest, handle, mapping->gfn, 544 (u64)mapping->nr_pages, mkold); 545 546 return young; 547 } 548 549 int pkvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr, enum kvm_pgtable_prot prot, 550 enum kvm_pgtable_walk_flags flags) 551 { 552 if (WARN_ON(kvm_vm_is_protected(kvm_s2_mmu_to_kvm(pgt->mmu)))) 553 return -EPERM; 554 555 return kvm_call_hyp_nvhe(__pkvm_host_relax_perms_guest, addr >> PAGE_SHIFT, prot); 556 } 557 558 void pkvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr, 559 enum kvm_pgtable_walk_flags flags) 560 { 561 if (WARN_ON(kvm_vm_is_protected(kvm_s2_mmu_to_kvm(pgt->mmu)))) 562 return; 563 564 WARN_ON(kvm_call_hyp_nvhe(__pkvm_host_mkyoung_guest, addr >> PAGE_SHIFT)); 565 } 566 567 void pkvm_pgtable_stage2_free_unlinked(struct kvm_pgtable_mm_ops *mm_ops, void *pgtable, s8 level) 568 { 569 WARN_ON_ONCE(1); 570 } 571 572 kvm_pte_t *pkvm_pgtable_stage2_create_unlinked(struct kvm_pgtable *pgt, u64 phys, s8 level, 573 enum kvm_pgtable_prot prot, void *mc, bool force_pte) 574 { 575 WARN_ON_ONCE(1); 576 return NULL; 577 } 578 579 int pkvm_pgtable_stage2_split(struct kvm_pgtable *pgt, u64 addr, u64 size, 580 struct kvm_mmu_memory_cache *mc) 581 { 582 WARN_ON_ONCE(1); 583 return -EINVAL; 584 } 585 586 /* 587 * Forcefully reclaim a page from the guest, zeroing its contents and 588 * poisoning the stage-2 pte so that pages can no longer be mapped at 589 * the same IPA. The page remains pinned until the guest is destroyed. 590 */ 591 bool pkvm_force_reclaim_guest_page(phys_addr_t phys) 592 { 593 int ret = kvm_call_hyp_nvhe(__pkvm_force_reclaim_guest_page, phys); 594 595 return !ret || ret == -EAGAIN; 596 } 597