From patchwork Thu Aug 28 18:02:30 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduardo Habkost X-Patchwork-Id: 4806111 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 64D69C0338 for ; Thu, 28 Aug 2014 18:02:55 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 277402013A for ; Thu, 28 Aug 2014 18:02:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2B5C720117 for ; Thu, 28 Aug 2014 18:02:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752130AbaH1SCq (ORCPT ); Thu, 28 Aug 2014 14:02:46 -0400 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 Received: from localhost (ovpn-113-147.phx2.redhat.com [10.3.113.147]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s7SI2iSb021647; Thu, 28 Aug 2014 14:02:45 -0400 From: Eduardo Habkost To: kvm@vger.kernel.org Cc: Paolo Bonzini 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> In-Reply-To: <1409248950-16268-1-git-send-email-ehabkost@redhat.com> References: <1409248950-16268-1-git-send-email-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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; }