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