In order to add a PCI VF into a secure VM, the TSM module needs to perform a "TDI bind" operation. The secure module ("PSP" for AMD) reuqires a VM id to associate with a VM and KVM has it. Since KVM cannot directly bind a TDI (as it does not have all necesessary data such as host/guest PCI BDFn). QEMU and IOMMUFD do know the BDFns but they do not have a VM id recognisable by the PSP. Add get_vmid() hook to KVM. Implement it for AMD SEV to return a sum of GCTX (a private page describing secure VM context) and ASID (required on unbind for IOMMU unfencing, when needed). Signed-off-by: Alexey Kardashevskiy <aik@xxxxxxx> --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ include/linux/kvm_host.h | 2 ++ arch/x86/kvm/svm/svm.c | 12 ++++++++++++ virt/kvm/kvm_main.c | 6 ++++++ 5 files changed, 23 insertions(+) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index c35550581da0..63102a224cd7 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -144,6 +144,7 @@ KVM_X86_OP_OPTIONAL(alloc_apic_backing_page) KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) KVM_X86_OP_OPTIONAL_RET0(private_max_mapping_level) KVM_X86_OP_OPTIONAL(gmem_invalidate) +KVM_X86_OP_OPTIONAL(tsm_get_vmid) #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 b15cde0a9b5c..9330e8d4d29d 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1875,6 +1875,8 @@ struct kvm_x86_ops { int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order); void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end); int (*private_max_mapping_level)(struct kvm *kvm, kvm_pfn_t pfn); + + u64 (*tsm_get_vmid)(struct kvm *kvm); }; struct kvm_x86_nested_ops { diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f34f4cfaa513..6cd351edb956 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2571,4 +2571,6 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, struct kvm_pre_fault_memory *range); #endif +u64 kvm_arch_tsm_get_vmid(struct kvm *kvm); + #endif diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 7640a84e554a..0276d60c61d6 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4998,6 +4998,16 @@ static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) return page_address(page); } +static u64 svm_tsm_get_vmid(struct kvm *kvm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + + if (!sev->es_active) + return 0; + + return ((u64) sev->snp_context) | sev->asid; +} + static struct kvm_x86_ops svm_x86_ops __initdata = { .name = KBUILD_MODNAME, @@ -5137,6 +5147,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .gmem_prepare = sev_gmem_prepare, .gmem_invalidate = sev_gmem_invalidate, .private_max_mapping_level = sev_private_max_mapping_level, + + .tsm_get_vmid = svm_tsm_get_vmid, }; /* diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ba0327e2d0d3..90c3ff7c5c02 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -6464,3 +6464,9 @@ void kvm_exit(void) kvm_irqfd_exit(); } EXPORT_SYMBOL_GPL(kvm_exit); + +u64 kvm_arch_tsm_get_vmid(struct kvm *kvm) +{ + return static_call(kvm_x86_tsm_get_vmid)(kvm); +} +EXPORT_SYMBOL_GPL(kvm_arch_tsm_get_vmid); -- 2.47.1