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 u64 pcid : 12; 9 u64 rsv : 52; 10 u64 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_pcid_enabled(void) 30 { 31 int passed = 0; 32 ulong cr0 = read_cr0(), cr3 = read_cr3(), cr4 = read_cr4(); 33 34 /* try setting CR4.PCIDE, no exception expected */ 35 if (write_cr4_checking(cr4 | X86_CR4_PCIDE) != 0) 36 goto report; 37 38 /* try clearing CR0.PG when CR4.PCIDE=1, #GP expected */ 39 if (write_cr0_checking(cr0 & ~X86_CR0_PG) != GP_VECTOR) 40 goto report; 41 42 write_cr4(cr4); 43 44 /* try setting CR4.PCIDE when CR3[11:0] != 0 , #GP expected */ 45 write_cr3(cr3 | 0x001); 46 if (write_cr4_checking(cr4 | X86_CR4_PCIDE) != GP_VECTOR) 47 goto report; 48 write_cr3(cr3); 49 50 passed = 1; 51 52 report: 53 report(passed, "Test on PCID when enabled"); 54 } 55 56 static void test_pcid_disabled(void) 57 { 58 int passed = 0; 59 ulong cr4 = read_cr4(); 60 61 /* try setting CR4.PCIDE, #GP expected */ 62 if (write_cr4_checking(cr4 | X86_CR4_PCIDE) != GP_VECTOR) 63 goto report; 64 65 passed = 1; 66 67 report: 68 report(passed, "Test on PCID when disabled"); 69 } 70 71 static void test_invpcid_enabled(int pcid_enabled) 72 { 73 int passed = 0, i; 74 ulong cr4 = read_cr4(); 75 struct invpcid_desc desc; 76 77 memset(&desc, 0, sizeof(desc)); 78 79 /* try executing invpcid when CR4.PCIDE=0, desc.pcid=0 and type=0..3 80 * no exception expected 81 */ 82 for (i = 0; i < 4; i++) { 83 if (invpcid_checking(i, &desc) != 0) 84 goto report; 85 } 86 87 /* try executing invpcid when CR4.PCIDE=0, desc.pcid=1 and type=0..1 88 * #GP expected 89 */ 90 desc.pcid = 1; 91 for (i = 0; i < 2; i++) { 92 if (invpcid_checking(i, &desc) != GP_VECTOR) 93 goto report; 94 } 95 96 /* Skip tests that require the PCIDE=1 if PCID isn't supported. */ 97 if (!pcid_enabled) 98 goto success; 99 100 if (write_cr4_checking(cr4 | X86_CR4_PCIDE) != 0) 101 goto report; 102 103 /* try executing invpcid when CR4.PCIDE=1 104 * no exception expected 105 */ 106 desc.pcid = 10; 107 if (invpcid_checking(2, &desc) != 0) 108 goto report; 109 110 success: 111 passed = 1; 112 113 report: 114 report(passed, "Test on INVPCID when enabled"); 115 } 116 117 static void test_invpcid_disabled(void) 118 { 119 int passed = 0; 120 struct invpcid_desc desc; 121 122 /* try executing invpcid, #UD expected */ 123 if (invpcid_checking(2, &desc) != UD_VECTOR) 124 goto report; 125 126 passed = 1; 127 128 report: 129 report(passed, "Test on INVPCID when disabled"); 130 } 131 132 int main(int ac, char **av) 133 { 134 int pcid_enabled = 0, invpcid_enabled = 0; 135 136 if (this_cpu_has(X86_FEATURE_PCID)) 137 pcid_enabled = 1; 138 if (this_cpu_has(X86_FEATURE_INVPCID)) 139 invpcid_enabled = 1; 140 141 if (pcid_enabled) 142 test_pcid_enabled(); 143 else 144 test_pcid_disabled(); 145 146 if (invpcid_enabled) 147 test_invpcid_enabled(pcid_enabled); 148 else 149 test_invpcid_disabled(); 150 151 return report_summary(); 152 } 153