From nobody Sun Apr 28 19:38:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=quarantine dis=quarantine) header.from=amazon.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1673001415420430.191637997861; Fri, 6 Jan 2023 02:36:55 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.472422.732637 (Exim 4.92) (envelope-from ) id 1pDk5Q-00050f-6Q; Fri, 06 Jan 2023 10:36:32 +0000 Received: by outflank-mailman (output) from mailman id 472422.732637; Fri, 06 Jan 2023 10:36:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pDk5Q-00050W-3L; Fri, 06 Jan 2023 10:36:32 +0000 Received: by outflank-mailman (input) for mailman id 472422; Fri, 06 Jan 2023 10:36:31 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pDk5P-000500-5n for xen-devel@lists.xenproject.org; Fri, 06 Jan 2023 10:36:31 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pDk5K-0003o2-SY; Fri, 06 Jan 2023 10:36:26 +0000 Received: from 54-240-197-227.amazon.com ([54.240.197.227] helo=debian.cbg12.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pDk5K-0001wX-Ir; Fri, 06 Jan 2023 10:36:26 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" From: Paul Durrant To: x86@kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , David Woodhouse Subject: [PATCH v7 1/2] KVM: x86/cpuid: generalize kvm_update_kvm_cpuid_base() and also capture limit Date: Fri, 6 Jan 2023 10:35:59 +0000 Message-Id: <20230106103600.528-2-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230106103600.528-1-pdurrant@amazon.com> References: <20230106103600.528-1-pdurrant@amazon.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1673001418158100001 Content-Type: text/plain; charset="utf-8" A subsequent patch will need to acquire the CPUID leaf range for emulated Xen so explicitly pass the signature of the hypervisor we're interested in to the new function. Also introduce a new kvm_hypervisor_cpuid structure so we can neatly store both the base and limit leaf indices. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: Sean Christopherson Cc: Paolo Bonzini Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: David Woodhouse v7: - Morph kvm_update_hypervisor_cpuid() into kvm_get_hypervisor_cpuid() - Place the definition of struct kvm_hypervisor_cpuid to avoid churn in patch #2. v6: - New in this version --- arch/x86/include/asm/kvm_host.h | 7 ++++++- arch/x86/kvm/cpuid.c | 24 +++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index c70690b2c82d..85cbe4571ac9 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -678,6 +678,11 @@ struct kvm_vcpu_hv { } nested; }; =20 +struct kvm_hypervisor_cpuid { + u32 base; + u32 limit; +}; + /* Xen HVM per vcpu emulation context */ struct kvm_vcpu_xen { u64 hypercall_rip; @@ -826,7 +831,7 @@ struct kvm_vcpu_arch { =20 int cpuid_nent; struct kvm_cpuid_entry2 *cpuid_entries; - u32 kvm_cpuid_base; + struct kvm_hypervisor_cpuid kvm_cpuid; =20 u64 reserved_gpa_bits; int maxphyaddr; diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 69768e4d53a6..db5a4d38fcd0 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -180,15 +180,15 @@ static int kvm_cpuid_check_equal(struct kvm_vcpu *vcp= u, struct kvm_cpuid_entry2 return 0; } =20 -static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu) +static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcp= u *vcpu, + const char *sig) { - u32 function; + struct kvm_hypervisor_cpuid cpuid =3D {}; struct kvm_cpuid_entry2 *entry; + u32 base; =20 - vcpu->arch.kvm_cpuid_base =3D 0; - - for_each_possible_hypervisor_cpuid_base(function) { - entry =3D kvm_find_cpuid_entry(vcpu, function); + for_each_possible_hypervisor_cpuid_base(base) { + entry =3D kvm_find_cpuid_entry(vcpu, base); =20 if (entry) { u32 signature[3]; @@ -197,19 +197,21 @@ static void kvm_update_kvm_cpuid_base(struct kvm_vcpu= *vcpu) signature[1] =3D entry->ecx; signature[2] =3D entry->edx; =20 - BUILD_BUG_ON(sizeof(signature) > sizeof(KVM_SIGNATURE)); - if (!memcmp(signature, KVM_SIGNATURE, sizeof(signature))) { - vcpu->arch.kvm_cpuid_base =3D function; + if (!memcmp(signature, sig, sizeof(signature))) { + cpuid.base =3D base; + cpuid.limit =3D entry->eax; break; } } } + + return cpuid; } =20 static struct kvm_cpuid_entry2 *__kvm_find_kvm_cpuid_features(struct kvm_v= cpu *vcpu, struct kvm_cpuid_entry2 *entries, int nent) { - u32 base =3D vcpu->arch.kvm_cpuid_base; + u32 base =3D vcpu->arch.kvm_cpuid.base; =20 if (!base) return NULL; @@ -439,7 +441,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct = kvm_cpuid_entry2 *e2, vcpu->arch.cpuid_entries =3D e2; vcpu->arch.cpuid_nent =3D nent; =20 - kvm_update_kvm_cpuid_base(vcpu); + vcpu->arch.kvm_cpuid =3D kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE); kvm_vcpu_after_set_cpuid(vcpu); =20 return 0; --=20 2.20.1 From nobody Sun Apr 28 19:38:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=quarantine dis=quarantine) header.from=amazon.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 167300143439753.32008618788075; Fri, 6 Jan 2023 02:37:14 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.472425.732649 (Exim 4.92) (envelope-from ) id 1pDk5e-0005Z8-GT; Fri, 06 Jan 2023 10:36:46 +0000 Received: by outflank-mailman (output) from mailman id 472425.732649; Fri, 06 Jan 2023 10:36:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pDk5e-0005Yz-C8; Fri, 06 Jan 2023 10:36:46 +0000 Received: by outflank-mailman (input) for mailman id 472425; Fri, 06 Jan 2023 10:36:45 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pDk5d-0005Uh-4T for xen-devel@lists.xenproject.org; Fri, 06 Jan 2023 10:36:45 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pDk5O-0003oH-OG; Fri, 06 Jan 2023 10:36:30 +0000 Received: from 54-240-197-227.amazon.com ([54.240.197.227] helo=debian.cbg12.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pDk5O-0001wX-EX; Fri, 06 Jan 2023 10:36:30 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" From: Paul Durrant To: x86@kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , Sean Christopherson , Paolo Bonzini , Juergen Gross , Boris Ostrovsky , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , David Woodhouse Subject: [PATCH v7 2/2] KVM: x86/xen: update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present Date: Fri, 6 Jan 2023 10:36:00 +0000 Message-Id: <20230106103600.528-3-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230106103600.528-1-pdurrant@amazon.com> References: <20230106103600.528-1-pdurrant@amazon.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1673001435115100001 Content-Type: text/plain; charset="utf-8" The scaling information in subleaf 1 should match the values set by KVM in the 'vcpu_info' sub-structure 'time_info' (a.k.a. pvclock_vcpu_time_info) which is shared with the guest, but is not directly available to the VMM. The offset values are not set since a TSC offset is already applied. The TSC frequency should also be set in sub-leaf 2. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: Sean Christopherson Cc: Paolo Bonzini Cc: Juergen Gross Cc: Boris Ostrovsky Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: David Woodhouse v7: - Add a definition of XEN_SIGNATURE into asm/xen/hypervisor.h and use that v6: - Stash Xen cpuid base and limit values when cpuid is set - Re-name kvm_xen_setup_tsc_info() to kvm_xen_update_tsc_info() v5: - Drop the caching of the CPUID entry pointers and only update the sub-leaves if the CPU frequency has actually changed v4: - Update commit comment v3: - Add leaf limit check in kvm_xen_set_cpuid() v2: - Make sure sub-leaf pointers are NULLed if the time leaf is removed --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/xen/hypervisor.h | 4 +++- arch/x86/kvm/cpuid.c | 2 ++ arch/x86/kvm/x86.c | 1 + arch/x86/kvm/xen.c | 26 ++++++++++++++++++++++++++ arch/x86/kvm/xen.h | 7 +++++++ 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 85cbe4571ac9..f3e9f6e4b3ea 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -703,6 +703,7 @@ struct kvm_vcpu_xen { struct hrtimer timer; int poll_evtchn; struct timer_list poll_timer; + struct kvm_hypervisor_cpuid cpuid; }; =20 struct kvm_queued_exception { diff --git a/arch/x86/include/asm/xen/hypervisor.h b/arch/x86/include/asm/x= en/hypervisor.h index 16f548a661cf..5fc35f889cd1 100644 --- a/arch/x86/include/asm/xen/hypervisor.h +++ b/arch/x86/include/asm/xen/hypervisor.h @@ -38,9 +38,11 @@ extern struct start_info *xen_start_info; =20 #include =20 +#define XEN_SIGNATURE "XenVMMXenVMM" + static inline uint32_t xen_cpuid_base(void) { - return hypervisor_cpuid_base("XenVMMXenVMM", 2); + return hypervisor_cpuid_base(XEN_SIGNATURE, 2); } =20 struct pci_dev; diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index db5a4d38fcd0..560f880cc9fd 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -25,6 +25,7 @@ #include "mmu.h" #include "trace.h" #include "pmu.h" +#include "xen.h" =20 /* * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need t= o be @@ -442,6 +443,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct = kvm_cpuid_entry2 *e2, vcpu->arch.cpuid_nent =3D nent; =20 vcpu->arch.kvm_cpuid =3D kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE); + vcpu->arch.xen.cpuid =3D kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE); kvm_vcpu_after_set_cpuid(vcpu); =20 return 0; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f6ef44dc8a0e..e849a5445cac 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3158,6 +3158,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) &vcpu->hv_clock.tsc_shift, &vcpu->hv_clock.tsc_to_system_mul); vcpu->hw_tsc_khz =3D tgt_tsc_khz; + kvm_xen_update_tsc_info(v); } =20 vcpu->hv_clock.tsc_timestamp =3D tsc_timestamp; diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 2e29bdc2949c..fbfc7c17defd 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -22,6 +22,9 @@ #include #include =20 +#include + +#include "cpuid.h" #include "trace.h" =20 static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm); @@ -2067,6 +2070,29 @@ void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu) del_timer_sync(&vcpu->arch.xen.poll_timer); } =20 +void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu) +{ + struct kvm_cpuid_entry2 *entry; + u32 function; + + if (!vcpu->arch.xen.cpuid.base) + return; + + function =3D vcpu->arch.xen.cpuid.base | XEN_CPUID_LEAF(3); + if (function > vcpu->arch.xen.cpuid.limit) + return; + + entry =3D kvm_find_cpuid_entry_index(vcpu, function, 1); + if (entry) { + entry->ecx =3D vcpu->arch.hv_clock.tsc_to_system_mul; + entry->edx =3D vcpu->arch.hv_clock.tsc_shift; + } + + entry =3D kvm_find_cpuid_entry_index(vcpu, function, 2); + if (entry) + entry->eax =3D vcpu->arch.hw_tsc_khz; +} + void kvm_xen_init_vm(struct kvm *kvm) { idr_init(&kvm->arch.xen.evtchn_ports); diff --git a/arch/x86/kvm/xen.h b/arch/x86/kvm/xen.h index ea33d80a0c51..f8f1fe22d090 100644 --- a/arch/x86/kvm/xen.h +++ b/arch/x86/kvm/xen.h @@ -9,6 +9,8 @@ #ifndef __ARCH_X86_KVM_XEN_H__ #define __ARCH_X86_KVM_XEN_H__ =20 +#include + #ifdef CONFIG_KVM_XEN #include =20 @@ -32,6 +34,7 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, int kvm_xen_setup_evtchn(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e, const struct kvm_irq_routing_entry *ue); +void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu); =20 static inline bool kvm_xen_msr_enabled(struct kvm *kvm) { @@ -135,6 +138,10 @@ static inline bool kvm_xen_timer_enabled(struct kvm_vc= pu *vcpu) { return false; } + +static inline void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu) +{ +} #endif =20 int kvm_xen_hypercall(struct kvm_vcpu *vcpu); --=20 2.20.1