cpu-intel.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young ([email protected]).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson ([email protected]).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young ([email protected])"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson ([email protected])"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <GFp/cpu.h>
  57. #if !defined(OPENSSL_NO_ASM) && (defined(OPENSSL_X86) || defined(OPENSSL_X86_64))
  58. #if defined(_MSC_VER) && !defined(__clang__)
  59. #pragma warning(push, 3)
  60. #include <immintrin.h>
  61. #include <intrin.h>
  62. #pragma warning(pop)
  63. #endif
  64. #include "internal.h"
  65. // OPENSSL_cpuid runs the cpuid instruction. |leaf| is passed in as EAX and ECX
  66. // is set to zero. It writes EAX, EBX, ECX, and EDX to |*out_eax| through
  67. // |*out_edx|.
  68. static void OPENSSL_cpuid(uint32_t *out_eax, uint32_t *out_ebx,
  69. uint32_t *out_ecx, uint32_t *out_edx, uint32_t leaf) {
  70. #if defined(_MSC_VER) && !defined(__clang__)
  71. int tmp[4];
  72. __cpuid(tmp, (int)leaf);
  73. *out_eax = (uint32_t)tmp[0];
  74. *out_ebx = (uint32_t)tmp[1];
  75. *out_ecx = (uint32_t)tmp[2];
  76. *out_edx = (uint32_t)tmp[3];
  77. #elif defined(__pic__) && defined(OPENSSL_32_BIT)
  78. // Inline assembly may not clobber the PIC register. For 32-bit, this is EBX.
  79. // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47602.
  80. __asm__ volatile (
  81. "xor %%ecx, %%ecx\n"
  82. "mov %%ebx, %%edi\n"
  83. "cpuid\n"
  84. "xchg %%edi, %%ebx\n"
  85. : "=a"(*out_eax), "=D"(*out_ebx), "=c"(*out_ecx), "=d"(*out_edx)
  86. : "a"(leaf)
  87. );
  88. #else
  89. __asm__ volatile (
  90. "xor %%ecx, %%ecx\n"
  91. "cpuid\n"
  92. : "=a"(*out_eax), "=b"(*out_ebx), "=c"(*out_ecx), "=d"(*out_edx)
  93. : "a"(leaf)
  94. );
  95. #endif
  96. }
  97. // OPENSSL_xgetbv returns the value of an Intel Extended Control Register (XCR).
  98. // Currently only XCR0 is defined by Intel so |xcr| should always be zero.
  99. //
  100. // See https://software.intel.com/en-us/articles/how-to-detect-new-instruction-support-in-the-4th-generation-intel-core-processor-family
  101. static uint64_t OPENSSL_xgetbv(uint32_t xcr) {
  102. #if defined(_MSC_VER) && !defined(__clang__)
  103. return (uint64_t)_xgetbv(xcr);
  104. #else
  105. uint32_t eax, edx;
  106. __asm__ volatile ("xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
  107. return (((uint64_t)edx) << 32) | eax;
  108. #endif
  109. }
  110. void GFp_cpuid_setup(void) {
  111. // Determine the vendor and maximum input value.
  112. uint32_t eax, ebx, ecx, edx;
  113. OPENSSL_cpuid(&eax, &ebx, &ecx, &edx, 0);
  114. uint32_t num_ids = eax;
  115. int is_intel = ebx == 0x756e6547 /* Genu */ &&
  116. edx == 0x49656e69 /* ineI */ &&
  117. ecx == 0x6c65746e /* ntel */;
  118. uint32_t extended_features[2] = {0};
  119. if (num_ids >= 7) {
  120. OPENSSL_cpuid(&eax, &ebx, &ecx, &edx, 7);
  121. extended_features[0] = ebx;
  122. extended_features[1] = ecx;
  123. }
  124. OPENSSL_cpuid(&eax, &ebx, &ecx, &edx, 1);
  125. // Force the hyper-threading bit so that the more conservative path is always
  126. // chosen.
  127. edx |= 1u << 28;
  128. // Reserved bit #20 was historically repurposed to control the in-memory
  129. // representation of RC4 state. Always set it to zero.
  130. edx &= ~(1u << 20);
  131. // Reserved bit #30 is repurposed to signal an Intel CPU.
  132. if (is_intel) {
  133. edx |= (1u << 30);
  134. // Clear the XSAVE bit on Knights Landing to mimic Silvermont. This enables
  135. // some Silvermont-specific codepaths which perform better. See OpenSSL
  136. // commit 64d92d74985ebb3d0be58a9718f9e080a14a8e7f.
  137. if ((eax & 0x0fff0ff0) == 0x00050670 /* Knights Landing */ ||
  138. (eax & 0x0fff0ff0) == 0x00080650 /* Knights Mill (per SDE) */) {
  139. ecx &= ~(1u << 26);
  140. }
  141. } else {
  142. edx &= ~(1u << 30);
  143. }
  144. // The SDBG bit is repurposed to denote AMD XOP support. Don't ever use AMD
  145. // XOP code paths.
  146. ecx &= ~(1u << 11);
  147. uint64_t xcr0 = 0;
  148. if (ecx & (1u << 27)) {
  149. // XCR0 may only be queried if the OSXSAVE bit is set.
  150. xcr0 = OPENSSL_xgetbv(0);
  151. }
  152. // See Intel manual, volume 1, section 14.3.
  153. if ((xcr0 & 6) != 6) {
  154. // YMM registers cannot be used.
  155. ecx &= ~(1u << 28); // AVX
  156. ecx &= ~(1u << 12); // FMA
  157. ecx &= ~(1u << 11); // AMD XOP
  158. // Clear AVX2 and AVX512* bits.
  159. //
  160. // TODO(davidben): Should bits 17 and 26-28 also be cleared? Upstream
  161. // doesn't clear those.
  162. extended_features[0] &=
  163. ~((1u << 5) | (1u << 16) | (1u << 21) | (1u << 30) | (1u << 31));
  164. }
  165. // See Intel manual, volume 1, section 15.2.
  166. if ((xcr0 & 0xe6) != 0xe6) {
  167. // Clear AVX512F. Note we don't touch other AVX512 extensions because they
  168. // can be used with YMM.
  169. extended_features[0] &= ~(1u << 16);
  170. }
  171. // Disable ADX instructions on Knights Landing. See OpenSSL commit
  172. // 64d92d74985ebb3d0be58a9718f9e080a14a8e7f.
  173. if ((ecx & (1u << 26)) == 0) {
  174. extended_features[0] &= ~(1u << 19);
  175. }
  176. GFp_ia32cap_P[0] = edx;
  177. GFp_ia32cap_P[1] = ecx;
  178. GFp_ia32cap_P[2] = extended_features[0];
  179. GFp_ia32cap_P[3] = extended_features[1];
  180. }
  181. #endif // !OPENSSL_NO_ASM && (OPENSSL_X86 || OPENSSL_X86_64)