Imported Upstream version 1.6.2
[platform/upstream/libgcrypt.git] / src / hwf-x86.c
1 /* hwf-x86.c - Detect hardware features - x86 part
2  * Copyright (C) 2007, 2011, 2012  Free Software Foundation, Inc.
3  * Copyright (C) 2012  Jussi Kivilinna
4  *
5  * This file is part of Libgcrypt.
6  *
7  * Libgcrypt is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * Libgcrypt is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27
28 #include "g10lib.h"
29 #include "hwf-common.h"
30
31 #if !defined (__i386__) && !defined (__x86_64__)
32 # error Module build for wrong CPU.
33 #endif
34
35 /* We use the next macro to decide whether we can test for certain
36    features.  */
37 #undef HAS_X86_CPUID
38
39 #if defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4 && defined (__GNUC__)
40 # define HAS_X86_CPUID 1
41
42 static int
43 is_cpuid_available(void)
44 {
45   int has_cpuid = 0;
46
47   /* Detect the CPUID feature by testing some undefined behaviour (16
48      vs 32 bit pushf/popf). */
49   asm volatile
50     ("pushf\n\t"                 /* Copy flags to EAX.  */
51      "popl %%eax\n\t"
52      "movl %%eax, %%ecx\n\t"     /* Save flags into ECX.  */
53      "xorl $0x200000, %%eax\n\t" /* Toggle ID bit and copy it to the flags.  */
54      "pushl %%eax\n\t"
55      "popf\n\t"
56      "pushf\n\t"                 /* Copy changed flags again to EAX.  */
57      "popl %%eax\n\t"
58      "pushl %%ecx\n\t"           /* Restore flags from ECX.  */
59      "popf\n\t"
60      "xorl %%eax, %%ecx\n\t"     /* Compare flags against saved flags.  */
61      "jz .Lno_cpuid%=\n\t"       /* Toggling did not work, thus no CPUID.  */
62      "movl $1, %0\n"             /* Worked. true -> HAS_CPUID.  */
63      ".Lno_cpuid%=:\n\t"
64      : "+r" (has_cpuid)
65      :
66      : "%eax", "%ecx", "cc"
67      );
68
69   return has_cpuid;
70 }
71
72 static void
73 get_cpuid(unsigned int in, unsigned int *eax, unsigned int *ebx,
74           unsigned int *ecx, unsigned int *edx)
75 {
76   unsigned int regs[4];
77
78   asm volatile
79     ("pushl %%ebx\n\t"           /* Save GOT register.  */
80      "movl %1, %%ebx\n\t"
81      "cpuid\n\t"
82      "movl %%ebx, %1\n\t"
83      "popl %%ebx\n\t"            /* Restore GOT register. */
84      : "=a" (regs[0]), "=r" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
85      : "0" (in), "1" (0), "2" (0), "3" (0)
86      : "cc"
87      );
88
89   if (eax)
90     *eax = regs[0];
91   if (ebx)
92     *ebx = regs[1];
93   if (ecx)
94     *ecx = regs[2];
95   if (edx)
96     *edx = regs[3];
97 }
98
99 static unsigned int
100 get_xgetbv(void)
101 {
102   unsigned int t_eax;
103
104   asm volatile
105     ("xgetbv\n\t"
106      : "=a" (t_eax)
107      : "c" (0)
108     );
109
110   return t_eax;
111 }
112
113 #endif /* i386 && GNUC */
114
115
116 #if defined (__x86_64__) && defined (__GNUC__)
117 # define HAS_X86_CPUID 1
118
119 static int
120 is_cpuid_available(void)
121 {
122   return 1;
123 }
124
125 static void
126 get_cpuid(unsigned int in, unsigned int *eax, unsigned int *ebx,
127           unsigned int *ecx, unsigned int *edx)
128 {
129   unsigned int regs[4];
130
131   asm volatile
132     ("cpuid\n\t"
133      : "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
134      : "0" (in), "1" (0), "2" (0), "3" (0)
135      : "cc"
136      );
137
138   if (eax)
139     *eax = regs[0];
140   if (ebx)
141     *ebx = regs[1];
142   if (ecx)
143     *ecx = regs[2];
144   if (edx)
145     *edx = regs[3];
146 }
147
148 static unsigned int
149 get_xgetbv(void)
150 {
151   unsigned int t_eax;
152
153   asm volatile
154     ("xgetbv\n\t"
155      : "=a" (t_eax)
156      : "c" (0)
157     );
158
159   return t_eax;
160 }
161
162 #endif /* x86-64 && GNUC */
163
164
165 #ifdef HAS_X86_CPUID
166 static unsigned int
167 detect_x86_gnuc (void)
168 {
169   char vendor_id[12+1];
170   unsigned int features;
171   unsigned int os_supports_avx_avx2_registers = 0;
172   unsigned int max_cpuid_level;
173   unsigned int result = 0;
174
175   (void)os_supports_avx_avx2_registers;
176
177   if (!is_cpuid_available())
178     return 0;
179
180   get_cpuid(0, &max_cpuid_level,
181             (unsigned int *)&vendor_id[0],
182             (unsigned int *)&vendor_id[8],
183             (unsigned int *)&vendor_id[4]);
184   vendor_id[12] = 0;
185
186   if (0)
187     ; /* Just to make "else if" and ifdef macros look pretty.  */
188 #ifdef ENABLE_PADLOCK_SUPPORT
189   else if (!strcmp (vendor_id, "CentaurHauls"))
190     {
191       /* This is a VIA CPU.  Check what PadLock features we have.  */
192
193       /* Check for extended centaur (EAX).  */
194       get_cpuid(0xC0000000, &features, NULL, NULL, NULL);
195
196       /* Has extended centaur features? */
197       if (features > 0xC0000000)
198         {
199            /* Ask for the extended feature flags (EDX). */
200            get_cpuid(0xC0000001, NULL, NULL, NULL, &features);
201
202            /* Test bits 2 and 3 to see whether the RNG exists and is enabled. */
203            if ((features & 0x0C) == 0x0C)
204              result |= HWF_PADLOCK_RNG;
205
206            /* Test bits 6 and 7 to see whether the ACE exists and is enabled. */
207            if ((features & 0xC0) == 0xC0)
208              result |= HWF_PADLOCK_AES;
209
210            /* Test bits 10 and 11 to see whether the PHE exists and is
211               enabled.  */
212            if ((features & 0xC00) == 0xC00)
213              result |= HWF_PADLOCK_SHA;
214
215            /* Test bits 12 and 13 to see whether the MONTMUL exists and is
216               enabled.  */
217            if ((features & 0x3000) == 0x3000)
218              result |= HWF_PADLOCK_MMUL;
219         }
220     }
221 #endif /*ENABLE_PADLOCK_SUPPORT*/
222   else if (!strcmp (vendor_id, "GenuineIntel"))
223     {
224       /* This is an Intel CPU.  */
225       result |= HWF_INTEL_CPU;
226     }
227   else if (!strcmp (vendor_id, "AuthenticAMD"))
228     {
229       /* This is an AMD CPU.  */
230     }
231
232   /* Detect Intel features, that might also be supported by other
233      vendors.  */
234
235   /* Get CPU info and Intel feature flags (ECX).  */
236   get_cpuid(1, NULL, NULL, &features, NULL);
237
238 #ifdef ENABLE_PCLMUL_SUPPORT
239   /* Test bit 1 for PCLMUL.  */
240   if (features & 0x00000002)
241      result |= HWF_INTEL_PCLMUL;
242 #endif
243   /* Test bit 9 for SSSE3.  */
244   if (features & 0x00000200)
245      result |= HWF_INTEL_SSSE3;
246 #ifdef ENABLE_AESNI_SUPPORT
247   /* Test bit 25 for AES-NI.  */
248   if (features & 0x02000000)
249      result |= HWF_INTEL_AESNI;
250 #endif /*ENABLE_AESNI_SUPPORT*/
251 #if defined(ENABLE_AVX_SUPPORT) || defined(ENABLE_AVX2_SUPPORT)
252   /* Test bit 27 for OSXSAVE (required for AVX/AVX2).  */
253   if (features & 0x08000000)
254     {
255       /* Check that OS has enabled both XMM and YMM state support.  */
256       if ((get_xgetbv() & 0x6) == 0x6)
257         os_supports_avx_avx2_registers = 1;
258     }
259 #endif
260 #ifdef ENABLE_AVX_SUPPORT
261   /* Test bit 28 for AVX.  */
262   if (features & 0x10000000)
263     if (os_supports_avx_avx2_registers)
264       result |= HWF_INTEL_AVX;
265 #endif /*ENABLE_AVX_SUPPORT*/
266 #ifdef ENABLE_DRNG_SUPPORT
267   /* Test bit 30 for RDRAND.  */
268   if (features & 0x40000000)
269      result |= HWF_INTEL_RDRAND;
270 #endif /*ENABLE_DRNG_SUPPORT*/
271
272   /* Check additional Intel feature flags.  Early Intel P5 processors report
273    * too high max_cpuid_level, so don't check level 7 if processor does not
274    * support SSE3 (as cpuid:7 contains only features for newer processors).
275    * Source: http://www.sandpile.org/x86/cpuid.htm  */
276   if (max_cpuid_level >= 7 && (features & 0x00000001))
277     {
278       /* Get CPUID:7 contains further Intel feature flags. */
279       get_cpuid(7, NULL, &features, NULL, NULL);
280
281       /* Test bit 8 for BMI2.  */
282       if (features & 0x00000100)
283           result |= HWF_INTEL_BMI2;
284
285 #ifdef ENABLE_AVX2_SUPPORT
286       /* Test bit 5 for AVX2.  */
287       if (features & 0x00000020)
288         if (os_supports_avx_avx2_registers)
289           result |= HWF_INTEL_AVX2;
290 #endif /*ENABLE_AVX_SUPPORT*/
291     }
292
293   return result;
294 }
295 #endif /* HAS_X86_CPUID */
296
297
298 unsigned int
299 _gcry_hwf_detect_x86 (void)
300 {
301 #if defined (HAS_X86_CPUID)
302   return detect_x86_gnuc ();
303 #else
304   return 0;
305 #endif
306 }