Received: by 10.223.176.5 with SMTP id f5csp811789wra; Fri, 2 Feb 2018 06:32:58 -0800 (PST) X-Google-Smtp-Source: AH8x224qIoUtlqfa0Rup5z7DeZO2Qpepdn02+K78LEExWLhOzG7gBJDgeVdF0blsaKhmqOiY+k+q X-Received: by 10.98.74.154 with SMTP id c26mr5891043pfj.188.1517581978154; Fri, 02 Feb 2018 06:32:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517581978; cv=none; d=google.com; s=arc-20160816; b=hJD6fsPw3C2XWOWXIJGaHnLsQxjRwb90s5zNa5ur67cSNdIC5M1MtvcOaxs41ZQCx1 Og2nV5Ttk5+ZxfjAMdU6hfMAdm7HBVTF0PAk5oyl2mu7EIX2NtoP2f/DcBz53BrDEBbg WtTNObLl6XopofPpcij2BEDxcTd5loBFZnMoWCjRLJquYuNl9Tgv1rBn44iftW83qWyA tD8+AmHtqeJEcDOBVUJypAuPwSGpLJB3z/g21tcqFaRGBBXDoRiMjExkcFyZMkobnIC/ ia5psR6m2EWhO1yzVz5OavEZPuDaWOknWg30HZsh2xofs/Z88Kd6IA6A6A3Q+9tyjtBb 8HVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=U28zzyj8V8r3uZrmEOYQpBCJ7+wTxB7Ko5/Wa8bLvkQ=; b=Gu7MEL091YufYnDWx15CcBqweCAqk7Nxx1rRtYwyQygpmaP9gHcMiARYFQQ5EoLh9y M+tXFHwlHOJ0OYkDdbwGbw88Mjl8Lki4tVk+zlL6CA42ZoKI0FcBmD4MvCDqDb4ou7vz NAMRQzMx4TH9XPyuCTJFvcfY2J+1WdFs4I6YMKu9zMdAftopZcHFZg6Lc8vOrJcEcskf Hk19NLPpV1I46PM9rZC1znPiEyXVI4G8F+WOxBG1LNgKFKoibpXebvd7bwCmi5i5JxH0 dfCiwMAcZW6Ah4JanmucHWK4yd7OjzQ0kawGxYkZ0CDkVWSBVLygiAYc3aHcicWvHkXd Wrmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Ex7Gbwex; 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=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u19si1873411pfh.154.2018.02.02.06.32.42; Fri, 02 Feb 2018 06:32:58 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Ex7Gbwex; 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=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752257AbeBBObM (ORCPT + 99 others); Fri, 2 Feb 2018 09:31:12 -0500 Received: from mail-qt0-f193.google.com ([209.85.216.193]:42703 "EHLO mail-qt0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751988AbeBBObG (ORCPT ); Fri, 2 Feb 2018 09:31:06 -0500 Received: by mail-qt0-f193.google.com with SMTP id c2so31075324qtn.9 for ; Fri, 02 Feb 2018 06:31:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=U28zzyj8V8r3uZrmEOYQpBCJ7+wTxB7Ko5/Wa8bLvkQ=; b=Ex7Gbwex7XNb9pjN74ELaAtdNIZSo/l9g6vxdp6J4BY08s8AYQHvLB99hmDPZn6h7W eCKVwDYldfj2X4j61QPc8DczPYzHmwRV/bzM7p7V8wtj6zW3YJC0NNVIjtuWCtp9kk0g k/ki6eHY6MY4sSY3k0S5SyqZZTppGGF97JsXyDDrjf60TiPcT3xavTfkLAnVCsin1aZE USuoECL8tSruLKv22rEsv4TopgcEQLZtC9jW799nEFn+/M8Q13VSm2mn3EqKO4+z2J/2 3lgFl4Z9Nbz+ZH/X+vuhpbAEDBy2YPF5ZZC3aGgSQyihZZiRMfN1ttGZu/f07QNnLTNS Ts2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=U28zzyj8V8r3uZrmEOYQpBCJ7+wTxB7Ko5/Wa8bLvkQ=; b=IC86m1SDRWU3rGNHr+riMoxqvXTIPEVejA0RZ15L4X+RCln2y8088smQ2ipWVZKmBJ WbJwbeeI3oOYsEoFM5O+tDrI+Iv6T2RTVW34MnUzqyfd4GbnhF361uN/IoP8Kx9J3dSR iz2UcaIGTo2EHdK4GZGx2Nnr8LHbiQCGpe4DiTq3xlAriILneCfAX8hA0xxIgd+wSZNN V+4XfGg9T0ycl7KMBE9zd4tOyOu6ZieeM0bKwLfp8oZkZQSEowh2nGARO0lerbsHfJdz P31HdihtszG7pZQXko4E554QDtC1e6752JrJkLyBkBVr88XpIRlyfjc4ypH+nGOSsG22 frAg== X-Gm-Message-State: AKwxytcLhZs8kR736+rdeTjUcCUIgnc/HFtPLXbGi0QmaamL9RqfIlif l9YS6UMuRD9G88URh3s6Ng== X-Received: by 10.200.54.138 with SMTP id a10mr63926119qtc.123.1517581864920; Fri, 02 Feb 2018 06:31:04 -0800 (PST) Received: from localhost.localdomain.localdomain (50-253-43-157-static.hfc.comcastbusiness.net. [50.253.43.157]) by smtp.gmail.com with ESMTPSA id i31sm1409708qtb.5.2018.02.02.06.31.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Feb 2018 06:31:03 -0800 (PST) From: Matthew Whitehead To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, linux-kernel@vger.kernel.org Cc: Matthew Whitehead Subject: [PATCH] x86/perf : Add check for CPUID instruction before using Date: Fri, 2 Feb 2018 09:30:51 -0500 Message-Id: <1517581851-6600-1-git-send-email-tedheadster@gmail.com> X-Mailer: git-send-email 1.8.3.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We still officially support the ancient i486 cpu. First generation versions of this processor do not have the CPUID instruction, though later versions do. Therefore you must check that the cpu supports it before using it. At present it fails with an "Illegal Instruction" signal on the early processors. This code was based on similar code in the gcc package. Signed-off-by: Matthew Whitehead --- tools/perf/arch/x86/util/header.c | 51 +++++++++++++++++++++++++++++++++++++++ tools/perf/util/header.h | 2 ++ 2 files changed, 53 insertions(+) diff --git a/tools/perf/arch/x86/util/header.c b/tools/perf/arch/x86/util/header.c index fb0d71afee8b..d4d12894c64d 100644 --- a/tools/perf/arch/x86/util/header.c +++ b/tools/perf/arch/x86/util/header.c @@ -7,6 +7,54 @@ #include "../../util/header.h" +/* This code based on gcc cpuid.h __get_cpuid_max() */ +unsigned int have_cpuid(void) +{ + unsigned int __eax, __ebx; + +#ifndef __x86_64__ +/* See if we can use cpuid. On AMD64 we always can. */ +#if __GNUC__ >= 3 + __asm__ ("pushf{l|d}\n\t" + "pushf{l|d}\n\t" + "pop{l}\t%0\n\t" + "mov{l}\t{%0, %1|%1, %0}\n\t" + "xor{l}\t{%2, %0|%0, %2}\n\t" + "push{l}\t%0\n\t" + "popf{l|d}\n\t" + "pushf{l|d}\n\t" + "pop{l}\t%0\n\t" + "popf{l|d}\n\t" + : "=&r" (__eax), "=&r" (__ebx) + : "i" (0x00200000)); +#else +/* Host GCCs older than 3.0 weren't supporting Intel asm syntax + * nor alternatives in i386 code. + */ + __asm__ ("pushfl\n\t" + "pushfl\n\t" + "popl\t%0\n\t" + "movl\t%0, %1\n\t" + "xorl\t%2, %0\n\t" + "pushl\t%0\n\t" + "popfl\n\t" + "pushfl\n\t" + "popl\t%0\n\t" + "popfl\n\t" + : "=&r" (__eax), "=&r" (__ebx) + : "i" (0x00200000)); +#endif + + if (!((__eax ^ __ebx) & 0x00200000)) + return 0; + else + return 1; +#endif + +/* All x86_64 support cpuid */ + return 1; +} + static inline void cpuid(unsigned int op, unsigned int *a, unsigned int *b, unsigned int *c, unsigned int *d) @@ -28,6 +76,9 @@ __get_cpuid(char *buffer, size_t sz, const char *fmt) int nb; char vendor[16]; + if (!have_cpuid()) + return -1; + cpuid(0, &lvl, &b, &c, &d); strncpy(&vendor[0], (char *)(&b), 4); strncpy(&vendor[4], (char *)(&d), 4); diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h index f28aaaa3a440..ce31fca36077 100644 --- a/tools/perf/util/header.h +++ b/tools/perf/util/header.h @@ -171,6 +171,8 @@ int write_padded(struct feat_fd *fd, const void *bf, /* * arch specific callback */ +unsigned int have_cpuid(void); + int get_cpuid(char *buffer, size_t sz); char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused); -- 2.13.6