什么是CPUID标准函数01H?

What is the CPUID standard function 01H?

我需要检查 ECX 位 30,这是 RDRAND 所必需的 CPU 功能。来自 RDRAND Wiki

If supported, bit 30 of the ECX register is set after calling CPUID standard function 01H.

我不太明白这是什么意思。 "Standard function 01H"?

这是否意味着 EAX=80000001h?我不太确定如何进行。

我认为是调用cpuid函数时%eax应该为1。硬件专家有奇怪的约定,所以他们说 01H 而不是 1 或 0x1。

参见 intel manual 第 2 卷第 3 章。Table 3.8。 eax 的初始值为 01H。 return 上的 ECX 充满了 table 3.10 中的功能信息。在第 30 位 table 的底部,它表示值为 1 表示处理器支持 RDRAND。

如果你有 gcc,你可以尝试这样的操作:

evaitl@bb ~/se $ cat foo.c 
#include <stdio.h>
#include <cpuid.h>
int main(){
    unsigned eax=0, ebx=0, ecx=0, edx=0;
    unsigned max_lvl=__get_cpuid_max(0,&ebx);
    printf("Max level: %d sig %x\n",max_lvl,ebx);
    ebx=0;
    for(int lvl=0; lvl<=max_lvl; ++lvl){
        __get_cpuid(lvl,&eax, &ebx, &ecx, &edx);
        printf("lvl %-2d eax %08x ebx %08x ecx %08x edx %08x\n",
               lvl, eax, ebx, ecx, edx);
        eax=ebx=ecx=edx=0;
    }
    return 0;
}
evaitl@bb ~/se $ ./foo 
Max level: 13 sig 756e6547
lvl 0  eax 0000000d ebx 756e6547 ecx 6c65746e edx 49656e69
lvl 1  eax 000306e4 ebx 07200800 ecx 7fbee3bf edx bfebfbff
lvl 2  eax 76036301 ebx 00f0b2ff ecx 00000000 edx 00ca0000
lvl 3  eax 00000000 ebx 00000000 ecx 00000000 edx 00000000
lvl 4  eax 00000000 ebx 00000000 ecx 00000000 edx 00000000
lvl 5  eax 00000040 ebx 00000040 ecx 00000003 edx 00001120
lvl 6  eax 00000077 ebx 00000002 ecx 00000009 edx 00000000
lvl 7  eax 00000000 ebx 00000000 ecx 00000000 edx 00000000
lvl 8  eax 00000000 ebx 00000000 ecx 00000000 edx 00000000
lvl 9  eax 00000001 ebx 00000000 ecx 00000000 edx 00000000
lvl 10 eax 07300403 ebx 00000000 ecx 00000000 edx 00000603
lvl 11 eax 00000000 ebx 00000000 ecx 0000006e edx 00000007
lvl 12 eax 00000000 ebx 00000000 ecx 00000000 edx 00000000
lvl 13 eax 00000000 ebx 00000000 ecx 00000000 edx 00000000

解码所有内容需要相当多的时间,但 中的定义可以提供帮助:

/* %ecx */
#define bit_SSE3    (1 << 0)
#define bit_PCLMUL  (1 << 1)
#define bit_LZCNT   (1 << 5)
#define bit_SSSE3   (1 << 9)
#define bit_FMA     (1 << 12)
#define bit_CMPXCHG16B  (1 << 13)
#define bit_SSE4_1  (1 << 19)
#define bit_SSE4_2  (1 << 20)
#define bit_MOVBE   (1 << 22)
#define bit_POPCNT  (1 << 23)
#define bit_AES     (1 << 25)
#define bit_XSAVE   (1 << 26)
#define bit_OSXSAVE (1 << 27)
#define bit_AVX     (1 << 28)
#define bit_F16C    (1 << 29)
#define bit_RDRND   (1 << 30)

头文件中还有一堆。您会注意到我的 ecx (7fbee3bf) 的第 30 位设置为级别 1,因此我有可用的 RDRND 指令。

这条指令实际上可以拉出更多信息。在调用 cpuid 时,存在带有 eax 设置最高位的扩展信息。根据调用 cpuid 时 ecx 的值,许多级别也有不同的 "leafs"。无聊的人可以花一两天的时间编写一些代码来完成所有的特征提取,并让它看起来很漂亮。或者,可以执行 "grep flags /proc/cpuinfo" 并得到如下内容:

flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms xsaveopt dtherm ida arat pln pts