pcid.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Basic PCID & INVPCID functionality test */
  2. #include "libcflat.h"
  3. #include "processor.h"
  4. #include "desc.h"
  5. #define X86_FEATURE_PCID (1 << 17)
  6. #define X86_FEATURE_INVPCID (1 << 10)
  7. struct invpcid_desc {
  8. unsigned long pcid : 12;
  9. unsigned long rsv : 52;
  10. unsigned long addr : 64;
  11. };
  12. int write_cr0_checking(unsigned long val)
  13. {
  14. asm volatile(ASM_TRY("1f")
  15. "mov %0, %%cr0\n\t"
  16. "1:": : "r" (val));
  17. return exception_vector();
  18. }
  19. int write_cr4_checking(unsigned long val)
  20. {
  21. asm volatile(ASM_TRY("1f")
  22. "mov %0, %%cr4\n\t"
  23. "1:": : "r" (val));
  24. return exception_vector();
  25. }
  26. int invpcid_checking(unsigned long type, void *desc)
  27. {
  28. asm volatile (ASM_TRY("1f")
  29. ".byte 0x66,0x0f,0x38,0x82,0x18 \n\t" /* invpcid (%rax), %rbx */
  30. "1:" : : "a" (desc), "b" (type));
  31. return exception_vector();
  32. }
  33. void test_cpuid_consistency(int pcid_enabled, int invpcid_enabled)
  34. {
  35. int passed = !(!pcid_enabled && invpcid_enabled);
  36. report("CPUID consistency", passed);
  37. }
  38. void test_pcid_enabled(void)
  39. {
  40. int passed = 0;
  41. ulong cr0 = read_cr0(), cr3 = read_cr3(), cr4 = read_cr4();
  42. /* try setting CR4.PCIDE, no exception expected */
  43. if (write_cr4_checking(cr4 | X86_CR4_PCIDE) != 0)
  44. goto report;
  45. /* try clearing CR0.PG when CR4.PCIDE=1, #GP expected */
  46. if (write_cr0_checking(cr0 & ~X86_CR0_PG) != GP_VECTOR)
  47. goto report;
  48. write_cr4(cr4);
  49. /* try setting CR4.PCIDE when CR3[11:0] != 0 , #GP expected */
  50. write_cr3(cr3 | 0x001);
  51. if (write_cr4_checking(cr4 | X86_CR4_PCIDE) != GP_VECTOR)
  52. goto report;
  53. write_cr3(cr3);
  54. passed = 1;
  55. report:
  56. report("Test on PCID when enabled", passed);
  57. }
  58. void test_pcid_disabled(void)
  59. {
  60. int passed = 0;
  61. ulong cr4 = read_cr4();
  62. /* try setting CR4.PCIDE, #GP expected */
  63. if (write_cr4_checking(cr4 | X86_CR4_PCIDE) != GP_VECTOR)
  64. goto report;
  65. passed = 1;
  66. report:
  67. report("Test on PCID when disabled", passed);
  68. }
  69. void test_invpcid_enabled(void)
  70. {
  71. int passed = 0;
  72. ulong cr4 = read_cr4();
  73. struct invpcid_desc desc;
  74. desc.rsv = 0;
  75. /* try executing invpcid when CR4.PCIDE=0, desc.pcid=0 and type=1
  76. * no exception expected
  77. */
  78. desc.pcid = 0;
  79. if (invpcid_checking(1, &desc) != 0)
  80. goto report;
  81. /* try executing invpcid when CR4.PCIDE=0, desc.pcid=1 and type=1
  82. * #GP expected
  83. */
  84. desc.pcid = 1;
  85. if (invpcid_checking(1, &desc) != GP_VECTOR)
  86. goto report;
  87. if (write_cr4_checking(cr4 | X86_CR4_PCIDE) != 0)
  88. goto report;
  89. /* try executing invpcid when CR4.PCIDE=1
  90. * no exception expected
  91. */
  92. desc.pcid = 10;
  93. if (invpcid_checking(2, &desc) != 0)
  94. goto report;
  95. passed = 1;
  96. report:
  97. report("Test on INVPCID when enabled", passed);
  98. }
  99. void test_invpcid_disabled(void)
  100. {
  101. int passed = 0;
  102. struct invpcid_desc desc;
  103. /* try executing invpcid, #UD expected */
  104. if (invpcid_checking(2, &desc) != UD_VECTOR)
  105. goto report;
  106. passed = 1;
  107. report:
  108. report("Test on INVPCID when disabled", passed);
  109. }
  110. int main(int ac, char **av)
  111. {
  112. struct cpuid _cpuid;
  113. int pcid_enabled = 0, invpcid_enabled = 0;
  114. setup_idt();
  115. _cpuid = cpuid(1);
  116. if (_cpuid.c & X86_FEATURE_PCID)
  117. pcid_enabled = 1;
  118. _cpuid = cpuid_indexed(7, 0);
  119. if (_cpuid.b & X86_FEATURE_INVPCID)
  120. invpcid_enabled = 1;
  121. test_cpuid_consistency(pcid_enabled, invpcid_enabled);
  122. if (pcid_enabled)
  123. test_pcid_enabled();
  124. else
  125. test_pcid_disabled();
  126. if (invpcid_enabled)
  127. test_invpcid_enabled();
  128. else
  129. test_invpcid_disabled();
  130. return report_summary();
  131. }