Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp586310iog; Wed, 29 Jun 2022 06:25:19 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vgp1ZIYt2vUVy7gD19pXHJ+iFfRmepG6STRc9K2o6yoqGzmur/kzf5q356kTl20L+b9/Si X-Received: by 2002:a05:6402:5256:b0:435:9017:e5d3 with SMTP id t22-20020a056402525600b004359017e5d3mr4317342edd.258.1656509119061; Wed, 29 Jun 2022 06:25:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656509119; cv=none; d=google.com; s=arc-20160816; b=uvo/zkJo4H3lRG+EYDrbU9B2Ur+XOq/EPD41Kjdz9c9ek23O1HtZQzpbaXW0SAeXJA qxyn3Pz3NhW+8o+xYH903E+z9L0vZpMhYNYWg3g/30IlYglmMoLme8X7nGKwOwqlmT/G FFIG+F3yr8hSage8FJOAARsOXDuI6WpLxcn7/KOs4B6CIz/cRgmOxNhDlBkESLSE6xHI x+eKb8ecTA4WmpkyPgQEmAYRUxYSP3iLJ7dhdOYOwOzRtVvoEfleMGrQtenAF/NOVBMM IRr88RJpXyqOtCm8RAHaOJI6TGgdwh8uSeb/5RzlimxT4KcxRn+G7NWdytnyE6cHtbmB 058A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=RQbyFQMbPS//dK4EaGjlyRyoqyD0Dj9U4ecViSqyJaY=; b=wUdRlQsmi9CeoWZnoxGPA9VeawCUvmwCNfuVkufaOVbCqzQsDqVQ8C4KIz7Dvo5r9C V0QlNqhEVMU0e6Cla6QrqF33ZepO4PM5mQhtWka4XyKuOGsRN8mpiNMfUewukaEdigQV NyDsuBkmn2HdR6BHawIzKCv1Zd/fnLqosHys2dnZLE8UmARW8gJXjxgymPqfGfZr6hm0 l0R4CcRT8nAAjWZGExbMw+wY3RTaivi4zNGm8WeK2CTTSq7erLAct1TJg5FJPGD1FmtX kBYtHB7z0+QCJawdTzNxKfmQ6BURicSf6Q1dnivXWmX4H6MQZTYvgAFCeFo5MHr8g4yO i3DA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ds8-20020a170907724800b006ff1a60c3ebsi605938ejc.821.2022.06.29.06.24.53; Wed, 29 Jun 2022 06:25:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233703AbiF2NHO (ORCPT + 99 others); Wed, 29 Jun 2022 09:07:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233693AbiF2NHJ (ORCPT ); Wed, 29 Jun 2022 09:07:09 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 874C62B1AB; Wed, 29 Jun 2022 06:07:05 -0700 (PDT) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1o6XOw-00033l-V4; Wed, 29 Jun 2022 13:06:38 +0000 Received: from 54-240-197-231.amazon.com ([54.240.197.231] 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 1o6XOw-0003LW-Ia; Wed, 29 Jun 2022 13:06:38 +0000 From: Paul Durrant To: x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH v5] KVM: x86/xen: Update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present Date: Wed, 29 Jun 2022 14:05:13 +0100 Message-Id: <20220629130514.15780-1-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIM_ADSP_ALL, RCVD_IN_DNSWL_MED,SPF_FAIL,SPF_HELO_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- Cc: David Woodhouse 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/kvm/cpuid.c | 2 ++ arch/x86/kvm/x86.c | 1 + arch/x86/kvm/xen.c | 51 +++++++++++++++++++++++++++++++++ arch/x86/kvm/xen.h | 10 +++++++ 5 files changed, 65 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 88a3026ee163..abb0a39f60eb 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -638,6 +638,7 @@ struct kvm_vcpu_xen { struct hrtimer timer; int poll_evtchn; struct timer_list poll_timer; + u32 cpuid_tsc_info; }; struct kvm_vcpu_arch { diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index d47222ab8e6e..544d0f823ee5 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" /* * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be @@ -310,6 +311,7 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) __cr4_reserved_bits(guest_cpuid_has, vcpu); kvm_hv_set_cpuid(vcpu); + kvm_xen_after_set_cpuid(vcpu); /* Invoke the vendor callback only after the above state is updated. */ static_call(kvm_x86_vcpu_after_set_cpuid)(vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 031678eff28e..29ed665c51db 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3110,6 +3110,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 = tgt_tsc_khz; + kvm_xen_setup_tsc_info(v); } vcpu->hv_clock.tsc_timestamp = tsc_timestamp; diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 610beba35907..c84424d5c8b6 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -10,6 +10,9 @@ #include "xen.h" #include "hyperv.h" #include "lapic.h" +#include "cpuid.h" + +#include #include #include @@ -1855,3 +1858,51 @@ void kvm_xen_destroy_vm(struct kvm *kvm) if (kvm->arch.xen_hvm_config.msr) static_branch_slow_dec_deferred(&kvm_xen_enabled); } + +void kvm_xen_after_set_cpuid(struct kvm_vcpu *vcpu) +{ + u32 base = 0; + u32 limit; + u32 function; + + vcpu->arch.xen.cpuid_tsc_info = 0; + + for_each_possible_hypervisor_cpuid_base(function) { + struct kvm_cpuid_entry2 *entry = kvm_find_cpuid_entry(vcpu, function, 0); + + if (entry && + entry->ebx == XEN_CPUID_SIGNATURE_EBX && + entry->ecx == XEN_CPUID_SIGNATURE_ECX && + entry->edx == XEN_CPUID_SIGNATURE_EDX) { + base = function; + limit = entry->eax; + break; + } + } + if (!base) + return; + + function = base | XEN_CPUID_LEAF(3); + if (function > limit) + return; + + vcpu->arch.xen.cpuid_tsc_info = function; +} + +void kvm_xen_setup_tsc_info(struct kvm_vcpu *vcpu) +{ + struct kvm_cpuid_entry2 *entry; + + if (!vcpu->arch.xen.cpuid_tsc_info) + return; + + entry = kvm_find_cpuid_entry(vcpu, vcpu->arch.xen.cpuid_tsc_info, 1); + if (entry) { + entry->ecx = vcpu->arch.hv_clock.tsc_to_system_mul; + entry->edx = vcpu->arch.hv_clock.tsc_shift; + } + + entry = kvm_find_cpuid_entry(vcpu, vcpu->arch.xen.cpuid_tsc_info, 2); + if (entry) + entry->eax = vcpu->arch.hw_tsc_khz; +} diff --git a/arch/x86/kvm/xen.h b/arch/x86/kvm/xen.h index 532a535a9e99..b2ca434431d6 100644 --- a/arch/x86/kvm/xen.h +++ b/arch/x86/kvm/xen.h @@ -32,6 +32,8 @@ 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_after_set_cpuid(struct kvm_vcpu *vcpu); +void kvm_xen_setup_tsc_info(struct kvm_vcpu *vcpu); static inline bool kvm_xen_msr_enabled(struct kvm *kvm) { @@ -135,6 +137,14 @@ static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu) { return false; } + +static inline void kvm_xen_after_set_cpuid(struct kvm_vcpu *vcpu) +{ +} + +static inline void kvm_xen_setup_tsc_info(struct kvm_vcpu *vcpu) +{ +} #endif int kvm_xen_hypercall(struct kvm_vcpu *vcpu); -- 2.20.1