Skip to contentSkip to conversation

tools/x86: add kcpuid tool to show raw CPU features

RFC PATCH Superseded Feng Tang 4 participants 12 messages Aug 27, 2020 – Sep 3, 2020

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Feng Tang

author
#0 lore raw

RFC PATCH 1/1 tools/x86: add 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 can 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
1. Can make up for the ambiguity of cpuinfo
2. Be easier to obtain updates for compared to existing userspace tooling (perhaps distributed like perf)
3. 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.
4. Can produce output consistent with /proc/cpuinfo to make comparison easier
5. Being in-kernel, could leverage kernel enabling, and even theoretically consume arch/x86/boot/cpustr.h so it could pick up new features directly from one-line X86_FEATURE_* definitions.

This RFC is an early prototype, and would get community's opinion on whether it's the right thing to do, and what functions it should also support.

Current version only shows limited number of cpu features, and can also dump all raw cpuid leaf data. Some todos are:
1. Parse all cpu raw features
2. Run cpuid with specific parameters from user input

This is based on the prototype code from Borislav Petkov [1]

output of the tool
------------------

	CPUID leafs total: 28

	cpu family	: 6
	model		: 13
	stepping	: 7

	CPU features
	------------
	 sse3
	 pclmlqdq
	 dtes64
	 mwait
	 ds_cpl
	 vmx
	 smx
	 eist
	 tm2
	 ssse3
	 cx16
	 pctxid
	 dca
	 sse4_1
	 sse4_2
	 x2apic
	 tsc_deadline_timer
	 aes
	 xsave
	 osxsave
	 avx
	 fpu
	 ...

[1] http://sr71.net/~dave/intel/stupid-cpuid.c

---
 tools/arch/x86/tools/kcpuid/Makefile |  21 ++
 tools/arch/x86/tools/kcpuid/kcpuid.c | 422 +++++++++++++++++++++++++++++++++++
 2 files changed, 443 insertions(+)
 create mode 100644 tools/arch/x86/tools/kcpuid/Makefile
 create mode 100644 tools/arch/x86/tools/kcpuid/kcpuid.c
tools/arch/x86/tools/kcpuid/Makefile new file +21 -0
new file mode 100644
index 0000000..21453e5
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for x86/kcpuid tool
+
+kcpuid : kcpuid.c
+
+CFLAGS =  -Wextra
+
+BINDIR ?= /usr/sbin
+
+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
tools/arch/x86/tools/kcpuid/kcpuid.c new file +422 -0

422 lines not on this page · raw message

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

output of the tool
------------------

CPUID leafs total: 28

cpu family : 6
model : 13
stepping : 7

Yeah, this should dump model etc and those numbers should be in hex and additionally in dec if people prefer them.

Quoted 17 lines
	CPU features
	------------
	 sse3
	 pclmlqdq
	 dtes64
	 mwait
	 ds_cpl
	 vmx
	 smx
	 eist
	 tm2
	 ssse3
	 cx16
	 pctxid
	 dca
	 sse4_1
	 sse4_2
Quoted 8 lines
	 x2apic
	 tsc_deadline_timer
	 aes
	 xsave
	 osxsave
	 avx
	 fpu
	 ...

I guess that's good for grepping. With a lot of leafs, leaf output should probably be controlled by cmdline opts.

Quoted 11 lines

[1] http://sr71.net/~dave/intel/stupid-cpuid.c

---
 tools/arch/x86/tools/kcpuid/Makefile |  21 ++
 tools/arch/x86/tools/kcpuid/kcpuid.c | 422 +++++++++++++++++++++++++++++++++++
 2 files changed, 443 insertions(+)
 create mode 100644 tools/arch/x86/tools/kcpuid/Makefile
 create mode 100644 tools/arch/x86/tools/kcpuid/kcpuid.c

Let's drop the second "tools" from the path:

tools/arch/x86/kcpuid/Makefile
tools/arch/x86/kcpuid/kcpuid.c

tools/arch/x86/tools/kcpuid/kcpuid.c line 51

Quoted 9 lines
tools/arch/x86/tools/kcpuid/kcpuid.c new file +9 -0
+struct reg_01_a {
+	u32 stepping: 4;	/* bit 0 */
+	u32 model: 4;
+	u32 family: 4;
+	u32 type: 2;
+	u32 pad1: 2;
+	u32 model_ext: 4;	/* bit 16 */
+	u32 family_ext: 8;
+};

