]> wimlib.net Git - wimlib/blob - src/x86_cpu_features.c
5e434f9f0b18cfcb36ca9559baed4d671d0ec1c7
[wimlib] / src / x86_cpu_features.c
1 /*
2  * x86_cpu_features.c
3  *
4  * Feature detection for x86 processors.
5  *
6  * Author:      Eric Biggers
7  * Year:        2015
8  *
9  * The author dedicates this file to the public domain.
10  * You can do whatever you want with this file.
11  */
12
13 #include "wimlib/x86_cpu_features.h"
14
15 #if defined(__i386__) || defined(__x86_64__)
16
17 #define DEBUG 0
18
19 #if DEBUG
20 #  include <stdio.h>
21 #endif
22
23 u32 _x86_cpu_features = 0;
24
25 /* With old GCC versions we have to manually save and restore the x86_32 PIC
26  * register (ebx).  See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47602  */
27 #if defined(__i386__) && defined(__PIC__)
28 #  define EBX_CONSTRAINT "=r"
29 #else
30 #  define EBX_CONSTRAINT "=b"
31 #endif
32
33 /* Execute the CPUID instruction.  */
34 static inline void
35 cpuid(u32 leaf, u32 subleaf, u32 *a, u32 *b, u32 *c, u32 *d)
36 {
37         __asm__(".ifnc %%ebx, %1; mov  %%ebx, %1; .endif\n"
38                 "cpuid                                  \n"
39                 ".ifnc %%ebx, %1; xchg %%ebx, %1; .endif\n"
40                 : "=a" (*a), EBX_CONSTRAINT (*b), "=c" (*c), "=d" (*d)
41                 : "a" (leaf), "c" (subleaf));
42 }
43
44 /* Read an extended control register.  */
45 static inline u64
46 read_xcr(u32 index)
47 {
48         u32 edx, eax;
49
50         /* Execute the "xgetbv" instruction.  Old versions of binutils do not
51          * recognize this instruction, so list the raw bytes instead.  */
52         __asm__ (".byte 0x0f, 0x01, 0xd0" : "=d" (edx), "=a" (eax) : "c" (index));
53
54         return ((u64)edx << 32) | eax;
55 }
56
57 #define IS_SET(reg, bit) ((reg) & ((u32)1 << (bit)))
58
59 /* Initialize _x86_cpu_features with bits for interesting processor features. */
60 void
61 x86_setup_cpu_features(void)
62 {
63         u32 features = 0;
64         u32 dummy1, dummy2, dummy3, dummy4;
65         u32 max_function;
66         u32 features_1, features_2, features_3, features_4;
67         bool os_saves_ymm_regs = false;
68
69         /* Get maximum supported function  */
70         cpuid(0, 0, &max_function, &dummy2, &dummy3, &dummy4);
71         if (max_function < 1)
72                 goto out;
73
74         /* Standard feature flags  */
75         cpuid(1, 0, &dummy1, &dummy2, &features_2, &features_1);
76
77         if (IS_SET(features_1, 25))
78                 features |= X86_CPU_FEATURE_SSE;
79
80         if (IS_SET(features_1, 26))
81                 features |= X86_CPU_FEATURE_SSE2;
82
83         if (IS_SET(features_2, 0))
84                 features |= X86_CPU_FEATURE_SSE3;
85
86         if (IS_SET(features_2, 9))
87                 features |= X86_CPU_FEATURE_SSSE3;
88
89         if (IS_SET(features_2, 19))
90                 features |= X86_CPU_FEATURE_SSE4_1;
91
92         if (IS_SET(features_2, 20))
93                 features |= X86_CPU_FEATURE_SSE4_2;
94
95         if (IS_SET(features_2, 27)) /* OSXSAVE set?  */
96                 if ((read_xcr(0) & 0x6) == 0x6)
97                         os_saves_ymm_regs = true;
98
99         if (os_saves_ymm_regs && IS_SET(features_2, 28))
100                 features |= X86_CPU_FEATURE_AVX;
101
102         if (max_function < 7)
103                 goto out;
104
105         /* Extended feature flags  */
106         cpuid(7, 0, &dummy1, &features_3, &features_4, &dummy4);
107
108         if (IS_SET(features_3, 3))
109                 features |= X86_CPU_FEATURE_BMI;
110
111         if (os_saves_ymm_regs && IS_SET(features_3, 5))
112                 features |= X86_CPU_FEATURE_AVX2;
113
114         if (IS_SET(features_3, 8))
115                 features |= X86_CPU_FEATURE_BMI2;
116
117 out:
118
119 #if DEBUG
120         printf("Detected x86 CPU features: ");
121         if (features & X86_CPU_FEATURE_SSE)
122                 printf("SSE ");
123         if (features & X86_CPU_FEATURE_SSE2)
124                 printf("SSE2 ");
125         if (features & X86_CPU_FEATURE_SSE3)
126                 printf("SSE3 ");
127         if (features & X86_CPU_FEATURE_SSSE3)
128                 printf("SSSE3 ");
129         if (features & X86_CPU_FEATURE_SSE4_1)
130                 printf("SSE4.1 ");
131         if (features & X86_CPU_FEATURE_SSE4_2)
132                 printf("SSE4.2 ");
133         if (features & X86_CPU_FEATURE_BMI)
134                 printf("BMI ");
135         if (features & X86_CPU_FEATURE_AVX)
136                 printf("AVX ");
137         if (features & X86_CPU_FEATURE_BMI2)
138                 printf("BMI2 ");
139         if (features & X86_CPU_FEATURE_AVX2)
140                 printf("AVX2 ");
141         printf("\n");
142 #endif /* DEBUG */
143
144         _x86_cpu_features = features | X86_CPU_FEATURES_KNOWN;
145 }
146
147 #endif /* __i386__ || __x86_64__ */