Re: [PATCH v5] tools/x86: Add a kcpuid tool to show raw CPU features

2021-03-05 Thread Borislav Petkov
On Fri, Mar 05, 2021 at 03:21:18PM +0800, Feng Tang wrote:
> End users frequently want to know what features their processor
> supports, independent of what the kernel supports.

Did some cleanups and improvements on top and I think we're ready to go.
I'll queue it soon if no-one complains.

---
From: Feng Tang 
Date: Fri, 5 Mar 2021 15:21:18 +0800
Subject: [PATCH] tools/x86: Add a kcpuid tool to show raw CPU features
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

End users frequently want to know what features their processor
supports, independent of what the kernel supports.

/proc/cpuinfo is great. It is omnipresent and since it is provided by
the kernel it is always as up to date as the kernel. But, it could be
ambiguous about processor features which can be disabled by the kernel
at boot-time or compile-time.

There are some user space tools showing more raw features, but they are
not bound with kernel, and go with distros. Many end users are still
using old distros with new kernels (upgraded by themselves), and may
not upgrade the distros only to get a newer tool.

So here arise the need for a new tool, which
  * shows raw CPU features read from the CPUID instruction
  * will be easier to update compared to existing userspace
tooling (perhaps distributed like perf)
  * inherits "modern" kernel development process, in contrast to some
of the existing userspace CPUID tools which are still being developed
without git and distributed in tarballs from non-https sites.
  * Can produce output consistent with /proc/cpuinfo to make comparison
easier.

The CPUID leaf definitions are kept in an .csv file which allows for
updating only that file to add support for new feature leafs.

