Skip to contentSkip to conversation

cpu-= command line parmeter, SYS_cpuid sys call, kernel-adjusted CPUID

PATCH Superseded Piotr Henryk Dabrowski 3 participants 10 messages Mar 10, 2016 – Mar 14, 2016

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpu-= command line parmeter, SYS_cpuid sys call, kernel-adjusted CPUID

Currently there is no way of disabling CPU features reported by the CPUID instruction. Which sometimes turn out to be broken [1] or undesired [2]. We can assume we will run into similar situations again sooner or later. The only way to fix this is to do a microcode update (if it is available), as the BIOS does not provide a way to disable CPUID bits either. When there is no new microcode, then there is no way to tell your system not to use certain CPU features. This sometimes leads to an unbootable and/or unusable system. Plus the ability to quickly disable certain CPU extensions would be handy for debugging.

This patch aims at providing system-wide support for the kernel-adjusted CPUID:
* The kernel takes a command line parameter (cpu-=...) allowing for an easy way to disable any of the known CPUID capability bits [3]. Plus the kernel may disable certain features by itself as well.
* Then the kernel provides a system call for obtaining the adjusted data [4] (SYS_cpuid, to be used instead of the __cpuid* macros from GCC's cpuid.h).

Since the cpuid instruction is available from the user-space, use of SYS_cpuid cannot be enforced on programmers. But it can be encouraged. And having a possibility to have it supported in glibc is a good start [5]. The expected impact is, after the new versions of kernel and glibc are widely adopted, to discourage use of low-level __cpuid* macros for checking supported CPU features on Linux as a coding issue that workarounds and breaks system features.
And we may expect users to report bugs for programs that do not respect CPU flags being disabled. Especially that they will be trivial to fix. It will take time, but if this is introduced now, it may become a widely used solution in a few years that will finally allow us to easily disable unwanted CPU features on demand.

Old 'no*' command line parameters are obsoleted by this patch, as the cpu-= syntax provides the same functionality in a more flexible and generic way.

*Replacing the syscall with vsyscall/vdso could be considered for this patch.*

This thread is a follow-up of a previous short discussion on this topic [6]:

Quoted 2 lines

[...] ask the kernel instead and that is a problem because older kernels won't have the newer features enabled in, say /proc/cpuinfo [...]

Hence the syscall, which will nicely fallback for older kernels with a non-zero return value.
Ideally, there should be a header file that does this. But having this syscall provides at least a posibility for the userland to painlessly replace the native __cpuid calls within their code. It even has the same syntax and parameters' types as the __cpuid_count macro (see test program below).
Reading the /proc/cpuinfo would be a nightmare. While syscall is probably the fastest, most low-level and fallback-enabled solution possible. If it is acceptable for glibc, then I guess it is good enough for all the programs above.

On GitLab you can find trees with both this patch [7][8] and the modified latest version of glibc [9]. And I attach a test program for the SYS_cpuid below [10].

This is my very first patch for the kernel, so please let me know of any code quality issues or improvement suggestions.

[1]  https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=800574
[2]  https://devtalk.nvidia.com/default/topic/893325/newest-and-beta-linux-driver-causing-segmentation-fault-core-dumped-on-all-skylake-platforms/
[3]  e.g. 'linux ... nosplash quiet cpu-=mmx,sse,sse2'
[4]  long sys_cpuid(const u32 level, const u32 count,
                    u32 __user *eax, u32 __user *ebx,
                    u32 __user *ecx, u32 __user *edx);
[5]  https://sourceware.org/ml/libc-alpha/2016-03/msg00260.html
[6]  https://lkml.org/lkml/2016/1/4/720
[7]  https://gitlab.com/ultr/linux/tags/ultr-sys_cpuid-master
[8]  https://gitlab.com/ultr/linux/tags/ultr-sys_cpuid-next
[9]  https://gitlab.com/ultr/glibc/tags/ultr-sys_cpuid-v2

[10] SYS_cpuid test program:
- - - - cut here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #include <stdio.h>
#include <stdint.h>

#include <unistd.h>
#include <sys/syscall.h>

#include <cpuid.h>

#ifndef __linux__
#warning Not a Linux!
#endif

#ifndef SYS_cpuid
    #warning Defining undefined SYS_cpuid!
    #ifdef __x86_64__
        #define SYS_cpuid 328
    #else
        #define SYS_cpuid 379
    #endif

#endif

void get_kernel(const uint32_t level, const uint32_t count) {
    uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
    int ret = syscall(SYS_cpuid, level, count, &eax, &ebx, &ecx, &edx);
    printf("sys_cpuid==%d: [0x%08lX,%lu] => [0x%08lX,0x%08lX,0x%08lX,0x%08lX]\n", ret, level, count, eax, ebx, ecx, edx);
}
void get_native(const uint32_t level, const uint32_t count) {
    register uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
    __cpuid_count(level, count, eax, ebx, ecx, edx);
    printf("native cpuid: [0x%08lX,%lu] => [0x%08lX,0x%08lX,0x%08lX,0x%08lX]\n", level, count, eax, ebx, ecx, edx);
}
void get(const uint32_t level, const uint32_t count) {
    get_native(level, count);
    get_kernel(level, count);
}
int main(int argc, char **argv) {
    printf("SYS_cpuid = %d\n", SYS_cpuid);
    get(0x00000001, 0);
    get(0x00000006, 0);
    get(0x00000007, 0);
    get(0x0000000D, 1);
    get(0x0000000F, 0);
    get(0x0000000F, 1);
    get(0x80000001, 0);
    get(0x80000008, 0);
    get(0x8000000A, 0);
    get(0x80860001, 0);
    get(0xC0000001, 0);

    get(0x00000002, 0);
    get(0x00000004, 0);
    get(0x00000004, 1);
    get(0x00000004, 2);
    get(0x00000004, 3);
    return 0;
}

- - - - cut here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Regards,
Piotr Henryk Dabrowski

--

[PATCH] kernel-adjusted cpuid

* cpu-= command line parmeter
* SYS_cpuid(level,count,eax,ebx,ecx,edx) syscall
* kernel-adjusted cpuid functions

---
 Documentation/kernel-parameters.txt    |  29 ++++--
 arch/x86/entry/syscalls/syscall_32.tbl |   1 +
 arch/x86/entry/syscalls/syscall_64.tbl |   1 +
 arch/x86/include/asm/cpufeature.h      |  29 +++---
 arch/x86/include/asm/processor.h       | 108 ++++++++++++++++++--
 arch/x86/include/asm/syscalls.h        |   6 ++
 arch/x86/kernel/cpu/centaur.c          |   3 +-
 arch/x86/kernel/cpu/common.c           | 175 ++++++++++++++++++++++++++++-----
 arch/x86/kernel/cpu/transmeta.c        |   9 +-
 9 files changed, 305 insertions(+), 56 deletions(-)
Documentation/kernel-parameters.txt +23 -6
index 1e58ae9..a681206 100644
@@ -675,7 +675,7 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			[SPARC64] tick
 			[X86-64] hpet,tsc
 
-	clearcpuid=BITNUM [X86]
+	clearcpuid=BITNUM [X86] Deprecated. Use cpu-= instead.
 			Disable CPUID feature X for the kernel. See
 			arch/x86/include/asm/cpufeatures.h for the valid bit
 			numbers. Note the Linux specific bits are not necessarily
@@ -776,6 +776,20 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			/proc/<pid>/coredump_filter.
 			See also Documentation/filesystems/proc.txt.
 
+	cpu-= 		[X86]
+			Comma-separated list of CPU features (flags) to
+			forcefully ignore.
+			See /proc/cpuinfo for the list of supported flags in
+			your CPU. Their names are case insensitive. Numeric
+			values for flags' bits can also be used, see
+			arch/x86/include/asm/cpufeature.h for the list.
+			You can query the kernel-adjusted CPUID with SYS_cpuid
+			sys-call and it is recommended to do so in your code.
+			Calling CPUID directly provides original hardware CPU
+			features.
+			Note the kernel might malfunction if you disable some
+			critical features.
+
 	cpuidle.off=1	[CPU_IDLE]
 			disable the cpuidle sub-system
 
@@ -983,7 +997,8 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			Enable debug messages at boot time.  See
 			Documentation/dynamic-debug-howto.txt for details.
 
-	nompx		[X86] Disables Intel Memory Protection Extensions.
+	nompx		[X86] Deprecated. Use cpu-=mpx instead.
+			Disables Intel Memory Protection Extensions.
 			See Documentation/x86/intel_mpx.txt for more
 			information about the feature.
 
@@ -2498,7 +2513,8 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 
 	nocache		[ARM]
 
-	noclflush	[BUGS=X86] Don't use the CLFLUSH instruction
+	noclflush	[BUGS=X86] Deprecated. Use cpu-=clflush instead.
+			Don't use the CLFLUSH instruction
 
 	nodelayacct	[KNL] Disable per-task delay accounting
 
@@ -2515,11 +2531,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			noexec=on: enable non-executable mappings (default)
 			noexec=off: disable non-executable mappings
 
-	nosmap		[X86]
+	nosmap		[X86] Deprecated. Use cpu-=smap instead.
 			Disable SMAP (Supervisor Mode Access Prevention)
 			even if it is supported by processor.
 
-	nosmep		[X86]
+	nosmep		[X86] Deprecated. Use cpu-=smep instead.
 			Disable SMEP (Supervisor Mode Execution Prevention)
 			even if it is supported by processor.
 
@@ -2663,7 +2679,8 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 
 	nosbagart	[IA-64]
 
-	nosep		[BUGS=X86-32] Disables x86 SYSENTER/SYSEXIT support.
+	nosep		[BUGS=X86-32] Deprecated. Use cpu-=sep instead.
+			Disables x86 SYSENTER/SYSEXIT support.
 
 	nosmp		[SMP] Tells an SMP kernel to act as a UP kernel,
 			and disable the IO APIC.  legacy for "maxcpus=0".
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index f65e418..98f6145 100644
@@ -385,3 +385,4 @@
 376	i386	mlock2			sys_mlock2
 377	i386	copy_file_range		sys_copy_file_range
 378	i386	madvisev		sys_madvisev			compat_sys_madvisev
+379	i386	cpuid			sys_cpuid
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index b89b41e..dfc9f48 100644
@@ -334,6 +334,7 @@
 325	common	mlock2			sys_mlock2
 326	common	copy_file_range		sys_copy_file_range
 327	64	madvisev		sys_madvisev
+328	common	cpuid			sys_cpuid
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
arch/x86/include/asm/cpufeature.h +15 -14
index 50e292a..95ccdaf 100644
@@ -10,24 +10,25 @@
 
 enum cpuid_leafs
 {
-	CPUID_1_EDX		= 0,
-	CPUID_8000_0001_EDX,
-	CPUID_8086_0001_EDX,
+	CPUID_00000001_0_EDX	= 0,
+	CPUID_80000001_0_EDX,
+	CPUID_80860001_0_EDX,
 	CPUID_LNX_1,
-	CPUID_1_ECX,
-	CPUID_C000_0001_EDX,
-	CPUID_8000_0001_ECX,
+	CPUID_00000001_0_ECX,
+	CPUID_C0000001_0_EDX,
+	CPUID_80000001_0_ECX,
 	CPUID_LNX_2,
 	CPUID_LNX_3,
-	CPUID_7_0_EBX,
-	CPUID_D_1_EAX,
-	CPUID_F_0_EDX,
-	CPUID_F_1_EDX,
-	CPUID_8000_0008_EBX,
-	CPUID_6_EAX,
-	CPUID_8000_000A_EDX,
-	CPUID_7_ECX,
+	CPUID_00000007_0_EBX,
+	CPUID_0000000D_1_EAX,
+	CPUID_0000000F_0_EDX,
+	CPUID_0000000F_1_EDX,
+	CPUID_80000008_0_EBX,
+	CPUID_00000006_0_EAX,
+	CPUID_8000000A_0_EDX,
+	CPUID_00000007_0_ECX,
 };
+#define CPUID_LEAFS_COUNT 17
 
 #ifdef CONFIG_X86_FEATURE_NAMES
 extern const char * const x86_cap_flags[NCAPINTS*32];
arch/x86/include/asm/processor.h +101 -7

149 lines not on this page · raw message

arch/x86/include/asm/syscalls.h +6 -0
index 91dfcaf..bb4aa14 100644
@@ -2,6 +2,7 @@
  * syscalls.h - Linux syscall interfaces (arch-specific)
  *
  * Copyright (c) 2008 Jaswinder Singh Rajput
+ * Copyright (c) 2016 Piotr Henryk Dabrowski <[email protected]>
  *
  * This file is released under the GPLv2.
  * See the file COPYING for more details.
@@ -30,6 +31,11 @@ asmlinkage long sys_rt_sigreturn(void);
 asmlinkage long sys_set_thread_area(struct user_desc __user *);
 asmlinkage long sys_get_thread_area(struct user_desc __user *);
 
+/* kernel/common.c */
+asmlinkage long sys_cpuid(const u32 level, const u32 count,
+			  u32 __user *eax, u32 __user *ebx,
+			  u32 __user *ecx, u32 __user *edx);
+
 /* X86_32 only */
 #ifdef CONFIG_X86_32
 
arch/x86/kernel/cpu/centaur.c +2 -1
index 1661d8e..593e58d 100644
@@ -43,7 +43,8 @@ static void init_c3(struct cpuinfo_x86 *c)
 		/* store Centaur Extended Feature Flags as
 		 * word 5 of the CPU capability bit array
 		 */
-		c->x86_capability[CPUID_C000_0001_EDX] = cpuid_edx(0xC0000001);
+		c->x86_capability[CPUID_C0000001_0_EDX] = cpuid_edx(0xC0000001);
+		cpuid_overrides[CPUID_C0000001_0_EDX] = &(c->x86_capability[CPUID_C0000001_0_EDX]);
 	}
 #ifdef CONFIG_X86_32
 	/* Cyrix III family needs CX8 & PGE explicitly enabled. */
