Re: [RFC PATCH v3] tools/x86: add kcpuid tool to show raw CPU features

From: Borislav Petkov
Date: Thu Nov 19 2020 - 04:18:30 EST


On Thu, Nov 19, 2020 at 03:20:55PM +0800, Feng Tang wrote:
> I just tried the patch on one Debian 9 and 2 Ubuntus (16.04 & 20.10) with
> different gcc versions, and haven't reproduced it yet.

That's because you're testing in VMs and they have CPUID(4) which baremetal -
where I am testing - doesn't and I get

parse_line: entry
parse_line: skip comments
parse_line: index/main leaf, func: 0x5588497d7300
parse_line: sub 0, leafs: (nil)
^^^^^^

for leafs for index 4 and thus this is a NUL ptr. And leaf 4 is the test
range in cpuid.csv.

Which brings me to another important point: setup_platform_cpuid()
should not blindly simply look at the two ranges 0 and 0x8000_0000
but detect whether it is running on baremetal or on a hypervisor and
depending on which, setup the respective ranges.

For example, guests would need to scan 0x4, in addition to the above.
Centaur CPUs have CPUID in the ranges 0xc0000000 and so on.

But you don't have to implement all CPUID leafs and ranges - just your
tool should be careful and first detect on what it runs and then scan
the possible ranges.

And then exit gracefully when it encounters a range which it doesn't
know and not segfault.

There could be another option which, when set, would go and scan the
*whole* 32-bit CPUID range to find whether there are some CPUID ranges
which are not known. Another option because the scan would potentially
take long time and thus off by default. And so on.

Here's the patch I've cleaned up and fixed up so far, please continue
with it.

Thx.

---