cpuid.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef ARCH_X86_KVM_CPUID_H
  3. #define ARCH_X86_KVM_CPUID_H
  4. #include "x86.h"
  5. #include <asm/cpu.h>
  6. #include <asm/processor.h>
  7. int kvm_update_cpuid(struct kvm_vcpu *vcpu);
  8. bool kvm_mpx_supported(void);
  9. struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
  10. u32 function, u32 index);
  11. int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
  12. struct kvm_cpuid_entry2 __user *entries,
  13. unsigned int type);
  14. int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  15. struct kvm_cpuid *cpuid,
  16. struct kvm_cpuid_entry __user *entries);
  17. int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  18. struct kvm_cpuid2 *cpuid,
  19. struct kvm_cpuid_entry2 __user *entries);
  20. int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
  21. struct kvm_cpuid2 *cpuid,
  22. struct kvm_cpuid_entry2 __user *entries);
  23. bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
  24. u32 *ecx, u32 *edx, bool check_limit);
  25. int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu);
  26. static inline int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
  27. {
  28. return vcpu->arch.maxphyaddr;
  29. }
  30. struct cpuid_reg {
  31. u32 function;
  32. u32 index;
  33. int reg;
  34. };
  35. static const struct cpuid_reg reverse_cpuid[] = {
  36. [CPUID_1_EDX] = { 1, 0, CPUID_EDX},
  37. [CPUID_8000_0001_EDX] = {0x80000001, 0, CPUID_EDX},
  38. [CPUID_8086_0001_EDX] = {0x80860001, 0, CPUID_EDX},
  39. [CPUID_1_ECX] = { 1, 0, CPUID_ECX},
  40. [CPUID_C000_0001_EDX] = {0xc0000001, 0, CPUID_EDX},
  41. [CPUID_8000_0001_ECX] = {0x80000001, 0, CPUID_ECX},
  42. [CPUID_7_0_EBX] = { 7, 0, CPUID_EBX},
  43. [CPUID_D_1_EAX] = { 0xd, 1, CPUID_EAX},
  44. [CPUID_F_0_EDX] = { 0xf, 0, CPUID_EDX},
  45. [CPUID_F_1_EDX] = { 0xf, 1, CPUID_EDX},
  46. [CPUID_8000_0008_EBX] = {0x80000008, 0, CPUID_EBX},
  47. [CPUID_6_EAX] = { 6, 0, CPUID_EAX},
  48. [CPUID_8000_000A_EDX] = {0x8000000a, 0, CPUID_EDX},
  49. [CPUID_7_ECX] = { 7, 0, CPUID_ECX},
  50. [CPUID_8000_0007_EBX] = {0x80000007, 0, CPUID_EBX},
  51. [CPUID_7_EDX] = { 7, 0, CPUID_EDX},
  52. };
  53. static __always_inline struct cpuid_reg x86_feature_cpuid(unsigned x86_feature)
  54. {
  55. unsigned x86_leaf = x86_feature / 32;
  56. BUILD_BUG_ON(x86_leaf >= ARRAY_SIZE(reverse_cpuid));
  57. BUILD_BUG_ON(reverse_cpuid[x86_leaf].function == 0);
  58. return reverse_cpuid[x86_leaf];
  59. }
  60. static __always_inline int *guest_cpuid_get_register(struct kvm_vcpu *vcpu, unsigned x86_feature)
  61. {
  62. struct kvm_cpuid_entry2 *entry;
  63. const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature);
  64. entry = kvm_find_cpuid_entry(vcpu, cpuid.function, cpuid.index);
  65. if (!entry)
  66. return NULL;
  67. switch (cpuid.reg) {
  68. case CPUID_EAX:
  69. return &entry->eax;
  70. case CPUID_EBX:
  71. return &entry->ebx;
  72. case CPUID_ECX:
  73. return &entry->ecx;
  74. case CPUID_EDX:
  75. return &entry->edx;
  76. default:
  77. BUILD_BUG();
  78. return NULL;
  79. }
  80. }
  81. static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu, unsigned x86_feature)
  82. {
  83. int *reg;
  84. if (x86_feature == X86_FEATURE_XSAVE &&
  85. !static_cpu_has(X86_FEATURE_XSAVE))
  86. return false;
  87. reg = guest_cpuid_get_register(vcpu, x86_feature);
  88. if (!reg)
  89. return false;
  90. return *reg & bit(x86_feature);
  91. }
  92. static __always_inline void guest_cpuid_clear(struct kvm_vcpu *vcpu, unsigned x86_feature)
  93. {
  94. int *reg;
  95. reg = guest_cpuid_get_register(vcpu, x86_feature);
  96. if (reg)
  97. *reg &= ~bit(x86_feature);
  98. }
  99. static inline bool guest_cpuid_is_amd(struct kvm_vcpu *vcpu)
  100. {
  101. struct kvm_cpuid_entry2 *best;
  102. best = kvm_find_cpuid_entry(vcpu, 0, 0);
  103. return best && best->ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx;
  104. }
  105. static inline int guest_cpuid_family(struct kvm_vcpu *vcpu)
  106. {
  107. struct kvm_cpuid_entry2 *best;
  108. best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  109. if (!best)
  110. return -1;
  111. return x86_family(best->eax);
  112. }
  113. static inline int guest_cpuid_model(struct kvm_vcpu *vcpu)
  114. {
  115. struct kvm_cpuid_entry2 *best;
  116. best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  117. if (!best)
  118. return -1;
  119. return x86_model(best->eax);
  120. }
  121. static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
  122. {
  123. struct kvm_cpuid_entry2 *best;
  124. best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  125. if (!best)
  126. return -1;
  127. return x86_stepping(best->eax);
  128. }
  129. static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu)
  130. {
  131. return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT;
  132. }
  133. static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)
  134. {
  135. return vcpu->arch.msr_misc_features_enables &
  136. MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
  137. }
  138. #endif