Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
wolfcrypt/src/cpuid.c@12:1a06964c2adb, 2017-08-22 (annotated)
- Committer:
- wolfSSL
- Date:
- Tue Aug 22 10:47:28 2017 +0000
- Revision:
- 12:1a06964c2adb
wolfSSL 3.12.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 12:1a06964c2adb | 1 | /* cpuid.c |
wolfSSL | 12:1a06964c2adb | 2 | * |
wolfSSL | 12:1a06964c2adb | 3 | * Copyright (C) 2006-2016 wolfSSL Inc. |
wolfSSL | 12:1a06964c2adb | 4 | * |
wolfSSL | 12:1a06964c2adb | 5 | * This file is part of wolfSSL. |
wolfSSL | 12:1a06964c2adb | 6 | * |
wolfSSL | 12:1a06964c2adb | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 12:1a06964c2adb | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 12:1a06964c2adb | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 12:1a06964c2adb | 10 | * (at your option) any later version. |
wolfSSL | 12:1a06964c2adb | 11 | * |
wolfSSL | 12:1a06964c2adb | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 12:1a06964c2adb | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 12:1a06964c2adb | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 12:1a06964c2adb | 15 | * GNU General Public License for more details. |
wolfSSL | 12:1a06964c2adb | 16 | * |
wolfSSL | 12:1a06964c2adb | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 12:1a06964c2adb | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 12:1a06964c2adb | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 12:1a06964c2adb | 20 | */ |
wolfSSL | 12:1a06964c2adb | 21 | |
wolfSSL | 12:1a06964c2adb | 22 | |
wolfSSL | 12:1a06964c2adb | 23 | #ifdef HAVE_CONFIG_H |
wolfSSL | 12:1a06964c2adb | 24 | #include <config.h> |
wolfSSL | 12:1a06964c2adb | 25 | #endif |
wolfSSL | 12:1a06964c2adb | 26 | |
wolfSSL | 12:1a06964c2adb | 27 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 12:1a06964c2adb | 28 | |
wolfSSL | 12:1a06964c2adb | 29 | #include <wolfssl/wolfcrypt/cpuid.h> |
wolfSSL | 12:1a06964c2adb | 30 | |
wolfSSL | 12:1a06964c2adb | 31 | #if defined(WOLFSSL_X86_64_BUILD) || defined(USE_INTEL_SPEEDUP) || \ |
wolfSSL | 12:1a06964c2adb | 32 | defined(WOLFSSL_AESNI) |
wolfSSL | 12:1a06964c2adb | 33 | /* Each platform needs to query info type 1 from cpuid to see if aesni is |
wolfSSL | 12:1a06964c2adb | 34 | * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts |
wolfSSL | 12:1a06964c2adb | 35 | */ |
wolfSSL | 12:1a06964c2adb | 36 | |
wolfSSL | 12:1a06964c2adb | 37 | #ifndef _MSC_VER |
wolfSSL | 12:1a06964c2adb | 38 | #define cpuid(reg, leaf, sub)\ |
wolfSSL | 12:1a06964c2adb | 39 | __asm__ __volatile__ ("cpuid":\ |
wolfSSL | 12:1a06964c2adb | 40 | "=a" (reg[0]), "=b" (reg[1]), "=c" (reg[2]), "=d" (reg[3]) :\ |
wolfSSL | 12:1a06964c2adb | 41 | "a" (leaf), "c"(sub)); |
wolfSSL | 12:1a06964c2adb | 42 | |
wolfSSL | 12:1a06964c2adb | 43 | #define XASM_LINK(f) asm(f) |
wolfSSL | 12:1a06964c2adb | 44 | #else |
wolfSSL | 12:1a06964c2adb | 45 | #include <intrin.h> |
wolfSSL | 12:1a06964c2adb | 46 | |
wolfSSL | 12:1a06964c2adb | 47 | #define cpuid(a,b) __cpuid((int*)a,b) |
wolfSSL | 12:1a06964c2adb | 48 | |
wolfSSL | 12:1a06964c2adb | 49 | #define XASM_LINK(f) |
wolfSSL | 12:1a06964c2adb | 50 | #endif /* _MSC_VER */ |
wolfSSL | 12:1a06964c2adb | 51 | |
wolfSSL | 12:1a06964c2adb | 52 | #define EAX 0 |
wolfSSL | 12:1a06964c2adb | 53 | #define EBX 1 |
wolfSSL | 12:1a06964c2adb | 54 | #define ECX 2 |
wolfSSL | 12:1a06964c2adb | 55 | #define EDX 3 |
wolfSSL | 12:1a06964c2adb | 56 | |
wolfSSL | 12:1a06964c2adb | 57 | static word32 cpuid_check = 0; |
wolfSSL | 12:1a06964c2adb | 58 | static word32 cpuid_flags = 0; |
wolfSSL | 12:1a06964c2adb | 59 | |
wolfSSL | 12:1a06964c2adb | 60 | static word32 cpuid_flag(word32 leaf, word32 sub, word32 num, word32 bit) |
wolfSSL | 12:1a06964c2adb | 61 | { |
wolfSSL | 12:1a06964c2adb | 62 | int got_intel_cpu = 0; |
wolfSSL | 12:1a06964c2adb | 63 | unsigned int reg[5]; |
wolfSSL | 12:1a06964c2adb | 64 | |
wolfSSL | 12:1a06964c2adb | 65 | reg[4] = '\0'; |
wolfSSL | 12:1a06964c2adb | 66 | cpuid(reg, 0, 0); |
wolfSSL | 12:1a06964c2adb | 67 | if (XMEMCMP((char *)&(reg[EBX]), "Genu", 4) == 0 && |
wolfSSL | 12:1a06964c2adb | 68 | XMEMCMP((char *)&(reg[EDX]), "ineI", 4) == 0 && |
wolfSSL | 12:1a06964c2adb | 69 | XMEMCMP((char *)&(reg[ECX]), "ntel", 4) == 0) { |
wolfSSL | 12:1a06964c2adb | 70 | got_intel_cpu = 1; |
wolfSSL | 12:1a06964c2adb | 71 | } |
wolfSSL | 12:1a06964c2adb | 72 | if (got_intel_cpu) { |
wolfSSL | 12:1a06964c2adb | 73 | cpuid(reg, leaf, sub); |
wolfSSL | 12:1a06964c2adb | 74 | return ((reg[num] >> bit) & 0x1); |
wolfSSL | 12:1a06964c2adb | 75 | } |
wolfSSL | 12:1a06964c2adb | 76 | return 0; |
wolfSSL | 12:1a06964c2adb | 77 | } |
wolfSSL | 12:1a06964c2adb | 78 | |
wolfSSL | 12:1a06964c2adb | 79 | |
wolfSSL | 12:1a06964c2adb | 80 | void cpuid_set_flags(void) |
wolfSSL | 12:1a06964c2adb | 81 | { |
wolfSSL | 12:1a06964c2adb | 82 | if (!cpuid_check) { |
wolfSSL | 12:1a06964c2adb | 83 | if (cpuid_flag(1, 0, ECX, 28)) { cpuid_flags |= CPUID_AVX1 ; } |
wolfSSL | 12:1a06964c2adb | 84 | if (cpuid_flag(7, 0, EBX, 5)) { cpuid_flags |= CPUID_AVX2 ; } |
wolfSSL | 12:1a06964c2adb | 85 | if (cpuid_flag(7, 0, EBX, 8)) { cpuid_flags |= CPUID_BMI2 ; } |
wolfSSL | 12:1a06964c2adb | 86 | if (cpuid_flag(1, 0, ECX, 30)) { cpuid_flags |= CPUID_RDRAND; } |
wolfSSL | 12:1a06964c2adb | 87 | if (cpuid_flag(7, 0, EBX, 18)) { cpuid_flags |= CPUID_RDSEED; } |
wolfSSL | 12:1a06964c2adb | 88 | if (cpuid_flag(1, 0, ECX, 26)) { cpuid_flags |= CPUID_AESNI ; } |
wolfSSL | 12:1a06964c2adb | 89 | cpuid_check = 1; |
wolfSSL | 12:1a06964c2adb | 90 | } |
wolfSSL | 12:1a06964c2adb | 91 | } |
wolfSSL | 12:1a06964c2adb | 92 | |
wolfSSL | 12:1a06964c2adb | 93 | word32 cpuid_get_flags(void) |
wolfSSL | 12:1a06964c2adb | 94 | { |
wolfSSL | 12:1a06964c2adb | 95 | if (!cpuid_check) |
wolfSSL | 12:1a06964c2adb | 96 | cpuid_set_flags(); |
wolfSSL | 12:1a06964c2adb | 97 | return cpuid_flags; |
wolfSSL | 12:1a06964c2adb | 98 | } |
wolfSSL | 12:1a06964c2adb | 99 | #endif |
wolfSSL | 12:1a06964c2adb | 100 | |
wolfSSL | 12:1a06964c2adb | 101 |