1 module libd.util.cpuid;
2 
3 // The actual code for calling cpuid, populating the store, etc. are all automatically generated.
4 // The tools/cpuidgen.d tool uses this file as a model for the generated code.
5 version(DLIB_GenCpuID){}
6 else public import libd.util._cpuid;
7 
8 package struct Bits
9 {
10     uint start;
11     uint length;
12     string name;
13 
14     version(DLIB_GenCpuID)
15     {
16         uint getMask()
17         {
18             uint mask;
19             foreach(i; 0..this.length)
20                 mask |= (1 << (this.start + i));
21             return mask;
22         }
23 
24         uint getRightShift()
25         {
26             return this.start;
27         }
28     }
29 }
30 
31 package struct Bit
32 {
33     uint index;
34     string name;
35 }
36 
37 package struct CpuIdLeaf
38 {
39     uint eax;
40     uint ecx;
41 }
42 
43 package struct Store
44 {
45     char[12] idString;
46     uint     maxLeaf;
47 
48     // EAX=1: Processor Info and Feature Bits
49     @(
50         CpuIdLeaf(1, 0),
51         Bits(0, 4,   "steppingId"),
52         Bits(4, 4,   "model"),
53         Bits(8, 4,   "familyId"),
54         Bits(12, 2,  "processorType"),
55         Bits(16, 4,  "extendedModelId"),
56         Bits(20, 8,  "extendedFamilyId")
57     )
58     uint eax1_eax;
59 
60     @(
61         CpuIdLeaf(1, 0),
62         Bits(0, 8, "brandIndex"),
63         Bits(8, 8, "clflushLineSize"),
64         Bits(16, 8, "maxAddressableIdsForLogicalProcessors"),
65         Bits(24, 8, "localApicId")
66     )
67     uint eax1_ebx;
68 
69     @(
70         CpuIdLeaf(1, 0),
71         Bit(0, "sse3"),             Bit(1, "pclmulqdq"),    Bit(2, "dtest64"),  Bit(3, "monitor"),
72         Bit(4, "ds_cpl"),           Bit(5, "vmx"),          Bit(6, "smx"),      Bit(7, "est"),
73         Bit(8, "tm2"),              Bit(9, "ssse3"),        Bit(10, "cnxt_id"), Bit(11, "sdbg"),
74         Bit(12, "fma"),             Bit(13, "cx16"),        Bit(14, "xtpr"),    Bit(15, "pdcm"),
75         Bit(16, "_ax1cx_res1"),     Bit(17, "pcid"),        Bit(18, "dca"),     Bit(19, "sse41"),
76         Bit(20, "sse42"),           Bit(21, "x2apic"),      Bit(22, "movbe"),   Bit(23, "popcnt"),
77         Bit(24, "tsc_deadline"),    Bit(25, "aes"),         Bit(26, "xsave"),   Bit(27, "osxsave"),
78         Bit(28, "avx"),             Bit(29, "f16c"),        Bit(30, "rdrnd"),   Bit(31, "hypervisor"),
79     )
80     uint eax1_ecx;
81 
82     @(
83         CpuIdLeaf(1, 0),
84         Bit(0, "fpu"),          Bit(1, "vem"),      Bit(2, "de"),           Bit(3, "pse"),
85         Bit(4, "tsc"),          Bit(5, "msr"),      Bit(6, "pae"),          Bit(7, "mce"),
86         Bit(8, "cx8"),          Bit(9, "apic"),     Bit(10, "_ax1dx_res1"), Bit(11, "sep"),
87         Bit(12, "mtrr"),        Bit(13, "pge"),     Bit(14, "mca"),         Bit(15, "cmov"),
88         Bit(16, "pat"),         Bit(17, "pse_36"),  Bit(18, "psn"),         Bit(19, "clfsh"),
89         Bit(20, "_ax1dx_res2"), Bit(21, "ds"),      Bit(22, "acpi"),        Bit(23, "mmx"),
90         Bit(24, "fxsr"),        Bit(25, "sse"),     Bit(26, "sse2"),        Bit(27, "ss"),
91         Bit(28, "htt"),         Bit(29, "tm"),      Bit(30, "ia64"),        Bit(31, "pbe"),
92     )
93     uint eax1_edx;
94 
95     @(
96         CpuIdLeaf(7, 0),
97         Bit(0, "fsgsbase"),     Bit(1, "IA32_TSC_ADJUST"),  Bit(2, "sgx"),              Bit(3, "bmi1"),
98         Bit(4, "hle"),          Bit(5, "avx2"),             Bit(6, "FDP_EXCPTN_ONLY"),  Bit(7, "smep"),
99         Bit(8, "bmi2"),         Bit(9, "erms"),             Bit(10, "invpcid"),         Bit(11, "rtm"),
100         Bit(12, "pqrn"),        Bit(13, "CS_DS_DEPR"),      Bit(14, "mpx"),             Bit(15, "pqe"),
101         Bit(16, "avx512_f"),    Bit(17, "avx512_dq"),       Bit(18, "rdseed"),          Bit(19, "adx"),
102         Bit(20, "smap"),        Bit(21, "avx512_ifma"),     Bit(22, "pcommit"),         Bit(23, "clflushopt"),
103         Bit(24, "clwb"),        Bit(25, "intel_pt"),        Bit(26, "avx512_pf"),       Bit(27, "avx512_er"),
104         Bit(28, "avx512_cd"),   Bit(29, "sha"),             Bit(30, "avx512_bw"),       Bit(31, "avx512_vl"),
105     )
106     uint eax7ecx0_ebx;
107 
108     @(
109         CpuIdLeaf(7, 0),
110         Bit(0, "prefetchwt1"),      Bit(1, "avx512_vbmi"),      Bit(2, "umip"),                 Bit(3, "pku"),
111         Bit(4, "ospke"),            Bit(5, "waitpkg"),          Bit(6, "avx512_vbmi2"),         Bit(7, "cet_ss"),
112         Bit(8, "gfni"),             Bit(9, "vaes"),             Bit(10, "vpclmulqdq"),          Bit(11, "avx512_vnni"),
113         Bit(12, "avx512_bitalg"),   Bit(13, "_ax7cx0cx_res1"),  Bit(14, "avx512_vpopcntdq"),    Bit(15, "_ax7cx0cx_res2"),
114         Bit(16, "fiveLevelPaging"),
115         Bits(17, 5, "mawau"),       Bit(22, "rdpid"),           Bit(23, "_ax7cx0cx_res3"),
116         Bit(24, "_ax7cx0cx_res4"),  Bit(25, "cldemote"),        Bit(26, "_ax7cx0cx_res5"),      Bit(27, "movdiri"),
117         Bit(28, "movdir64b"),       Bit(29, "enqcmd"),          Bit(30, "sgx_lc"),              Bit(31, "pks"),
118     )
119     uint eax7ecx0_ecx;
120 
121     @(
122         CpuIdLeaf(7, 0),
123         Bit(0, "_ax7cx0dx_res1"),       Bit(1, "_ax7cx0dx_res2"),           Bit(2, "avx512_4vnniw"),            Bit(3, "avx512_4fmaps"),
124         Bit(4, "fsrm"),                 Bit(5, "_ax7cx0dx_res3"),           Bit(6, "_ax7cx0dx_res4"),           Bit(7, "_ax7cx0dx_res5"),
125         Bit(8, "avx512_vp2inersect"),   Bit(9, "SRDBS_CTRL"),               Bit(10, "md_clear"),                Bit(11, "_ax7cx0dx_res6"),
126         Bit(12, "_ax7cx0dx_res7"),      Bit(13, "tsx_force_abort"),         Bit(14, "SERIALIZE"),               Bit(15, "Hybrid"),
127         Bit(16, "TSXLDTRK"),            Bit(17, "_ax7cx0dx_res8"),          Bit(18, "pconfig"),                 Bit(19, "lbr"),
128         Bit(20, "cet_ibt"),             Bit(21, "_ax7cx0dx_res9"),          Bit(22, "amx_bf16"),                Bit(23, "AVX512_FP16"),
129         Bit(24, "amx_tile"),            Bit(25, "amx_int8"),                Bit(26, "spec_ctrl"),               Bit(27, "stibp"),
130         Bit(28, "L1D_FLUSH"),           Bit(29, "IA32_ARCH_CAPABILITIES"),  Bit(30, "IA32_CORE_CAPABILITIES"),  Bit(31, "ssbd"),
131     )
132     uint eax7ecx0_edx;
133 }
134 
135 package __gshared Store g_cpuidStore;
136 extern(C) void cpuidPopulateStore(Store*);
137 
138 version(DLIB_GenCpuID)
139 {
140     // Allow cpuidgen to access priate types.
141     alias BitsUda = Bits;
142     alias BitUda = Bit;
143     alias CpuStore = Store;
144     alias LeafUda = CpuIdLeaf;
145 
146     void cpuidInit()
147     {
148     }
149 }
150 else
151 {
152     void cpuidInit()
153     {
154         cpuidPopulateStore(&g_cpuidStore);
155     }
156 }