Xuyi Wang / wolfcrypt

Dependents:   OS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cpuid.c Source File

cpuid.c

00001 /* cpuid.c
00002  *
00003  * Copyright (C) 2006-2017 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 #ifdef HAVE_CONFIG_H
00024     #include <config.h>
00025 #endif
00026 
00027 #include <wolfcrypt/settings.h>
00028 
00029 #include <wolfcrypt/cpuid.h>
00030 
00031 #if (defined(WOLFSSL_X86_64_BUILD) || defined(USE_INTEL_SPEEDUP) || \
00032      defined(WOLFSSL_AESNI)) && !defined(WOLFSSL_NO_ASM)
00033     /* Each platform needs to query info type 1 from cpuid to see if aesni is
00034      * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
00035      */
00036 
00037     #ifndef _MSC_VER
00038         #define cpuid(reg, leaf, sub)\
00039             __asm__ __volatile__ ("cpuid":\
00040                 "=a" (reg[0]), "=b" (reg[1]), "=c" (reg[2]), "=d" (reg[3]) :\
00041                 "a" (leaf), "c"(sub));
00042 
00043         #define XASM_LINK(f) asm(f)
00044     #else
00045         #include <intrin.h>
00046 
00047         #define cpuid(a,b,c) __cpuidex((int*)a,b,c)
00048 
00049         #define XASM_LINK(f)
00050     #endif /* _MSC_VER */
00051 
00052     #define EAX 0
00053     #define EBX 1
00054     #define ECX 2
00055     #define EDX 3
00056 
00057     static word32 cpuid_check = 0;
00058     static word32 cpuid_flags = 0;
00059 
00060     static word32 cpuid_flag(word32 leaf, word32 sub, word32 num, word32 bit)
00061     {
00062         int got_intel_cpu = 0;
00063         int got_amd_cpu = 0;
00064         unsigned int reg[5];
00065         reg[4] = '\0';
00066         cpuid(reg, 0, 0);
00067 
00068         /* check for Intel cpu */
00069         if (XMEMCMP((char *)&(reg[EBX]), "Genu", 4) == 0 &&
00070             XMEMCMP((char *)&(reg[EDX]), "ineI", 4) == 0 &&
00071             XMEMCMP((char *)&(reg[ECX]), "ntel", 4) == 0) {
00072             got_intel_cpu = 1;
00073         }
00074 
00075         /* check for AMD cpu */
00076         if (XMEMCMP((char *)&(reg[EBX]), "Auth", 4) == 0 &&
00077             XMEMCMP((char *)&(reg[EDX]), "enti", 4) == 0 &&
00078             XMEMCMP((char *)&(reg[ECX]), "cAMD", 4) == 0) {
00079             got_amd_cpu = 1;
00080         }
00081 
00082         if (got_intel_cpu || got_amd_cpu) {
00083             cpuid(reg, leaf, sub);
00084             return ((reg[num] >> bit) & 0x1);
00085         }
00086         return 0;
00087     }
00088 
00089 
00090     void cpuid_set_flags(void)
00091     {
00092         if (!cpuid_check) {
00093             if (cpuid_flag(1, 0, ECX, 28)) { cpuid_flags |= CPUID_AVX1  ; }
00094             if (cpuid_flag(7, 0, EBX,  5)) { cpuid_flags |= CPUID_AVX2  ; }
00095             if (cpuid_flag(7, 0, EBX,  8)) { cpuid_flags |= CPUID_BMI2  ; }
00096             if (cpuid_flag(1, 0, ECX, 30)) { cpuid_flags |= CPUID_RDRAND; }
00097             if (cpuid_flag(7, 0, EBX, 18)) { cpuid_flags |= CPUID_RDSEED; }
00098             if (cpuid_flag(1, 0, ECX, 26)) { cpuid_flags |= CPUID_AESNI ; }
00099             if (cpuid_flag(7, 0, EBX, 19)) { cpuid_flags |= CPUID_ADX   ; }
00100             cpuid_check = 1;
00101         }
00102     }
00103 
00104     word32 cpuid_get_flags(void)
00105     {
00106         if (!cpuid_check)
00107             cpuid_set_flags();
00108         return cpuid_flags;
00109     }
00110 #endif
00111