This is based on prototype code from Borislav Petkov
(http://sr71.net/~dave/intel/stupid-cpuid.c).

 [ bp:
   - Massage, add #define _GNU_SOURCE to fix implicit declaration of
 function ‘strcasestr' warning
   - remove superfluous newlines
   - fallback to cpuid.csv in the current dir if none found
   - fix typos
   - move comments over the lines instead of sideways. ]

Originally-from: Borislav Petkov 
Suggested-by: Dave Hansen 
Suggested-by: Borislav Petkov 
Signed-off-by: Feng Tang 
Signed-off-by: Borislav Petkov 
Link: 
https://lkml.kernel.org/r/1614928878-86075-1-git-send-email-feng.t...@intel.com
---
 tools/arch/x86/kcpuid/Makefile  |  24 ++
 tools/arch/x86/kcpuid/cpuid.csv | 380 ++
 tools/arch/x86/kcpuid/kcpuid.c  | 655 
 3 files changed, 1059 insertions(+)
 create mode 100644 tools/arch/x86/kcpuid/Makefile
 create mode 100644 tools/arch/x86/kcpuid/cpuid.csv
 create mode 100644 tools/arch/x86/kcpuid/kcpuid.c

diff --git a/tools/arch/x86/kcpuid/Makefile b/tools/arch/x86/kcpuid/Makefile
new file mode 100644
index ..87b554fab14b
--- /dev/null
+++ b/tools/arch/x86/kcpuid/Makefile
@@ -0,0 +1,24 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for x86/kcpuid tool
+
+kcpuid : kcpuid.c
+
+CFLAGS = -Wextra
+
+BINDIR ?= /usr/sbin
+
+HWDATADIR ?= /usr/share/misc/
+
+override CFLAGS += -O2 -Wall -I../../../include
+
+%: %.c
+   $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
+
+.PHONY : clean
+clean :
+   @rm -f kcpuid
+
+install : kcpuid
+   install -d  $(DESTDIR)$(BINDIR)
+   install -m 755 -p kcpuid $(DESTDIR)$(BINDIR)/kcpuid
+   install -m 444 -p cpuid.csv $(HWDATADIR)/cpuid.csv
diff --git a/tools/arch/x86/kcpuid/cpuid.csv b/tools/arch/x86/kcpuid/cpuid.csv
new file mode 100644
index ..f4a5b85073f4
--- /dev/null
+++ b/tools/arch/x86/kcpuid/cpuid.csv
@@ -0,0 +1,380 @@
+# The basic row format is:
+# LEAF, SUBLEAF, register_name, bits, short_name, long_description
+
+# Leaf 00H
+ 0,0,  EAX,   31:0, max_basic_leafs, Max input value for supported 
subleafs
+
+# Leaf 01H
+ 1,0,  EAX,3:0, stepping, Stepping ID
+ 1,0,  EAX,7:4, model, Model
+ 1,0,  EAX,   11:8, family, Family ID
+ 1,0,  EAX,  13:12, processor, Processor Type
+ 1,0,  EAX,  19:16, model_ext, Extended Model ID
+ 1,0,  EAX,  27:20, family_ext, Extended Family ID
+
+ 1,0,  EBX,7:0, brand, Brand Index
+ 1,0,  EBX,   15:8, clflush_size, CLFLUSH line size (value * 8) in 
bytes
+ 1,0,  EBX,  23:16, max_cpu_id, Maxim number of addressable logic 
cpu in this package
+ 1,0,  EBX,  31:24, apic_id, Initial APIC ID
+
+ 1,0,  ECX,  0, sse3, Streaming SIMD Extensions 3(SSE3)
+ 1,0,  ECX,  1, pclmulqdq, PCLMULQDQ instruction supported
+ 1,0,  ECX,  2, dtes64, DS area uses 64-bit layout
+ 1,0,  ECX,  3, mwait, MONITOR/MWAIT supported
+ 1,0,  ECX,  4, ds_cpl, CPL Qualified Debug Store which allows 
for branch message storage qualified by CPL
+ 1,0,  ECX,  5, vmx, Virtual Machine Extensions supported
+ 1,0

[PATCH v5] tools/x86: Add a kcpuid tool to show raw CPU features

2021-03-04 Thread Feng Tang
End users frequently want to know what features their processor
supports, independent of what the kernel supports.

/proc/cpuinfo is great. It is omnipresent and since it is provided by
the kernel it is always as up to date as the kernel. But, it could be
ambiguous about processor features which can be disabled by the kernel
at boot-time or compile-time.

There are some user space tools showing more raw features, but they are
not bound with kernel, and go with distros. Many end users are still
using old distros with new kernels (upgraded by themselves), and may
not upgrade the distros only to get a newer tool.

So here arise the need for a new tool, which
  * shows raw CPU features read from the CPUID instruction
  * will be easier to update compared to existing userspace
tooling (perhaps distributed like perf)
  * inherits "modern" kernel development process, in contrast to some
of the existing userspace CPUID tools which are still being developed
without git and distributed in tarballs from non-https sites.
  * Can produce output consistent with /proc/cpuinfo to make comparison
easier.

The CPUID leaf definitions are kept in an .csv file which allows for
updating only that file to add support for new feature leafs.

This is based on prototype code from Borislav Petkov
(http://sr71.net/~dave/intel/stupid-cpuid.c).

 [ bp: Massage, add #define _GNU_SOURCE to fix implicit declaration of
   function ‘strcasestr' warning. ]

Originally-from: Borislav Petkov 
Suggested-by: Dave Hansen 
Suggested-by: Borislav Petkov 
Signed-off-by: Feng Tang 
Signed-off-by: Borislav Petkov 
Link: 
https://lkml.kernel.org/r/1603344083-100742-1-git-send-email-feng.t...@intel.com
---
Changelog:

  v5:
  * rebased against v5.11
  * fix a buffer overflow issue in v4

  v4:
  * rebase against 5.11-rc4
  * Boris helped to find and fix a segmentation fault and one
compile warning
  * add more cpuid info into cpuid.csv
  * will show boolean bit flags only by default
  * some code cleanup

  v3:
  * use .csv file instead of plain text file which also uses
comma to separate fields (Borislav)
  * add option to support a user specified .csv file (Dave)
  * make install will put the csv file under /usr/share/hwdata/ (Dave)

  v2:
  * use a new text file to store all the bits definition of each
CPUID leaf/subleafs, which is easier for future expansion, as
the core .c file will be kept untouched, suggested by Borislav/Dave
  * some code cleanup

 tools/arch/x86/kcpuid/Makefile  |  24 ++
 tools/arch/x86/kcpuid/cpuid.csv | 380 +++
 tools/arch/x86/kcpuid/kcpuid.c  | 646 
 3 files changed, 1050 insertions(+)
 create mode 100644 tools/arch/x86/kcpuid/Makefile
 create mode 100644 tools/arch/x86/kcpuid/cpuid.csv
 create mode 100644 tools/arch/x86/kcpuid/kcpuid.c

diff --git a/tools/arch/x86/kcpuid/Makefile b/tools/arch/x86/kcpuid/Makefile
new file mode 100644
index 000..87b554f
--- /dev/null
+++ b/tools/arch/x86/kcpuid/Makefile
@@ -0,0 +1,24 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for x86/kcpuid tool
+
+kcpuid : kcpuid.c
+
+CFLAGS = -Wextra
+
+BINDIR ?= /usr/sbin
+
+HWDATADIR ?= /usr/share/misc/
+
+override CFLAGS += -O2 -Wall -I../../../include
+
+%: %.c
+   $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
+
+.PHONY : clean
+clean :
+   @rm -f kcpuid
+
+install : kcpuid
+   install -d  $(DESTDIR)$(BINDIR)
+   install -m 755 -p kcpuid $(DESTDIR)$(BINDIR)/kcpuid
+   install -m 444 -p cpuid.csv $(HWDATADIR)/cpuid.csv
diff --git a/tools/arch/x86/kcpuid/cpuid.csv b/tools/arch/x86/kcpuid/cpuid.csv
new file mode 100644
index 000..f4a5b85
--- /dev/null
+++ b/tools/arch/x86/kcpuid/cpuid.csv
@@ -0,0 +1,380 @@
+# The basic row format is:
+# LEAF, SUBLEAF, register_name, bits, short_name, long_description
+
+# Leaf 00H
+ 0,0,  EAX,   31:0, max_basic_leafs, Max input value for supported 
subleafs
+
+# Leaf 01H
+ 1,0,  EAX,3:0, stepping, Stepping ID
+ 1,0,  EAX,7:4, model, Model
+ 1,0,  EAX,   11:8, family, Family ID
+ 1,0,  EAX,  13:12, processor, Processor Type
+ 1,0,  EAX,  19:16, model_ext, Extended Model ID
+ 1,0,  EAX,  27:20, family_ext, Extended Family ID
+
+ 1,0,  EBX,7:0, brand, Brand Index
+ 1,0,  EBX,   15:8, clflush_size, CLFLUSH line size (value * 8) in 
bytes
+ 1,0,  EBX,  23:16, max_cpu_id, Maxim number of addressable logic 
cpu in this package
+ 1,0,  EBX,  31:24, apic_id, Initial APIC ID
+
+ 1,0,  ECX,  0, sse3, Streaming SIMD Extensions 3(SSE3)
+ 1,0,  ECX,  1, pclmulqdq, PCLMULQDQ instruction supported
+ 1,0,  ECX,  2, dtes64, DS area uses 64-bit layout
+ 1,0,  ECX,  3, mwait, MONITOR/MWAIT supported
+ 1,0,  ECX,  4, ds_cpl, CPL Qualified Debug Store which allows 
for branch message storage qualified by CPL