From: Isaku Yamahata <isaku.yamahata@xxxxxxxxx> Add a hook in kvm_arch_vcpu_map_memory() for KVM_MAP_MEMORY before calling kvm_mmu_map_page() to adjust the error code for a page fault. The hook can hold vendor-specific logic to make those adjustments and enforce the restrictions. SEV and TDX KVM will use the hook. In the case of SEV and TDX, they need to adjust the KVM page fault error code or refuse the operation due to their restriction. TDX requires that the guest memory population must be before finalizing the VM. Signed-off-by: Isaku Yamahata <isaku.yamahata@xxxxxxxxx> --- v2: - Make pre_mmu_map_page() to take error_code. - Drop post_mmu_map_page(). - Drop struct kvm_memory_map.source check. --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/x86.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 5187fcf4b610..a5d4f4d5265d 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -139,6 +139,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL(alloc_apic_backing_page) +KVM_X86_OP_OPTIONAL(pre_mmu_map_page); #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 3ce244ad44e5..2bf7f97f889b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1812,6 +1812,9 @@ struct kvm_x86_ops { gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu, gva_t gva, unsigned int flags); void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); + int (*pre_mmu_map_page)(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping, + u64 *error_code); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 8ba9c1720ac9..b76d854701d5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5868,6 +5868,26 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, } } +static int kvm_pre_mmu_map_page(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping, + u64 *error_code) +{ + int r = 0; + + if (vcpu->kvm->arch.vm_type == KVM_X86_DEFAULT_VM) { + /* nothing */ + } else if (vcpu->kvm->arch.vm_type == KVM_X86_SW_PROTECTED_VM) { + if (kvm_mem_is_private(vcpu->kvm, gpa_to_gfn(mapping->base_address))) + *error_code |= PFERR_PRIVATE_ACCESS; + } else if (kvm_x86_ops.pre_mmu_map_page) + r = static_call(kvm_x86_pre_mmu_map_page)(vcpu, mapping, + error_code); + else + r = -EOPNOTSUPP; + + return r; +} + int kvm_arch_vcpu_map_memory(struct kvm_vcpu *vcpu, struct kvm_memory_mapping *mapping) { @@ -5900,6 +5920,14 @@ int kvm_arch_vcpu_map_memory(struct kvm_vcpu *vcpu, /* reload is optimized for repeated call. */ kvm_mmu_reload(vcpu); + /* + * Adjust error_code for VM-type. max_level is adjusted by + * gmem_max_level() callback. + */ + r = kvm_pre_mmu_map_page(vcpu, mapping, &error_code); + if (r) + goto out; + r = kvm_tdp_map_page(vcpu, mapping->base_address, error_code, &level); if (r) goto out; -- 2.43.2