1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021 Google LLC
4 * Author: Fuad Tabba <tabba@google.com>
7 #include <linux/kvm_host.h>
9 #include <nvhe/fixed_config.h>
10 #include <nvhe/mem_protect.h>
11 #include <nvhe/memory.h>
12 #include <nvhe/pkvm.h>
13 #include <nvhe/trap_handler.h>
15 /* Used by icache_is_vpipt(). */
16 unsigned long __icache_flags;
18 /* Used by kvm_get_vttbr(). */
19 unsigned int kvm_arm_vmid_bits;
22 * Set trap register values based on features in ID_AA64PFR0.
24 static void pvm_init_traps_aa64pfr0(struct kvm_vcpu *vcpu)
26 const u64 feature_ids = pvm_read_id_reg(vcpu, SYS_ID_AA64PFR0_EL1);
32 /* Protected KVM does not support AArch32 guests. */
33 BUILD_BUG_ON(FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_EL0),
34 PVM_ID_AA64PFR0_RESTRICT_UNSIGNED) != ID_AA64PFR0_EL1_ELx_64BIT_ONLY);
35 BUILD_BUG_ON(FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_EL1),
36 PVM_ID_AA64PFR0_RESTRICT_UNSIGNED) != ID_AA64PFR0_EL1_ELx_64BIT_ONLY);
39 * Linux guests assume support for floating-point and Advanced SIMD. Do
40 * not change the trapping behavior for these from the KVM default.
42 BUILD_BUG_ON(!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_FP),
43 PVM_ID_AA64PFR0_ALLOW));
44 BUILD_BUG_ON(!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AdvSIMD),
45 PVM_ID_AA64PFR0_ALLOW));
50 /* Trap RAS unless all current versions are supported */
51 if (FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_RAS), feature_ids) <
52 ID_AA64PFR0_EL1_RAS_V1P1) {
53 hcr_set |= HCR_TERR | HCR_TEA;
54 hcr_clear |= HCR_FIEN;
58 if (!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU), feature_ids)) {
59 hcr_clear |= HCR_AMVOFFEN;
60 cptr_set |= CPTR_EL2_TAM;
64 if (!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_SVE), feature_ids)) {
66 cptr_clear |= CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN;
68 cptr_set |= CPTR_EL2_TZ;
71 vcpu->arch.hcr_el2 |= hcr_set;
72 vcpu->arch.hcr_el2 &= ~hcr_clear;
73 vcpu->arch.cptr_el2 |= cptr_set;
74 vcpu->arch.cptr_el2 &= ~cptr_clear;
78 * Set trap register values based on features in ID_AA64PFR1.
80 static void pvm_init_traps_aa64pfr1(struct kvm_vcpu *vcpu)
82 const u64 feature_ids = pvm_read_id_reg(vcpu, SYS_ID_AA64PFR1_EL1);
86 /* Memory Tagging: Trap and Treat as Untagged if not supported. */
87 if (!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR1_EL1_MTE), feature_ids)) {
89 hcr_clear |= HCR_DCT | HCR_ATA;
92 vcpu->arch.hcr_el2 |= hcr_set;
93 vcpu->arch.hcr_el2 &= ~hcr_clear;
97 * Set trap register values based on features in ID_AA64DFR0.
99 static void pvm_init_traps_aa64dfr0(struct kvm_vcpu *vcpu)
101 const u64 feature_ids = pvm_read_id_reg(vcpu, SYS_ID_AA64DFR0_EL1);
106 /* Trap/constrain PMU */
107 if (!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), feature_ids)) {
108 mdcr_set |= MDCR_EL2_TPM | MDCR_EL2_TPMCR;
109 mdcr_clear |= MDCR_EL2_HPME | MDCR_EL2_MTPME |
114 if (!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DebugVer), feature_ids))
115 mdcr_set |= MDCR_EL2_TDRA | MDCR_EL2_TDA | MDCR_EL2_TDE;
117 /* Trap OS Double Lock */
118 if (!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DoubleLock), feature_ids))
119 mdcr_set |= MDCR_EL2_TDOSA;
122 if (!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMSVer), feature_ids)) {
123 mdcr_set |= MDCR_EL2_TPMS;
124 mdcr_clear |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT;
127 /* Trap Trace Filter */
128 if (!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_TraceFilt), feature_ids))
129 mdcr_set |= MDCR_EL2_TTRF;
132 if (!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_TraceVer), feature_ids)) {
134 cptr_set |= CPACR_EL1_TTA;
136 cptr_set |= CPTR_EL2_TTA;
139 vcpu->arch.mdcr_el2 |= mdcr_set;
140 vcpu->arch.mdcr_el2 &= ~mdcr_clear;
141 vcpu->arch.cptr_el2 |= cptr_set;
145 * Set trap register values based on features in ID_AA64MMFR0.
147 static void pvm_init_traps_aa64mmfr0(struct kvm_vcpu *vcpu)
149 const u64 feature_ids = pvm_read_id_reg(vcpu, SYS_ID_AA64MMFR0_EL1);
152 /* Trap Debug Communications Channel registers */
153 if (!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64MMFR0_EL1_FGT), feature_ids))
154 mdcr_set |= MDCR_EL2_TDCC;
156 vcpu->arch.mdcr_el2 |= mdcr_set;
160 * Set trap register values based on features in ID_AA64MMFR1.
162 static void pvm_init_traps_aa64mmfr1(struct kvm_vcpu *vcpu)
164 const u64 feature_ids = pvm_read_id_reg(vcpu, SYS_ID_AA64MMFR1_EL1);
168 if (!FIELD_GET(ARM64_FEATURE_MASK(ID_AA64MMFR1_EL1_LO), feature_ids))
171 vcpu->arch.hcr_el2 |= hcr_set;
175 * Set baseline trap register values.
177 static void pvm_init_trap_regs(struct kvm_vcpu *vcpu)
179 const u64 hcr_trap_feat_regs = HCR_TID3;
180 const u64 hcr_trap_impdef = HCR_TACR | HCR_TIDCP | HCR_TID1;
184 * - Feature id registers: to control features exposed to guests
185 * - Implementation-defined features
187 vcpu->arch.hcr_el2 |= hcr_trap_feat_regs | hcr_trap_impdef;
189 /* Clear res0 and set res1 bits to trap potential new features. */
190 vcpu->arch.hcr_el2 &= ~(HCR_RES0);
191 vcpu->arch.mdcr_el2 &= ~(MDCR_EL2_RES0);
193 vcpu->arch.cptr_el2 |= CPTR_NVHE_EL2_RES1;
194 vcpu->arch.cptr_el2 &= ~(CPTR_NVHE_EL2_RES0);
199 * Initialize trap register values for protected VMs.
201 void __pkvm_vcpu_init_traps(struct kvm_vcpu *vcpu)
203 pvm_init_trap_regs(vcpu);
204 pvm_init_traps_aa64pfr0(vcpu);
205 pvm_init_traps_aa64pfr1(vcpu);
206 pvm_init_traps_aa64dfr0(vcpu);
207 pvm_init_traps_aa64mmfr0(vcpu);
208 pvm_init_traps_aa64mmfr1(vcpu);
212 * Start the VM table handle at the offset defined instead of at 0.
213 * Mainly for sanity checking and debugging.
215 #define HANDLE_OFFSET 0x1000
217 static unsigned int vm_handle_to_idx(pkvm_handle_t handle)
219 return handle - HANDLE_OFFSET;
222 static pkvm_handle_t idx_to_vm_handle(unsigned int idx)
224 return idx + HANDLE_OFFSET;
228 * Spinlock for protecting state related to the VM table. Protects writes
229 * to 'vm_table' and 'nr_table_entries' as well as reads and writes to
230 * 'last_hyp_vcpu_lookup'.
232 static DEFINE_HYP_SPINLOCK(vm_table_lock);
235 * The table of VM entries for protected VMs in hyp.
236 * Allocated at hyp initialization and setup.
238 static struct pkvm_hyp_vm **vm_table;
240 void pkvm_hyp_vm_table_init(void *tbl)
247 * Return the hyp vm structure corresponding to the handle.
249 static struct pkvm_hyp_vm *get_vm_by_handle(pkvm_handle_t handle)
251 unsigned int idx = vm_handle_to_idx(handle);
253 if (unlikely(idx >= KVM_MAX_PVMS))
256 return vm_table[idx];
259 struct pkvm_hyp_vcpu *pkvm_load_hyp_vcpu(pkvm_handle_t handle,
260 unsigned int vcpu_idx)
262 struct pkvm_hyp_vcpu *hyp_vcpu = NULL;
263 struct pkvm_hyp_vm *hyp_vm;
265 hyp_spin_lock(&vm_table_lock);
266 hyp_vm = get_vm_by_handle(handle);
267 if (!hyp_vm || hyp_vm->nr_vcpus <= vcpu_idx)
270 hyp_vcpu = hyp_vm->vcpus[vcpu_idx];
271 hyp_page_ref_inc(hyp_virt_to_page(hyp_vm));
273 hyp_spin_unlock(&vm_table_lock);
277 void pkvm_put_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
279 struct pkvm_hyp_vm *hyp_vm = pkvm_hyp_vcpu_to_hyp_vm(hyp_vcpu);
281 hyp_spin_lock(&vm_table_lock);
282 hyp_page_ref_dec(hyp_virt_to_page(hyp_vm));
283 hyp_spin_unlock(&vm_table_lock);
286 static void unpin_host_vcpu(struct kvm_vcpu *host_vcpu)
289 hyp_unpin_shared_mem(host_vcpu, host_vcpu + 1);
292 static void unpin_host_vcpus(struct pkvm_hyp_vcpu *hyp_vcpus[],
293 unsigned int nr_vcpus)
297 for (i = 0; i < nr_vcpus; i++)
298 unpin_host_vcpu(hyp_vcpus[i]->host_vcpu);
301 static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm,
302 unsigned int nr_vcpus)
304 hyp_vm->host_kvm = host_kvm;
305 hyp_vm->kvm.created_vcpus = nr_vcpus;
306 hyp_vm->kvm.arch.mmu.vtcr = host_mmu.arch.mmu.vtcr;
309 static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu,
310 struct pkvm_hyp_vm *hyp_vm,
311 struct kvm_vcpu *host_vcpu,
312 unsigned int vcpu_idx)
316 if (hyp_pin_shared_mem(host_vcpu, host_vcpu + 1))
319 if (host_vcpu->vcpu_idx != vcpu_idx) {
324 hyp_vcpu->host_vcpu = host_vcpu;
326 hyp_vcpu->vcpu.kvm = &hyp_vm->kvm;
327 hyp_vcpu->vcpu.vcpu_id = READ_ONCE(host_vcpu->vcpu_id);
328 hyp_vcpu->vcpu.vcpu_idx = vcpu_idx;
330 hyp_vcpu->vcpu.arch.hw_mmu = &hyp_vm->kvm.arch.mmu;
331 hyp_vcpu->vcpu.arch.cflags = READ_ONCE(host_vcpu->arch.cflags);
334 unpin_host_vcpu(host_vcpu);
338 static int find_free_vm_table_entry(struct kvm *host_kvm)
342 for (i = 0; i < KVM_MAX_PVMS; ++i) {
351 * Allocate a VM table entry and insert a pointer to the new vm.
353 * Return a unique handle to the protected VM on success,
354 * negative error code on failure.
356 static pkvm_handle_t insert_vm_table_entry(struct kvm *host_kvm,
357 struct pkvm_hyp_vm *hyp_vm)
359 struct kvm_s2_mmu *mmu = &hyp_vm->kvm.arch.mmu;
362 hyp_assert_lock_held(&vm_table_lock);
365 * Initializing protected state might have failed, yet a malicious
366 * host could trigger this function. Thus, ensure that 'vm_table'
369 if (unlikely(!vm_table))
372 idx = find_free_vm_table_entry(host_kvm);
376 hyp_vm->kvm.arch.pkvm.handle = idx_to_vm_handle(idx);
378 /* VMID 0 is reserved for the host */
379 atomic64_set(&mmu->vmid.id, idx + 1);
381 mmu->arch = &hyp_vm->kvm.arch;
382 mmu->pgt = &hyp_vm->pgt;
384 vm_table[idx] = hyp_vm;
385 return hyp_vm->kvm.arch.pkvm.handle;
389 * Deallocate and remove the VM table entry corresponding to the handle.
391 static void remove_vm_table_entry(pkvm_handle_t handle)
393 hyp_assert_lock_held(&vm_table_lock);
394 vm_table[vm_handle_to_idx(handle)] = NULL;
397 static size_t pkvm_get_hyp_vm_size(unsigned int nr_vcpus)
399 return size_add(sizeof(struct pkvm_hyp_vm),
400 size_mul(sizeof(struct pkvm_hyp_vcpu *), nr_vcpus));
403 static void *map_donated_memory_noclear(unsigned long host_va, size_t size)
405 void *va = (void *)kern_hyp_va(host_va);
407 if (!PAGE_ALIGNED(va))
410 if (__pkvm_host_donate_hyp(hyp_virt_to_pfn(va),
411 PAGE_ALIGN(size) >> PAGE_SHIFT))
417 static void *map_donated_memory(unsigned long host_va, size_t size)
419 void *va = map_donated_memory_noclear(host_va, size);
427 static void __unmap_donated_memory(void *va, size_t size)
429 WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(va),
430 PAGE_ALIGN(size) >> PAGE_SHIFT));
433 static void unmap_donated_memory(void *va, size_t size)
439 __unmap_donated_memory(va, size);
442 static void unmap_donated_memory_noclear(void *va, size_t size)
447 __unmap_donated_memory(va, size);
451 * Initialize the hypervisor copy of the protected VM state using the
452 * memory donated by the host.
454 * Unmaps the donated memory from the host at stage 2.
456 * host_kvm: A pointer to the host's struct kvm.
457 * vm_hva: The host va of the area being donated for the VM state.
458 * Must be page aligned.
459 * pgd_hva: The host va of the area being donated for the stage-2 PGD for
460 * the VM. Must be page aligned. Its size is implied by the VM's
463 * Return a unique handle to the protected VM on success,
464 * negative error code on failure.
466 int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
467 unsigned long pgd_hva)
469 struct pkvm_hyp_vm *hyp_vm = NULL;
470 size_t vm_size, pgd_size;
471 unsigned int nr_vcpus;
475 ret = hyp_pin_shared_mem(host_kvm, host_kvm + 1);
479 nr_vcpus = READ_ONCE(host_kvm->created_vcpus);
485 vm_size = pkvm_get_hyp_vm_size(nr_vcpus);
486 pgd_size = kvm_pgtable_stage2_pgd_size(host_mmu.arch.mmu.vtcr);
490 hyp_vm = map_donated_memory(vm_hva, vm_size);
492 goto err_remove_mappings;
494 pgd = map_donated_memory_noclear(pgd_hva, pgd_size);
496 goto err_remove_mappings;
498 init_pkvm_hyp_vm(host_kvm, hyp_vm, nr_vcpus);
500 hyp_spin_lock(&vm_table_lock);
501 ret = insert_vm_table_entry(host_kvm, hyp_vm);
505 ret = kvm_guest_prepare_stage2(hyp_vm, pgd);
507 goto err_remove_vm_table_entry;
508 hyp_spin_unlock(&vm_table_lock);
510 return hyp_vm->kvm.arch.pkvm.handle;
512 err_remove_vm_table_entry:
513 remove_vm_table_entry(hyp_vm->kvm.arch.pkvm.handle);
515 hyp_spin_unlock(&vm_table_lock);
517 unmap_donated_memory(hyp_vm, vm_size);
518 unmap_donated_memory(pgd, pgd_size);
520 hyp_unpin_shared_mem(host_kvm, host_kvm + 1);
525 * Initialize the hypervisor copy of the protected vCPU state using the
526 * memory donated by the host.
528 * handle: The handle for the protected vm.
529 * host_vcpu: A pointer to the corresponding host vcpu.
530 * vcpu_hva: The host va of the area being donated for the vcpu state.
531 * Must be page aligned. The size of the area must be equal to
532 * the page-aligned size of 'struct pkvm_hyp_vcpu'.
533 * Return 0 on success, negative error code on failure.
535 int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu,
536 unsigned long vcpu_hva)
538 struct pkvm_hyp_vcpu *hyp_vcpu;
539 struct pkvm_hyp_vm *hyp_vm;
543 hyp_vcpu = map_donated_memory(vcpu_hva, sizeof(*hyp_vcpu));
547 hyp_spin_lock(&vm_table_lock);
549 hyp_vm = get_vm_by_handle(handle);
555 idx = hyp_vm->nr_vcpus;
556 if (idx >= hyp_vm->kvm.created_vcpus) {
561 ret = init_pkvm_hyp_vcpu(hyp_vcpu, hyp_vm, host_vcpu, idx);
565 hyp_vm->vcpus[idx] = hyp_vcpu;
568 hyp_spin_unlock(&vm_table_lock);
571 unmap_donated_memory(hyp_vcpu, sizeof(*hyp_vcpu));
577 teardown_donated_memory(struct kvm_hyp_memcache *mc, void *addr, size_t size)
579 size = PAGE_ALIGN(size);
580 memset(addr, 0, size);
582 for (void *start = addr; start < addr + size; start += PAGE_SIZE)
583 push_hyp_memcache(mc, start, hyp_virt_to_phys);
585 unmap_donated_memory_noclear(addr, size);
588 int __pkvm_teardown_vm(pkvm_handle_t handle)
590 struct kvm_hyp_memcache *mc;
591 struct pkvm_hyp_vm *hyp_vm;
592 struct kvm *host_kvm;
597 hyp_spin_lock(&vm_table_lock);
598 hyp_vm = get_vm_by_handle(handle);
604 if (WARN_ON(hyp_page_count(hyp_vm))) {
609 host_kvm = hyp_vm->host_kvm;
611 /* Ensure the VMID is clean before it can be reallocated */
612 __kvm_tlb_flush_vmid(&hyp_vm->kvm.arch.mmu);
613 remove_vm_table_entry(handle);
614 hyp_spin_unlock(&vm_table_lock);
616 /* Reclaim guest pages (including page-table pages) */
617 mc = &host_kvm->arch.pkvm.teardown_mc;
618 reclaim_guest_pages(hyp_vm, mc);
619 unpin_host_vcpus(hyp_vm->vcpus, hyp_vm->nr_vcpus);
621 /* Push the metadata pages to the teardown memcache */
622 for (idx = 0; idx < hyp_vm->nr_vcpus; ++idx) {
623 struct pkvm_hyp_vcpu *hyp_vcpu = hyp_vm->vcpus[idx];
625 teardown_donated_memory(mc, hyp_vcpu, sizeof(*hyp_vcpu));
628 vm_size = pkvm_get_hyp_vm_size(hyp_vm->kvm.created_vcpus);
629 teardown_donated_memory(mc, hyp_vm, vm_size);
630 hyp_unpin_shared_mem(host_kvm, host_kvm + 1);
634 hyp_spin_unlock(&vm_table_lock);