Yeah, instead of defining a separate struct for each leaf I think it would be smarter/better to have a text file in a machine parseable format which defines your leafs.

When you need to add a new leaf, you simply extend the text file and the tool parses it anew and has its all CPUID info uptodate. This way you won't even have to recompile it. Adding new CPUID leafs would be adding new lines to the file.

For example:

LEAF<num>,SUBLEAF<num>,[EAX,EBX,ECX,EDX]{[width]<Mnemonic>|<Long text>,...}

LEAF07,SUBLEAF00,EAX{[31:0]max_value|Max input value for supported subleafs} LEAF07,SUBLEAF00,EBX{[0]FSGSBASE|RDFSBASE/RDGSBASE/WRFSBASE/WRGSBASE if 1.,
[1]TSC_ADJUST|IA32_TSC_ADJUST MSR is supported if 1., [2]SGX|Supports Intel® Software Guard Extensions (Intel® SGX Extensions) if 1.,
...
}
LEAF07,SUBLEAF00,ECX{[0]PREFETCHWT1|(Intel® Xeon PhiTM only.),

		     [1]AVX512_VBMI|,
		     ...

This is just a dumb attempt but I hope it comes across where I'm getting with this.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#2 lore raw
Quoted 16 lines · Borislav Petkov

When you need to add a new leaf, you simply extend the text file and the tool parses it anew and has its all CPUID info uptodate. This way you won't even have to recompile it. Adding new CPUID leafs would be adding new lines to the file.

For example:

LEAF<num>,SUBLEAF<num>,[EAX,EBX,ECX,EDX]{[width]<Mnemonic>|<Long text>,...}

LEAF07,SUBLEAF00,EAX{[31:0]max_value|Max input value for supported subleafs} LEAF07,SUBLEAF00,EBX{[0]FSGSBASE|RDFSBASE/RDGSBASE/WRFSBASE/WRGSBASE if 1.,
[1]TSC_ADJUST|IA32_TSC_ADJUST MSR is supported if 1., [2]SGX|Supports Intel® Software Guard Extensions (Intel® SGX Extensions) if 1.,
...
}
LEAF07,SUBLEAF00,ECX{[0]PREFETCHWT1|(Intel® Xeon PhiTM only.),

Quoted 5 lines
		     [1]AVX512_VBMI|,
		     ...

This is just a dumb attempt but I hope it comes across where I'm getting with this.

Ugh. I hate text parsers. But, I do see what you're getting at.

If we do this, I bet having each value on its own line, I bet this would be a bit less painful to parse. It'll be a _bit_ harder to read, but not really. Maybe:

LEAF[07],SUBLEAF[00],EAX[31:0],max_value, Max input value for supported
subleafs
LEAF[07],SUBLEAF[00],EBX[ 0],FSGSBASE,
RDFSBASE/RDGSBASE/WRFSBASE/WRGSBASE if 1.
LEAF[07],SUBLEAF[00],EBX[ 1],TSC_ADJUST, IA32_TSC_ADJUST MSR is
supported if 1.
LEAF[07],SUBLEAF[00],EBX[ 2],SGX, Supports Intel® Software Guard
Extensions (Intel® SGX Extensions) if 1.

I was *really* hoping that we could eventually feed kcpuid and the X86_FEATURE_* bits from the same source. Does this take us away from that? Is that a worthy long-term goal?

Maybe we'd be better off reusing the parser for cpufeatures.h. The majority of the same information is there, except for the mapping from word to CPUID leaf, and multi-bit values.

#3 lore raw
Quoted 1 line · Dave Hansen

Ugh. I hate text parsers. But, I do see what you're getting at.

That's why it'll be a simple one. Nothing fancy. I'm hoping the final version would be somewhat readable to humans too.

Quoted 4 lines

If we do this, I bet having each value on its own line, I bet this would be a bit less painful to parse. It'll be a _bit_ harder to read, but not really. Maybe:

Quoted 8 lines

LEAF[07],SUBLEAF[00],EAX[31:0],max_value, Max input value for supported
subleafs
LEAF[07],SUBLEAF[00],EBX[ 0],FSGSBASE,
RDFSBASE/RDGSBASE/WRFSBASE/WRGSBASE if 1.
LEAF[07],SUBLEAF[00],EBX[ 1],TSC_ADJUST, IA32_TSC_ADJUST MSR is
supported if 1.
LEAF[07],SUBLEAF[00],EBX[ 2],SGX, Supports Intel® Software Guard
Extensions (Intel® SGX Extensions) if 1.

Yeah, this was just a stupid example to start the conversation.

Quoted 2 lines

I was *really* hoping that we could eventually feed kcpuid and the X86_FEATURE_* bits from the same source.

But X86_FEATURE_* won't be all bits in all CPUID leafs - only the ones the kernel has enabled/use for/needs/...

Also you have CPUID fields which are multi-bit and which cannot be X86_FEATURE_* things.

So you need a place which has the whole CPUID space and X86_FEATURE_* simply cannot represent it all.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#4 lore raw
Quoted 10 lines · Borislav Petkov
Quoted 2 lines

I was *really* hoping that we could eventually feed kcpuid and the X86_FEATURE_* bits from the same source.

But X86_FEATURE_* won't be all bits in all CPUID leafs - only the ones the kernel has enabled/use for/needs/...

Also you have CPUID fields which are multi-bit and which cannot be X86_FEATURE_* things.

So you need a place which has the whole CPUID space and X86_FEATURE_* simply cannot represent it all.

All good points.

Personally, I think I'd be OK if kcpuid _focused_ on single-bit values only. Those are the vast majority of the things that we need from such a tool. I don't think there's nearly as much demand for the topology enumeration or TLB structure leaf parsing.

I also kinda doubt we can come up with a file format off the top of our heads that can represent all the ways data is represented in the CPUID space. Single bits, sure.

#5 lore raw
Quoted 10 lines · Dave Hansen

All good points.

Personally, I think I'd be OK if kcpuid _focused_ on single-bit values only. Those are the vast majority of the things that we need from such a tool. I don't think there's nearly as much demand for the topology enumeration or TLB structure leaf parsing.

I also kinda doubt we can come up with a file format off the top of our heads that can represent all the ways data is represented in the CPUID space. Single bits, sure.

Yap, agreed.

However, we should try to at least provide for some rudimentary support for multibit fields in case we decide we need them down the road and after the fact.

I guess it is enough if the tool dumps them and their meaning - if one needs further info then one would go do the *actual* CPU documentation anyway...

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author
#6 lore raw
In reply to Borislav Petkov
Quoted 4 lines · Borislav Petkov
Quoted 3 lines · Feng Tang

output of the tool
------------------

Quoted 8 lines
Quoted 5 lines

CPUID leafs total: 28

cpu family : 6
model : 13
stepping : 7

Yeah, this should dump model etc and those numbers should be in hex and additionally in dec if people prefer them.

Ok

Quoted 12 lines
Quoted 11 lines
	CPU features
	------------
	 sse3
	 pclmlqdq
	 dtes64
	 mwait
	 ds_cpl
	 vmx
	 smx
	 fpu
	 ...
Quoted 2 lines

I guess that's good for grepping. With a lot of leafs, leaf output should probably be controlled by cmdline opts.

Will add input option for specific leafs or all.

Quoted 10 lines
Quoted 5 lines
 tools/arch/x86/tools/kcpuid/Makefile |  21 ++
 tools/arch/x86/tools/kcpuid/kcpuid.c | 422 +++++++++++++++++++++++++++++++++++
 2 files changed, 443 insertions(+)
 create mode 100644 tools/arch/x86/tools/kcpuid/Makefile
 create mode 100644 tools/arch/x86/tools/kcpuid/kcpuid.c

Let's drop the second "tools" from the path:

tools/arch/x86/kcpuid/Makefile
tools/arch/x86/kcpuid/kcpuid.c

Will do.

Quoted 30 lines

tools/arch/x86/tools/kcpuid/kcpuid.c line 51

Quoted 9 lines
tools/arch/x86/tools/kcpuid/kcpuid.c new file +9 -0
+struct reg_01_a {
+	u32 stepping: 4;	/* bit 0 */
+	u32 model: 4;
+	u32 family: 4;
+	u32 type: 2;
+	u32 pad1: 2;
+	u32 model_ext: 4;	/* bit 16 */
+	u32 family_ext: 8;
+};

Yeah, instead of defining a separate struct for each leaf I think it would be smarter/better to have a text file in a machine parseable format which defines your leafs.

When you need to add a new leaf, you simply extend the text file and the tool parses it anew and has its all CPUID info uptodate. This way you won't even have to recompile it. Adding new CPUID leafs would be adding new lines to the file.

For example:

LEAF<num>,SUBLEAF<num>,[EAX,EBX,ECX,EDX]{[width]<Mnemonic>|<Long text>,...}

LEAF07,SUBLEAF00,EAX{[31:0]max_value|Max input value for supported subleafs} LEAF07,SUBLEAF00,EBX{[0]FSGSBASE|RDFSBASE/RDGSBASE/WRFSBASE/WRGSBASE if 1.,
[1]TSC_ADJUST|IA32_TSC_ADJUST MSR is supported if 1., [2]SGX|Supports Intel® Software Guard Extensions (Intel® SGX Extensions) if 1.,
...
}
LEAF07,SUBLEAF00,ECX{[0]PREFETCHWT1|(Intel® Xeon PhiTM only.),

Quoted 5 lines
		     [1]AVX512_VBMI|,
		     ...

This is just a dumb attempt but I hope it comes across where I'm getting with this.

Will check this, and further study the different bits you, Peter and Dave have mentioned in the discussion.

Thank you all for the review and suggestions!

- Feng

#7 lore raw
In reply to Feng Tang
Quoted 7 lines · 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 can be ambiguous about processor features which can be disabled by the kernel at boot-time or compile-time.

Let me once again suggest we use CPUID faulting to 'fix' this.

We really should clear the CPUID bits when the kernel explicitly disables things.

https://lkml.kernel.org/r/[email protected]

#8 lore raw
Quoted 9 lines · On 9/2/20 9:45 AM, [email protected] wrote:
Quoted 7 lines · 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 can be ambiguous about processor features which can be disabled by the kernel at boot-time or compile-time.

Quoted 6 lines

Let me once again suggest we use CPUID faulting to 'fix' this.

We really should clear the CPUID bits when the kernel explicitly disables things.

https://lkml.kernel.org/r/[email protected]

Wouldn't we also have to turn on faulting for all the bits that are enumerated, but of which the kernel is unaware?

Right now, a bit that we clearcpuid= and one about which the kernel is totally unaware look the same.

#9 lore raw
Quoted 15 lines · Dave Hansen
Quoted 14 lines · On 9/2/20 9:45 AM, [email protected] wrote:
Quoted 7 lines · 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 can be ambiguous about processor features which can be disabled by the kernel at boot-time or compile-time.

Let me once again suggest we use CPUID faulting to 'fix' this.

We really should clear the CPUID bits when the kernel explicitly disables things.

Quoted 7 lines
Quoted 1 line

https://lkml.kernel.org/r/[email protected]

Wouldn't we also have to turn on faulting for all the bits that are enumerated, but of which the kernel is unaware?

Right now, a bit that we clearcpuid= and one about which the kernel is totally unaware look the same.

I mostly care about the things the kernel disables (and thus knows about), the rest is a pass-through IIRC.

#10 lore raw
In reply to [email protected]
Quoted 2 lines · On Wed, Sep 02, 2020 at 06:45:38PM +0200, [email protected] wrote:

We really should clear the CPUID bits when the kernel explicitly disables things.

Actually, you want to *disable* the functionality behind it by clearing a bit in CR4 - and yes, not all features have CR4 bits - so that luserspace doesn't "probe" the existence of certain instructions.

Example: you can still try to run RDRAND and succeed even if the corresponding CPUID bit is clear.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#11 lore raw
Quoted 10 lines · Borislav Petkov
Quoted 2 lines · On Wed, Sep 02, 2020 at 06:45:38PM +0200, [email protected] wrote:

We really should clear the CPUID bits when the kernel explicitly disables things.

Actually, you want to *disable* the functionality behind it by clearing a bit in CR4 - and yes, not all features have CR4 bits - so that luserspace doesn't "probe" the existence of certain instructions.

Example: you can still try to run RDRAND and succeed even if the corresponding CPUID bit is clear.

Well yes, but as you say, we don't have that :/ Clearing it in CPUID is the best we can do.