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, i; 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=0..3 79 * no exception expected 80 */ 81 desc.pcid = 0; 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 if (write_cr4_checking(cr4 | X86_CR4_PCIDE) != 0) 97 goto report; 98 99 /* try executing invpcid when CR4.PCIDE=1 100 * no exception expected 101 */ 102 desc.pcid = 10; 103 if (invpcid_checking(2, &desc) != 0) 104 goto report; 105 106 passed = 1; 107 108 report: 109 report(passed, "Test on INVPCID when enabled"); 110 } 111 112 static void test_invpcid_disabled(void) 113 { 114 int passed = 0; 115 struct invpcid_desc desc; 116 117 /* try executing invpcid, #UD expected */ 118 if (invpcid_checking(2, &desc) != UD_VECTOR) 119 goto report; 120 121 passed = 1; 122 123 report: 124 report(passed, "Test on INVPCID when disabled"); 125 } 126 127 int main(int ac, char **av) 128 { 129 int pcid_enabled = 0, invpcid_enabled = 0; 130 131 if (this_cpu_has(X86_FEATURE_PCID)) 132 pcid_enabled = 1; 133 if (this_cpu_has(X86_FEATURE_INVPCID)) 134 invpcid_enabled = 1; 135 136 if (pcid_enabled) 137 test_pcid_enabled(); 138 else 139 test_pcid_disabled(); 140 141 if (invpcid_enabled) 142 test_invpcid_enabled(); 143 else 144 test_invpcid_disabled(); 145 146 return report_summary(); 147 } 148