eal: fix cpuflags for latest microarch
[dpdk.git] / lib / librte_eal / common / eal_common_cpuflags.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <stdint.h>
37 #include <rte_cpuflags.h>
38
39 /*
40  * This should prevent use of advanced instruction sets in this file. Otherwise
41  * the check function itself could cause a crash.
42  */
43 #ifdef __INTEL_COMPILER
44 #pragma optimize ("", off)
45 #else
46 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
47 #if GCC_VERSION > 404000
48 #pragma GCC optimize ("O0")
49 #endif
50 #endif
51
52 /**
53  * Enumeration of CPU registers
54  */
55 enum cpu_register_t {
56         REG_EAX = 0,
57         REG_EBX,
58         REG_ECX,
59         REG_EDX,
60 };
61
62 /**
63  * Parameters for CPUID instruction
64  */
65 struct cpuid_parameters_t {
66         uint32_t eax;
67         uint32_t ebx;
68         uint32_t ecx;
69         uint32_t edx;
70         enum cpu_register_t return_register;
71 };
72
73 #define CPU_FLAG_NAME_MAX_LEN 64
74
75 /**
76  * Struct to hold a processor feature entry
77  */
78 struct feature_entry {
79         enum rte_cpu_flag_t feature;            /**< feature name */
80         char name[CPU_FLAG_NAME_MAX_LEN];       /**< String for printing */
81         struct cpuid_parameters_t params;       /**< cpuid parameters */
82         uint32_t feature_mask;                  /**< bitmask for feature */
83 };
84
85 #define FEAT_DEF(f) RTE_CPUFLAG_##f, #f
86
87 /**
88  * An array that holds feature entries
89  */
90 static const struct feature_entry cpu_feature_table[] = {
91         {FEAT_DEF(SSE3),              {0x1, 0, 0, 0, REG_ECX}, 0x00000001},
92         {FEAT_DEF(PCLMULQDQ),         {0x1, 0, 0, 0, REG_ECX}, 0x00000002},
93         {FEAT_DEF(DTES64),            {0x1, 0, 0, 0, REG_ECX}, 0x00000004},
94         {FEAT_DEF(MONITOR),           {0x1, 0, 0, 0, REG_ECX}, 0x00000008},
95         {FEAT_DEF(DS_CPL),            {0x1, 0, 0, 0, REG_ECX}, 0x00000010},
96         {FEAT_DEF(VMX),               {0x1, 0, 0, 0, REG_ECX}, 0x00000020},
97         {FEAT_DEF(SMX),               {0x1, 0, 0, 0, REG_ECX}, 0x00000040},
98         {FEAT_DEF(EIST),              {0x1, 0, 0, 0, REG_ECX}, 0x00000080},
99         {FEAT_DEF(TM2),               {0x1, 0, 0, 0, REG_ECX}, 0x00000100},
100         {FEAT_DEF(SSSE3),             {0x1, 0, 0, 0, REG_ECX}, 0x00000200},
101         {FEAT_DEF(CNXT_ID),           {0x1, 0, 0, 0, REG_ECX}, 0x00000400},
102         {FEAT_DEF(FMA),               {0x1, 0, 0, 0, REG_ECX}, 0x00001000},
103         {FEAT_DEF(CMPXCHG16B),        {0x1, 0, 0, 0, REG_ECX}, 0x00002000},
104         {FEAT_DEF(XTPR),              {0x1, 0, 0, 0, REG_ECX}, 0x00004000},
105         {FEAT_DEF(PDCM),              {0x1, 0, 0, 0, REG_ECX}, 0x00008000},
106         {FEAT_DEF(PCID),              {0x1, 0, 0, 0, REG_ECX}, 0x00020000},
107         {FEAT_DEF(DCA),               {0x1, 0, 0, 0, REG_ECX}, 0x00040000},
108         {FEAT_DEF(SSE4_1),            {0x1, 0, 0, 0, REG_ECX}, 0x00080000},
109         {FEAT_DEF(SSE4_2),            {0x1, 0, 0, 0, REG_ECX}, 0x00100000},
110         {FEAT_DEF(X2APIC),            {0x1, 0, 0, 0, REG_ECX}, 0x00200000},
111         {FEAT_DEF(MOVBE),             {0x1, 0, 0, 0, REG_ECX}, 0x00400000},
112         {FEAT_DEF(POPCNT),            {0x1, 0, 0, 0, REG_ECX}, 0x00800000},
113         {FEAT_DEF(TSC_DEADLINE),      {0x1, 0, 0, 0, REG_ECX}, 0x01000000},
114         {FEAT_DEF(AES),               {0x1, 0, 0, 0, REG_ECX}, 0x02000000},
115         {FEAT_DEF(XSAVE),             {0x1, 0, 0, 0, REG_ECX}, 0x04000000},
116         {FEAT_DEF(OSXSAVE),           {0x1, 0, 0, 0, REG_ECX}, 0x08000000},
117         {FEAT_DEF(AVX),               {0x1, 0, 0, 0, REG_ECX}, 0x10000000},
118         {FEAT_DEF(F16C),              {0x1, 0, 0, 0, REG_ECX}, 0x20000000},
119         {FEAT_DEF(RDRAND),            {0x1, 0, 0, 0, REG_ECX}, 0x40000000},
120
121         {FEAT_DEF(FPU),               {0x1, 0, 0, 0, REG_EDX}, 0x00000001},
122         {FEAT_DEF(VME),               {0x1, 0, 0, 0, REG_EDX}, 0x00000002},
123         {FEAT_DEF(DE),                {0x1, 0, 0, 0, REG_EDX}, 0x00000004},
124         {FEAT_DEF(PSE),               {0x1, 0, 0, 0, REG_EDX}, 0x00000008},
125         {FEAT_DEF(TSC),               {0x1, 0, 0, 0, REG_EDX}, 0x00000010},
126         {FEAT_DEF(MSR),               {0x1, 0, 0, 0, REG_EDX}, 0x00000020},
127         {FEAT_DEF(PAE),               {0x1, 0, 0, 0, REG_EDX}, 0x00000040},
128         {FEAT_DEF(MCE),               {0x1, 0, 0, 0, REG_EDX}, 0x00000080},
129         {FEAT_DEF(CX8),               {0x1, 0, 0, 0, REG_EDX}, 0x00000100},
130         {FEAT_DEF(APIC),              {0x1, 0, 0, 0, REG_EDX}, 0x00000200},
131         {FEAT_DEF(SEP),               {0x1, 0, 0, 0, REG_EDX}, 0x00000800},
132         {FEAT_DEF(MTRR),              {0x1, 0, 0, 0, REG_EDX}, 0x00001000},
133         {FEAT_DEF(PGE),               {0x1, 0, 0, 0, REG_EDX}, 0x00002000},
134         {FEAT_DEF(MCA),               {0x1, 0, 0, 0, REG_EDX}, 0x00004000},
135         {FEAT_DEF(CMOV),              {0x1, 0, 0, 0, REG_EDX}, 0x00008000},
136         {FEAT_DEF(PAT),               {0x1, 0, 0, 0, REG_EDX}, 0x00010000},
137         {FEAT_DEF(PSE36),             {0x1, 0, 0, 0, REG_EDX}, 0x00020000},
138         {FEAT_DEF(PSN),               {0x1, 0, 0, 0, REG_EDX}, 0x00040000},
139         {FEAT_DEF(CLFSH),             {0x1, 0, 0, 0, REG_EDX}, 0x00080000},
140         {FEAT_DEF(DS),                {0x1, 0, 0, 0, REG_EDX}, 0x00200000},
141         {FEAT_DEF(ACPI),              {0x1, 0, 0, 0, REG_EDX}, 0x00400000},
142         {FEAT_DEF(MMX),               {0x1, 0, 0, 0, REG_EDX}, 0x00800000},
143         {FEAT_DEF(FXSR),              {0x1, 0, 0, 0, REG_EDX}, 0x01000000},
144         {FEAT_DEF(SSE),               {0x1, 0, 0, 0, REG_EDX}, 0x02000000},
145         {FEAT_DEF(SSE2),              {0x1, 0, 0, 0, REG_EDX}, 0x04000000},
146         {FEAT_DEF(SS),                {0x1, 0, 0, 0, REG_EDX}, 0x08000000},
147         {FEAT_DEF(HTT),               {0x1, 0, 0, 0, REG_EDX}, 0x10000000},
148         {FEAT_DEF(TM),                {0x1, 0, 0, 0, REG_EDX}, 0x20000000},
149         {FEAT_DEF(PBE),               {0x1, 0, 0, 0, REG_EDX}, 0x80000000},
150
151         {FEAT_DEF(DIGTEMP),           {0x6, 0, 0, 0, REG_EAX}, 0x00000001},
152         {FEAT_DEF(TRBOBST),           {0x6, 0, 0, 0, REG_EAX}, 0x00000002},
153         {FEAT_DEF(ARAT),              {0x6, 0, 0, 0, REG_EAX}, 0x00000004},
154         {FEAT_DEF(PLN),               {0x6, 0, 0, 0, REG_EAX}, 0x00000010},
155         {FEAT_DEF(ECMD),              {0x6, 0, 0, 0, REG_EAX}, 0x00000020},
156         {FEAT_DEF(PTM),               {0x6, 0, 0, 0, REG_EAX}, 0x00000040},
157
158         {FEAT_DEF(MPERF_APERF_MSR),   {0x6, 0, 0, 0, REG_ECX}, 0x00000001},
159         {FEAT_DEF(ACNT2),             {0x6, 0, 0, 0, REG_ECX}, 0x00000002},
160         {FEAT_DEF(ENERGY_EFF),        {0x6, 0, 0, 0, REG_ECX}, 0x00000008},
161
162         {FEAT_DEF(FSGSBASE),          {0x7, 0, 0, 0, REG_EBX}, 0x00000001},
163         {FEAT_DEF(BMI1),              {0x7, 0, 0, 0, REG_EBX}, 0x00000004},
164         {FEAT_DEF(HLE),               {0x7, 0, 0, 0, REG_EBX}, 0x00000010},
165         {FEAT_DEF(AVX2),              {0x7, 0, 0, 0, REG_EBX}, 0x00000020},
166         {FEAT_DEF(SMEP),              {0x7, 0, 0, 0, REG_EBX}, 0x00000040},
167         {FEAT_DEF(BMI2),              {0x7, 0, 0, 0, REG_EBX}, 0x00000080},
168         {FEAT_DEF(ERMS),              {0x7, 0, 0, 0, REG_EBX}, 0x00000100},
169         {FEAT_DEF(INVPCID),           {0x7, 0, 0, 0, REG_EBX}, 0x00000400},
170         {FEAT_DEF(RTM),               {0x7, 0, 0, 0, REG_EBX}, 0x00000800},
171
172         {FEAT_DEF(LAHF_SAHF),  {0x80000001, 0, 0, 0, REG_ECX}, 0x00000001},
173         {FEAT_DEF(LZCNT),      {0x80000001, 0, 0, 0, REG_ECX}, 0x00000010},
174
175         {FEAT_DEF(SYSCALL),    {0x80000001, 0, 0, 0, REG_EDX}, 0x00000800},
176         {FEAT_DEF(XD),         {0x80000001, 0, 0, 0, REG_EDX}, 0x00100000},
177         {FEAT_DEF(1GB_PG),     {0x80000001, 0, 0, 0, REG_EDX}, 0x04000000},
178         {FEAT_DEF(RDTSCP),     {0x80000001, 0, 0, 0, REG_EDX}, 0x08000000},
179         {FEAT_DEF(EM64T),      {0x80000001, 0, 0, 0, REG_EDX}, 0x20000000},
180
181         {FEAT_DEF(INVTSC),     {0x80000007, 0, 0, 0, REG_EDX}, 0x00000100},
182 };
183
184 /*
185  * Execute CPUID instruction and get contents of a specific register
186  *
187  * This function, when compiled with GCC, will generate architecture-neutral
188  * code, as per GCC manual.
189  */
190 static inline int
191 rte_cpu_get_features(struct cpuid_parameters_t params)
192 {
193         int eax, ebx, ecx, edx;            /* registers */
194
195 #ifndef __PIC__
196    asm volatile ("cpuid"
197                  /* output */
198                  : "=a" (eax),
199                    "=b" (ebx),
200                    "=c" (ecx),
201                    "=d" (edx)
202                  /* input */
203                  : "a" (params.eax),
204                    "b" (params.ebx),
205                    "c" (params.ecx),
206                    "d" (params.edx));
207 #else
208         asm volatile ( 
209             "mov %%ebx, %%edi\n"
210             "cpuid\n"
211             "xchgl %%ebx, %%edi;\n"
212             : "=a" (eax),
213               "=D" (ebx),
214               "=c" (ecx),
215               "=d" (edx)
216             /* input */
217             : "a" (params.eax),
218               "D" (params.ebx),
219               "c" (params.ecx),
220               "d" (params.edx));
221 #endif
222
223         switch (params.return_register) {
224         case REG_EAX:
225                 return eax;
226         case REG_EBX:
227                 return ebx;
228         case REG_ECX:
229                 return ecx;
230         case REG_EDX:
231                 return edx;
232         default:
233                 return 0;
234         }
235 }
236
237 /*
238  * Checks if a particular flag is available on current machine.
239  */
240 int
241 rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
242 {
243         int value;
244
245         if (feature >= RTE_CPUFLAG_NUMFLAGS)
246                 /* Flag does not match anything in the feature tables */
247                 return -ENOENT;
248
249         /* get value of the register containing the desired feature */
250         value = rte_cpu_get_features(cpu_feature_table[feature].params);
251
252         /* check if the feature is enabled */
253         return (cpu_feature_table[feature].feature_mask & value) > 0;
254 }
255
256 /**
257  * Checks if the machine is adequate for running the binary. If it is not, the
258  * program exits with status 1.
259  * The function attribute forces this function to be called before main(). But
260  * with ICC, the check is generated by the compiler.
261  */
262 #ifndef __INTEL_COMPILER
263 void __attribute__ ((__constructor__))
264 #else
265 void
266 #endif
267 rte_cpu_check_supported(void)
268 {
269         /* This is generated at compile-time by the build system */
270         static const enum rte_cpu_flag_t compile_time_flags[] = {
271                         RTE_COMPILE_TIME_CPUFLAGS
272         };
273         unsigned i;
274
275         for (i = 0; i < sizeof(compile_time_flags)/sizeof(compile_time_flags[0]); i++)
276                 if (rte_cpu_get_flag_enabled(compile_time_flags[i]) < 1) {
277                         fprintf(stderr,
278                                 "ERROR: This system does not support \"%s\".\n"
279                                 "Please check that RTE_MACHINE is set correctly.\n",
280                                 cpu_feature_table[compile_time_flags[i]].name);
281                         exit(1);
282                 }
283 }