1 2 #include "x86/msr.h" 3 #include "x86/processor.h" 4 #include "x86/apic-defs.h" 5 #include "x86/apic.h" 6 #include "x86/desc.h" 7 #include "x86/isr.h" 8 #include "alloc.h" 9 10 #include "libcflat.h" 11 #include <stdint.h> 12 13 #define FIXED_CNT_INDEX 32 14 #define PC_VECTOR 32 15 16 #define EVNSEL_EVENT_SHIFT 0 17 #define EVNTSEL_UMASK_SHIFT 8 18 #define EVNTSEL_USR_SHIFT 16 19 #define EVNTSEL_OS_SHIFT 17 20 #define EVNTSEL_EDGE_SHIFT 18 21 #define EVNTSEL_PC_SHIFT 19 22 #define EVNTSEL_INT_SHIFT 20 23 #define EVNTSEL_EN_SHIF 22 24 #define EVNTSEL_INV_SHIF 23 25 #define EVNTSEL_CMASK_SHIFT 24 26 27 #define EVNTSEL_EN (1 << EVNTSEL_EN_SHIF) 28 #define EVNTSEL_USR (1 << EVNTSEL_USR_SHIFT) 29 #define EVNTSEL_OS (1 << EVNTSEL_OS_SHIFT) 30 #define EVNTSEL_PC (1 << EVNTSEL_PC_SHIFT) 31 #define EVNTSEL_INT (1 << EVNTSEL_INT_SHIFT) 32 #define EVNTSEL_INV (1 << EVNTSEL_INV_SHIF) 33 34 #define N 1000000 35 36 typedef struct { 37 uint32_t ctr; 38 uint32_t config; 39 uint64_t count; 40 int idx; 41 } pmu_counter_t; 42 43 union cpuid10_eax { 44 struct { 45 unsigned int version_id:8; 46 unsigned int num_counters:8; 47 unsigned int bit_width:8; 48 unsigned int mask_length:8; 49 } split; 50 unsigned int full; 51 } eax; 52 53 union cpuid10_ebx { 54 struct { 55 unsigned int no_unhalted_core_cycles:1; 56 unsigned int no_instructions_retired:1; 57 unsigned int no_unhalted_reference_cycles:1; 58 unsigned int no_llc_reference:1; 59 unsigned int no_llc_misses:1; 60 unsigned int no_branch_instruction_retired:1; 61 unsigned int no_branch_misses_retired:1; 62 } split; 63 unsigned int full; 64 } ebx; 65 66 union cpuid10_edx { 67 struct { 68 unsigned int num_counters_fixed:5; 69 unsigned int bit_width_fixed:8; 70 unsigned int reserved:19; 71 } split; 72 unsigned int full; 73 } edx; 74 75 struct pmu_event { 76 const char *name; 77 uint32_t unit_sel; 78 int min; 79 int max; 80 } gp_events[] = { 81 {"core cycles", 0x003c, 1*N, 50*N}, 82 {"instructions", 0x00c0, 10*N, 10.2*N}, 83 {"ref cycles", 0x013c, 0.1*N, 30*N}, 84 {"llc refference", 0x4f2e, 1, 2*N}, 85 {"llc misses", 0x412e, 1, 1*N}, 86 {"branches", 0x00c4, 1*N, 1.1*N}, 87 {"branch misses", 0x00c5, 0, 0.1*N}, 88 }, fixed_events[] = { 89 {"fixed 1", MSR_CORE_PERF_FIXED_CTR0, 10*N, 10.2*N}, 90 {"fixed 2", MSR_CORE_PERF_FIXED_CTR0 + 1, 1*N, 30*N}, 91 {"fixed 3", MSR_CORE_PERF_FIXED_CTR0 + 2, 0.1*N, 30*N} 92 }; 93 94 static int num_counters; 95 96 char *buf; 97 98 static inline void loop(void) 99 { 100 unsigned long tmp, tmp2, tmp3; 101 102 asm volatile("1: mov (%1), %2; add $64, %1; nop; nop; nop; nop; nop; nop; nop; loop 1b" 103 : "=c"(tmp), "=r"(tmp2), "=r"(tmp3): "0"(N), "1"(buf)); 104 105 } 106 107 volatile uint64_t irq_received; 108 109 static void cnt_overflow(isr_regs_t *regs) 110 { 111 irq_received++; 112 apic_write(APIC_EOI, 0); 113 } 114 115 static bool check_irq(void) 116 { 117 int i; 118 irq_received = 0; 119 irq_enable(); 120 for (i = 0; i < 100000 && !irq_received; i++) 121 asm volatile("pause"); 122 irq_disable(); 123 return irq_received; 124 } 125 126 static bool is_gp(pmu_counter_t *evt) 127 { 128 return evt->ctr < MSR_CORE_PERF_FIXED_CTR0; 129 } 130 131 static int event_to_global_idx(pmu_counter_t *cnt) 132 { 133 return cnt->ctr - (is_gp(cnt) ? MSR_IA32_PERFCTR0 : 134 (MSR_CORE_PERF_FIXED_CTR0 - FIXED_CNT_INDEX)); 135 } 136 137 static struct pmu_event* get_counter_event(pmu_counter_t *cnt) 138 { 139 if (is_gp(cnt)) { 140 int i; 141 142 for (i = 0; i < sizeof(gp_events)/sizeof(gp_events[0]); i++) 143 if (gp_events[i].unit_sel == (cnt->config & 0xffff)) 144 return &gp_events[i]; 145 } else 146 return &fixed_events[cnt->ctr - MSR_CORE_PERF_FIXED_CTR0]; 147 148 return (void*)0; 149 } 150 151 static void global_enable(pmu_counter_t *cnt) 152 { 153 cnt->idx = event_to_global_idx(cnt); 154 155 wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, rdmsr(MSR_CORE_PERF_GLOBAL_CTRL) | 156 (1ull << cnt->idx)); 157 } 158 159 static void global_disable(pmu_counter_t *cnt) 160 { 161 wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, rdmsr(MSR_CORE_PERF_GLOBAL_CTRL) & 162 ~(1ull << cnt->idx)); 163 } 164 165 166 static void start_event(pmu_counter_t *evt) 167 { 168 wrmsr(evt->ctr, evt->count); 169 if (is_gp(evt)) 170 wrmsr(MSR_P6_EVNTSEL0 + event_to_global_idx(evt), 171 evt->config | EVNTSEL_EN); 172 else { 173 uint32_t ctrl = rdmsr(MSR_CORE_PERF_FIXED_CTR_CTRL); 174 int shift = (evt->ctr - MSR_CORE_PERF_FIXED_CTR0) * 4; 175 uint32_t usrospmi = 0; 176 177 if (evt->config & EVNTSEL_OS) 178 usrospmi |= (1 << 0); 179 if (evt->config & EVNTSEL_USR) 180 usrospmi |= (1 << 1); 181 if (evt->config & EVNTSEL_INT) 182 usrospmi |= (1 << 3); // PMI on overflow 183 ctrl = (ctrl & ~(0xf << shift)) | (usrospmi << shift); 184 wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl); 185 } 186 global_enable(evt); 187 apic_write(APIC_LVTPC, PC_VECTOR); 188 } 189 190 static void stop_event(pmu_counter_t *evt) 191 { 192 global_disable(evt); 193 if (is_gp(evt)) 194 wrmsr(MSR_P6_EVNTSEL0 + event_to_global_idx(evt), 195 evt->config & ~EVNTSEL_EN); 196 else { 197 uint32_t ctrl = rdmsr(MSR_CORE_PERF_FIXED_CTR_CTRL); 198 int shift = (evt->ctr - MSR_CORE_PERF_FIXED_CTR0) * 4; 199 wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl & ~(0xf << shift)); 200 } 201 evt->count = rdmsr(evt->ctr); 202 } 203 204 static void measure(pmu_counter_t *evt, int count) 205 { 206 int i; 207 for (i = 0; i < count; i++) 208 start_event(&evt[i]); 209 loop(); 210 for (i = 0; i < count; i++) 211 stop_event(&evt[i]); 212 } 213 214 static bool verify_event(uint64_t count, struct pmu_event *e) 215 { 216 // printf("%lld >= %lld <= %lld\n", e->min, count, e->max); 217 return count >= e->min && count <= e->max; 218 219 } 220 221 static bool verify_counter(pmu_counter_t *cnt) 222 { 223 return verify_event(cnt->count, get_counter_event(cnt)); 224 } 225 226 static void check_gp_counter(struct pmu_event *evt) 227 { 228 pmu_counter_t cnt = { 229 .ctr = MSR_IA32_PERFCTR0, 230 .config = EVNTSEL_OS | EVNTSEL_USR | evt->unit_sel, 231 }; 232 int i; 233 234 for (i = 0; i < num_counters; i++, cnt.ctr++) { 235 cnt.count = 0; 236 measure(&cnt, 1); 237 report("%s-%d", verify_event(cnt.count, evt), evt->name, i); 238 } 239 } 240 241 static void check_gp_counters(void) 242 { 243 int i; 244 245 for (i = 0; i < sizeof(gp_events)/sizeof(gp_events[0]); i++) 246 if (!(ebx.full & (1 << i))) 247 check_gp_counter(&gp_events[i]); 248 else 249 printf("GP event '%s' is disabled\n", 250 gp_events[i].name); 251 } 252 253 static void check_fixed_counters(void) 254 { 255 pmu_counter_t cnt = { 256 .config = EVNTSEL_OS | EVNTSEL_USR, 257 }; 258 int i; 259 260 for (i = 0; i < edx.split.num_counters_fixed; i++) { 261 cnt.count = 0; 262 cnt.ctr = fixed_events[i].unit_sel; 263 measure(&cnt, 1); 264 report("fixed-%d", verify_event(cnt.count, &fixed_events[i]), i); 265 } 266 } 267 268 static void check_counters_many(void) 269 { 270 pmu_counter_t cnt[10]; 271 int i, n; 272 273 for (i = 0, n = 0; n < num_counters; i++) { 274 if (ebx.full & (1 << i)) 275 continue; 276 277 cnt[n].count = 0; 278 cnt[n].ctr = MSR_IA32_PERFCTR0 + n; 279 cnt[n].config = EVNTSEL_OS | EVNTSEL_USR | 280 gp_events[i % ARRAY_SIZE(gp_events)].unit_sel; 281 n++; 282 } 283 for (i = 0; i < edx.split.num_counters_fixed; i++) { 284 cnt[n].count = 0; 285 cnt[n].ctr = fixed_events[i].unit_sel; 286 cnt[n].config = EVNTSEL_OS | EVNTSEL_USR; 287 n++; 288 } 289 290 measure(cnt, n); 291 292 for (i = 0; i < n; i++) 293 if (!verify_counter(&cnt[i])) 294 break; 295 296 report("all counters", i == n); 297 } 298 299 static void check_counter_overflow(void) 300 { 301 uint64_t count; 302 int i; 303 pmu_counter_t cnt = { 304 .ctr = MSR_IA32_PERFCTR0, 305 .config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */, 306 .count = 0, 307 }; 308 measure(&cnt, 1); 309 count = cnt.count; 310 311 /* clear status before test */ 312 wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL, rdmsr(MSR_CORE_PERF_GLOBAL_STATUS)); 313 314 report_prefix_push("overflow"); 315 316 for (i = 0; i < num_counters + 1; i++, cnt.ctr++) { 317 uint64_t status; 318 int idx; 319 320 cnt.count = 1 - count; 321 322 if (i == num_counters) { 323 cnt.ctr = fixed_events[0].unit_sel; 324 cnt.count &= (1ul << edx.split.bit_width_fixed) - 1; 325 } 326 327 if (i % 2) 328 cnt.config |= EVNTSEL_INT; 329 else 330 cnt.config &= ~EVNTSEL_INT; 331 idx = event_to_global_idx(&cnt); 332 measure(&cnt, 1); 333 report("cntr-%d", cnt.count == 1, i); 334 status = rdmsr(MSR_CORE_PERF_GLOBAL_STATUS); 335 report("status-%d", status & (1ull << idx), i); 336 wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL, status); 337 status = rdmsr(MSR_CORE_PERF_GLOBAL_STATUS); 338 report("status clear-%d", !(status & (1ull << idx)), i); 339 report("irq-%d", check_irq() == (i % 2), i); 340 } 341 342 report_prefix_pop(); 343 } 344 345 static void check_gp_counter_cmask(void) 346 { 347 pmu_counter_t cnt = { 348 .ctr = MSR_IA32_PERFCTR0, 349 .config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */, 350 .count = 0, 351 }; 352 cnt.config |= (0x2 << EVNTSEL_CMASK_SHIFT); 353 measure(&cnt, 1); 354 report("cmask", cnt.count < gp_events[1].min); 355 } 356 357 static void do_rdpmc_fast(void *ptr) 358 { 359 pmu_counter_t *cnt = ptr; 360 uint32_t idx = (uint32_t)cnt->idx | (1u << 31); 361 362 if (!is_gp(cnt)) 363 idx |= 1 << 30; 364 365 cnt->count = rdpmc(idx); 366 } 367 368 369 static void check_rdpmc(void) 370 { 371 uint64_t val = 0x1f3456789ull; 372 bool exc; 373 int i; 374 375 report_prefix_push("rdpmc"); 376 377 for (i = 0; i < num_counters; i++) { 378 uint64_t x; 379 pmu_counter_t cnt = { 380 .ctr = MSR_IA32_PERFCTR0 + i, 381 .idx = i 382 }; 383 384 /* 385 * Only the low 32 bits are writable, and the value is 386 * sign-extended. 387 */ 388 x = (uint64_t)(int64_t)(int32_t)val; 389 390 /* Mask according to the number of supported bits */ 391 x &= (1ull << eax.split.bit_width) - 1; 392 393 wrmsr(MSR_IA32_PERFCTR0 + i, val); 394 report("cntr-%d", rdpmc(i) == x, i); 395 396 exc = test_for_exception(GP_VECTOR, do_rdpmc_fast, &cnt); 397 if (exc) 398 report_skip("fast-%d", i); 399 else 400 report("fast-%d", cnt.count == (u32)val, i); 401 } 402 for (i = 0; i < edx.split.num_counters_fixed; i++) { 403 uint64_t x = val & ((1ull << edx.split.bit_width_fixed) - 1); 404 pmu_counter_t cnt = { 405 .ctr = MSR_CORE_PERF_FIXED_CTR0 + i, 406 .idx = i 407 }; 408 409 wrmsr(MSR_CORE_PERF_FIXED_CTR0 + i, x); 410 report("fixed cntr-%d", rdpmc(i | (1 << 30)) == x, i); 411 412 exc = test_for_exception(GP_VECTOR, do_rdpmc_fast, &cnt); 413 if (exc) 414 report_skip("fixed fast-%d", i); 415 else 416 report("fixed fast-%d", cnt.count == (u32)x, i); 417 } 418 419 report_prefix_pop(); 420 } 421 422 int main(int ac, char **av) 423 { 424 struct cpuid id = cpuid(10); 425 426 setup_vm(); 427 setup_idt(); 428 handle_irq(PC_VECTOR, cnt_overflow); 429 buf = malloc(N*64); 430 431 eax.full = id.a; 432 ebx.full = id.b; 433 edx.full = id.d; 434 435 if (!eax.split.version_id) { 436 printf("No pmu is detected!\n"); 437 return report_summary(); 438 } 439 printf("PMU version: %d\n", eax.split.version_id); 440 printf("GP counters: %d\n", eax.split.num_counters); 441 printf("GP counter width: %d\n", eax.split.bit_width); 442 printf("Mask length: %d\n", eax.split.mask_length); 443 printf("Fixed counters: %d\n", edx.split.num_counters_fixed); 444 printf("Fixed counter width: %d\n", edx.split.bit_width_fixed); 445 446 num_counters = eax.split.num_counters; 447 448 apic_write(APIC_LVTPC, PC_VECTOR); 449 450 check_gp_counters(); 451 check_fixed_counters(); 452 check_rdpmc(); 453 check_counters_many(); 454 check_counter_overflow(); 455 check_gp_counter_cmask(); 456 457 return report_summary(); 458 } 459