zend_cpuinfo.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Xinchen Hui <xinchen.h@zend.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "zend_cpuinfo.h"
  19. typedef struct _zend_cpu_info {
  20. uint32_t eax;
  21. uint32_t ebx;
  22. uint32_t ecx;
  23. uint32_t edx;
  24. uint32_t initialized;
  25. } zend_cpu_info;
  26. static zend_cpu_info cpuinfo = {0};
  27. #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  28. # if defined(HAVE_CPUID_H) && defined(HAVE_CPUID_COUNT)
  29. # include <cpuid.h>
  30. static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
  31. __cpuid_count(func, subfunc, cpuinfo->eax, cpuinfo->ebx, cpuinfo->ecx, cpuinfo->edx);
  32. }
  33. # else
  34. static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
  35. #if defined(__i386__) && (defined(__pic__) || defined(__PIC__))
  36. /* PIC on i386 uses %ebx, so preserve it. */
  37. __asm__ __volatile__ (
  38. "pushl %%ebx\n"
  39. "cpuid\n"
  40. "mov %%ebx,%1\n"
  41. "popl %%ebx"
  42. : "=a"(cpuinfo->eax), "=r"(cpuinfo->ebx), "=c"(cpuinfo->ecx), "=d"(cpuinfo->edx)
  43. : "a"(func), "c"(subfunc)
  44. );
  45. #else
  46. __asm__ __volatile__ (
  47. "cpuid"
  48. : "=a"(cpuinfo->eax), "=b"(cpuinfo->ebx), "=c"(cpuinfo->ecx), "=d"(cpuinfo->edx)
  49. : "a"(func), "c"(subfunc)
  50. );
  51. #endif
  52. }
  53. # endif
  54. #elif defined(ZEND_WIN32) && !defined(__clang__)
  55. # include <intrin.h>
  56. static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
  57. int regs[4];
  58. __cpuidex(regs, func, subfunc);
  59. cpuinfo->eax = regs[0];
  60. cpuinfo->ebx = regs[1];
  61. cpuinfo->ecx = regs[2];
  62. cpuinfo->edx = regs[3];
  63. }
  64. #else
  65. static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
  66. cpuinfo->eax = 0;
  67. }
  68. #endif
  69. #if defined(__i386__) || defined(__x86_64__)
  70. /* Function based on compiler-rt implementation. */
  71. static unsigned get_xcr0_eax(void) {
  72. # if defined(__GNUC__) || defined(__clang__)
  73. // Check xgetbv; this uses a .byte sequence instead of the instruction
  74. // directly because older assemblers do not include support for xgetbv and
  75. // there is no easy way to conditionally compile based on the assembler used.
  76. unsigned eax, edx;
  77. __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c"(0));
  78. return eax;
  79. # elif defined(ZEND_WIN32) && defined(_XCR_XFEATURE_ENABLED_MASK)
  80. return _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
  81. # else
  82. return 0;
  83. # endif
  84. }
  85. static bool is_avx_supported(void) {
  86. if (!(cpuinfo.ecx & ZEND_CPU_FEATURE_AVX)) {
  87. /* No support for AVX */
  88. return 0;
  89. }
  90. if (!(cpuinfo.ecx & ZEND_CPU_FEATURE_OSXSAVE)) {
  91. /* The operating system does not support XSAVE. */
  92. return 0;
  93. }
  94. if ((get_xcr0_eax() & 0x6) != 0x6) {
  95. /* XCR0 SSE and AVX bits must be set. */
  96. return 0;
  97. }
  98. return 1;
  99. }
  100. #else
  101. static bool is_avx_supported(void) {
  102. return 0;
  103. }
  104. #endif
  105. void zend_cpu_startup(void)
  106. {
  107. if (!cpuinfo.initialized) {
  108. zend_cpu_info ebx;
  109. int max_feature;
  110. cpuinfo.initialized = 1;
  111. __zend_cpuid(0, 0, &cpuinfo);
  112. max_feature = cpuinfo.eax;
  113. if (max_feature == 0) {
  114. return;
  115. }
  116. __zend_cpuid(1, 0, &cpuinfo);
  117. /* for avx2 */
  118. if (max_feature >= 7) {
  119. __zend_cpuid(7, 0, &ebx);
  120. cpuinfo.ebx = ebx.ebx;
  121. } else {
  122. cpuinfo.ebx = 0;
  123. }
  124. if (!is_avx_supported()) {
  125. cpuinfo.edx &= ~ZEND_CPU_FEATURE_AVX;
  126. cpuinfo.ebx &= ~(ZEND_CPU_FEATURE_AVX2 & ~ZEND_CPU_EBX_MASK);
  127. }
  128. }
  129. }
  130. ZEND_API int zend_cpu_supports(zend_cpu_feature feature) {
  131. ZEND_ASSERT(cpuinfo.initialized);
  132. if (feature & ZEND_CPU_EDX_MASK) {
  133. return (cpuinfo.edx & (feature & ~ZEND_CPU_EDX_MASK));
  134. } else if (feature & ZEND_CPU_EBX_MASK) {
  135. return (cpuinfo.ebx & (feature & ~ZEND_CPU_EBX_MASK));
  136. } else {
  137. return (cpuinfo.ecx & feature);
  138. }
  139. }