wolfSSL SSL/TLS library, support up to TLS1.3
Dependents: CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more
wolfcrypt/src/cpuid.c@17:a5f916481144, 2020-06-05 (annotated)
- Committer:
- wolfSSL
- Date:
- Fri Jun 05 00:11:07 2020 +0000
- Revision:
- 17:a5f916481144
- Parent:
- 16:8e0d178b1d1e
wolfSSL 4.4.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 15:117db924cf7c | 1 | /* cpuid.c |
wolfSSL | 15:117db924cf7c | 2 | * |
wolfSSL | 16:8e0d178b1d1e | 3 | * Copyright (C) 2006-2020 wolfSSL Inc. |
wolfSSL | 15:117db924cf7c | 4 | * |
wolfSSL | 15:117db924cf7c | 5 | * This file is part of wolfSSL. |
wolfSSL | 15:117db924cf7c | 6 | * |
wolfSSL | 15:117db924cf7c | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 15:117db924cf7c | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 15:117db924cf7c | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 15:117db924cf7c | 10 | * (at your option) any later version. |
wolfSSL | 15:117db924cf7c | 11 | * |
wolfSSL | 15:117db924cf7c | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 15:117db924cf7c | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 15:117db924cf7c | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 15:117db924cf7c | 15 | * GNU General Public License for more details. |
wolfSSL | 15:117db924cf7c | 16 | * |
wolfSSL | 15:117db924cf7c | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 15:117db924cf7c | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 15:117db924cf7c | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 15:117db924cf7c | 20 | */ |
wolfSSL | 15:117db924cf7c | 21 | |
wolfSSL | 15:117db924cf7c | 22 | |
wolfSSL | 15:117db924cf7c | 23 | #ifdef HAVE_CONFIG_H |
wolfSSL | 15:117db924cf7c | 24 | #include <config.h> |
wolfSSL | 15:117db924cf7c | 25 | #endif |
wolfSSL | 15:117db924cf7c | 26 | |
wolfSSL | 15:117db924cf7c | 27 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 15:117db924cf7c | 28 | |
wolfSSL | 15:117db924cf7c | 29 | #include <wolfssl/wolfcrypt/cpuid.h> |
wolfSSL | 15:117db924cf7c | 30 | |
wolfSSL | 15:117db924cf7c | 31 | #if (defined(WOLFSSL_X86_64_BUILD) || defined(USE_INTEL_SPEEDUP) || \ |
wolfSSL | 15:117db924cf7c | 32 | defined(WOLFSSL_AESNI)) && !defined(WOLFSSL_NO_ASM) |
wolfSSL | 15:117db924cf7c | 33 | /* Each platform needs to query info type 1 from cpuid to see if aesni is |
wolfSSL | 15:117db924cf7c | 34 | * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts |
wolfSSL | 15:117db924cf7c | 35 | */ |
wolfSSL | 15:117db924cf7c | 36 | |
wolfSSL | 15:117db924cf7c | 37 | #ifndef _MSC_VER |
wolfSSL | 15:117db924cf7c | 38 | #define cpuid(reg, leaf, sub)\ |
wolfSSL | 15:117db924cf7c | 39 | __asm__ __volatile__ ("cpuid":\ |
wolfSSL | 15:117db924cf7c | 40 | "=a" (reg[0]), "=b" (reg[1]), "=c" (reg[2]), "=d" (reg[3]) :\ |
wolfSSL | 15:117db924cf7c | 41 | "a" (leaf), "c"(sub)); |
wolfSSL | 15:117db924cf7c | 42 | |
wolfSSL | 15:117db924cf7c | 43 | #define XASM_LINK(f) asm(f) |
wolfSSL | 15:117db924cf7c | 44 | #else |
wolfSSL | 15:117db924cf7c | 45 | #include <intrin.h> |
wolfSSL | 15:117db924cf7c | 46 | |
wolfSSL | 15:117db924cf7c | 47 | #define cpuid(a,b,c) __cpuidex((int*)a,b,c) |
wolfSSL | 15:117db924cf7c | 48 | |
wolfSSL | 15:117db924cf7c | 49 | #define XASM_LINK(f) |
wolfSSL | 15:117db924cf7c | 50 | #endif /* _MSC_VER */ |
wolfSSL | 15:117db924cf7c | 51 | |
wolfSSL | 15:117db924cf7c | 52 | #define EAX 0 |
wolfSSL | 15:117db924cf7c | 53 | #define EBX 1 |
wolfSSL | 15:117db924cf7c | 54 | #define ECX 2 |
wolfSSL | 15:117db924cf7c | 55 | #define EDX 3 |
wolfSSL | 15:117db924cf7c | 56 | |
wolfSSL | 15:117db924cf7c | 57 | static word32 cpuid_check = 0; |
wolfSSL | 15:117db924cf7c | 58 | static word32 cpuid_flags = 0; |
wolfSSL | 15:117db924cf7c | 59 | |
wolfSSL | 15:117db924cf7c | 60 | static word32 cpuid_flag(word32 leaf, word32 sub, word32 num, word32 bit) |
wolfSSL | 15:117db924cf7c | 61 | { |
wolfSSL | 15:117db924cf7c | 62 | int got_intel_cpu = 0; |
wolfSSL | 15:117db924cf7c | 63 | int got_amd_cpu = 0; |
wolfSSL | 15:117db924cf7c | 64 | unsigned int reg[5]; |
wolfSSL | 15:117db924cf7c | 65 | reg[4] = '\0'; |
wolfSSL | 15:117db924cf7c | 66 | cpuid(reg, 0, 0); |
wolfSSL | 15:117db924cf7c | 67 | |
wolfSSL | 15:117db924cf7c | 68 | /* check for Intel cpu */ |
wolfSSL | 15:117db924cf7c | 69 | if (XMEMCMP((char *)&(reg[EBX]), "Genu", 4) == 0 && |
wolfSSL | 15:117db924cf7c | 70 | XMEMCMP((char *)&(reg[EDX]), "ineI", 4) == 0 && |
wolfSSL | 15:117db924cf7c | 71 | XMEMCMP((char *)&(reg[ECX]), "ntel", 4) == 0) { |
wolfSSL | 15:117db924cf7c | 72 | got_intel_cpu = 1; |
wolfSSL | 15:117db924cf7c | 73 | } |
wolfSSL | 15:117db924cf7c | 74 | |
wolfSSL | 15:117db924cf7c | 75 | /* check for AMD cpu */ |
wolfSSL | 15:117db924cf7c | 76 | if (XMEMCMP((char *)&(reg[EBX]), "Auth", 4) == 0 && |
wolfSSL | 15:117db924cf7c | 77 | XMEMCMP((char *)&(reg[EDX]), "enti", 4) == 0 && |
wolfSSL | 15:117db924cf7c | 78 | XMEMCMP((char *)&(reg[ECX]), "cAMD", 4) == 0) { |
wolfSSL | 15:117db924cf7c | 79 | got_amd_cpu = 1; |
wolfSSL | 15:117db924cf7c | 80 | } |
wolfSSL | 15:117db924cf7c | 81 | |
wolfSSL | 15:117db924cf7c | 82 | if (got_intel_cpu || got_amd_cpu) { |
wolfSSL | 15:117db924cf7c | 83 | cpuid(reg, leaf, sub); |
wolfSSL | 15:117db924cf7c | 84 | return ((reg[num] >> bit) & 0x1); |
wolfSSL | 15:117db924cf7c | 85 | } |
wolfSSL | 15:117db924cf7c | 86 | return 0; |
wolfSSL | 15:117db924cf7c | 87 | } |
wolfSSL | 15:117db924cf7c | 88 | |
wolfSSL | 15:117db924cf7c | 89 | |
wolfSSL | 15:117db924cf7c | 90 | void cpuid_set_flags(void) |
wolfSSL | 15:117db924cf7c | 91 | { |
wolfSSL | 15:117db924cf7c | 92 | if (!cpuid_check) { |
wolfSSL | 15:117db924cf7c | 93 | if (cpuid_flag(1, 0, ECX, 28)) { cpuid_flags |= CPUID_AVX1 ; } |
wolfSSL | 15:117db924cf7c | 94 | if (cpuid_flag(7, 0, EBX, 5)) { cpuid_flags |= CPUID_AVX2 ; } |
wolfSSL | 15:117db924cf7c | 95 | if (cpuid_flag(7, 0, EBX, 8)) { cpuid_flags |= CPUID_BMI2 ; } |
wolfSSL | 15:117db924cf7c | 96 | if (cpuid_flag(1, 0, ECX, 30)) { cpuid_flags |= CPUID_RDRAND; } |
wolfSSL | 15:117db924cf7c | 97 | if (cpuid_flag(7, 0, EBX, 18)) { cpuid_flags |= CPUID_RDSEED; } |
wolfSSL | 16:8e0d178b1d1e | 98 | if (cpuid_flag(1, 0, ECX, 25)) { cpuid_flags |= CPUID_AESNI ; } |
wolfSSL | 15:117db924cf7c | 99 | if (cpuid_flag(7, 0, EBX, 19)) { cpuid_flags |= CPUID_ADX ; } |
wolfSSL | 15:117db924cf7c | 100 | cpuid_check = 1; |
wolfSSL | 15:117db924cf7c | 101 | } |
wolfSSL | 15:117db924cf7c | 102 | } |
wolfSSL | 15:117db924cf7c | 103 | |
wolfSSL | 15:117db924cf7c | 104 | word32 cpuid_get_flags(void) |
wolfSSL | 15:117db924cf7c | 105 | { |
wolfSSL | 15:117db924cf7c | 106 | if (!cpuid_check) |
wolfSSL | 15:117db924cf7c | 107 | cpuid_set_flags(); |
wolfSSL | 15:117db924cf7c | 108 | return cpuid_flags; |
wolfSSL | 15:117db924cf7c | 109 | } |
wolfSSL | 15:117db924cf7c | 110 | #endif |
wolfSSL | 15:117db924cf7c | 111 |