arch/x86/kernel/cpu/common.c +150 -25

312 lines not on this page · raw message

arch/x86/kernel/cpu/transmeta.c +6 -3
index 3417856..0f46bc8 100644
@@ -11,8 +11,10 @@ static void early_init_transmeta(struct cpuinfo_x86 *c)
 	/* Transmeta-defined flags: level 0x80860001 */
 	xlvl = cpuid_eax(0x80860000);
 	if ((xlvl & 0xffff0000) == 0x80860000) {
-		if (xlvl >= 0x80860001)
-			c->x86_capability[CPUID_8086_0001_EDX] = cpuid_edx(0x80860001);
+		if (xlvl >= 0x80860001) {
+			c->x86_capability[CPUID_80860001_0_EDX] = cpuid_edx(0x80860001);
+			cpuid_overrides[CPUID_80860001_0_EDX] = &(c->x86_capability[CPUID_80860001_0_EDX]);
+		}
 	}
 }
 
@@ -82,7 +84,8 @@ static void init_transmeta(struct cpuinfo_x86 *c)
 	/* Unhide possibly hidden capability flags */
 	rdmsr(0x80860004, cap_mask, uk);
 	wrmsr(0x80860004, ~0, uk);
-	c->x86_capability[CPUID_1_EDX] = cpuid_edx(0x00000001);
+	c->x86_capability[CPUID_00000001_0_EDX] = cpuid_edx(0x00000001);
+	cpuid_overrides[CPUID_00000001_0_EDX] = &(c->x86_capability[CPUID_00000001_0_EDX]);
 	wrmsr(0x80860004, cap_mask, uk);
 
 	/* All Transmeta CPUs have a constant TSC */
Signature · 1 line
2.1.4
#1 lore raw
reproduce:

Hi Piotr,

