Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp2846490ybf; Mon, 2 Mar 2020 16:59:20 -0800 (PST) X-Google-Smtp-Source: ADFU+vs5ytfx0kNFYE9mfsa8pqYP3U/NjtaaxXQsh3+D6YexNDXh8+oPZN7trev6IvR0F6vtbuVz X-Received: by 2002:a05:6808:aa8:: with SMTP id r8mr782907oij.7.1583197160113; Mon, 02 Mar 2020 16:59:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583197160; cv=none; d=google.com; s=arc-20160816; b=VlYX07Z4HoUhiakYiU1d9fQNmo56y0lNZc6BT4OSVe1XGFDU+Kdp+c2jve2koJjTBc wlpCJGRLPAV2/Sb6DbCog2BoHq3FN5rjFwhyx+wlgQcI+i6Kjmy2M8Rt3NWAmlvaGsCj yML1juGlT76PavRqAli07XMwquZJuwYPW0LcJWFlNmlgVx8a1Is39OTpGVqzmlERwAUB nPaqIAEOf38XP5n145V/aBZ9GJ19zrN0P6LbF6w4+Xfl8bXvypeRev2nSi+na+IS9Ef2 qJAKLrM3BrsjVh3e/3rJGry3uCvR0205ENlCflE0Cwkhv6Mlp3YbRuED9zMszx4Yyqr0 ofMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=j4kLmkiteV9uOXNrQaN4gJvZevkMGeKJYfvqgzMS/FY=; b=PAMo3k0QlZCSyQ9fvtYnyHPZCSPH44+UuFmBzjoN6ar7IMxSK4wjk4GmxJi6Tq87Qr IJq8DUhAW6cjuYabWB+owCz/30aQnuP71b5etZ/0bUpmxufXL1i3Fe864yYhFjm5PlMb AN9eE1z2FLQFg4ZEDO8NnizlvQE18uPwK001gJpmv/TuqMLuDIFDicWZLiec0n9UE+75 dYTJqQzoKpk94uGKLmMtLypp0ugNiQBrbSzaIRj33A0pi9vQHuDew3CTFIcrCvve1YkF ttnUgCIHKzSO1UiMbAlm6ETBJlsBzztq2WDModXdHAk8qJe4TNn2dbPHylDM5ZzCR2B/ N8OA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f187si7769628oia.218.2020.03.02.16.59.08; Mon, 02 Mar 2020 16:59:20 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727061AbgCCA53 (ORCPT + 99 others); Mon, 2 Mar 2020 19:57:29 -0500 Received: from mga02.intel.com ([134.134.136.20]:28810 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726838AbgCCA52 (ORCPT ); Mon, 2 Mar 2020 19:57:28 -0500 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Mar 2020 16:57:27 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,509,1574150400"; d="scan'208";a="233579149" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.202]) by fmsmga008.fm.intel.com with ESMTP; 02 Mar 2020 16:57:27 -0800 Date: Mon, 2 Mar 2020 16:57:27 -0800 From: Sean Christopherson To: Jim Mattson Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Joerg Roedel , kvm list , LKML , Jan Kiszka , Xiaoyao Li Subject: Re: [PATCH 2/6] KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges Message-ID: <20200303005727.GB27842@linux.intel.com> References: <20200302195736.24777-1-sean.j.christopherson@intel.com> <20200302195736.24777-3-sean.j.christopherson@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 02, 2020 at 01:59:10PM -0800, Jim Mattson wrote: > On Mon, Mar 2, 2020 at 11:57 AM Sean Christopherson > wrote: > > > > Extend the mask in cpuid_function_in_range() for finding the "class" of > > the function to 0xfffffff00. While there is no official definition of > > what constitutes a class, e.g. arguably bits 31:16 should be the class > > and bits 15:0 the functions within that class, the Hypervisor logic > > effectively uses bits 31:8 as the class by virtue of checking for > > different bases in increments of 0x100, e.g. KVM advertises its CPUID > > functions starting at 0x40000100 when HyperV features are advertised at > > the default base of 0x40000000. > > This convention deserves explicit documentation outside of the commit message. No argument there. > > Masking against 0x80000000 only handles basic and extended leafs, which > > results in Centaur and Hypervisor range checks being performed against > > the basic CPUID range, e.g. if CPUID.0x40000000.EAX=0x4000000A and there > > is no entry for CPUID.0x40000006, then function 0x40000006 would be > > incorrectly reported as out of bounds. > > > > The bad range check doesn't cause function problems for any known VMM > > because out-of-range semantics only come into play if the exact entry > > isn't found, and VMMs either support a very limited Hypervisor range, > > e.g. the official KVM range is 0x40000000-0x40000001 (effectively no > > room for undefined leafs) or explicitly defines gaps to be zero, e.g. > > Qemu explicitly creates zeroed entries up to the Cenatur and Hypervisor > > limits (the latter comes into play when providing HyperV features). > > Does Centaur implement the bizarre Intel behavior for out-of-bound > entries? It seems that if there are Centaur leaves defined, the CPUD > semantics should be those specified by Centaur. Ah, right, because this code triggers on !=AMD, not ==Intel. Your guess is as good as mine, I've dug around a few times trying to track down a spec for Centaur/VIA without success. I would say that KVM's emulation behavior should probably be all or nothing, i.e. either due Intel's silly logic for all ranges/classes or do it for none. > > The bad behavior can be visually confirmed by dumping CPUID output in > > the guest when running Qemu with a stable TSC, as Qemu extends the limit > > of range 0x40000000 to 0x40000010 to advertise VMware's cpuid_freq, > > without defining zeroed entries for 0x40000002 - 0x4000000f. > > > > Fixes: 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH") > > Cc: Jim Mattson > > Signed-off-by: Sean Christopherson > > --- > > arch/x86/kvm/cpuid.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c > > index 6be012937eba..c320126e0118 100644 > > --- a/arch/x86/kvm/cpuid.c > > +++ b/arch/x86/kvm/cpuid.c > > @@ -993,7 +993,7 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function) > > { > > struct kvm_cpuid_entry2 *max; > > > > - max = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0); > > + max = kvm_find_cpuid_entry(vcpu, function & 0xffffff00u, 0); > > This assumes that CPUID.(function & 0xffffff00):EAX always contains > the maximum input value for the 256-entry range sharing the high 24 > bits. I don't believe that convention has ever been established or > documented. Not sure if it's formally documented, but it's well established. The closest thing I could find to documentation is the lkml thread where what's implemented today (AFAICT) was proposed. https://lore.kernel.org/lkml/48E3BBC1.2050607@goop.org/ The relevant linux code in Linux (arch/x86/include/asm/processor.h), where @leaves contains the kernel's required minimum leaf to enable paravirt stuff for the hypervisor. static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves) { uint32_t base, eax, signature[3]; for (base = 0x40000000; base < 0x40010000; base += 0x100) { cpuid(base, &eax, &signature[0], &signature[1], &signature[2]); if (!memcmp(sig, signature, 12) && (leaves == 0 || ((eax - base) >= leaves))) return base; } return 0; } > > return max && function <= max->eax; > > } > > > > -- > > 2.24.1 > >