cpuinfo-i386.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SPDX-License-Identifier: GPL-2.0-or-later
  3. * Host specific cpu identification for x86.
  4. */
  5. #include "qemu/osdep.h"
  6. #include "host/cpuinfo.h"
  7. #ifdef CONFIG_CPUID_H
  8. # include "qemu/cpuid.h"
  9. #endif
  10. unsigned cpuinfo;
  11. /* Called both as constructor and (possibly) via other constructors. */
  12. unsigned __attribute__((constructor)) cpuinfo_init(void)
  13. {
  14. unsigned info = cpuinfo;
  15. if (info) {
  16. return info;
  17. }
  18. #ifdef CONFIG_CPUID_H
  19. unsigned max, a, b, c, d, b7 = 0, c7 = 0;
  20. max = __get_cpuid_max(0, 0);
  21. if (max >= 7) {
  22. __cpuid_count(7, 0, a, b7, c7, d);
  23. info |= (b7 & bit_BMI ? CPUINFO_BMI1 : 0);
  24. info |= (b7 & bit_BMI2 ? CPUINFO_BMI2 : 0);
  25. }
  26. if (max >= 1) {
  27. __cpuid(1, a, b, c, d);
  28. info |= (d & bit_SSE2 ? CPUINFO_SSE2 : 0);
  29. info |= (c & bit_MOVBE ? CPUINFO_MOVBE : 0);
  30. info |= (c & bit_POPCNT ? CPUINFO_POPCNT : 0);
  31. info |= (c & bit_PCLMUL ? CPUINFO_PCLMUL : 0);
  32. /* Our AES support requires PSHUFB as well. */
  33. info |= ((c & bit_AES) && (c & bit_SSSE3) ? CPUINFO_AES : 0);
  34. /* For AVX features, we must check available and usable. */
  35. if ((c & bit_AVX) && (c & bit_OSXSAVE)) {
  36. unsigned bv = xgetbv_low(0);
  37. if ((bv & 6) == 6) {
  38. info |= CPUINFO_AVX1;
  39. info |= (b7 & bit_AVX2 ? CPUINFO_AVX2 : 0);
  40. if ((bv & 0xe0) == 0xe0) {
  41. info |= (b7 & bit_AVX512F ? CPUINFO_AVX512F : 0);
  42. info |= (b7 & bit_AVX512VL ? CPUINFO_AVX512VL : 0);
  43. info |= (b7 & bit_AVX512BW ? CPUINFO_AVX512BW : 0);
  44. info |= (b7 & bit_AVX512DQ ? CPUINFO_AVX512DQ : 0);
  45. info |= (c7 & bit_AVX512VBMI2 ? CPUINFO_AVX512VBMI2 : 0);
  46. }
  47. /*
  48. * The Intel SDM has added:
  49. * Processors that enumerate support for Intel® AVX
  50. * (by setting the feature flag CPUID.01H:ECX.AVX[bit 28])
  51. * guarantee that the 16-byte memory operations performed
  52. * by the following instructions will always be carried
  53. * out atomically:
  54. * - MOVAPD, MOVAPS, and MOVDQA.
  55. * - VMOVAPD, VMOVAPS, and VMOVDQA when encoded with VEX.128.
  56. * - VMOVAPD, VMOVAPS, VMOVDQA32, and VMOVDQA64 when encoded
  57. * with EVEX.128 and k0 (masking disabled).
  58. * Note that these instructions require the linear addresses
  59. * of their memory operands to be 16-byte aligned.
  60. *
  61. * AMD has provided an even stronger guarantee that processors
  62. * with AVX provide 16-byte atomicity for all cacheable,
  63. * naturally aligned single loads and stores, e.g. MOVDQU.
  64. *
  65. * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104688
  66. */
  67. __cpuid(0, a, b, c, d);
  68. if (c == signature_INTEL_ecx) {
  69. info |= CPUINFO_ATOMIC_VMOVDQA;
  70. } else if (c == signature_AMD_ecx) {
  71. info |= CPUINFO_ATOMIC_VMOVDQA | CPUINFO_ATOMIC_VMOVDQU;
  72. }
  73. }
  74. }
  75. }
  76. max = __get_cpuid_max(0x8000000, 0);
  77. if (max >= 1) {
  78. __cpuid(0x80000001, a, b, c, d);
  79. info |= (c & bit_LZCNT ? CPUINFO_LZCNT : 0);
  80. }
  81. #endif
  82. info |= CPUINFO_ALWAYS;
  83. cpuinfo = info;
  84. return info;
  85. }