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_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(void) 72 { 73 int passed = 0; 74 ulong cr4 = read_cr4(); 75 struct invpcid_desc desc; 76 desc.rsv = 0; 77 78 /* try executing invpcid when CR4.PCIDE=0, desc.pcid=0 and type=1 79 * no exception expected 80 */ 81 desc.pcid = 0; 82 if (invpcid_checking(1, &desc) != 0) 83 goto report; 84 85 /* try executing invpcid when CR4.PCIDE=0, desc.pcid=1 and type=1 86 * #GP expected 87 */ 88 desc.pcid = 1; 89 if (invpcid_checking(1, &desc) != GP_VECTOR) 90 goto report; 91 92 if (write_cr4_checking(cr4 | X86_CR4_PCIDE) != 0) 93 goto report; 94 95 /* try executing invpcid when CR4.PCIDE=1 96 * no exception expected 97 */ 98 desc.pcid = 10; 99 if (invpcid_checking(2, &desc) != 0) 100 goto report; 101 102 passed = 1; 103 104 report: 105 report(passed, "Test on INVPCID when enabled"); 106 } 107 108 static void test_invpcid_disabled(void) 109 { 110 int passed = 0; 111 struct invpcid_desc desc; 112 113 /* try executing invpcid, #UD expected */ 114 if (invpcid_checking(2, &desc) != UD_VECTOR) 115 goto report; 116 117 passed = 1; 118 119 report: 120 report(passed, "Test on INVPCID when disabled"); 121 } 122 123 int main(int ac, char **av) 124 { 125 int pcid_enabled = 0, invpcid_enabled = 0; 126 127 if (this_cpu_has(X86_FEATURE_PCID)) 128 pcid_enabled = 1; 129 if (this_cpu_has(X86_FEATURE_INVPCID)) 130 invpcid_enabled = 1; 131 132 if (pcid_enabled) 133 test_pcid_enabled(); 134 else 135 test_pcid_disabled(); 136 137 if (invpcid_enabled) 138 test_invpcid_enabled(); 139 else 140 test_invpcid_disabled(); 141 142 return report_summary(); 143 } 144