cpuid.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* cpuid.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL 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 General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/wolfcrypt/cpuid.h>
  26. #if defined(HAVE_CPUID) || defined(HAVE_CPUID_INTEL)
  27. static word32 cpuid_check = 0;
  28. static word32 cpuid_flags = 0;
  29. #endif
  30. #ifdef HAVE_CPUID_INTEL
  31. /* Each platform needs to query info type 1 from cpuid to see if aesni is
  32. * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
  33. */
  34. #ifndef _MSC_VER
  35. #define cpuid(reg, leaf, sub)\
  36. __asm__ __volatile__ ("cpuid":\
  37. "=a" ((reg)[0]), "=b" ((reg)[1]), "=c" ((reg)[2]), "=d" ((reg)[3]) :\
  38. "a" (leaf), "c"(sub));
  39. #define XASM_LINK(f) asm(f)
  40. #else
  41. #include <intrin.h>
  42. #define cpuid(a,b,c) __cpuidex((int*)a,b,c)
  43. #define XASM_LINK(f)
  44. #endif /* _MSC_VER */
  45. #define EAX 0
  46. #define EBX 1
  47. #define ECX 2
  48. #define EDX 3
  49. static word32 cpuid_flag(word32 leaf, word32 sub, word32 num, word32 bit)
  50. {
  51. int got_intel_cpu = 0;
  52. int got_amd_cpu = 0;
  53. unsigned int reg[5];
  54. reg[4] = '\0';
  55. cpuid(reg, 0, 0);
  56. /* check for Intel cpu */
  57. if (XMEMCMP((char *)&(reg[EBX]), "Genu", 4) == 0 &&
  58. XMEMCMP((char *)&(reg[EDX]), "ineI", 4) == 0 &&
  59. XMEMCMP((char *)&(reg[ECX]), "ntel", 4) == 0) {
  60. got_intel_cpu = 1;
  61. }
  62. /* check for AMD cpu */
  63. if (XMEMCMP((char *)&(reg[EBX]), "Auth", 4) == 0 &&
  64. XMEMCMP((char *)&(reg[EDX]), "enti", 4) == 0 &&
  65. XMEMCMP((char *)&(reg[ECX]), "cAMD", 4) == 0) {
  66. got_amd_cpu = 1;
  67. }
  68. if (got_intel_cpu || got_amd_cpu) {
  69. cpuid(reg, leaf, sub);
  70. return ((reg[num] >> bit) & 0x1);
  71. }
  72. return 0;
  73. }
  74. void cpuid_set_flags(void)
  75. {
  76. if (!cpuid_check) {
  77. if (cpuid_flag(1, 0, ECX, 28)) { cpuid_flags |= CPUID_AVX1 ; }
  78. if (cpuid_flag(7, 0, EBX, 5)) { cpuid_flags |= CPUID_AVX2 ; }
  79. if (cpuid_flag(7, 0, EBX, 8)) { cpuid_flags |= CPUID_BMI2 ; }
  80. if (cpuid_flag(1, 0, ECX, 30)) { cpuid_flags |= CPUID_RDRAND; }
  81. if (cpuid_flag(7, 0, EBX, 18)) { cpuid_flags |= CPUID_RDSEED; }
  82. if (cpuid_flag(1, 0, ECX, 25)) { cpuid_flags |= CPUID_AESNI ; }
  83. if (cpuid_flag(7, 0, EBX, 19)) { cpuid_flags |= CPUID_ADX ; }
  84. if (cpuid_flag(1, 0, ECX, 22)) { cpuid_flags |= CPUID_MOVBE ; }
  85. if (cpuid_flag(7, 0, EBX, 3)) { cpuid_flags |= CPUID_BMI1 ; }
  86. cpuid_check = 1;
  87. }
  88. }
  89. #elif defined(HAVE_CPUID)
  90. void cpuid_set_flags(void)
  91. {
  92. if (!cpuid_check) {
  93. cpuid_flags = 0;
  94. cpuid_check = 1;
  95. }
  96. }
  97. #endif
  98. #ifdef HAVE_CPUID
  99. word32 cpuid_get_flags(void)
  100. {
  101. if (!cpuid_check)
  102. cpuid_set_flags();
  103. return cpuid_flags;
  104. }
  105. void cpuid_select_flags(word32 flags)
  106. {
  107. cpuid_flags = flags;
  108. }
  109. void cpuid_set_flag(word32 flag)
  110. {
  111. cpuid_flags |= flag;
  112. }
  113. void cpuid_clear_flag(word32 flag)
  114. {
  115. cpuid_flags &= ~flag;
  116. }
  117. #endif /* HAVE_CPUID */