From: Eduardo Habkost Subject: [PATCH 3/3] x86: Check level, xlevel before returning CPUID data Date: Thu, 28 Aug 2014 15:02:30 -0300 Message-ID: <1409248950-16268-4-git-send-email-ehabkost@redhat.com> References: <1409248950-16268-1-git-send-email-ehabkost@redhat.com> Cc: Paolo Bonzini To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:57091 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750892AbaH1SCp (ORCPT ); Thu, 28 Aug 2014 14:02:45 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s7SI2jti018830 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Thu, 28 Aug 2014 14:02:45 -0400 In-Reply-To: <1409248950-16268-1-git-send-email-ehabkost@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: None of the existing code using cpuid checks level or xlevel before running it. Instead of changing all callers, make the cpuid() function check if the requested leaf is available, before returning any data. All existing callers of cpuid() and cpuid_indexed() are checks for the presence of feature bits, so it is safe to return all zeroes when the requested CPUID leaf is not available. Signed-off-by: Eduardo Habkost --- lib/x86/processor.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/x86/processor.h b/lib/x86/processor.h index d4e295b..7973879 100644 --- a/lib/x86/processor.h +++ b/lib/x86/processor.h @@ -280,7 +280,7 @@ static inline ulong read_dr7(void) struct cpuid { u32 a, b, c, d; }; -static inline struct cpuid cpuid_indexed(u32 function, u32 index) +static inline struct cpuid raw_cpuid(u32 function, u32 index) { struct cpuid r; asm volatile ("cpuid" @@ -289,6 +289,14 @@ static inline struct cpuid cpuid_indexed(u32 function, u32 index) return r; } +static inline struct cpuid cpuid_indexed(u32 function, u32 index) +{ + u32 level = raw_cpuid(function & 0xf0000000, 0).a; + if (level < function) + return (struct cpuid) { 0, 0, 0, 0 }; + return raw_cpuid(function, index); +} + static inline struct cpuid cpuid(u32 function) { return cpuid_indexed(function, 0); @@ -296,9 +304,9 @@ static inline struct cpuid cpuid(u32 function) static inline u8 cpuid_maxphyaddr(void) { - if (cpuid(0x80000000).a < 0x80000008) + if (raw_cpuid(0x80000000, 0).a < 0x80000008) return 36; - return cpuid(0x80000008).a & 0xff; + return raw_cpuid(0x80000008, 0).a & 0xff; } -- 1.9.3