[auto build test ERROR on next-20160310]
[cannot apply to tip/x86/core v4.5-rc7 v4.5-rc6 v4.5-rc5 v4.5-rc7]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Piotr-Henryk-Dabrowski/cpu-command-line-parmeter-SYS_cpuid-sys-call-kernel-adjusted-CPUID/20160311-043022
config: i386-tinyconfig (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

In file included from arch/x86/include/asm/cpufeature.h:4:0,

                    from arch/x86/include/asm/thread_info.h:52,
                    from include/linux/thread_info.h:54,
                    from arch/x86/include/asm/preempt.h:6,
                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/mmzone.h:7,
                    from include/linux/gfp.h:5,
                    from include/linux/slab.h:14,
                    from include/linux/crypto.h:24,
                    from arch/x86/kernel/asm-offsets.c:8:
>> arch/x86/include/asm/processor.h:162:33: error: 'CPUID_LEAFS_COUNT' undeclared here (not in a function)
    extern __u32   *cpuid_overrides[CPUID_LEAFS_COUNT];
                                    ^
   arch/x86/include/asm/processor.h: In function '__kernel_cpuid':
>> arch/x86/include/asm/processor.h:499:26: error: 'CPUID_00000001_0_ECX' undeclared (first use in this function)
      oecx = cpuid_overrides[CPUID_00000001_0_ECX];
                             ^
   arch/x86/include/asm/processor.h:499:26: note: each undeclared identifier is reported only once for each function it appears in
>> arch/x86/include/asm/processor.h:500:26: error: 'CPUID_00000001_0_EDX' undeclared (first use in this function)
      oedx = cpuid_overrides[CPUID_00000001_0_EDX];
                             ^
>> arch/x86/include/asm/processor.h:502:26: error: 'CPUID_00000006_0_EAX' undeclared (first use in this function)
      oeax = cpuid_overrides[CPUID_00000006_0_EAX];
                             ^
>> arch/x86/include/asm/processor.h:504:26: error: 'CPUID_00000007_0_EBX' undeclared (first use in this function)
      oebx = cpuid_overrides[CPUID_00000007_0_EBX];
                             ^
>> arch/x86/include/asm/processor.h:506:26: error: 'CPUID_0000000D_1_EAX' undeclared (first use in this function)
      oeax = cpuid_overrides[CPUID_0000000D_1_EAX];
                             ^
>> arch/x86/include/asm/processor.h:508:26: error: 'CPUID_0000000F_0_EDX' undeclared (first use in this function)
      oedx = cpuid_overrides[CPUID_0000000F_0_EDX];
                             ^
>> arch/x86/include/asm/processor.h:510:26: error: 'CPUID_0000000F_1_EDX' undeclared (first use in this function)
      oedx = cpuid_overrides[CPUID_0000000F_1_EDX];
                             ^
>> arch/x86/include/asm/processor.h:512:26: error: 'CPUID_80000001_0_ECX' undeclared (first use in this function)
      oecx = cpuid_overrides[CPUID_80000001_0_ECX];
                             ^
>> arch/x86/include/asm/processor.h:513:26: error: 'CPUID_80000001_0_EDX' undeclared (first use in this function)
      oedx = cpuid_overrides[CPUID_80000001_0_EDX];
                             ^
>> arch/x86/include/asm/processor.h:515:26: error: 'CPUID_80000008_0_EBX' undeclared (first use in this function)
      oebx = cpuid_overrides[CPUID_80000008_0_EBX];
                             ^
>> arch/x86/include/asm/processor.h:517:26: error: 'CPUID_8000000A_0_EDX' undeclared (first use in this function)
      oedx = cpuid_overrides[CPUID_8000000A_0_EDX];
                             ^
>> arch/x86/include/asm/processor.h:519:26: error: 'CPUID_80860001_0_EDX' undeclared (first use in this function)
      oedx = cpuid_overrides[CPUID_80860001_0_EDX];
                             ^
>> arch/x86/include/asm/processor.h:521:26: error: 'CPUID_C0000001_0_EDX' undeclared (first use in this function)
      oedx = cpuid_overrides[CPUID_C0000001_0_EDX];
                             ^

make[2]: *** [arch/x86/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors. make[1]: *** [prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [sub-make] Error 2

vim +/CPUID_LEAFS_COUNT +162 arch/x86/include/asm/processor.h

156 extern struct cpuinfo_x86 boot_cpu_data;
157 extern struct cpuinfo_x86 new_cpu_data;
158
159 extern struct tss_struct doublefault_tss;
160 extern __u32 cpu_caps_cleared[NCAPINTS];
161 extern __u32 cpu_caps_set[NCAPINTS];

 > 162	extern __u32			*cpuid_overrides[CPUID_LEAFS_COUNT];

163
164 #ifdef CONFIG_SMP
165 DECLARE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#2 lore raw
In reply to Piotr Henryk Dabrowski
reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…

Hi Piotr,

[auto build test WARNING on next-20160310]
[cannot apply to tip/x86/core v4.5-rc7 v4.5-rc6 v4.5-rc5 v4.5-rc7]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Piotr-Henryk-Dabrowski/cpu-command-line-parmeter-SYS_cpuid-sys-call-kernel-adjusted-CPUID/20160311-043022
config: xtensa-allyesconfig (attached as .config)

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=xtensa

All warnings (new ones prefixed by >>):

<stdin>:1298:2: warning: #warning syscall userfaultfd not implemented [-Wcpp] <stdin>:1301:2: warning: #warning syscall membarrier not implemented [-Wcpp] <stdin>:1304:2: warning: #warning syscall mlock2 not implemented [-Wcpp] <stdin>:1307:2: warning: #warning syscall copy_file_range not implemented [-Wcpp] <stdin>:1310:2: warning: #warning syscall madvisev not implemented [-Wcpp]

>> <stdin>:1313:2: warning: #warning syscall cpuid not implemented [-Wcpp]

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#3 lore raw
In reply to Piotr Henryk Dabrowski
reproduce:

Hi Piotr,

[auto build test ERROR on next-20160310]
[cannot apply to tip/x86/core v4.5-rc7 v4.5-rc6 v4.5-rc5 v4.5-rc7]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Piotr-Henryk-Dabrowski/cpu-command-line-parmeter-SYS_cpuid-sys-call-kernel-adjusted-CPUID/20160311-043022
config: um-x86_64_defconfig (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=um SUBARCH=x86_64

All errors (new ones prefixed by >>):

>> arch/x86/um/built-in.o:(.rodata+0xac0): undefined reference to `sys_cpuid'

collect2: error: ld returned 1 exit status

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#4 lore raw

PATCH v2 1/1 cpu-= command line parmeter, SYS_cpuid sys call, kernel-adjusted CPUID

In reply to Piotr Henryk Dabrowski

Currently there is no way of disabling CPU features reported by the CPUID instruction. Which sometimes turn out to be broken [1] or undesired [2]. We can assume we will run into similar situations again sooner or later. The only way to fix this is to do a microcode update (if it is available), as the BIOS does not provide a way to disable CPUID bits either. When there is no new microcode, then there is no way to tell your system not to use certain CPU features. This sometimes leads to an unbootable and/or unusable system. Plus the ability to quickly disable certain CPU extensions would be handy for debugging.

This patch aims at providing system-wide support for the kernel-adjusted CPUID:
* The kernel takes a command line parameter (cpu-=...) allowing for an easy way to disable any of the known CPUID capability bits [3]. Plus the kernel may disable certain features by itself as well.
* Then the kernel provides a system call for obtaining the adjusted data [4] (SYS_cpuid, to be used instead of the __cpuid* macros from GCC's cpuid.h).

Since the cpuid instruction is available from the user-space, use of SYS_cpuid cannot be enforced on programmers. But it can be encouraged. And having a possibility to have it supported in glibc is a good start [5]. The expected impact is, after the new versions of kernel and glibc are widely adopted, to discourage use of low-level __cpuid* macros for checking supported CPU features on Linux as a coding issue that workarounds and breaks system features.
And we may expect users to report bugs for programs that do not respect CPU flags being disabled. Especially that they will be trivial to fix. It will take time, but if this is introduced now, it may become a widely used solution in a few years that will finally allow us to easily disable unwanted CPU features on demand.

Old 'no*' command line parameters are obsoleted by this patch, as the cpu-= syntax provides the same functionality in a more flexible and generic way.

*Replacing the syscall with vsyscall/vdso could be considered for this patch.*

This thread is a follow-up of a previous short discussion on this topic [6]:

Quoted 2 lines

[...] ask the kernel instead and that is a problem because older kernels won't have the newer features enabled in, say /proc/cpuinfo [...]

Hence the syscall, which will nicely fallback for older kernels with a non-zero return value.
Ideally, there should be a header file that does this. But having this syscall provides at least a posibility for the userland to painlessly replace the native __cpuid calls within their code. It even has the same syntax and parameters' types as the __cpuid_count macro (see test program below).
Reading the /proc/cpuinfo would be a nightmare. While syscall is probably the fastest, most low-level and fallback-enabled solution possible. If it is acceptable for glibc, then I guess it is good enough for all the programs above.

On GitLab you can find trees with both this patch [7][8] and the modified latest version of glibc [9]. And I attach a test program for the SYS_cpuid below [10].

This is my very first patch for the kernel, so please let me know of any code quality issues or improvement suggestions.

[1]  https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=800574
[2]  https://devtalk.nvidia.com/default/topic/893325/newest-and-beta-linux-driver-causing-segmentation-fault-core-dumped-on-all-skylake-platforms/
[3]  e.g. 'linux ... nosplash quiet cpu-=mmx,sse,sse2'
[4]  long sys_cpuid(const u32 level, const u32 count,
                    u32 __user *eax, u32 __user *ebx,
                    u32 __user *ecx, u32 __user *edx);
[5]  https://sourceware.org/ml/libc-alpha/2016-03/msg00260.html
[6]  https://lkml.org/lkml/2016/1/4/720
[7]  https://gitlab.com/ultr/linux/tags/ultr-sys_cpuid-master
[8]  https://gitlab.com/ultr/linux/tags/ultr-sys_cpuid-next-v2
[9]  https://gitlab.com/ultr/glibc/tags/ultr-sys_cpuid-v2

[10] SYS_cpuid test program:
- - - - cut here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #include <stdio.h>
#include <stdint.h>

#include <unistd.h>
#include <sys/syscall.h>

#include <cpuid.h>

#ifndef __linux__
#warning Not a Linux!
#endif

#ifndef SYS_cpuid
    #warning Defining undefined SYS_cpuid!
    #ifdef __x86_64__
        #define SYS_cpuid 328
    #else
        #define SYS_cpuid 379
    #endif

#endif

void get_kernel(const uint32_t level, const uint32_t count) {
    uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
    int ret = syscall(SYS_cpuid, level, count, &eax, &ebx, &ecx, &edx);
    printf("sys_cpuid==%d: [0x%08lX,%lu] => [0x%08lX,0x%08lX,0x%08lX,0x%08lX]\n", ret, level, count, eax, ebx, ecx, edx);
}
void get_native(const uint32_t level, const uint32_t count) {
    register uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
    __cpuid_count(level, count, eax, ebx, ecx, edx);
    printf("native cpuid: [0x%08lX,%lu] => [0x%08lX,0x%08lX,0x%08lX,0x%08lX]\n", level, count, eax, ebx, ecx, edx);
}
void get(const uint32_t level, const uint32_t count) {
    get_native(level, count);
    get_kernel(level, count);
}
int main(int argc, char **argv) {
    printf("SYS_cpuid = %d\n", SYS_cpuid);
    get(0x00000001, 0);
    get(0x00000006, 0);
    get(0x00000007, 0);
    get(0x0000000D, 1);
    get(0x0000000F, 0);
    get(0x0000000F, 1);
    get(0x80000001, 0);
    get(0x80000008, 0);
    get(0x8000000A, 0);
    get(0x80860001, 0);
    get(0xC0000001, 0);

    get(0x00000002, 0);
    get(0x00000004, 0);
    get(0x00000004, 1);
    get(0x00000004, 2);
    get(0x00000004, 3);
    return 0;
}

- - - - cut here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Regards,
Piotr Henryk Dabrowski

--

kernel-adjusted cpuid

* cpu-= command line parmeter
* SYS_cpuid(level,count,eax,ebx,ecx,edx) syscall
* kernel-adjusted cpuid functions

---
 Documentation/kernel-parameters.txt    |  29 ++++--
 arch/x86/entry/syscalls/syscall_32.tbl |   1 +
 arch/x86/entry/syscalls/syscall_64.tbl |   1 +
 arch/x86/include/asm/cpufeature.h      |  21 ----
 arch/x86/include/asm/cpuid_leafs.h     |  26 +++++
 arch/x86/include/asm/elf.h             |   2 +-
 arch/x86/include/asm/processor.h       | 110 +++++++++++++++++++--
 arch/x86/include/asm/syscalls.h        |   6 ++
 arch/x86/kernel/cpu/centaur.c          |   3 +-
 arch/x86/kernel/cpu/common.c           | 175 ++++++++++++++++++++++++++++-----
 arch/x86/kernel/cpu/transmeta.c        |   9 +-
 arch/x86/kernel/mpparse.c              |   3 +-
 arch/x86/lguest/boot.c                 |   2 +-
 arch/x86/xen/enlighten.c               |   2 +-
 14 files changed, 323 insertions(+), 67 deletions(-)
 create mode 100644 arch/x86/include/asm/cpuid_leafs.h
Documentation/kernel-parameters.txt +23 -6
index 1e58ae9..a681206 100644
@@ -675,7 +675,7 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			[SPARC64] tick
 			[X86-64] hpet,tsc
 
-	clearcpuid=BITNUM [X86]
+	clearcpuid=BITNUM [X86] Deprecated. Use cpu-= instead.
 			Disable CPUID feature X for the kernel. See
 			arch/x86/include/asm/cpufeatures.h for the valid bit
 			numbers. Note the Linux specific bits are not necessarily
@@ -776,6 +776,20 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			/proc/<pid>/coredump_filter.
 			See also Documentation/filesystems/proc.txt.
 
+	cpu-= 		[X86]
+			Comma-separated list of CPU features (flags) to
+			forcefully ignore.
+			See /proc/cpuinfo for the list of supported flags in
+			your CPU. Their names are case insensitive. Numeric
+			values for flags' bits can also be used, see
+			arch/x86/include/asm/cpufeature.h for the list.
+			You can query the kernel-adjusted CPUID with SYS_cpuid
+			sys-call and it is recommended to do so in your code.
+			Calling CPUID directly provides original hardware CPU
+			features.
+			Note the kernel might malfunction if you disable some
+			critical features.
+
 	cpuidle.off=1	[CPU_IDLE]
 			disable the cpuidle sub-system
 
@@ -983,7 +997,8 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			Enable debug messages at boot time.  See
 			Documentation/dynamic-debug-howto.txt for details.
 
-	nompx		[X86] Disables Intel Memory Protection Extensions.
+	nompx		[X86] Deprecated. Use cpu-=mpx instead.
+			Disables Intel Memory Protection Extensions.
 			See Documentation/x86/intel_mpx.txt for more
 			information about the feature.
 
@@ -2498,7 +2513,8 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 
 	nocache		[ARM]
 
-	noclflush	[BUGS=X86] Don't use the CLFLUSH instruction
+	noclflush	[BUGS=X86] Deprecated. Use cpu-=clflush instead.
+			Don't use the CLFLUSH instruction
 
 	nodelayacct	[KNL] Disable per-task delay accounting
 
@@ -2515,11 +2531,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			noexec=on: enable non-executable mappings (default)
 			noexec=off: disable non-executable mappings
 
-	nosmap		[X86]
+	nosmap		[X86] Deprecated. Use cpu-=smap instead.
 			Disable SMAP (Supervisor Mode Access Prevention)
 			even if it is supported by processor.
 
-	nosmep		[X86]
+	nosmep		[X86] Deprecated. Use cpu-=smep instead.
 			Disable SMEP (Supervisor Mode Execution Prevention)
 			even if it is supported by processor.
 
@@ -2663,7 +2679,8 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 
 	nosbagart	[IA-64]
 
-	nosep		[BUGS=X86-32] Disables x86 SYSENTER/SYSEXIT support.
+	nosep		[BUGS=X86-32] Deprecated. Use cpu-=sep instead.
+			Disables x86 SYSENTER/SYSEXIT support.
 
 	nosmp		[SMP] Tells an SMP kernel to act as a UP kernel,
 			and disable the IO APIC.  legacy for "maxcpus=0".
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index f65e418..98f6145 100644
@@ -385,3 +385,4 @@
 376	i386	mlock2			sys_mlock2
 377	i386	copy_file_range		sys_copy_file_range
 378	i386	madvisev		sys_madvisev			compat_sys_madvisev
+379	i386	cpuid			sys_cpuid
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index b89b41e..dfc9f48 100644
@@ -334,6 +334,7 @@
 325	common	mlock2			sys_mlock2
 326	common	copy_file_range		sys_copy_file_range
 327	64	madvisev		sys_madvisev
+328	common	cpuid			sys_cpuid
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
arch/x86/include/asm/cpufeature.h +0 -21
index 50e292a..67575cb 100644
@@ -8,27 +8,6 @@
 #include <asm/asm.h>
 #include <linux/bitops.h>
 
-enum cpuid_leafs
-{
-	CPUID_1_EDX		= 0,
-	CPUID_8000_0001_EDX,
-	CPUID_8086_0001_EDX,
-	CPUID_LNX_1,
-	CPUID_1_ECX,
-	CPUID_C000_0001_EDX,
-	CPUID_8000_0001_ECX,
-	CPUID_LNX_2,
-	CPUID_LNX_3,
-	CPUID_7_0_EBX,
-	CPUID_D_1_EAX,
-	CPUID_F_0_EDX,
-	CPUID_F_1_EDX,
-	CPUID_8000_0008_EBX,
-	CPUID_6_EAX,
-	CPUID_8000_000A_EDX,
-	CPUID_7_ECX,
-};
-
 #ifdef CONFIG_X86_FEATURE_NAMES
 extern const char * const x86_cap_flags[NCAPINTS*32];
 extern const char * const x86_power_flags[32];
arch/x86/include/asm/cpuid_leafs.h new file +26 -0
new file mode 100644
index 0000000..7e1ae39
@@ -0,0 +1,26 @@
+#ifndef _ASM_X86_CPUID_LEAFS_H
+#define _ASM_X86_CPUID_LEAFS_H
+
+enum cpuid_leafs
+{
+	CPUID_00000001_0_EDX	= 0,
+	CPUID_80000001_0_EDX,
+	CPUID_80860001_0_EDX,
+	CPUID_LNX_1,
+	CPUID_00000001_0_ECX,
+	CPUID_C0000001_0_EDX,
+	CPUID_80000001_0_ECX,
+	CPUID_LNX_2,
+	CPUID_LNX_3,
+	CPUID_00000007_0_EBX,
+	CPUID_0000000D_1_EAX,
+	CPUID_0000000F_0_EDX,
+	CPUID_0000000F_1_EDX,
+	CPUID_80000008_0_EBX,
+	CPUID_00000006_0_EAX,
+	CPUID_8000000A_0_EDX,
+	CPUID_00000007_0_ECX,
+};
+#define CPUID_LEAFS_COUNT 17
+
+#endif /* _ASM_X86_CPUID_LEAFS_H */
arch/x86/include/asm/elf.h +1 -1
index 15340e3..8a043e7 100644
@@ -256,7 +256,7 @@ extern int force_personality32;
    instruction set this CPU supports.  This could be done in user space,
    but it's not easy, and we've already done it here.  */
 
-#define ELF_HWCAP		(boot_cpu_data.x86_capability[CPUID_1_EDX])
+#define ELF_HWCAP	(boot_cpu_data.x86_capability[CPUID_00000001_0_EDX])
 
 /* This yields a string that ld.so will use to load implementation
    specific libraries for optimization.  This is more specific in
arch/x86/include/asm/processor.h +103 -7

157 lines not on this page · raw message

arch/x86/include/asm/syscalls.h +6 -0
index 91dfcaf..bb4aa14 100644
@@ -2,6 +2,7 @@
  * syscalls.h - Linux syscall interfaces (arch-specific)
  *
  * Copyright (c) 2008 Jaswinder Singh Rajput
+ * Copyright (c) 2016 Piotr Henryk Dabrowski <[email protected]>
  *
  * This file is released under the GPLv2.
  * See the file COPYING for more details.
@@ -30,6 +31,11 @@ asmlinkage long sys_rt_sigreturn(void);
 asmlinkage long sys_set_thread_area(struct user_desc __user *);
 asmlinkage long sys_get_thread_area(struct user_desc __user *);
 
+/* kernel/common.c */
+asmlinkage long sys_cpuid(const u32 level, const u32 count,
+			  u32 __user *eax, u32 __user *ebx,
+			  u32 __user *ecx, u32 __user *edx);
+
 /* X86_32 only */
 #ifdef CONFIG_X86_32
 
arch/x86/kernel/cpu/centaur.c +2 -1
index 1661d8e..593e58d 100644
@@ -43,7 +43,8 @@ static void init_c3(struct cpuinfo_x86 *c)
 		/* store Centaur Extended Feature Flags as
 		 * word 5 of the CPU capability bit array
 		 */
-		c->x86_capability[CPUID_C000_0001_EDX] = cpuid_edx(0xC0000001);
+		c->x86_capability[CPUID_C0000001_0_EDX] = cpuid_edx(0xC0000001);
+		cpuid_overrides[CPUID_C0000001_0_EDX] = &(c->x86_capability[CPUID_C0000001_0_EDX]);
 	}
 #ifdef CONFIG_X86_32
 	/* Cyrix III family needs CX8 & PGE explicitly enabled. */
arch/x86/kernel/cpu/common.c +150 -25

312 lines not on this page · raw message

arch/x86/kernel/cpu/transmeta.c +6 -3
index 3417856..0f46bc8 100644
@@ -11,8 +11,10 @@ static void early_init_transmeta(struct cpuinfo_x86 *c)
 	/* Transmeta-defined flags: level 0x80860001 */
 	xlvl = cpuid_eax(0x80860000);
 	if ((xlvl & 0xffff0000) == 0x80860000) {
-		if (xlvl >= 0x80860001)
-			c->x86_capability[CPUID_8086_0001_EDX] = cpuid_edx(0x80860001);
+		if (xlvl >= 0x80860001) {
+			c->x86_capability[CPUID_80860001_0_EDX] = cpuid_edx(0x80860001);
+			cpuid_overrides[CPUID_80860001_0_EDX] = &(c->x86_capability[CPUID_80860001_0_EDX]);
+		}
 	}
 }
 
@@ -82,7 +84,8 @@ static void init_transmeta(struct cpuinfo_x86 *c)
 	/* Unhide possibly hidden capability flags */
 	rdmsr(0x80860004, cap_mask, uk);
 	wrmsr(0x80860004, ~0, uk);
-	c->x86_capability[CPUID_1_EDX] = cpuid_edx(0x00000001);
+	c->x86_capability[CPUID_00000001_0_EDX] = cpuid_edx(0x00000001);
+	cpuid_overrides[CPUID_00000001_0_EDX] = &(c->x86_capability[CPUID_00000001_0_EDX]);
 	wrmsr(0x80860004, cap_mask, uk);
 
 	/* All Transmeta CPUs have a constant TSC */
arch/x86/kernel/mpparse.c +2 -1
index 97340f2..25ef73b 100644
@@ -408,7 +408,8 @@ static inline void __init construct_default_ISA_mptable(int mpc_default_type)
 	processor.cpuflag = CPU_ENABLED;
 	processor.cpufeature = (boot_cpu_data.x86 << 8) |
 	    (boot_cpu_data.x86_model << 4) | boot_cpu_data.x86_mask;
-	processor.featureflag = boot_cpu_data.x86_capability[CPUID_1_EDX];
+	processor.featureflag =
+	    boot_cpu_data.x86_capability[CPUID_00000001_0_EDX];
 	processor.reserved[0] = 0;
 	processor.reserved[1] = 0;
 	for (i = 0; i < 2; i++) {
arch/x86/lguest/boot.c +1 -1
index fd57d3a..8223827 100644
@@ -1529,7 +1529,7 @@ __init void lguest_init(void)
 	 */
 	cpu_detect(&new_cpu_data);
 	/* head.S usually sets up the first capability word, so do it here. */
-	new_cpu_data.x86_capability[CPUID_1_EDX] = cpuid_edx(1);
+	new_cpu_data.x86_capability[CPUID_00000001_0_EDX] = cpuid_edx(1);
 
 	/* Math is always hard! */
 	set_cpu_cap(&new_cpu_data, X86_FEATURE_FPU);
arch/x86/xen/enlighten.c +1 -1
index 2379a5a..ea960dc 100644
@@ -1655,7 +1655,7 @@ asmlinkage __visible void __init xen_start_kernel(void)
 	cpu_detect(&new_cpu_data);
 	set_cpu_cap(&new_cpu_data, X86_FEATURE_FPU);
 	new_cpu_data.wp_works_ok = 1;
-	new_cpu_data.x86_capability[CPUID_1_EDX] = cpuid_edx(1);
+	new_cpu_data.x86_capability[CPUID_00000001_0_EDX] = cpuid_edx(1);
 #endif
 
 	if (xen_start_info->mod_start) {
Signature · 1 line
2.1.4
#5 lore raw
reproduce:

Hi Piotr,

[auto build test ERROR on next-20160310]
[cannot apply to tip/x86/core xen-tip/linux-next v4.5-rc7 v4.5-rc6 v4.5-rc5 v4.5-rc7] [if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Piotr-Henryk-Dabrowski/cpu-command-line-parmeter-SYS_cpuid-sys-call-kernel-adjusted-CPUID/20160311-081546
config: i386-tinyconfig (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

arch/x86/kernel/cpu/common.c: In function 'print_forcefully_ignored_caps':

>> arch/x86/kernel/cpu/common.c:172:8: error: 'x86_cap_flags' undeclared (first use in this function)
       if (x86_cap_flags[bit] != NULL)
           ^
   arch/x86/kernel/cpu/common.c:172:8: note: each undeclared identifier is reported only once for each function it appears in
   arch/x86/kernel/cpu/common.c: In function 'setup_forcefully_ignore_caps':
   arch/x86/kernel/cpu/common.c:224:7: error: 'x86_cap_flags' undeclared (first use in this function)
      if (x86_cap_flags[bit] != NULL) {
          ^

vim +/x86_cap_flags +172 arch/x86/kernel/cpu/common.c

166
167 buf[0] = '\0';
168 for (bit = 0; bit < 32 * NCAPINTS; bit++)
169 {
170 if (test_bit(bit, (unsigned long *)forcefully_ignored_caps)) {
171 int len = strlen(buf);

 > 172				if (x86_cap_flags[bit] != NULL)

173 snprintf(buf + len, sizeof(buf) - len,
174 " %s", x86_cap_flags[bit]);
175 else

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#6 lore raw
In reply to Piotr Henryk Dabrowski
reproduce:

Hi Piotr,

[auto build test WARNING on next-20160310]
[cannot apply to tip/x86/core xen-tip/linux-next v4.5-rc7 v4.5-rc6 v4.5-rc5 v4.5-rc7] [if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Piotr-Henryk-Dabrowski/cpu-command-line-parmeter-SYS_cpuid-sys-call-kernel-adjusted-CPUID/20160311-081546
config: i386-randconfig-s1-201610 (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All warnings (new ones prefixed by >>):

>> WARNING: vmlinux.o(.text+0x3bed7): Section mismatch in reference from the function cpu_init() to the variable .init.data:buf.44557

The function cpu_init() references
the variable __initdata buf.44557.
This is often because cpu_init lacks a __initdata annotation or the annotation of buf.44557 is wrong.

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#7 lore raw

PATCH v3 1/1 cpu-= command line parmeter, SYS_cpuid sys call, kernel-adjusted CPUID

In reply to Piotr Henryk Dabrowski

Currently there is no way of disabling CPU features reported by the CPUID instruction. Which sometimes turn out to be broken [1] or undesired [2]. We can assume we will run into similar situations again sooner or later. The only way to fix this is to do a microcode update (if it is available), as the BIOS does not provide a way to disable CPUID bits either. When there is no new microcode, then there is no way to tell your system not to use certain CPU features. This sometimes leads to an unbootable and/or unusable system. Plus the ability to quickly disable certain CPU extensions would be handy for debugging.

This patch aims at providing system-wide support for the kernel-adjusted CPUID:
* The kernel takes a command line parameter (cpu-=...) allowing for an easy way to disable any of the known CPUID capability bits [3]. Plus the kernel may disable certain features by itself as well.
* Then the kernel provides a system call for obtaining the adjusted data [4] (SYS_cpuid, to be used instead of the __cpuid* macros from GCC's cpuid.h).

Since the cpuid instruction is available from the user-space, use of SYS_cpuid cannot be enforced on programmers. But it can be encouraged. And having a possibility to have it supported in glibc is a good start [5]. The expected impact is, after the new versions of kernel and glibc are widely adopted, to discourage use of low-level __cpuid* macros for checking supported CPU features on Linux as a coding issue that workarounds and breaks system features.
And we may expect users to report bugs for programs that do not respect CPU flags being disabled. Especially that they will be trivial to fix. It will take time, but if this is introduced now, it may become a widely used solution in a few years that will finally allow us to easily disable unwanted CPU features on demand.

Old 'no*' command line parameters are obsoleted by this patch, as the cpu-= syntax provides the same functionality in a more flexible and generic way.

*Replacing the syscall with vsyscall/vdso could be considered for this patch.*

This thread is a follow-up of a previous short discussion on this topic [6]:

Quoted 2 lines

[...] ask the kernel instead and that is a problem because older kernels won't have the newer features enabled in, say /proc/cpuinfo [...]

Hence the syscall, which will nicely fallback for older kernels with a non-zero return value.
Ideally, there should be a header file that does this. But having this syscall provides at least a posibility for the userland to painlessly replace the native __cpuid calls within their code. It even has the same syntax and parameters' types as the __cpuid_count macro (see test program below).
Reading the /proc/cpuinfo would be a nightmare. While syscall is probably the fastest, most low-level and fallback-enabled solution possible. If it is acceptable for glibc, then I guess it is good enough for all the programs above.

On GitLab you can find trees with both this patch [7][8] and the modified latest version of glibc [9]. And I attach a test program for the SYS_cpuid below [10].

This is my very first patch for the kernel, so please let me know of any code quality issues or improvement suggestions.

[1]  https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=800574
[2]  https://devtalk.nvidia.com/default/topic/893325/newest-and-beta-linux-driver-causing-segmentation-fault-core-dumped-on-all-skylake-platforms/
[3]  e.g. 'linux ... nosplash quiet cpu-=mmx,sse,sse2'
[4]  long sys_cpuid(const u32 level, const u32 count,
                    u32 __user *eax, u32 __user *ebx,
                    u32 __user *ecx, u32 __user *edx);
[5]  https://sourceware.org/ml/libc-alpha/2016-03/msg00260.html
[6]  https://lkml.org/lkml/2016/1/4/720
[7]  https://gitlab.com/ultr/linux/tags/ultr-sys_cpuid-master
[8]  https://gitlab.com/ultr/linux/tags/ultr-sys_cpuid-next-v3
[9]  https://gitlab.com/ultr/glibc/tags/ultr-sys_cpuid-v2

[10] SYS_cpuid test program:
- - - - cut here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #include <stdio.h>
#include <stdint.h>

#include <unistd.h>
#include <sys/syscall.h>

#include <cpuid.h>

#ifndef __linux__
#warning Not a Linux!
#endif

#ifndef SYS_cpuid
    #warning Defining undefined SYS_cpuid!
    #ifdef __x86_64__
        #define SYS_cpuid 327
    #else
        #define SYS_cpuid 378
    #endif

#endif

void get_native(const uint32_t level, const uint32_t count) {
    register uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
    __cpuid_count(level, count, eax, ebx, ecx, edx);
    printf("native cpuid:\t[0x%08X,%u] => [0x%08X,0x%08X,0x%08X,0x%08X]\n", level, count, eax, ebx, ecx, edx);
}
void get_kernel(const uint32_t level, const uint32_t count) {
    uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
    int ret = syscall(SYS_cpuid, level, count, &eax, &ebx, &ecx, &edx);
    printf("sys_cpuid==%d:\t[0x%08X,%u] => [0x%08X,0x%08X,0x%08X,0x%08X]\n", ret, level, count, eax, ebx, ecx, edx);
}
void get(const uint32_t level, const uint32_t count) {
    get_native(level, count);
    get_kernel(level, count);
}
int main(int argc, char **argv) {
    printf("SYS_cpuid = %d\n", SYS_cpuid);
    get(0x00000001, 0);
    get(0x00000006, 0);
    get(0x00000007, 0);
    get(0x0000000D, 1);
    get(0x0000000F, 0);
    get(0x0000000F, 1);
    get(0x80000001, 0);
    get(0x80000008, 0);
    get(0x8000000A, 0);
    get(0x80860001, 0);
    get(0xC0000001, 0);

    get(0x00000002, 0);
    get(0x00000004, 0);
    get(0x00000004, 1);
    get(0x00000004, 2);
    get(0x00000004, 3);
    return 0;
}

- - - - cut here - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Regards,
Piotr Henryk Dabrowski

--

kernel-adjusted cpuid

* cpu-= command line parmeter
* SYS_cpuid(level,count,eax,ebx,ecx,edx) syscall
* kernel-adjusted cpuid functions

---
 Documentation/kernel-parameters.txt    |  29 +++--
 arch/x86/entry/syscalls/syscall_32.tbl |   1 +
 arch/x86/entry/syscalls/syscall_64.tbl |   1 +
 arch/x86/include/asm/cpufeature.h      |  21 ----
 arch/x86/include/asm/cpuid_leafs.h     |  26 +++++
 arch/x86/include/asm/elf.h             |   3 +-
 arch/x86/include/asm/processor.h       | 110 +++++++++++++++++--
 arch/x86/include/asm/syscalls.h        |   6 ++
 arch/x86/kernel/cpu/centaur.c          |   4 +-
 arch/x86/kernel/cpu/common.c           | 186 ++++++++++++++++++++++++++++-----
 arch/x86/kernel/cpu/transmeta.c        |  10 +-
 arch/x86/kernel/mpparse.c              |   4 +-
 arch/x86/lguest/boot.c                 |   3 +-
 arch/x86/um/sys_call_table_64.c        |   1 +
 arch/x86/xen/enlighten.c               |   3 +-
 kernel/sys_ni.c                        |   1 +
 16 files changed, 342 insertions(+), 67 deletions(-)
 create mode 100644 arch/x86/include/asm/cpuid_leafs.h
Documentation/kernel-parameters.txt +23 -6
index 1e58ae9..a681206 100644
@@ -675,7 +675,7 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			[SPARC64] tick
 			[X86-64] hpet,tsc
 
-	clearcpuid=BITNUM [X86]
+	clearcpuid=BITNUM [X86] Deprecated. Use cpu-= instead.
 			Disable CPUID feature X for the kernel. See
 			arch/x86/include/asm/cpufeatures.h for the valid bit
 			numbers. Note the Linux specific bits are not necessarily
@@ -776,6 +776,20 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			/proc/<pid>/coredump_filter.
 			See also Documentation/filesystems/proc.txt.
 
+	cpu-= 		[X86]
+			Comma-separated list of CPU features (flags) to
+			forcefully ignore.
+			See /proc/cpuinfo for the list of supported flags in
+			your CPU. Their names are case insensitive. Numeric
+			values for flags' bits can also be used, see
+			arch/x86/include/asm/cpufeature.h for the list.
+			You can query the kernel-adjusted CPUID with SYS_cpuid
+			sys-call and it is recommended to do so in your code.
+			Calling CPUID directly provides original hardware CPU
+			features.
+			Note the kernel might malfunction if you disable some
+			critical features.
+
 	cpuidle.off=1	[CPU_IDLE]
 			disable the cpuidle sub-system
 
@@ -983,7 +997,8 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			Enable debug messages at boot time.  See
 			Documentation/dynamic-debug-howto.txt for details.
 
-	nompx		[X86] Disables Intel Memory Protection Extensions.
+	nompx		[X86] Deprecated. Use cpu-=mpx instead.
+			Disables Intel Memory Protection Extensions.
 			See Documentation/x86/intel_mpx.txt for more
 			information about the feature.
 
@@ -2498,7 +2513,8 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 
 	nocache		[ARM]
 
-	noclflush	[BUGS=X86] Don't use the CLFLUSH instruction
+	noclflush	[BUGS=X86] Deprecated. Use cpu-=clflush instead.
+			Don't use the CLFLUSH instruction
 
 	nodelayacct	[KNL] Disable per-task delay accounting
 
@@ -2515,11 +2531,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			noexec=on: enable non-executable mappings (default)
 			noexec=off: disable non-executable mappings
 
-	nosmap		[X86]
+	nosmap		[X86] Deprecated. Use cpu-=smap instead.
 			Disable SMAP (Supervisor Mode Access Prevention)
 			even if it is supported by processor.
 
-	nosmep		[X86]
+	nosmep		[X86] Deprecated. Use cpu-=smep instead.
 			Disable SMEP (Supervisor Mode Execution Prevention)
 			even if it is supported by processor.
 
@@ -2663,7 +2679,8 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 
 	nosbagart	[IA-64]
 
-	nosep		[BUGS=X86-32] Disables x86 SYSENTER/SYSEXIT support.
+	nosep		[BUGS=X86-32] Deprecated. Use cpu-=sep instead.
+			Disables x86 SYSENTER/SYSEXIT support.
 
 	nosmp		[SMP] Tells an SMP kernel to act as a UP kernel,
 			and disable the IO APIC.  legacy for "maxcpus=0".
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index cb713df..60f2524 100644
@@ -384,3 +384,4 @@
 375	i386	membarrier		sys_membarrier
 376	i386	mlock2			sys_mlock2
 377	i386	copy_file_range		sys_copy_file_range
+378	i386	cpuid			sys_cpuid
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index 2e5b565..0f7ecda 100644
@@ -333,6 +333,7 @@
 324	common	membarrier		sys_membarrier
 325	common	mlock2			sys_mlock2
 326	common	copy_file_range		sys_copy_file_range
+327	common	cpuid			sys_cpuid
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
arch/x86/include/asm/cpufeature.h +0 -21
index 50e292a..67575cb 100644
@@ -8,27 +8,6 @@
 #include <asm/asm.h>
 #include <linux/bitops.h>
 
-enum cpuid_leafs
-{
-	CPUID_1_EDX		= 0,
-	CPUID_8000_0001_EDX,
-	CPUID_8086_0001_EDX,
-	CPUID_LNX_1,
-	CPUID_1_ECX,
-	CPUID_C000_0001_EDX,
-	CPUID_8000_0001_ECX,
-	CPUID_LNX_2,
-	CPUID_LNX_3,
-	CPUID_7_0_EBX,
-	CPUID_D_1_EAX,
-	CPUID_F_0_EDX,
-	CPUID_F_1_EDX,
-	CPUID_8000_0008_EBX,
-	CPUID_6_EAX,
-	CPUID_8000_000A_EDX,
-	CPUID_7_ECX,
-};
-
 #ifdef CONFIG_X86_FEATURE_NAMES
 extern const char * const x86_cap_flags[NCAPINTS*32];
 extern const char * const x86_power_flags[32];
arch/x86/include/asm/cpuid_leafs.h new file +26 -0
new file mode 100644
index 0000000..7e1ae39
@@ -0,0 +1,26 @@
+#ifndef _ASM_X86_CPUID_LEAFS_H
+#define _ASM_X86_CPUID_LEAFS_H
+
+enum cpuid_leafs
+{
+	CPUID_00000001_0_EDX	= 0,
+	CPUID_80000001_0_EDX,
+	CPUID_80860001_0_EDX,
+	CPUID_LNX_1,
+	CPUID_00000001_0_ECX,
+	CPUID_C0000001_0_EDX,
+	CPUID_80000001_0_ECX,
+	CPUID_LNX_2,
+	CPUID_LNX_3,
+	CPUID_00000007_0_EBX,
+	CPUID_0000000D_1_EAX,
+	CPUID_0000000F_0_EDX,
+	CPUID_0000000F_1_EDX,
+	CPUID_80000008_0_EBX,
+	CPUID_00000006_0_EAX,
+	CPUID_8000000A_0_EDX,
+	CPUID_00000007_0_ECX,
+};
+#define CPUID_LEAFS_COUNT 17
+
+#endif /* _ASM_X86_CPUID_LEAFS_H */
arch/x86/include/asm/elf.h +2 -1
index 15340e3..f39b3c7 100644
@@ -9,6 +9,7 @@
 #include <asm/ptrace.h>
 #include <asm/user.h>
 #include <asm/auxvec.h>
+#include <asm/cpuid_leafs.h>
 
 typedef unsigned long elf_greg_t;
 
@@ -256,7 +257,7 @@ extern int force_personality32;
    instruction set this CPU supports.  This could be done in user space,
    but it's not easy, and we've already done it here.  */
 
-#define ELF_HWCAP		(boot_cpu_data.x86_capability[CPUID_1_EDX])
+#define ELF_HWCAP	(boot_cpu_data.x86_capability[CPUID_00000001_0_EDX])
 
 /* This yields a string that ld.so will use to load implementation
    specific libraries for optimization.  This is more specific in
arch/x86/include/asm/processor.h +103 -7

157 lines not on this page · raw message

arch/x86/include/asm/syscalls.h +6 -0
index 91dfcaf..bb4aa14 100644
@@ -2,6 +2,7 @@
  * syscalls.h - Linux syscall interfaces (arch-specific)
  *
  * Copyright (c) 2008 Jaswinder Singh Rajput
+ * Copyright (c) 2016 Piotr Henryk Dabrowski <[email protected]>
  *
  * This file is released under the GPLv2.
  * See the file COPYING for more details.
@@ -30,6 +31,11 @@ asmlinkage long sys_rt_sigreturn(void);
 asmlinkage long sys_set_thread_area(struct user_desc __user *);
 asmlinkage long sys_get_thread_area(struct user_desc __user *);
 
+/* kernel/common.c */
+asmlinkage long sys_cpuid(const u32 level, const u32 count,
+			  u32 __user *eax, u32 __user *ebx,
+			  u32 __user *ecx, u32 __user *edx);
+
 /* X86_32 only */
 #ifdef CONFIG_X86_32
 
arch/x86/kernel/cpu/centaur.c +3 -1
index 1661d8e..977c218 100644
@@ -5,6 +5,7 @@
 #include <asm/e820.h>
 #include <asm/mtrr.h>
 #include <asm/msr.h>
+#include <asm/cpuid_leafs.h>
 
 #include "cpu.h"
 
@@ -43,7 +44,8 @@ static void init_c3(struct cpuinfo_x86 *c)
 		/* store Centaur Extended Feature Flags as
 		 * word 5 of the CPU capability bit array
 		 */
-		c->x86_capability[CPUID_C000_0001_EDX] = cpuid_edx(0xC0000001);
+		c->x86_capability[CPUID_C0000001_0_EDX] = cpuid_edx(0xC0000001);
+		cpuid_overrides[CPUID_C0000001_0_EDX] = &(c->x86_capability[CPUID_C0000001_0_EDX]);
 	}
 #ifdef CONFIG_X86_32
 	/* Cyrix III family needs CX8 & PGE explicitly enabled. */
arch/x86/kernel/cpu/common.c +161 -25

329 lines not on this page · raw message

arch/x86/kernel/cpu/transmeta.c +7 -3
index 3417856..3def5fa 100644
@@ -2,6 +2,7 @@
 #include <linux/mm.h>
 #include <asm/cpufeature.h>
 #include <asm/msr.h>
+#include <asm/cpuid_leafs.h>
 #include "cpu.h"
 
 static void early_init_transmeta(struct cpuinfo_x86 *c)
@@ -11,8 +12,10 @@ static void early_init_transmeta(struct cpuinfo_x86 *c)
 	/* Transmeta-defined flags: level 0x80860001 */
 	xlvl = cpuid_eax(0x80860000);
 	if ((xlvl & 0xffff0000) == 0x80860000) {
-		if (xlvl >= 0x80860001)
-			c->x86_capability[CPUID_8086_0001_EDX] = cpuid_edx(0x80860001);
+		if (xlvl >= 0x80860001) {
+			c->x86_capability[CPUID_80860001_0_EDX] = cpuid_edx(0x80860001);
+			cpuid_overrides[CPUID_80860001_0_EDX] = &(c->x86_capability[CPUID_80860001_0_EDX]);
+		}
 	}
 }
 
@@ -82,7 +85,8 @@ static void init_transmeta(struct cpuinfo_x86 *c)
 	/* Unhide possibly hidden capability flags */
 	rdmsr(0x80860004, cap_mask, uk);
 	wrmsr(0x80860004, ~0, uk);
-	c->x86_capability[CPUID_1_EDX] = cpuid_edx(0x00000001);
+	c->x86_capability[CPUID_00000001_0_EDX] = cpuid_edx(0x00000001);
+	cpuid_overrides[CPUID_00000001_0_EDX] = &(c->x86_capability[CPUID_00000001_0_EDX]);
 	wrmsr(0x80860004, cap_mask, uk);
 
 	/* All Transmeta CPUs have a constant TSC */
arch/x86/kernel/mpparse.c +3 -1
index 97340f2..33e879a 100644
@@ -30,6 +30,7 @@
 #include <asm/e820.h>
 #include <asm/setup.h>
 #include <asm/smp.h>
+#include <asm/cpuid_leafs.h>
 
 #include <asm/apic.h>
 /*
@@ -408,7 +409,8 @@ static inline void __init construct_default_ISA_mptable(int mpc_default_type)
 	processor.cpuflag = CPU_ENABLED;
 	processor.cpufeature = (boot_cpu_data.x86 << 8) |
 	    (boot_cpu_data.x86_model << 4) | boot_cpu_data.x86_mask;
-	processor.featureflag = boot_cpu_data.x86_capability[CPUID_1_EDX];
+	processor.featureflag =
+	    boot_cpu_data.x86_capability[CPUID_00000001_0_EDX];
 	processor.reserved[0] = 0;
 	processor.reserved[1] = 0;
 	for (i = 0; i < 2; i++) {
arch/x86/lguest/boot.c +2 -1
index fd57d3a..b21dd30 100644
@@ -76,6 +76,7 @@
 #include <asm/kvm_para.h>
 #include <asm/pci_x86.h>
 #include <asm/pci-direct.h>
+#include <asm/cpuid_leafs.h>
 
 /*G:010
  * Welcome to the Guest!
@@ -1529,7 +1530,7 @@ __init void lguest_init(void)
 	 */
 	cpu_detect(&new_cpu_data);
 	/* head.S usually sets up the first capability word, so do it here. */
-	new_cpu_data.x86_capability[CPUID_1_EDX] = cpuid_edx(1);
+	new_cpu_data.x86_capability[CPUID_00000001_0_EDX] = cpuid_edx(1);
 
 	/* Math is always hard! */
 	set_cpu_cap(&new_cpu_data, X86_FEATURE_FPU);
arch/x86/um/sys_call_table_64.c +1 -0
index f306413..be43219 100644
@@ -34,6 +34,7 @@
 #define stub_execve sys_execve
 #define stub_execveat sys_execveat
 #define stub_rt_sigreturn sys_rt_sigreturn
+#define stub_cpuid sys_cpuid
 
 #define __SYSCALL_64(nr, sym, qual) extern asmlinkage long sym(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long) ;
 #include <asm/syscalls_64.h>
arch/x86/xen/enlighten.c +2 -1
index 2379a5a..9398164 100644
@@ -77,6 +77,7 @@
 #include <asm/pci_x86.h>
 #include <asm/pat.h>
 #include <asm/cpu.h>
+#include <asm/cpuid_leafs.h>
 
 #ifdef CONFIG_ACPI
 #include <linux/acpi.h>
@@ -1655,7 +1656,7 @@ asmlinkage __visible void __init xen_start_kernel(void)
 	cpu_detect(&new_cpu_data);
 	set_cpu_cap(&new_cpu_data, X86_FEATURE_FPU);
 	new_cpu_data.wp_works_ok = 1;
-	new_cpu_data.x86_capability[CPUID_1_EDX] = cpuid_edx(1);
+	new_cpu_data.x86_capability[CPUID_00000001_0_EDX] = cpuid_edx(1);
 #endif
 
 	if (xen_start_info->mod_start) {
kernel/sys_ni.c +1 -0
index 2c5e3a8..ef22bea 100644
@@ -187,6 +187,7 @@ cond_syscall(sys_spu_create);
 cond_syscall(sys_subpage_prot);
 cond_syscall(sys_s390_pci_mmio_read);
 cond_syscall(sys_s390_pci_mmio_write);
+cond_syscall(sys_cpuid);
 
 /* mmu depending weak syscall entries */
 cond_syscall(sys_mprotect);
Signature · 1 line
2.1.4
#8 lore raw
Quoted 2 lines · Piotr Henryk Dabrowski

Currently there is no way of disabling CPU features reported by the CPUID instruction. Which sometimes turn out to be broken [1] or undesired [2].

...

Quoted 5 lines

* The kernel takes a command line parameter (cpu-=...) allowing for an easy way to disable any of the known CPUID capability bits [3]. Plus the kernel may disable certain features by itself as well.
* Then the kernel provides a system call for obtaining the adjusted data [4] (SYS_cpuid, to be used instead of the __cpuid* macros from GCC's cpuid.h).

Wouldn't it be better to (finally) extend the AT_HWCAP ELF stuff properly on x86 for the missing cpuid levels? Basically, get every cpuid leaf that contributes to the /proc/cpuinfo "flags" field into new AT_HWCAPx ELF fields? Some arches already have AT_HWCAP2, for example. x86 would need more than just AT_HWCAP2, though.

https://lwn.net/Articles/519085/
http://man7.org/linux/man-pages/man3/getauxval.3.html

AT_HWCAP is not only useful for LDSO tricks to load flag-optimized versions of libraries, it is directly accessible to the process, so it could also be used as an alternate source of cpuid() information that the kernel can modify through quirks.

Quoted 2 lines

Since the cpuid instruction is available from the user-space, use of SYS_cpuid cannot be enforced on programmers. But it can be encouraged. And having a

Indeed. Well, we already have it, but it is stuck in the past and gathering cowebs.

Signature · 4 lines
  "One disk to rule them all, One disk to find them. One disk to bring
  them all and in the darkness grind them. In the Land of Redmond
  where the shadows lie." -- The Silicon Valley Tarot
  Henrique Holschuh
#9 lore raw
Quoted 5 lines · Henrique de Moraes Holschuh

Wouldn't it be better to (finally) extend the AT_HWCAP ELF stuff properly on x86 for the missing cpuid levels? Basically, get every cpuid leaf that contributes to the /proc/cpuinfo "flags" field into new AT_HWCAPx ELF fields? Some arches already have AT_HWCAP2, for example. x86 would need more than just AT_HWCAP2, though.

man 3 getauxval: "The contents of the bit mask are hardware dependent (for example, see the kernel source file arch/x86/include/asm/cpufeature.h for details relating to the Intel x86 architecture)." [1]

IMHO this doesn't seem like a solution anyone in the userland would like to port their __cpuid based code to.

1. Bits in the kernel are totally incompatible with those returned by __cpuid. So for a portable application this requires including yet another header file for the new set of bits. Which comes with kernel headers only, resulting in people probably hard-copying it into their source code instead of requiring this dependency.

2. Developers will need to add a totally different implementation alongside with the fallback __cpuid call into their programs. And, as mentioned above, test the bits separately as well.
Meanwhile the cpuid syscall/vdso the way it is proposed (having identical syntax) requires just one additional condition before fallback __cpuid [2] (plus a simple #ifndef to support non-Linux OSes). The entire remaining code stays the same.

3. Kernel leafs are arbitrarily chosen and composed into an internal array mixing every possible op/count call and register together [3]. While the __cpuid bits correspond to the output of the CPU hardware that is well documented, kernel bits are just arbitrarily assigned indexes for a private internal array in the kernel source. Even the kernel uses macros to check/disable specific features to hide this fact.
IMO this array should never ever be exposed outside of the kernel source, at least the way it is now.

If we want this feature to be respected and incorporated into every program, then I think we should really make sure it is easy to switch to.

Quoted 4 lines

AT_HWCAP is not only useful for LDSO tricks to load flag-optimized versions of libraries, it is directly accessible to the process, so it could also be used as an alternate source of cpuid() information that the kernel can modify through quirks.

Instead/alongside the syscall a vdso function can be implemented resulting in exactly the same benefit.

And if we are thinking of ignoring the __cpuid way and creating a completely new and portable API/ABI, then something better than constantly expanding AT_HWCAPx internal arrays is needed.
Maybe a fast vdso function for checking for CPU features by their names, like:

    int __vdso_cpu_has_feature(char *feature_name);

Something that encourages programmers to use it instead of scaring them away.

Quoted 2 lines

Indeed. Well, we already have it, but it is stuck in the past and gathering cowebs.

Still a cpu-= equivalent in the command line is required to let users disable all the features they do not want to be reported.

[1] http://man7.org/linux/man-pages/man3/getauxval.3.html
[2]

    unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
    if (syscall(SYS_cpuid, level, count, &eax, &ebx, &ecx, &edx) != 0)
        __cpuid_count(level, count, eax, ebx, ecx, edx);
[3]
    enum cpuid_leafs
    {
        CPUID_00000001_0_EDX	= 0,
        CPUID_80000001_0_EDX,
        CPUID_80860001_0_EDX,
        CPUID_LNX_1,
        CPUID_00000001_0_ECX,
        CPUID_C0000001_0_EDX,
        CPUID_80000001_0_ECX,
        CPUID_LNX_2,
        CPUID_LNX_3,
        CPUID_00000007_0_EBX,
        CPUID_0000000D_1_EAX,
        CPUID_0000000F_0_EDX,
        CPUID_0000000F_1_EDX,
        CPUID_80000008_0_EBX,
        CPUID_00000006_0_EAX,
        CPUID_8000000A_0_EDX,
        CPUID_00000007_0_ECX,
    };

Regards,
Piotr Henryk Dabrowski