为什么 __get_cpuid return leaf=4 全部为零?

Why does __get_cpuid return all zeros for leaf=4?

我想写一个简单的程序调用__get_cpuid来获取缓存信息:

#include <cpuid.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int leaf = atoi(argv[1]);

    uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;

    if (__get_cpuid(leaf, &eax, &ebx, &ecx, &edx))
    {
        printf("leaf=%d, eax=0x%x, ebx=0x%x, ecx=0x%x, edx=0x%x\n",
                leaf, eax, ebx, ecx, edx);
    }
    return 0;
}

首先,我将 leaf 作为 2 传递:

$ ./a.out 2
leaf=2, eax=0x76035a01, ebx=0xf0b2ff, ecx=0x0, edx=0xca0000

由于ebx中有0xff,这意味着我可以从leaf=4中获取缓存信息(参考here):

$ ./a.out 4
leaf=4, eax=0x0, ebx=0x0, ecx=0x0, edx=0x0

但是这次,所有 return 的值都是 0。为什么我无法从 __get_cpuid 获取有效信息?

查看 EAX=4 的链接参考,我们发现 ECX 需要设置为 "cache level to query (e.g. 0=L1D, 1=L2, or 0=L1D, 1=L1I, 2=L2)"。

我无法快速找到关于 __get_cpuid 的任何文档,但搜索确实找到了 soure code,我注意到您需要调用 __get_cpuid_count 才能获得 ecx 在调用 cpuid 之前设置(否则你会得到随机答案——看起来大部分是 0)。