1 /*******************************************************************************
2   Copyright (c) 2018-2020, Intel Corporation
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions are met:
6 
7       * Redistributions of source code must retain the above copyright notice,
8         this list of conditions and the following disclaimer.
9       * Redistributions in binary form must reproduce the above copyright
10         notice, this list of conditions and the following disclaimer in the
11         documentation and/or other materials provided with the distribution.
12       * Neither the name of Intel Corporation nor the names of its contributors
13         may be used to endorse or promote products derived from this software
14         without specific prior written permission.
15 
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *******************************************************************************/
27 
28 #include <stdint.h>
29 #ifdef __WIN32
30 #include <intrin.h>
31 #endif
32 
33 #include "cpu_feature.h"
34 
35 struct cpuid_regs {
36         uint32_t eax;
37         uint32_t ebx;
38         uint32_t ecx;
39         uint32_t edx;
40 };
41 
42 static struct cpuid_regs cpuid_1_0;
43 static struct cpuid_regs cpuid_7_0;
44 
45 /*
46  * A C wrapper for CPUID opcode
47  *
48  * Parameters:
49  *    [in] leaf    - CPUID leaf number (EAX)
50  *    [in] subleaf - CPUID sub-leaf number (ECX)
51  *    [out] out    - registers structure to store results of CPUID into
52  */
53 static void
__mbcpuid(const unsigned leaf,const unsigned subleaf,struct cpuid_regs * out)54 __mbcpuid(const unsigned leaf, const unsigned subleaf, struct cpuid_regs *out)
55 {
56 #ifdef _WIN32
57         /* Windows */
58         int regs[4];
59 
60         __cpuidex(regs, leaf, subleaf);
61         out->eax = regs[0];
62         out->ebx = regs[1];
63         out->ecx = regs[2];
64         out->edx = regs[3];
65 #else
66         /* Linux */
67         asm volatile("mov %4, %%eax\n\t"
68                      "mov %5, %%ecx\n\t"
69                      "cpuid\n\t"
70                      "mov %%eax, %0\n\t"
71                      "mov %%ebx, %1\n\t"
72                      "mov %%ecx, %2\n\t"
73                      "mov %%edx, %3\n\t"
74                      : "=g" (out->eax), "=g" (out->ebx), "=g" (out->ecx),
75                        "=g" (out->edx)
76                      : "g" (leaf), "g" (subleaf)
77                      : "%eax", "%ebx", "%ecx", "%edx");
78 #endif /* Linux */
79 }
80 
detect_shani(void)81 static uint32_t detect_shani(void)
82 {
83         /* Check presence of SHANI - bit 29 of EBX */
84         return (cpuid_7_0.ebx & (1 << 29));
85 }
86 
detect_aesni(void)87 static uint32_t detect_aesni(void)
88 {
89         /* Check presence of AESNI - bit 25 of ECX */
90         return (cpuid_1_0.ecx & (1 << 25));
91 }
92 
detect_pclmulqdq(void)93 static uint32_t detect_pclmulqdq(void)
94 {
95         /* Check presence of PCLMULQDQ - bit 1 of ECX */
96         return (cpuid_1_0.ecx & (1 << 1));
97 }
98 
detect_cmov(void)99 static uint32_t detect_cmov(void)
100 {
101         /* Check presence of CMOV - bit 15 of EDX */
102         return (cpuid_1_0.edx & (1 << 15));
103 }
104 
detect_sse42(void)105 static uint32_t detect_sse42(void)
106 {
107         /* Check presence of SSE4.2 - bit 20 of ECX */
108         return (cpuid_1_0.ecx & (1 << 20));
109 }
110 
detect_avx(void)111 static uint32_t detect_avx(void)
112 {
113         /* Check presence of AVX - bit 28 of ECX */
114         return (cpuid_1_0.ecx & (1 << 28));
115 }
116 
detect_avx2(void)117 static uint32_t detect_avx2(void)
118 {
119         /* Check presence of AVX2 - bit 5 of EBX */
120         return (cpuid_7_0.ebx & (1 << 5));
121 }
122 
detect_avx512f(void)123 static uint32_t detect_avx512f(void)
124 {
125         /* Check presence of AVX512F - bit 16 of EBX */
126         return (cpuid_7_0.ebx & (1 << 16));
127 }
128 
detect_avx512dq(void)129 static uint32_t detect_avx512dq(void)
130 {
131         /* Check presence of AVX512DQ - bit 17 of EBX */
132         return (cpuid_7_0.ebx & (1 << 17));
133 }
134 
detect_avx512cd(void)135 static uint32_t detect_avx512cd(void)
136 {
137         /* Check presence of AVX512CD - bit 28 of EBX */
138         return (cpuid_7_0.ebx & (1 << 28));
139 }
140 
detect_avx512bw(void)141 static uint32_t detect_avx512bw(void)
142 {
143         /* Check presence of AVX512BW - bit 30 of EBX */
144         return (cpuid_7_0.ebx & (1 << 30));
145 }
146 
detect_avx512vl(void)147 static uint32_t detect_avx512vl(void)
148 {
149         /* Check presence of AVX512VL - bit 31 of EBX */
150         return (cpuid_7_0.ebx & (1 << 31));
151 }
152 
detect_vaes(void)153 static uint32_t detect_vaes(void)
154 {
155         /* Check presence of VAES - bit 9 of ECX */
156         return (cpuid_7_0.ecx & (1 << 9));
157 }
158 
detect_vpclmulqdq(void)159 static uint32_t detect_vpclmulqdq(void)
160 {
161         /* Check presence of VAES - bit 10 of ECX */
162         return (cpuid_7_0.ecx & (1 << 10));
163 }
164 
detect_gfni(void)165 static uint32_t detect_gfni(void)
166 {
167         /* Check presence of GFNI - bit 8 of ECX */
168         return (cpuid_7_0.ecx & (1 << 8));
169 }
170 
cpu_feature_detect(void)171 uint64_t cpu_feature_detect(void)
172 {
173         static const struct {
174                 unsigned req_leaf_number;
175                 uint64_t feat;
176                 uint32_t (*detect_fn)(void);
177         } feat_tab[] = {
178                 { 7, IMB_FEATURE_SHANI, detect_shani },
179                 { 1, IMB_FEATURE_AESNI, detect_aesni },
180                 { 1, IMB_FEATURE_PCLMULQDQ, detect_pclmulqdq },
181                 { 1, IMB_FEATURE_CMOV, detect_cmov },
182                 { 1, IMB_FEATURE_SSE4_2, detect_sse42 },
183                 { 1, IMB_FEATURE_AVX, detect_avx },
184                 { 7, IMB_FEATURE_AVX2, detect_avx2 },
185                 { 7, IMB_FEATURE_AVX512F, detect_avx512f },
186                 { 7, IMB_FEATURE_AVX512DQ, detect_avx512dq },
187                 { 7, IMB_FEATURE_AVX512CD, detect_avx512cd },
188                 { 7, IMB_FEATURE_AVX512BW, detect_avx512bw },
189                 { 7, IMB_FEATURE_AVX512VL, detect_avx512vl },
190                 { 7, IMB_FEATURE_VAES, detect_vaes },
191                 { 7, IMB_FEATURE_VPCLMULQDQ, detect_vpclmulqdq },
192                 { 7, IMB_FEATURE_GFNI, detect_gfni },
193         };
194         struct cpuid_regs r;
195         unsigned hi_leaf_number = 0;
196         uint64_t features = 0;
197         unsigned i;
198 
199         /* Get highest supported CPUID leaf number */
200         __mbcpuid(0x0, 0x0, &r);
201         hi_leaf_number = r.eax;
202 
203         /* Get the most common CPUID leafs to speed up the detection */
204         if (hi_leaf_number >= 1)
205                 __mbcpuid(0x1, 0x0, &cpuid_1_0);
206 
207         if (hi_leaf_number >= 7)
208                 __mbcpuid(0x7, 0x0, &cpuid_7_0);
209 
210         for (i = 0; i < IMB_DIM(feat_tab); i++) {
211                 if (hi_leaf_number < feat_tab[i].req_leaf_number)
212                         continue;
213 
214                 if (feat_tab[i].detect_fn() != 0)
215                         features |= feat_tab[i].feat;
216         }
217 
218 #ifdef SAFE_DATA
219         features |= IMB_FEATURE_SAFE_DATA;
220 #endif
221 #ifdef SAFE_PARAM
222         features |= IMB_FEATURE_SAFE_PARAM;
223 #endif
224 
225         return features;
226 }
227 
cpu_feature_adjust(const uint64_t flags,uint64_t features)228 uint64_t cpu_feature_adjust(const uint64_t flags, uint64_t features)
229 {
230         if (flags & IMB_FLAG_SHANI_OFF)
231                 features &= ~IMB_FEATURE_SHANI;
232 
233         if (flags & IMB_FLAG_AESNI_OFF)
234                 features &= ~IMB_FEATURE_AESNI;
235 
236         return features;
237 }
238