Skip to contentSkip to conversation

tools/x86: Add a kcpuid tool to show raw CPU features

PATCH Applied Feng Tang 3 participants 3 messages Mar 5, 2021 – Mar 8, 2021 x86 cpuid database

lore
Share & tools
lore

Feng Tang

author
#0 lore raw

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

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 <[email protected]>

---
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
tools/arch/x86/kcpuid/Makefile new file +24 -0
new file mode 100644
index 0000000..87b554f
@@ -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
tools/arch/x86/kcpuid/cpuid.csv new file +380 -0

380 lines not on this page · raw message

tools/arch/x86/kcpuid/kcpuid.c new file +646 -0

646 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw
Quoted 2 lines · Feng Tang

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 <[email protected]>
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 <[email protected]>

---
 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
tools/arch/x86/kcpuid/Makefile new file +24 -0
new file mode 100644
index 000000000000..87b554fab14b
@@ -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
tools/arch/x86/kcpuid/cpuid.csv new file +380 -0

380 lines not on this page · raw message

tools/arch/x86/kcpuid/kcpuid.c new file +655 -0

655 lines not on this page · raw message

Signature · 7 lines
2.29.2

-- 
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
merge bot Applied
#2 lore raw
In reply to Feng Tang
The following commit has been merged into the x86/misc branch of tip:

The following commit has been merged into the x86/misc branch of tip:

Commit-ID:     c6b2f240bf8d5604e6507aff15d5c441944c2f89
Gitweb:        https://git.kernel.org/tip/c6b2f240bf8d5604e6507aff15d5c441944c2f89
Author:        Feng Tang <[email protected]>
AuthorDate:    Fri, 05 Mar 2021 15:21:18 +08:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 08 Mar 2021 12:50:19 +01:00

tools/x86: Add a kcpuid tool to show raw CPU features

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 <[email protected]>

---
 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
tools/arch/x86/kcpuid/Makefile new file +24 -0
new file mode 100644
index 0000000..87b554f
@@ -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
tools/arch/x86/kcpuid/cpuid.csv new file +380 -0

380 lines not on this page · raw message

tools/arch/x86/kcpuid/kcpuid.c new file +655 -0

655 lines not on this page · raw message